// The YouTube About extractor over SYNTHETIC captures (__fixtures__/yt-*): // the 2013 server-rendered About page, a 2019 Polymer About tab, a 2021 // bare channel page and a 2024 @handle About capture whose ytInitialData is // an escaped string. // // Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test lib/waybackPages/extractors/ytAbout.test.ts import { test } from "node:test"; import assert from "node:assert/strict"; import { extractYtAbout, jsonAfter, youtubeChannelOf, ytAboutExtractor } from "./ytAbout"; import { captureOf, fixture } from "./__fixtures__/capture"; const CID = "UCteapot0000000000000001"; const run = (file: string, url: string, ts: string) => extractYtAbout({ html: fixture(file), capture: captureOf(url, ts) }); test("channel URLs: /user, /c, /channel, /@handle, a custom name's /about; not a watch page", () => { const c = (u: string) => youtubeChannelOf(new URL(u)); assert.equal(c("http://www.youtube.com/user/teapotchannel/about"), "teapotchannel"); assert.equal(c("https://www.youtube.com/c/TeapotChannel"), "TeapotChannel"); assert.equal(c(`https://www.youtube.com/channel/${CID}/about`), CID); assert.equal(c("https://www.youtube.com/@TeapotChannel/about"), "@TeapotChannel"); assert.equal(c("https://www.youtube.com/@TeapotChannel"), "@TeapotChannel"); assert.equal(c("https://www.youtube.com/teapotchannel/about"), "teapotchannel"); assert.equal(c("https://m.youtube.com/user/teapotchannel/featured"), "teapotchannel"); assert.equal(c("https://www.youtube.com/watch?v=AbCdEfGhIjK"), null); assert.equal(c("https://www.youtube.com/user/teapotchannel/videos"), null); assert.equal(c("https://www.youtube.com/results/about"), null); assert.equal(c("https://example.com/user/x/about"), null); }); test("jsonAfter: brace-matched past braces inside strings; an escaped string literal; garbage is null", () => { assert.deepEqual(jsonAfter(``, /ytInitialData\s*=\s*/), { a: "}{", b: { c: 1 } }); assert.deepEqual(jsonAfter(`ytInitialData = '\\x7b\\x22a\\x22:1\\x7d';`, /ytInitialData\s*=\s*/), { a: 1 }); assert.equal(jsonAfter(`ytInitialData = {"a": nope};`, /ytInitialData\s*=\s*/), null); assert.equal(jsonAfter(`nothing here`, /ytInitialData\s*=\s*/), null); // A comparison is not the assignment. const page = ``; assert.equal(extractYtAbout({ html: page, capture: captureOf("https://www.youtube.com/@X/about", "20240101000000") })[0].text, "X\nSaid here."); }); test("the 2013 About page: description, links, joined, counts, country", () => { const drafts = run("yt-about-2013.html", "http://www.youtube.com/user/teapotchannel/about", "20131001000000"); assert.deepEqual(drafts, [ { kind: "yt-about", itemId: `yt-about-${CID}-20131001000000`, author: "teapotchannel", authorName: "Teapot Channel", title: "Teapot Channel", text: [ "Teapot Channel", "Videos about teapots, every Sunday.", "Business enquiries through the website.", "Twitter: https://twitter.com/TeapotFan", "Website: http://www.teapot-channel.example/", "Joined Jan 5, 2010", "12,345 subscribers", "1,234,567 views", "Country: United Kingdom", ].join("\n"), links: ["https://twitter.com/TeapotFan", "http://www.teapot-channel.example/"], }, ]); }); test("a 2019 Polymer About tab: read from ytInitialData", () => { const [d] = run("yt-about-2019-polymer.html", `https://www.youtube.com/channel/${CID}/about`, "20190101000000"); assert.equal(d.itemId, `yt-about-${CID}-20190101000000`); assert.equal(d.author, "TeapotChannel"); assert.equal(d.title, "Teapot Channel"); assert.equal( d.text, [ "Teapot Channel", "Videos about teapots {and lids}, every Sunday.", "Now with kettles.", "Twitter: https://twitter.com/TeapotFan", "Website: https://www.teapot-channel.example/", "Joined Jan 5, 2010", "45K subscribers", "2,345,678 views", "Country: United Kingdom", ].join("\n"), ); assert.deepEqual(d.links, ["https://twitter.com/TeapotFan", "https://www.teapot-channel.example/"]); assert.equal(d.noContent, undefined); }); test("a 2021 bare channel page: the metadata's description and the header's links", () => { const [d] = run("yt-channel-2021.html", "https://www.youtube.com/c/TeapotChannel", "20210101000000"); assert.equal(d.itemId, `yt-about-${CID}-20210101000000`); assert.equal( d.text, "Teapot Channel\nTeapots, kettles and the occasional mug.\nShop: https://www.teapot-channel.example/shop\nTwitter: https://twitter.com/TeapotFan\n61.2K subscribers", ); assert.deepEqual(d.links, ["https://www.teapot-channel.example/shop", "https://twitter.com/TeapotFan"]); }); test("a 2024 @handle About capture: the view model, from an escaped-string ytInitialData", () => { const [d] = run("yt-about-2024-handle.html", "https://www.youtube.com/@TeapotChannel/about", "20240101000000"); assert.equal(d.itemId, `yt-about-${CID}-20240101000000`); assert.equal(d.author, "@TeapotChannel"); assert.equal(d.authorName, "Teapot Channel"); assert.equal( d.text, "Teapot Channel\nTeapots, kettles and the occasional mug.\nSince 2010.\nWebsite: https://teapot-channel.example/\nJoined Jan 5, 2010\n80.1K subscribers\n3,456,789 views\nCountry: United Kingdom", ); }); test("two captures of one channel are two records; a page with nothing on it is noContent", () => { const a = run("yt-about-2019-polymer.html", `https://www.youtube.com/channel/${CID}/about`, "20190101000000")[0]; const b = run("yt-about-2024-handle.html", "https://www.youtube.com/@TeapotChannel/about", "20240101000000")[0]; assert.notEqual(a.itemId, b.itemId); const empty = extractYtAbout({ html: "", capture: captureOf("https://www.youtube.com/@Gone/about", "20250101000000") }); assert.deepEqual(empty, [ { kind: "yt-about", itemId: "yt-about-@Gone-20250101000000", author: "@Gone", text: "This capture holds no channel description.", links: [], noContent: true }, ]); }); test("matches: channel pages only", () => { assert.equal(ytAboutExtractor.matches(new URL("https://www.youtube.com/@TeapotChannel/about"), ""), true); assert.equal(ytAboutExtractor.matches(new URL("https://www.youtube.com/watch?v=AbCdEfGhIjK"), ""), false); assert.equal(ytAboutExtractor.name, "yt-about"); assert.equal(ytAboutExtractor.version, 1); });