fix(webui): connection-diff-loop compares ONLY level — strip detail timestamps
Adversarial review caught: HttpPostAdapter.describeStatus() embeds
HH:MM:SS in 'detail' (e.g. "上次推送 14:53:01"). Every webhook delivery
rotates that string every second under any active push — and the diff
loop's raw JSON.stringify compared everything, so it published on every
500ms tick under any non-idle webhook. Net effect: SSE pushed a full
connections frame at 2 Hz, exactly the polling the loop was meant to
replace, just on the push channel.
Add an optional pickComparable projector. server.ts caller passes one
that returns { uin, nickname, adapters[{ name, kind, status }] } — the
LEVEL only, not the live-rendered detail string. The SSE handler still
ships the FULL snapshot (including detail) to the WebUI; only the diff
key is pruned.
2 new tests pin the invariant: 10 detail-only ticks → ZERO publishes; a
real status-level transition publishes exactly once. Plus a throwing-
projector isolation test. 7/7 diff-loop tests green.
This commit is contained in:
4 files changed
+96
-3
No files matched your search
@@ -84,6 +84,63 @@ describe('startConnectionDiffLoop', () => {
|
||||
expect(seen).toEqual([]);
|
||||
});
|
||||
|
||||
it('pickComparable() strips volatile fields so detail-only changes do NOT publish', () => {
|
||||
const bus = new StateBus();
|
||||
const seen: string[] = [];
|
||||
bus.subscribe((r) => seen.push(r));
|
||||
// Reproduces the real-world bug: HttpPostAdapter.describeStatus()
|
||||
// embeds HH:MM:SS in `detail`; every webhook delivery rotates that
|
||||
// string every second. WITHOUT pickComparable, the loop would publish
|
||||
// every tick under any active webhook. WITH it (stripping `detail`),
|
||||
// only level changes publish.
|
||||
let snap = [{ uin: '1', adapters: [{ kind: 'httpClient', status: 'ok', detail: '上次推送 14:00:00' }] }];
|
||||
const handle = startConnectionDiffLoop({
|
||||
bus,
|
||||
getSnapshot: () => snap,
|
||||
pickComparable: (s) => {
|
||||
if (!Array.isArray(s)) return s;
|
||||
return (s as Array<{ uin: string; adapters?: unknown[] }>).map((acc) => ({
|
||||
uin: acc.uin,
|
||||
adapters: Array.isArray(acc.adapters)
|
||||
? acc.adapters.map((a: unknown) => {
|
||||
const o = a as { name?: string; kind?: string; status?: string };
|
||||
return { name: o.name, kind: o.kind, status: o.status };
|
||||
})
|
||||
: acc.adapters,
|
||||
}));
|
||||
},
|
||||
intervalMs: 500,
|
||||
});
|
||||
vi.advanceTimersByTime(500); // baseline
|
||||
expect(seen).toEqual([]);
|
||||
// detail changes every second; raw JSON differs each tick.
|
||||
for (let t = 1; t <= 10; t++) {
|
||||
snap = [{ uin: '1', adapters: [{ kind: 'httpClient', status: 'ok', detail: `上次推送 14:00:${String(t).padStart(2, '0')}` }] }];
|
||||
vi.advanceTimersByTime(500);
|
||||
}
|
||||
expect(seen).toEqual([]); // ZERO publishes — detail is not comparable
|
||||
// Now a REAL state change (status level) → publish exactly once.
|
||||
snap = [{ uin: '1', adapters: [{ kind: 'httpClient', status: 'down', detail: '连接失败' }] }];
|
||||
vi.advanceTimersByTime(500);
|
||||
expect(seen).toEqual(['connections']);
|
||||
handle.dispose();
|
||||
});
|
||||
|
||||
it('a throwing pickComparable() is isolated — loop keeps ticking, no publish', () => {
|
||||
const bus = new StateBus();
|
||||
const seen: string[] = [];
|
||||
bus.subscribe((r) => seen.push(r));
|
||||
const handle = startConnectionDiffLoop({
|
||||
bus,
|
||||
getSnapshot: () => [{ uin: '1' }],
|
||||
pickComparable: () => { throw new Error('projector boom'); },
|
||||
intervalMs: 500,
|
||||
});
|
||||
vi.advanceTimersByTime(2_000);
|
||||
expect(seen).toEqual([]);
|
||||
handle.dispose();
|
||||
});
|
||||
|
||||
it('survives a throwing snapshot() — keeps trying on the next tick', () => {
|
||||
const bus = new StateBus();
|
||||
const seen: string[] = [];
|
||||
|
||||
Reference in new issue
Block a user