96 lines
6.6 KiB
TypeScript
96 lines
6.6 KiB
TypeScript
/** Inert unit fixtures only: not evidence of real startup, PG, runtime or speed. */
|
|
import assert from "node:assert/strict"
|
|
import { test } from "node:test"
|
|
import { planServerPollMaintenance, planServerPollMaintenanceV2, projectServerPollEffect, type ServerPollContext, type ServerPollEffect, type PollRow } from "./server-poll-execution-model.js"
|
|
|
|
const around = "2026-10-29T12:00:00.000Z"
|
|
const cold = (): ServerPollContext => ({ servers: [], settings: { traffic: null, uptime: null, internetPath: null }, partitions: [], snapshots: [], protections: [] })
|
|
function apply(context: ServerPollContext, effects: ServerPollEffect[]) {
|
|
let current = structuredClone(context)
|
|
for (const effect of effects) {
|
|
const row = effect.kind === "partition.ensure" ? { namespace: "public", parent: effect.parent, name: effect.name, from: effect.from, to: effect.to, storage: {}, opaqueUnitField: "kept" } : undefined
|
|
current = projectServerPollEffect(current, effect, { row })
|
|
}
|
|
return current
|
|
}
|
|
function catalogue(context: ServerPollContext) { return [...context.partitions].sort((a, b) => String(a.name).localeCompare(String(b.name))) }
|
|
test("snapshot JSONB object key order does not change values or hide field/type/array drift", () => {
|
|
const before = cold(), effect: ServerPollEffect = { kind: "snapshot.insert", snapshot: {
|
|
serverId: 1, polledAt: "2026-10-30T00:00:00.000Z", status: "online",
|
|
rawIpAddresses: [{ ".id": "*b", interface: "ether1", address: "192.0.2.1/24" }],
|
|
rawInterfaces: [{ name: "ether1", flags: { running: true, disabled: false } }],
|
|
} }
|
|
const row: PollRow = { id: 1, ...effect.snapshot,
|
|
rawIpAddresses: [{ ".id": "*b", address: "192.0.2.1/24", interface: "ether1" }],
|
|
rawInterfaces: [{ flags: { disabled: false, running: true }, name: "ether1" }],
|
|
}
|
|
assert.deepEqual(projectServerPollEffect(before, effect, { row }).snapshots, [row])
|
|
for (const mutate of [
|
|
(row: PollRow) => { row.rawIpAddresses = [{ ".id": "*b", address: "192.0.2.2/24", interface: "ether1" }] },
|
|
(row: PollRow) => { row.rawInterfaces = [{ flags: { disabled: false, running: true, opaque: "changed" }, name: "ether1" }] },
|
|
(row: PollRow) => { row.serverId = 2 },
|
|
(row: PollRow) => { row.rawInterfaces = [{ flags: { disabled: false, running: "true" }, name: "ether1" }] },
|
|
(row: PollRow) => { row.rawIpAddresses = [] },
|
|
]) {
|
|
const changed = structuredClone(row); mutate(changed)
|
|
assert.throws(() => projectServerPollEffect(before, effect, { row: changed }))
|
|
}
|
|
assert.throws(() => projectServerPollEffect(before, { ...effect, snapshot: { ...effect.snapshot, rawInterfaces: [{ name: "a" }, { name: "b" }] } },
|
|
{ row: { ...row, rawInterfaces: [{ name: "b" }, { name: "a" }] } }))
|
|
assert.deepEqual(before.snapshots, [])
|
|
})
|
|
function warm() { const source = cold(); return apply(source, planServerPollMaintenance(source, around)) }
|
|
|
|
test("cold V2 retains every actual legacy CREATE/ALTER and does not mutate input", () => {
|
|
const source = cold(), before = structuredClone(source)
|
|
assert.deepEqual(planServerPollMaintenanceV2(source, around), planServerPollMaintenance(source, around))
|
|
assert.deepEqual(source, before)
|
|
})
|
|
test("complete warm catalogue retains first-partial-window DROP/CREATE, while eliding only exact live noops", () => {
|
|
const source = warm(), old = planServerPollMaintenance(source, around), next = planServerPollMaintenanceV2(source, around)
|
|
assert(old.some(e => e.kind === "partition.ensure")); assert(next.length < old.length)
|
|
for (const drop of old.filter(e => e.kind === "partition.drop")) {
|
|
assert(next.some(e => e.kind === "partition.drop" && e.name === drop.name))
|
|
for (const recreation of old.filter(e => e.kind !== "partition.drop" && "name" in e && e.name === drop.name)) assert(next.includes(recreation) || next.some(e => JSON.stringify(e) === JSON.stringify(recreation)))
|
|
}
|
|
assert.deepEqual(catalogue(apply(source, next)), catalogue(apply(source, old)))
|
|
})
|
|
test("missing fact leaf retains CREATE and ALTER even when predecessor's storage matched", () => {
|
|
const source = warm(), fact = source.partitions.find(r => r.parent === "flow_hour_facts")!
|
|
source.partitions = source.partitions.filter(r => r.name !== fact.name)
|
|
const effects = planServerPollMaintenanceV2(source, around)
|
|
assert(effects.some(e => e.kind === "partition.ensure" && e.name === fact.name))
|
|
assert(effects.some(e => e.kind === "partition.storage" && e.name === fact.name))
|
|
assert.deepEqual(catalogue(apply(source, effects)), catalogue(apply(source, planServerPollMaintenance(source, around))))
|
|
})
|
|
test("wrong bound and wrong parent refuse even when same name is scheduled for drop/recreate", () => {
|
|
for (const field of ["from", "to", "parent", "namespace"] as const) {
|
|
const source = warm(), target = source.partitions.find(r => r.parent === "flow_hour_facts")!
|
|
target[field] = "wrong"
|
|
assert.throws(() => planServerPollMaintenanceV2(source, around), { code: "STATE_UNAVAILABLE" })
|
|
}
|
|
})
|
|
test("changed and missing required storage preserve ALTER; exact extra keys survive elision", () => {
|
|
for (const mode of ["changed", "missing", "extra"] as const) {
|
|
const source = warm(), legacy = planServerPollMaintenance(source, around)
|
|
const drops = new Set(legacy.filter(e => e.kind === "partition.drop").map(e => e.name))
|
|
const target = source.partitions.find(r => r.parent === "flow_hour_facts" && !drops.has(String(r.name)))!
|
|
assert(target)
|
|
const storage = target.storage as Record<string, string>
|
|
if (mode === "changed") storage.fillfactor = "80"
|
|
if (mode === "missing") delete storage.fillfactor
|
|
if (mode === "extra") storage.autovacuum_enabled = "false"
|
|
const before = structuredClone(source), next = planServerPollMaintenanceV2(source, around)
|
|
assert.equal(next.some(e => e.kind === "partition.storage" && e.name === target.name), mode !== "extra")
|
|
assert.deepEqual(source, before)
|
|
assert.deepEqual(catalogue(apply(source, next)), catalogue(apply(source, planServerPollMaintenance(source, around))))
|
|
}
|
|
})
|
|
test("arbitrary unrelated partitions and opaque storage/catalogue fields survive unchanged", () => {
|
|
const source = warm(); source.partitions.push({ namespace: "public", parent: "server_snapshots", name: "server_snapshots_custom", from: null, to: null, storage: { custom: "opaque" }, catalogue: { exact: 1 } })
|
|
const legacy = planServerPollMaintenance(source, around)
|
|
const before = structuredClone(legacy), next = planServerPollMaintenanceV2(source, around)
|
|
assert.deepEqual(planServerPollMaintenance(source, around), before, "V1 meaning must remain byte-for-byte stable")
|
|
assert.deepEqual(catalogue(apply(source, next)), catalogue(apply(source, legacy)))
|
|
})
|