import { createRequire } from "node:module"; import { randomUUID } from "node:crypto"; import { execFileSync } from "node:child_process"; import { mkdir } from "node:fs/promises"; import { fileURLToPath } from "node:url"; const require = createRequire(process.env.PLAYWRIGHT_REQUIRE_FROM ?? import.meta.url); const { chromium } = require("playwright"); const outputDirectory = new URL("./screenshots/", import.meta.url); const webURL = process.env.VISUAL_WEB_URL ?? "http://localhost:3100"; const webHostPattern = new RegExp(new URL(webURL).host.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")); const oidcURLPattern = new RegExp(process.env.VISUAL_OIDC_URL_PATTERN ?? "localhost:58180"); const username = process.env.VISUAL_USERNAME ?? "owner"; const password = process.env.VISUAL_PASSWORD ?? "LocalOnly123!"; const marker = randomUUID().slice(0, 8); const subjectPublicID = "10000000-0000-0000-0000-000000000411"; const devicePublicID = "10000000-0000-0000-0000-000000000511"; const revisedBackground = `长期生活在杭州并熟悉铁路工作-${marker}`; const revisedGoal = `下一次交流了解第一次独立值班-${marker}`; const targetVolume = 73; await mkdir(outputDirectory, { recursive: true }); function psql(statement) { return execFileSync("docker", ["compose", "exec", "-T", "postgres", "psql", "-v", "ON_ERROR_STOP=1", "-U", "voice", "-d", "voice_platform", "-Atc", statement], { cwd: process.cwd(), encoding: "utf8", stdio: ["ignore", "pipe", "pipe"] }).trim(); } const subjectStateBefore = psql(`SELECT profile_version || '|' || profile_data::text || '|' || conversation_preferences::text FROM subject_profile WHERE public_id='${subjectPublicID}';`); const twinStateBefore = psql(`SELECT dt.desired_revision || '|' || dt.desired_config::text FROM device_twin dt JOIN device d ON d.id=dt.device_id WHERE d.public_id='${devicePublicID}';`); const browser = await chromium.launch({ headless: true, executablePath: process.env.CHROME_PATH ?? "C:/Program Files/Google/Chrome/Application/chrome.exe" }); const page = await browser.newPage({ viewport: { width: 1440, height: 1100 }, deviceScaleFactor: 1 }); const consoleErrors = []; const failedRequests = []; const mutationRequests = []; page.on("console", (message) => { if (message.type() === "error") consoleErrors.push(message.text()); }); page.on("request", (request) => { const pathname = new URL(request.url()).pathname; if (request.method() === "PATCH" && (/^\/api\/v1\/subjects\/[^/]+$/.test(pathname) || /^\/api\/v1\/devices\/[^/]+\/twin$/.test(pathname))) mutationRequests.push(`${request.method()} ${pathname}`); }); page.on("requestfailed", (request) => { const failure = request.failure()?.errorText ?? "failed"; const requestURL = new URL(request.url()); if (request.method() === "GET" && requestURL.pathname === "/api/v1/events" && failure.includes("ERR_ABORTED")) return; failedRequests.push(`${request.method()} ${request.url()} ${failure}`); }); try { await page.goto(webURL, { waitUntil: "networkidle" }); await page.getByRole("button", { name: "登录研发环境" }).click(); await page.waitForURL(oidcURLPattern); await page.locator("#username").fill(username); await page.locator("#password").fill(password); await page.locator("#kc-login").click(); await page.waitForURL(webHostPattern, { timeout: 15000 }); await page.getByRole("heading", { name: "家庭业务工作台" }).waitFor(); const profileForm = page.locator("form.profile-editor"); const profileButton = profileForm.getByRole("button", { name: "核对并保存新版本" }); const unchangedProfileBlocked = await profileButton.isDisabled(); await profileForm.getByLabel("背景资料").fill(revisedBackground); await profileForm.getByLabel("交流目标").fill(revisedGoal); await profileForm.getByLabel("交流节奏").selectOption("normal"); await profileForm.getByLabel("兴趣(逗号分隔)").fill("老照片,铁路往事"); await profileForm.getByLabel("优先话题(逗号分隔)").fill("第一次值班,工作伙伴"); await profileButton.click(); let control = page.getByRole("form", { name: "家庭业务受控操作" }); await control.waitFor(); let text = await control.textContent(); const profileDiffBound = text?.includes(subjectPublicID) === true && text?.includes("当前档案 v1 → 新档案 v2") === true && text?.includes(revisedBackground) === true && text?.includes(revisedGoal) === true && text?.includes("放慢语速") === true && text?.includes("自然语速") === true && text?.includes("老照片、铁路往事") === true && text?.includes("第一次值班、工作伙伴") === true && text?.includes("强ETag") === true; let action = control.getByRole("button", { name: "确认保存档案新版本" }); const profileNeedsAcknowledgement = await action.isDisabled(); await control.getByLabel(/逐项核对从档案v1到v2的变化/).check(); const profileEnabled = await action.isEnabled(); await control.screenshot({ path: fileURLToPath(new URL("family-subject-update-control.png", outputDirectory)) }); await control.getByRole("button", { name: "取消" }).click(); const profileCancelClosed = await control.count() === 0; await page.getByRole("button", { name: "设备管理", exact: true }).click(); const deviceCard = page.locator("article.device-card").filter({ hasText: "测试长者" }).first(); const volumeSlider = deviceCard.getByRole("slider", { name: /扬声器音量/ }); await volumeSlider.fill(String(targetVolume)); await deviceCard.getByRole("button", { name: "保存音量配置" }).click(); control = page.getByRole("form", { name: "家庭业务受控操作" }); await control.waitFor(); text = await control.textContent(); const deviceDiffBound = text?.includes(devicePublicID) === true && text?.includes(subjectPublicID) === true && text?.includes(`未设置 → ${targetVolume}%`) === true && text?.includes("设备上报实际值 未上报") === true && text?.includes("期望配置 v1 → v2") === true && text?.includes("强ETag") === true && text?.includes("等待设备领取和上报") === true; action = control.getByRole("button", { name: "确认下发期望配置" }); const deviceNeedsAcknowledgement = await action.isDisabled(); await control.getByLabel(/把设备扬声器期望值从未设置调整为73%/).check(); const deviceEnabled = await action.isEnabled(); await control.screenshot({ path: fileURLToPath(new URL("family-device-twin-update-control.png", outputDirectory)) }); await control.getByRole("button", { name: "取消" }).click(); const deviceCancelClosed = await control.count() === 0; const subjectUnchanged = psql(`SELECT profile_version || '|' || profile_data::text || '|' || conversation_preferences::text FROM subject_profile WHERE public_id='${subjectPublicID}';`) === subjectStateBefore; const twinUnchanged = psql(`SELECT dt.desired_revision || '|' || dt.desired_config::text FROM device_twin dt JOIN device d ON d.id=dt.device_id WHERE d.public_id='${devicePublicID}';`) === twinStateBefore; const overflow = await page.evaluate(() => [...document.querySelectorAll("body *")] .filter((element) => element instanceof HTMLElement && element.scrollWidth > element.clientWidth + 2 && getComputedStyle(element).overflowX === "visible") .slice(0, 20) .map((element) => `${element.tagName}.${element.className}: ${element.clientWidth}/${element.scrollWidth}`)); const result = { unchangedProfileBlocked, profileDiffBound, profileNeedsAcknowledgement, profileEnabled, profileCancelClosed, deviceDiffBound, deviceNeedsAcknowledgement, deviceEnabled, deviceCancelClosed, subjectUnchanged, twinUnchanged, mutationRequests, overflow, consoleErrors, failedRequests }; console.log(JSON.stringify(result, null, 2)); if (Object.entries(result).some(([key, value]) => !["mutationRequests", "overflow", "consoleErrors", "failedRequests"].includes(key) && value !== true) || mutationRequests.length || overflow.length || consoleErrors.length || failedRequests.length) throw new Error("Family profile/device update controls visual smoke failed"); } finally { await browser.close(); }