forked from longtao.wu/droboticflow
232 lines
13 KiB
JavaScript
232 lines
13 KiB
JavaScript
// Real HTTP router + isolated local MemoryStore or Mongo. Start the documented replica server
|
||
// and Vite first; this test never mocks a successful statistics response.
|
||
import { chromium } from "playwright";
|
||
import fs from "node:fs";
|
||
import path from "node:path";
|
||
import assert from "node:assert/strict";
|
||
const out = path.resolve(
|
||
process.env.ANNOTATION_OUTPUT_VERIFY_DIR || "local-data/verification/customer-dashboard-20261020",
|
||
);
|
||
fs.mkdirSync(out, { recursive: true });
|
||
const accounts = JSON.parse(
|
||
fs.readFileSync(
|
||
path.resolve(
|
||
process.env.ANNOTATION_OUTPUT_ACCOUNTS || "local-data/verification/customer-dashboard-20261020/accounts.json",
|
||
),
|
||
"utf8",
|
||
),
|
||
);
|
||
assert.match(accounts.database, /^annotation_output_browser_\d+$/);
|
||
assert.match(accounts.origin, /^http:\/\/127\.0\.0\.1:\d+$/);
|
||
const browser = await chromium.launch({
|
||
executablePath: process.env.BROWSER_EXECUTABLE || undefined,
|
||
headless: true,
|
||
args: ["--no-proxy-server"],
|
||
});
|
||
const report = { database: accounts.database, roles: [], failures: [] };
|
||
try {
|
||
for (const id of ["admin", "u1", "manager"]) {
|
||
const a = accounts.accounts.find((x) => x.id === id),
|
||
ctx = await browser.newContext({
|
||
viewport: { width: 1500, height: 1100 },
|
||
acceptDownloads: true,
|
||
serviceWorkers: "block",
|
||
});
|
||
const row = { id, pageErrors: [], unexpectedWrites: [], checks: [] };
|
||
report.roles.push(row);
|
||
try {
|
||
console.log("Role", id, "started");
|
||
const login = await ctx.request.post(accounts.origin + "/api/auth/login", {
|
||
data: { phone: a.phone, password: a.password },
|
||
});
|
||
assert.equal(login.status(), 200, "local login");
|
||
await login.dispose();
|
||
const page = await ctx.newPage();
|
||
page.on("pageerror", (e) => row.pageErrors.push(e.message));
|
||
page.on("request", (r) => {
|
||
if (!["GET", "HEAD", "OPTIONS"].includes(r.method()) && !new URL(r.url()).pathname.startsWith("/api/auth/"))
|
||
row.unexpectedWrites.push(new URL(r.url()).pathname);
|
||
});
|
||
const initialData = page.waitForResponse(
|
||
(r) =>
|
||
r.url().includes("/api/dashboard/annotation-output?") &&
|
||
r.status() === 200 &&
|
||
!r.url().includes("format=csv"),
|
||
);
|
||
await page.goto(accounts.origin + "/?view=dashboard", { waitUntil: "domcontentloaded" });
|
||
console.log("Role", id, "page loaded");
|
||
const dashboard = page.getByRole("region", { name: id === "u1" ? "我的工作记录" : "生产产出看板", exact: true });
|
||
await dashboard.waitFor();
|
||
const tasks = dashboard.getByRole("region", { name: "任务明细", exact: true });
|
||
await tasks.getByRole("button", { name: "查看详情" }).first().waitFor({ timeout: 30000 });
|
||
assert.equal(await dashboard.getByText(/个 part|待核对/).count(), 0);
|
||
row.checks.push("no internal terminology");
|
||
const snapshotId = (await (await initialData).json()).snapshot?.id;
|
||
assert.ok(snapshotId, "server returned a shared generation");
|
||
const firstVideo = await tasks.locator("tbody tr").first().locator("td").first().innerText();
|
||
const cardNumbers = await dashboard.locator(".annotation-output-card strong").allTextContents();
|
||
const pager = tasks.getByRole("navigation", { name: "任务明细分页" });
|
||
const nextData = page.waitForResponse((r) => r.url().includes("tasksPage=2") && r.status() === 200);
|
||
await pager.getByRole("button", { name: "下一页", exact: true }).click();
|
||
const nextResponse = await nextData;
|
||
assert.equal(new URL(nextResponse.url()).searchParams.get("snapshotId"), snapshotId);
|
||
assert.equal((await nextResponse.json()).snapshot.id, snapshotId);
|
||
row.checks.push("pagination pins shared generation");
|
||
await tasks.getByText(/第 6–/).waitFor();
|
||
assert.notEqual(await tasks.locator("tbody tr").first().locator("td").first().innerText(), firstVideo);
|
||
assert.deepEqual(await dashboard.locator(".annotation-output-card strong").allTextContents(), cardNumbers);
|
||
row.checks.push("pagination preserves totals");
|
||
// Presentation edits must not reset the existing query, snapshot or task page.
|
||
let layoutRequests = 0;
|
||
const countLayoutRequest = (request) => {
|
||
if (request.url().includes("/api/dashboard/annotation-output?")) layoutRequests++;
|
||
};
|
||
page.on("request", countLayoutRequest);
|
||
const sectionOrder = () =>
|
||
dashboard.locator("[data-output-section]").evaluateAll((nodes) => nodes.map((n) => n.dataset.outputSection));
|
||
const originalOrder = await sectionOrder();
|
||
await dashboard.getByRole("button", { name: "调整布局", exact: true }).click();
|
||
const settings = dashboard.getByRole("region", { name: "调整看板布局", exact: true });
|
||
await settings.getByLabel("显示密度").selectOption("compact");
|
||
await settings.getByLabel("强调色").selectOption("blue");
|
||
await settings.getByRole("button", { name: "任务明细上移", exact: true }).click();
|
||
assert.equal(await dashboard.getAttribute("data-density"), "compact");
|
||
assert.notDeepEqual(await sectionOrder(), originalOrder);
|
||
await settings.getByRole("button", { name: "取消", exact: true }).click();
|
||
assert.deepEqual(await sectionOrder(), originalOrder);
|
||
assert.equal(await dashboard.getAttribute("data-accent"), "orange");
|
||
await dashboard.getByRole("button", { name: "调整布局", exact: true }).click();
|
||
await settings.getByLabel("显示密度").selectOption("compact");
|
||
await settings.getByLabel("强调色").selectOption("teal");
|
||
await settings.getByRole("button", { name: "任务明细上移", exact: true }).click();
|
||
await settings.getByRole("button", { name: "保存布局", exact: true }).click();
|
||
assert.equal(await dashboard.getAttribute("data-accent"), "teal");
|
||
assert.deepEqual(await dashboard.locator(".annotation-output-card strong").allTextContents(), cardNumbers);
|
||
await tasks.getByText(/第 6–/).waitFor();
|
||
assert.equal(layoutRequests, 0, "presentation does not requery statistics");
|
||
page.off("request", countLayoutRequest);
|
||
row.checks.push(
|
||
"layout preview/cancel/save preserves page, totals and query with zero extra statistics requests",
|
||
);
|
||
const expectedRows = Number((await pager.locator("span").first().innerText()).match(/共 (\d+)/)[1]);
|
||
const exported = page.waitForResponse((r) => r.url().includes("format=csv") && r.status() === 200);
|
||
const downloaded = page.waitForEvent("download");
|
||
await dashboard.getByRole("button", { name: "导出明细", exact: true }).click();
|
||
const download = await downloaded;
|
||
assert.equal((await exported).headers()["x-output-snapshot"], snapshotId);
|
||
row.checks.push("CSV pins same generation");
|
||
const file = await download.path(),
|
||
csv = fs.readFileSync(file, "utf8");
|
||
assert.equal(csv.trim().split(/\r?\n/).length, expectedRows + 1);
|
||
row.checks.push("CSV exports whole filtered scope from page 2");
|
||
await pager.getByRole("combobox").selectOption("10");
|
||
await page.waitForFunction(
|
||
() =>
|
||
document.querySelector('[aria-label="任务明细每页条数"]')?.value === "10" &&
|
||
document.querySelector('[aria-label="任务明细分页"]')?.textContent.includes("第 1–"),
|
||
);
|
||
row.checks.push("page size resets page");
|
||
await tasks.getByRole("button", { name: "查看详情" }).first().click();
|
||
const dialog = page.getByRole("dialog", { name: "任务详情" });
|
||
await dialog.waitFor();
|
||
await page.keyboard.press("Escape");
|
||
await dialog.waitFor({ state: "hidden" });
|
||
row.checks.push("read-only detail and Escape close");
|
||
if (id === "u1") {
|
||
await pager.getByRole("combobox").selectOption("5");
|
||
await page.waitForFunction(
|
||
() =>
|
||
document.querySelector('[aria-label="任务明细每页条数"]')?.value === "5" &&
|
||
!!document.querySelector('[aria-label="任务明细分页"] [aria-current="page"]'),
|
||
);
|
||
|
||
let expiredOnce = false;
|
||
await page.route("**/api/dashboard/annotation-output?*", async (route) => {
|
||
const url = new URL(route.request().url());
|
||
if (!expiredOnce && url.searchParams.get("tasksPage") === "2" && url.searchParams.has("snapshotId")) {
|
||
expiredOnce = true;
|
||
url.searchParams.set("snapshotId", "expired-test-generation");
|
||
return route.continue({ url: url.toString() });
|
||
}
|
||
return route.continue();
|
||
});
|
||
const expiredResponse = page.waitForResponse(
|
||
(r) => r.url().includes("/api/dashboard/annotation-output?") && r.status() === 409,
|
||
);
|
||
const recoveredResponse = page.waitForResponse((r) => {
|
||
const u = new URL(r.url());
|
||
return (
|
||
u.pathname === "/api/dashboard/annotation-output" &&
|
||
r.status() === 200 &&
|
||
u.searchParams.get("tasksPage") === "1" &&
|
||
!u.searchParams.has("snapshotId")
|
||
);
|
||
});
|
||
await pager.getByRole("button", { name: "下一页", exact: true }).click();
|
||
await expiredResponse;
|
||
await recoveredResponse;
|
||
await page.waitForFunction(
|
||
() =>
|
||
document.querySelector('[aria-label="任务明细分页"] [aria-current="page"]')?.textContent === "1" &&
|
||
!document.querySelector(".annotation-output-loading"),
|
||
);
|
||
assert.equal(expiredOnce, true);
|
||
assert.equal(await dashboard.locator(".annotation-output-error").count(), 0);
|
||
await page.unroute("**/api/dashboard/annotation-output?*");
|
||
row.checks.push("expired pin resets whole dashboard without mixed generations");
|
||
}
|
||
if (id !== "u1") {
|
||
const vendors = dashboard.getByRole("region", { name: "供应商总体产出", exact: true });
|
||
await vendors.getByRole("button", { name: "查看人员" }).first().click();
|
||
const daily = dashboard.getByRole("region", { name: "标注员每日及累计产出", exact: true });
|
||
await daily.locator("tbody tr").first().waitFor();
|
||
await daily.getByRole("navigation", { name: "人员每日产出分页" }).waitFor();
|
||
row.checks.push("supplier/member/daily drilldown");
|
||
await dashboard.getByLabel("项目", { exact: true }).selectOption("p1");
|
||
const projectResult = page.waitForResponse(
|
||
(r) => new URL(r.url()).searchParams.get("projectId") === "p1" && r.status() === 200,
|
||
);
|
||
await dashboard.getByRole("button", { name: "查询", exact: true }).click();
|
||
await projectResult;
|
||
await tasks.getByRole("button", { name: "查看详情" }).first().waitFor();
|
||
assert.match(await tasks.getByRole("navigation").innerText(), /第 1–/);
|
||
row.checks.push("project filtering resets pagination");
|
||
if (id === "admin") {
|
||
assert.equal(await dashboard.getByLabel("项目", { exact: true }).locator("option").count(), 3);
|
||
await dashboard.getByLabel("项目", { exact: true }).selectOption("p2");
|
||
const switched = page.waitForResponse(
|
||
(r) => new URL(r.url()).searchParams.get("projectId") === "p2" && r.status() === 200,
|
||
);
|
||
await dashboard.getByRole("button", { name: "查询", exact: true }).click();
|
||
await switched;
|
||
await tasks.getByRole("button", { name: "查看详情" }).first().waitFor();
|
||
row.checks.push("switch between projects without reset");
|
||
}
|
||
}
|
||
await page.screenshot({ path: path.join(out, `${id}-implementation.png`), fullPage: true });
|
||
await page.reload({ waitUntil: "domcontentloaded" });
|
||
await dashboard.waitFor();
|
||
assert.equal(await dashboard.getAttribute("data-accent"), "teal");
|
||
assert.equal(await dashboard.getAttribute("data-density"), "compact");
|
||
await dashboard.getByRole("button", { name: "调整布局", exact: true }).click();
|
||
await settings.getByRole("button", { name: "恢复默认", exact: true }).click();
|
||
await settings.getByRole("button", { name: "保存布局", exact: true }).click();
|
||
assert.equal(await dashboard.getAttribute("data-accent"), "orange");
|
||
row.checks.push("layout persists after reload and restores defaults");
|
||
assert.deepEqual(row.pageErrors, []);
|
||
assert.deepEqual(row.unexpectedWrites, []);
|
||
await page.close();
|
||
} catch (e) {
|
||
row.error = e.message;
|
||
report.failures.push({ id, error: e.message });
|
||
} finally {
|
||
await ctx.close();
|
||
fs.writeFileSync(path.join(out, "browser-report.json"), JSON.stringify(report, null, 2));
|
||
}
|
||
}
|
||
assert.equal(report.failures.length, 0, JSON.stringify(report.failures));
|
||
console.log(JSON.stringify(report));
|
||
} finally {
|
||
await browser.close();
|
||
}
|