import { mkdtempSync, readFileSync } from "node:fs"; import { rm } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import { test, after } from "node:test"; import assert from "node:assert/strict"; // Run with: pnpm -C editor exec tsx --test "app/**/*.test.ts" // // THE SETTINGS FORM IS NOT SENT `autoQueue` (settings/page.tsx): the four // lanes' policy trees are most of settings.json on a large corpus, the form // reads none of them, and they were serialised into /settings twice. That is // only safe while a save from the form cannot write settings back without // them — which holds because the action builds its patch from the SUBMITTED // FIELDS alone and saveSettings merges it over the stored file. This pins it: // a save through the real action, from a form that never saw `autoQueue`, // leaves every lane's tree, and every other block the form does not edit, // exactly as stored. // // SETTINGS SEAM as in saveSettings.test.ts: getPaths() memoizes, so // SETTINGS_FILE is set before the module under test is imported. const ROOT = mkdtempSync(path.join(os.tmpdir(), "save-settings-action-")); process.env.TRANSCRIPTS_DIR = ROOT; process.env.SETTINGS_FILE = path.join(ROOT, "settings.json"); const { saveSettingsAction } = await import("./actions"); const { defaultSiteSettings, getSettings, writeSettings } = await import( "yt-dlp-transcript-common/lib/settings" ); after(() => rm(ROOT, { recursive: true, force: true })); // What the form posts: its own fields, and nothing of any other block. function formOf(over: Record = {}): FormData { const s = getSettings(); const fields: Record = { adminTitle: s.adminTitle, homepageUrl: s.homepageUrl ?? "", maxTranscriptPageBytes: String(s.maxTranscriptPageBytes), sleepBetweenDownloadsSeconds: String(s.sleepBetweenDownloadsSeconds), autoRefreshIntervalSeconds: String(s.autoRefreshIntervalSeconds), minFreeDiskGB: String(s.minFreeDiskGB), resumeMarginGB: String(s.resumeMarginGB), pacingJitterPercent: String(Math.round(s.pacing.jitter * 100)), downloadFormat: s.downloadFormat, sourceVideoQuality: s.sourceVideoQuality, reportDebouncePreset: s.reportDebouncePreset, archiveStorageBucket: "", archiveStoragePublicBaseUrl: "", maxParallelBuilds: String(s.buildPipeline.maxParallelBuilds), dockerImage: s.buildPipeline.dockerImage, dockerfile: s.buildPipeline.dockerfile, socialLinksJson: "[]", ...over, }; const form = new FormData(); for (const [k, v] of Object.entries(fields)) form.set(k, v); return form; } test("a save from the settings form keeps autoQueue, which the form is never sent", async () => { // A stored file whose lanes are NOT the defaults: a held lane, a changed // order, and a policy tree with a channel in it. const stored = defaultSiteSettings(); const lanes = Object.keys(stored.autoQueue) as Array; assert.ok(lanes.length >= 4, "the four lanes"); for (const lane of lanes) { const l = stored.autoQueue[lane] as unknown as Record; l.held = true; l.enabled = true; } stored.channelPriority.channels = { "some-channel": { tier: "low" } }; stored.workers = []; await writeSettings(stored); const before = getSettings(); assert.equal((before.autoQueue[lanes[0]] as unknown as { held: boolean }).held, true); // No field of the form names autoQueue: it has nothing to post it with. const form = formOf({ adminTitle: "Renamed by the form" }); assert.deepEqual( [...form.keys()].filter((k) => /autoQueue|channelPriority|workers/i.test(k)), [], ); const result = await saveSettingsAction(undefined, form); assert.deepEqual(result, { ok: true }); const after_ = getSettings(); assert.equal(after_.adminTitle, "Renamed by the form"); assert.deepEqual(after_.autoQueue, before.autoQueue); assert.deepEqual(after_.channelPriority, before.channelPriority); assert.deepEqual(after_.workers, before.workers); // And on disk, not only through the reader's defaults. const raw = JSON.parse(readFileSync(process.env.SETTINGS_FILE!, "utf8")); assert.deepEqual(raw.autoQueue, JSON.parse(JSON.stringify(before.autoQueue))); }); test("a refused save writes nothing", async () => { const before = readFileSync(process.env.SETTINGS_FILE!, "utf8"); const result = await saveSettingsAction(undefined, formOf({ adminTitle: "" })); assert.equal(result.ok, false); assert.equal(readFileSync(process.env.SETTINGS_FILE!, "utf8"), before); });