Files
MikrotikManager/backend/src/services/server-poll-storage-model.test.ts
T
Denozordec ef3d7b6779
Docker images / prepare-release (push) Successful in 11s
Docker images / backend-test (push) Successful in 10m21s
Docker images / frontend-image (push) Successful in 2m8s
Docker images / updater-image (push) Successful in 38s
Docker images / netflow-agent-image (push) Successful in 32s
Docker images / backend-image (push) Successful in 1m59s
Docker images / publish-release (push) Successful in 12s
Docker images / notify-webhook (push) Successful in 2s
fix(poll): снять квоты журналов полного опроса
2026-11-23 11:35:04 +07:00

98 lines
7.2 KiB
TypeScript

import assert from "node:assert/strict"
import { test } from "node:test"
import { SERVER_POLL_STORAGE_POLICY as policy, PollStorageRefused, pollStoragePolicy, pollStorageScope,
validatePollStorageUsage, pollStorageGcEligible, type PollStorageScope, type PollStorageGcCandidate } from "./server-poll-storage-model.js"
const DAY = 86400000
const scope: PollStorageScope = { receiptId: "5f4aaf7b-cc19-4b36-b693-5ce092a03852", executionId: "b332191d-bf58-4196-a629-f30367a48ef7",
kind: "rest", ownerId: "operator", bindingProof: "a".repeat(64), contextProof: "b".repeat(64), issuedAt: 1000, expiresAt: 2000 }
const empty = { attempts: 0, bytes: 0, rows: 0 }
const usage = (attempts: number) => ({ attempts, bytes: attempts * (policy.attemptBytes + policy.admissionBytes), rows: attempts * policy.attemptRows })
const compactUsage = (attempts: number, compactedAttempts:number, encodedPerArchive=59075) => ({ attempts,compactedAttempts,archiveBytes:compactedAttempts*encodedPerArchive,
bytes:attempts*(policy.attemptBytes+policy.admissionBytes)+compactedAttempts*(policy.admissionBytes+encodedPerArchive),rows:attempts*policy.attemptRows+compactedAttempts*2 })
const candidate = (patch: Partial<PollStorageGcCandidate> = {}): PollStorageGcCandidate => ({ scope, lastActivityAt: 1500,
gcNotBefore: 0, retentionDays: 14, safetyWindowMs: 60000, mcpTerminal: false, ...patch })
const boundary = 2000 + 14 * DAY + 60000
test("reservations cover all journal rows and metadata independently, with immutable cloned policy", () => {
assert.equal(policy.attemptRows, 1 + 2008 + 2008 + 1 + 1)
assert.equal(policy.attemptBytes, 8 * 1024 * 1024)
assert.equal(policy.admissionBytes, 32768)
const input = { ...policy, retentionDays: 30 }, cloned = pollStoragePolicy(input)
input.retentionDays = 1
assert.equal(cloned.retentionDays, 30); assert.equal(Object.isFrozen(cloned), true)
for (const patch of [{ attemptRows: 4018 }, { attemptBytes: policy.attemptBytes + 1 }, { retentionDays: 0 },
{ retentionDays: 366 }, { safetyWindowMs: 59999 }, { safetyWindowMs: DAY + 1 }, { retentionDays: 1.5 }]) {
assert.throws(() => pollStoragePolicy({ ...policy, ...patch }), PollStorageRefused)
}
assert.equal(pollStoragePolicy({ ...policy, retentionDays: 365, safetyWindowMs: DAY }).retentionDays, 365)
})
test("scope validates original identity, clones and freezes, and never invents a lifetime cap", () => {
const input = { ...scope }, parsed = pollStorageScope(input, { now: 1000 })
input.ownerId = "changed"
assert.equal(parsed.ownerId, "operator"); assert.equal(Object.isFrozen(parsed), true)
assert.equal(pollStorageScope({ ...scope, ownerId: "x".repeat(200), expiresAt: 2000 + 1000 * DAY }, { now: 1500 }).expiresAt, 2000 + 1000 * DAY)
for (const patch of [{ receiptId: "invalid" }, { executionId: "invalid" }, { ownerId: " operator" }, { ownerId: "operator " },
{ ownerId: "x".repeat(201) }, { ownerId: "x\n" }, { ownerId: "x\u0085" }, { ownerId: "" }, { bindingProof: "A".repeat(64) },
{ contextProof: "b".repeat(63) }, { kind: "unknown" }, { issuedAt: -1 }, { issuedAt: 1.5 }, { issuedAt: NaN },
{ expiresAt: Infinity }, { expiresAt: 8640000000000001 }, { expiresAt: 1000 }, { extra: true }]) {
assert.throws(() => pollStorageScope({ ...scope, ...patch }, { now: 1500 }), PollStorageRefused)
}
assert.throws(() => pollStorageScope(scope, { now: 999 }), PollStorageRefused)
assert.throws(() => pollStorageScope(scope, { now: 2000 }), PollStorageRefused)
assert.equal(pollStorageScope(scope, { now: 2000, allowExpired: true }).expiresAt, 2000)
})
test("exact accounting accepts past quotas and rejects forged totals", () => {
for (const attempts of [0, 8, 20, 130, 1000]) {
const value = usage(attempts)
assert.equal(validatePollStorageUsage(value).attempts, attempts)
for (const dimension of ["bytes", "rows", "attempts"] as const) {
for (const invalid of [-1, 0.5, NaN, Infinity, Number.MAX_SAFE_INTEGER + 1, "0", undefined, value[dimension] + 1])
assert.throws(() => validatePollStorageUsage({ ...value, [dimension]: invalid }), PollStorageRefused)
}
}
assert.throws(() => validatePollStorageUsage({ ...empty, extra: true } as typeof empty), PollStorageRefused)
})
test("GC is strictly after expiry plus retention/safety and persisted gcNotBefore", () => {
assert.equal(pollStorageGcEligible(candidate(), policy, boundary), false)
assert.equal(pollStorageGcEligible(candidate(), policy, boundary + 1), true)
assert.equal(pollStorageGcEligible(candidate({ gcNotBefore: boundary + DAY }), policy, boundary + DAY), false)
assert.equal(pollStorageGcEligible(candidate({ gcNotBefore: boundary + DAY }), policy, boundary + DAY + 1), true)
assert.equal(pollStorageGcEligible(candidate({ scope: { ...scope, kind: "mcp" } }), policy, boundary + 1), false)
assert.equal(pollStorageGcEligible(candidate({ scope: { ...scope, kind: "mcp" }, mcpTerminal: true }), policy, boundary + 1), true)
})
test("compacted accounting validates the exact optional pair, encoded bounds and opaque extras",()=>{
const good=compactUsage(0,1,40)
assert.deepEqual(validatePollStorageUsage(good),good)
assert.deepEqual(validatePollStorageUsage(compactUsage(20,336,131072)),compactUsage(20,336,131072))
const {archiveBytes:_bytes,...missingArchive}=good,{compactedAttempts:_count,...missingCount}=good
for(const value of [missingArchive,missingCount,{...good,archiveBytes:undefined},{...good,compactedAttempts:undefined},
compactUsage(0,1,39),compactUsage(0,1,131073),{...good,bytes:good.bytes-1},{...good,rows:good.rows+1},
{...good,compactedAttempts:-0},{...good,archiveBytes:NaN},{...good,opaque:true},
{...usage(0),compactedAttempts:0,archiveBytes:1},compactUsage(0,Number.MAX_SAFE_INTEGER,40)]){
assert.throws(() => validatePollStorageUsage(value as typeof good), PollStorageRefused)
}
})
test("retention increases, persisted retention/safety, recent stages and long original authority all delay GC", () => {
assert.equal(pollStorageGcEligible(candidate(), { ...policy, retentionDays: 30 }, boundary + 1), false)
assert.equal(pollStorageGcEligible(candidate({ retentionDays: 30 }), policy, boundary + 1), false)
assert.equal(pollStorageGcEligible(candidate({ safetyWindowMs: DAY }), policy, boundary + 1), false)
assert.equal(pollStorageGcEligible(candidate({ lastActivityAt: boundary }), policy, boundary + 1), false)
assert.equal(pollStorageGcEligible(candidate({ scope: { ...scope, expiresAt: 2000 + 1000 * DAY } }), policy, boundary + 1), false)
assert.equal(pollStorageGcEligible(candidate(), { ...policy, safetyWindowMs: DAY }, boundary + 1), false)
})
test("GC fails closed for malformed metadata, unsafe arithmetic and Date overflow", () => {
for (const patch of [{ retentionDays: 0 }, { retentionDays: 366 }, { lastActivityAt: -1 }, { lastActivityAt: 999 },
{ lastActivityAt: boundary + 2 }, { gcNotBefore: NaN }, { gcNotBefore: Infinity }, { safetyWindowMs: 59999 }, { safetyWindowMs: DAY + 1 }]) {
assert.equal(pollStorageGcEligible(candidate(patch), policy, boundary + 1), false)
}
assert.equal(pollStorageGcEligible(candidate(), policy, NaN), false)
assert.equal(pollStorageGcEligible(candidate({ scope: { ...scope, expiresAt: 8640000000000000 } }), policy, 8640000000000000), false)
})