Files
plugins/packages/chat/epub-reader.test.mjs
T
ninon 63cd0e4bf9 chat: send an EPUB's text with chat messages (#621, ninondev)
Chat (page-chat 0.1.8 -> 0.1.9) can send an EPUB's text with a message. The book is read in the page (chat/epub-reader.js: its own ZIP reader with size, CRC and encryption checks, DecompressionStream inflate, chapters parsed inert in spine order, no external resource loaded, no script run), never uploaded. Each request to the gateway inlines the text once in the user message that carried it, between 【电子书:name】 and 【电子书结束】; the same book again is a note, a failed read is a note and a toast. Send waits while a book is read, and switching chats drops a book still being read. Image-only user messages now stay in history.

Run at 1cb1ecb400fe31bbc1f58463af09af757374d27e:
- CI (pr.yml, ubuntu): HOME=$(mktemp -d) bun test and bun scripts/check.mjs green; the spawned-Chrome reader test ran and passed there (10.5s).
- mote: packages/chat bun test 34 pass; the reader test's own Chrome spawn gets no page load over ssh on macOS (and in WSL), so its fixtures were run again through Playwright 1.62 Chromium and WebKit: 2 pass, 21 expects, each engine.
- End to end on mote, Chromium and WebKit, 420px: startServer with a sandbox config and HOME, a fake gateway, the real page; attached Project Gutenberg's Alice (132,120 chars) and 紅樓夢 (876,576 chars) EPUBs, sent, then followed up. Each gateway request carried the book once, before the question, with no base64 or EPUB data; the follow-up carried user,assistant,user with the book once. The page made no request outside its own service.

Not changed: the MCP tool pick still scores the last message, which on a book's first message is the whole book (filed as #623).
2026-11-02 16:01:29 +08:00

141 lines
8.4 KiB
JavaScript

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) =>
`<item id="c${i}" href="${href}" media-type="${type}"/>`).join("");
const spine = chapters.map((_, i) => `<itemref idref="c${i}"/>`).join("");
return zip([
["mimetype", "application/epub+zip", 0],
["META-INF/container.xml", '<container><rootfiles><rootfile full-path="OPS/package/book.opf"/></rootfiles></container>'],
["OPS/package/book.opf", `<package><manifest>${manifest}</manifest><spine>${spine}</spine></package>`],
...[...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 => `<html xmlns="http://www.w3.org/1999/xhtml"><body>${text}</body></html>`;
const first = "../text/第%20一.xhtml#start", second = "../text/2.html";
const fixtures = {
mixed: book([
[first, xhtml('<p>红<b>楼</b>梦𠀀。</p><img src="/unexpected"/><script>window.injected = true</script><style>隐藏</style>'), undefined, 0],
[second, '<html><head><title>书名不该进入正文</title></head><body><p>次<b>章</b>。<p>有 空格<br>下一行<img src="/unexpected"><iframe src="/unexpected"></iframe></body></html>', "text/html"]
], [["META-INF/encryption.xml", '<encryption><CipherReference URI="OPS/font.otf"/></encryption>']]),
duplicates: book([[first, xhtml("<p>同文</p>")], ["../text/./第%20一.xhtml#end", null],
["../text/second.xhtml", xhtml("<p>同文</p>")]]),
hybrid: book([
["../cover.xhtml", xhtml('<svg xmlns="http://www.w3.org/2000/svg"><image href="cover.jpg"/></svg>')],
[first, xhtml("<p>可读正文</p>")],
["../text/scanned.html", '<img src="/unexpected">', "text/html"],
["../text/scanned.html#bottom", null, "text/html"],
["../text/blank.xhtml", xhtml("<p> </p>")]
]),
legacy: book([[first, '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "/unexpected"><html><body><p>古&nbsp;籍&mdash;𠀀&#133;完</p><p><![CDATA[&nbsp;]]></p></body></html>']]),
long: book([[first, xhtml("<p>" + "文".repeat(100000) + "末</p>")]]),
missing: book([[first, xhtml("<p>第一页</p>")], [second, null]]),
locked: book([[first, xhtml("<p>加密章节</p>")]], [["META-INF/encryption.xml",
'<encryption><CipherReference URI="OPS/text/第%20一.xhtml"/></encryption>']]),
invalid: book([[first, "<html><body><p>坏了</body></html>"]]),
unterminated: book([[first, "<html><body>" + "<!--".repeat(50000) + "&nbsp;"]]),
empty: book([[first, xhtml('<img src="/unexpected"/>')]]),
oversized: book([[first, xhtml("<p>" + "x".repeat(2 * 1024 * 1024 + 1) + "</p>")]])
};
const { promise, resolve } = Promise.withResolvers();
const requested = [];
const server = Bun.serve({ hostname: "127.0.0.1", port: 0, async fetch(req) {
const path = new URL(req.url).pathname;
if (path === "/reader.js") return new Response(Bun.file(join(import.meta.dir, "chat/epub-reader.js")));
if (path === "/result") { resolve(await req.json()); return new Response("ok"); }
if (path === "/") return new Response(`<!DOCTYPE html><meta charset="utf-8"><script src="/reader.js"></script><script>
(async () => {
const result = {};
for (const [name, url] of Object.entries(${JSON.stringify(fixtures)})) {
try {
const { text, characters, imagePages } = await MagpieEpub.read(url);
result[name] = { text: text.length > 1000 ? text.slice(-1) : text, characters, imagePages };
} catch (e) { result[name] = e.message; }
}
result.injected = !!window.injected;
await fetch('/result', {method:'POST', body:JSON.stringify(result)});
})();
</script>`, { headers: { "Content-Type": "text/html" } });
requested.push(path);
return new Response("", { status: 404 });
} });
const profile = mkdtempSync(join(tmpdir(), "magpie-epub-browser-"));
const proc = Bun.spawn([browserPath, "--headless", "--disable-gpu", "--disable-background-networking",
"--no-first-run", "--user-data-dir=" + profile, String(server.url)], { stdout: "ignore", stderr: "ignore" });
try {
const result = await Promise.race([promise, proc.exited.then(() => { throw new Error("browser exited"); }),
new Promise((_, reject) => setTimeout(() => reject(new Error("browser timed out")), 15000).unref())]);
expect(result.mixed).toEqual({ text: "红楼梦𠀀。\n\n次章。\n\n有 空格\n\n下一行", characters: 14, imagePages: 0 });
expect(result.duplicates).toEqual({ text: "同文\n\n同文", characters: 4, imagePages: 0 });
expect(result.hybrid).toEqual({ text: "可读正文", characters: 4, imagePages: 2 });
expect(result.legacy).toEqual({ text: "古 籍—𠀀 完\n\n&nbsp;", characters: 11, imagePages: 0 });
expect(result.long).toEqual({ text: "末", characters: 100001, imagePages: 0 });
expect(result.missing).toContain("损坏");
expect(result.locked).toContain("已加密");
expect(result.invalid).toContain("损坏");
expect(result.unterminated).toContain("损坏");
expect(result.empty).toContain("没有可读取的文字");
expect(result.oversized).toContain("太大");
expect(result.injected).toBe(false);
expect(requested).not.toContain("/unexpected");
} finally {
proc.kill(); await proc.exited; await server.stop(true);
rmSync(profile, { recursive: true, force: true });
}
}, 20000);