112 lines
6.8 KiB
JavaScript
112 lines
6.8 KiB
JavaScript
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
const origin = process.argv[2] || "http://localhost:5173";
|
|
|
|
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 index = 0; index < 60; index += 1) {
|
|
if (await valueOf(send, expression)) return;
|
|
await sleep(100);
|
|
}
|
|
throw new Error(message);
|
|
};
|
|
let auditId = 0;
|
|
const open = async (send, route, selector) => {
|
|
auditId += 1;
|
|
const url = `${origin}/?fBusiness=${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)}))`, `missing ${selector}: ${route}`);
|
|
};
|
|
const click = (send, selector) => valueOf(send, `document.querySelector(${JSON.stringify(selector)}).click()`);
|
|
const setInput = (send, selector, value) => valueOf(send, `(() => { const input = document.querySelector(${JSON.stringify(selector)}); input.value = ${JSON.stringify(value)}; input.dispatchEvent(new Event('input', { bubbles: true })); return input.value; })()`);
|
|
|
|
const run = async () => {
|
|
const { socket, send } = await connect();
|
|
try {
|
|
await send("Page.enable");
|
|
await send("Runtime.enable");
|
|
|
|
// 步骤一:F02 空内容必须由真实发布按钮触发项目内 Toast,不能依赖旧截图状态助手。
|
|
await open(send, "/pages/family/f02-publish-feed?genealogyId=1001", ".publish-state--form");
|
|
await click(send, ".publish-form .app-button");
|
|
await waitFor(send, "Boolean(document.querySelector('.app-toast'))", "F02 empty submit did not show the project Toast");
|
|
if (!(await valueOf(send, "document.querySelector('.app-toast')?.textContent.includes('请先写下动态内容')"))) {
|
|
throw new Error("F02 empty submit showed unexpected feedback");
|
|
}
|
|
await setInput(send, ".publish-form textarea", "这是一条本地动态预览。");
|
|
await click(send, ".publish-form .app-button");
|
|
await waitFor(send, "Boolean(document.querySelector('.publish-state--preview'))", "F02 did not enter honest local preview");
|
|
if (!(await valueOf(send, "document.querySelector('.publish-result')?.textContent.includes('尚未提交服务器')"))) {
|
|
throw new Error("F02 preview did not disclose that content was not published");
|
|
}
|
|
|
|
for (const size of [{ width: 320, height: 568 }, { width: 412, height: 915 }]) {
|
|
await send("Emulation.setDeviceMetricsOverride", { ...size, deviceScaleFactor: 1, mobile: true, screenWidth: size.width, screenHeight: size.height });
|
|
await open(send, "/pages/family/f04-article-list?genealogyId=1001", ".article-card");
|
|
if ((await valueOf(send, "document.querySelectorAll('.article-card').length")) !== 3) throw new Error(`F04 did not render the scoped article owner at ${size.width}`);
|
|
if ((await valueOf(send, "document.documentElement.scrollWidth")) > size.width + 1) throw new Error(`F04 horizontal overflow at ${size.width}`);
|
|
await valueOf(send, "document.querySelector('.article-card:last-of-type').scrollIntoView()");
|
|
|
|
await open(send, "/pages/family/f07-album-list?genealogyId=1001", ".album-card");
|
|
if ((await valueOf(send, "document.querySelectorAll('.album-card').length")) !== 3) throw new Error(`F07 did not render the scoped album owner at ${size.width}`);
|
|
if ((await valueOf(send, "document.documentElement.scrollWidth")) > size.width + 1) throw new Error(`F07 horizontal overflow at ${size.width}`);
|
|
}
|
|
|
|
await open(send, "/pages/family/f04-article-list?genealogyId=1001", ".article-card");
|
|
await click(send, ".article-card");
|
|
await waitFor(send, "location.hash.includes('/pages/family/f05-article-detail?genealogyId=1001&articleId=101')", "F04 card did not open composite-identity F05");
|
|
|
|
await open(send, "/pages/family/f03-feed-detail?genealogyId=1001&feedId=1", ".feed-detail-contract-note");
|
|
if (!(await valueOf(send, "document.querySelector('.feed-detail-contract-note')?.textContent.includes('不读取宽接口')"))) {
|
|
throw new Error("F03 did not disclose its contract-safe unavailable state");
|
|
}
|
|
|
|
await open(send, "/pages/family/f07-album-list?genealogyId=1001", ".album-list > .app-button");
|
|
const albumCount = await valueOf(send, "document.querySelectorAll('.album-card').length");
|
|
await click(send, ".album-list > .app-button");
|
|
await waitFor(send, "Boolean(document.querySelector('.app-dialog-layer'))", "F07 create dialog did not open");
|
|
await setInput(send, ".album-dialog-field input", "清明祭祖影像");
|
|
await click(send, ".app-dialog__actions .app-button:last-child");
|
|
await waitFor(send, "document.querySelector('.album-local-preview')?.textContent.includes('清明祭祖影像')", "F07 local album preview did not render");
|
|
if ((await valueOf(send, "document.querySelectorAll('.album-card').length")) !== albumCount) throw new Error("F07 inserted an unsubmitted album into the official list");
|
|
|
|
await open(send, "/pages/family/f03-feed-detail?genealogyId=1002&feedId=1", ".feed-state--expired");
|
|
await open(send, "/pages/family/f05-article-detail?genealogyId=1002&articleId=101", ".article-state--expired");
|
|
await open(send, "/pages/family/f08-album-detail?genealogyId=1002&albumId=201", ".album-state--expired");
|
|
await open(send, "/pages/family/f09-media-upload?genealogyId=1002&albumId=201", ".media-upload-state--invalid");
|
|
|
|
process.stdout.write("F-BUSINESS-FLOW-RUNTIME-SMOKE PASS\n");
|
|
} finally {
|
|
try { await send("Emulation.clearDeviceMetricsOverride"); } catch (_) {}
|
|
socket.close();
|
|
}
|
|
};
|
|
|
|
run().catch((error) => { process.stderr.write(`${error.stack || error.message}\n`); process.exit(1); });
|