import { test } from "node:test"; import assert from "node:assert/strict"; import { mkdtemp, readFile, rm, stat, writeFile } from "node:fs/promises"; import { hostname, tmpdir } from "node:os"; import path from "node:path"; import { COMPACTING_MARKER_FILENAME, CompactionInProgressError, MARKER_MAX_AGE_HOURS, assertNotCompacting, clearCompactingMarker, liveCompactingMarker, markerLiveness, processStartTime, readCompactingMarker, registerCompactionJobProbe, writeCompactingMarker, } from "./compactingMarker-server"; // Run with: // pnpm --filter yt-dlp-transcript-common exec tsx --test lib/compactingMarker.test.ts // // The "being compacted" marker (review F3): live only when boot, host, pid, // the pid's start time, the job and the age all hold; every stale case is // ignored by the guards and cleared on request. async function withDir(fn: (dir: string) => Promise): Promise { const dir = await mkdtemp(path.join(tmpdir(), "marker-")); try { await fn(dir); } finally { await rm(dir, { recursive: true, force: true }); registerCompactionJobProbe(null); } } const bootId = async () => (await readFile("/proc/sys/kernel/random/boot_id", "utf8")).trim(); async function write(dir: string, m: Record): Promise { const base = { pid: process.pid, pidStart: await processStartTime(process.pid), bootId: await bootId(), host: hostname(), token: "t", startedAt: new Date().toISOString(), }; await writeFile(path.join(dir, COMPACTING_MARKER_FILENAME), JSON.stringify({ ...base, ...m })); } test("a marker this process wrote is live, refuses others, and passes its own token", async () => { await withDir(async (dir) => { const m = await writeCompactingMarker(dir, { video: "kick/v1" }); assert.ok(await liveCompactingMarker(dir)); await assert.rejects(assertNotCompacting(dir, "Persisting"), (e: Error) => e instanceof CompactionInProgressError); await assertNotCompacting(dir, "the compaction's own drop", m.token); assert.deepEqual(await clearCompactingMarker(dir), { cleared: false, why: `live: pid ${process.pid} since ${m.startedAt.slice(0, 16)} — cancel that job instead` }); assert.equal((await clearCompactingMarker(dir, { token: m.token })).cleared, true); await assert.rejects(stat(path.join(dir, COMPACTING_MARKER_FILENAME))); }); }); test("every stale case is ignored by the guards and cleared", async () => { const cases: [string, Record, RegExp][] = [ ["a dead pid", { pid: 2 ** 22 + 7 }, /process .* is gone/], ["a pid reused by another live process", { pid: process.ppid }, /is another process now/], ["pid 1, as a container's editor would be", { pid: 1 }, /is another process now/], ["another host or container", { host: "another-host" }, /another host/], ["before this machine last started", { bootId: "00000000-0000-0000-0000-000000000000" }, /before this machine last started/], ["older than the cap", { startedAt: new Date(Date.now() - (MARKER_MAX_AGE_HOURS + 1) * 3_600_000).toISOString() }, /older than/], ["a marker with no start time or boot (an older writer)", { pidStart: null, bootId: null }, /before this machine last started|another process/], ]; for (const [label, m, why] of cases) { await withDir(async (dir) => { await write(dir, m); const read = (await readCompactingMarker(dir))!; const l = await markerLiveness(read); assert.equal(l.live, false, label); assert.match(l.why, why, label); await assertNotCompacting(dir, "Persisting"); const c = await clearCompactingMarker(dir); assert.equal(c.cleared, true, label); }); } }); test("this process's marker for a job the registry does not run is stale", async () => { await withDir(async (dir) => { await writeCompactingMarker(dir, { jobId: "j-gone" }); registerCompactionJobProbe((id) => id === "j-running"); assert.match((await markerLiveness((await readCompactingMarker(dir))!)).why, /job j-gone is not running/); await assertNotCompacting(dir, "Persisting"); await writeCompactingMarker(dir, { jobId: "j-running" }); await assert.rejects(assertNotCompacting(dir, "Persisting"), (e: Error) => e instanceof CompactionInProgressError); }); }); test("D3: a clear never deletes a marker written between its check and its removal", async () => { await withDir(async (dir) => { await write(dir, { pid: 2 ** 22 + 7, token: "stale" }); let fresh = ""; const r = await clearCompactingMarker(dir, { beforeRemove: async () => { // A compaction starts right here. fresh = (await writeCompactingMarker(dir)).token; }, }); assert.deepEqual(r, { cleared: false, why: "a new marker was written meanwhile: kept" }); assert.equal((await readCompactingMarker(dir))?.token, fresh); assert.ok(await liveCompactingMarker(dir)); }); });