droboticflow/tests/browser/connection-diagnostics.mjs

100 lines
4.6 KiB
JavaScript

import assert from "node:assert/strict";
import fs from "node:fs/promises";
import { chromium } from "playwright";
const base = process.env.DIAGNOSTICS_TEST_URL || "http://127.0.0.1:5197";
assert(["127.0.0.1", "localhost"].includes(new URL(base).hostname));
const out = "local-data/verification/anonymous-diagnostics-20260926";
await fs.mkdir(out, { recursive: true });
const browser = await chromium.launch({
executablePath: "/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge",
headless: true,
args: ["--no-proxy-server"],
});
try {
const ctx = await browser.newContext({ viewport: { width: 900, height: 740 } });
await ctx.addCookies([{ name: "droboticflow_session", value: "PRIVATE_COOKIE", url: base }]);
await ctx.addInitScript(() => {
Object.defineProperty(window, "isSecureContext", { value: false });
Object.defineProperty(navigator, "locks", { value: undefined });
Object.defineProperty(navigator, "clipboard", { value: undefined });
Object.defineProperty(window, "localStorage", {
get() {
throw new Error("blocked storage");
},
});
});
const page = await ctx.newPage(),
calls = [],
errors = [];
let failed = false;
page.on("pageerror", (e) => errors.push(e.message));
const handle = async (route) => {
const r = route.request(),
u = new URL(r.url());
assert.equal(r.method(), "GET");
assert(!r.headers().cookie, "anonymous probes must not send session cookies");
calls.push(u.pathname);
assert(["/api/health", "/api/auth/bootstrap-status", "/diagnostics-probe.json"].includes(u.pathname));
const data =
u.pathname === "/api/health"
? { status: "ok", buildCommit: "a".repeat(40) }
: u.pathname.includes("bootstrap")
? { bootstrapRequired: false }
: { diagnosticProbe: "droboticflow-connectivity-v1" };
await route.fulfill({ status: failed ? 503 : 200, json: data, headers: { "X-Request-ID": "a".repeat(32) } });
};
await page.route(base + "/api/**", handle);
await page.route(base + "/diagnostics-probe.json*", handle);
await page.goto(base + "/diagnostics?itemId=human-item-test&taskId=annotation-task-test");
await page.getByRole("heading", { name: "连接检查" }).waitFor();
assert.equal(calls.length, 0);
await page.getByRole("button", { name: "开始检查", exact: true }).click();
await page.getByText("基础连接正常", { exact: true }).waitFor();
assert.deepEqual(calls, ["/diagnostics-probe.json", "/api/health", "/api/auth/bootstrap-status"]);
const report = JSON.parse(await page.getByLabel("诊断报告").inputValue());
assert(report.checks.every((c) => c.state === "pass"));
assert.doesNotMatch(await page.locator('[aria-label="检查结果"]').innerText(), /401|需核对|登录状态|锁定|版本/);
assert.doesNotMatch(JSON.stringify(report), /PRIVATE_COOKIE|human-item-test|annotation-task-test/);
await page.getByRole("button", { name: "复制报告" }).click();
await page.getByRole("status").filter({ hasText: "请下载报告" }).waitFor();
const event = page.waitForEvent("download");
await page.getByRole("button", { name: "下载报告" }).click();
const download = await event;
assert.deepEqual(JSON.parse(await fs.readFile(await download.path(), "utf8")), report);
await page.screenshot({ path: out + "/normal.png", fullPage: true });
failed = true;
await page.getByRole("button", { name: "开始检查", exact: true }).click();
await page.getByText("检测到连接问题,请发送报告协助排查。", { exact: true }).waitFor();
assert.equal(await page.getByText("基础连接正常", { exact: true }).count(), 0);
await page.setViewportSize({ width: 390, height: 740 });
await page.screenshot({ path: out + "/failure-mobile.png", fullPage: true });
await page.evaluate(() => {
window.fetch = undefined;
});
await page.getByRole("button", { name: "开始检查", exact: true }).click();
await page.getByText("检查未完成,可下载报告或重新检查。", { exact: true }).waitFor();
assert.equal(await page.getByText("检测到连接问题,请发送报告协助排查。", { exact: true }).count(), 0);
assert.equal(errors.length, 0, errors.join("\n"));
await fs.writeFile(
out + "/browser-report.json",
JSON.stringify(
{
passed: true,
checks: [
"cookie-free public probes",
"no login/task gate",
"HTTP without optional features passes",
"compact report and clipboard fallback",
"real outages fail",
"no render errors",
],
},
null,
2,
),
);
console.log("Anonymous diagnostics browser regression passed");
} finally {
await browser.close();
}