/** * @file Vérifie que les métadonnées d'un envoi de toolbox restent justes sur disque quand plusieurs lectures * ou une révocation se croisent. */ import assert from "node:assert/strict"; import { existsSync, readdirSync, readFileSync } from "node:fs"; import { mkdtemp, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { test } from "node:test"; import { ShareStore } from "../services/shares/store.js"; const ID = "AAAAAAAAAAAAAAAAAAAAAA"; async function storeWithShare(t) { const dir = await mkdtemp(join(tmpdir(), "sokko-share-store-")); t.after(() => rm(dir, { recursive: true, force: true })); const now = Date.UTC(2026, 10, 20, 12); const store = new ShareStore({ dir, now: () => now }); await writeFile(store.partPath(ID), Buffer.alloc(64, 1)); await store.commit(ID, { createdAt: now, expiresAt: now + 60_000, size: 64, singleUse: false, reads: 0, deleteTokenHash: "00" }); return { dir, store, now }; } test("simultaneous reads of a reusable share all reach the file on disk", async (t) => { const { dir, store, now } = await storeWithShare(t); const results = await Promise.allSettled(Array.from({ length: 10 }, () => store.recordRead(ID))); assert.deepEqual(results.filter(({ status }) => status === "rejected").map(({ reason }) => reason.code), []); assert.equal(JSON.parse(readFileSync(store.metaPath(ID), "utf8")).reads, 10); assert.deepEqual(readdirSync(dir).filter((name) => name.endsWith(".tmp")), []); assert.equal(new ShareStore({ dir, now: () => now }).get(ID).reads, 10); }); test("a revocation is not undone by a read still being written", async (t) => { const { dir, store, now } = await storeWithShare(t); const reads = [store.recordRead(ID), store.recordRead(ID)]; await store.remove(ID); await Promise.all(reads); assert.equal(existsSync(store.metaPath(ID)), false); assert.equal(existsSync(store.binPath(ID)), false); assert.equal(new ShareStore({ dir, now: () => now }).count, 0); });