/** * Shared helpers for console UI smoke / layout tests. * Auth is mocked; APIs return synthetic envelopes. Login page is out of scope. */ import { expect, type Locator, type Page } from "@playwright/test"; export function ok(data: unknown, requestId = "ui-smoke"): string { return JSON.stringify({ success: true, data, request_id: requestId }); } /** Signed-in watchman-admin via /auth/me mock. */ export async function signedInAdmin(page: Page): Promise { await page.route("**/auth/me", async (route) => { await route.fulfill({ status: 200, contentType: "application/json", body: ok({ subject: "admin-1", display_name: "Admin", roles: ["watchman-admin"], }), }); }); } export async function mockHealth(page: Page): Promise { await page.route("**/health", async (route) => { await route.fulfill({ status: 200, contentType: "application/json", body: ok({ status: "ok", app: "watchman", version: "0.1.0" }), }); }); } export async function mockDatasourceList(page: Page): Promise { await page.route("**/datasources?**", async (route) => { await route.fulfill({ status: 200, contentType: "application/json", body: ok({ items: [ { id: "ds-1", name: "MES-PG", engine: "postgresql", host: "127.0.0.1", port: 5432, database_name: "mes", username: "ro", enabled: true, circuit_state: "closed", last_test_at: null, last_test_category: null, }, ], total: 1, page: 1, page_size: 20, }), }); }); } export async function mockQueryList(page: Page): Promise { await page.route("**/datasources?**", async (route) => { await route.fulfill({ status: 200, contentType: "application/json", body: ok({ items: [], total: 0, page: 1, page_size: 100 }), }); }); await page.route("**/queries?**", async (route) => { await route.fulfill({ status: 200, contentType: "application/json", body: ok({ items: [ { id: "q-1", name: "炉温查询", datasource_id: "ds-1", enabled: true, current_version_no: 2, updated_at: "2024-11-18T10:00:00Z", }, ], total: 1, page: 1, page_size: 20, }), }); }); } export async function mockMappingList(page: Page): Promise { await page.route("**/mappings**", async (route) => { const pathname = new URL(route.request().url()).pathname; if (!pathname.startsWith("/mappings") || route.request().method() !== "GET") { await route.continue(); return; } await route.fulfill({ status: 200, contentType: "application/json", body: ok({ items: [{ id: "m-1", name: "炉温映射", query: "炉温查询", current_version_no: 1 }], total: 1, page: 1, page_size: 20, }), }); }); } export async function mockAlarmList(page: Page): Promise { await page.route("**/alarms?**", async (route) => { await route.fulfill({ status: 200, contentType: "application/json", body: ok({ items: [ { id: "a-1", module_key: "furnace", scenario_key: "temp", scenario_name: "炉温超限", identity_key: "F1", status: "pending", close_result: null, last_occurred_at: "2024-11-18T12:00:00Z", }, ], total: 1, page: 1, page_size: 20, }), }); }); } /** Read a CSS custom property from :root. */ export async function cssVar(page: Page, name: string): Promise { return page.locator(":root").evaluate((el, prop) => getComputedStyle(el).getPropertyValue(prop).trim(), name); } export async function computed(locator: Locator, prop: keyof CSSStyleDeclaration): Promise { return locator.evaluate((el, p) => { const style = getComputedStyle(el); return String(style[p as keyof CSSStyleDeclaration] ?? ""); }, prop); } /** Assert list page card skeleton (P1): surface card + 32px filter gutters + right pager. */ export async function expectListPageLayout(page: Page, listTestId: string): Promise { const card = page.getByTestId(listTestId); await expect(card).toBeVisible(); await expect(card).toHaveClass(/wm-list-page/); const radius = await computed(card, "borderRadius"); expect(radius).toMatch(/16px/); const filters = card.locator(".wm-list-page__filters"); await expect(filters).toBeVisible(); const padLeft = await computed(filters, "paddingLeft"); const padRight = await computed(filters, "paddingRight"); expect(padLeft).toBe("32px"); expect(padRight).toBe("32px"); const pager = card.locator(".wm-list-page__pager"); if ((await pager.count()) > 0) { await expect(pager).toBeVisible(); // Chromium may serialize flex-end as flow-end. expect(await computed(pager, "justifyContent")).toMatch(/^(flex-end|end|flow-end)$/); expect(await computed(pager, "paddingLeft")).toBe("32px"); expect(await computed(pager, "paddingRight")).toBe("32px"); } } /** Assert form/detail card: scroll body + centered sticky footer. */ export async function expectFormPageLayout( page: Page, opts: { pageTestId: string; footerTestId: string; bodyTestId?: string }, ): Promise { const card = page.getByTestId(opts.pageTestId); await expect(card).toBeVisible(); await expect(card).toHaveClass(/wm-form-page/); expect(await computed(card, "borderRadius")).toMatch(/16px/); if (opts.bodyTestId) { const body = page.getByTestId(opts.bodyTestId); await expect(body).toBeVisible(); expect(await computed(body, "overflow")).toMatch(/auto|scroll/); expect(await computed(body, "paddingLeft")).toBe("32px"); } const footer = page.getByTestId(opts.footerTestId); await expect(footer).toBeVisible(); expect(await computed(footer, "justifyContent")).toMatch(/^(center|flex-center)$/); expect(await computed(footer, "paddingLeft")).toBe("32px"); } /** Shell chrome: brand nav, breadcrumb bar, padded main stage. */ export async function expectShellChrome(page: Page): Promise { await expect(page.getByTestId("side-nav")).toBeVisible(); await expect(page.getByTestId("side-brand")).toBeVisible(); await expect(page.getByTestId("breadcrumb")).toBeVisible(); await expect(page.getByTestId("side-user")).toBeVisible(); await expect(page.getByTestId("logout")).toBeVisible(); // AppShell content column: header + main stage with page padding. const shellMain = page.locator("div.flex.min-w-0.flex-1.flex-col > main"); await expect(shellMain).toBeVisible(); expect(await computed(shellMain, "paddingTop")).toBe("16px"); expect(await computed(shellMain, "paddingLeft")).toBe("16px"); }