import { describe, test, expect } from "bun:test"; import { statusSorter, availabilitySorter, latencySorter, nameSorter, } from "../../../src/web/constants/target-table-sorters"; import type { TargetStatus } from "../../../src/shared/api"; function makeTarget(overrides: Partial = {}): TargetStatus { return { id: 1, name: "test", type: "http", target: "https://example.com", group: "default", interval: "5s", latestCheck: null, stats: { totalChecks: 0, availability: 100 }, recentSamples: [], ...overrides, }; } describe("statusSorter", () => { test("DOWN 排在 UP 前面", () => { const up = makeTarget({ latestCheck: { timestamp: "", matched: true, durationMs: 10, statusDetail: null, failure: null }, }); const down = makeTarget({ latestCheck: { timestamp: "", matched: false, durationMs: 10, statusDetail: null, failure: null }, }); expect(statusSorter(down, up)).toBeLessThan(0); expect(statusSorter(up, down)).toBeGreaterThan(0); }); test("相同状态返回 0", () => { const a = makeTarget({ latestCheck: { timestamp: "", matched: true, durationMs: 10, statusDetail: null, failure: null }, }); const b = makeTarget({ latestCheck: { timestamp: "", matched: true, durationMs: 20, statusDetail: null, failure: null }, }); expect(statusSorter(a, b)).toBe(0); }); test("无 latestCheck 的目标排在最后", () => { const noCheck = makeTarget(); const up = makeTarget({ latestCheck: { timestamp: "", matched: true, durationMs: 10, statusDetail: null, failure: null }, }); expect(statusSorter(noCheck, up)).toBeGreaterThan(0); }); }); describe("availabilitySorter", () => { test("低可用率排前面", () => { const low = makeTarget({ stats: { totalChecks: 100, availability: 95 } }); const high = makeTarget({ stats: { totalChecks: 100, availability: 99.9 } }); expect(availabilitySorter(low, high)).toBeLessThan(0); }); test("相同可用率返回 0", () => { const a = makeTarget({ stats: { totalChecks: 100, availability: 99.9 } }); const b = makeTarget({ stats: { totalChecks: 50, availability: 99.9 } }); expect(availabilitySorter(a, b)).toBe(0); }); test("无 stats 按 0 处理", () => { const noStats = makeTarget({ stats: undefined as unknown as TargetStatus["stats"] }); const high = makeTarget({ stats: { totalChecks: 100, availability: 99.9 } }); expect(availabilitySorter(noStats, high)).toBeLessThan(0); }); }); describe("latencySorter", () => { test("低延迟排前面", () => { const fast = makeTarget({ latestCheck: { timestamp: "", matched: true, durationMs: 50, statusDetail: null, failure: null }, }); const slow = makeTarget({ latestCheck: { timestamp: "", matched: true, durationMs: 200, statusDetail: null, failure: null }, }); expect(latencySorter(fast, slow)).toBeLessThan(0); }); test("无延迟排最后", () => { const noLatency = makeTarget({ latestCheck: { timestamp: "", matched: true, durationMs: null, statusDetail: null, failure: null }, }); const hasLatency = makeTarget({ latestCheck: { timestamp: "", matched: true, durationMs: 100, statusDetail: null, failure: null }, }); expect(latencySorter(noLatency, hasLatency)).toBeGreaterThan(0); }); }); describe("nameSorter", () => { test("按名称字母排序", () => { const a = makeTarget({ name: "Alpha" }); const b = makeTarget({ name: "Beta" }); expect(nameSorter(a, b)).toBeLessThan(0); }); test("中文名称排序", () => { const a = makeTarget({ name: "百度" }); const b = makeTarget({ name: "谷歌" }); const result = nameSorter(a, b); expect(typeof result).toBe("number"); }); });