Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import { confirmSettleConverged } from "./settleConvergence";
4
5function harness(readings: ReadonlyArray<boolean | null>) {
6 const waits: number[] = [];
7 let resyncs = 0;
8 let index = 0;
9 return {
10 waits,
11 resyncCount: () => resyncs,
12 options: {
13 readSettled: () => readings[Math.min(index++, readings.length - 1)] as boolean | null,
14 requestResync: () => {
15 resyncs += 1;
16 },
17 delay: async (ms: number) => {
18 waits.push(ms);
19 },
20 delaysMs: [10, 20, 30] as const,
21 },
22 };
23}
24
25describe("confirmSettleConverged", () => {
26 it("stops at the first check when the view already agrees", async () => {
27 const h = harness([true]);
28 await expect(confirmSettleConverged(h.options)).resolves.toBe("converged");
29 expect(h.waits).toEqual([10]);
30 expect(h.resyncCount()).toBe(0);
31 });
32
33 it("keeps checking while the view lags, then converges without a resync", async () => {
34 const h = harness([false, false, true]);
35 await expect(confirmSettleConverged(h.options)).resolves.toBe("converged");
36 expect(h.waits).toEqual([10, 20, 30]);
37 expect(h.resyncCount()).toBe(0);
38 });
39
40 it("requests a resync when the view never reflects the accepted settle", async () => {
41 const h = harness([false, false, false]);
42 await expect(confirmSettleConverged(h.options)).resolves.toBe("resync-requested");
43 expect(h.waits).toEqual([10, 20, 30]);
44 expect(h.resyncCount()).toBe(1);
45 });
46
47 it("leaves a thread that vanished from the shell alone", async () => {
48 const h = harness([null]);
49 await expect(confirmSettleConverged(h.options)).resolves.toBe("thread-absent");
50 expect(h.resyncCount()).toBe(0);
51 });
52});