58 lines
4.4 KiB
TypeScript
58 lines
4.4 KiB
TypeScript
import assert from "node:assert/strict"
|
|
import { createServer } from "node:http"
|
|
import { MikrotikClient } from "./mikrotik.js"
|
|
import { OspfCostWriteError } from "./ospf-template-cost-write.js"
|
|
import { pool, closePool } from "../db/index.js"
|
|
import { configurationAuditTransactionFixture, emptyActiveRoutingProfilesSqlFixture } from "../test/fixtures/managed-mutation-sql.js"
|
|
let writes = 0
|
|
let reads = 0
|
|
const rows = [{ ".id": "*1", cost: "10", disabled: "false", "auth-key": "NEVER_OSPF_AUTH" }]
|
|
|
|
const http = createServer(async (request, response) => {
|
|
if (request.method === "GET" && request.url === "/rest/routing/ospf/interface-template") { reads++; response.setHeader("Content-Type", "application/json"); response.end(JSON.stringify(rows)); return }
|
|
if (request.method === "PATCH" && request.url?.endsWith("/interface-template/*1")) {
|
|
let body = ""; for await (const chunk of request) body += chunk
|
|
writes++; rows[0].cost = JSON.parse(body).cost
|
|
response.setHeader("Content-Type", "application/json"); response.end("{}"); return
|
|
}
|
|
response.statusCode = 404; response.end("{}")
|
|
})
|
|
await new Promise<void>(resolve => http.listen(0, "127.0.0.1", resolve))
|
|
const originalQuery = pool.query, originalConnect = pool.connect
|
|
const address = http.address(); assert.ok(address && typeof address !== "string")
|
|
pool.connect = (async () => ({ async query(text: string, values?: unknown[]) {
|
|
const admission = configurationAuditTransactionFixture(text, values)
|
|
if (admission) return admission
|
|
if (text.startsWith("INSERT INTO mcp_audit(")) return pool.query(text, values)
|
|
if (text.includes("'mm-config:application'")) { assert.equal(values, undefined); return { rows: [{ acquired: true }] } }
|
|
assert(["SELECT pg_try_advisory_lock(hashtextextended($1,0)) AS acquired", "SELECT pg_advisory_unlock(hashtextextended($1,0))"].includes(text)); assert.deepEqual(values, ["mm-config:71"]); return { rows: [{ acquired: true }] }
|
|
}, release() {} })) as typeof pool.connect
|
|
pool.query = (async (text: string, values?: unknown[]) => {
|
|
const routing = emptyActiveRoutingProfilesSqlFixture(text, values, 71)
|
|
if (routing) return routing
|
|
if (text.startsWith("SELECT host,port,use_ssl,verify_ssl,username,password,enabled,routing_endpoint_epoch FROM servers")) { assert.deepEqual(values, [71]); return { rows: [{ host: "127.0.0.1", port: address.port, use_ssl: false, verify_ssl: false, username: "fixture", password: "fixture", enabled: true, routing_endpoint_epoch: undefined }] } }
|
|
if (text.startsWith("SELECT object_ref FROM mcp_protections WHERE")) { assert.deepEqual(values, ["71"]); return { rows: [] } }
|
|
assert.equal(text, "INSERT INTO mcp_audit(actor_id,action,connection_id,payload) VALUES($1,$2,$3,$4::jsonb)"); assert.equal(JSON.parse(String(values?.[3])).serverId, 71); return { rows: [] }
|
|
}) as typeof pool.query
|
|
try {
|
|
const client = new MikrotikClient({ host: "127.0.0.1", port: address.port, username: "fixture", password: "fixture", useSsl: false, verifySsl: false }, 71)
|
|
await client.setOspfInterfaceTemplateCost("*1", 20, 10)
|
|
assert.equal(writes, 1); assert.equal(rows[0].cost, "20")
|
|
await assert.rejects(client.setOspfInterfaceTemplateCost("*1", 30, 10)); assert.equal(writes, 1)
|
|
let gates=0
|
|
const beforeGateReads=reads
|
|
await assert.rejects(client.setOspfInterfaceTemplateCost("*1",30,20,undefined,async()=>{
|
|
gates++;assert.equal(reads,beforeGateReads+1);assert.equal(writes,1)
|
|
throw Error("NEVER_PUBLISH_SOURCE_ERROR")
|
|
}),error=>error instanceof OspfCostWriteError&&!error.writeAttempted&&!error.message.includes("NEVER_PUBLISH"))
|
|
assert.equal(writes,1);assert.equal(gates,1);assert.equal(rows[0].cost,"20")
|
|
const beforeAcceptedReads=reads
|
|
await client.setOspfInterfaceTemplateCost("*1",30,20,undefined,async()=>{
|
|
gates++;assert.equal(reads,beforeAcceptedReads+1);assert.equal(writes,1);assert.equal(rows[0].cost,"20")
|
|
})
|
|
assert.equal(writes,2);assert.equal(gates,2);assert.equal(reads,beforeAcceptedReads+2)
|
|
await client.setOspfInterfaceTemplateCost("*1",30,30,undefined,async()=>{throw Error("No-op must not dispatch")})
|
|
assert.equal(writes,2);assert.equal(gates,2)
|
|
} finally { pool.query = originalQuery; pool.connect = originalConnect; await new Promise<void>(resolve => http.close(() => resolve())); await closePool() }
|
|
console.log("✓ OSPF cost shared writer: expected cost, whole native read-back, opaque/auth preservation, ignored/lost/rejected stop, no replay and actual client HTTP")
|