chore: establish local platform baseline
This commit is contained in:
@@ -0,0 +1,132 @@
|
||||
import { createHash, randomUUID } from "node:crypto";
|
||||
import { execFileSync } from "node:child_process";
|
||||
import { mkdir } from "node:fs/promises";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { createRequire } from "node:module";
|
||||
|
||||
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 newHouseholdName = `受控新家庭-${marker}`;
|
||||
const targetHouseholdName = `邀请目标家庭-${marker}`;
|
||||
const targetHouseholdID = randomUUID();
|
||||
const targetHouseholdPublicID = randomUUID();
|
||||
const targetMemberID = randomUUID();
|
||||
const targetMemberPublicID = randomUUID();
|
||||
const invitationID = randomUUID();
|
||||
const invitationPublicID = randomUUID();
|
||||
const invitationToken = `INVITE-RELATIONSHIP-${randomUUID()}-${marker}`;
|
||||
const tokenDigest = createHash("sha256").update(invitationToken).digest("hex");
|
||||
const tenantPublicID = "10000000-0000-0000-0000-000000000010";
|
||||
const tenantName = "合成数据一号客户组织";
|
||||
const inviteeHint = `本地所有者-${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();
|
||||
}
|
||||
|
||||
psql(`
|
||||
INSERT INTO household (id,public_id,tenant_id,name,owner_user_id)
|
||||
VALUES ('${targetHouseholdID}','${targetHouseholdPublicID}','10000000-0000-0000-0000-000000000001','${targetHouseholdName}','10000000-0000-0000-0000-000000000102');
|
||||
INSERT INTO household_member (id,public_id,tenant_id,household_id,user_id,role)
|
||||
VALUES ('${targetMemberID}','${targetMemberPublicID}','10000000-0000-0000-0000-000000000001','${targetHouseholdID}','10000000-0000-0000-0000-000000000102','owner');
|
||||
INSERT INTO member_invitation (id,public_id,tenant_id,household_id,invitee_hint,token_digest,role,expires_at,created_by)
|
||||
VALUES ('${invitationID}','${invitationPublicID}','10000000-0000-0000-0000-000000000001','${targetHouseholdID}','${inviteeHint}','${tokenDigest}','viewer',now()+interval '2 hours','10000000-0000-0000-0000-000000000102');
|
||||
`);
|
||||
|
||||
const householdCountBefore = Number(psql("SELECT count(*) FROM household;"));
|
||||
const memberCountBefore = Number(psql("SELECT count(*) FROM household_member;"));
|
||||
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: 1050 }, deviceScaleFactor: 1 });
|
||||
const consoleErrors = [];
|
||||
const failedRequests = [];
|
||||
const mutationRequests = [];
|
||||
const previewRequests = [];
|
||||
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" && pathname === "/api/v1/member-invitations/preview") previewRequests.push(`${request.method()} ${pathname}`);
|
||||
if (request.method() === "POST" && (pathname === "/api/v1/households" || pathname === "/api/v1/member-invitations/accept")) 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 householdDetails = page.locator("details.household-create");
|
||||
await householdDetails.locator("summary").click();
|
||||
const householdForm = householdDetails.locator("form");
|
||||
await householdForm.getByLabel("家庭名称").fill(newHouseholdName);
|
||||
await householdForm.getByRole("button", { name: "创建家庭" }).click();
|
||||
let control = page.getByRole("form", { name: "家庭业务受控操作" });
|
||||
await control.waitFor();
|
||||
let text = await control.textContent();
|
||||
const createTargetBound = text?.includes(tenantName) === true && text?.includes(tenantPublicID) === true && text?.includes("本地所有者") === true && text?.includes(newHouseholdName) === true && text?.includes("不包含长者、授权、设备或通话") === true;
|
||||
let action = control.getByRole("button", { name: "确认创建并成为所有者" });
|
||||
const createNeedsAcknowledgement = await action.isDisabled();
|
||||
await control.getByLabel(/将在所选客户组织内创建独立家庭空间/).check();
|
||||
const createEnabled = await action.isEnabled();
|
||||
await control.screenshot({ path: fileURLToPath(new URL("family-household-create-control.png", outputDirectory)) });
|
||||
await control.getByRole("button", { name: "取消" }).click();
|
||||
const createCancelClosed = await control.count() === 0;
|
||||
|
||||
const acceptForm = page.locator("form.accept-invitation");
|
||||
const tokenInput = acceptForm.getByLabel("收到家庭邀请?");
|
||||
const previewButton = acceptForm.getByRole("button", { name: "预览并接受" });
|
||||
await tokenInput.fill("1234567890123456789012345678901");
|
||||
const shortInvitationBlocked = await previewButton.isDisabled();
|
||||
await tokenInput.fill(invitationToken);
|
||||
await previewButton.click();
|
||||
control = page.getByRole("form", { name: "家庭业务受控操作" });
|
||||
await control.waitFor();
|
||||
text = await control.textContent();
|
||||
const invitationTargetBound = text?.includes(tenantName) === true && text?.includes(tenantPublicID) === true && text?.includes(targetHouseholdName) === true && text?.includes(targetHouseholdPublicID) === true && text?.includes(inviteeHint) === true && text?.includes("查看者") === true && text?.includes("本次预览不创建账号或成员关系") === true;
|
||||
const invitationSecretMasked = text?.includes(invitationToken) === false && text?.includes(invitationToken.slice(-4)) === true;
|
||||
action = control.getByRole("button", { name: "确认加入该家庭" });
|
||||
const invitationNeedsAcknowledgement = await action.isDisabled();
|
||||
await control.getByLabel(/加入预览所示客户组织和家庭/).check();
|
||||
const invitationEnabled = await action.isEnabled();
|
||||
await control.screenshot({ path: fileURLToPath(new URL("family-invitation-accept-control.png", outputDirectory)) });
|
||||
await control.getByRole("button", { name: "取消" }).click();
|
||||
const invitationCancelClosed = await control.count() === 0;
|
||||
const tokenCleared = await tokenInput.inputValue() === "";
|
||||
|
||||
const householdsUnchanged = Number(psql("SELECT count(*) FROM household;")) === householdCountBefore;
|
||||
const membersUnchanged = Number(psql("SELECT count(*) FROM household_member;")) === memberCountBefore;
|
||||
const invitationUnused = psql(`SELECT (used_at IS NULL AND revoked_at IS NULL)::text FROM member_invitation WHERE id='${invitationID}';`) === "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 = { createTargetBound, createNeedsAcknowledgement, createEnabled, createCancelClosed, shortInvitationBlocked, invitationTargetBound, invitationSecretMasked, invitationNeedsAcknowledgement, invitationEnabled, invitationCancelClosed, tokenCleared, householdsUnchanged, membersUnchanged, invitationUnused, previewRequests, mutationRequests, overflow, consoleErrors, failedRequests };
|
||||
console.log(JSON.stringify(result, null, 2));
|
||||
if (Object.entries(result).some(([key, value]) => !["previewRequests", "mutationRequests", "overflow", "consoleErrors", "failedRequests"].includes(key) && value !== true) || previewRequests.length !== 1 || mutationRequests.length || overflow.length || consoleErrors.length || failedRequests.length) throw new Error("Family relationship create controls visual smoke failed");
|
||||
} finally {
|
||||
await browser.close();
|
||||
psql(`
|
||||
DELETE FROM member_invitation WHERE id='${invitationID}';
|
||||
DELETE FROM household_member WHERE id='${targetMemberID}';
|
||||
DELETE FROM household WHERE id='${targetHouseholdID}';
|
||||
`);
|
||||
}
|
||||
Reference in New Issue
Block a user