import assert from "node:assert/strict" import { test } from "node:test" import { controlPathVrfOwner } from "./control-path-model.js" import { isCommunityPathEvidence, pathProofTag, PATH_PROOF_VERSION } from "./path-proof-purpose.js" import { routeAiProfileSchema } from "@mmapp/contracts/mcp-routing" test("control-path and Community proofs require explicit distinct purpose and version", () => { assert.notEqual(pathProofTag("control-path"), pathProofTag("community")) assert.equal(isCommunityPathEvidence({ proofPurpose: "community", proofVersion: PATH_PROOF_VERSION }), true) for (const value of [{}, { proofPurpose: "control-path", proofVersion: PATH_PROOF_VERSION }, { proofPurpose: "community" }, { proofPurpose: "community", proofVersion: 1 }]) assert.equal(isCommunityPathEvidence(value), false) }) test("control-path membership respects row order and refuses unknown earlier interface lists", () => { const interfaces = new Set(["wan", "other"]), main = { name: "main", interfaces: "all", disabled: "false" }, diag = { name: "diag", interfaces: "wan", disabled: "false" } assert.equal(controlPathVrfOwner([diag, main], interfaces, "wan"), "diag") assert.equal(controlPathVrfOwner([main, diag], interfaces, "wan"), "main") assert.equal(controlPathVrfOwner([{ ...diag, disabled: "true" }, main], interfaces, "wan"), "main") for (const row of [{ ...diag, interfaces: "WAN-LIST" }, { ...diag, disabled: undefined }, { ...diag, invalid: "true" }]) assert.throws(() => controlPathVrfOwner([row, main], interfaces, "wan")) assert.throws(() => controlPathVrfOwner([main], interfaces, "missing")) }) test("path admission restricts each hop to its candidate devices before context acquisition", () => { const candidate = { id: "a", wanInterface: "wan", downstreamInterface: "down", jumpHostId: 2, exitNodeId: 3, gateway: "192.0.2.1", gatewayRef: { resource: "ipv4_route", id: "*r" }, controlProbe: { targetInterface: "target", expectedHops: [{ serverId: 1, interface: "wan" }, { serverId: 2, interface: "hop" }, { serverId: 3, interface: "target" }] } } const profile = { name: "scoped hops", serverId: 1, community: "65000:21", family: "ipv4", candidates: [candidate, { ...candidate, id: "b", jumpHostId: 4, exitNodeId: 5, controlProbe: undefined }] } assert.equal(routeAiProfileSchema.safeParse(profile).success, true) for (const serverId of [4, 99]) { const invalid = { ...profile, candidates: [{ ...candidate, controlProbe: { ...candidate.controlProbe, expectedHops: [{ serverId, interface: "foreign" }] } }, profile.candidates[1]] } assert.equal(routeAiProfileSchema.safeParse(invalid).success, false, "a hop outside the selected candidate cannot extend the credential scope, including another candidate's device") } })