/** * Demo walkthrough screenshots for M06. Route-mocked so Casdoor is not required. * Output: docs/evidence/M06/demo/ */ import { expect, test, type Page, type Route } from "@playwright/test"; import fs from "node:fs"; import path from "node:path"; import { fileURLToPath } from "node:url"; const demoDir = path.resolve( path.dirname(fileURLToPath(import.meta.url)), "../../../../docs/evidence/M06/demo", ); fs.mkdirSync(demoDir, { recursive: true }); const SUBJECT = "demo-operator"; const OTHER = "demo-lin"; const PENDING = "11111111-1111-4111-8111-111111111111"; const PROCESSING = "22222222-2222-4222-8222-222222222222"; const CLOSED = "33333333-3333-4333-8333-333333333333"; const OBJECT = "aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa"; type Alarm = { id: string; status: string; current_handler: string | null; claimed_at: string | null; closed_at: string | null; close_result: string | null; occurrence_count: number; first_occurred_at: string; last_occurred_at: string; previous_alarm_id: string | null; mapped_input_id: string; identity_key: string; module_key: string; scenario_key: string; scenario_name: string; snapshot_id: string; snapshot_version_no: number; cycle_key: string; event_key: string; opened_at: string; updated_at: string; }; type HistoryItem = { id: string; seq: number; command: string; actor_subject: string; actor_role: string; handler_subject: string; from_status: string; to_status: string; note: string | null; close_result: string | null; evidence_ref: string | null; recorded_at: string; }; type Mock = { alarms: Alarm[]; history: Record; }; function ok(data: unknown): string { return JSON.stringify({ success: true, data, request_id: "demo-alarm" }); } function seedAlarm(overrides: Partial & Pick): Alarm { return { current_handler: null, claimed_at: null, closed_at: null, close_result: null, occurrence_count: 1, first_occurred_at: "2026-11-19T01:00:00.000Z", last_occurred_at: "2026-11-19T02:00:00.000Z", previous_alarm_id: null, mapped_input_id: OBJECT, identity_key: "furnace-01", module_key: "furnace", scenario_key: "furnace-overtemp", snapshot_id: "bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb", snapshot_version_no: 3, cycle_key: "cycle-1", event_key: "event-1", opened_at: "2026-11-19T01:00:00.000Z", updated_at: "2026-11-19T02:00:00.000Z", ...overrides, }; } function seedAlarms(): Alarm[] { return [ seedAlarm({ id: PENDING, status: "pending", scenario_name: "炉温超限", identity_key: "furnace-01" }), seedAlarm({ id: PROCESSING, status: "processing", scenario_name: "气压偏低", identity_key: "comp-02", module_key: "utility", scenario_key: "air-pressure", current_handler: OTHER, claimed_at: "2026-11-19T01:30:00.000Z", mapped_input_id: "cccccccc-cccc-4ccc-8ccc-cccccccccccc", }), seedAlarm({ id: CLOSED, status: "closed", scenario_name: "尺寸超差", identity_key: "part-09", module_key: "quality", scenario_key: "dimension", current_handler: OTHER, claimed_at: "2026-11-19T01:10:00.000Z", closed_at: "2026-11-19T03:00:00.000Z", close_result: "resolved", occurrence_count: 2, mapped_input_id: "dddddddd-dddd-4ddd-8ddd-dddddddddddd", }), ]; } async function signIn(page: Page, roles: string[]): Promise { await page.route( (url) => url.pathname === "/auth/me", async (route) => { await route.fulfill({ status: 200, contentType: "application/json", body: ok({ subject: SUBJECT, display_name: "演示操作员", roles }), }); }, ); } function pushHistory( mock: Mock, alarm: Alarm, command: string, note: string | null, result: string | null = null, ): void { const items = mock.history[alarm.id] ?? []; items.push({ id: `h-${items.length + 1}`, seq: items.length + 1, command, actor_subject: SUBJECT, actor_role: "watchman-operator", handler_subject: alarm.current_handler ?? SUBJECT, from_status: command === "claim" ? "pending" : "processing", to_status: command === "close" ? "closed" : "processing", note, close_result: result, evidence_ref: null, recorded_at: "2026-11-19T05:00:00.000Z", }); mock.history[alarm.id] = items; } async function mockAlarms(page: Page): Promise { const mock: Mock = { alarms: seedAlarms(), history: { [CLOSED]: [ { id: "h1", seq: 1, command: "claim", actor_subject: OTHER, actor_role: "watchman-operator", handler_subject: OTHER, from_status: "pending", to_status: "processing", note: null, close_result: null, evidence_ref: null, recorded_at: "2026-11-19T01:10:00.000Z", }, { id: "h2", seq: 2, command: "close", actor_subject: OTHER, actor_role: "watchman-operator", handler_subject: OTHER, from_status: "processing", to_status: "closed", note: "已核对量具并复测通过", close_result: "resolved", evidence_ref: null, recorded_at: "2026-11-19T03:00:00.000Z", }, ], }, }; const matcher = (url: URL) => /^\/alarms(\/[^/]+(\/(occurrences|evidence|history|claim|progress|close))?)?$/.test(url.pathname); await page.route(matcher, async (route: Route) => { const request = route.request(); const url = new URL(request.url()); const method = request.method(); const parts = url.pathname.split("/").filter(Boolean); const json = (status: number, text: string) => route.fulfill({ status, contentType: "application/json", body: text }); if (parts.length === 1 && method === "GET") { return json( 200, ok({ items: mock.alarms, total: mock.alarms.length, page: 1, page_size: 20 }), ); } const id = parts[1]; const alarm = mock.alarms.find((item) => item.id === id); if (!alarm) { return json(404, ok(null)); } if (parts.length === 2 && method === "GET") { return json(200, ok(alarm)); } const action = parts[2]; if (method === "GET" && action === "occurrences") { return json( 200, ok({ alarm_id: id, items: [ { id: `${id}-occ-0`, mapped_input_revision_id: OBJECT, mapping_version_id: OBJECT, rule_version_id: OBJECT, snapshot_id: alarm.snapshot_id, snapshot_version_no: alarm.snapshot_version_no, module_key: alarm.module_key, scenario_key: alarm.scenario_key, scenario_name: alarm.scenario_name, cycle_key: alarm.cycle_key, event_key: alarm.event_key, observed_at: alarm.first_occurred_at, received_at: alarm.first_occurred_at, created_at: alarm.opened_at, }, ], }), ); } if (method === "GET" && action === "evidence") { return json(200, ok({ alarm_id: id, revision_evidence: [], closed_basis: [] })); } if (method === "GET" && action === "history") { return json(200, ok({ alarm_id: id, items: mock.history[id] ?? [] })); } if (method === "POST" && action === "claim") { alarm.status = "processing"; alarm.current_handler = SUBJECT; alarm.claimed_at = "2026-11-19T02:10:00.000Z"; pushHistory(mock, alarm, "claim", null); return json( 200, ok({ alarm_id: id, event_id: "evt-1", command: "claim", status: alarm.status, current_handler: alarm.current_handler, close_result: null, replayed: false, }), ); } if (method === "POST" && action === "progress") { const body = request.postDataJSON() as { note?: string }; pushHistory(mock, alarm, "progress", body.note ?? null); return json( 200, ok({ alarm_id: id, event_id: "evt-2", command: "progress", status: alarm.status, current_handler: alarm.current_handler, close_result: null, replayed: false, }), ); } if (method === "POST" && action === "close") { const body = request.postDataJSON() as { result?: string; note?: string }; alarm.status = "closed"; alarm.close_result = body.result ?? "resolved"; alarm.closed_at = "2026-11-19T05:00:00.000Z"; pushHistory(mock, alarm, "close", body.note ?? null, alarm.close_result); return json( 200, ok({ alarm_id: id, event_id: "evt-3", command: "close", status: alarm.status, current_handler: alarm.current_handler, close_result: alarm.close_result, replayed: false, }), ); } return json(404, ok(null)); }); return mock; } async function shot(page: Page, name: string): Promise { await page.screenshot({ path: path.join(demoDir, name), fullPage: true }); } test.describe("alarm center demo", () => { test("walk through list, claim, progress, close, history, viewer", async ({ page }) => { await page.setViewportSize({ width: 1440, height: 900 }); await signIn(page, ["watchman-operator"]); await mockAlarms(page); await page.goto("/#/alarms"); await expect(page.getByTestId("alarm-table")).toBeVisible(); await expect(page.getByTestId("alarm-table")).toContainText("炉温超限"); await expect(page.getByTestId("alarm-table")).toContainText("气压偏低"); await expect(page.getByTestId("alarm-table")).toContainText("尺寸超差"); await shot(page, "01-list-three-alarms.png"); await page.getByTestId(`alarm-row-${PENDING}`).getByTestId("alarm-open").click(); await expect(page.getByTestId("alarm-detail-status")).toContainText("待处理"); await shot(page, "02-detail-pending.png"); await page.getByTestId("footer-claim").click(); await expect(page.getByTestId("alarm-detail-status")).toContainText("处理中"); await shot(page, "03-after-claim.png"); await page.getByTestId("footer-progress").click(); await page.getByTestId("progress-note").locator("textarea").fill("已到现场核对温度曲线"); await page.getByTestId("progress-submit").click(); await expect(page.getByTestId("alarm-history")).toContainText("已到现场核对温度曲线"); await shot(page, "04-after-progress.png"); await page.getByTestId("footer-close").click(); await page.getByTestId("close-result").click(); await page.getByRole("option", { name: "已处理" }).click(); await page.getByTestId("close-note").locator("textarea").fill("调整冷却参数后恢复正常"); await page.getByTestId("close-submit").click(); await expect(page.getByTestId("alarm-detail-status")).toContainText("已关闭"); await shot(page, "05-after-close.png"); await page.goto(`/#/alarms/${CLOSED}`); await expect(page.getByTestId("alarm-history")).toContainText("已核对量具并复测通过"); await shot(page, "06-closed-history.png"); }); test("viewer sees detail without handling buttons", async ({ page }) => { await page.setViewportSize({ width: 1440, height: 900 }); await signIn(page, ["watchman-viewer"]); await mockAlarms(page); await page.goto(`/#/alarms/${PENDING}`); await expect(page.getByTestId("alarm-detail")).toBeVisible(); await expect(page.getByTestId("footer-back")).toBeVisible(); await expect(page.getByTestId("footer-claim")).toHaveCount(0); await expect(page.getByTestId("footer-progress")).toHaveCount(0); await expect(page.getByTestId("footer-close")).toHaveCount(0); await shot(page, "07-viewer-no-actions.png"); }); });