155 lines
12 KiB
JavaScript
155 lines
12 KiB
JavaScript
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 marker = randomUUID().slice(0, 8);
|
||
|
|
const keyID = `visual-key-${marker}`;
|
||
|
|
const keyInternalID = randomUUID();
|
||
|
|
const currentReleaseID = randomUUID();
|
||
|
|
const currentReleasePublicID = randomUUID();
|
||
|
|
const rollbackReleaseID = randomUUID();
|
||
|
|
const rollbackReleasePublicID = randomUUID();
|
||
|
|
const rolloutID = randomUUID();
|
||
|
|
const rolloutPublicID = randomUUID();
|
||
|
|
const grantID = randomUUID();
|
||
|
|
const grantPublicID = randomUUID();
|
||
|
|
const resourcePublicID = randomUUID();
|
||
|
|
const rolloutName = `页面固件控制夹具-${marker}`;
|
||
|
|
const sharePurpose = `页面分享撤销夹具-${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();
|
||
|
|
}
|
||
|
|
function cleanup() {
|
||
|
|
psql(`BEGIN; DELETE FROM sharing_grant WHERE id='${grantID}'::uuid; DELETE FROM firmware_rollout WHERE id='${rolloutID}'::uuid; DELETE FROM firmware_release WHERE id IN ('${currentReleaseID}'::uuid,'${rollbackReleaseID}'::uuid); DELETE FROM firmware_signing_key WHERE id='${keyInternalID}'::uuid; COMMIT;`);
|
||
|
|
}
|
||
|
|
|
||
|
|
psql(`BEGIN;
|
||
|
|
INSERT INTO firmware_signing_key(id,tenant_id,key_id,public_key_base64,status,created_by)
|
||
|
|
VALUES('${keyInternalID}'::uuid,'10000000-0000-0000-0000-000000000001'::uuid,'${keyID}','AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=','ACTIVE','10000000-0000-0000-0000-000000000101'::uuid);
|
||
|
|
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
|
||
|
|
('${currentReleaseID}'::uuid,'${currentReleasePublicID}'::uuid,'10000000-0000-0000-0000-000000000001'::uuid,(SELECT id FROM household WHERE public_id='10000000-0000-0000-0000-000000000211'::uuid),'visual-terminal','rev-a','9.9.2','object://visual/current-${marker}',1024,repeat('a',64),'visual-signature','${keyID}','1.0.0','页面暂停夹具','READY','10000000-0000-0000-0000-000000000101'::uuid,now()),
|
||
|
|
('${rollbackReleaseID}'::uuid,'${rollbackReleasePublicID}'::uuid,'10000000-0000-0000-0000-000000000001'::uuid,(SELECT id FROM household WHERE public_id='10000000-0000-0000-0000-000000000211'::uuid),'visual-terminal','rev-a','9.9.1','object://visual/rollback-${marker}',1024,repeat('b',64),'visual-signature','${keyID}','1.0.0','页面回滚夹具','READY','10000000-0000-0000-0000-000000000101'::uuid,now());
|
||
|
|
INSERT INTO firmware_rollout(id,public_id,tenant_id,household_id,release_id,name,status,target_percentage,created_by,started_at)
|
||
|
|
VALUES('${rolloutID}'::uuid,'${rolloutPublicID}'::uuid,'10000000-0000-0000-0000-000000000001'::uuid,(SELECT id FROM household WHERE public_id='10000000-0000-0000-0000-000000000211'::uuid),'${currentReleaseID}'::uuid,'${rolloutName}','RUNNING',25,'10000000-0000-0000-0000-000000000101'::uuid,now()-interval '10 minutes');
|
||
|
|
INSERT INTO sharing_grant(id,public_id,tenant_id,household_id,subject_id,resource_type,resource_public_id,grantee_user_id,permission,purpose,login_required,granted_by,expires_at,version)
|
||
|
|
VALUES('${grantID}'::uuid,'${grantPublicID}'::uuid,'10000000-0000-0000-0000-000000000001'::uuid,(SELECT id FROM household WHERE public_id='10000000-0000-0000-0000-000000000211'::uuid),(SELECT id FROM subject_profile WHERE public_id='10000000-0000-0000-0000-000000000411'::uuid),'STORY','${resourcePublicID}'::uuid,'10000000-0000-0000-0000-000000000102'::uuid,'VIEW_TEXT','${sharePurpose}',true,'10000000-0000-0000-0000-000000000101'::uuid,now()+interval '1 day',3);
|
||
|
|
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: 1100 }, deviceScaleFactor: 1 });
|
||
|
|
const consoleErrors = [];
|
||
|
|
const failedRequests = [];
|
||
|
|
const mutations = [];
|
||
|
|
page.on("console", (message) => { if (message.type() === "error") consoleErrors.push(message.text()); });
|
||
|
|
page.on("request", (request) => {
|
||
|
|
if ((request.method() === "POST" && /\/(consents\/[^/]+\/withdraw|ownership|firmware-rollouts\/[^/]+\/(pause|rollback))$/.test(request.url())) || (request.method() === "DELETE" && /\/(households\/[^/]+\/members\/[^/]+|sharing-grants\/[^/]+)$/.test(request.url()))) mutations.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(process.env.VISUAL_USERNAME ?? "owner");
|
||
|
|
await page.locator("#password").fill(process.env.VISUAL_PASSWORD ?? "LocalOnly123!");
|
||
|
|
await page.locator("#kc-login").click();
|
||
|
|
await page.waitForURL(webHostPattern, { timeout: 15000 });
|
||
|
|
await page.getByRole("heading", { name: "家庭业务工作台" }).waitFor();
|
||
|
|
|
||
|
|
const memoryConsent = page.locator(".data-row").filter({ hasText: "个性化记忆" });
|
||
|
|
await memoryConsent.getByRole("button", { name: "撤回" }).click();
|
||
|
|
let form = page.getByRole("form", { name: "家庭业务受控操作" });
|
||
|
|
let text = await form.textContent();
|
||
|
|
const consentTargetBound = text?.includes("10000000-0000-0000-0000-000000000813") === true && text?.includes("测试长者") === true && text?.includes("记录版本 v1") === true;
|
||
|
|
let submit = form.getByRole("button", { name: "确认撤回授权" });
|
||
|
|
const consentDisabled = await submit.isDisabled();
|
||
|
|
await form.getByLabel(/我确认该授权将立即失效/).check();
|
||
|
|
const consentEnabled = await submit.isEnabled();
|
||
|
|
await form.getByRole("button", { name: "取消" }).click();
|
||
|
|
|
||
|
|
await page.getByRole("button", { name: "家庭成员" }).click();
|
||
|
|
const memberRow = page.locator(".member-row").filter({ hasText: "本地编辑者" });
|
||
|
|
await memberRow.getByRole("button", { name: "转移所有权" }).click();
|
||
|
|
form = page.getByRole("form", { name: "家庭业务受控操作" });
|
||
|
|
text = await form.textContent();
|
||
|
|
const ownershipTargetBound = text?.includes("10000000-0000-0000-0000-000000000312") === true && text?.includes("合成数据一号家庭") === true && text?.includes("记录版本 v1") === true;
|
||
|
|
submit = form.getByRole("button", { name: "确认转移所有权" });
|
||
|
|
await form.getByLabel("输入成员姓名确认").fill("本地编辑");
|
||
|
|
const ownershipWrongDisabled = await submit.isDisabled();
|
||
|
|
await form.getByLabel("输入成员姓名确认").fill("本地编辑者");
|
||
|
|
const ownershipEnabled = await submit.isEnabled();
|
||
|
|
await form.getByRole("button", { name: "取消" }).click();
|
||
|
|
|
||
|
|
await memberRow.getByRole("button", { name: "移除" }).click();
|
||
|
|
form = page.getByRole("form", { name: "家庭业务受控操作" });
|
||
|
|
text = await form.textContent();
|
||
|
|
const removeTargetBound = text?.includes("10000000-0000-0000-0000-000000000312") === true && text?.includes("失去家庭、长者、设备和内容访问能力") === true;
|
||
|
|
submit = form.getByRole("button", { name: "确认移出成员" });
|
||
|
|
await form.getByLabel("输入成员姓名确认").fill("本地编辑者");
|
||
|
|
const removeEnabled = await submit.isEnabled();
|
||
|
|
await form.getByRole("button", { name: "取消" }).click();
|
||
|
|
|
||
|
|
await page.getByRole("button", { name: "固件发布" }).click();
|
||
|
|
await page.locator(".stack-card--button").filter({ hasText: rolloutName }).click();
|
||
|
|
const detail = page.locator(".firmware-detail").filter({ hasText: rolloutName });
|
||
|
|
await detail.waitFor();
|
||
|
|
await detail.getByRole("button", { name: "暂停批次" }).click();
|
||
|
|
form = page.getByRole("form", { name: "家庭业务受控操作" });
|
||
|
|
text = await form.textContent();
|
||
|
|
const pauseTargetBound = text?.includes(rolloutPublicID) === true && text?.includes("灰度 25%") === true && text?.includes("当前发布中") === true;
|
||
|
|
submit = form.getByRole("button", { name: "确认暂停批次" });
|
||
|
|
const pauseDisabled = await submit.isDisabled();
|
||
|
|
await form.getByLabel(/我确认尚未投递的设备命令将被取消/).check();
|
||
|
|
const pauseEnabled = await submit.isEnabled();
|
||
|
|
await form.getByRole("button", { name: "取消" }).click();
|
||
|
|
|
||
|
|
await detail.getByLabel("回滚到已验证版本").selectOption(rollbackReleasePublicID);
|
||
|
|
await detail.getByRole("button", { name: "创建回滚批次" }).click();
|
||
|
|
form = page.getByRole("form", { name: "家庭业务受控操作" });
|
||
|
|
text = await form.textContent();
|
||
|
|
const rollbackTargetBound = text?.includes(rolloutPublicID) === true && text?.includes(rollbackReleasePublicID) === true && text?.includes("visual-terminal / 9.9.1") === true;
|
||
|
|
submit = form.getByRole("button", { name: "确认创建回滚" });
|
||
|
|
const rollbackDisabled = await submit.isDisabled();
|
||
|
|
await form.getByLabel(/我确认所选版本已经验证/).check();
|
||
|
|
const rollbackEnabled = await submit.isEnabled();
|
||
|
|
await form.getByRole("button", { name: "取消" }).click();
|
||
|
|
|
||
|
|
await page.getByRole("button", { name: "资源分享" }).click();
|
||
|
|
const shareRow = page.locator(".data-row").filter({ hasText: sharePurpose });
|
||
|
|
await shareRow.getByRole("button", { name: "撤销" }).click();
|
||
|
|
form = page.getByRole("form", { name: "家庭业务受控操作" });
|
||
|
|
text = await form.textContent();
|
||
|
|
const shareTargetBound = text?.includes(grantPublicID) === true && text?.includes(resourcePublicID) === true && text?.includes("记录版本 v3") === true;
|
||
|
|
submit = form.getByRole("button", { name: "确认撤销分享" });
|
||
|
|
const shareDisabled = await submit.isDisabled();
|
||
|
|
await form.getByLabel(/我确认后续使用该分享凭据/).check();
|
||
|
|
const shareEnabled = await submit.isEnabled();
|
||
|
|
await page.screenshot({ path: fileURLToPath(new URL("family-controlled-actions.png", outputDirectory)), fullPage: true });
|
||
|
|
await form.getByRole("button", { name: "取消" }).click();
|
||
|
|
const cancelClosed = await form.count() === 0;
|
||
|
|
|
||
|
|
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 = { consentTargetBound, consentDisabled, consentEnabled, ownershipTargetBound, ownershipWrongDisabled, ownershipEnabled, removeTargetBound, removeEnabled, pauseTargetBound, pauseDisabled, pauseEnabled, rollbackTargetBound, rollbackDisabled, rollbackEnabled, shareTargetBound, shareDisabled, shareEnabled, cancelClosed, mutations, overflow, consoleErrors, failedRequests };
|
||
|
|
console.log(JSON.stringify(result, null, 2));
|
||
|
|
if (!consentTargetBound || !consentDisabled || !consentEnabled || !ownershipTargetBound || !ownershipWrongDisabled || !ownershipEnabled || !removeTargetBound || !removeEnabled || !pauseTargetBound || !pauseDisabled || !pauseEnabled || !rollbackTargetBound || !rollbackDisabled || !rollbackEnabled || !shareTargetBound || !shareDisabled || !shareEnabled || !cancelClosed || mutations.length || overflow.length || consoleErrors.length || failedRequests.length) throw new Error("Family controlled actions visual smoke failed");
|
||
|
|
} finally {
|
||
|
|
await browser.close();
|
||
|
|
cleanup();
|
||
|
|
}
|