|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { compareByStarted, compareByWorker } from "./liveSort"; |
| 3 | + |
| 4 | +describe("compareByStarted", () => { |
| 5 | + it("orders oldest start first", () => { |
| 6 | + const rows = [ |
| 7 | + { started_at: "2026-07-01T10:00:02Z", worker_id: 2 }, |
| 8 | + { started_at: "2026-07-01T10:00:01Z", worker_id: 1 }, |
| 9 | + { started_at: "2026-07-01T10:00:03Z", worker_id: 3 }, |
| 10 | + ]; |
| 11 | + expect([...rows].sort(compareByStarted).map((r) => r.worker_id)).toEqual([1, 2, 3]); |
| 12 | + }); |
| 13 | + |
| 14 | + it("is stable across reshuffled input (kills flicker)", () => { |
| 15 | + const a = { started_at: "2026-07-01T10:00:01Z", worker_id: 5 }; |
| 16 | + const b = { started_at: "2026-07-01T10:00:02Z", worker_id: 9 }; |
| 17 | + const c = { started_at: "2026-07-01T10:00:03Z", worker_id: 1 }; |
| 18 | + // Any input permutation yields the same output order. |
| 19 | + const want = [5, 9, 1]; |
| 20 | + expect([a, b, c].sort(compareByStarted).map((r) => r.worker_id)).toEqual(want); |
| 21 | + expect([c, a, b].sort(compareByStarted).map((r) => r.worker_id)).toEqual(want); |
| 22 | + expect([b, c, a].sort(compareByStarted).map((r) => r.worker_id)).toEqual(want); |
| 23 | + }); |
| 24 | + |
| 25 | + it("breaks ties on worker id so equal timestamps never reorder", () => { |
| 26 | + const t = "2026-07-01T10:00:00Z"; |
| 27 | + const rows = [ |
| 28 | + { started_at: t, worker_id: 30 }, |
| 29 | + { started_at: t, worker_id: 10 }, |
| 30 | + { started_at: t, worker_id: 20 }, |
| 31 | + ]; |
| 32 | + expect([...rows].sort(compareByStarted).map((r) => r.worker_id)).toEqual([10, 20, 30]); |
| 33 | + }); |
| 34 | + |
| 35 | + it("sorts missing timestamps last (still deterministic)", () => { |
| 36 | + const rows = [ |
| 37 | + { worker_id: 7 }, |
| 38 | + { started_at: "2026-07-01T10:00:01Z", worker_id: 2 }, |
| 39 | + { worker_id: 3 }, |
| 40 | + ]; |
| 41 | + expect([...rows].sort(compareByStarted).map((r) => r.worker_id)).toEqual([2, 3, 7]); |
| 42 | + }); |
| 43 | +}); |
| 44 | + |
| 45 | +describe("compareByWorker", () => { |
| 46 | + it("orders by cluster-unique worker id", () => { |
| 47 | + const rows = [{ worker_id: 30 }, { worker_id: 10 }, { worker_id: 20 }]; |
| 48 | + expect([...rows].sort(compareByWorker).map((r) => r.worker_id)).toEqual([10, 20, 30]); |
| 49 | + }); |
| 50 | +}); |
0 commit comments