18 lines
1.2 KiB
TypeScript
18 lines
1.2 KiB
TypeScript
import { test } from "node:test"
|
|
import assert from "node:assert/strict"
|
|
import { parseSpeedRunResponse } from "./speed-run-response"
|
|
const receipt = (tx: unknown, rx: unknown, runId = "run-1") => ({ ok: true, result: { runId, txAvgMbps: tx, rxAvgMbps: rx } })
|
|
test("nullable result and measured zero stay distinct", () => {
|
|
assert.equal(parseSpeedRunResponse(receipt(null, 0), "run-1").txAvgMbps, null)
|
|
assert.equal(parseSpeedRunResponse(receipt(null, 0), "run-1").rxAvgMbps, 0)
|
|
assert.equal(parseSpeedRunResponse(receipt(0.25, 10), "run-1").txAvgMbps, 0.25)
|
|
})
|
|
test("invalid or foreign receipts cannot publish measurements", () => {
|
|
for (const raw of [receipt(-1, 0), receipt("10", 0), receipt(NaN, 0), receipt(Infinity, 0), receipt(undefined, 0), receipt(10, 0, "other"), { result: receipt(10, 0).result }, { ok: false, result: receipt(10, 0).result }, null]) {
|
|
assert.throws(() => parseSpeedRunResponse(raw, "run-1"), /Результат операции неизвестен/)
|
|
}
|
|
})
|
|
test("invalid post-test ping rejects the whole receipt", () => {
|
|
assert.throws(() => parseSpeedRunResponse({ ...receipt(10, 0), result: { ...receipt(10, 0).result, afterBtPing: { rttMs: null, lossPct: 101, error: null } } }, "run-1"))
|
|
})
|