fix(uptime): сохранить неизвестную скорость и проверить подтверждение теста
This commit is contained in:
1 parent
1904c86657
commit
9d5959601f
10 files changed
+160
-58
No files matched your search
@@ -0,0 +1,17 @@
|
||||
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"))
|
||||
})
|
||||
@@ -0,0 +1,24 @@
|
||||
import { z } from "zod"
|
||||
|
||||
const metric = z.number().finite().nonnegative().nullable()
|
||||
const address = z.string().nullable().optional()
|
||||
const response = z.object({
|
||||
ok: z.literal(true),
|
||||
result: z.object({
|
||||
runId: z.string().min(1),
|
||||
txAvgMbps: metric,
|
||||
rxAvgMbps: metric,
|
||||
raw: z.array(z.record(z.string(), z.unknown())).optional(),
|
||||
afterBtPing: z.object({ rttMs: metric, lossPct: z.number().finite().min(0).max(100).nullable(), error: z.string().nullable() }).optional(),
|
||||
srcAddress: address, dstAddress: address,
|
||||
srcInterfaceAddress: address, dstInterfaceAddress: address,
|
||||
}),
|
||||
})
|
||||
|
||||
export function parseSpeedRunResponse(raw: unknown, runId: string) {
|
||||
const parsed = response.safeParse(raw)
|
||||
if (!parsed.success || parsed.data.result.runId !== runId) {
|
||||
throw new Error("Некорректное подтверждение speed-test. Результат операции неизвестен")
|
||||
}
|
||||
return parsed.data.result
|
||||
}
|
||||
Reference in new issue
Block a user