80 lines
4.2 KiB
TypeScript
80 lines
4.2 KiB
TypeScript
import assert from "node:assert/strict"
|
|
import { randomBytes, randomUUID } from "node:crypto"
|
|
import { chmod, link, mkdtemp, readFile, realpath, rm, writeFile } from "node:fs/promises"
|
|
import { tmpdir } from "node:os"
|
|
import path from "node:path"
|
|
import { test } from "node:test"
|
|
import { appendRoutingChargeJournal, readRoutingChargeJournal } from "./routing-charge-journal.js"
|
|
|
|
const hour = 3_600_000
|
|
const now = Date.parse("2026-11-15T10:00:00.000Z")
|
|
const receipt = (time: number, mac = "a") => ({
|
|
allocationId: randomUUID(), createdAt: new Date(time).toISOString(), auditMac: mac.repeat(64),
|
|
})
|
|
const refused = { code: "STATE_CONFLICT" }
|
|
|
|
async function owned(work: (root: string, profileId: string, filename: string) => Promise<void>) {
|
|
const parent = await realpath(tmpdir())
|
|
const root = await mkdtemp(path.join(parent, "mm-routing-charge-journal-"))
|
|
const previous = process.env.MCP_STATE_DIR
|
|
process.env.MCP_STATE_DIR = root
|
|
try {
|
|
await writeFile(path.join(root, "encryption-key"), randomBytes(32), { mode: 0o600 })
|
|
const profileId = randomUUID()
|
|
await work(root, profileId, path.join(root, "routing-charge-journal", profileId + ".json"))
|
|
} finally {
|
|
if (previous === undefined) delete process.env.MCP_STATE_DIR
|
|
else process.env.MCP_STATE_DIR = previous
|
|
assert.equal(path.dirname(await realpath(root)), parent)
|
|
assert.match(path.basename(root), /^mm-routing-charge-journal-/)
|
|
await rm(root, { recursive: true, force: true })
|
|
}
|
|
}
|
|
|
|
test("host journal preserves exact receipts within the strict hourly window", () => owned(async (_root, profileId, filename) => {
|
|
assert.deepEqual(await readRoutingChargeJournal(profileId, now), [])
|
|
const start = receipt(now - hour), fresh = receipt(now - hour + 1), current = receipt(now, "b")
|
|
for (const item of [start, fresh, current]) await appendRoutingChargeJournal(profileId, item)
|
|
const persisted = await readFile(filename, "utf8")
|
|
assert.deepEqual(JSON.parse(persisted).body.witnesses, [fresh, current])
|
|
assert.deepEqual(await readRoutingChargeJournal(profileId, now), [fresh, current])
|
|
assert.deepEqual(await readRoutingChargeJournal(profileId, now + 1), [current])
|
|
await appendRoutingChargeJournal(profileId, current)
|
|
await assert.rejects(appendRoutingChargeJournal(profileId, { ...current, auditMac: "c".repeat(64) }), refused)
|
|
assert.equal(await readFile(filename, "utf8"), persisted)
|
|
}))
|
|
|
|
test("host journal refuses changed payload, identity, and signing material", () => owned(async (root, profileId, filename) => {
|
|
const item = receipt(now)
|
|
await appendRoutingChargeJournal(profileId, item)
|
|
const original = await readFile(filename, "utf8"), changed = JSON.parse(original)
|
|
changed.body.witnesses[0].auditMac = "c".repeat(64)
|
|
await writeFile(filename, JSON.stringify(changed))
|
|
await assert.rejects(readRoutingChargeJournal(profileId, now), refused)
|
|
await assert.rejects(appendRoutingChargeJournal(profileId, receipt(now)), refused)
|
|
await writeFile(filename, original)
|
|
const other = randomUUID()
|
|
await writeFile(path.join(path.dirname(filename), other + ".json"), original, { mode: 0o600 })
|
|
await assert.rejects(readRoutingChargeJournal(other, now), refused)
|
|
await assert.rejects(readRoutingChargeJournal("../" + profileId, now), refused)
|
|
await writeFile(path.join(root, "encryption-key"), randomBytes(32))
|
|
await assert.rejects(readRoutingChargeJournal(profileId, now), refused)
|
|
}))
|
|
|
|
test("host journal bounds and protects the snapshot file", () => owned(async (_root, profileId, filename) => {
|
|
for (let index = 0; index < 11; index++) await appendRoutingChargeJournal(profileId, receipt(now))
|
|
await assert.rejects(appendRoutingChargeJournal(profileId, receipt(now)), refused)
|
|
assert.equal((await readRoutingChargeJournal(profileId, now)).length, 11)
|
|
const alias = path.join(path.dirname(filename), "alias.json")
|
|
await link(filename, alias)
|
|
await assert.rejects(readRoutingChargeJournal(profileId, now), refused)
|
|
await rm(alias)
|
|
if (process.platform !== "win32") {
|
|
await chmod(filename, 0o644)
|
|
await assert.rejects(readRoutingChargeJournal(profileId, now), refused)
|
|
await chmod(filename, 0o600)
|
|
}
|
|
await writeFile(filename, Buffer.alloc(4097))
|
|
await assert.rejects(readRoutingChargeJournal(profileId, now), refused)
|
|
}))
|