/** * The rack view: which unit a piece of hardware says it sits in, how the racks are made from that, clashes and what is not placed. * * Run with: npm test --workspace server */ import { test, describe } from "node:test"; import assert from "node:assert/strict"; import { buildRacks, parseRackUnit, type RackAsset } from "../src/services/racks.js"; const asset = (id: number, name: string, location: string | null, rackUnit: string | null, extra: Partial = {}): RackAsset => ({ id, name, kind: "server", manufacturer: null, model: null, location, rackUnit, powerWatts: null, serverId: null, ...extra }); describe("reading a unit", () => { test("a unit, or a run of them, in the ways people write them", () => { assert.deepEqual(parseRackUnit("U12"), { from: 12, to: 12 }); assert.deepEqual(parseRackUnit("u3"), { from: 3, to: 3 }); assert.deepEqual(parseRackUnit("12"), { from: 12, to: 12 }); assert.deepEqual(parseRackUnit("U12-13"), { from: 12, to: 13 }); assert.deepEqual(parseRackUnit("12 – 14"), { from: 12, to: 14 }); assert.deepEqual(parseRackUnit("U14-U12"), { from: 12, to: 14 }, "either way round"); assert.equal(parseRackUnit("top shelf"), null); assert.equal(parseRackUnit("U0"), null); assert.equal(parseRackUnit(""), null); assert.equal(parseRackUnit(null), null); }); }); describe("making racks", () => { test("hardware is grouped by where it says it is, placed by unit, in a rack of a real size", () => { const racks = buildRacks([asset(1, "Switch", "Rack A", "U40", { kind: "network", powerWatts: 40 }), asset(2, "NAS", "Rack A", "U10-13", { powerWatts: 120 }), asset(3, "UPS", "Rack A", "U1-3", { kind: "power" }), asset(4, "Laptop", "Desk", null)]); assert.equal(racks.length, 1, "a desk with no unit is not a rack"); const a = racks[0]; assert.deepEqual([a.name, a.units, a.usedUnits, a.watts], ["Rack A", 42, 1 + 4 + 3, 160]); assert.deepEqual(a.slots.map((s) => [s.name, s.from, s.to]), [["UPS", 1, 3], ["NAS", 10, 13], ["Switch", 40, 40]], "from the bottom up"); assert.ok(a.slots.every((s) => !s.clash)); }); test("a small rack is drawn small, and a tall piece makes it as tall as it needs", () => { assert.equal(buildRacks([asset(1, "A", "R", "U5")])[0].units, 12); assert.equal(buildRacks([asset(1, "A", "R", "U13")])[0].units, 18); assert.equal(buildRacks([asset(1, "A", "R", "U43-48")])[0].units, 48); assert.equal(buildRacks([asset(1, "A", "R", "U60-70")])[0].units, 70, "beyond any real rack it just fits"); }); test("two pieces on the same unit clash, and both are said to", () => { const [rack] = buildRacks([asset(1, "A", "R", "U4-5"), asset(2, "B", "R", "U5"), asset(3, "C", "R", "U8")]); assert.deepEqual(rack.slots.map((s) => [s.name, s.clash]), [["A", true], ["B", true], ["C", false]]); assert.equal(rack.usedUnits, 3, "a unit is used once however many claim it"); }); test("hardware that names a rack and no unit is listed beside it, not placed", () => { const [rack] = buildRacks([asset(1, "NAS", "Rack A", "U10"), asset(2, "Cable box", "Rack A", null), asset(3, "Patch panel", "Rack A", "somewhere")]); assert.deepEqual(rack.unplaced.map((u) => u.name), ["Cable box", "Patch panel"]); }); test("a unit with no place goes in a rack of its own, last; racks are in order, numbers as numbers", () => { const racks = buildRacks([asset(1, "X", null, "U2"), asset(2, "Y", "Rack 10", "U1"), asset(3, "Z", "Rack 2", "U1")]); assert.deepEqual(racks.map((r) => r.name), ["Rack 2", "Rack 10", "(no rack named)"]); }); });