|
| 1 | +import { describe, it, expect, beforeEach, afterEach } from "vitest" |
| 2 | +import fs from "node:fs" |
| 3 | +import path from "node:path" |
| 4 | +import os from "node:os" |
| 5 | +import { Hono } from "hono" |
| 6 | +import { createBookStorage } from "@adt/storage" |
| 7 | +import { errorHandler } from "../middleware/error-handler.js" |
| 8 | +import { createPageRoutes } from "./pages.js" |
| 9 | + |
| 10 | +describe("Page routes", () => { |
| 11 | + let tmpDir: string |
| 12 | + let app: Hono |
| 13 | + const label = "test-book" |
| 14 | + |
| 15 | + beforeEach(() => { |
| 16 | + tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "pages-routes-")) |
| 17 | + |
| 18 | + // Create a book with extracted pages and pipeline data |
| 19 | + const storage = createBookStorage(label, tmpDir) |
| 20 | + try { |
| 21 | + // Simulate extracted pages |
| 22 | + const fakeImage = { |
| 23 | + imageId: `${label}_p1_page`, |
| 24 | + pngBuffer: Buffer.from("fake-png-data"), |
| 25 | + hash: "abc123", |
| 26 | + width: 800, |
| 27 | + height: 600, |
| 28 | + } |
| 29 | + storage.putExtractedPage({ |
| 30 | + pageId: `${label}_p1`, |
| 31 | + pageNumber: 1, |
| 32 | + text: "Page one text content", |
| 33 | + pageImage: fakeImage, |
| 34 | + images: [], |
| 35 | + }) |
| 36 | + |
| 37 | + const fakeImage2 = { |
| 38 | + imageId: `${label}_p2_page`, |
| 39 | + pngBuffer: Buffer.from("fake-png-data-2"), |
| 40 | + hash: "def456", |
| 41 | + width: 800, |
| 42 | + height: 600, |
| 43 | + } |
| 44 | + storage.putExtractedPage({ |
| 45 | + pageId: `${label}_p2`, |
| 46 | + pageNumber: 2, |
| 47 | + text: "Page two text content", |
| 48 | + pageImage: fakeImage2, |
| 49 | + images: [], |
| 50 | + }) |
| 51 | + |
| 52 | + // Simulate pipeline output for page 1 |
| 53 | + storage.putNodeData("text-classification", `${label}_p1`, { |
| 54 | + reasoning: "test reasoning", |
| 55 | + groups: [ |
| 56 | + { |
| 57 | + groupId: "g1", |
| 58 | + groupType: "body", |
| 59 | + texts: [ |
| 60 | + { textType: "paragraph", text: "Hello world", isPruned: false }, |
| 61 | + ], |
| 62 | + }, |
| 63 | + ], |
| 64 | + }) |
| 65 | + storage.putNodeData("image-classification", `${label}_p1`, { |
| 66 | + images: [], |
| 67 | + }) |
| 68 | + storage.putNodeData("page-sectioning", `${label}_p1`, { |
| 69 | + reasoning: "sectioned", |
| 70 | + sections: [ |
| 71 | + { |
| 72 | + sectionType: "content", |
| 73 | + partIds: ["g1"], |
| 74 | + backgroundColor: "#ffffff", |
| 75 | + textColor: "#000000", |
| 76 | + pageNumber: 1, |
| 77 | + isPruned: false, |
| 78 | + }, |
| 79 | + ], |
| 80 | + }) |
| 81 | + storage.putNodeData("web-rendering", `${label}_p1`, { |
| 82 | + sections: [ |
| 83 | + { |
| 84 | + sectionIndex: 0, |
| 85 | + sectionType: "content", |
| 86 | + reasoning: "rendered", |
| 87 | + html: "<div>Hello world</div>", |
| 88 | + }, |
| 89 | + ], |
| 90 | + }) |
| 91 | + } finally { |
| 92 | + storage.close() |
| 93 | + } |
| 94 | + |
| 95 | + const routes = createPageRoutes(tmpDir) |
| 96 | + app = new Hono() |
| 97 | + app.onError(errorHandler) |
| 98 | + app.route("/api", routes) |
| 99 | + }) |
| 100 | + |
| 101 | + afterEach(() => { |
| 102 | + fs.rmSync(tmpDir, { recursive: true, force: true }) |
| 103 | + }) |
| 104 | + |
| 105 | + describe("GET /api/books/:label/pages", () => { |
| 106 | + it("returns list of pages", async () => { |
| 107 | + const res = await app.request(`/api/books/${label}/pages`) |
| 108 | + |
| 109 | + expect(res.status).toBe(200) |
| 110 | + const body = await res.json() |
| 111 | + expect(body).toHaveLength(2) |
| 112 | + expect(body[0].pageId).toBe(`${label}_p1`) |
| 113 | + expect(body[0].pageNumber).toBe(1) |
| 114 | + expect(body[0].hasRendering).toBe(true) |
| 115 | + expect(body[1].pageId).toBe(`${label}_p2`) |
| 116 | + expect(body[1].pageNumber).toBe(2) |
| 117 | + expect(body[1].hasRendering).toBe(false) |
| 118 | + }) |
| 119 | + |
| 120 | + it("returns 404 for nonexistent book", async () => { |
| 121 | + const res = await app.request("/api/books/no-such-book/pages") |
| 122 | + expect(res.status).toBe(404) |
| 123 | + }) |
| 124 | + }) |
| 125 | + |
| 126 | + describe("GET /api/books/:label/pages/:pageId", () => { |
| 127 | + it("returns full page data with pipeline outputs", async () => { |
| 128 | + const res = await app.request( |
| 129 | + `/api/books/${label}/pages/${label}_p1` |
| 130 | + ) |
| 131 | + |
| 132 | + expect(res.status).toBe(200) |
| 133 | + const body = await res.json() |
| 134 | + expect(body.pageId).toBe(`${label}_p1`) |
| 135 | + expect(body.pageNumber).toBe(1) |
| 136 | + expect(body.text).toBe("Page one text content") |
| 137 | + expect(body.textClassification).toBeTruthy() |
| 138 | + expect(body.textClassification.groups).toHaveLength(1) |
| 139 | + expect(body.imagClassification).toBeFalsy // typo check |
| 140 | + expect(body.imageClassification).toBeTruthy() |
| 141 | + expect(body.sectioning).toBeTruthy() |
| 142 | + expect(body.sectioning.sections).toHaveLength(1) |
| 143 | + expect(body.rendering).toBeTruthy() |
| 144 | + expect(body.rendering.sections[0].html).toBe( |
| 145 | + "<div>Hello world</div>" |
| 146 | + ) |
| 147 | + }) |
| 148 | + |
| 149 | + it("returns page without pipeline data if not processed", async () => { |
| 150 | + const res = await app.request( |
| 151 | + `/api/books/${label}/pages/${label}_p2` |
| 152 | + ) |
| 153 | + |
| 154 | + expect(res.status).toBe(200) |
| 155 | + const body = await res.json() |
| 156 | + expect(body.pageId).toBe(`${label}_p2`) |
| 157 | + expect(body.textClassification).toBeNull() |
| 158 | + expect(body.rendering).toBeNull() |
| 159 | + }) |
| 160 | + |
| 161 | + it("returns 404 for nonexistent page", async () => { |
| 162 | + const res = await app.request( |
| 163 | + `/api/books/${label}/pages/fake-page` |
| 164 | + ) |
| 165 | + expect(res.status).toBe(404) |
| 166 | + }) |
| 167 | + }) |
| 168 | + |
| 169 | + describe("GET /api/books/:label/pages/:pageId/image", () => { |
| 170 | + it("returns page image as base64 JSON", async () => { |
| 171 | + const res = await app.request( |
| 172 | + `/api/books/${label}/pages/${label}_p1/image` |
| 173 | + ) |
| 174 | + |
| 175 | + expect(res.status).toBe(200) |
| 176 | + const body = await res.json() |
| 177 | + expect(body.imageBase64).toBeTruthy() |
| 178 | + expect(typeof body.imageBase64).toBe("string") |
| 179 | + }) |
| 180 | + |
| 181 | + it("returns 404 for nonexistent page image", async () => { |
| 182 | + const res = await app.request( |
| 183 | + `/api/books/${label}/pages/fake-page/image` |
| 184 | + ) |
| 185 | + expect(res.status).toBe(404) |
| 186 | + }) |
| 187 | + }) |
| 188 | +}) |
0 commit comments