forked from longtao.wu/droboticflow
86 lines
3.6 KiB
JavaScript
86 lines
3.6 KiB
JavaScript
import test from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import { runConnectionDiagnostics } from "../../src/lib/connection-diagnostics.js";
|
|
const json = (data, status = 200) =>
|
|
new Response(JSON.stringify(data), { status, headers: { "X-Request-ID": "a".repeat(32) } });
|
|
const payload = (path) =>
|
|
path.startsWith("/diagnostics-probe.json")
|
|
? { diagnosticProbe: "droboticflow-connectivity-v1" }
|
|
: path.startsWith("/api/health")
|
|
? { status: "ok", buildCommit: "b".repeat(40) }
|
|
: { bootstrapRequired: false };
|
|
test("anonymous and authenticated browsers use identical public probes without cookies or account calls", async () => {
|
|
const calls = [];
|
|
const checks = await runConnectionDiagnostics({
|
|
fetcher: async (path, options) => {
|
|
calls.push({ path, ...options });
|
|
return json({ ...payload(path), passwordHash: "SECRET", phone: "13800000000" });
|
|
},
|
|
});
|
|
assert.equal(checks.length, 3);
|
|
assert(checks.every((c) => c.state === "pass"));
|
|
assert(calls.every((c) => c.method === "GET" && c.credentials === "omit" && !c.body && c.cache === "no-store"));
|
|
assert(!calls.some((c) => /auth\/me|human-items|tasks|projects/.test(c.path)));
|
|
assert.doesNotMatch(JSON.stringify(checks), /SECRET|13800000000|passwordHash/);
|
|
});
|
|
test("a transient failure is retried and recovered connection passes while preserving evidence", async () => {
|
|
let first = true;
|
|
const checks = await runConnectionDiagnostics({
|
|
fetcher: async (path) => {
|
|
if (first) {
|
|
first = false;
|
|
throw new Error("private-secret-url");
|
|
}
|
|
return json(payload(path));
|
|
},
|
|
});
|
|
assert.equal(checks[0].state, "pass");
|
|
assert.equal(checks[0].attempts.length, 2);
|
|
assert.equal(checks[0].attempts[0].reason, "network_or_response");
|
|
assert.doesNotMatch(JSON.stringify(checks), /secret/);
|
|
});
|
|
test("public route authorization failures are real failures, not mistaken for expected unsigned session", async () => {
|
|
const checks = await runConnectionDiagnostics({
|
|
fetcher: async (path) => (path.includes("bootstrap") ? json({}, 401) : json(payload(path))),
|
|
});
|
|
assert.equal(checks[2].state, "fail");
|
|
assert.equal(checks[2].attempts.length, 1);
|
|
});
|
|
test("cancellation halts future checks and cannot become an all-pass report", async () => {
|
|
const c = new AbortController();
|
|
let calls = 0;
|
|
const checks = await runConnectionDiagnostics({
|
|
signal: c.signal,
|
|
timeoutMs: 20,
|
|
fetcher: async (_p, o) => {
|
|
calls++;
|
|
return new Promise((_, reject) => o.signal.addEventListener("abort", () => reject(new Error("abort"))));
|
|
},
|
|
onResult: () => c.abort(),
|
|
});
|
|
assert.equal(calls, 2);
|
|
assert.equal(checks.length, 1);
|
|
assert.equal(checks[0].state, "fail");
|
|
assert.equal(checks[0].attempts[1].reason, "timeout");
|
|
});
|
|
test("HTML fallback and oversized bodies never give false success", async () => {
|
|
const checks = await runConnectionDiagnostics({ fetcher: async () => new Response("x".repeat(70000)) });
|
|
assert(checks.every((c) => c.state === "fail"));
|
|
assert.doesNotMatch(JSON.stringify(checks), /xxxx/);
|
|
});
|
|
test("browsers without streaming response readers still pass valid public checks", async () => {
|
|
const checks = await runConnectionDiagnostics({
|
|
fetcher: async (path) => ({
|
|
ok: true,
|
|
status: 200,
|
|
headers: new Headers(),
|
|
text: async () => JSON.stringify(payload(path)),
|
|
}),
|
|
});
|
|
assert(checks.every((c) => c.state === "pass"));
|
|
});
|
|
test("HTTP 200 with unrelated JSON is not mistaken for successful connectivity", async () => {
|
|
const checks = await runConnectionDiagnostics({ fetcher: async () => json({ unrelated: true }) });
|
|
assert(checks.every((c) => c.state === "fail"));
|
|
});
|