60 lines
5.4 KiB
TypeScript
60 lines
5.4 KiB
TypeScript
import assert from "node:assert/strict"
|
|
import { createHash } from "node:crypto"
|
|
import { createTrafficFlowSettingsService } from "./traffic-flow-settings-service.js"
|
|
import type { FlowSettingsConfiguration } from "./traffic-flow-settings-execution.js"
|
|
|
|
export function settingsFixture() {
|
|
let configuration: 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: "public-existing", hostPrivateKey: "PRIVATE-EXISTING", peersJson: [{ opaque: "PEER-SECRET" }], future: { opaque: "FUTURE-SECRET" } }
|
|
const intents = new Map<string, unknown>(), effects: string[] = []
|
|
let pinned = false, allowed = true, listenerFailure = false, writeUnknown = false, receiptFailure = false, listenerHook = () => {}
|
|
const proof = async (value: unknown) => createHash("sha256").update(JSON.stringify(value)).digest("hex")
|
|
const store = {
|
|
read: async () => structuredClone(configuration),
|
|
claim: async (id: string, phase: string, _proof: string) => { const key = `${id}:${phase}`; if (intents.has(key)) throw Error("Permanent claim exists"); effects.push(`intent:${phase}`); intents.set(key, null) },
|
|
compareAndSet: async (_id: string, _proof: string, before: FlowSettingsConfiguration, desired: FlowSettingsConfiguration) => { assert.deepEqual(configuration, before); effects.push("write"); configuration = structuredClone(desired); if (writeUnknown) throw Error("lost commit ACK PRIVATE"); return true },
|
|
complete: async (id: string, phase: string, _proof: string, result: unknown) => { effects.push(`complete:${phase}`); if (receiptFailure) throw Error("receipt unavailable PRIVATE"); intents.set(`${id}:${phase}`, result) },
|
|
}
|
|
const update = createTrafficFlowSettingsService({
|
|
store: () => store,
|
|
exclusive: async work => { effects.push("app->flow-lock"); return work() },
|
|
guard: async work => { effects.push("guard-global"); return work() },
|
|
agentPinned: () => pinned, proof,
|
|
startListener: async authorize => { await authorize?.(); effects.push("listener:stop"); listenerHook(); await authorize?.(); effects.push("listener:flush"); if (listenerFailure) throw Error("listener PRIVATE unavailable"); await authorize?.(); effects.push("listener:start-request") },
|
|
})
|
|
return { update, store, proof, effects, intents, get configuration() { return configuration },
|
|
authorize: async () => { if (!allowed) throw Error("revoked PRIVATE") },
|
|
pin: () => { pinned = true }, revoke: () => { allowed = false },
|
|
listenerHook: (hook: () => void) => { listenerHook = hook },
|
|
failListener: () => { listenerFailure = true }, unknownWrite: () => { writeUnknown = true }, failReceipt: () => { receiptFailure = true } }
|
|
}
|
|
const all = { enabled: false, collectorIp: "10.255.253.1", flowListenPort: 4740, wgListenPort: 51822, prefix: "10.255.253.0/24", publicEndpoint: "flow.test", hubServerId: 2, retentionHours: 48, topN: 100, mapServiceMinSharePct: 0 }
|
|
for (const patch of [all, { mapServiceMinSharePct: 0 }, { enabled: false }, { topN: 200 }]) {
|
|
const f = settingsFixture(), before = structuredClone(f.configuration), receipt = await f.update(patch, { authorize: f.authorize })
|
|
assert.equal(receipt.status, "applied"); assert.equal(receipt.receiptConfirmed, true); assert.equal(receipt.listenerReadinessConfirmed, false)
|
|
assert.deepEqual(f.configuration, { ...before, ...patch })
|
|
assert.deepEqual(f.effects, ["app->flow-lock", "guard-global", "intent:configuration", "write", "intent:listener", "listener:stop", "listener:flush", "listener:start-request", "complete:configuration", "complete:listener"])
|
|
assert.ok(!JSON.stringify(receipt).includes("PRIVATE")); assert.ok(!JSON.stringify(receipt).includes("SECRET"))
|
|
await assert.rejects(f.update(patch, { receiptId: receipt.receiptId })); assert.equal(f.effects.filter(x => x === "write").length, 1)
|
|
}
|
|
for (const patch of [{ collectorIp: "10.255.253.1" }, { prefix: "10.255.253.0/24" }, { flowListenPort: 4740 }, { wgListenPort: 51822 }]) {
|
|
const f = settingsFixture(); f.pin(); await assert.rejects(f.update(patch), /закреплены/); assert.equal(f.effects.includes("write"), false)
|
|
}
|
|
{
|
|
const f = settingsFixture(); f.pin(); assert.equal((await f.update({ collectorIp: "10.255.254.1", mapServiceMinSharePct: 0 })).status, "applied")
|
|
}
|
|
for (const fail of ["listener", "receipt", "revoke"] as const) {
|
|
const f = settingsFixture()
|
|
if (fail === "listener") f.failListener()
|
|
if (fail === "receipt") f.failReceipt()
|
|
if (fail === "revoke") f.listenerHook(f.revoke)
|
|
const receipt = await f.update({ topN: 100 }, { authorize: f.authorize })
|
|
assert.equal(receipt.configurationConfirmed, true); assert.equal(receipt.status, "partial"); assert.ok(receipt.listenerError)
|
|
assert.ok(!JSON.stringify(receipt).includes("PRIVATE")); assert.equal(f.effects.filter(x => x === "write").length, 1)
|
|
if (fail === "revoke") assert.equal(f.effects.includes("listener:flush"), false)
|
|
}
|
|
{
|
|
const f = settingsFixture(); f.unknownWrite(); await assert.rejects(f.update({ topN: 100 }), /не подтверждено/)
|
|
assert.equal(f.configuration.topN, 100); assert.equal(f.effects.includes("listener:stop"), false)
|
|
}
|
|
console.log("NetFlow full settings service: all ten fields, no-op/display restart, full configuration preservation, permanent two-phase intents, agent pin, partial/unknown sanitized no replay; inert dependencies only")
|