diff options
Diffstat (limited to 'castle-search/test')
| -rw-r--r-- | castle-search/test/http.test.ts | 188 | ||||
| -rw-r--r-- | castle-search/test/video.test.ts | 79 | ||||
| -rw-r--r-- | castle-search/test/x-limits.test.ts | 98 | ||||
| -rw-r--r-- | castle-search/test/x-login.test.ts | 91 | ||||
| -rw-r--r-- | castle-search/test/youtube.test.ts | 72 |
5 files changed, 528 insertions, 0 deletions
diff --git a/castle-search/test/http.test.ts b/castle-search/test/http.test.ts new file mode 100644 index 0000000..c076863 --- /dev/null +++ b/castle-search/test/http.test.ts @@ -0,0 +1,188 @@ +import assert from "node:assert/strict"; +import { test } from "node:test"; + +import { NoResultsError, SearchChallengeError } from "../src/errors.ts"; +import { handleSearch, parseListen, type Searcher } from "../src/http.ts"; + +const signal = new AbortController().signal; + +const returning = (results: { title: string; url: string; snippet: string }[]): Searcher => + async () => ({ results, searchUrl: "", finalUrl: "", endpoint: "", engine: {} as never, engineSource: "" }); + +const throwing = (e: Error): Searcher => async () => { + throw e; +}; + +test("results come back in Open WebUI's shape", async () => { + const reply = await handleSearch( + JSON.stringify({ query: "x", count: 2 }), + returning([{ title: "T", url: "https://e.com/", snippet: "S" }]), + signal, + ); + assert.equal(reply.status, 200); + assert.deepEqual(reply.body, [{ link: "https://e.com/", title: "T", snippet: "S" }]); +}); + +test("count is clamped and defaulted", async () => { + const seen: number[] = []; + const run: Searcher = async (request) => { + seen.push(request.numResults); + return returning([])(request, signal); + }; + await handleSearch(JSON.stringify({ query: "x", count: 500 }), run, signal); + await handleSearch(JSON.stringify({ query: "x", count: 0 }), run, signal); + await handleSearch(JSON.stringify({ query: "x" }), run, signal); + assert.deepEqual(seen, [20, 1, 5]); +}); + +test("a challenge is an error with its reason, never an empty list", async () => { + const reply = await handleSearch( + JSON.stringify({ query: "x" }), + throwing(new SearchChallengeError({ + challenge: "captcha", pageUrl: "https://duckduckgo.com/", endpoint: "e", engine: "DuckDuckGo", alternatives: ["bing"], + })), + signal, + ); + assert.equal(reply.status, 502); + assert.match((reply.body as { error: string }).error, /DuckDuckGo is blocked/); + assert.match((reply.body as { error: string }).error, /cannot switch engines/); +}); + +test("no results is an error too", async () => { + const reply = await handleSearch(JSON.stringify({ query: "x" }), throwing(new NoResultsError("nothing")), signal); + assert.equal(reply.status, 502); +}); + +test("bad requests are refused", async () => { + const run = returning([]); + assert.equal((await handleSearch("not json", run, signal)).status, 400); + assert.equal((await handleSearch(JSON.stringify({ query: " " }), run, signal)).status, 400); + assert.equal((await handleSearch(JSON.stringify({}), run, signal)).status, 400); +}); + +test("listen addresses parse", () => { + assert.deepEqual(parseListen("127.0.0.1:11158"), { host: "127.0.0.1", port: 11158 }); + assert.throws(() => parseListen("11158")); + assert.throws(() => parseListen("h:99999")); +}); + +import { buildXSearchUrl, parseMetrics } from "../src/x-search.ts"; +import { handleXSearch, OPENAPI, type XSearcher } from "../src/http.ts"; + +const xReturning = (seen: unknown[] = []): XSearcher => async (request) => { + seen.push(request); + return { query: request.query, sort: request.sort ?? "latest", searchUrl: "", posts: [] }; +}; + +test("x-search defaults to the 10 latest posts and clamps count", async () => { + const seen: unknown[] = []; + await handleXSearch(JSON.stringify({ query: " from:nasa " }), xReturning(seen), signal); + await handleXSearch(JSON.stringify({ query: "x", count: 99, sort: "top" }), xReturning(seen), signal); + assert.deepEqual(seen, [ + { query: "from:nasa", count: 10, sort: "latest" }, + { query: "x", count: 20, sort: "top" }, + ]); +}); + +test("x-search refuses a bad sort or query rather than guessing", async () => { + assert.equal((await handleXSearch(JSON.stringify({ query: "x", sort: "new" }), xReturning(), signal)).status, 400); + assert.equal((await handleXSearch(JSON.stringify({ query: "" }), xReturning(), signal)).status, 400); +}); + +test("a failed x-search is a 502 with its reason", async () => { + const reply = await handleXSearch( + JSON.stringify({ query: "x" }), + async () => { + throw new Error("signed out"); + }, + signal, + ); + assert.equal(reply.status, 502); + assert.deepEqual(reply.body, { error: "signed out" }); +}); + +test("x search URLs: latest is f=live, top has no tab parameter", () => { + assert.equal(buildXSearchUrl("from:a b", "latest"), "https://x.com/search?q=from%3Aa+b&src=typed_query&f=live"); + assert.equal(buildXSearchUrl("from:a b", "top"), "https://x.com/search?q=from%3Aa+b&src=typed_query"); +}); + +test("X's action-bar label parses, singular and plural", () => { + assert.deepEqual(parseMetrics("1 reply, 971 reposts, 8852 likes, 1247 bookmarks, 16029782 views"), { + replies: 1, reposts: 971, likes: 8852, views: 16029782, + }); + assert.deepEqual(parseMetrics(null), {}); +}); + +test("the OpenAPI description names the operation Open WebUI will offer", () => { + assert.equal(OPENAPI.paths["/x-search"].post.operationId, "search_x"); +}); + +import { handleXPost, handleXTrends, handleXUser } from "../src/http.ts"; +import { handleOf, parseProfile, parseTrend, postIdOf } from "../src/x-search.ts"; + +test("post links of every shape give the post id", () => { + assert.equal(postIdOf("https://x.com/NASA/status/2122740072386609201"), "2122740072386609201"); + assert.equal(postIdOf("https://twitter.com/nasa/status/2122740072386609201?s=20"), "2122740072386609201"); + assert.equal(postIdOf("https://x.com/i/web/status/2122740072386609201"), "2122740072386609201"); + assert.equal(postIdOf("2122740072386609201"), "2122740072386609201"); + assert.equal(postIdOf("https://x.com/NASA"), undefined); +}); + +test("handles are read from @name, name or a profile link", () => { + assert.equal(handleOf("@NASA"), "NASA"); + assert.equal(handleOf("https://x.com/nasa/"), "nasa"); + assert.equal(handleOf("not a handle"), undefined); + assert.equal(handleOf("waytoolonghandle_123"), undefined); +}); + +test("a trend cell's lines become a trend", () => { + assert.deepEqual(parseTrend("2\n·\nVideo game genres · Trending\nGTA 6\nTrending with #GTAVI, Metacritic"), { + rank: 2, context: "Video game genres · Trending", name: "GTA 6", query: "GTA 6", detail: "Trending with #GTAVI, Metacritic", + }); + assert.deepEqual(parseTrend("Trending in United Kingdom\nGlastonbury"), { context: "Trending in United Kingdom", name: "Glastonbury", query: "Glastonbury" }); + assert.equal(parseTrend("1"), undefined); +}); + +test("a profile's JSON-LD gives exact counts", () => { + const p = parseProfile( + JSON.stringify({ + dateCreated: "2007-12-19T20:20:32.000Z", + relatedLink: ["https://t.co/9NkQJKAnuU", "http://www.nasa.gov/"], + mainEntity: { + additionalName: "NASA", name: "NASA", description: "Making the seemingly impossible, possible.", + homeLocation: { name: "Pale Blue Dot" }, url: "https://x.com/NASA", + interactionStatistic: [ + { name: "Follows", userInteractionCount: 92225170 }, + { name: "Friends", userInteractionCount: 115 }, + { name: "Tweets", userInteractionCount: 74553 }, + ], + }, + }), + "nasa", + ); + assert.equal(p.handle, "@NASA"); + assert.equal(p.followers, 92225170); + assert.equal(p.following, 115); + assert.equal(p.posts, 74553); + assert.equal(p.website, "http://www.nasa.gov/"); + assert.equal(p.location, "Pale Blue Dot"); +}); + +test("the X reads check what they are given before touching the browser", async () => { + const never = async (): Promise<never> => { + throw new Error("must not run"); + }; + assert.equal((await handleXPost(JSON.stringify({ url: "https://x.com/NASA" }), never, signal)).status, 400); + assert.equal((await handleXUser(JSON.stringify({ handle: "two words" }), never, signal)).status, 400); + assert.equal((await handleXTrends(JSON.stringify({ tab: "music" }), never, signal)).status, 400); + assert.equal((await handleXPost("[1]", never, signal)).status, 400); + const seen: unknown[] = []; + await handleXPost(JSON.stringify({ url: "https://x.com/a/status/12345678", replies: 99 }), async (r) => (seen.push(r), { post: {}, thread: [], replies: [] }) as never, signal); + await handleXTrends("", async (t) => (seen.push(t), { tab: t, trends: [] }), signal); + assert.deepEqual(seen, [{ url: "https://x.com/a/status/12345678", replies: 20 }, "trending"]); +}); + +test("every X read is in the OpenAPI description", () => { + assert.deepEqual(Object.keys(OPENAPI.paths), ["/x-search", "/x-post", "/x-user", "/x-trends"]); + assert.equal(OPENAPI.info.title, "X.com"); +}); diff --git a/castle-search/test/video.test.ts b/castle-search/test/video.test.ts new file mode 100644 index 0000000..ece0353 --- /dev/null +++ b/castle-search/test/video.test.ts @@ -0,0 +1,79 @@ +import assert from "node:assert/strict"; +import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { test } from "node:test"; + +import { handleVideo } from "../src/http.ts"; +import { clipName, planOf, usedToday, type VideoRequest, videoRef } from "../src/video.ts"; + +const req = (over: Partial<VideoRequest> = {}): VideoRequest => ({ prompt: "a fox", kind: "fast", portrait: false, ...over }); + +test("each kind asks its own model, with its own fixed length and sound", () => { + assert.equal(planOf(req()).model, "FastVideo/FastWan2.2-TI2V-5B-FullAttn-Diffusers"); + assert.deepEqual(planOf(req()).body, { prompt: "a fox", seconds: 5, resolution: "720p", orientation: "landscape" }); + assert.equal(planOf(req({ kind: "quality", portrait: true })).body["orientation"], "portrait"); + const long = planOf(req({ kind: "long", seconds: 12, imageUrl: "https://x/y.png" })); + assert.equal(long.model, "Lightricks/LTX-2.3-Fast"); + assert.deepEqual(long.body, { prompt: "a fox", duration: 12, resolution: "1280x720", generate_audio: true, image_url: "https://x/y.png" }); + assert.equal(planOf(req({ kind: "long" })).seconds, 6); + const sound = planOf(req({ kind: "sound", portrait: true })); + assert.equal(sound.model, "google/veo-3.1-fast"); + assert.equal(sound.body["aspect_ratio"], "9:16"); + assert.equal(sound.sound, true); + assert.equal(planOf(req()).sound, false); +}); + +test("a clip is named by the time and the prompt's first words", () => { + const at = new Date(2026, 10, 19, 21, 5, 9); + assert.equal(clipName("A red fox, trotting through fresh snow in a pine forest", at), "2026-11-19-210509-a-red-fox-trotting-through-fresh.mp4"); + assert.equal(clipName("¡¿", at), "2026-11-19-210509.mp4"); + assert.equal(clipName("Café crème", at), "2026-11-19-210509-cafe-creme.mp4"); +}); + +test("the daily count is today's only, and a missing or broken file is none", () => { + const dir = mkdtempSync(join(tmpdir(), "video-")); + try { + const state = join(dir, "state.json"); + const now = new Date("2026-11-19T12:00:00Z"); + assert.equal(usedToday(state, now), 0); + assert.equal(usedToday(undefined, now), 0); + writeFileSync(state, JSON.stringify({ day: "2026-11-19", clips: 4 })); + assert.equal(usedToday(state, now), 4); + assert.equal(usedToday(state, new Date("2026-11-20T00:00:01Z")), 0); + writeFileSync(state, "{nope"); + assert.equal(usedToday(state, now), 0); + } finally { + rmSync(dir, { recursive: true, force: true }); + } +}); + +test("the video comes from video_url or the first of videos", () => { + assert.equal(videoRef({ video_url: "data:video/mp4;base64,AA" }), "data:video/mp4;base64,AA"); + assert.equal(videoRef({ status: "ok", videos: ["/model/inference/x.mp4"] }), "/model/inference/x.mp4"); + assert.equal(videoRef({ status: "error", videos: [] }), null); +}); + +test("/video checks its body before spending anything", async () => { + const calls: VideoRequest[] = []; + const run = async (r: VideoRequest) => { + calls.push(r); + return { file: "video/x.mp4" } as never; + }; + const bad = async (body: unknown) => (await handleVideo(JSON.stringify(body), run)).status; + assert.equal(await bad({}), 400); + assert.equal(await bad({ prompt: " " }), 400); + assert.equal(await bad({ prompt: "x".repeat(2001) }), 400); + assert.equal(await bad({ prompt: "a fox", kind: "cinema" }), 400); + assert.equal(await bad({ prompt: "a fox", orientation: "square" }), 400); + assert.equal(await bad({ prompt: "a fox", seconds: 10 }), 400); + assert.equal(await bad({ prompt: "a fox", kind: "long", seconds: 7 }), 400); + assert.equal(await bad({ prompt: "a fox", image_url: "https://x/y.png" }), 400); + assert.equal(await bad({ prompt: "a fox", kind: "long", image_url: "file:///etc/passwd" }), 400); + assert.equal(calls.length, 0); + + assert.equal(await bad({ prompt: " a fox " }), 200); + assert.deepEqual(calls.at(-1), { prompt: "a fox", kind: "fast", portrait: false, seconds: undefined, imageUrl: undefined }); + assert.equal(await bad({ prompt: "a fox", kind: "long", seconds: 10, orientation: "portrait", image_url: "data:image/png;base64,AA" }), 200); + assert.deepEqual(calls.at(-1), { prompt: "a fox", kind: "long", portrait: true, seconds: 10, imageUrl: "data:image/png;base64,AA" }); +}); diff --git a/castle-search/test/x-limits.test.ts b/castle-search/test/x-limits.test.ts new file mode 100644 index 0000000..6be5f7d --- /dev/null +++ b/castle-search/test/x-limits.test.ts @@ -0,0 +1,98 @@ +import assert from "node:assert/strict"; +import { test } from "node:test"; + +import { type Clock, XLimiter, XRateLimitError } from "../src/x-limits.ts"; + +/** A clock that only moves when told to, recording the waits asked of it. */ +const fakeClock = () => { + let now = 1_000_000; + const waits: number[] = []; + let draw = 0.5; + const clock: Clock = { + random: () => draw, + now: () => now, + sleep: async (ms) => { + waits.push(ms); + now += ms; + }, + }; + return { clock, waits, advance: (ms: number) => (now += ms), setDraw: (r: number) => (draw = r) }; +}; + +const signal = new AbortController().signal; +const ok = async () => "ok"; + +test("a search sooner than the gap waits out the rest of it", async () => { + const { clock, waits, advance } = fakeClock(); + const l = new XLimiter(clock, { gap: 10_000, jitter: 0, hour: 30, day: 150 }); + await l.run(signal, ok); + advance(3_000); + await l.run(signal, ok); + assert.deepEqual(waits, [7_000]); +}); + +test("over the hourly cap a search fails at once, naming when it can run", async () => { + const { clock, advance } = fakeClock(); + const l = new XLimiter(clock, { gap: 0, jitter: 0, hour: 2, day: 150 }); + await l.run(signal, ok); + await l.run(signal, ok); + await assert.rejects(l.run(signal, ok), (e) => e instanceof XRateLimitError && /last hour/.test(e.message)); + advance(3_600_001); + assert.equal(await l.run(signal, ok), "ok"); +}); + +test("the daily cap holds across hours", async () => { + const { clock, advance } = fakeClock(); + const l = new XLimiter(clock, { gap: 0, jitter: 0, hour: 100, day: 3 }); + for (let i = 0; i < 3; i++) { + await l.run(signal, ok); + advance(3_600_001); + } + await assert.rejects(l.run(signal, ok), (e) => e instanceof XRateLimitError && /24 hours/.test(e.message)); +}); + +test("one at a time: the second starts only after the first finishes", async () => { + const { clock } = fakeClock(); + const l = new XLimiter(clock, { gap: 0, jitter: 0, hour: 30, day: 150 }); + const order: string[] = []; + let release!: () => void; + const first = l.run(signal, async () => { + order.push("first start"); + await new Promise<void>((r) => (release = r)); + order.push("first end"); + }); + const second = l.run(signal, async () => { + order.push("second start"); + }); + await new Promise((r) => setTimeout(r, 10)); + release(); + await Promise.all([first, second]); + assert.deepEqual(order, ["first start", "first end", "second start"]); +}); + +test("a failed search does not block the queue", async () => { + const { clock } = fakeClock(); + const l = new XLimiter(clock, { gap: 0, jitter: 0, hour: 30, day: 150 }); + await assert.rejects(l.run(signal, async () => { + throw new Error("boom"); + })); + assert.equal(await l.run(signal, ok), "ok"); +}); + +test("the gap is drawn afresh each time, within gap ± jitter", async () => { + const { clock, waits, setDraw } = fakeClock(); + const l = new XLimiter(clock, { gap: 30_000, jitter: 25_000, hour: 30, day: 150 }); + await l.run(signal, ok); + setDraw(0); // the shortest: 30 - 25 s + await l.run(signal, ok); + setDraw(0.999999); // close to the longest: 30 + 25 s + await l.run(signal, ok); + assert.equal(waits[0], 5_000); + assert.ok(waits[1]! > 54_999 && waits[1]! <= 55_000); +}); + +test("the defaults are 30 s ± 25 s", async () => { + const { GAP_MS, JITTER_MS } = await import("../src/x-limits.ts"); + assert.equal(GAP_MS, 30_000); + assert.equal(JITTER_MS, 25_000); +}); diff --git a/castle-search/test/x-login.test.ts b/castle-search/test/x-login.test.ts new file mode 100644 index 0000000..11468d5 --- /dev/null +++ b/castle-search/test/x-login.test.ts @@ -0,0 +1,91 @@ +import assert from "node:assert/strict"; +import { test } from "node:test"; + +import { decodeWords, parseFetch, parseInternalDate, xCodeFromSubject } from "../src/imap.ts"; +import { classify, type LoginProbe } from "../src/x-login.ts"; +import { base32Decode, totp, totpRemaining } from "../src/totp.ts"; + +// RFC 6238 appendix B, SHA-1: the secret "12345678901234567890". +const RFC_SECRET = "GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ"; + +test("totp matches RFC 6238's SHA-1 vectors", () => { + assert.equal(totp(RFC_SECRET, 59_000, 8), "94287082"); + assert.equal(totp(RFC_SECRET, 1_111_111_109_000, 8), "07081804"); + assert.equal(totp(RFC_SECRET, 1_234_567_890_000, 8), "89005924"); + assert.equal(totp(RFC_SECRET, 59_000), "287082"); +}); + +test("base32 ignores spaces and case, and refuses other characters", () => { + assert.deepEqual(base32Decode("gezd gnbv"), base32Decode("GEZDGNBV")); + assert.throws(() => base32Decode("GEZD1")); +}); + +test("totpRemaining counts down within the step", () => { + assert.equal(totpRemaining(0), 30); + assert.equal(totpRemaining(29_000), 1); +}); + +test("X's code is read from the subjects X uses, and nothing else", () => { + assert.equal(xCodeFromSubject("Your X confirmation code is abc12def"), "abc12def"); + assert.equal(xCodeFromSubject("Your X verification code is: 123456"), "123456"); + assert.equal(xCodeFromSubject("123456 is your X verification code"), "123456"); + assert.equal(xCodeFromSubject("New login to X from Chrome on Mac"), undefined); + assert.equal(xCodeFromSubject("Security alert"), undefined); +}); + +test("encoded subjects are decoded", () => { + assert.equal(decodeWords("=?UTF-8?B?WW91ciBYIGNvbmZpcm1hdGlvbiBjb2RlIGlzIGFiYzEyZGVm?="), "Your X confirmation code is abc12def"); + assert.equal(decodeWords("=?UTF-8?Q?Caf=C3=A9_menu?="), "Café menu"); +}); + +test("a FETCH reply splits into one header set per message", () => { + const raw = + '* 1 FETCH (INTERNALDATE "18-Nov-2026 10:16:23 +0000" BODY[HEADER.FIELDS (FROM SUBJECT)] {60}\r\n' + + "From: X <verify@x.com>\r\nSubject: Your X confirmation code is\r\n abc12def\r\n\r\n)\r\n" + + '* 2 FETCH (INTERNALDATE "18-Nov-2026 10:20:00 +0000" BODY[HEADER.FIELDS (FROM SUBJECT)] {40}\r\n' + + "From: X <info@x.com>\r\nSubject: New login\r\n\r\n)\r\nc4 OK Success\r\n"; + const mails = parseFetch(raw); + assert.equal(mails.length, 2); + assert.equal(mails[0]!.subject, "Your X confirmation code is abc12def"); + assert.equal(mails[0]!.received, parseInternalDate("18-Nov-2026 10:16:23 +0000")); + assert.equal(mails[0]!.received, Date.UTC(2026, 10, 18, 10, 16, 23)); + assert.equal(mails[1]!.from, "X <info@x.com>"); +}); + +const probe = (over: Partial<LoginProbe>): LoginProbe => ({ + url: "https://x.com/i/jf/onboarding/web?mode=login", + path: "/i/jf/onboarding/web", + hash: "", + text: "", + inputs: [], + signedIn: false, + ...over, +}); + +// The steps as castle Chrome saw them on 2026-11-18. +test("each step of X's sign-in is recognised", () => { + assert.equal( + classify(probe({ text: "See what's happening\n\nEmail or username\n\nContinue", inputs: [{ name: "username_or_email", type: "text", autocomplete: "username webauthn" }] })).kind, + "username", + ); + assert.equal( + classify(probe({ hash: "#/s/login_enter_password/r-1", text: "Login\n\nUsername\nPassword", inputs: [{ name: "username", type: "text", autocomplete: "" }, { name: "password", type: "password", autocomplete: "" }] })).kind, + "password", + ); + assert.equal( + classify(probe({ hash: "#/s/two_factor_code/r-2", text: "Enter your two factor code\n\nUse your authenticator app to generate the code.", inputs: [{ name: "", type: "text", autocomplete: "one-time-code" }] })).kind, + "totp", + ); + assert.equal( + classify(probe({ text: "Check your email\n\nYou'll receive a code to verify here so you can sign in.", inputs: [{ name: "text", type: "text", autocomplete: "" }] })).kind, + "email-code", + ); + assert.equal( + classify(probe({ text: "Enter your phone number or username\n\nThere was unusual login activity on your account.", inputs: [{ name: "text", type: "text", autocomplete: "" }] })).kind, + "username", + ); + assert.equal(classify(probe({ url: "https://x.com/home", path: "/home", signedIn: true })).kind, "done"); + assert.equal(classify(probe({})).kind, "loading"); + assert.equal(classify(probe({ text: "Authenticate your account\n\nSolve this puzzle" })).kind, "unknown"); + assert.equal(classify(probe({ text: "Wrong password!", inputs: [{ name: "password", type: "password", autocomplete: "" }] })).kind, "wrong-password"); +}); diff --git a/castle-search/test/youtube.test.ts b/castle-search/test/youtube.test.ts new file mode 100644 index 0000000..c5530fb --- /dev/null +++ b/castle-search/test/youtube.test.ts @@ -0,0 +1,72 @@ +import assert from "node:assert/strict"; +import { test } from "node:test"; + +import { handleTranscript } from "../src/http.ts"; +import { cueTexts, dedupeRolling, isoDate, paragraphs, titleSlug, videoIdOf } from "../src/youtube.ts"; + +test("every shape of YouTube link gives the video id, share junk ignored", () => { + for (const url of [ + "https://www.youtube.com/watch?v=jNQXAC9IVRw", + "https://youtube.com/watch?feature=share&v=jNQXAC9IVRw&t=30s", + "https://youtu.be/jNQXAC9IVRw?si=T-d5TUXNFjmR1Uad", + "https://www.youtube.com/shorts/jNQXAC9IVRw", + "https://www.youtube.com/live/jNQXAC9IVRw?feature=shared", + "https://m.youtube.com/watch?v=jNQXAC9IVRw&list=PL123", + "jNQXAC9IVRw", + ]) { + assert.equal(videoIdOf(url), "jNQXAC9IVRw", url); + } + assert.equal(videoIdOf("https://vimeo.com/123"), undefined); +}); + +test("titles become the archive's dashed names", () => { + assert.equal(titleSlug("How to stay calm when you know you'll be stressed | Daniel Levitin | TED"), "How-to-stay-calm-when-you-know-youll-be-stressed-Daniel-Levitin-TED"); + assert.equal(titleSlug("Anthropic Files for IPO: Nasdaq!"), "Anthropic-Files-for-IPO-Nasdaq"); + assert.equal(titleSlug("Café Müller"), "Cafe-Muller"); + assert.equal(titleSlug("!!!"), "transcript"); + assert.equal(isoDate("20051024"), "2005-10-24"); +}); + +// The shape of YouTube's automatic captions: each cue repeats the last one's words. +const ROLLING = `WEBVTT +Kind: captions +Language: en + +00:00:00.000 --> 00:00:02.000 align:start position:0% +the filing is out<00:00:01.000><c> ENT</c><00:00:01.500><c> is</c> + +00:00:02.000 --> 00:00:02.010 align:start position:0% +the filing is out ENT is + +00:00:02.010 --> 00:00:04.000 align:start position:0% +the filing is out ENT is +going to be [Music] big & bold + +00:00:04.000 --> 00:00:06.000 +going to be big & bold for everyone +`; + +test("rolling captions come out once, markup and noise gone", () => { + const cues = cueTexts(ROLLING); + assert.equal(cues[0], "the filing is out ENT is"); + assert.equal(dedupeRolling(cues).join(" "), "the filing is out ENT is going to be big & bold for everyone"); +}); + +test("words are set in paragraphs of 90", () => { + const words = Array.from({ length: 200 }, (_, i) => `w${i}`); + const text = paragraphs(words); + assert.equal(text.split("\n\n").length, 3); + assert.equal(text.split("\n\n")[0]!.split(" ").length, 90); +}); + +test("the transcript route checks the link and language first", async () => { + const signal = new AbortController().signal; + const never = async (): Promise<never> => { + throw new Error("must not run"); + }; + assert.equal((await handleTranscript(JSON.stringify({ url: "https://vimeo.com/1" }), never, signal)).status, 400); + assert.equal((await handleTranscript(JSON.stringify({ url: "https://youtu.be/jNQXAC9IVRw", language: "english!" }), never, signal)).status, 400); + const seen: unknown[] = []; + await handleTranscript(JSON.stringify({ url: "https://youtu.be/jNQXAC9IVRw", language: "pt", max_chars: 10 }), async (r) => (seen.push(r), {}) as never, signal); + assert.deepEqual(seen, [{ url: "https://youtu.be/jNQXAC9IVRw", language: "pt", maxChars: 1000 }]); +}); |
