Files
MikrotikManager/backend/src/services/server-poll-approval-context.test.ts
T

89 lines
4.8 KiB
TypeScript

import assert from "node:assert/strict"
import { randomUUID } from "node:crypto"
import { test } from "node:test"
import { projectServerPollApprovalContext } from "./server-poll-approval-context.js"
import { ServerPollProblem, type ServerPollContext } from "./server-poll-execution-model.js"
const initial = (): ServerPollContext & { partitionParents: { oid: string }[] } => ({
servers: [{ id: 1, name: "synthetic", enabled: true, routingEndpointEpoch: randomUUID(), host: "192.0.2.1" }],
settings: { traffic: { retentionDays: 14, pollConfigurationEpoch: randomUUID() },
uptime: { retentionDays: 14, pollConfigurationEpoch: randomUUID() },
internetPath: { retentionDays: 14, pollConfigurationEpoch: randomUUID() } },
partitions: [{ namespace: "public", parent: "server_snapshots", name: "server_snapshots_w_20000103",
from: "2000-01-03T00:00:00.000Z", to: "2000-01-10T00:00:00.000Z", storage: {}, catalog: { oid: "555" } }],
snapshots: [], protections: [], partitionParents: [{ oid: "777" }],
})
const project = (context: ServerPollContext) => projectServerPollApprovalContext(context, 1)
test("collector runtime-only changes preserve approval comparison and leave full source untouched", () => {
const source = initial(), before = structuredClone(source), expected = project(source)
for (const row of Object.values(source.settings)) {
assert(row)
row.lastCollectedAt = "2026-10-30T01:00:00.000Z"; row.lastDurationMs = 17
row.lastError = "synthetic collector state"; row.updatedAt = "2026-10-30T01:00:01.000Z"
}
const completeSource = structuredClone(source)
assert.deepEqual(project(source), expected)
assert.deepEqual(source, completeSource)
assert.notDeepEqual(source, before)
assert.notDeepEqual(expected, source)
})
test("every settings group retains epoch, retention, unknown configuration and createdAt", () => {
const source = initial(), expected = project(source)
for (const key of ["traffic", "uptime", "internetPath"] as const) {
for (const [field, value] of Object.entries({ pollConfigurationEpoch: randomUUID(), retentionDays: 15,
enabled: false, intervalSec: 42, createdAt: "2026-10-30T01:00:00.000Z", future: { nested: [1, true] } })) {
const changed = structuredClone(source)
changed.settings[key]![field] = value
assert.notDeepEqual(project(changed), expected, key + ":" + field)
}
}
})
test("configuration ABA remains changed even when all visible values were restored", () => {
const source = initial(), expected = project(source), changed = structuredClone(source)
changed.settings.uptime!.retentionDays = 15
changed.settings.uptime!.pollConfigurationEpoch = randomUUID()
changed.settings.uptime!.retentionDays = 14
changed.settings.uptime!.pollConfigurationEpoch = randomUUID()
assert.notDeepEqual(project(changed), expected)
})
test("servers, snapshots, protections and unknown catalog sources remain exact", () => {
const source = initial(), expected = project(source)
const modifications: ((value: ReturnType<typeof initial>) => void)[] = [
value => { value.servers[0].updatedAt = "2026-10-30T01:00:00.000Z" },
value => { value.servers[0].host = "192.0.2.2" },
value => { value.servers[0].routingEndpointEpoch = randomUUID() },
value => { value.snapshots.push({ id: 1, serverId: 1, polledAt: "2026-10-30T01:00:00.000Z" }) },
value => { value.protections.push({ kind: "server", id: "1" }) },
value => { value.partitionParents[0].oid = "778" },
value => { value.partitions[0].catalog = { oid: "556" } },
value => { value.partitions[0].storage = { fillfactor: "70" } },
value => { value.servers[0].lastCollectedAt = "2026-10-30T01:00:00.000Z" },
]
for (const modify of modifications) {
const changed = structuredClone(source); modify(changed)
assert.notDeepEqual(project(changed), expected)
}
const result = project(source)
result.context.servers[0].host = "192.0.2.3"
assert.equal(source.servers[0].host, "192.0.2.1")
})
test("missing/null/forged epochs and incomplete source refuse without fallback", () => {
for (const key of ["traffic", "uptime", "internetPath"] as const) {
for (const epoch of [undefined, null, "", "00000000-0000-0000-0000-000000000000", "caller-selected"]) {
const source = initial()
if (epoch === undefined) delete source.settings[key]!.pollConfigurationEpoch
else source.settings[key]!.pollConfigurationEpoch = epoch
assert.throws(() => project(source), error => error instanceof ServerPollProblem && error.code === "STATE_UNAVAILABLE")
}
const source = initial(); source.settings[key] = null
assert.throws(() => project(source), error => error instanceof ServerPollProblem && error.code === "STATE_UNAVAILABLE")
}
const source = initial(); source.servers = []
assert.throws(() => project(source), error => error instanceof ServerPollProblem && error.code === "STATE_UNAVAILABLE")
})