import { expect, test } from "bun:test"; import { mkdtempSync, rmSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { crc32, deflateRawSync } from "node:zlib"; import "./chat/epub-reader.js"; function zip(files) { const local = [], central = []; let offset = 0; for (const [path, contents, method = 8, declaredSize] of files) { const name = Buffer.from(path), body = Buffer.from(contents); const data = method === 8 ? deflateRawSync(body) : body; const l = Buffer.alloc(30), c = Buffer.alloc(46); l.writeUInt32LE(0x04034b50); c.writeUInt32LE(0x02014b50); for (const [header, shift] of [[l, 0], [c, 2]]) { header.writeUInt16LE(20, 4 + shift); header.writeUInt16LE(method, 8 + shift); header.writeUInt32LE(crc32(body), 14 + shift); header.writeUInt32LE(data.length, 18 + shift); header.writeUInt32LE(declaredSize ?? body.length, 22 + shift); header.writeUInt16LE(name.length, 26 + shift); } c.writeUInt32LE(offset, 42); local.push(l, name, data); central.push(c, name); offset += l.length + name.length + data.length; } const index = Buffer.concat(central), end = Buffer.alloc(22); end.writeUInt32LE(0x06054b50); end.writeUInt16LE(files.length, 8); end.writeUInt16LE(files.length, 10); end.writeUInt32LE(index.length, 12); end.writeUInt32LE(offset, 16); return "data:application/epub+zip;base64," + Buffer.concat([...local, index, end]).toString("base64"); } function book(chapters, extras = []) { const manifest = chapters.map(([href, , type = "application/xhtml+xml"], i) => ``).join(""); const spine = chapters.map((_, i) => ``).join(""); return zip([ ["mimetype", "application/epub+zip", 0], ["META-INF/container.xml", ''], ["OPS/package/book.opf", `${manifest}${spine}`], ...[...chapters].reverse().filter(([, source]) => source != null).map(([href, source, , method]) => [decodeURIComponent(new URL(href, "https://archive/OPS/package/book.opf").pathname).slice(1), source, method]), ...extras ]); } test("EPUB recognition, malformed ZIPs and false uncompressed sizes", async () => { const { isEpub, read } = globalThis.MagpieEpub; expect(isEpub({ name: "文章.EPUB", type: "application/octet-stream" })).toBe(true); expect(isEpub({ type: "application/epub+zip" })).toBe(true); expect(isEpub({ name: "文章.pdf" })).toBe(false); await expect(read("data:;base64,UEs=")).rejects.toThrow("损坏"); await expect(read("data:;base64," + "A".repeat(4 * Math.ceil((8 * 1024 * 1024) / 3) + 4))).rejects.toThrow("8 MiB"); const stored = zip([["META-INF/container.xml", "original", 0]]); const bytes = Buffer.from(stored.split(",")[1], "base64"); bytes[bytes.indexOf("original")] ^= 1; await expect(read("data:;base64," + bytes.toString("base64"))).rejects.toThrow("损坏"); await expect(read(zip([["META-INF/container.xml", "x".repeat(50000), 8, 1]]))).rejects.toThrow("损坏"); await expect(read(zip([["META-INF/container.xml", "x", 8, 25 * 1024 * 1024]]))).rejects.toThrow("太大"); }); // CI uses its installed browser; EPUB_TEST_BROWSER can select another Chromium executable. const browserPath = process.env.EPUB_TEST_BROWSER || ["google-chrome", "google-chrome-stable", "chromium", "chromium-browser"] .map(name => Bun.which(name)).find(Boolean); test.skipIf(!browserPath && !process.env.CI)("reads complete books in spine order without loading external resources", async () => { if (!browserPath) throw new Error("Browser tests need Chrome or Chromium in PATH, or EPUB_TEST_BROWSER."); const xhtml = text => `${text}`; const first = "../text/第%20一.xhtml#start", second = "../text/2.html"; const fixtures = { mixed: book([ [first, xhtml('

红楼梦𠀀。

'), undefined, 0], [second, '书名不该进入正文

次章。

有 空格
下一行', "text/html"] ], [["META-INF/encryption.xml", '']]), duplicates: book([[first, xhtml("

同文

")], ["../text/./第%20一.xhtml#end", null], ["../text/second.xhtml", xhtml("

同文

")]]), hybrid: book([ ["../cover.xhtml", xhtml('')], [first, xhtml("

可读正文

")], ["../text/scanned.html", '', "text/html"], ["../text/scanned.html#bottom", null, "text/html"], ["../text/blank.xhtml", xhtml("

")] ]), legacy: book([[first, '

古 籍—𠀀…完

']]), long: book([[first, xhtml("

" + "文".repeat(100000) + "末

")]]), missing: book([[first, xhtml("

第一页

")], [second, null]]), locked: book([[first, xhtml("

加密章节

")]], [["META-INF/encryption.xml", '']]), invalid: book([[first, "

坏了"]]), unterminated: book([[first, "" + "