import assert from "node:assert/strict" import { test, mock } from "node:test" import * as fs from "node:fs/promises" import { tmpdir } from "node:os" import { join, dirname } from "node:path" test("bounded source files: exact reads, partial reads and concurrent inode/size changes", async t => { const directory = await fs.mkdtemp(join(tmpdir(), "mm-bandwidth-file-")) const path = join(directory, "issuer.key"), original = Buffer.alloc(32, 73) let mode: "normal" | "partial" | "append" | "truncate" | "replace" | "cancel" = "normal", reads = 0 let controller = new AbortController() const moduleMock = mock.module("node:fs/promises", { namedExports: { ...fs, open: async (...args: Parameters) => { if (args[0] === path && mode === "replace") { const replacement = join(directory, "replacement.key") await fs.writeFile(replacement, original); await fs.rename(replacement, path) } const handle = await fs.open(...args) if (args[0] !== path) return handle return { stat: handle.stat.bind(handle), close: handle.close.bind(handle), async read(buffer: Buffer, offset: number, length: number, position: number) { reads++ const result = await handle.read(buffer, offset, mode === "partial" ? Math.min(3, length) : length, position) if (mode === "append") await fs.appendFile(path, Buffer.from([1])) if (mode === "truncate") await fs.truncate(path, 4) if (mode === "cancel") controller.abort() return result }, } }, } }) const { readBandwidthControlFile } = await import("./bandwidth-control-source.js") const { closePool } = await import("../db/index.js") const live = () => controller.signal.throwIfAborted() const reset = async (next: typeof mode) => { mode = next; reads = 0; controller = new AbortController(); await fs.writeFile(path, original) } try { await t.test("exact 32-byte key needs one read and returns an independent buffer", async () => { await reset("normal"); const result = await readBandwidthControlFile(path, 32, live) assert.deepEqual(result, original); assert.equal(reads, 1) result.fill(0); assert.deepEqual(await fs.readFile(path), original) }) await t.test("short reads still return every original byte", async () => { await reset("partial"); assert.deepEqual(await readBandwidthControlFile(path, 32, live), original); assert.equal(reads, 11) }) for (const mutation of ["append", "truncate", "replace", "cancel"] as const) await t.test(`${mutation} during capture refuses`, async () => { await reset(mutation); await assert.rejects(readBandwidthControlFile(path, 32, live)) }) await t.test("empty and oversized files refuse before any content read", async () => { await reset("normal"); await fs.writeFile(path, Buffer.alloc(0)); await assert.rejects(readBandwidthControlFile(path, 32, live)); assert.equal(reads, 0) await fs.writeFile(path, Buffer.alloc(33)); await assert.rejects(readBandwidthControlFile(path, 32, live)); assert.equal(reads, 0) }) } finally { moduleMock.restore(); await closePool() assert.equal(dirname(path), directory); assert(directory.startsWith(join(tmpdir(), "mm-bandwidth-file-"))) await fs.rm(directory, { recursive: true, force: true }) } })