From 900f2bfc30ec38110b08a9ef6b412a768810cbf0 Mon Sep 17 00:00:00 2001 From: Maurice Date: Tue, 17 Nov 2026 14:33:07 +0100 Subject: [PATCH] test(realtime): read each parity case after its notification sends have settled On Postgres a fire-and-forget notification still ran when the case read its recording, so it landed in the next case. The harness now waits for NotificationsService.settled() after each write and each exemption; a late send of any other kind still fails the next case. --- .../integration/broadcast-parity/harness.ts | 8 ++++++++ .../integration/broadcast-parity/run-group.ts | 16 +++++++++++++--- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/server/tests/integration/broadcast-parity/harness.ts b/server/tests/integration/broadcast-parity/harness.ts index 445efdc09..6eae6d9d3 100644 --- a/server/tests/integration/broadcast-parity/harness.ts +++ b/server/tests/integration/broadcast-parity/harness.ts @@ -13,6 +13,7 @@ import { RoadtripVias } from '../../../src/db/entities/RoadtripVias.entity'; import { createLocalMcpServer, McpRegistryService } from '../../../src/nest-mcp'; import { AccommodationsService } from '../../../src/nest/accommodations/accommodations.service'; import { withRequestContext } from '../../../src/nest/database/request-context'; +import { NotificationsService } from '../../../src/nest/notifications/notifications.service'; import { RealtimeService } from '../../../src/nest/realtime/realtime.service'; import { SchoolHolidaysService } from '../../../src/nest/school-holidays/school-holidays.service'; import { authCookie } from '../../helpers/auth'; @@ -209,6 +210,12 @@ export interface ParityHarness { listTools(userId: number): Promise; /** The REST twin, as `userId`, with the session cookie and X-Socket-Id 'sock-rest'. */ rest(userId: number, call: RestCall, s: Scene): Promise<{ status: number; text: string }>; + /** + * Resolves once every notification send started so far has finished. A + * write starts most of them without waiting, and on Postgres one still runs + * after the answer, so the case reads its recording only after this. + */ + notificationsSettled(): Promise; close(): Promise; } @@ -296,6 +303,7 @@ export async function openHarness(): Promise { const res = call.body ? await req.send(call.body(s)) : await req; return { status: res.status, text: res.text }; }, + notificationsSettled: () => app.get(NotificationsService).settled(), close: async () => { vi.restoreAllMocks(); await app.close(); diff --git a/server/tests/integration/broadcast-parity/run-group.ts b/server/tests/integration/broadcast-parity/run-group.ts index a2d1da5be..59d5dda1c 100644 --- a/server/tests/integration/broadcast-parity/run-group.ts +++ b/server/tests/integration/broadcast-parity/run-group.ts @@ -169,6 +169,16 @@ function nothingSentYet(h: ParityHarness, before: string): void { const nextMacrotask = () => new Promise((resolve) => setImmediate(resolve)); +/** + * What a write left running once it answered: a send one macrotask later when + * the case says so, then every notification send it started, so each send is + * read with the case that caused it. + */ +async function writeSettled(h: ParityHarness, settle: boolean | undefined): Promise { + if (settle) await nextMacrotask(); + await h.notificationsSettled(); +} + async function runCase(h: ParityHarness, c: ParityCase): Promise { const restore = c.stub?.(h.app); try { @@ -176,7 +186,7 @@ async function runCase(h: ParityHarness, c: ParityCase): Promise { await c.setup?.(mcpScene, h.app); nothingSentYet(h, `${c.tool} ran over MCP`); const outcome = await h.callTool(actorOf(c, mcpScene).id, c.tool, c.args(mcpScene)); - if (c.settle) await nextMacrotask(); + await writeSettled(h, c.settle); const mcp = h.recorder.take(); expect(outcome.isError, `${c.tool} failed over MCP: ${outcome.text}`).toBe(false); checkSurface('MCP', c, mcp, mcpScene); @@ -186,7 +196,7 @@ async function runCase(h: ParityHarness, c: ParityCase): Promise { await c.setup?.(restScene, h.app); nothingSentYet(h, `the REST twin of ${c.tool} ran`); const res = await h.rest(actorOf(c, restScene).id, c.rest, restScene); - if (c.settle) await nextMacrotask(); + await writeSettled(h, c.settle); const rest = h.recorder.take(); expect(res.status, `the REST twin of ${c.tool} answered ${res.status}: ${res.text}`).toBeGreaterThanOrEqual(200); expect(res.status, `the REST twin of ${c.tool} answered ${res.status}: ${res.text}`).toBeLessThan(300); @@ -208,7 +218,7 @@ async function runExempt(h: ParityHarness, e: ExemptTool): Promise { expect(before.isError, `${e.first.tool}, run before ${e.tool}, failed: ${before.text}`).toBe(false); } const outcome = await h.callTool(actor.id, e.tool, e.args(s)); - await nextMacrotask(); + await writeSettled(h, true); const sent = h.recorder.take(); expect(outcome.isError, `${e.tool} failed over MCP: ${outcome.text}`).toBe(false); expect(sent, `${e.tool} is exempt as silent, yet sent:\n${trace(sent)}`).toEqual([]);