Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfiCVCgCVJZq4NDEqZs
85 lines
4.4 KiB
TypeScript
85 lines
4.4 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
|
|
const mocks = vi.hoisted(() => ({ callMobileApi: vi.fn(), getToken: vi.fn() }));
|
|
vi.mock("@/app/lib/server/mobileApi", () => ({ callMobileApi: mocks.callMobileApi }));
|
|
vi.mock("@/app/lib/server/session", () => ({ getToken: mocks.getToken }));
|
|
|
|
import * as pub from "@/app/p/[company]/[slug]/frame/[block]/route";
|
|
import * as preview from "@/app/pages/[id]/preview/frame/[block]/route";
|
|
|
|
// add-custom-html-block: the frame addresses serve only an HTML block, sealed; every public miss looks the same.
|
|
const req = new Request("http://localhost/x");
|
|
const ctx = <T,>(p: T) => ({ params: Promise.resolve(p) });
|
|
const page = (blocks: object[]) => ({ ok: true, data: { company: { code: "INS", name: "INS" }, page: { slug: "hotel", title: "Hotel", blocks } } });
|
|
const BLOCKS = [{ type: "HTML", html: "<h1>Designed</h1>" }, { type: "FORM", heading: "Join" }];
|
|
|
|
beforeEach(() => {
|
|
mocks.callMobileApi.mockReset();
|
|
mocks.getToken.mockReset();
|
|
mocks.getToken.mockResolvedValue("jwt");
|
|
mocks.callMobileApi.mockResolvedValue(page(BLOCKS));
|
|
});
|
|
|
|
describe("public frame", () => {
|
|
it("serves a published page's HTML block sealed", async () => {
|
|
const res = await pub.GET(req, ctx({ company: "INS", slug: "hotel", block: "0" }));
|
|
expect(mocks.callMobileApi).toHaveBeenCalledWith("/api/public/pages/INS/hotel");
|
|
expect(res.status).toBe(200);
|
|
expect(res.headers.get("content-security-policy")).toMatch(/^sandbox allow-scripts/);
|
|
expect(await res.text()).toContain("<h1>Designed</h1>");
|
|
});
|
|
|
|
it("answers every miss with the same empty not-found", async () => {
|
|
// The API answers the published page; a bad index never reaches it.
|
|
for (const block of ["1", "7", "x", "-1", "100"]) {
|
|
const res = await pub.GET(req, ctx({ company: "INS", slug: "hotel", block }));
|
|
expect(res.status, block).toBe(404);
|
|
expect(await res.text()).toBe("");
|
|
}
|
|
mocks.callMobileApi.mockResolvedValueOnce({ ok: false, status: 404 });
|
|
const draft = await pub.GET(req, ctx({ company: "INS", slug: "draft", block: "0" }));
|
|
expect(draft.status).toBe(404);
|
|
expect(await draft.text()).toBe("");
|
|
});
|
|
|
|
it("encodes the address parts", async () => {
|
|
await pub.GET(req, ctx({ company: "I/S", slug: "a b", block: "0" }));
|
|
expect(mocks.callMobileApi).toHaveBeenCalledWith("/api/public/pages/I%2FS/a%20b");
|
|
});
|
|
|
|
it("reports an upstream failure as 502", async () => {
|
|
mocks.callMobileApi.mockResolvedValueOnce({ ok: false, status: 500 });
|
|
expect((await pub.GET(req, ctx({ company: "INS", slug: "hotel", block: "0" }))).status).toBe(502);
|
|
});
|
|
});
|
|
|
|
describe("preview frame", () => {
|
|
it("serves a draft HTML block to staff through the admin API, uncached", async () => {
|
|
mocks.callMobileApi.mockResolvedValueOnce({ ok: true, data: { slug: "hotel", title: "Hotel", blocks: BLOCKS } });
|
|
const res = await preview.GET(req, ctx({ id: "5", block: "0" }));
|
|
expect(mocks.callMobileApi).toHaveBeenCalledWith("/api/admin/pages/5", { token: "jwt" });
|
|
expect(res.status).toBe(200);
|
|
expect(res.headers.get("cache-control")).toBe("no-store");
|
|
expect(res.headers.get("content-security-policy")).toMatch(/^sandbox allow-scripts/);
|
|
});
|
|
|
|
it("serves the unpublished changes' HTML block when the page has a draft", async () => {
|
|
const draft = { slug: "hotel", title: "Hotel", blocks: [{ type: "HTML", html: "<p>draft only</p>" }] };
|
|
mocks.callMobileApi.mockResolvedValueOnce({ ok: true, data: { slug: "hotel", title: "Hotel", blocks: [{ type: "FAQ" }], draft } });
|
|
const res = await preview.GET(req, ctx({ id: "5", block: "0" }));
|
|
expect(res.status).toBe(200);
|
|
expect(await res.text()).toContain("draft only");
|
|
});
|
|
|
|
it("is not found without a session, for a bad id, a refused page or a non-HTML block", async () => {
|
|
mocks.getToken.mockResolvedValueOnce(null);
|
|
expect((await preview.GET(req, ctx({ id: "5", block: "0" }))).status).toBe(404);
|
|
expect((await preview.GET(req, ctx({ id: "5x", block: "0" }))).status).toBe(404);
|
|
mocks.callMobileApi.mockResolvedValueOnce({ ok: false, status: 403 });
|
|
expect((await preview.GET(req, ctx({ id: "5", block: "0" }))).status).toBe(404);
|
|
mocks.callMobileApi.mockResolvedValueOnce({ ok: true, data: { slug: "hotel", title: "Hotel", blocks: BLOCKS } });
|
|
expect((await preview.GET(req, ctx({ id: "5", block: "1" }))).status).toBe(404);
|
|
expect(mocks.callMobileApi).toHaveBeenCalledTimes(2);
|
|
});
|
|
});
|