import { assertEquals, assertStringIncludes } from "@std/assert"; import { EpubBuilder, inspectEpub } from "./epub_builder.ts"; Deno.test("generates EPUB 3 with a linked table of contents and spine", async () => { const root = await Deno.makeTempDir(); const publication = `${root}/publication`; const templates = `${root}/templates`; const output = `${root}/book.epub`; await Deno.mkdir(`${publication}/1::专栏`, { recursive: true }); await Deno.mkdir(templates, { recursive: true }); await Deno.writeTextFile( `${publication}/1::专栏/1::第一篇.md`, "正文甲", ); await Deno.writeTextFile( `${publication}/1::专栏/2::第二篇.md`, "# 第二篇\n\n第二段\n\n![封面](assets/pixel.png)\n\n[转到第一篇](1::第一篇.md)", ); await Deno.mkdir(`${publication}/1::专栏/assets`, { recursive: true }); await Deno.writeFile( `${publication}/1::专栏/assets/pixel.png`, new Uint8Array([1, 2, 3]), ); const builder = new EpubBuilder({ journal: { build_mode: "detached", from_directory: publication, template_directory: templates, identifier: "urn:test:book", }, epub: { path: output, title: "测试书", author: "作者甲" }, }).with_files_sync(); try { await builder.build(); const archiveBytes = await Deno.readFile(output); const zip = inspectEpub(archiveBytes); const mimetype = new TextDecoder().decode(zip.mimetype); assertEquals(mimetype, "application/epub+zip"); const archiveView = new DataView( archiveBytes.buffer, archiveBytes.byteOffset, archiveBytes.byteLength, ); // Local-file-header fields: compression method is the uint16 at offset 8. assertEquals(archiveView.getUint16(8, true), 0); assertEquals( new TextDecoder().decode(archiveBytes.slice(30, 38)), "mimetype", ); const nav = new TextDecoder().decode(zip["EPUB/nav.xhtml"]); assertStringIncludes(nav, 'epub:type="toc"'); assertStringIncludes(nav, "text/0001-第一篇.xhtml"); assertStringIncludes(nav, "text/0002-第二篇.xhtml"); const opf = new TextDecoder().decode(zip["EPUB/content.opf"]); assertStringIncludes(opf, ""); assertStringIncludes(opf, ''); assertStringIncludes( new TextDecoder().decode(zip["EPUB/text/0001-第一篇.xhtml"]), "正文甲", ); const secondChapter = new TextDecoder().decode( zip["EPUB/text/0002-第二篇.xhtml"], ); assertEquals((secondChapter.match(/"); assertStringIncludes(secondChapter, "../media/2/1.png"); assertStringIncludes(secondChapter, "0001-第一篇.xhtml"); assertEquals(zip["EPUB/media/2/1.png"].length, 3); } finally { await Deno.remove(root, { recursive: true }); } }); Deno.test("uses EPUB output path or a stable default", () => { const builder = new EpubBuilder({ journal: { from_directory: "publication", template_directory: "templates" }, }); assertEquals(builder.outputPath(), "./outputs/book.epub"); });