droboticflow/tests/browser/dashboard-presentation-replica.mjs

90 lines
4.9 KiB
JavaScript

import { browserExecutableOptions } from "../../scripts/lib/browser-executable.mjs";
import assert from "node:assert/strict";
import { readFile, writeFile } from "node:fs/promises";
import { chromium } from "playwright";
import {
dashboardPresentationKey,
defaultDashboardPresentation,
} from "../../src/features/annotation-output/presentation.ts";
const directory = process.env.ANNOTATION_OUTPUT_VERIFY_DIR || "local-data/verification/customer-dashboard-20261020";
const accounts = JSON.parse(
await readFile(process.env.ANNOTATION_OUTPUT_ACCOUNTS || `${directory}/accounts.json`, "utf8"),
);
assert.match(accounts.origin, /^http:\/\/127\.0\.0\.1:\d+$/);
assert.match(accounts.database, /^annotation_output_browser_\d+$/);
const scope = (id) => JSON.stringify(["output-browser-org", id, id === "admin" ? "admin" : "annotator"]);
const browser = await chromium.launch({
...browserExecutableOptions(),
headless: true,
args: ["--no-proxy-server"],
});
const report = { checks: [], pageErrors: [], passed: false };
try {
const context = await browser.newContext({ viewport: { width: 1440, height: 1100 }, serviceWorkers: "block" });
await context.route("**/*", (route) =>
new URL(route.request().url()).origin === accounts.origin ? route.continue() : route.abort(),
);
const page = await context.newPage();
page.on("pageerror", (e) => report.pageErrors.push(e.message));
const login = async (id) => {
const a = accounts.accounts.find((x) => x.id === id);
const response = await context.request.post(accounts.origin + "/api/auth/login", {
data: { phone: a.phone, password: a.password },
});
assert.equal(response.status(), 200);
await page.goto(accounts.origin + "/?view=dashboard", { waitUntil: "domcontentloaded" });
await page.locator(".annotation-output-card strong").first().waitFor();
};
await login("admin");
const dashboard = page.locator(".annotation-output");
await dashboard.getByRole("button", { name: "调整布局", exact: true }).click();
await dashboard.getByLabel("强调色").selectOption("blue");
await dashboard.getByLabel("显示密度").selectOption("compact");
await dashboard.getByRole("button", { name: "保存布局", exact: true }).click();
await login("u1");
assert.equal(await dashboard.getAttribute("data-accent"), "orange");
assert.equal(await dashboard.locator('[data-output-section="suppliers"]').count(), 0);
await login("admin");
assert.equal(await dashboard.getAttribute("data-accent"), "blue");
report.checks.push("same-browser account switching isolates preferences and role content");
await page.evaluate(({ key, config }) => localStorage.setItem(key, JSON.stringify(config)), {
key: dashboardPresentationKey(scope("admin")),
config: { ...defaultDashboardPresentation(), role: "admin", css: "body{display:none}" },
});
await page.reload({ waitUntil: "domcontentloaded" });
await dashboard.waitFor();
assert.equal(await dashboard.getAttribute("data-accent"), "orange");
report.checks.push("tampered stored document falls back without executing or hiding content");
await page.evaluate(() => {
const original = Storage.prototype.setItem;
Storage.prototype.setItem = function (key, value) {
if (key.startsWith("droboticflow.presentation:")) throw new DOMException("Blocked", "QuotaExceededError");
return original.call(this, key, value);
};
});
await dashboard.getByRole("button", { name: "调整布局", exact: true }).click();
await dashboard.getByLabel("强调色").selectOption("teal");
await dashboard.getByRole("button", { name: "保存布局", exact: true }).click();
await dashboard.getByRole("alert").filter({ hasText: "无法保存布局" }).waitFor();
await dashboard.getByRole("button", { name: "取消", exact: true }).click();
assert.equal(await dashboard.getAttribute("data-accent"), "orange");
report.checks.push("storage failure retains preview and Cancel restores saved state");
await page.reload({ waitUntil: "domcontentloaded" });
await dashboard.getByRole("button", { name: "调整布局", exact: true }).click();
await dashboard.getByLabel("强调色").selectOption("blue");
await page.screenshot({ path: `${directory}/layout-settings.png`, fullPage: true });
await page.setViewportSize({ width: 900, height: 1100 });
await page.screenshot({ path: `${directory}/layout-settings-narrow.png`, fullPage: true });
assert.equal(await dashboard.getByRole("button", { name: "保存布局", exact: true }).isVisible(), true);
await page.keyboard.press("Escape");
assert.equal(await dashboard.getByRole("region", { name: "调整看板布局", exact: true }).count(), 0);
report.checks.push("narrow layout controls remain reachable; Escape cancels preview");
assert.deepEqual(report.pageErrors, []);
report.passed = true;
} finally {
await browser.close();
await writeFile(`${directory}/presentation-report.json`, JSON.stringify(report, null, 2) + "\n");
console.log(JSON.stringify(report));
}