|
| 1 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import { NextRequest, NextResponse } from "next/server"; |
| 3 | +import { getContentTemplateDetailHandler } from "@/lib/content/getContentTemplateDetailHandler"; |
| 4 | +import { validateAuthContext } from "@/lib/auth/validateAuthContext"; |
| 5 | +import { loadTemplate } from "@/lib/content/templates"; |
| 6 | + |
| 7 | +vi.mock("@/lib/networking/getCorsHeaders", () => ({ |
| 8 | + getCorsHeaders: vi.fn(() => ({ "Access-Control-Allow-Origin": "*" })), |
| 9 | +})); |
| 10 | + |
| 11 | +vi.mock("@/lib/auth/validateAuthContext", () => ({ |
| 12 | + validateAuthContext: vi.fn(), |
| 13 | +})); |
| 14 | + |
| 15 | +vi.mock("@/lib/content/templates", () => ({ |
| 16 | + loadTemplate: vi.fn(), |
| 17 | +})); |
| 18 | + |
| 19 | +describe("getContentTemplateDetailHandler", () => { |
| 20 | + beforeEach(() => { |
| 21 | + vi.clearAllMocks(); |
| 22 | + }); |
| 23 | + |
| 24 | + it("returns 401 when not authenticated", async () => { |
| 25 | + vi.mocked(validateAuthContext).mockResolvedValue( |
| 26 | + NextResponse.json({ status: "error", error: "Unauthorized" }, { status: 401 }), |
| 27 | + ); |
| 28 | + const request = new NextRequest("http://localhost/api/content/templates/bedroom", { |
| 29 | + method: "GET", |
| 30 | + }); |
| 31 | + |
| 32 | + const result = await getContentTemplateDetailHandler(request, { |
| 33 | + params: Promise.resolve({ id: "bedroom" }), |
| 34 | + }); |
| 35 | + |
| 36 | + expect(result.status).toBe(401); |
| 37 | + }); |
| 38 | + |
| 39 | + it("returns 404 for unknown template", async () => { |
| 40 | + vi.mocked(validateAuthContext).mockResolvedValue({ |
| 41 | + accountId: "acc_123", |
| 42 | + orgId: null, |
| 43 | + authToken: "test-key", |
| 44 | + }); |
| 45 | + vi.mocked(loadTemplate).mockReturnValue(null); |
| 46 | + |
| 47 | + const request = new NextRequest("http://localhost/api/content/templates/nonexistent", { |
| 48 | + method: "GET", |
| 49 | + }); |
| 50 | + |
| 51 | + const result = await getContentTemplateDetailHandler(request, { |
| 52 | + params: Promise.resolve({ id: "nonexistent" }), |
| 53 | + }); |
| 54 | + const body = await result.json(); |
| 55 | + |
| 56 | + expect(result.status).toBe(404); |
| 57 | + expect(body.error).toBe("Template not found"); |
| 58 | + }); |
| 59 | + |
| 60 | + it("returns full template for valid id", async () => { |
| 61 | + vi.mocked(validateAuthContext).mockResolvedValue({ |
| 62 | + accountId: "acc_123", |
| 63 | + orgId: null, |
| 64 | + authToken: "test-key", |
| 65 | + }); |
| 66 | + const mockTemplate = { |
| 67 | + id: "artist-caption-bedroom", |
| 68 | + description: "Moody purple bedroom setting", |
| 69 | + image: { prompt: "test", reference_images: [], style_rules: {} }, |
| 70 | + video: { moods: ["calm"], movements: ["slow pan"] }, |
| 71 | + caption: { guide: { tone: "dreamy", rules: [], formats: [] }, examples: [] }, |
| 72 | + edit: { operations: [] }, |
| 73 | + }; |
| 74 | + vi.mocked(loadTemplate).mockReturnValue(mockTemplate); |
| 75 | + |
| 76 | + const request = new NextRequest( |
| 77 | + "http://localhost/api/content/templates/artist-caption-bedroom", |
| 78 | + { method: "GET" }, |
| 79 | + ); |
| 80 | + |
| 81 | + const result = await getContentTemplateDetailHandler(request, { |
| 82 | + params: Promise.resolve({ id: "artist-caption-bedroom" }), |
| 83 | + }); |
| 84 | + const body = await result.json(); |
| 85 | + |
| 86 | + expect(result.status).toBe(200); |
| 87 | + expect(body.id).toBe("artist-caption-bedroom"); |
| 88 | + expect(body.description).toBe("Moody purple bedroom setting"); |
| 89 | + expect(body.image).toBeDefined(); |
| 90 | + expect(body.video).toBeDefined(); |
| 91 | + expect(body.caption).toBeDefined(); |
| 92 | + expect(body.edit).toBeDefined(); |
| 93 | + }); |
| 94 | +}); |
0 commit comments