|
| 1 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import { NextRequest, NextResponse } from "next/server"; |
| 3 | +import { createContentHandler } from "@/lib/content/createContentHandler"; |
| 4 | +import { validateCreateContentBody } from "@/lib/content/validateCreateContentBody"; |
| 5 | +import { triggerCreateContent } from "@/lib/trigger/triggerCreateContent"; |
| 6 | +import { getArtistContentReadiness } from "@/lib/content/getArtistContentReadiness"; |
| 7 | + |
| 8 | +vi.mock("@/lib/networking/getCorsHeaders", () => ({ |
| 9 | + getCorsHeaders: vi.fn(() => ({ "Access-Control-Allow-Origin": "*" })), |
| 10 | +})); |
| 11 | + |
| 12 | +vi.mock("@/lib/content/validateCreateContentBody", () => ({ |
| 13 | + validateCreateContentBody: vi.fn(), |
| 14 | +})); |
| 15 | + |
| 16 | +vi.mock("@/lib/trigger/triggerCreateContent", () => ({ |
| 17 | + triggerCreateContent: vi.fn(), |
| 18 | +})); |
| 19 | + |
| 20 | +vi.mock("@/lib/content/getArtistContentReadiness", () => ({ |
| 21 | + getArtistContentReadiness: vi.fn(), |
| 22 | +})); |
| 23 | + |
| 24 | +vi.mock("@/lib/supabase/account_snapshots/selectAccountSnapshots", () => ({ |
| 25 | + selectAccountSnapshots: vi.fn(), |
| 26 | +})); |
| 27 | + |
| 28 | +describe("createContentHandler", () => { |
| 29 | + beforeEach(() => { |
| 30 | + vi.clearAllMocks(); |
| 31 | + vi.mocked(getArtistContentReadiness).mockResolvedValue({ |
| 32 | + artist_account_id: "art_456", |
| 33 | + ready: true, |
| 34 | + missing: [], |
| 35 | + warnings: [], |
| 36 | + githubRepo: "https://github.com/test/repo", |
| 37 | + }); |
| 38 | + }); |
| 39 | + |
| 40 | + it("returns validation/auth error when validation fails", async () => { |
| 41 | + const errorResponse = NextResponse.json( |
| 42 | + { status: "error", error: "Unauthorized" }, |
| 43 | + { status: 401 }, |
| 44 | + ); |
| 45 | + vi.mocked(validateCreateContentBody).mockResolvedValue(errorResponse); |
| 46 | + const request = new NextRequest("http://localhost/api/content/create", { method: "POST" }); |
| 47 | + |
| 48 | + const result = await createContentHandler(request); |
| 49 | + |
| 50 | + expect(result).toBe(errorResponse); |
| 51 | + }); |
| 52 | + |
| 53 | + it("returns 202 with runIds when trigger succeeds", async () => { |
| 54 | + vi.mocked(validateCreateContentBody).mockResolvedValue({ |
| 55 | + accountId: "acc_123", |
| 56 | + artistAccountId: "art_456", |
| 57 | + artistSlug: "gatsby-grace", |
| 58 | + template: "artist-caption-bedroom", |
| 59 | + lipsync: false, |
| 60 | + captionLength: "short", |
| 61 | + upscale: false, |
| 62 | + batch: 1, |
| 63 | + }); |
| 64 | + vi.mocked(triggerCreateContent).mockResolvedValue({ id: "run_abc123" } as never); |
| 65 | + const request = new NextRequest("http://localhost/api/content/create", { method: "POST" }); |
| 66 | + |
| 67 | + const result = await createContentHandler(request); |
| 68 | + const body = await result.json(); |
| 69 | + |
| 70 | + expect(result.status).toBe(202); |
| 71 | + expect(body.runIds).toEqual(["run_abc123"]); |
| 72 | + expect(body.status).toBe("triggered"); |
| 73 | + expect(body.artist_account_id).toBe("art_456"); |
| 74 | + }); |
| 75 | + |
| 76 | + it("returns 202 with empty runIds and failed count when trigger fails", async () => { |
| 77 | + vi.mocked(validateCreateContentBody).mockResolvedValue({ |
| 78 | + accountId: "acc_123", |
| 79 | + artistAccountId: "art_456", |
| 80 | + artistSlug: "gatsby-grace", |
| 81 | + template: "artist-caption-bedroom", |
| 82 | + lipsync: false, |
| 83 | + captionLength: "short", |
| 84 | + upscale: false, |
| 85 | + batch: 1, |
| 86 | + }); |
| 87 | + vi.mocked(triggerCreateContent).mockRejectedValue(new Error("Trigger unavailable")); |
| 88 | + const request = new NextRequest("http://localhost/api/content/create", { method: "POST" }); |
| 89 | + |
| 90 | + const result = await createContentHandler(request); |
| 91 | + const body = await result.json(); |
| 92 | + |
| 93 | + expect(result.status).toBe(202); |
| 94 | + expect(body.runIds).toEqual([]); |
| 95 | + expect(body.failed).toBe(1); |
| 96 | + }); |
| 97 | + |
| 98 | + it("still triggers when readiness check finds missing files (best-effort)", async () => { |
| 99 | + vi.mocked(validateCreateContentBody).mockResolvedValue({ |
| 100 | + accountId: "acc_123", |
| 101 | + artistAccountId: "art_456", |
| 102 | + artistSlug: "gatsby-grace", |
| 103 | + template: "artist-caption-bedroom", |
| 104 | + lipsync: false, |
| 105 | + captionLength: "short", |
| 106 | + upscale: false, |
| 107 | + batch: 1, |
| 108 | + }); |
| 109 | + vi.mocked(getArtistContentReadiness).mockResolvedValue({ |
| 110 | + artist_account_id: "art_456", |
| 111 | + ready: false, |
| 112 | + missing: [ |
| 113 | + { |
| 114 | + file: "context/images/face-guide.png", |
| 115 | + severity: "required", |
| 116 | + fix: "Generate a face guide image before creating content.", |
| 117 | + }, |
| 118 | + ], |
| 119 | + warnings: [], |
| 120 | + }); |
| 121 | + const request = new NextRequest("http://localhost/api/content/create", { method: "POST" }); |
| 122 | + |
| 123 | + const result = await createContentHandler(request); |
| 124 | + const body = await result.json(); |
| 125 | + |
| 126 | + // Best-effort: validation doesn't block, task handles its own file discovery |
| 127 | + expect(result.status).toBe(202); |
| 128 | + expect(body.runIds).toBeDefined(); |
| 129 | + }); |
| 130 | +}); |
| 131 | + |
0 commit comments