|
| 1 | +import { StateOrchestrator, WsLike } from "./StateOrchestrator"; |
| 2 | + |
| 3 | +class FakeWs implements WsLike { |
| 4 | + sent: string[] = []; |
| 5 | + send(message: string): void { |
| 6 | + this.sent.push(message); |
| 7 | + } |
| 8 | + last(): Record<string, unknown> { |
| 9 | + return JSON.parse(this.sent[this.sent.length - 1]); |
| 10 | + } |
| 11 | + byType(type: string): Record<string, unknown>[] { |
| 12 | + return this.sent |
| 13 | + .map((s) => JSON.parse(s)) |
| 14 | + .filter((m) => m.type === type); |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +describe("StateOrchestrator", () => { |
| 19 | + let ws: FakeWs; |
| 20 | + let orch: StateOrchestrator; |
| 21 | + |
| 22 | + beforeEach(() => { |
| 23 | + jest.useFakeTimers(); |
| 24 | + ws = new FakeWs(); |
| 25 | + orch = new StateOrchestrator({ ws, minDebounceMs: 10, maxDebounceMs: 5000 }); |
| 26 | + }); |
| 27 | + |
| 28 | + afterEach(() => { |
| 29 | + orch.dispose(); |
| 30 | + jest.useRealTimers(); |
| 31 | + }); |
| 32 | + |
| 33 | + it("starts with token 0", () => { |
| 34 | + expect(orch.currentToken).toBe(0); |
| 35 | + }); |
| 36 | + |
| 37 | + it("onStateChange bumps token and ships state_change", () => { |
| 38 | + orch.onStateChange({ search_string: "x" }); |
| 39 | + expect(orch.currentToken).toBe(1); |
| 40 | + const msg = ws.last(); |
| 41 | + expect(msg.type).toBe("state_change"); |
| 42 | + expect(msg.state_token).toBe(1); |
| 43 | + expect((msg.new_state as Record<string, unknown>).search_string).toBe("x"); |
| 44 | + }); |
| 45 | + |
| 46 | + it("schedules compute_stat_group after the debounce", () => { |
| 47 | + orch.onStateChange({ search_string: "PIZZA" }); |
| 48 | + // Only the immediate state_change has been sent so far. |
| 49 | + expect(ws.byType("compute_stat_group")).toHaveLength(0); |
| 50 | + |
| 51 | + // Default baseline is 500ms × 2× = 1000ms; clamped to maxDebounceMs=5000. |
| 52 | + jest.advanceTimersByTime(999); |
| 53 | + expect(ws.byType("compute_stat_group")).toHaveLength(0); |
| 54 | + |
| 55 | + jest.advanceTimersByTime(1); |
| 56 | + const reqs = ws.byType("compute_stat_group"); |
| 57 | + expect(reqs).toHaveLength(1); |
| 58 | + expect(reqs[0].scope).toBe("filt"); |
| 59 | + expect(reqs[0].group).toBe("aggregate"); |
| 60 | + expect(reqs[0].state_token).toBe(1); |
| 61 | + }); |
| 62 | + |
| 63 | + it("back-to-back state_changes cancel the previous debounce timer", () => { |
| 64 | + orch.onStateChange({ search_string: "P" }); |
| 65 | + jest.advanceTimersByTime(500); |
| 66 | + orch.onStateChange({ search_string: "PI" }); |
| 67 | + // The first timer would have fired at t=1000ms; the second |
| 68 | + // resets it so at t=999ms from the SECOND call (= 1499 overall) |
| 69 | + // no compute_stat_group has fired yet. |
| 70 | + jest.advanceTimersByTime(998); |
| 71 | + expect(ws.byType("compute_stat_group")).toHaveLength(0); |
| 72 | + |
| 73 | + // The second timer fires. |
| 74 | + jest.advanceTimersByTime(2); |
| 75 | + const reqs = ws.byType("compute_stat_group"); |
| 76 | + expect(reqs).toHaveLength(1); |
| 77 | + expect(reqs[0].state_token).toBe(2); // second state_change's token |
| 78 | + }); |
| 79 | + |
| 80 | + it("rapid typing produces just one aggregate request, with the latest token", () => { |
| 81 | + // Simulate 5 keystrokes at 100ms intervals — typical typing cadence. |
| 82 | + for (let i = 0; i < 5; i++) { |
| 83 | + orch.onStateChange({ search_string: "P".repeat(i + 1) }); |
| 84 | + jest.advanceTimersByTime(100); |
| 85 | + } |
| 86 | + // Default debounce = 1000ms after the last keystroke. No aggregate yet. |
| 87 | + expect(ws.byType("compute_stat_group")).toHaveLength(0); |
| 88 | + |
| 89 | + jest.advanceTimersByTime(1000); |
| 90 | + const reqs = ws.byType("compute_stat_group"); |
| 91 | + // Exactly one aggregate request, with the 5th (final) token. |
| 92 | + expect(reqs).toHaveLength(1); |
| 93 | + expect(reqs[0].state_token).toBe(5); |
| 94 | + }); |
| 95 | + |
| 96 | + it("onStatGroupResult with matching token updates the baseline", () => { |
| 97 | + orch.onStateChange({ search_string: "x" }); |
| 98 | + const applied = orch.onStatGroupResult({ |
| 99 | + type: "stat_group_result", |
| 100 | + state_token: 1, |
| 101 | + scope: "filt", |
| 102 | + group: "aggregate", |
| 103 | + elapsed_ms: 7500, |
| 104 | + }); |
| 105 | + expect(applied).toBe(true); |
| 106 | + |
| 107 | + // Next debounce is 2× 7500 = 15000ms, clamped to maxDebounceMs=5000. |
| 108 | + expect(orch.computeDebounce("filt")).toBe(5000); |
| 109 | + }); |
| 110 | + |
| 111 | + it("onStatGroupResult with stale token is silently dropped", () => { |
| 112 | + orch.onStateChange({ search_string: "x" }); |
| 113 | + orch.onStateChange({ search_string: "xy" }); |
| 114 | + // Token is now 2. |
| 115 | + const applied = orch.onStatGroupResult({ |
| 116 | + type: "stat_group_result", |
| 117 | + state_token: 1, // stale |
| 118 | + scope: "filt", |
| 119 | + group: "aggregate", |
| 120 | + elapsed_ms: 9999, |
| 121 | + }); |
| 122 | + expect(applied).toBe(false); |
| 123 | + // Baseline unchanged — debounce stays at the default. |
| 124 | + expect(orch.computeDebounce("filt")).toBe(1000); |
| 125 | + }); |
| 126 | + |
| 127 | + it("computeDebounce respects minDebounceMs floor", () => { |
| 128 | + const o = new StateOrchestrator({ |
| 129 | + ws: new FakeWs(), |
| 130 | + minDebounceMs: 500, |
| 131 | + maxDebounceMs: 3000, |
| 132 | + multiplier: 2, |
| 133 | + }); |
| 134 | + o.onStatGroupResult({ |
| 135 | + type: "stat_group_result", |
| 136 | + state_token: 0, |
| 137 | + scope: "filt", |
| 138 | + group: "aggregate", |
| 139 | + elapsed_ms: 10, // 2× 10 = 20, well under floor |
| 140 | + }); |
| 141 | + expect(o.computeDebounce("filt")).toBe(500); |
| 142 | + }); |
| 143 | + |
| 144 | + it("computeDebounce respects maxDebounceMs ceiling", () => { |
| 145 | + const o = new StateOrchestrator({ |
| 146 | + ws: new FakeWs(), |
| 147 | + minDebounceMs: 200, |
| 148 | + maxDebounceMs: 3000, |
| 149 | + multiplier: 2, |
| 150 | + }); |
| 151 | + o.onStatGroupResult({ |
| 152 | + type: "stat_group_result", |
| 153 | + state_token: 0, |
| 154 | + scope: "filt", |
| 155 | + group: "aggregate", |
| 156 | + elapsed_ms: 6000, // 2× = 12000, hits ceiling |
| 157 | + }); |
| 158 | + expect(o.computeDebounce("filt")).toBe(3000); |
| 159 | + }); |
| 160 | + |
| 161 | + it("initialAggregateMs seeds the baseline before any observed compute", () => { |
| 162 | + const o = new StateOrchestrator({ |
| 163 | + ws: new FakeWs(), |
| 164 | + initialAggregateMs: { filt: 250 }, |
| 165 | + minDebounceMs: 10, |
| 166 | + maxDebounceMs: 5000, |
| 167 | + multiplier: 2, |
| 168 | + }); |
| 169 | + expect(o.computeDebounce("filt")).toBe(500); // 2× 250 |
| 170 | + // Unrelated scope still uses fallback. |
| 171 | + expect(o.computeDebounce("clean")).toBe(1000); // 2× 500 (default) |
| 172 | + }); |
| 173 | + |
| 174 | + it("dispose cancels all pending aggregate timers", () => { |
| 175 | + orch.onStateChange({ search_string: "x" }); |
| 176 | + orch.dispose(); |
| 177 | + jest.advanceTimersByTime(10_000); |
| 178 | + expect(ws.byType("compute_stat_group")).toHaveLength(0); |
| 179 | + }); |
| 180 | + |
| 181 | + it("scopesForAggregate parameter overrides default ['filt']", () => { |
| 182 | + orch.onStateChange({ search_string: "x" }, { scopesForAggregate: ["filt", "clean"] }); |
| 183 | + jest.advanceTimersByTime(10_000); |
| 184 | + const reqs = ws.byType("compute_stat_group"); |
| 185 | + const scopes = reqs.map((r) => r.scope).sort(); |
| 186 | + expect(scopes).toEqual(["clean", "filt"]); |
| 187 | + }); |
| 188 | +}); |
0 commit comments