|
|
|
|
@@ -1,23 +1,25 @@
|
|
|
|
|
import { describe, test, expect } from "bun:test";
|
|
|
|
|
import { describe, expect, test } from "bun:test";
|
|
|
|
|
|
|
|
|
|
import type { TargetStatus } from "../../../src/shared/api";
|
|
|
|
|
|
|
|
|
|
import {
|
|
|
|
|
statusSorter,
|
|
|
|
|
availabilitySorter,
|
|
|
|
|
latencySorter,
|
|
|
|
|
nameSorter,
|
|
|
|
|
statusSorter,
|
|
|
|
|
} from "../../../src/web/constants/target-table-sorters";
|
|
|
|
|
import type { TargetStatus } from "../../../src/shared/api";
|
|
|
|
|
|
|
|
|
|
function makeTarget(overrides: Partial<TargetStatus> = {}): TargetStatus {
|
|
|
|
|
return {
|
|
|
|
|
id: 1,
|
|
|
|
|
name: "test",
|
|
|
|
|
type: "http",
|
|
|
|
|
target: "https://example.com",
|
|
|
|
|
group: "default",
|
|
|
|
|
id: 1,
|
|
|
|
|
interval: "5s",
|
|
|
|
|
latestCheck: null,
|
|
|
|
|
stats: { totalChecks: 0, availability: 100 },
|
|
|
|
|
name: "test",
|
|
|
|
|
recentSamples: [],
|
|
|
|
|
stats: { availability: 100, totalChecks: 0 },
|
|
|
|
|
target: "https://example.com",
|
|
|
|
|
type: "http",
|
|
|
|
|
...overrides,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
@@ -25,10 +27,10 @@ function makeTarget(overrides: Partial<TargetStatus> = {}): TargetStatus {
|
|
|
|
|
describe("statusSorter", () => {
|
|
|
|
|
test("DOWN 排在 UP 前面", () => {
|
|
|
|
|
const up = makeTarget({
|
|
|
|
|
latestCheck: { timestamp: "", matched: true, durationMs: 10, statusDetail: null, failure: null },
|
|
|
|
|
latestCheck: { durationMs: 10, failure: null, matched: true, statusDetail: null, timestamp: "" },
|
|
|
|
|
});
|
|
|
|
|
const down = makeTarget({
|
|
|
|
|
latestCheck: { timestamp: "", matched: false, durationMs: 10, statusDetail: null, failure: null },
|
|
|
|
|
latestCheck: { durationMs: 10, failure: null, matched: false, statusDetail: null, timestamp: "" },
|
|
|
|
|
});
|
|
|
|
|
expect(statusSorter(down, up)).toBeLessThan(0);
|
|
|
|
|
expect(statusSorter(up, down)).toBeGreaterThan(0);
|
|
|
|
|
@@ -36,10 +38,10 @@ describe("statusSorter", () => {
|
|
|
|
|
|
|
|
|
|
test("相同状态返回 0", () => {
|
|
|
|
|
const a = makeTarget({
|
|
|
|
|
latestCheck: { timestamp: "", matched: true, durationMs: 10, statusDetail: null, failure: null },
|
|
|
|
|
latestCheck: { durationMs: 10, failure: null, matched: true, statusDetail: null, timestamp: "" },
|
|
|
|
|
});
|
|
|
|
|
const b = makeTarget({
|
|
|
|
|
latestCheck: { timestamp: "", matched: true, durationMs: 20, statusDetail: null, failure: null },
|
|
|
|
|
latestCheck: { durationMs: 20, failure: null, matched: true, statusDetail: null, timestamp: "" },
|
|
|
|
|
});
|
|
|
|
|
expect(statusSorter(a, b)).toBe(0);
|
|
|
|
|
});
|
|
|
|
|
@@ -47,7 +49,7 @@ describe("statusSorter", () => {
|
|
|
|
|
test("无 latestCheck 的目标排在最后", () => {
|
|
|
|
|
const noCheck = makeTarget();
|
|
|
|
|
const up = makeTarget({
|
|
|
|
|
latestCheck: { timestamp: "", matched: true, durationMs: 10, statusDetail: null, failure: null },
|
|
|
|
|
latestCheck: { durationMs: 10, failure: null, matched: true, statusDetail: null, timestamp: "" },
|
|
|
|
|
});
|
|
|
|
|
expect(statusSorter(noCheck, up)).toBeGreaterThan(0);
|
|
|
|
|
});
|
|
|
|
|
@@ -55,20 +57,20 @@ describe("statusSorter", () => {
|
|
|
|
|
|
|
|
|
|
describe("availabilitySorter", () => {
|
|
|
|
|
test("低可用率排前面", () => {
|
|
|
|
|
const low = makeTarget({ stats: { totalChecks: 100, availability: 95 } });
|
|
|
|
|
const high = makeTarget({ stats: { totalChecks: 100, availability: 99.9 } });
|
|
|
|
|
const low = makeTarget({ stats: { availability: 95, totalChecks: 100 } });
|
|
|
|
|
const high = makeTarget({ stats: { availability: 99.9, totalChecks: 100 } });
|
|
|
|
|
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 } });
|
|
|
|
|
const a = makeTarget({ stats: { availability: 99.9, totalChecks: 100 } });
|
|
|
|
|
const b = makeTarget({ stats: { availability: 99.9, totalChecks: 50 } });
|
|
|
|
|
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 } });
|
|
|
|
|
const high = makeTarget({ stats: { availability: 99.9, totalChecks: 100 } });
|
|
|
|
|
expect(availabilitySorter(noStats, high)).toBeLessThan(0);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
@@ -76,20 +78,20 @@ describe("availabilitySorter", () => {
|
|
|
|
|
describe("latencySorter", () => {
|
|
|
|
|
test("低延迟排前面", () => {
|
|
|
|
|
const fast = makeTarget({
|
|
|
|
|
latestCheck: { timestamp: "", matched: true, durationMs: 50, statusDetail: null, failure: null },
|
|
|
|
|
latestCheck: { durationMs: 50, failure: null, matched: true, statusDetail: null, timestamp: "" },
|
|
|
|
|
});
|
|
|
|
|
const slow = makeTarget({
|
|
|
|
|
latestCheck: { timestamp: "", matched: true, durationMs: 200, statusDetail: null, failure: null },
|
|
|
|
|
latestCheck: { durationMs: 200, failure: null, matched: true, statusDetail: null, timestamp: "" },
|
|
|
|
|
});
|
|
|
|
|
expect(latencySorter(fast, slow)).toBeLessThan(0);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test("无延迟排最后", () => {
|
|
|
|
|
const noLatency = makeTarget({
|
|
|
|
|
latestCheck: { timestamp: "", matched: true, durationMs: null, statusDetail: null, failure: null },
|
|
|
|
|
latestCheck: { durationMs: null, failure: null, matched: true, statusDetail: null, timestamp: "" },
|
|
|
|
|
});
|
|
|
|
|
const hasLatency = makeTarget({
|
|
|
|
|
latestCheck: { timestamp: "", matched: true, durationMs: 100, statusDetail: null, failure: null },
|
|
|
|
|
latestCheck: { durationMs: 100, failure: null, matched: true, statusDetail: null, timestamp: "" },
|
|
|
|
|
});
|
|
|
|
|
expect(latencySorter(noLatency, hasLatency)).toBeGreaterThan(0);
|
|
|
|
|
});
|
|
|
|
|
|