import assert from "node:assert/strict" import { mkdtemp, rename, rm, writeFile } from "node:fs/promises" import { tmpdir } from "node:os" import { join } from "node:path" import { readStateFile } from "./state-file-cache.js" const directory = await mkdtemp(join(tmpdir(), "mcp-state-cache-")), file = join(directory, "access-generation") try { await writeFile(file, Buffer.alloc(32, 1)) assert.deepEqual(await readStateFile(file), Buffer.alloc(32, 1)) const copy = await readStateFile(file); copy.fill(9) assert.deepEqual(await readStateFile(file), Buffer.alloc(32, 1), "callers get a copy; zeroizing it never touches the cache") // Rotation by atomic replace is seen on the very next call. await writeFile(file + ".tmp", Buffer.alloc(32, 2)); await rename(file + ".tmp", file) assert.deepEqual(await readStateFile(file), Buffer.alloc(32, 2)) // In-place overwrite is seen as well. await writeFile(file, Buffer.alloc(32, 3)) assert.deepEqual(await readStateFile(file), Buffer.alloc(32, 3)) await rm(file) await assert.rejects(readStateFile(file), (error: NodeJS.ErrnoException) => error.code === "ENOENT") } finally { await rm(directory, { recursive: true, force: true }) } console.log("MCP state file cache: copy per call, atomic replace/in-place overwrite seen at once, removal is ENOENT")