// THE DRIVE-HEALTH TIMINGS AS SETTINGS: `settings.storage.health`. // // Run with: // pnpm --filter yt-dlp-transcript-common exec tsx --test lib/storageHealthTimings.test.ts // // The claims: absent is every default; a read clamps an out-of-range value into // its range (the schema's rule: a read never throws); only a value that differs // from its default is kept, so an untuned file has no `health` key and a save // writes none; the block survives a save and a read, and the mediaRoot // migration. Its own file for the SETTINGS SEAM (settingsWrite.test.ts says // why): SETTINGS_FILE is set before the settings module is imported. import { mkdtempSync, readFileSync, writeFileSync } from "node:fs"; import { rm } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import { after, test } from "node:test"; import assert from "node:assert/strict"; import { HEALTH_TIMING_BOUNDS, HEALTH_TIMING_DEFAULTS, HEALTH_TIMING_KEYS, clearRuleText, counterSampleMinimumMs, resolveHealthTimings, sanitizeStorageHealth, secondsText, } from "./storageHealthTimings"; const ROOT = mkdtempSync(path.join(os.tmpdir(), "health-timings-")); process.env.TRANSCRIPTS_DIR = ROOT; process.env.SETTINGS_FILE = path.join(ROOT, "settings.json"); const { getSettings, writeSettings, siteSettingsSchema, defaultSiteSettings } = await import("./settings"); after(() => rm(ROOT, { recursive: true, force: true })); const onDisk = () => JSON.parse(readFileSync(process.env.SETTINGS_FILE!, "utf8")) as { storage: Record; }; test("the defaults are the constants they replace", () => { assert.deepEqual(HEALTH_TIMING_DEFAULTS, { budgetMs: 3_000, passIntervalMs: 15_000, probeTimeoutMs: 3_000, clearAfterCleanPasses: 2, inFlightPerLocation: 4, }); assert.deepEqual([...HEALTH_TIMING_KEYS].sort(), Object.keys(HEALTH_TIMING_DEFAULTS).sort()); for (const key of HEALTH_TIMING_KEYS) { const { min, max } = HEALTH_TIMING_BOUNDS[key]; const d = HEALTH_TIMING_DEFAULTS[key]; assert.ok(min <= d && d <= max, `${key}'s default is in its range`); } }); test("absent is every default: no block, an empty block, junk", () => { for (const raw of [undefined, null, {}, [], "3000", 7]) { assert.deepEqual(sanitizeStorageHealth(raw), {}); assert.deepEqual(resolveHealthTimings(raw as never), HEALTH_TIMING_DEFAULTS); } assert.equal(getSettings().storage.health, undefined, "no file: no block"); assert.equal(defaultSiteSettings().storage.health, undefined); }); test("a read clamps into the range, rounds, drops non-numbers and keeps only what differs from the default", () => { assert.deepEqual( sanitizeStorageHealth({ budgetMs: 200, // below 500 passIntervalMs: 900_000, // above 300000 probeTimeoutMs: "4000", // a string is not a number clearAfterCleanPasses: 2, // the default inFlightPerLocation: 7.6, unknown: 1, }), { budgetMs: 500, passIntervalMs: 300_000, inFlightPerLocation: 8 }, ); // A value that clamps ONTO its default is the default, and is not kept. assert.deepEqual(sanitizeStorageHealth({ clearAfterCleanPasses: 2.2 }), {}); assert.deepEqual(resolveHealthTimings({ budgetMs: 200 }), { ...HEALTH_TIMING_DEFAULTS, budgetMs: 500, }); }); test("a settings.json round trip: a tuned value is written, read back, and a default is not written", async () => { const base = defaultSiteSettings(); await writeSettings({ ...base, storage: { ...base.storage, health: { budgetMs: 4_000, clearAfterCleanPasses: 2 } }, }); assert.deepEqual(onDisk().storage.health, { budgetMs: 4_000 }); assert.deepEqual(getSettings().storage.health, { budgetMs: 4_000 }); // Every value back to its default: the key is not written at all. await writeSettings({ ...base, storage: { ...base.storage, health: { budgetMs: 3_000 } } }); assert.equal("health" in onDisk().storage, false); // A hand-edited file out of range reads clamped. writeFileSync( process.env.SETTINGS_FILE!, JSON.stringify({ storage: { locations: [], health: { inFlightPerLocation: 99 } } }), ); assert.deepEqual(getSettings().storage.health, { inFlightPerLocation: 8 }); }); test("the timings survive the mediaRoot migration (a block with no locations)", () => { const parsed = siteSettingsSchema.parse({ storage: { health: { passIntervalMs: 30_000 } }, }); assert.deepEqual(parsed.storage.health, { passIntervalMs: 30_000 }); writeFileSync( process.env.SETTINGS_FILE!, JSON.stringify({ storage: { mediaRoot: "/mnt/cold", health: { budgetMs: 6_000 } } }), ); const s = getSettings(); assert.deepEqual(s.storage.locations.map((l) => l.root), ["/mnt/cold"]); assert.deepEqual(s.storage.health, { budgetMs: 6_000 }); }); test("the counters' sample spacing: min(10 s, interval − 5 s), floored at half the interval", () => { assert.equal(counterSampleMinimumMs(15_000), 10_000, "the default, as it was"); assert.equal(counterSampleMinimumMs(300_000), 10_000); assert.equal(counterSampleMinimumMs(12_000), 7_000); assert.equal(counterSampleMinimumMs(10_000), 5_000); assert.equal(counterSampleMinimumMs(8_000), 4_000, "the floor: 3 s would be under half"); assert.equal(counterSampleMinimumMs(5_000), 2_500, "never 0 at the minimum interval"); }); test("the words", () => { assert.equal(secondsText(3_000), "3 s"); assert.equal(secondsText(2_500), "2.5 s"); assert.equal(secondsText(80), "0.08 s"); assert.equal(clearRuleText(1), "once"); assert.equal(clearRuleText(2), "twice in a row"); assert.equal(clearRuleText(5), "5 times in a row"); });