test: migrate workbench fixtures to persistent event streams

Static fulfilled SSE bodies closed immediately and made ordinary UI cases reconnect. Serve persistent fixture streams, retain outage and cursor recovery coverage, and update legacy approval and investigation assertions for current disclosures and SVG diagrams.

Validation: 12 related browser scenarios, 877 unit tests, type checking, scoped ESLint, and diff checks. Unrelated SCADA replay changes remain outside this commit.
This commit is contained in:
2026-11-02 18:56:57 +08:00
parent 0ec74b15b2
commit 78481903d7
7 changed files with 125 additions and 53 deletions
+3 -5
View File
@@ -1,4 +1,5 @@
import type { Page } from "@playwright/test";
import { mockWorkbenchEventStream } from "./mock-workbench-event-stream";
const emptyRuntimeSnapshot = {
availability: "not_configured",
@@ -27,6 +28,7 @@ export async function mockScadaApi(
analysisRuns: Record<string, unknown> = {},
analysisHistory: unknown[] = []
) {
await mockWorkbenchEventStream(page);
const corsHeaders = { "Access-Control-Allow-Origin": "*" };
await page.route("https://tjwater-api.invalid/**", async (route) => {
if (route.request().method() === "OPTIONS") {
@@ -111,11 +113,7 @@ export async function mockScadaApi(
return;
}
if (url.pathname === "/api/v1/workbench/events") {
await route.fulfill({
contentType: "text/event-stream",
headers: { ...corsHeaders, "Cache-Control": "no-cache" },
body: ": test keepalive\n\n"
});
await route.fallback();
return;
}
if (url.pathname === "/api/v1/workbench/analysis-runs") {
@@ -0,0 +1,48 @@
import { createServer, type ServerResponse } from "node:http";
import type { AddressInfo } from "node:net";
import type { Page } from "@playwright/test";
const fixtures = new WeakMap<Page, { cursors: string[] }>();
// route.fulfill() closes an SSE response after its body. UI fixtures need a
// real open response so the application stays live until the page disconnects.
export async function mockWorkbenchEventStream(page: Page) {
const existing = fixtures.get(page);
if (existing) return existing;
const clients = new Set<ServerResponse>();
const cursors: string[] = [];
const server = createServer((request, response) => {
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Headers", "Authorization, Content-Type, Last-Event-ID, X-Project-Id");
response.setHeader("Access-Control-Allow-Methods", "GET, OPTIONS");
if (request.method === "OPTIONS") {
response.writeHead(204).end();
return;
}
cursors.push(new URL(request.url!, "http://localhost").searchParams.get("after") ?? "");
response.writeHead(200, { "Content-Type": "text/event-stream", "Cache-Control": "no-cache" });
response.write(": fixture connected\n\n");
clients.add(response);
response.on("close", () => clients.delete(response));
});
await new Promise<void>((resolve, reject) => {
server.once("error", reject);
server.listen(0, "127.0.0.1", resolve);
});
const { port } = server.address() as AddressInfo;
page.on("close", () => {
for (const client of clients) client.destroy();
server.close();
server.closeAllConnections();
});
await page.route("**/api/v1/workbench/events**", (route) => {
const query = new URL(route.request().url()).search;
return route.fulfill({ status: 307, headers: {
"Access-Control-Allow-Origin": "*",
"Location": `http://127.0.0.1:${port}/events${query}`
} });
});
const fixture = { cursors };
fixtures.set(page, fixture);
return fixture;
}