import { test } from "node:test"; import assert from "node:assert/strict"; import { BUILT_IN_PRESETS, builtInPresetById } from "yt-dlp-transcript-common/views/builtInPresets"; import { buildWidgetQuery, parseWidgetConfig } from "./config"; // Run from the repo root with: // ./node_modules/.bin/tsx --test editor/app/widget/lib/builtInPresets.test.ts // // A preset is a stored LINK, so the only thing that can go wrong with one is // that it stops meaning what it says: a renamed param, a changed default, a // section dropped from the registry, a layout the current placement math would // normalize differently. Each of those shows up as a query that does not // re-serialize to itself, which is what this asserts. // Parse a query string the way /widget does, then re-emit it. function roundTrip(query: string): string { const params: Record = {}; for (const [k, v] of new URLSearchParams(query)) params[k] = v; return buildWidgetQuery(parseWidgetConfig(params)); } test("every built-in preset parses and re-serializes to itself", () => { for (const preset of BUILT_IN_PRESETS) { assert.equal(roundTrip(preset.query), preset.query, `${preset.name} round-trips`); } }); test("built-in ids and names are unique and non-empty", () => { const ids = new Set(); const names = new Set(); for (const p of BUILT_IN_PRESETS) { assert.ok(p.id, "id is set"); assert.ok(p.name, "name is set"); assert.ok(p.hint, "hint is set"); assert.ok(!ids.has(p.id), `duplicate id ${p.id}`); assert.ok(!names.has(p.name), `duplicate name ${p.name}`); ids.add(p.id); names.add(p.name); } }); test("Jobs is the way back to the default widget", () => { // The "start over" entry has to be the empty query, or a bare /widget link // would no longer be reachable from the menu. assert.equal(builtInPresetById("jobs")?.query, ""); assert.equal(roundTrip(""), ""); }); test("Cockpit is the three-row layout, fill row included", () => { const config = parseWidgetConfig( Object.fromEntries( new URLSearchParams(builtInPresetById("cockpit")?.query ?? ""), ), ); assert.equal(config.layout.rows.length, 3); assert.equal(config.layout.cols, 2); assert.equal(config.layout.rows[0].cells[0].span, 2); assert.equal(config.layout.rows[1].cells[0].flow, "row"); assert.equal(config.layout.rows[2].size, "fill"); }); test("Glance is one dense rail and nothing else", () => { const config = parseWidgetConfig( Object.fromEntries( new URLSearchParams(builtInPresetById("glance")?.query ?? ""), ), ); assert.equal(config.layout.rows.length, 1); assert.equal(config.layout.rows[0].cells.length, 1); assert.equal(config.layout.rows[0].cells[0].flow, "row"); assert.equal(config.showTitles, false); }); test("Wall is locked down: no controls, no gear", () => { const config = parseWidgetConfig( Object.fromEntries(new URLSearchParams(builtInPresetById("wall")?.query ?? "")), ); assert.equal(config.controls, false); assert.equal(config.settings, false); assert.equal(config.showTitles, true); assert.equal(config.layout.cols, 3); });