import { createRequire } from "node:module"; import { createHash, 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 memoryID = randomUUID(); const memoryPublicID = randomUUID(); const versionID = randomUUID(); const versionPublicID = randomUUID(); const indexID = randomUUID(); const indexPublicID = randomUUID(); const content = `浏览器记忆管理夹具 ${memoryPublicID.slice(0, 8)}`; const digest = createHash("sha256").update(content).digest("hex"); 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(); } function cleanup() { psql(`BEGIN; DELETE FROM memory_index_entry WHERE id='${indexID}'::uuid; UPDATE curated_memory SET current_version_id=NULL WHERE id='${memoryID}'::uuid; DELETE FROM memory_version WHERE id='${versionID}'::uuid; DELETE FROM curated_memory WHERE id='${memoryID}'::uuid; COMMIT;`); } psql(`BEGIN; INSERT INTO curated_memory(id,public_id,tenant_id,subject_id,status,sensitivity,valid_from) VALUES('${memoryID}'::uuid,'${memoryPublicID}'::uuid,'10000000-0000-0000-0000-000000000001'::uuid,'10000000-0000-0000-0000-000000000401'::uuid,'ACTIVE','normal',now()); INSERT INTO memory_version(id,public_id,memory_id,version_no,content,source_refs,confirmed_by,change_reason,content_digest) VALUES('${versionID}'::uuid,'${versionPublicID}'::uuid,'${memoryID}'::uuid,1,'${content}', '[{"source":"visual-memory-management"}]'::jsonb,'10000000-0000-0000-0000-000000000101'::uuid,'浏览器页面可用性检查','${digest}'); UPDATE curated_memory SET current_version_id='${versionID}'::uuid WHERE id='${memoryID}'::uuid; INSERT INTO memory_index_entry(id,public_id,tenant_id,subject_id,memory_id,memory_version_id,index_type,status) VALUES('${indexID}'::uuid,'${indexPublicID}'::uuid,'10000000-0000-0000-0000-000000000001'::uuid,'10000000-0000-0000-0000-000000000401'::uuid,'${memoryID}'::uuid,'${versionID}'::uuid,'pgvector','PENDING'); COMMIT;`); 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: 1000 }, deviceScaleFactor: 1 }); const consoleErrors = []; const failedRequests = []; const memoryMutations = []; page.on("console", (message) => { if (message.type() === "error") consoleErrors.push(message.text()); }); page.on("request", (request) => { if (["PATCH", "DELETE"].includes(request.method()) && /\/api\/v1\/memories\//.test(request.url())) memoryMutations.push(`${request.method()} ${request.url()}`); }); 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(); await page.getByRole("button", { name: "故事与记忆", exact: true }).click(); const memoryRow = page.locator(".memory-row").filter({ hasText: content }); await memoryRow.waitFor(); await memoryRow.getByRole("button", { name: "管理", exact: true }).click(); const management = page.getByRole("region", { name: "记忆管理" }); await management.waitFor(); const contentEditor = management.getByLabel("修订后的记忆正文"); const editReason = management.getByLabel("修订原因"); const saveButton = management.getByRole("button", { name: "保存为新版本" }); const unchangedSaveDisabled = await saveButton.isDisabled(); await contentEditor.fill(`${content},补充页面修订内容`); await editReason.fill("验证页面内修订表单"); const changedSaveEnabled = await saveButton.isEnabled(); await management.getByRole("button", { name: "停用记忆" }).click(); const statusButton = management.getByRole("button", { name: "确认停用" }); const emptyStatusReasonDisabled = await statusButton.isDisabled(); await management.getByLabel("停用原因").fill("验证页面内停用确认"); const statusReasonEnabled = await statusButton.isEnabled(); await management.getByRole("button", { name: "永久删除" }).click(); const deleteButton = management.getByRole("button", { name: "确认永久删除" }); await management.getByLabel("删除原因").fill("验证页面内永久删除确认"); const uncheckedDeleteDisabled = await deleteButton.isDisabled(); await management.getByLabel("我已核对来源和家庭要求,确认永久删除这条记忆。").check(); const checkedDeleteEnabled = await deleteButton.isEnabled(); await page.screenshot({ path: fileURLToPath(new URL("memory-management-confirmation.png", outputDirectory)), fullPage: true }); 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 = { unchangedSaveDisabled, changedSaveEnabled, emptyStatusReasonDisabled, statusReasonEnabled, uncheckedDeleteDisabled, checkedDeleteEnabled, memoryMutations, overflow, consoleErrors, failedRequests }; console.log(JSON.stringify(result, null, 2)); if (!unchangedSaveDisabled || !changedSaveEnabled || !emptyStatusReasonDisabled || !statusReasonEnabled || !uncheckedDeleteDisabled || !checkedDeleteEnabled || memoryMutations.length || overflow.length || consoleErrors.length || failedRequests.length) { throw new Error("Memory management visual smoke failed"); } } finally { await browser.close(); cleanup(); }