import { test } from "node:test"; import assert from "node:assert/strict"; import { HEALTH_TIMING_DEFAULTS, HEALTH_TIMING_KEYS, } from "yt-dlp-transcript-common/lib/storageHealthTimings"; import { HEALTH_TIMING_FIELDS, parseHealthTimingsForm } from "./healthTimingsForm"; // Run with: pnpm -C editor exec tsx --test "app/**/*.test.ts" // // THE /storage DRIVE HEALTH TIMING FORM'S PARSE: an empty field is the default // and is not written; a value equal to its default is not written either; a // whole number in range is; anything else is refused with a sentence naming // the field, and nothing is saved. function form(fields: Record): FormData { const f = new FormData(); for (const [k, v] of Object.entries(fields)) f.set(k, v); return f; } test("every timing has one field, in the settings' order", () => { assert.deepEqual( HEALTH_TIMING_FIELDS.map((f) => f.key), [...HEALTH_TIMING_KEYS], ); const names = HEALTH_TIMING_FIELDS.map((f) => f.ariaLabel); assert.equal(new Set(names).size, names.length); // No accessible name contains another (Playwright's getByLabel is a // substring match). for (const a of names) { for (const b of names) if (a !== b) assert.ok(!a.includes(b), `${a} contains ${b}`); } }); test("empty fields are the defaults: nothing is written", () => { assert.deepEqual(parseHealthTimingsForm(form({})), { ok: true, health: {} }); assert.deepEqual( parseHealthTimingsForm(form({ budgetMs: "", passIntervalMs: " " })), { ok: true, health: {} }, ); }); test("a value in range is kept, trimmed; one equal to its default is not written", () => { assert.deepEqual( parseHealthTimingsForm( form({ budgetMs: " 4000 ", passIntervalMs: String(HEALTH_TIMING_DEFAULTS.passIntervalMs), probeTimeoutMs: "500", clearAfterCleanPasses: "3", inFlightPerLocation: "8", }), ), { ok: true, health: { budgetMs: 4_000, probeTimeoutMs: 500, clearAfterCleanPasses: 3, inFlightPerLocation: 8, }, }, ); }); test("out of range is refused with the field, the range and the value — not clamped", () => { assert.deepEqual(parseHealthTimingsForm(form({ budgetMs: "200" })), { ok: false, error: "Read budget must be between 500 and 60000 ms (got 200 ms).", }); assert.deepEqual(parseHealthTimingsForm(form({ inFlightPerLocation: "9" })), { ok: false, error: "Reads at once per drive must be between 1 and 8 (got 9).", }); assert.deepEqual(parseHealthTimingsForm(form({ clearAfterCleanPasses: "0" })), { ok: false, error: "Clean checks to clear must be between 1 and 10 (got 0).", }); }); test("not a whole number is refused", () => { for (const bad of ["3.5", "-1", "3e3", "abc", "0x10"]) { const r = parseHealthTimingsForm(form({ passIntervalMs: bad })); assert.equal(r.ok, false, bad); if (!r.ok) { assert.equal(r.error, `Health check interval must be a whole number of milliseconds (got "${bad}").`); } } const count = parseHealthTimingsForm(form({ clearAfterCleanPasses: "two" })); assert.deepEqual(count, { ok: false, error: 'Clean checks to clear must be a whole number (got "two").', }); });