import { HEALTH_TIMING_BOUNDS, HEALTH_TIMING_DEFAULTS, type HealthTimingKey, type StorageHealthSettings, } from "yt-dlp-transcript-common/lib/storageHealthTimings"; // THE DRIVE HEALTH TIMING FORM ON /storage: its fields' names and units, and // the one parse of what was submitted. Pure, and client-safe (it imports only // the pure timings module), so the form draws its labels from here and the // server action parses with it. // // AN EMPTY FIELD IS THE DEFAULT. The inputs show each default as a placeholder // and hold a value only where the operator set one, so clearing a field puts // that timing back on its default — and nothing is written for it // (`sanitizeStorageHealth` keeps only what differs from a default). // // OUT OF RANGE IS REFUSED, NOT CLAMPED. The schema clamps a hand-edited value on // read (a read never throws); a save from this form says which field is out of // range and writes nothing, because storing another number than the one typed // reads as a form that did not listen. export type HealthTimingField = { key: HealthTimingKey; // The visible label, and the input's accessible name (a contract once an // e2e spec names it). label: string; ariaLabel: string; // "ms", or "" for a count. unit: string; }; export const HEALTH_TIMING_FIELDS: readonly HealthTimingField[] = [ { key: "budgetMs", label: "Read budget", ariaLabel: "read budget", unit: "ms" }, { key: "passIntervalMs", label: "Health check interval", ariaLabel: "health check interval", unit: "ms", }, { key: "probeTimeoutMs", label: "Health check timeout", ariaLabel: "health check timeout", unit: "ms", }, { key: "clearAfterCleanPasses", label: "Clean checks to clear", ariaLabel: "clean checks to clear", unit: "", }, { key: "inFlightPerLocation", label: "Reads at once per drive", ariaLabel: "reads at once per drive", unit: "", }, ]; export type HealthTimingsParse = | { ok: true; health: StorageHealthSettings } | { ok: false; error: string }; // What the form posted, as the stored block: each non-empty field a whole // number in its range, and only the ones that differ from their default. export function parseHealthTimingsForm(form: { get(name: string): FormDataEntryValue | null; }): HealthTimingsParse { const health: StorageHealthSettings = {}; for (const field of HEALTH_TIMING_FIELDS) { const raw = form.get(field.key); const text = typeof raw === "string" ? raw.trim() : ""; if (text === "") continue; const unit = field.unit ? ` ${field.unit}` : ""; const n = Number(text); if (!/^\d+$/.test(text) || !Number.isSafeInteger(n)) { const what = field.unit === "ms" ? "a whole number of milliseconds" : "a whole number"; return { ok: false, error: `${field.label} must be ${what} (got "${text}").` }; } const { min, max } = HEALTH_TIMING_BOUNDS[field.key]; if (n < min || n > max) { return { ok: false, error: `${field.label} must be between ${min} and ${max}${unit} (got ${n}${unit}).`, }; } if (n !== HEALTH_TIMING_DEFAULTS[field.key]) health[field.key] = n; } return { ok: true, health }; }