// The Google Docs extractor over SYNTHETIC captures (__fixtures__/gdoc-*): // mobilebasic, pub, and the /edit shell. // // Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test lib/waybackPages/extractors/gdoc.test.ts import { test } from "node:test"; import assert from "node:assert/strict"; import { extractGdoc, gdocExtractor, googleDocId, mobilebasicUrl } from "./gdoc"; import { captureOf, fixture } from "./__fixtures__/capture"; const ID = "1AbCdEfGhIjKlMnOpQrStUvWxYz0123456789"; const run = (file: string, url: string) => extractGdoc({ html: fixture(file), capture: captureOf(url, "20200101000000") }); test("ids: /d//, the old pub?id=, and the published-to-web /d/e//", () => { assert.deepEqual(googleDocId(new URL(`https://docs.google.com/document/d/${ID}/edit?usp=sharing`)), { id: ID }); assert.deepEqual(googleDocId(new URL(`https://docs.google.com/document/pub?id=${ID}`)), { id: ID }); assert.deepEqual(googleDocId(new URL("https://docs.google.com/document/d/e/2PACX-1vTeapot/pub?embedded=true")), { id: "2PACX-1vTeapot", published: true }); assert.deepEqual(googleDocId(new URL("https://docs.google.com/document/u/0/")), {}); assert.equal(mobilebasicUrl(ID), `https://docs.google.com/document/d/${ID}/mobilebasic`); }); test("mobilebasic: the title, headings and list items as lines, links unwrapped from Google's redirector", () => { const drafts = run("gdoc-mobilebasic.html", `https://docs.google.com/document/d/${ID}/mobilebasic`); assert.deepEqual(drafts, [ { kind: "gdoc", title: "Teapot Society Minutes", text: [ "Teapot Society Minutes", "Attendance", "Five members came. One sent apologies.", "Decisions", "- Buy a new kettle.", "- Meet again in June.", "Minutes are kept at the society site (https://teapot-society.example/minutes).", ].join("\n"), links: ["https://teapot-society.example/minutes"], }, ]); }); test("pub (embedded): the document's body without Google's banner, header and footer", () => { const [d] = run("gdoc-pub.html", "https://docs.google.com/document/d/e/2PACX-1vTeapot/pub?embedded=true"); assert.equal(d.kind, "gdoc"); assert.equal(d.title, "Teapot Society Minutes"); assert.equal(d.text, "Attendance\nFive members came. One sent apologies.\nDecisions\n- Buy a new kettle.\n- Meet again in June.\n[image]"); assert.deepEqual(d.links, []); assert.deepEqual(d.media, [{ kind: "image", url: "https://lh3.googleusercontent.com/teapot-society-photo" }]); assert.equal(d.noContent, undefined); assert.ok(!/Report abuse|Updated automatically|Published using/.test(d.text)); }); test("the /edit shell: noContent, the text naming the mobilebasic URL to look for", () => { const drafts = run("gdoc-edit-shell.html", `https://docs.google.com/document/d/${ID}/edit`); assert.deepEqual(drafts, [ { kind: "gdoc", title: "Teapot Society Minutes", text: `This capture holds only the Google Docs editor, not the document's text. Look for a capture of https://docs.google.com/document/d/${ID}/mobilebasic`, links: [`https://docs.google.com/document/d/${ID}/mobilebasic`], noContent: true, }, ]); // A published-to-web id has no mobilebasic form to suggest. const pub = extractGdoc({ html: fixture("gdoc-edit-shell.html"), capture: captureOf("https://docs.google.com/document/d/e/2PACX-1vTeapot/pub", "20200101000000") }); assert.equal(pub[0].text, "This capture holds no document text."); assert.deepEqual(pub[0].links, []); }); test("matches: docs.google.com documents only", () => { const m = (u: string) => gdocExtractor.matches(new URL(u), ""); assert.equal(m(`https://docs.google.com/document/d/${ID}/preview`), true); assert.equal(m(`https://docs.google.com/spreadsheets/d/${ID}/edit`), false); assert.equal(m("https://drive.google.com/file/d/x/view"), false); assert.equal(gdocExtractor.name, "gdoc"); assert.equal(gdocExtractor.version, 1); });