|
| 1 | +import { describe, it, expect, beforeEach, afterEach } from "vitest" |
| 2 | +import fs from "node:fs" |
| 3 | +import os from "node:os" |
| 4 | +import path from "node:path" |
| 5 | +import { Hono } from "hono" |
| 6 | +import { createBookStorage } from "@adt/storage" |
| 7 | +import { errorHandler } from "../middleware/error-handler.js" |
| 8 | +import { createPackageRoutes } from "./package.js" |
| 9 | + |
| 10 | +describe("Package routes", () => { |
| 11 | + let tmpDir: string |
| 12 | + let webAssetsDir: string |
| 13 | + let app: Hono |
| 14 | + |
| 15 | + beforeEach(() => { |
| 16 | + tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "package-routes-")) |
| 17 | + webAssetsDir = path.join(tmpDir, "web-assets") |
| 18 | + fs.mkdirSync(webAssetsDir, { recursive: true }) |
| 19 | + |
| 20 | + app = new Hono() |
| 21 | + app.onError(errorHandler) |
| 22 | + app.route("/api", createPackageRoutes(tmpDir, webAssetsDir)) |
| 23 | + }) |
| 24 | + |
| 25 | + afterEach(() => { |
| 26 | + fs.rmSync(tmpDir, { recursive: true, force: true }) |
| 27 | + }) |
| 28 | + |
| 29 | + function createTestBook(label: string): void { |
| 30 | + const storage = createBookStorage(label, tmpDir) |
| 31 | + storage.close() |
| 32 | + } |
| 33 | + |
| 34 | + describe("POST /api/books/:label/package-adt", () => { |
| 35 | + it("returns 404 for missing book", async () => { |
| 36 | + const res = await app.request("/api/books/missing/package-adt", { |
| 37 | + method: "POST", |
| 38 | + }) |
| 39 | + expect(res.status).toBe(404) |
| 40 | + }) |
| 41 | + |
| 42 | + it("returns 409 when master is not completed", async () => { |
| 43 | + createTestBook("book1") |
| 44 | + |
| 45 | + const res = await app.request("/api/books/book1/package-adt", { |
| 46 | + method: "POST", |
| 47 | + }) |
| 48 | + |
| 49 | + expect(res.status).toBe(409) |
| 50 | + const body = await res.json() |
| 51 | + expect(body.error).toContain("Master phase must be completed") |
| 52 | + }) |
| 53 | + }) |
| 54 | + |
| 55 | + describe("GET /api/books/:label/package-adt/status", () => { |
| 56 | + it("returns hasAdt=false when pages.json is missing", async () => { |
| 57 | + createTestBook("book2") |
| 58 | + |
| 59 | + const res = await app.request("/api/books/book2/package-adt/status") |
| 60 | + expect(res.status).toBe(200) |
| 61 | + expect(await res.json()).toEqual({ label: "book2", hasAdt: false }) |
| 62 | + }) |
| 63 | + |
| 64 | + it("returns hasAdt=false when pages.json is invalid JSON", async () => { |
| 65 | + createTestBook("book3") |
| 66 | + const pagesPath = path.join(tmpDir, "book3", "adt", "content", "pages.json") |
| 67 | + fs.mkdirSync(path.dirname(pagesPath), { recursive: true }) |
| 68 | + fs.writeFileSync(pagesPath, "{not-json") |
| 69 | + |
| 70 | + const res = await app.request("/api/books/book3/package-adt/status") |
| 71 | + expect(res.status).toBe(200) |
| 72 | + expect(await res.json()).toEqual({ label: "book3", hasAdt: false }) |
| 73 | + }) |
| 74 | + |
| 75 | + it("returns hasAdt=false when pages.json has no href entries", async () => { |
| 76 | + createTestBook("book4") |
| 77 | + const pagesPath = path.join(tmpDir, "book4", "adt", "content", "pages.json") |
| 78 | + fs.mkdirSync(path.dirname(pagesPath), { recursive: true }) |
| 79 | + fs.writeFileSync(pagesPath, JSON.stringify([{ section_id: "pg001" }])) |
| 80 | + |
| 81 | + const res = await app.request("/api/books/book4/package-adt/status") |
| 82 | + expect(res.status).toBe(200) |
| 83 | + expect(await res.json()).toEqual({ label: "book4", hasAdt: false }) |
| 84 | + }) |
| 85 | + |
| 86 | + it("returns hasAdt=true when pages.json has at least one href entry", async () => { |
| 87 | + createTestBook("book5") |
| 88 | + const pagesPath = path.join(tmpDir, "book5", "adt", "content", "pages.json") |
| 89 | + fs.mkdirSync(path.dirname(pagesPath), { recursive: true }) |
| 90 | + fs.writeFileSync( |
| 91 | + pagesPath, |
| 92 | + JSON.stringify([{ section_id: "pg001", href: "pg001.html" }]), |
| 93 | + ) |
| 94 | + |
| 95 | + const res = await app.request("/api/books/book5/package-adt/status") |
| 96 | + expect(res.status).toBe(200) |
| 97 | + expect(await res.json()).toEqual({ label: "book5", hasAdt: true }) |
| 98 | + }) |
| 99 | + }) |
| 100 | +}) |
0 commit comments