import test from "node:test"; import assert from "node:assert/strict"; import { mkdir, mkdtemp, writeFile } from "fs/promises"; import { tmpdir } from "os"; import { dirname, join } from "path"; import { getPageFiles, isNotebook, listRmContents } from "../src/utils/zip"; async function createExtractedDir(files: string[]): Promise { const dir = await mkdtemp(join(tmpdir(), "remarkable-zip-test-")); for (const file of files) { const filePath = join(dir, ...file.split("/")); await mkdir(dirname(filePath), { recursive: true }); await writeFile(filePath, ""); } return dir; } test("listRmContents recursively lists files without shelling out to find", async () => { const dir = await createExtractedDir(["content.json", "pages/2.rm", "pages/nested/1.rm"]); const contents = (await listRmContents(dir)).map((path) => path.replace(`${dir}/`, "")).sort(); assert.deepEqual(contents, ["content.json", "pages/2.rm", "pages/nested/1.rm"]); }); test("getPageFiles returns sorted nested page files", async () => { const dir = await createExtractedDir(["pages/2.rm", "pages/1.rm", "pages/ignored.zip"]); const pages = (await getPageFiles(dir)).map((path) => path.replace(`${dir}/`, "")); assert.deepEqual(pages, ["pages/1.rm", "pages/2.rm"]); }); test("isNotebook treats annotated PDFs as non-notebooks", async () => { const dir = await createExtractedDir(["pages/1.rm", "content.pdf"]); assert.equal(await isNotebook(dir), false); }); test("isNotebook accepts extracted notebooks with rm pages and no PDF", async () => { const dir = await createExtractedDir(["pages/1.rm", "content.json"]); assert.equal(await isNotebook(dir), true); });