mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-20 02:56:10 +00:00
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.
This commit is contained in:
@@ -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<ListedTool[]>;
|
||||
/** 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<void>;
|
||||
close(): Promise<void>;
|
||||
}
|
||||
|
||||
@@ -296,6 +303,7 @@ export async function openHarness(): Promise<ParityHarness> {
|
||||
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();
|
||||
|
||||
@@ -169,6 +169,16 @@ function nothingSentYet(h: ParityHarness, before: string): void {
|
||||
|
||||
const nextMacrotask = () => new Promise<void>((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<void> {
|
||||
if (settle) await nextMacrotask();
|
||||
await h.notificationsSettled();
|
||||
}
|
||||
|
||||
async function runCase(h: ParityHarness, c: ParityCase): Promise<void> {
|
||||
const restore = c.stub?.(h.app);
|
||||
try {
|
||||
@@ -176,7 +186,7 @@ async function runCase(h: ParityHarness, c: ParityCase): Promise<void> {
|
||||
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<void> {
|
||||
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<void> {
|
||||
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([]);
|
||||
|
||||
Reference in New Issue
Block a user