import assert from "node:assert/strict" import { planIpsecUserUpdate as plan, projectIpsecUserUpdate as project } from "./ipsec-user-update-model.js" import type { ServerRow } from "../modules/servers/repository/servers-repository.js" import type { IpsecPeerDeleteState } from "./ipsec-peer-delete.js" const server = { id: 1, name: "fixture", host: "fixture", enabled: true } as ServerRow const paths = ["/ip/ipsec/peer", "/ip/ipsec/identity", "/ip/ipsec/mode-config", "/ip/pool", "/ip/ipsec/policy", "/ip/firewall/nat", "/certificate", "/ip/ipsec/profile", "/ip/ipsec/proposal", "/ip/ipsec/policy/group", "/ip/ipsec/active-peers"] const base = (): IpsecPeerDeleteState => ({ source: "source", proof: "proof", protections: [{ object_ref: { kind: "server", id: "1" } }], catalogs: { ...Object.fromEntries(paths.map(path => [path, []])), "/ip/ipsec/peer": [{ ".id": "*p", name: "ike", "exchange-mode": "ike2", passive: "yes", opaque: "peer" }], "/ip/ipsec/identity": [{ ".id": "*i", peer: "ike", "auth-method": "pre-shared-key", "remote-id": "key-id:alice", "mode-config": "mc-ipsec-alice", comment: "MikrotikManager:ipsec user=Alice", disabled: "true", opaque: "identity" }], "/ip/ipsec/mode-config": [{ ".id": "*m", name: "mc-ipsec-alice", address: "192.0.2.1", opaque: "old" }, { ".id": "*s", name: "ipsec-vpn", "address-pool": "pool", opaque: "shared" }], "/ip/pool": [{ ".id": "*l", name: "pool", ranges: "192.0.2.1-192.0.2.20" }], "/certificate": [{ ".id": "*a", name: "ca", "key-usage": "key-cert-sign", authority: "true" }, { ".id": "*c", name: "ipsec-user-Alice", "common-name": "Alice", "key-usage": "tls-client", ca: "ca", opaque: "private" }, { ".id": "*v", name: "ipsec-server", "key-usage": "tls-server", ca: "ca" }], } }) const s = base(), before = structuredClone(s) assert.deepEqual(plan(s, server, "identity:*i", {}).commands, []) const full = plan(s, server, "identity:*i", { name: "Bob", staticIp: "192.0.2.9", psk: "new-secret", remoteId: "bob", disabled: false }) assert.deepEqual(full.commands.map(c => [c.action, c.path]), [["update", "/ip/ipsec/identity"], ["create", "/ip/ipsec/mode-config"], ["update", "/ip/ipsec/identity"], ["delete", "/ip/ipsec/mode-config"], ["update", "/ip/ipsec/identity"]]) let next = s for (const [_index, command] of full.commands.entries()) next = project(next, command, command.action === "create" ? { assignedId: "*new", createdRow: { ".id": "*new", ...command.fields, responder: "true", "native-default": "retained" } } : {}) assert.deepEqual(s, before) assert.deepEqual(next.protections, s.protections) assert.deepEqual(next.catalogs["/ip/ipsec/identity"][0], { ...s.catalogs["/ip/ipsec/identity"][0], comment: "MikrotikManager:ipsec user=Bob", "mode-config": "mc-ipsec-bob", secret: "new-secret", "remote-id": "key-id:bob", disabled: "no" }) assert.deepEqual(next.catalogs["/ip/ipsec/mode-config"].map(row => row[".id"]), ["*s", "*new"]) assert.equal(next.catalogs["/ip/ipsec/mode-config"][1]["native-default"], "retained") for (const path of paths.filter(p => !["/ip/ipsec/identity", "/ip/ipsec/mode-config"].includes(p))) assert.deepEqual(next.catalogs[path], s.catalogs[path]) assert.throws(() => plan(s, server, "identity:*i", { name: "Bob", staticIp: "" }), /IPv4/) assert.throws(() => plan(s, server, "identity:*i", { name: "Bob", staticIp: " " }), /IPv4/) assert.throws(() => project(s, full.commands[1], { createdRow: { ".id": "*new", ...full.commands[1].fields, address: "192.0.2.99" } }), /не подтверждены/) assert.throws(() => project(s, full.commands[1], { assignedId: "*m", createdRow: { ".id": "*m", ...full.commands[1].fields } }), /Не подтверждён/) const pooled = plan(s, server, "identity:*i", { staticIp: null }) assert.deepEqual(pooled.commands.map(c => c.action), ["update", "delete"]) assert.equal(pooled.commands[0].fields["mode-config"], "ipsec-vpn") const noShared = base(); noShared.catalogs["/ip/ipsec/mode-config"].pop() assert.deepEqual(plan(noShared, server, "identity:*i", { staticIp: null }).commands, []) const shared = base(); shared.catalogs["/ip/ipsec/identity"].push({ ".id": "*j", peer: "ike", "auth-method": "pre-shared-key", comment: "Other", "mode-config": "mc-ipsec-alice" }) assert.equal(plan(shared, server, "identity:*i", { staticIp: null }).commands.length, 1) const foreign = base(); foreign.catalogs["/ip/ipsec/mode-config"].push({ ".id": "*f", name: "mc-ipsec-bob", address: "192.0.2.8", "address-pool": "pool", "static-dns": "192.0.2.53", opaque: "foreign" }) foreign.catalogs["/ip/ipsec/identity"].push({ ".id": "*j", peer: "ike", "auth-method": "pre-shared-key", comment: "Other", "mode-config": "mc-ipsec-bob" }) const foreignPlan = plan(foreign, server, "identity:*i", { name: "Bob", staticIp: "192.0.2.9" }) assert.equal(foreignPlan.commands[1].action, "update") assert.equal(foreignPlan.commands[1].id, "*f") const foreignProjected = project(foreign, foreignPlan.commands[1]) assert.deepEqual(foreignProjected.catalogs["/ip/ipsec/mode-config"][2], { ...foreign.catalogs["/ip/ipsec/mode-config"][2], ...foreignPlan.commands[1].fields }) assert(foreignPlan.companions.some(row => row.kind === "ipsec_identity" && row.id === "*j")) assert(foreignPlan.companions.some(row => row.kind === "ip_pool" && row.id === "*l")) const unmanaged = base(); unmanaged.catalogs["/ip/ipsec/identity"][0].comment = "Alice" assert.equal(plan(unmanaged, server, "identity:*i", { name: "Bob" }).commands[0].fields.comment, "Bob") assert.equal(plan(s, server, "identity:*i", { name: " " }).commands[0].fields.comment, "MikrotikManager:ipsec user=") assert.deepEqual(plan(s, server, "identity:*i", { name: "Alice" }).commands, []) const collision = base(); collision.catalogs["/ip/ipsec/identity"].push({ ".id": "*j", peer: "ike", "auth-method": "pre-shared-key", "remote-id": "key-id:bob", comment: "MikrotikManager:ipsec user=Bob" }) assert.throws(() => plan(collision, server, "identity:*i", { name: "Bob" }), /Имя/) assert.throws(() => plan(collision, server, "identity:*i", { remoteId: "bob" }), /Remote ID/) const certOnly = base(); certOnly.catalogs["/ip/ipsec/identity"] = [{ ".id": "*listener", peer: "ike", "auth-method": "digital-signature", certificate: "ipsec-server", "mode-config": "ipsec-vpn" }] const renameCert = plan(certOnly, server, "cert:*c", { name: "Bob", psk: "ignored-psk", remoteId: "ignored", disabled: true, staticIp: null }) assert.equal(renameCert.commands.length, 1) assert.equal(renameCert.commands[0].action, "create") assert.equal(renameCert.commands[0].fields["remote-certificate"], "ipsec-user-Alice") assert.equal(renameCert.commands[0].fields.comment, "MikrotikManager:ipsec user=Bob") assert.equal(renameCert.commands[0].fields["mode-config"], "ipsec-vpn") assert.equal(renameCert.commands[0].fields.secret, undefined) assert.deepEqual(renameCert.ignoredFields, ["psk", "remoteId", "disabled", "staticIp"]) const staticCert = plan(certOnly, server, "cert:*c", { name: "Bob", staticIp: "192.0.2.9", disabled: false }) assert.deepEqual(staticCert.commands.map(c => c.action), ["create", "create"]) assert.equal(staticCert.commands[1].fields["mode-config"], "mc-ipsec-bob") assert.equal(staticCert.commands[1].fields.disabled, undefined) const missingServerCert = structuredClone(certOnly) missingServerCert.catalogs["/ip/ipsec/identity"] = [] missingServerCert.catalogs["/certificate"] = missingServerCert.catalogs["/certificate"].filter(row => row.name !== "ipsec-server") assert.throws(() => plan(missingServerCert, server, "cert:*c", { staticIp: "192.0.2.9" }), /сертификат/) const fallbackServerCert = structuredClone(missingServerCert) fallbackServerCert.catalogs["/certificate"].push({ ".id": "*fallback", name: "ipsec-server" }) assert.equal(plan(fallbackServerCert, server, "cert:*c", { staticIp: "192.0.2.9" }).commands[1].fields.certificate, "ipsec-server") const paddedServerCert = structuredClone(fallbackServerCert) paddedServerCert.catalogs["/certificate"].find(row => row[".id"] === "*fallback")!.name = " ipsec-server " assert.throws(() => plan(paddedServerCert, server, "cert:*c", { staticIp: "192.0.2.9" }), /точный серверный/) const boolNamedCommand = { ...renameCert.commands[0], fields: { ...renameCert.commands[0].fields, comment: "yes" } } assert.throws(() => project(certOnly, boolNamedCommand, { createdRow: { ".id": "*new", ...boolNamedCommand.fields, comment: "true" } }), /не подтверждены/) for (const flag of ["dynamic", "builtin", "default"]) for (const value of ["true", "yes", "unknown", ""]) { assert.throws(() => project(certOnly, renameCert.commands[0], { createdRow: { ".id": "*new", ...renameCert.commands[0].fields, [flag]: value } }), /флаг/) } const acknowledged = project(certOnly, renameCert.commands[0], { createdRow: { ".id": "*new", ...renameCert.commands[0].fields, dynamic: "false", builtin: "no", default: "false", opaque: "read-only native defaults" } }) assert.equal(acknowledged.catalogs["/ip/ipsec/identity"].at(-1)!.opaque, "read-only native defaults") assert.deepEqual(plan(certOnly, server, "cert:*c", { disabled: false }).commands, []) const ambiguous = base(); ambiguous.catalogs["/ip/ipsec/mode-config"].push({ ".id": "*dup", name: "mc-ipsec-alice" }) assert.throws(() => plan(ambiguous, server, "identity:*i", {}), /Неоднозначное/) const missingCatalog = base(); delete missingCatalog.catalogs["/ip/ipsec/profile"] assert.throws(() => plan(missingCatalog, server, "identity:*i", {}), /Неполный/) const certAlias = base(); certAlias.catalogs["/certificate"].push({ ".id": "*d", name: "*c", "key-usage": "tls-client", ca: "ca" }) assert.throws(() => plan(certAlias, server, "cert:*c", {}), /Неоднозначный/) const protectedState = base(); protectedState.catalogs["/ip/ipsec/identity"][0].dynamic = "unknown" assert.throws(() => plan(protectedState, server, "identity:*i", { disabled: true }), /Системный/) console.log("✓ IPsec user update planner: full PATCH branches, cert promotion, no-op/ignored fields, upsert consumers, managed cleanup, immutable raw projection, ACK defaults and ambiguity guards")