Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | INFRASTRUCTURE & AUTOMATION4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : MacLustr Command Center (MCC)8 * File : apps/web/e2e/specs/04-realtime.spec.ts9 * Module : web10 * Repository : https://git.spboucher.ai/maclustr-command-center11 * Project Website : https://clustr.spboucher.ai12 * Created : 2026-11-1813 * Last Modified : 2026-11-1814 *15 * Author : Simon-Pierre Boucher16 * Position : Professor of Finance17 * Institution : Université du Québec en Outaouais (UQO)18 * Organization : SP Boucher19 * Website : https://spboucher.ai20 * Git : https://git.spboucher.ai21 *22 * Preferred Contact: admin@spboucher.ai23 * Technical Contact: dev@spboucher.ai24 * Security/Admin : admin@spboucher.ai25 *26 * Copyright (c) 202627 * Simon-Pierre Boucher28 * All rights reserved where applicable. This file is governed by the29 * applicable project license, copyright notices and relevant law.30 * Existing third-party rights and notices remain unaffected.31 *32 * License : None specified (no license file in this repository)33 *34 * Do not embed credentials, private keys, access tokens,35 * certificates or confidential personal information.36 *37 * =============================================================================38 * END OF SP BOUCHER SOURCE HEADER39 * =============================================================================40 */4142import { expect, test } from "@playwright/test";43import { emitEvent } from "../lib/fixtures";44import { restartApi, signedIn, sql } from "../lib/harness";4546test.describe.serial("real-time stream", () => {47 test("an event written by a backend service reaches the page", async ({ browser }) => {48 const { context, page, problems } = await signedIn(browser, "e2e-owner");49 const indicator = page.getByTestId("connection-indicator");50 await expect(indicator).toHaveAttribute("data-state", "live");51 await expect(indicator).toContainText("En direct");52 emitEvent("maintenance.changed", { change: "note_created", note_id: "0e2e0000-0000-7000-8000-00000000aaaa" });53 const seq = sql(`select max(seq) from events_outbox`);54 await expect(indicator).toHaveAttribute("data-last-event-id", seq);55 await expect(indicator).toHaveAttribute("data-last-event-topic", "maintenance.changed");56 // a second one: ids keep increasing (the client will resume from the last one)57 emitEvent("job.changed", { job_id: "0e2e0000-0000-7000-8000-00000000bbbb", state: "running", template: "e2e" });58 await expect(indicator).toHaveAttribute("data-last-event-id", String(Number(seq) + 1));59 problems.assertClean();60 await context.close();61 });6263 test("the connection indicator reacts to a backend restart and events sent meanwhile are replayed", async ({ browser }) => {64 test.setTimeout(120_000);65 const { context, page, problems } = await signedIn(browser, "e2e-owner");66 const indicator = page.getByTestId("connection-indicator");67 await expect(indicator).toHaveAttribute("data-state", "live");68 emitEvent("job.changed", { job_id: "0e2e0000-0000-7000-8000-00000000bbbb", state: "running", template: "e2e" });69 const before = sql(`select max(seq) from events_outbox`);70 await expect(indicator).toHaveAttribute("data-last-event-id", before);7172 const streams: (string | null)[] = [];73 page.on("request", (r) => { if (r.url().endsWith("/api/v1/events")) streams.push(r.headers()["last-event-id"] ?? null); });7475 await restartApi(6000, async () => {76 // while the server is down: the indicator says so, visibly and for assistive technology77 await expect(indicator).toHaveAttribute("data-state", /reconnecting|offline/);78 await expect(indicator).toContainText(/Reconnexion|Hors ligne depuis \d{2}:\d{2}:\d{2}/);79 // an event committed during the outage80 emitEvent("job.changed", { job_id: "0e2e0000-0000-7000-8000-00000000bbbb", state: "succeeded", template: "e2e" });81 });82 await expect(indicator).toHaveAttribute("data-state", "live", { timeout: 60_000 });83 // the reconnect resumed from the last event seen (Last-Event-ID) and received what it missed84 expect(streams.filter((h) => h === before).length).toBeGreaterThan(0);85 await expect(indicator).toHaveAttribute("data-last-event-id", String(Number(before) + 1));86 // the session survived the restart87 expect(await page.evaluate(async () => (await fetch("/api/v1/me")).status)).toBe(200);88 problems.assertClean();89 await context.close();90 });91});92