返回 CodeWhale
docs-search-delivery.test.ts
根目录 / web / lib / docs-search-delivery.test.ts
1 import { describe, expect, it, vi } from "vitest";
2 import { GET } from "../app/api/docs/search/[locale]/route";
3 import * as index from "./docs-article-search";
4 import type { DocSearchSection } from "./doc-search-match";
5
6 const context = (locale: string) => ({ params: Promise.resolve({ locale }) });
7
8 describe("locale-only documentation search delivery", () => {
9 it("delivers the complete source index on demand and revalidates it without a body", async () => {
10 const request = new Request("http://localhost/api/docs/search/en");
11 const response = await GET(request, context("en"));
12 expect(response.status).toBe(200);
13 expect(response.headers.get("cache-control")).toContain("max-age=300");
14 const etag = response.headers.get("etag");
15 expect(etag).toMatch(/^W\/"[0-9a-f]{64}"$/);
16 const payload = await response.json() as { sections: DocSearchSection[] };
17 for (const path of ["/install#", "/models#", "/plugins#", "/constitution#", "/docs/runtime-reference#", "/docs/workflow-tutorial#", "/docs/plugin-authoring#"]) {
18 expect(payload.sections.some(section => section.href.startsWith(path)), path).toBe(true);
19 }
20 const unchanged = await GET(new Request(request.url, { headers: { "if-none-match": etag! } }), context("en"));
21 expect(unchanged.status).toBe(304);
22 expect(await unchanged.text()).toBe("");
23 const withQuery = await GET(new Request(`${request.url}?q=private-needle-597cfed`), context("en"));
24 expect(withQuery.headers.get("etag")).toBe(etag);
25 expect(await withQuery.text()).not.toContain("private-needle-597cfed");
26 });
27
28 it("rejects unknown locales and fails explicitly when the source cannot build", async () => {
29 const unknown = await GET(new Request("http://localhost/api/docs/search/unknown"), context("unknown"));
30 expect(unknown.status).toBe(404);
31 expect(unknown.headers.get("cache-control")).toBe("no-store");
32 const spy = vi.spyOn(index, "getDocSearchSections").mockImplementationOnce(() => { throw new Error("private source detail"); });
33 try {
34 const failed = await GET(new Request("http://localhost/api/docs/search/ja"), context("ja"));
35 expect(failed.status).toBe(503);
36 expect(await failed.json()).toEqual({ error: "unavailable" });
37 expect(failed.headers.get("cache-control")).toBe("no-store");
38 expect((await GET(new Request("http://localhost/api/docs/search/ja"), context("ja"))).status).toBe(200);
39 } finally { spy.mockRestore(); }
40 });
41 });
42
42 lines TYPESCRIPT