import { test } from "node:test"; import assert from "node:assert/strict"; import { mkdtemp, mkdir, readFile, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import type { Paths } from "../lib/paths"; import { patchChannelConfig, readChannelConfig } from "./channels"; // Run with: // pnpm --filter yt-dlp-transcript-common exec tsx --test controller/manualDownloadsConfig.test.ts // // `downloadMode` (2026-10-29, lib/manualDownloads.ts) through the one config // writer, in a temp corpus. test("patchChannelConfig sets it, keeps it across another patch, and unsets it", async () => { const dir = await mkdtemp(path.join(tmpdir(), "manual-dl-")); const paths = { channelsDir: path.join(dir, "channels") } as Paths; const slug = "harmful"; await mkdir(path.join(paths.channelsDir, slug), { recursive: true }); const file = path.join(paths.channelsDir, slug, "config.json"); await writeFile( file, JSON.stringify({ handling: "transcribe", url: "https://www.youtube.com/@h", downloadFilter: { include: "guest" } }), ); await patchChannelConfig(paths, slug, { downloadMode: "manual" }); assert.equal((await readChannelConfig(paths, slug))?.downloadMode, "manual"); assert.equal(JSON.parse(await readFile(file, "utf8")).downloadMode, "manual"); // The operator's second step: the filter goes, the mode stays. await patchChannelConfig(paths, slug, {}, { unset: ["downloadFilter"] }); const after = await readChannelConfig(paths, slug); assert.equal(after?.downloadMode, "manual"); assert.equal(after && "downloadFilter" in after, false); await patchChannelConfig(paths, slug, {}, { unset: ["downloadMode"] }); assert.equal("downloadMode" in JSON.parse(await readFile(file, "utf8")), false); });