import { strict as assert } from "node:assert"; import test from "node:test"; import { keyOf, tally, targetsFor, type Result, type Roster } from "../reach.js"; const roster: Roster = { node: "here", machines: [ { name: "here", fqdn: "here.internal", address: "10.0.0.1" }, { name: "there", fqdn: "there.internal", address: "10.0.0.2", public: "there.example.test" }, ], }; test("its own machine is checked, which is the case that distinguishes a caller on it from one in a container", () => { const own = targetsFor(roster, 9876).filter((t) => t.claim === "this machine"); assert.equal(own.length, 2, "its own machine by address and by name"); assert.ok(own.some((t) => t.at === "10.0.0.1" && !t.byName)); assert.ok(own.some((t) => t.at === "here.internal" && t.byName)); }); test("every other machine is checked over the private network", () => { const other = targetsFor(roster, 9876).filter((t) => t.claim === "the private network"); assert.deepEqual(other.map((t) => t.machine), ["there", "there"]); }); test("the public claim is only checked where a machine has a public name", () => { const pub = targetsFor(roster, 9876, 443).filter((t) => t.claim === "the public network"); assert.equal(pub.length, 1, "only the machine with a public name"); assert.equal(pub[0]!.at, "there.example.test"); assert.equal(pub[0]!.port, 443); }); test("no public claim is made when no public port was given", () => { assert.equal(targetsFor(roster, 9876).filter((t) => t.claim === "the public network").length, 0); }); const failed = (at: string): Result => ({ claim: "this machine", machine: "here", at, port: 9876, byName: false, ok: false, failed: "connection", ms: 1, }); const passed = (at: string): Result => ({ claim: "this machine", machine: "here", at, port: 9876, byName: false, ok: true, ms: 1, }); test("one failure is not a fault — a machine rebooting is ordinary", () => { const { counts, broken } = tally([failed("10.0.0.1")], {}, 2); assert.equal(broken.length, 0, "one missed dial says nothing"); assert.equal(counts[keyOf(failed("10.0.0.1"))], 1, "and is remembered"); }); test("a path that keeps failing is broken, and the count travels with it", () => { const first = tally([failed("10.0.0.1")], {}, 2); const second = tally([failed("10.0.0.1")], first.counts, 2); assert.equal(second.broken.length, 1); assert.equal(second.broken[0]!.consecutive, 2, "so a reader can tell briefly away from never worked"); }); test("a path that recovers stops being counted", () => { const first = tally([failed("10.0.0.1")], {}, 2); const second = tally([passed("10.0.0.1")], first.counts, 2); assert.equal(second.broken.length, 0); assert.deepEqual(second.counts, {}, "nothing carried forward for a path that works"); }); test("a count follows one path and not another", () => { const a = failed("10.0.0.1"); const b = failed("10.0.0.2"); const first = tally([a, b], {}, 2); const second = tally([a], first.counts, 2); assert.equal(second.broken.length, 1, "only the path dialled this run is judged"); assert.equal(second.broken[0]!.at, "10.0.0.1"); });