1import assert from "node:assert/strict";
2import { probe } from "../service/substation/monitor.js";
3
4const requestId = "2c42c339-cd80-4f22-a4d7-61a411bba5e8";
5const replies = [
6 [401, { error: "unauthorized" }],
7 [200, { object: "list", data: [] }],
8 [200, { version: "fixture" }],
9];
10let calls = [];
11let scenario = "healthy";
12globalThis.fetch = async (url, options) => {
13 const index = calls.length;
14 calls.push({ url: String(url), options });
15 if (scenario === "network") throw new Error("client-private-fixture");
16 const [status, body] = replies[index];
17 return new Response(JSON.stringify(scenario === "body" ? { private: "client-private-fixture" } : body), {
18 status: scenario === "redirect" ? 302 : status,
19 headers: { "X-Request-ID": scenario === "id" ? "client-private-fixture" : requestId },
20 });
21};
22for (scenario of ["healthy", "redirect", "body", "id", "network"]) {
23 calls = [];
24 const checks = await probe("https://substation.invalid", "client-private-fixture", "management-private-fixture");
25 assert.equal(checks.length, 3);
26 assert.equal(checks.every(check => check.ok), scenario === "healthy");
27 assert.ok(calls.every(call => call.options.redirect === "manual" && call.options.signal instanceof AbortSignal));
28 assert.equal(calls[0].options.headers.Authorization, undefined);
29 assert.equal(calls[1].options.headers.Authorization, "Bearer client-private-fixture");
30 assert.equal(calls[2].options.headers.Authorization, "Bearer management-private-fixture");
31 assert.ok(!JSON.stringify(checks).includes("private-fixture"));
32 if (scenario === "healthy") assert.ok(checks.every(check => check.requestId === requestId));
33 if (scenario === "id" || scenario === "network") assert.ok(checks.every(check => check.requestId === null));
34}
35console.log("Substation monitor: authentication, redirect rejection, response validation, request IDs and private failures passed");