import { test } from "node:test"; import assert from "node:assert/strict"; import { DEFAULT_LOOKOUTS_SETTINGS, lookoutById, lookoutIdFrom, sanitizeLookoutItem, sanitizeLookouts, } from "./lookoutSettings"; import { defaults, siteSettingsSchema } from "./settingsSchema"; // Run with: // pnpm --filter yt-dlp-transcript-common exec tsx --test lib/lookoutSettings.test.ts test("off and empty by default, in the schema too", () => { assert.deepEqual(DEFAULT_LOOKOUTS_SETTINGS, { enabled: false, afterSync: false, items: [] }); assert.deepEqual(sanitizeLookouts(undefined), DEFAULT_LOOKOUTS_SETTINGS); assert.deepEqual(defaults().lookouts, DEFAULT_LOOKOUTS_SETTINGS); assert.deepEqual(siteSettingsSchema.parse({ lookouts: "nonsense" }).lookouts, DEFAULT_LOOKOUTS_SETTINGS); }); test("an item gets every default it leaves out", () => { assert.deepEqual(sanitizeLookoutItem({ id: "weekly", channels: ["example-news", "example-live"] }), { id: "weekly", label: "weekly", channels: ["example-news", "example-live"], days: 7, prompt: "", maxVideos: 40, minSeconds: 90, timeZone: "", siteOrigin: "", }); }); test("an item is coerced, never thrown on", () => { const item = sanitizeLookoutItem({ label: " Example Week! ", channels: ["example-news", "example-news", "", 7, "../escape", "ok.slug_1"], days: 999, maxVideos: "12", minSeconds: -5, timeZone: "Mars/Olympus", siteOrigin: "https://example.pages.dev/some/path?q=1", prompt: "x".repeat(5000), }); assert.ok(item); assert.equal(item.id, "example-week"); assert.equal(item.label, "Example Week!"); assert.deepEqual(item.channels, ["example-news", "ok.slug_1"]); assert.equal(item.days, 60); assert.equal(item.maxVideos, 12); assert.equal(item.minSeconds, 0); assert.equal(item.timeZone, "", "an unknown zone falls back to the machine's"); assert.equal(item.siteOrigin, "https://example.pages.dev"); assert.equal(item.prompt.length, 4000); assert.equal(sanitizeLookoutItem({ timeZone: "America/Chicago", id: "a" })?.timeZone, "America/Chicago"); assert.equal(sanitizeLookoutItem({ siteOrigin: "ftp://x.example", id: "a" })?.siteOrigin, ""); }); test("an item with no usable id is dropped, and so is a second with the same id", () => { const s = sanitizeLookouts({ enabled: true, afterSync: "yes", items: [{ channels: ["a"] }, { id: "one", channels: ["a"] }, { id: "one", channels: ["b"] }, "junk", { label: "Two" }], }); assert.equal(s.enabled, true); assert.equal(s.afterSync, false, "only true is true"); assert.deepEqual(s.items.map((i) => [i.id, i.channels]), [["one", ["a"]], ["two", []]]); assert.equal(lookoutById(s, "two")?.label, "Two"); assert.equal(lookoutById(s, "three"), null); }); test("lookoutIdFrom makes a slug", () => { assert.equal(lookoutIdFrom("Café Weekly — News"), "cafe-weekly-news"); assert.equal(lookoutIdFrom("!!!"), ""); assert.equal(lookoutIdFrom("a".repeat(80)).length, 64); });