import assert from "node:assert/strict" import { createFlowSettingsExecution, type FlowSettingsConfiguration, type FlowSettingsDependencies, type FlowSettingsPhase } from "./traffic-flow-settings-execution.js" import { reviewFlowSettingsPlan } from "../modules/mcp/flow-settings-review.js" const baseline = (): FlowSettingsConfiguration => ({ id: 1, enabled: false, collectorIp: "10.255.254.1", flowListenPort: 4739, wgListenPort: 51821, prefix: "10.255.254.0/24", publicEndpoint: "", hubServerId: null, retentionHours: 24, topN: 200, mapServiceMinSharePct: 5, hostPublicKey: "", hostPrivateKey: "", peersJson: [{ serverId: 1, publicKey: "peer-public", allowedIps: ["10.255.254.2/32"] }], createdAt: "initial", futureConfig: { keep: true } }) function fixture() { let state = baseline(), writes = 0, starts = 0, generations = 0, reads = 0 let denied = false let onRead = () => {}, onWrite = () => {}, onStart = () => {}, onIntent = (_phase: FlowSettingsPhase) => {} const phases: string[] = [] const deps: FlowSettingsDependencies = { authorize: async () => { if (denied) throw new Error("revoked") }, exclusive: async work => work(), readConfiguration: async () => { reads++; onRead(); return structuredClone(state) }, intent: async phase => { phases.push(phase); onIntent(phase) }, compareAndSet: async (_expected, desired) => { writes++; state = structuredClone(desired); onWrite(); return true }, generateKeys: () => { generations++; return { publicKey: "synthetic-public", privateKey: "synthetic-private" } }, startListener: async fence => { await fence(); starts++; onStart() }, } return { engine: createFlowSettingsExecution(deps), deps, phases, get state() { return state }, get writes() { return writes }, get starts() { return starts }, get reads() { return reads }, get generations() { return generations }, deny: () => { denied = true }, readHook: (fn: typeof onRead) => { onRead = fn }, writeHook: (fn: typeof onWrite) => { onWrite = fn }, startHook: (fn: typeof onStart) => { onStart = fn }, intentHook: (fn: typeof onIntent) => { onIntent = fn } } } let cases = 0 const patches = [ { enabled: true }, { enabled: false }, { collectorIp: "10.255.254.3" }, { flowListenPort: 4740 }, { wgListenPort: 51822 }, { prefix: "10.200.0.0/24" }, { publicEndpoint: "flow.example" }, { hubServerId: 3 }, { hubServerId: null }, { retentionHours: 2147483647 }, { topN: 1000 }, { mapServiceMinSharePct: 0 }, { mapServiceMinSharePct: 100 } ] for (const patch of [...patches, Object.assign({}, ...patches)]) { const f = fixture(), before = structuredClone(f.state) const plan = await f.engine.prepare({ kind: "patch", patch }) const receipt = await f.engine.execute(plan) assert.equal(receipt.status, "applied"); assert.equal(receipt.configurationConfirmed, true) assert.equal(receipt.listenerReadinessConfirmed, false); assert.equal(f.starts, 1); assert.equal(f.writes, 1) assert.deepEqual(f.state, { ...before, ...patch }); assert.deepEqual(f.phases, ["configuration", "listener"]) assert.equal(reviewFlowSettingsPlan(plan).collectorRestartRequired, true); cases++ } for (const patch of [{}, { bad: true }, { hostPrivateKey: "forbidden" }, { flowListenPort: 0 }, { flowListenPort: 65536 }, { topN: 1001 }, { retentionHours: 0 }, { mapServiceMinSharePct: -1 }, { enabled: "false" }, { hubServerId: 0 }, { prefix: "::/64" }]) { const f = fixture() await assert.rejects(f.engine.prepare({ kind: "patch", patch: patch as never })) assert.equal(f.reads, 0); assert.equal(f.writes, 0); assert.equal(f.starts, 0); cases++ } for (const mutate of [ (state: FlowSettingsConfiguration) => { state.hostPrivateKey = "changed" }, (state: FlowSettingsConfiguration) => { state.peersJson = [] }, (state: FlowSettingsConfiguration) => { state.futureConfig = { keep: false } }, ]) { const f = fixture(), plan = await f.engine.prepare({ kind: "patch", patch: { topN: 20 } }) mutate(f.state); await assert.rejects(f.engine.execute(plan)); assert.equal(f.writes, 0); assert.equal(f.starts, 0); cases++ } for (const phase of ["configuration", "listener"] as const) { const f = fixture(), plan = await f.engine.prepare({ kind: "patch", patch: { topN: 20 } }) f.intentHook(current => { if (current === phase) f.deny() }) if (phase === "configuration") { await assert.rejects(f.engine.execute(plan)); assert.equal(f.writes, 0) } else { assert.equal((await f.engine.execute(plan)).status, "partial"); assert.equal(f.writes, 1) } assert.equal(f.starts, 0); cases++ } for (const phase of ["write", "listener", "read-after-write", "read-after-listener"] as const) { const f = fixture(), plan = await f.engine.prepare({ kind: "patch", patch: { topN: 20 } }) if (phase === "write") f.writeHook(() => { throw new Error("synthetic-private") }) if (phase === "listener") f.startHook(() => { throw new Error("synthetic-private") }) if (phase === "read-after-write") f.writeHook(() => f.readHook(() => { throw new Error("synthetic-private") })) if (phase === "read-after-listener") f.startHook(() => f.readHook(() => { throw new Error("synthetic-private") })) const receipt = await f.engine.execute(plan) assert.equal(receipt.status, ["write", "read-after-write"].includes(phase) ? "unknown" : "partial") assert.equal(f.writes, 1); assert.ok(f.starts <= 1); assert.equal(receipt.retries, 0) assert.ok(!JSON.stringify(receipt).includes("synthetic-private")); cases++ } for (const pair of [["", ""], ["old-public", ""], ["", "old-private"], ["old-public", "old-private"]]) { const f = fixture(); [f.state.hostPublicKey, f.state.hostPrivateKey] = pair const original = structuredClone(f.state), present = Boolean(pair[0] && pair[1]) const plan = await f.engine.prepare({ kind: "ensure-keys" }), receipt = await f.engine.execute(plan) assert.equal(receipt.status, "applied"); assert.equal(receipt.created, !present) assert.equal(f.generations, present ? 0 : 1); assert.equal(f.writes, present ? 0 : 1); assert.equal(f.starts, 0) assert.deepEqual(f.state, present ? original : { ...original, hostPublicKey: "synthetic-public", hostPrivateKey: "synthetic-private" }) assert.ok(!JSON.stringify({ receipt, review: reviewFlowSettingsPlan(plan) }).includes("private\"")); cases++ } { const f = fixture(), plan = await f.engine.prepare({ kind: "ensure-keys" }) f.deps.compareAndSet = async () => false assert.equal((await f.engine.execute(plan)).status, "unknown"); assert.equal(f.starts, 0); cases++ } for (const key of ["hostPrivateKey", "peersJson", "futureConfig"]) { const f = fixture(), plan = await f.engine.prepare({ kind: "patch", patch: { topN: 30 } }) f.writeHook(() => { f.state[key] = "collateral" }) assert.equal((await f.engine.execute(plan)).status, "unknown"); assert.equal(f.starts, 0); cases++ } { const f = fixture(), plan = await f.engine.prepare({ kind: "patch", patch: { topN: 30 } }) f.deps.startListener = async fence => { f.deny(); await fence(); throw new Error("must not reach side effect") } const receipt = await f.engine.execute(plan) assert.equal(receipt.status, "partial"); assert.equal(receipt.attempts, 2); assert.equal(f.starts, 0); cases++ } console.log(`NetFlow settings candidate: ${cases} full-field, private conservation, intent revocation, lost ACK, listener and key lifecycle cases; injected dependencies only, no SQL/network/collector starts`)