Files
MikrotikManager/shared/api/collector-settings-mutations.test.ts
T

25 lines
2.0 KiB
TypeScript

import assert from "node:assert/strict"
import { test } from "node:test"
import { putTrafficCollectorSettings, putServersApiPingSettings, putUptimeCollectorSettings, putInternetPathSettings } from "./collector-settings"
const common = { enabled: false, intervalSec: 300, retentionDays: 14, lastCollectedAt: null, lastDurationMs: 0, lastError: null }
const uptime = { ...common, resourcesEnabled: false, pingEnabled: false, speedEnabled: false, probeIntervalSec: 15, speedIntervalSec: 60 }
for (const [name, put, path, dto] of [
["traffic", putTrafficCollectorSettings, "/api/traffic/settings", common],
["REST ping", putServersApiPingSettings, "/api/servers-api-ping/settings", common],
["uptime", putUptimeCollectorSettings, "/api/uptime/settings", uptime],
["internet", putInternetPathSettings, "/api/internet-path/settings", common],
] as const) test(name + ": selective PUT requires exact explicit receipt without retries", async () => {
const original = globalThis.fetch; let calls = 0
try {
globalThis.fetch = async (url, init) => { calls++; assert.equal(new URL(String(url)).pathname, path); assert.equal(init?.method, "PUT"); assert.deepEqual(JSON.parse(String(init.body)), { intervalSec: 300 }); return new Response(JSON.stringify({ ok: true, settings: dto })) }
assert.equal((await put("http://fixture.test", { intervalSec: 300 })).intervalSec, 300); assert.equal(calls, 1)
for (const raw of [{}, { ok: false, settings: dto }, { ok: true, settings: {} }, { ok: true, settings: { ...dto, intervalSec: 301 } }]) {
calls = 0; globalThis.fetch = async () => { calls++; return new Response(JSON.stringify(raw)) }
await assert.rejects(() => put("http://fixture.test", { intervalSec: 300 }), /подтвер/); assert.equal(calls, 1)
}
for (const payload of [{ intervalSec: 0 }, { intervalSec: 30.5 }, { intervalSec: NaN }, {}]) {
calls = 0; await assert.rejects(() => put("http://fixture.test", payload)); assert.equal(calls, 0)
}
} finally { globalThis.fetch = original }
})