import assert from "node:assert/strict" import { readOspfTemplateFlags } from "./ospf-template-flags.js" const native = [{ ".id": "*8", passive: "", disabled: "false", cost: "30", opaque: "preserve" }, { ".id": "*9", disabled: "false" }] const calls: string[] = [] const rows = await readOspfTemplateFlags(native, async (id, key) => { calls.push(id + ":" + key); return { ret: id === "*8" ? "true" : "false" } }) assert.equal(rows[0].passive, "true"); assert.equal(rows[1].passive, "false"); assert.equal(rows[0].opaque, "preserve") assert.equal(native[0].passive, ""); assert.equal("passive" in native[1], false) assert.deepEqual(calls.sort(), ["*8:passive", "*9:passive"]) for (const ret of ["", undefined, null, "unknown", true]) await assert.rejects(readOspfTemplateFlags(native, async () => ({ ret }))) let dispatched = 0 await assert.rejects(readOspfTemplateFlags(Array.from({ length: 129 }, (_, index) => ({ ".id": "*" + index.toString(16), disabled: "false" })), async () => { dispatched++; return { ret: "false" } })) assert.equal(dispatched, 0) await assert.rejects(readOspfTemplateFlags([native[0]], async () => { dispatched++; return { ret: "true" } }, async () => { throw new Error("grant revoked") }), /grant revoked/) assert.equal(dispatched, 0, "Revoked access must stop before the effective getter") const unset = [{ ".id": "*9", disabled: "false" }] const noValue = async () => [] const negativeReceipt = async () => ({ present: [], absent: [{ ".id": "*9" }] }) assert.equal((await readOspfTemplateFlags(unset, noValue, undefined, negativeReceipt))[0].passive, "false") await assert.rejects(readOspfTemplateFlags(unset, noValue), "An empty getter alone is not false") for (const receipt of [ { present: [], absent: [] }, { present: [{ ".id": "*9" }], absent: [{ ".id": "*9" }] }, { present: [{ ".id": "*foreign" }], absent: [{ ".id": "*9" }] }, { present: {}, absent: [{ ".id": "*9" }] }, ]) await assert.rejects(readOspfTemplateFlags(unset, noValue, undefined, async () => receipt)) let fences = 0, membershipCalls = 0 await assert.rejects(readOspfTemplateFlags(unset, noValue, async () => { if (++fences === 1) throw new Error("paused before native query") }, async () => { membershipCalls++; return negativeReceipt() }), /paused/) assert.equal(membershipCalls, 0) const many = Array.from({ length: 129 }, (_, index) => ({ ".id": "*" + index.toString(16), disabled: "false" })) let unusedGetters = 0 assert.equal((await readOspfTemplateFlags(many, async () => { unusedGetters++; return [] }, undefined, async () => ({ present: [], absent: many.map(row => ({ ".id": row[".id"] })) }))).length, 129) assert.equal(unusedGetters, 0, "Bulk switch receipts avoid one getter per unset template") const mixed = [{ ".id": "*8", disabled: "false", passive: "true" }, ...unset] assert.equal((await readOspfTemplateFlags(mixed, noValue, undefined, async () => ({ present: [{ ".id": "*8" }], absent: [{ ".id": "*9" }] })))[1].passive, "false") await assert.rejects(readOspfTemplateFlags(mixed, noValue, undefined, async () => ({ present: [], absent: [{ ".id": "*8" }, { ".id": "*9" }] }))) await assert.rejects(readOspfTemplateFlags([{ ".id": "*8", passive: "garbage", disabled: "false" }], async () => ({ ret: "true" }))) let active = 0, maximum = 0 await readOspfTemplateFlags(Array.from({ length: 20 }, (_, index) => ({ ".id": "*" + index.toString(16) })), async () => { maximum = Math.max(maximum, ++active); await new Promise(resolve => setTimeout(resolve, 1)); active--; return { ret: "false" } }) assert.equal(maximum, 4) console.log("OSPF template flags: observed empty passive switch resolved through native receipt, missing false verified, unknown rejected and getter budget bounded")