Files
MikrotikManager/lib/speed-probe-snapshot.test.ts

34 lines
2.4 KiB
TypeScript

import { test } from "node:test"
import assert from "node:assert/strict"
import { emptySpeedProbeObservations } from "@mmapp/contracts"
import { publishSpeedProbeSnapshots } from "./speed-probe-snapshot"
const probe = { id: "p", srcServerId: "1", dstServerId: "2", srcInterface: "ether1", dstInterface: "ether2", protocol: "tcp" as const, direction: "both" as "both" | "transmit" | "receive", durationSec: "10", enabled: true, ...emptySpeedProbeObservations, lastTxAvgMbps: 0 as number | null, lastRxAvgMbps: 80 as number | null, lastStatus: "done" as "done" | "error" | null, lastRunAt: "2026-10-07T00:00:00Z" as string | null }
test("same pair preserves latest zero and snapshot instead of readonly values copied into a stale draft", () => {
const next = { ...probe, enabled: false, lastTxAvgMbps: 999, lastRxAvgMbps: 999 }
const [saved] = publishSpeedProbeSnapshots([probe], [next])
assert.equal(saved.enabled, false)
assert.equal(saved.lastTxAvgMbps, 0)
assert.equal(saved.lastRxAvgMbps, 80)
assert.equal(saved.lastStatus, "done")
assert.equal(next.lastTxAvgMbps, 999)
})
for (const field of ["srcServerId", "dstServerId", "id"] as const) test(`changed ${field} starts with unknown measurements`, () => {
const [saved] = publishSpeedProbeSnapshots([probe], [{ ...probe, [field]: "3" }])
for (const key of Object.keys(emptySpeedProbeObservations)) assert.equal(saved[key as keyof typeof emptySpeedProbeObservations], null)
assert.equal(probe.lastRxAvgMbps, 80)
})
test("new row and reordered existing rows retain correct ownership without mutation", () => {
const next = { ...probe, id: "new" }
const saved = publishSpeedProbeSnapshots([probe], [next, probe])
assert.equal(saved[0].lastStatus, null)
assert.equal(saved[1].lastTxAvgMbps, 0)
assert.deepEqual(saved.map(p => p.id), ["new", "p"])
assert.equal(next.lastRxAvgMbps, 80)
})
for (const edit of [{ srcInterface: "ether3" }, { dstInterface: "ether3" }, { protocol: "udp" as const }, { direction: "transmit" as const }, { durationSec: "20" }]) test("changed measurement " + Object.keys(edit)[0] + " resets all observations without altering history", () => {
const [saved] = publishSpeedProbeSnapshots([probe], [{ ...probe, ...edit }])
for (const key of Object.keys(emptySpeedProbeObservations)) assert.equal(saved[key as keyof typeof emptySpeedProbeObservations], null)
assert.equal(probe.lastRxAvgMbps, 80)
})