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 keyID = `visual-rollout-key-${marker}`; const keyInternalID = randomUUID(); const releaseInternalID = randomUUID(); const releasePublicID = randomUUID(); const releaseVersion = `9.9.${Number.parseInt(marker.slice(0, 2), 16)}`; const releaseDigest = "ab".repeat(32); const rolloutName = `受控灰度-${marker}`; const householdPublicID = "10000000-0000-0000-0000-000000000211"; const subjectPublicID = "10000000-0000-0000-0000-000000000411"; const devicePublicID = "10000000-0000-0000-0000-000000000511"; 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(); } psql(` INSERT INTO firmware_signing_key(id,tenant_id,key_id,public_key_base64,status,created_by) VALUES('${keyInternalID}','10000000-0000-0000-0000-000000000001','${keyID}','AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=','ACTIVE','10000000-0000-0000-0000-000000000101'); INSERT INTO firmware_release(id,public_id,tenant_id,household_id,model,hardware_revision,version,object_ref,expected_size,expected_digest,signature,signing_key_id,minimum_security_version,release_notes,status,created_by,verified_at) VALUES('${releaseInternalID}','${releasePublicID}','10000000-0000-0000-0000-000000000001','10000000-0000-0000-0000-000000000201','simulated-terminal','sim-rev-a','${releaseVersion}','visual/firmware/${releasePublicID}.bin',4096,'${releaseDigest}','visual-signature','${keyID}','1.0.0','页面受控灰度夹具','READY','10000000-0000-0000-0000-000000000101',now()); `); const consentCountBefore = Number(psql("SELECT count(*) FROM consent_record;")); const rolloutCountBefore = Number(psql("SELECT count(*) FROM firmware_rollout;")); const commandCountBefore = Number(psql("SELECT count(*) FROM device_command;")); 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\/subjects\/[^/]+\/consents$/.test(pathname) || /^\/api\/v1\/households\/[^/]+\/firmware-rollouts$/.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 consentForm = page.locator("form.tool-form").filter({ hasText: "新增分项授权" }); const consentType = consentForm.getByLabel("授权项目"); const noticeCheckbox = consentForm.locator("label.consent-confirm input"); const consentButton = consentForm.getByRole("button", { name: "核对并授权" }); await consentForm.getByText("当前长者已有同类有效授权,请先查看或按正式流程撤回。").waitFor(); const duplicateConsentShown = (await consentForm.textContent())?.includes("已有同类有效授权") === true; await noticeCheckbox.check(); const duplicateConsentBlocked = await consentButton.isDisabled(); await consentType.selectOption("raw_audio_storage"); const typeChangeClearedNotice = await noticeCheckbox.isChecked() === false; await noticeCheckbox.check(); const newConsentEnabled = await consentButton.isEnabled(); await consentButton.click(); let control = page.getByRole("form", { name: "家庭业务受控操作" }); await control.waitFor(); let text = await control.textContent(); const consentTargetBound = text?.includes("测试长者") === true && text?.includes(subjectPublicID) === true && text?.includes("原录音保存") === true && text?.includes("后续新通话") === true && text?.includes("local-v1") === true && text?.includes("family-web-confirmation") === true && text?.includes("持续有效,直至单独撤回") === true && text?.includes("双轨原始录音") === true && text?.includes("播放和下载仍需显式录音访问能力") === true; let action = control.getByRole("button", { name: "确认新增分项授权" }); const consentNeedsAcknowledgement = await action.isDisabled(); await control.getByLabel(/说明“原录音保存”的数据范围/).check(); const consentEnabled = await action.isEnabled(); await control.screenshot({ path: fileURLToPath(new URL("family-consent-grant-control.png", outputDirectory)) }); await control.getByRole("button", { name: "取消" }).click(); const consentCancelClosed = await control.count() === 0; const consentCancelClearedNotice = await noticeCheckbox.isChecked() === false; await page.getByRole("button", { name: "固件发布", exact: true }).click(); const rolloutForm = page.locator("form.firmware-rollout-form"); await rolloutForm.getByLabel("就绪版本").selectOption(releasePublicID); await rolloutForm.getByLabel("批次名称").fill(rolloutName); await rolloutForm.getByLabel(/灰度比例/).fill("25"); const deviceChoice = rolloutForm.getByLabel(new RegExp(`simulated-terminal.*${devicePublicID.slice(0, 8)}`)); const compatibleDeviceEnabled = await deviceChoice.isEnabled(); await deviceChoice.check(); const rolloutButton = rolloutForm.getByRole("button", { name: "核对并启动灰度发布" }); const rolloutReady = await rolloutButton.isEnabled(); await rolloutButton.click(); control = page.getByRole("form", { name: "家庭业务受控操作" }); await control.waitFor(); text = await control.textContent(); const rolloutTargetBound = text?.includes(householdPublicID) === true && text?.includes(rolloutName) === true && text?.includes(releasePublicID) === true && text?.includes(releaseVersion) === true && text?.includes(releaseDigest.slice(0, 12)) === true && text?.includes("25%") === true && text?.includes(devicePublicID) === true && text?.includes("0.1.0-sim") === true && text?.includes("确定实际命中设备") === true && text?.includes("24小时可靠升级命令") === true; action = control.getByRole("button", { name: "确认启动灰度批次" }); const rolloutNeedsAcknowledgement = await action.isDisabled(); await control.getByLabel(/所选1台候选设备与已验证固件的型号、主板和注册状态匹配/).check(); const rolloutEnabled = await action.isEnabled(); await control.screenshot({ path: fileURLToPath(new URL("family-firmware-rollout-start-control.png", outputDirectory)) }); await control.getByRole("button", { name: "取消" }).click(); const rolloutCancelClosed = await control.count() === 0; const consentsUnchanged = Number(psql("SELECT count(*) FROM consent_record;")) === consentCountBefore; const rolloutsUnchanged = Number(psql("SELECT count(*) FROM firmware_rollout;")) === rolloutCountBefore; const commandsUnchanged = Number(psql("SELECT count(*) FROM device_command;")) === commandCountBefore; 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 = { duplicateConsentShown, duplicateConsentBlocked, typeChangeClearedNotice, newConsentEnabled, consentTargetBound, consentNeedsAcknowledgement, consentEnabled, consentCancelClosed, consentCancelClearedNotice, compatibleDeviceEnabled, rolloutReady, rolloutTargetBound, rolloutNeedsAcknowledgement, rolloutEnabled, rolloutCancelClosed, consentsUnchanged, rolloutsUnchanged, commandsUnchanged, 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 consent/rollout create controls visual smoke failed"); } finally { await browser.close(); psql(`DELETE FROM firmware_release WHERE id='${releaseInternalID}'; DELETE FROM firmware_signing_key WHERE id='${keyInternalID}';`); }