70 lines
4.0 KiB
TypeScript
70 lines
4.0 KiB
TypeScript
import assert from "node:assert/strict"
|
|
import { test } from "node:test"
|
|
import { preparedPlanExpiry } from "./plan-expiry.js"
|
|
|
|
const iso = (milliseconds: number) => new Date(Date.now() + milliseconds).toISOString()
|
|
const poll = (validUntil?: string) => preparedPlanExpiry("servers.poll", "servers", { validUntil })
|
|
|
|
test("Community honors sealed validity but keeps the ordinary lifetime without a bound", () => {
|
|
const bound = iso(90000)
|
|
assert.equal(preparedPlanExpiry("routing.community.switch", "network", { validUntil: bound })?.toISOString(), bound)
|
|
assert.equal(preparedPlanExpiry("routing.community.switch", "network", { validUntil: iso(-1000) }), null)
|
|
const before = Date.now(), manual = preparedPlanExpiry("routing.community.switch", "network", {})!.getTime()
|
|
assert(manual >= before + 300000 && manual <= Date.now() + 300000)
|
|
})
|
|
|
|
test("Poll uses its existing sealed five-minute validity as the nominal window", () => {
|
|
const bound = iso(300000)
|
|
assert.equal(poll(bound)?.toISOString(), bound)
|
|
})
|
|
|
|
test("scoped inverse review expires at the sealed deadline and rejects missing or elapsed validity", () => {
|
|
const step = { kind: "service", service: "routing.community.scoped.recover", input: {} } as const
|
|
const bound = iso(45000)
|
|
assert.equal(preparedPlanExpiry("operations.recover", "network", { validUntil: bound, steps: [step] })?.toISOString(), bound)
|
|
assert.equal(preparedPlanExpiry("operations.recover", "network", { validUntil: iso(-1000), steps: [step] }), null)
|
|
assert.equal(preparedPlanExpiry("operations.recover", "network", { steps: [step] }), null)
|
|
const before = Date.now()
|
|
const generic = preparedPlanExpiry("operations.recover", "network", {
|
|
validUntil: iso(-1000), steps: [{ ...step, service: "routing.community.restore" }],
|
|
})!.getTime()
|
|
assert(generic >= before + 300000 && generic <= Date.now() + 300000)
|
|
})
|
|
for (const [name, milliseconds] of [["credential", 220000], ["policy", 240000], ["owner", 260000]] as const) {
|
|
test(`Poll preserves a shorter original ${name} deadline`, () => {
|
|
const bound = iso(milliseconds)
|
|
assert.equal(poll(bound)?.toISOString(), bound)
|
|
})
|
|
}
|
|
test("Poll cannot extend the old ordinary upper bound with longer evidence", () => {
|
|
const before = Date.now(), actual = poll(iso(1800000))!.getTime(), after = Date.now()
|
|
assert(actual >= before + 900000 && actual <= after + 900000)
|
|
})
|
|
for (const [name, value] of [["missing", undefined], ["malformed", "invalid"], ["permissive Date.parse", "1"], ["non-datetime", "9999"], ["expired", new Date(0).toISOString()]] as const) {
|
|
test(`Poll ${name} validity refuses instead of minting a nominal deadline`, () => {
|
|
assert.equal(poll(value), null)
|
|
})
|
|
}
|
|
for (const section of ["servers", "network"]) {
|
|
test(`generic ${section} plans keep the original lifetime even with expired evidence`, () => {
|
|
const before = Date.now(), actual = preparedPlanExpiry("generic.operation", section, { validUntil: new Date(0).toISOString() })!.getTime(), after = Date.now()
|
|
const lifetime = section === "network" ? 300000 : 900000
|
|
assert(actual >= before + lifetime && actual <= after + lifetime)
|
|
})
|
|
}
|
|
test("generic plans never read evidence validity, including a throwing getter", () => {
|
|
const evidence = { get validUntil(): string { throw Error("Generic evidence must stay unread") } }
|
|
assert(preparedPlanExpiry("generic.operation", "servers", evidence) instanceof Date)
|
|
assert(preparedPlanExpiry("generic.operation", "network", evidence) instanceof Date)
|
|
})
|
|
test("Poll reads its trusted validity once and does not mutate the evidence", () => {
|
|
const bound = iso(300000); let reads = 0
|
|
const evidence = Object.freeze({ get validUntil() { reads++; return bound } })
|
|
assert.equal(preparedPlanExpiry("servers.poll", "servers", evidence)?.toISOString(), bound)
|
|
assert.equal(reads, 1)
|
|
})
|
|
test("Poll accepts strict datetime offsets while preserving their absolute instant", () => {
|
|
const absolute = iso(300000), offset = absolute.replace(/Z$/, "+00:00")
|
|
assert.equal(poll(offset)?.toISOString(), absolute)
|
|
})
|