Files
MikrotikManager/backend/src/services/server-poll-maintenance-groups.test.ts
T

99 lines
7.7 KiB
TypeScript

import assert from "node:assert/strict"
import test from "node:test"
import { groupAtomicMaintenance, publicationLayout, compileAtomicMaintenanceGroup, projectAtomicMaintenance } from "./server-poll-maintenance-groups.js"
import { planServerPollMaintenanceV2, projectServerPollEffect, type ServerPollContext, type ServerPollEffect } from "./server-poll-execution-model.js"
const context: ServerPollContext = { servers: [], settings: { traffic: null, uptime: null, internetPath: null }, partitions: [], snapshots: [], protections: [] }
const manifest = planServerPollMaintenanceV2(context, "2026-10-30T00:00:00.000Z")
test("actual complete planner manifest retains order and every effect in bounded copied groups", () => {
const groups = groupAtomicMaintenance(manifest)
assert.deepEqual(groups.flatMap(group => group.effects), manifest)
assert(groups.every((group, index) => group.kind === "partition.atomic" && group.offset === index * 8 && group.effects.length <= 8 && group.effects.length > 0))
assert.equal(groups.length, Math.ceil(manifest.length / 8))
groups[0].effects[0].name = "changed-copy"
assert.notEqual(groups[0].effects[0].name, (manifest[0] as { name: string }).name)
})
test("143 and maximum2000 logical effects retain original mutation cost and full coverage", () => {
for (const count of [0, 1, 8, 9, 143, 336, 2000]) {
const maintenance = Array.from({ length: count }, (_, index) => manifest[index % manifest.length])
const groups = groupAtomicMaintenance(maintenance)
assert.deepEqual(groups.flatMap(group => group.effects), maintenance)
assert.deepEqual(publicationLayout({ version: 4, maintenance, mutationCost: count + 8, maintenanceGroupSize: 8 }),
{ logicalMutationCost: count + 8, journalMaxStages: Math.ceil(count / 8) + 8, maintenancePublications: Math.ceil(count / 8) })
}
})
test("legacy1/2/3 never group and reject a new field, even explicit undefined", () => {
for (const version of [1, 2, 3]) {
const plan = { version, maintenance: manifest, mutationCost: manifest.length + 8 }
assert.equal(publicationLayout(plan).maintenancePublications, manifest.length)
assert.throws(() => publicationLayout({ ...plan, maintenanceGroupSize: 8 }))
assert.throws(() => publicationLayout({ ...plan, maintenanceGroupSize: undefined }))
}
})
test("new protocol rejects missing/custom group size, old cost substitution and unknown version", () => {
const plan = { version: 4, maintenance: manifest, mutationCost: manifest.length + 8, maintenanceGroupSize: 8 }
for (const maintenanceGroupSize of [undefined, 0, 1, 7, 9, 2000, Infinity]) assert.throws(() => publicationLayout({ ...plan, maintenanceGroupSize }))
assert.throws(() => publicationLayout({ ...plan, mutationCost: Math.ceil(manifest.length / 8) + 8 }))
assert.throws(() => publicationLayout({ ...plan, version: 5 }))
})
test("nonmaintenance, foreign identifiers, injected SQL and unknown effect fields refuse", () => {
const invalid: unknown[] = [null, { kind: "flow-cache.invalidate", serverId: 1 }, { kind: "snapshot.insert", snapshot: {} },
{ kind: "partition.drop", parent: "mcp_connections", name: "mcp_connections_d_20261030" },
{ kind: "partition.drop", parent: "server_snapshots", name: "server_snapshots_w_20261026;DROP TABLE servers" },
{ kind: "partition.drop", parent: "server_snapshots", name: "server_snapshots_w_20261026", sql: "SELECT 1" },
{ kind: "partition.storage", parent: "server_snapshots", name: "server_snapshots_w_20261026", settings: {} }]
for (const item of invalid) assert.throws(() => groupAtomicMaintenance([item as ServerPollEffect]))
assert.throws(() => groupAtomicMaintenance(Array(2001).fill(manifest[0])))
assert.throws(() => groupAtomicMaintenance(Array(1)))
})
test("range/leaf name disagreement, invalid date, noncanonical time and arbitrary storage refuse", () => {
const valid = { kind: "partition.ensure", parent: "server_snapshots", name: "server_snapshots_w_20261026", from: "2026-10-26T00:00:00.000Z", to: "2026-11-02T00:00:00.000Z" } as const
assert.equal(groupAtomicMaintenance([valid]).length, 1)
for (const changed of [{ to: "2026-11-03T00:00:00.000Z" }, { from: "2026-10-27T00:00:00.000Z" },
{ from: "2026-10-26T01:00:00.000Z" }, { name: "server_snapshots_w_20261102" }, { from: "2026-10-26" }, { from: "invalid" }]) {
assert.throws(() => groupAtomicMaintenance([{ ...valid, ...changed }]))
}
const storage = { kind: "partition.storage", parent: "flow_hour_facts", name: "flow_hour_facts_d_20261030",
settings: { fillfactor: "70", autovacuum_vacuum_scale_factor: "0.05", autovacuum_vacuum_cost_limit: "2000" } } as const
assert.equal(groupAtomicMaintenance([storage]).length, 1)
assert.throws(() => groupAtomicMaintenance([{ ...storage, settings: { ...storage.settings, fillfactor: "100" } }]))
assert.throws(() => groupAtomicMaintenance([{ ...storage, settings: { ...storage.settings, arbitrary: "1" } }]))
})
test("compiler accepts only the exact group slice, without caller SQL, omission or reordering", () => {
const groups = groupAtomicMaintenance(manifest), group = groups[0]
const sql = compileAtomicMaintenanceGroup(manifest, group)
assert(sql.startsWith("DO $poll_atomic_maintenance$ BEGIN "))
assert(sql.endsWith(" END $poll_atomic_maintenance$;"))
assert.equal((sql.match(/CREATE TABLE IF NOT EXISTS|ALTER TABLE|DROP TABLE IF EXISTS/g) ?? []).length, group.effects.length)
assert(!/\b(COMMIT|ROLLBACK|GRANT|TRUNCATE|CASCADE)\b/.test(sql))
for (const changed of [{ ...group, offset: 1 }, { ...group, offset: 8 }, { ...group, sql: "SELECT 1" },
{ ...group, effects: group.effects.slice(1) }, { ...group, effects: [...group.effects].reverse() },
{ ...group, effects: [group.effects[0], ...group.effects] }, { ...group, offset: -8 }, { ...group, offset: Infinity }]) {
assert.throws(() => compileAtomicMaintenanceGroup(manifest, changed))
}
})
test("compiler preserves the final short group and fixed storage/drop statements", () => {
const maintenance: ServerPollEffect[] = [
{ kind: "partition.drop", parent: "server_snapshots", name: "server_snapshots_w_20261026" },
{ kind: "partition.storage", parent: "flow_hour_facts", name: "flow_hour_facts_d_20261030", settings: { fillfactor: "70", autovacuum_vacuum_scale_factor: "0.05", autovacuum_vacuum_cost_limit: "2000" } }]
const sql = compileAtomicMaintenanceGroup(maintenance, groupAtomicMaintenance(maintenance)[0])
assert(sql.includes('DROP TABLE IF EXISTS "public"."server_snapshots_w_20261026";'))
assert(sql.includes('ALTER TABLE "public"."flow_hour_facts_d_20261030" SET (fillfactor=70,autovacuum_vacuum_scale_factor=0.05,autovacuum_vacuum_cost_limit=2000);'))
assert.throws(() => compileAtomicMaintenanceGroup([], { kind: "partition.atomic", offset: 0, effects: [] }))
})
test("group projection preserves effect order and isolates source and ACK rows", () => {
const maintenance = manifest.filter(effect => "parent" in effect && effect.parent === "flow_hour_facts").slice(0, 8)
const [group] = groupAtomicMaintenance(maintenance)
const effects = group.effects.map(effect => effect.kind === "partition.ensure"
? { row: { parent: effect.parent, name: effect.name, from: effect.from, to: effect.to, namespace: "public", storage: {} } }
: {})
const expected = group.effects.reduce((current, effect, index) => projectServerPollEffect(current, effect, effects[index]), context)
assert.deepEqual(projectAtomicMaintenance(context, maintenance, group, { effects }), expected)
assert.deepEqual(context.partitions, [])
assert(effects.every(ack => !ack.row || Object.keys(ack.row.storage).length === 0))
const incomplete = effects.map((ack, index) => index === effects.length - 2 ? {} : ack)
assert.throws(() => projectAtomicMaintenance(context, maintenance, group, { effects: incomplete }))
assert.deepEqual(context.partitions, [])
})