| 1 | import assert from "node:assert/strict"; |
| 2 | import { probe } from "../service/substation/monitor.js"; |
| 3 | |
| 4 | const requestId = "2c42c339-cd80-4f22-a4d7-61a411bba5e8"; |
| 5 | const replies = [ |
| 6 | [401, { error: "unauthorized" }], |
| 7 | [200, { object: "list", data: [] }], |
| 8 | [200, { version: "fixture" }], |
| 9 | ]; |
| 10 | let calls = []; |
| 11 | let scenario = "healthy"; |
| 12 | globalThis.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 | }; |
| 22 | for (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 | } |
| 35 | console.log("Substation monitor: authentication, redirect rejection, response validation, request IDs and private failures passed"); |