import test from "node:test"; import assert from "node:assert/strict"; import { citationText, facetsOf, hitKey, isoDate, trackOf, yearOf } from "./facets.mjs"; import { sessionKey } from "./session.mjs"; import { isLocalOnly, LOCAL_ONLY_CHANNELS } from "./rules.mjs"; test("trackOf: vtt is captions, *-json our transcription, anything else unknown", () => { assert.equal(trackOf("vtt"), "captions"); assert.equal(trackOf("whisper-json"), "transcript"); assert.equal(trackOf("chough-json"), "transcript"); assert.equal(trackOf(undefined), "unknown"); assert.equal(trackOf("srt"), "unknown"); }); test("facets: every year in the span (zeros kept), channels most first, tracks, states", () => { const h = (channelSlug, uploadDate, track = "captions", state = "available") => ({ channelSlug, channelName: channelSlug.toUpperCase(), uploadDate, track, state }); const f = facetsOf([h("a", "20190105"), h("b", "20220101"), h("b", "20220301", "transcript"), h("a", "20220601", "captions", "deleted")]); assert.deepEqual(f.years, [ { year: "2019", n: 1 }, { year: "2020", n: 0 }, { year: "2021", n: 0 }, { year: "2022", n: 3 }, ]); assert.deepEqual(f.channels.map((c) => [c.slug, c.n]), [ ["a", 2], ["b", 2], ]); assert.deepEqual(f.tracks, { captions: 3, transcript: 1 }); assert.deepEqual(f.states, { available: 3, deleted: 1 }); assert.deepEqual(facetsOf([]).years, []); }); test("keys and dates", () => { assert.equal(hitKey({ channelSlug: "c", videoId: "v" }), "c/v"); assert.equal(yearOf("20240102"), "2024"); assert.equal(isoDate("20240102"), "2024-01-02"); }); test("sessionKey: same query and scope, same session, whatever the order", () => { const a = sessionKey({ source: "local:/x", query: "fired ", channels: ["b", "a"] }); const b = sessionKey({ source: "local:/x", query: "fired", channels: ["a", "b"] }); assert.equal(a, b); assert.match(a, /^[0-9a-f]{16}$/); assert.notEqual(a, sessionKey({ source: "local:/x", query: "fired", channels: ["a"] })); assert.notEqual(a, sessionKey({ source: "local:/x", query: "fired", channels: ["a", "b"], regex: true })); }); test("citationText: the house format for a video, a post, and a video with no link", () => { assert.equal( citationText({ quote: "How many employees?\nYou fired them all,", title: "Kino Casino", clock: "1:22:53", url: "https://j.example/?v=c%2Fv&t=4973" }), '"How many employees? You fired them all," [Kino Casino @ 1:22:53](https://j.example/?v=c%2Fv&t=4973)', ); assert.equal( citationText({ quote: "No.", title: "", post: { author: "synthbaker", date: "2026-02-11" }, url: "https://x.example/p/1" }), '"No." [post by synthbaker, 2026-02-11](https://x.example/p/1)', ); assert.equal(citationText({ title: "T", clock: "0:05", url: null }), "T @ 0:05"); }); test("Candace channels are local-only", () => { assert.deepEqual([...LOCAL_ONLY_CHANNELS], ["candace-owens", "realcandaceo"]); assert.ok(isLocalOnly("realcandaceo")); assert.ok(isLocalOnly(" Candace-Owens")); assert.ok(!isLocalOnly("the-quartering")); assert.ok(!isLocalOnly(undefined)); });