完成50%

This commit is contained in:
2026-07-23 08:23:59 +08:00
parent 9b0ad62df4
commit f1edc6b533
218 changed files with 24318 additions and 5514 deletions
+143 -56
View File
@@ -1,59 +1,146 @@
const assert = require('assert')
const origin = process.argv[2] || 'http://localhost:5173'
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms))
"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((item) => item.type === 'page' && item.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)}`, `R02 navigation failed: ${route}`)
await waitFor(send, `Boolean(document.querySelector(${JSON.stringify(selector)}))`, `R02 state missing: ${selector}`)
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', '.people-state--ready')
await send('Runtime.evaluate', { expression: "document.querySelector('.person-card')?.click()" })
await waitFor(send, "location.hash.startsWith('#/pages/records/r02-person-detail?personId=1')", 'R01 did not open R02')
await waitFor(send, "Boolean(document.querySelector('.person-detail-state--detail'))", 'R02 detail did not render from R01')
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('第 18 世')"), true)
await send('Runtime.evaluate', { expression: "document.querySelector('.person-edit-action')?.click()" })
await waitFor(send, "Boolean(document.querySelector('.person-detail-state--edit'))", 'R02 did not enter edit')
await send('Runtime.evaluate', { expression: `(() => { 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'))", 'R02 validation did not render')
await send('Runtime.evaluate', { expression: "document.querySelector('.person-cancel-action')?.click()" })
await waitFor(send, "Boolean(document.querySelector('.person-detail-state--detail'))", 'R02 did not cancel the invalid edit')
await send('Runtime.evaluate', { expression: "document.querySelector('.person-edit-action')?.click()" })
await waitFor(send, "Boolean(document.querySelector('.person-detail-state--edit'))", 'R02 did not re-enter edit after validation')
await send('Runtime.evaluate', { expression: "document.querySelector('.person-save-action')?.click()" })
await sleep(200)
const saveState = await valueOf(send, `({
detail: Boolean(document.querySelector('.person-detail-state--detail')),
values: Array.from(document.querySelectorAll('.person-field input')).map((input) => input.value),
errors: Array.from(document.querySelectorAll('.person-field-error')).map((error) => error.textContent.trim())
})`)
if (!saveState.detail) throw new Error(`R02 did not return to detail after save: ${JSON.stringify(saveState)}`)
await waitFor(send, "Boolean(document.querySelector('.app-toast'))", 'R02 save feedback missing')
await navigate('/pages/records/r02-person-detail?personId=1&state=privacy', '.person-detail-state--privacy')
await navigate('/pages/records/r02-person-detail?personId=1&state=expired', '.person-detail-state--expired')
await navigate('/pages/records/r02-person-detail?personId=1&state=error', '.person-detail-state--error')
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) })
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);
});