import test from "node:test"; import assert from "node:assert/strict"; import { callPost, setupOpsCorpus } from "./_testCorpus"; // Run with: // pnpm -C editor exec tsx --test "app/api/ops/_research.test.ts" // // The research actions (release 27 S3b): archive-search, archive-item and // import-post, refused at the door before anything reaches archive.org or a // platform. The reads and the import themselves are tested in common // (controller/archiveOrgResearch.test.ts, social/importPost.test.ts). const corpus = await setupOpsCorpus("one-youtube-channel-with-data"); type Post = Parameters[0]; const route = async (name: string) => (await import(`./${name}/route`)).POST as Post; test.after(() => corpus.cleanup()); test("archive-search: a query is required; the limit is at most 500", async () => { const search = await route("archive-search"); const none = await callPost(search, {}); assert.equal(none.status, 400); assert.match(String(none.body.error), /"query" is required/); const big = await callPost(search, { query: "bakery", limit: 501 }); assert.equal(big.status, 400); assert.match(String(big.body.error), /"limit" is at most 500/); }); test("archive-item: an identifier, nothing else", async () => { const item = await route("archive-item"); const bad = await callPost(item, { item: "../../etc" }); assert.equal(bad.status, 400); assert.match(String(bad.body.error), /is not an archive\.org identifier/); const extra = await callPost(item, { item: "x", files: [] }); assert.equal(extra.status, 400); }); test("import-post: refused before any job — not a post URL, not a posts channel, no such channel", async () => { const imp = await route("import-post"); const before = await corpus.listJobIds(); const notPost = await callPost(imp, { slug: "test-youtube", url: "https://video.example/v/1" }); assert.equal(notPost.status, 400); assert.match(String(notPost.body.error), /not a post URL/); const video = await callPost(imp, { slug: "test-youtube", url: "https://x.com/synthbaker/status/1" }); assert.equal(video.status, 400); assert.match(String(video.body.error), /test-youtube is not a posts channel/); const missing = await callPost(imp, { slug: "no-such-channel", url: "https://x.com/synthbaker/status/1" }); assert.equal(missing.status, 400); assert.match(String(missing.body.error), /No such channel/); const noUrl = await callPost(imp, { slug: "test-youtube" }); assert.equal(noUrl.status, 400); assert.deepEqual(await corpus.listJobIds(), before, "nothing was queued"); }); // Release 29 E2: archive-search's YouTube-id mode, wayback-lookup and find-copy // — refused at the door before anything reaches archive.org. test("archive-search: youtubeId or query, not both; an id is an id", async () => { const search = await route("archive-search"); const both = await callPost(search, { query: "x", youtubeId: "dQw4w9WgXcQ" }); assert.equal(both.status, 400); assert.match(String(both.body.error), /either "query" or "youtubeId"/); const bad = await callPost(search, { youtubeId: "not an id" }); assert.equal(bad.status, 400); assert.match(String(bad.body.error), /is not a YouTube id/); }); test("wayback-lookup: a URL or a record, not both; an http(s) URL; a record with no known page is a 404", async () => { const lookup = await route("wayback-lookup"); const none = await callPost(lookup, {}); assert.equal(none.status, 400); assert.match(String(none.body.error), /"url" \(a page\) or "slug" and "id"/); const both = await callPost(lookup, { url: "https://a.example/x", slug: "test-youtube", id: "x" }); assert.equal(both.status, 400); const ftp = await callPost(lookup, { url: "ftp://a.example/x" }); assert.equal(ftp.status, 400); assert.match(String(ftp.body.error), /http\(s\) URL/); const big = await callPost(lookup, { url: "https://a.example/x", limit: 501 }); assert.equal(big.status, 400); // A record of no channel: no page to look up (a YouTube channel's record would // be given its watch URL from its id, and asked of the real Wayback — never // in a test). const unknown = await callPost(lookup, { slug: "no-such-channel", id: "no-such-video" }); assert.equal(unknown.status, 404); assert.match(String(unknown.body.error), /no page is known for no-such-channel\/no-such-video/); }); test("find-copy: exactly one of article, articles, siteId; an unknown one is refused before any lookup", async () => { const find = await route("find-copy"); const before = await corpus.listJobIds(); assert.equal((await callPost(find, {})).status, 400); const two = await callPost(find, { article: "a", siteId: "b" }); assert.equal(two.status, 400); assert.match(String(two.body.error), /send one of "article"/); const noArticle = await callPost(find, { article: "no-such-article", dryRun: true }); assert.equal(noArticle.status, 400); assert.match(String(noArticle.body.error), /no article "no-such-article"/); const badRetry = await callPost(find, { article: "a", retryFailed: "yes" }); assert.equal(badRetry.status, 400); assert.match(String(badRetry.body.error), /"retryFailed" must be a boolean/); const noSite = await callPost(find, { siteId: "no-such-site" }); assert.equal(noSite.status, 400); assert.match(String(noSite.body.error), /No site "no-such-site"/); assert.deepEqual(await corpus.listJobIds(), before, "nothing was queued"); }); // Release 31 (lane C): find-copy's scope mode — `{slug | siteId, gone}` — // refused at the door before any lookup. test("find-copy scope mode: a known gone scope, one of slug or siteId, max at most 200, an existing channel", async () => { const find = await route("find-copy"); const before = await corpus.listJobIds(); const badGone = await callPost(find, { slug: "test-youtube", gone: "some" }); assert.equal(badGone.status, 400); assert.match(String(badGone.body.error), /"gone" must be one of no-transcript, all/); const both = await callPost(find, { slug: "test-youtube", siteId: "x", gone: "all" }); assert.equal(both.status, 400); assert.match(String(both.body.error), /with "gone", send one of "slug"/); const withArticle = await callPost(find, { article: "a", slug: "test-youtube", gone: "all" }); assert.equal(withArticle.status, 400); const big = await callPost(find, { slug: "test-youtube", gone: "all", max: 201 }); assert.equal(big.status, 400); assert.match(String(big.body.error), /"max" must be at most 200/); const zero = await callPost(find, { slug: "test-youtube", gone: "all", max: 0 }); assert.equal(zero.status, 400); const noChannel = await callPost(find, { slug: "no-such-channel", gone: "no-transcript", dryRun: true }); assert.equal(noChannel.status, 400); assert.match(String(noChannel.body.error), /No channel "no-such-channel"/); const noSite = await callPost(find, { siteId: "no-such-site", gone: "no-transcript" }); assert.equal(noSite.status, 400); assert.match(String(noSite.body.error), /No site "no-such-site"/); const slugAlone = await callPost(find, { slug: "test-youtube" }); assert.equal(slugAlone.status, 400); assert.match(String(slugAlone.body.error), /"slug" and "max" go with "gone"/); assert.deepEqual(await corpus.listJobIds(), before, "nothing was queued"); });