38 lines
2.0 KiB
JavaScript
Raw Normal View History

"use strict";
const test = require("node:test");
const assert = require("node:assert/strict");
const fs = require("node:fs");
const os = require("node:os");
const path = require("node:path");
const { MapGate } = require("./map-gate");
test("non-map actions stay locked until the exact current map is acknowledged", () => {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "map-gate-"));
try {
const maps = path.join(dir, "maps"); fs.mkdirSync(maps);
fs.writeFileSync(path.join(maps, "JD-FD-PRIMARY.json"), JSON.stringify({ node_id: "JD-FD-PRIMARY", modules: [{ code: "JD-GTW-01" }] }));
const gate = new MapGate({ mapsDir: maps, stateFile: path.join(dir, "state.json") });
const map = gate.read("JD-FD-PRIMARY");
assert.equal(gate.verify("session-token", "JD-FD-PRIMARY", map.hash).reason, "map_not_acknowledged");
assert.equal(gate.ack("session-token", "JD-FD-PRIMARY", map.hash, 100, 3600).ok, true);
assert.equal(gate.verify("session-token", "JD-FD-PRIMARY", map.hash, 101).ok, true);
assert.equal(gate.verify("session-token", "OTHER", map.hash, 101).reason, "map_target_mismatch");
} finally { fs.rmSync(dir, { recursive: true, force: true }); }
});
test("changing the server map invalidates an old acknowledgement", () => {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "map-gate-"));
try {
const maps = path.join(dir, "maps"); fs.mkdirSync(maps);
const file = path.join(maps, "JD-FD-PRIMARY.json");
fs.writeFileSync(file, JSON.stringify({ node_id: "JD-FD-PRIMARY", modules: ["A"] }));
const gate = new MapGate({ mapsDir: maps });
const oldMap = gate.read("JD-FD-PRIMARY");
gate.ack("session-token", "JD-FD-PRIMARY", oldMap.hash, 100, 3600);
fs.writeFileSync(file, JSON.stringify({ node_id: "JD-FD-PRIMARY", modules: ["A", "B"] }));
const newMap = gate.read("JD-FD-PRIMARY");
assert.notEqual(oldMap.hash, newMap.hash);
assert.equal(gate.verify("session-token", "JD-FD-PRIMARY", newMap.hash, 101).reason, "map_changed");
} finally { fs.rmSync(dir, { recursive: true, force: true }); }
});