import { expect, test, type Page, type Route } from "@playwright/test"; import path from "node:path"; import { fileURLToPath } from "node:url"; const evidenceDir = path.resolve( path.dirname(fileURLToPath(import.meta.url)), "../../../../docs/evidence/M05/ui", ); const MAP_ID = "44444444-4444-4444-8444-444444444444"; function ok(data: unknown): string { return JSON.stringify({ success: true, data, request_id: "pw-m" }); } const columns = [ { source_column_name: "temperature", value_type: "number", is_identity: false, is_observation_time: false, numeric_multiply: null, numeric_offset: null }, ]; const tree = { column: "temperature", operator: "gt", value: 80 }; async function signIn(page: Page): Promise { await page.route("**/auth/me", async (route) => { await route.fulfill({ status: 200, contentType: "application/json", body: ok({ subject: "user-1", display_name: "管理员", roles: ["watchman-admin"] }) }); }); await page.route("**/health", async (route) => { await route.fulfill({ status: 200, contentType: "application/json", body: ok({ status: "ok", app: "watchman", version: "0.1.0" }) }); }); } async function mockApi(page: Page): Promise { await page.route("**/mappings**", async (route: Route) => { const url = new URL(route.request().url()); const pathname = url.pathname; if (!pathname.startsWith("/mappings")) { await route.continue(); return; } if (route.request().method() === "GET" && pathname === "/mappings") { await route.fulfill({ status: 200, contentType: "application/json", body: ok({ items: [{ id: MAP_ID, name: "当前炉温", query: "当前炉温", current_version_no: 1 }], total: 1, page: 1, page_size: 20 }) }); return; } if (route.request().method() === "GET" && pathname === `/mappings/${MAP_ID}`) { await route.fulfill({ status: 200, contentType: "application/json", body: ok({ id: MAP_ID, query_definition_id: "q1", current_version_no: 1 }) }); return; } if (route.request().method() === "GET" && pathname === `/mappings/${MAP_ID}/versions`) { await route.fulfill({ status: 200, contentType: "application/json", body: ok({ items: [{ version_no: 1 }] }) }); return; } if (route.request().method() === "GET" && pathname.startsWith(`/mappings/${MAP_ID}/versions/`)) { await route.fulfill({ status: 200, contentType: "application/json", body: ok({ version_no: 1, columns, condition_tree: tree }) }); return; } if (route.request().method() === "POST" && pathname === `/mappings/${MAP_ID}/versions`) { await route.fulfill({ status: 200, contentType: "application/json", body: ok({ version_no: 2, condition_tree: tree }) }); return; } if (route.request().method() === "POST" && pathname === `/mappings/${MAP_ID}/trial`) { const body = route.request().postDataJSON() as { rows?: { temperature?: number }[] }; const value = body.rows?.[0]?.temperature; const code = value == null ? "unknown" : value > 80 ? "abnormal" : "normal"; const skipped = (body.rows?.length ?? 1) > 1 ? (body.rows?.length ?? 1) - 1 : 0; await route.fulfill({ status: 200, contentType: "application/json", body: ok({ code, outcome: "result", skipped_rows: skipped }) }); return; } await route.fulfill({ status: 404, contentType: "application/json", body: ok({}) }); }); } test("mapping save and trial show the three conclusions and the first-row note", async ({ page }) => { await signIn(page); await mockApi(page); await page.goto("/#/mappings"); await expect(page.getByTestId("nav-mappings")).toBeVisible(); await expect(page.getByRole("button", { name: "判断规则" })).toHaveCount(0); await page.getByTestId("mapping-open").click(); await expect(page.getByText("判断规则")).toBeVisible(); await page.getByTestId("mapping-value-0").fill("80"); await page.getByTestId("mapping-save").click(); await page.getByRole("button", { name: "手填数据" }).click(); await page.getByTestId("mapping-rows").fill(JSON.stringify([{ temperature: 90 }])); await page.getByTestId("mapping-trial").click(); await expect(page.getByTestId("mapping-outcome")).toContainText("异常"); await page.screenshot({ path: path.join(evidenceDir, "trial-abnormal.png"), fullPage: true }); await page.getByTestId("mapping-rows").fill(JSON.stringify([{ temperature: 70 }])); await page.getByTestId("mapping-trial").click(); await expect(page.getByTestId("mapping-outcome")).toContainText("正常"); await page.screenshot({ path: path.join(evidenceDir, "trial-normal.png"), fullPage: true }); await page.getByTestId("mapping-rows").fill(JSON.stringify([{ temperature: null }])); await page.getByTestId("mapping-trial").click(); await expect(page.getByTestId("mapping-outcome")).toContainText("未知"); await page.screenshot({ path: path.join(evidenceDir, "trial-unknown.png"), fullPage: true }); await page.getByTestId("mapping-rows").fill(JSON.stringify([{ temperature: 90 }, { temperature: 70 }])); await page.getByTestId("mapping-trial").click(); await expect(page.getByTestId("mapping-skip")).toContainText("其余 1 行未采用"); await page.screenshot({ path: path.join(evidenceDir, "trial-first-row.png"), fullPage: true }); });