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 householdPublicID = "10000000-0000-0000-0000-000000000211"; const subjectPublicID = "10000000-0000-0000-0000-000000000411"; const subjectName = `测试长者-${marker}`; const background = `长期居住在杭州,喜欢与家人回忆往事-${marker}`; const goal = `了解第一次参加工作的经历-${marker}`; const factContent = `近期需要提醒放慢说话速度-${marker}`; 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 subjectCountBefore = Number(psql("SELECT count(*) FROM subject_profile;")); const factCountBefore = Number(psql("SELECT count(*) FROM subject_fact;")); 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() === "POST" && (/^\/api\/v1\/households\/[^/]+\/subjects$/.test(pathname) || /^\/api\/v1\/subjects\/[^/]+\/facts$/.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 subjectForm = page.locator("form.subject-create-form"); await subjectForm.getByLabel("称呼").fill(subjectName); await subjectForm.getByLabel("交流节奏").selectOption("slow"); await subjectForm.getByLabel("背景资料").fill(background); await subjectForm.getByLabel("本次交流目标").fill(goal); await subjectForm.getByLabel("兴趣(逗号分隔)").fill("老照片,家乡变化"); await subjectForm.getByLabel("优先话题(逗号分隔)").fill("工作经历,家庭往事"); await subjectForm.getByRole("button", { name: "创建档案" }).click(); let control = page.getByRole("form", { name: "家庭业务受控操作" }); await control.waitFor(); let text = await control.textContent(); const subjectTargetBound = text?.includes(householdPublicID) === true && text?.includes(subjectName) === true && text?.includes(background) === true && text?.includes(goal) === true && text?.includes("老照片、家乡变化") === true && text?.includes("工作经历、家庭往事") === true && text?.includes("授权、设备绑定和通话需分别完成") === true; let action = control.getByRole("button", { name: "确认创建长期档案" }); const subjectNeedsAcknowledgement = await action.isDisabled(); await control.getByLabel(/确认这些个人资料可以作为长期档案保存/).check(); const subjectEnabled = await action.isEnabled(); await control.screenshot({ path: fileURLToPath(new URL("family-subject-create-control.png", outputDirectory)) }); await control.getByRole("button", { name: "取消" }).click(); const subjectCancelClosed = await control.count() === 0; const factForm = page.locator("form.tool-form").filter({ hasText: "登记稳定事实" }); await factForm.getByLabel("事实类型").selectOption("health_context"); const healthDefaultsSensitive = await factForm.getByLabel("敏感等级").inputValue() === "sensitive"; await factForm.getByLabel("事实内容").fill(factContent); await factForm.getByRole("button", { name: "登记候选" }).click(); control = page.getByRole("form", { name: "家庭业务受控操作" }); await control.waitFor(); text = await control.textContent(); const factTargetBound = text?.includes("测试长者") === true && text?.includes(subjectPublicID) === true && text?.includes("健康背景") === true && text?.includes(factContent) === true && text?.includes("家庭成员录入") === true && text?.includes("敏感") === true && text?.includes("待核对") === true && text?.includes("不会自动确认、创建记忆或改写历史通话") === true; action = control.getByRole("button", { name: "确认登记待核对事实" }); const factNeedsAcknowledgement = await action.isDisabled(); await control.getByLabel(/类型和敏感等级已正确标注/).check(); const factEnabled = await action.isEnabled(); await control.screenshot({ path: fileURLToPath(new URL("family-fact-create-control.png", outputDirectory)) }); await control.getByRole("button", { name: "取消" }).click(); const factCancelClosed = await control.count() === 0; const subjectsUnchanged = Number(psql("SELECT count(*) FROM subject_profile;")) === subjectCountBefore; const factsUnchanged = Number(psql("SELECT count(*) FROM subject_fact;")) === factCountBefore; 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 = { subjectTargetBound, subjectNeedsAcknowledgement, subjectEnabled, subjectCancelClosed, healthDefaultsSensitive, factTargetBound, factNeedsAcknowledgement, factEnabled, factCancelClosed, subjectsUnchanged, factsUnchanged, 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 personal-data create controls visual smoke failed"); } finally { await browser.close(); }