147 lines
5.5 KiB
JavaScript
147 lines
5.5 KiB
JavaScript
"use strict";
|
|
|
|
const assert = require("assert");
|
|
const origin = process.argv[2] || "http://localhost:5173";
|
|
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
|
|
const connect = async () => {
|
|
const pages = await (await fetch("http://127.0.0.1:9222/json/list")).json();
|
|
const page = pages.find(
|
|
(candidate) => candidate.type === "page" && candidate.url.startsWith(`${origin}/`),
|
|
);
|
|
if (!page) throw new Error(`Chrome debugging has no ${origin} project page`);
|
|
const socket = new WebSocket(page.webSocketDebuggerUrl);
|
|
await new Promise((resolve, reject) => {
|
|
socket.addEventListener("open", resolve, { once: true });
|
|
socket.addEventListener("error", reject, { once: true });
|
|
});
|
|
let id = 0;
|
|
const pending = new Map();
|
|
socket.addEventListener("message", (event) => {
|
|
const message = JSON.parse(event.data);
|
|
const request = pending.get(message.id);
|
|
if (!request) return;
|
|
pending.delete(message.id);
|
|
message.error
|
|
? request.reject(new Error(message.error.message))
|
|
: request.resolve(message.result);
|
|
});
|
|
const send = (method, params = {}) =>
|
|
new Promise((resolve, reject) => {
|
|
id += 1;
|
|
pending.set(id, { resolve, reject });
|
|
socket.send(JSON.stringify({ id, method, params }));
|
|
});
|
|
return { socket, send };
|
|
};
|
|
|
|
const valueOf = async (send, expression) =>
|
|
(await send("Runtime.evaluate", { expression, returnByValue: true })).result?.value;
|
|
const waitFor = async (send, expression, message) => {
|
|
for (let attempt = 0; attempt < 60; attempt += 1) {
|
|
if (await valueOf(send, expression)) return;
|
|
await sleep(80);
|
|
}
|
|
throw new Error(message);
|
|
};
|
|
|
|
const run = async () => {
|
|
const { socket, send } = await connect();
|
|
let auditId = 0;
|
|
const navigate = async (route, selector) => {
|
|
auditId += 1;
|
|
const url = `${origin}/?r02RuntimeAudit=${auditId}#${route}`;
|
|
await send("Page.navigate", { url });
|
|
await waitFor(send, `location.href===${JSON.stringify(url)}`, `navigation failed: ${route}`);
|
|
await waitFor(
|
|
send,
|
|
`Boolean(document.querySelector(${JSON.stringify(selector)}))`,
|
|
`state missing: ${selector}`,
|
|
);
|
|
};
|
|
const click = (selector) =>
|
|
valueOf(send, `document.querySelector(${JSON.stringify(selector)})?.click()`);
|
|
|
|
try {
|
|
await send("Page.enable");
|
|
await send("Runtime.enable");
|
|
await navigate(
|
|
"/pages/records/r01-people-list?genealogyId=1001",
|
|
".people-state--ready",
|
|
);
|
|
await click(".person-card");
|
|
await waitFor(
|
|
send,
|
|
"location.hash.includes('genealogyId=1001&mode=view&personId=101&sourceKey=R01')",
|
|
"R01 did not open the scoped R02 person",
|
|
);
|
|
await waitFor(
|
|
send,
|
|
"Boolean(document.querySelector('.person-detail-state--detail'))",
|
|
"R02 detail did not render",
|
|
);
|
|
assert.strictEqual(
|
|
await valueOf(send, "document.querySelector('.person-identity-card')?.textContent.includes('汤文远')"),
|
|
true,
|
|
);
|
|
assert.strictEqual(
|
|
await valueOf(send, "document.querySelector('.person-identity-card')?.textContent.includes('第 12 世')"),
|
|
true,
|
|
);
|
|
|
|
await click(".person-edit-action");
|
|
await waitFor(send, "Boolean(document.querySelector('.person-detail-state--edit'))", "edit state missing");
|
|
await valueOf(
|
|
send,
|
|
"(() => { const input=document.querySelector('.person-field input'); input.value=''; input.dispatchEvent(new Event('input',{bubbles:true})); document.querySelector('.person-save-action')?.click(); })()",
|
|
);
|
|
await waitFor(send, "Boolean(document.querySelector('.person-field-error'))", "validation missing");
|
|
await click(".person-cancel-action");
|
|
await waitFor(send, "Boolean(document.querySelector('.app-dialog-layer'))", "discard confirmation missing");
|
|
await click(".app-dialog__actions .app-button:last-child");
|
|
await waitFor(send, "Boolean(document.querySelector('.person-detail-state--detail'))", "discard did not restore detail");
|
|
|
|
await click(".person-edit-action");
|
|
await click(".person-save-action");
|
|
await waitFor(send, "Boolean(document.querySelector('.person-detail-state--preview'))", "local preview missing");
|
|
await waitFor(send, "Boolean(document.querySelector('.app-toast'))", "preview feedback missing");
|
|
await click(".header-back");
|
|
await waitFor(send, "Boolean(document.querySelector('.app-dialog-layer'))", "preview back guard missing");
|
|
await click(".app-dialog__actions .app-button:first-child");
|
|
assert.strictEqual(
|
|
await valueOf(send, "Boolean(document.querySelector('.person-detail-state--preview'))"),
|
|
true,
|
|
"canceling discard must keep the local preview",
|
|
);
|
|
|
|
await navigate(
|
|
"/pages/records/r02-person-detail?genealogyId=1001&mode=view&personId=102",
|
|
".person-detail-state--privacy",
|
|
);
|
|
await navigate(
|
|
"/pages/records/r02-person-detail?genealogyId=1001&mode=view&personId=103",
|
|
".person-detail-state--privacy",
|
|
);
|
|
await navigate(
|
|
"/pages/records/r02-person-detail?genealogyId=1001&mode=view&personId=999",
|
|
".person-detail-state--expired",
|
|
);
|
|
await navigate(
|
|
"/pages/records/r02-person-detail?mode=view&personId=101",
|
|
".person-detail-state--error",
|
|
);
|
|
await navigate(
|
|
"/pages/records/r02-person-detail?genealogyId=1001&mode=create",
|
|
".person-detail-state--edit",
|
|
);
|
|
process.stdout.write("R02-PERSON-DETAIL-RUNTIME-SMOKE PASS\n");
|
|
} finally {
|
|
socket.close();
|
|
}
|
|
};
|
|
|
|
run().catch((error) => {
|
|
process.stderr.write(`${error.stack || error.message}\n`);
|
|
process.exit(1);
|
|
});
|