37 lines
3.3 KiB
TypeScript
37 lines
3.3 KiB
TypeScript
import assert from "node:assert/strict"
|
|
import { MAX_LOG_EXPORT_BYTES, serializeLogExport, LogExportLimitError, withLogExportSlot, LogExportBusyError } from "./router-logs-export.js"
|
|
import { MAX_LOG_ARTIFACT_CHUNK_BYTES, assertLogArtifactBudget, MAX_LOG_ARTIFACT_CIPHER_BYTES_PER_CONNECTION, MAX_LOG_ARTIFACT_TOTAL_CIPHER_BYTES } from "../modules/mcp/log-artifacts.js"
|
|
import { MAX_MCP_PUBLIC_STRING_LENGTH, redact } from "../modules/mcp/security.js"
|
|
|
|
const date = new Date("2026-10-21T00:00:00.123Z")
|
|
const rows = [{ effectiveAt: date, receivedAt: date, serverName: "=untrusted", host: "192.0.2.1", message: 'line "one"\n😀', topics: ["system", "info"], level: "info", rawTime: "00:00:00", timeKind: "receipt" }]
|
|
assert.equal(JSON.parse(serializeLogExport(rows, "json"))[0].effectiveAt, date.toISOString())
|
|
assert.equal(JSON.parse(serializeLogExport(rows, "json"))[0].host, "192.0.2.1")
|
|
const [scopedJson] = JSON.parse(serializeLogExport(rows, "json", false))
|
|
assert.equal(Object.hasOwn(scopedJson, "host"), false)
|
|
assert.equal(scopedJson.message, rows[0].message)
|
|
const csv = serializeLogExport(rows, "csv"), scopedCsv = serializeLogExport(rows, "csv", false)
|
|
assert.ok(csv.startsWith("\uFEFF")); assert.ok(csv.split("\r\n")[0].includes('"host"'))
|
|
assert.ok(!scopedCsv.split("\r\n")[0].includes('"host"')); assert.ok(csv.includes("'=untrusted")); assert.ok(csv.includes('""one""'))
|
|
assert.throws(() => serializeLogExport([{ message: "x".repeat(MAX_LOG_EXPORT_BYTES + 1) }], "json"), (error: unknown) => error instanceof LogExportLimitError && error.kind === "bytes")
|
|
for (const length of [0, 1, 2, 100, MAX_LOG_ARTIFACT_CHUNK_BYTES - 1, MAX_LOG_ARTIFACT_CHUNK_BYTES]) {
|
|
const data = Buffer.alloc(length, 255).toString("base64")
|
|
assert.ok(data.length <= MAX_MCP_PUBLIC_STRING_LENGTH)
|
|
assert.equal((redact({ dataBase64: data }) as { dataBase64: string }).dataBase64, data, "Binary chunks survive the actual public response/audit redactor")
|
|
}
|
|
let release!: () => void
|
|
const gate = new Promise<void>(resolve => { release = resolve }), first = withLogExportSlot(() => gate), second = withLogExportSlot(() => gate)
|
|
await assert.rejects(withLogExportSlot(async () => {}), LogExportBusyError)
|
|
release(); await Promise.all([first, second])
|
|
await assert.rejects(withLogExportSlot(async () => { throw Error("owned failure") }), /owned failure/)
|
|
await withLogExportSlot(async () => {})
|
|
const quotaError = (error: { code?: string }) => error.code === "EXPORT_QUOTA_EXCEEDED"
|
|
assertLogArtifactBudget(7n, 0n, 0n, 1)
|
|
assert.throws(() => assertLogArtifactBudget(8n, 0n, 0n, 1), quotaError)
|
|
assertLogArtifactBudget(0n, BigInt(MAX_LOG_ARTIFACT_CIPHER_BYTES_PER_CONNECTION - 1), BigInt(MAX_LOG_ARTIFACT_CIPHER_BYTES_PER_CONNECTION - 1), 1)
|
|
assert.throws(() => assertLogArtifactBudget(0n, BigInt(MAX_LOG_ARTIFACT_CIPHER_BYTES_PER_CONNECTION), BigInt(MAX_LOG_ARTIFACT_CIPHER_BYTES_PER_CONNECTION), 1), quotaError)
|
|
assertLogArtifactBudget(0n, 0n, BigInt(MAX_LOG_ARTIFACT_TOTAL_CIPHER_BYTES - 1), 1)
|
|
assert.throws(() => assertLogArtifactBudget(0n, 0n, BigInt(MAX_LOG_ARTIFACT_TOTAL_CIPHER_BYTES), 1), quotaError)
|
|
assert.throws(() => assertLogArtifactBudget(0n, 2n, 1n, 1), (error: { code?: string }) => error.code === "STATE_UNAVAILABLE")
|
|
console.log("Log export serialization: native timestamp/CSV parity, host projection, formula/quote safety, byte budget and full-sized base64 response survive redaction")
|