157 lines
6.2 KiB
JavaScript
157 lines
6.2 KiB
JavaScript
import { createServer } from "vite";
|
|
import { chromium } from "playwright";
|
|
import { browserExecutableOptions } from "../../scripts/lib/browser-executable.mjs";
|
|
import assert from "node:assert/strict";
|
|
import fs from "node:fs/promises";
|
|
const out = process.env.DASHBOARD_TEST_OUTPUT || "local-data/test-results/dashboard-refresh";
|
|
await fs.mkdir(out, { recursive: true });
|
|
const server = await createServer({
|
|
configFile: "tests/browser/vite.platform-feedback.config.ts",
|
|
server: { host: "127.0.0.1", port: 0 },
|
|
cacheDir: out + "/vite-cache",
|
|
plugins: [
|
|
{
|
|
name: "dashboard-diagnostic-fixture",
|
|
enforce: "pre",
|
|
async transform(code, id) {
|
|
if (id.endsWith("/tests/browser/fixtures/acceptance-playback-rate.tsx"))
|
|
return await fs.readFile("tests/browser/fixtures/dashboard-refresh.tsx", "utf8");
|
|
},
|
|
},
|
|
],
|
|
});
|
|
let browser;
|
|
try {
|
|
await server.listen();
|
|
const origin = server.resolvedUrls.local[0].replace(/\/$/, "");
|
|
browser = await chromium.launch({ ...browserExecutableOptions(), headless: true, args: ["--no-proxy-server"] });
|
|
const page = await browser.newPage({ viewport: { width: 1500, height: 1000 } });
|
|
const errors = [];
|
|
page.on("pageerror", (e) => errors.push(e.message));
|
|
const user = {
|
|
id: "synthetic-ac",
|
|
name: "Synthetic acceptor",
|
|
role: "acceptance_reviewer",
|
|
status: "active",
|
|
supplierId: "synthetic-v",
|
|
};
|
|
const metric = { count: 1, seconds: 3600 };
|
|
const totals = {
|
|
submitted: metric,
|
|
quality: { count: 0, seconds: 0 },
|
|
acceptance: metric,
|
|
rework: { count: 0, seconds: 0 },
|
|
};
|
|
let fail = false;
|
|
let slow = false,
|
|
reads = 0;
|
|
await page.route("**/api/**", async (route) => {
|
|
const path = new URL(route.request().url()).pathname;
|
|
if (!path.startsWith("/api/")) return route.continue();
|
|
if (path === "/api/auth/me")
|
|
return route.fulfill({ json: { user, organization: { id: "synthetic-org", name: "Synthetic" } } });
|
|
if (path === "/api/dashboard/production-quality") {
|
|
reads++;
|
|
if (slow) await new Promise((resolve) => setTimeout(resolve, 1200));
|
|
if (fail) return route.fulfill({ status: 503, json: { error: "看板加载失败,请重试" } });
|
|
const body = {
|
|
snapshot: { id: "synthetic-snapshot", asOf: new Date().toISOString(), stale: false, refreshing: false },
|
|
audienceScope: "self",
|
|
from: "2026-09-10",
|
|
to: "2026-11-10",
|
|
totals,
|
|
deliverable: { count: 0, seconds: 0 },
|
|
quality: [],
|
|
suppliers: { items: [], total: 0, page: 1, pageSize: 5 },
|
|
people: {
|
|
items: [
|
|
{
|
|
id: user.id,
|
|
name: user.name,
|
|
supplierId: user.supplierId,
|
|
supplierName: "Synthetic",
|
|
role: "acceptance",
|
|
totals,
|
|
deliverable: { count: 0, seconds: 0 },
|
|
quality: null,
|
|
},
|
|
],
|
|
total: 1,
|
|
page: 1,
|
|
pageSize: 5,
|
|
},
|
|
projects: [],
|
|
supplierOptions: [],
|
|
users: [],
|
|
};
|
|
return route.fulfill({ json: body });
|
|
}
|
|
return route.fulfill({ json: { items: [] } });
|
|
});
|
|
await page.route("**/dashboard-flicker-repro", async (route) =>
|
|
route.fulfill({
|
|
contentType: "text/html",
|
|
body: `<html><body><div id="root"></div><script type="module">import RefreshRuntime from '/@react-refresh';RefreshRuntime.injectIntoGlobalHook(window);window.$RefreshReg$=()=>{};window.$RefreshSig$=()=>type=>type;window.__vite_plugin_react_preamble_installed__=true;</script><script type="module" src="/tests/browser/fixtures/acceptance-playback-rate.tsx"></script></body></html>`,
|
|
}),
|
|
);
|
|
await page.goto(origin + "/dashboard-flicker-repro");
|
|
await page
|
|
.getByRole("heading", { name: "我的批次质量", exact: true })
|
|
.waitFor({ timeout: 15000 })
|
|
.catch(async (e) => {
|
|
console.log(JSON.stringify({ errors, body: (await page.locator("body").innerText()).slice(0, 1200) }));
|
|
throw e;
|
|
});
|
|
await page.waitForFunction(
|
|
() => document.querySelectorAll(".pq-cards>div").length === 3 && !document.querySelector(".pq-loading"),
|
|
);
|
|
const before = await page.locator(".pq-cards").boundingBox();
|
|
await page.evaluate(() => {
|
|
window.__diagnosticDashboardNode = document.querySelector(".pq-dashboard");
|
|
window.__diagnosticCardsNode = document.querySelector(".pq-cards");
|
|
});
|
|
await page.screenshot({ path: out + "/before.png" });
|
|
slow = true;
|
|
await page.getByRole("button", { name: "刷新", exact: true }).click();
|
|
await page.getByRole("status").filter({ hasText: "正在更新" }).waitFor();
|
|
const during = await page.locator(".pq-cards").boundingBox();
|
|
const report = {
|
|
sameDashboardNode: await page.evaluate(
|
|
() => window.__diagnosticDashboardNode === document.querySelector(".pq-dashboard"),
|
|
),
|
|
sameCardsNode: await page.evaluate(() => window.__diagnosticCardsNode === document.querySelector(".pq-cards")),
|
|
beforeY: before.y,
|
|
duringY: during.y,
|
|
shiftPixels: during.y - before.y,
|
|
dashboardInstances: await page.locator(".pq-dashboard").count(),
|
|
cardCount: await page.locator(".pq-cards>div").count(),
|
|
reads,
|
|
errors,
|
|
};
|
|
await page.screenshot({ path: out + "/during.png" });
|
|
await fs.writeFile(out + "/result.json", JSON.stringify(report, null, 2));
|
|
console.log(JSON.stringify(report));
|
|
assert.equal(report.errors.length, 0);
|
|
assert(Math.abs(report.shiftPixels) < 1, "Refresh must not move the existing dashboard cards");
|
|
await page.getByRole("status").waitFor({ state: "hidden" });
|
|
fail = true;
|
|
await page.getByRole("button", { name: "刷新", exact: true }).click();
|
|
await page.getByRole("alert").filter({ hasText: "看板加载失败" }).waitFor();
|
|
assert.equal(
|
|
await page.locator(".pq-cards strong").filter({ hasText: "1" }).count(),
|
|
0,
|
|
"failed refresh clears stale metric values",
|
|
);
|
|
await page.screenshot({ path: out + "/error.png" });
|
|
fail = false;
|
|
slow = false;
|
|
await page.getByRole("button", { name: "刷新", exact: true }).click();
|
|
await page.getByRole("alert").waitFor({ state: "hidden" });
|
|
await page.waitForFunction(
|
|
() => document.querySelector(".pq-cards strong")?.textContent?.replace(/\s/g, "") === "1小时",
|
|
);
|
|
} finally {
|
|
await browser?.close();
|
|
await server.close();
|
|
}
|