|
| 1 | +import { describe, it, expect, vi, beforeEach } from "vitest"; |
| 2 | +import { NextRequest } from "next/server"; |
| 3 | + |
| 4 | +// Mock the disk-cache store so tests don't touch the filesystem. |
| 5 | +const mockReadAtmosphereMusic = vi.fn(); |
| 6 | +const mockSaveAtmosphereMusic = vi.fn(); |
| 7 | +const mockDeleteAtmosphereMusic = vi.fn(); |
| 8 | +vi.mock("@/lib/ai/atmosphereMusicStore", () => ({ |
| 9 | + readAtmosphereMusic: (world: string) => mockReadAtmosphereMusic(world), |
| 10 | + saveAtmosphereMusic: (world: string, data: Buffer) => mockSaveAtmosphereMusic(world, data), |
| 11 | + deleteAtmosphereMusic: (world: string) => mockDeleteAtmosphereMusic(world), |
| 12 | +})); |
| 13 | + |
| 14 | +// Mock the ElevenLabs client so no network call is made. |
| 15 | +const mockElevenLabsFetch = vi.fn(); |
| 16 | +vi.mock("@/lib/elevenlabs/client", () => ({ |
| 17 | + elevenLabsFetch: (path: string, init?: RequestInit) => mockElevenLabsFetch(path, init), |
| 18 | +})); |
| 19 | + |
| 20 | +function makeRequest(url: string): NextRequest { |
| 21 | + return new NextRequest(new Request(url)); |
| 22 | +} |
| 23 | + |
| 24 | +describe("/api/music/[world]/atmosphere", () => { |
| 25 | + beforeEach(() => { |
| 26 | + mockReadAtmosphereMusic.mockReset(); |
| 27 | + mockSaveAtmosphereMusic.mockReset(); |
| 28 | + mockDeleteAtmosphereMusic.mockReset(); |
| 29 | + mockElevenLabsFetch.mockReset(); |
| 30 | + delete process.env.ELEVENLABS_API_KEY; |
| 31 | + }); |
| 32 | + |
| 33 | + it("returns cached bytes immutable on a cache hit", async () => { |
| 34 | + mockReadAtmosphereMusic.mockResolvedValueOnce({ |
| 35 | + data: Buffer.from("cached-music"), |
| 36 | + contentType: "audio/mpeg", |
| 37 | + }); |
| 38 | + const { GET } = await import("./route"); |
| 39 | + |
| 40 | + const response = await GET(makeRequest("http://localhost/api/music/grand-hotel/atmosphere"), { |
| 41 | + params: Promise.resolve({ world: "grand-hotel" }), |
| 42 | + }); |
| 43 | + |
| 44 | + expect(response.status).toBe(200); |
| 45 | + expect(response.headers.get("Content-Type")).toBe("audio/mpeg"); |
| 46 | + expect(response.headers.get("Cache-Control")).toBe("public, max-age=31536000, immutable"); |
| 47 | + expect(Buffer.from(await response.arrayBuffer()).toString()).toBe("cached-music"); |
| 48 | + expect(mockElevenLabsFetch).not.toHaveBeenCalled(); |
| 49 | + }); |
| 50 | + |
| 51 | + it("redirects to the noir fallback when no key is configured", async () => { |
| 52 | + mockReadAtmosphereMusic.mockResolvedValueOnce(null); |
| 53 | + const { GET } = await import("./route"); |
| 54 | + |
| 55 | + const response = await GET(makeRequest("http://localhost/api/music/grand-hotel/atmosphere"), { |
| 56 | + params: Promise.resolve({ world: "grand-hotel" }), |
| 57 | + }); |
| 58 | + |
| 59 | + expect(response.status).toBe(307); |
| 60 | + expect(response.headers.get("Location")).toContain("/assets/noir/noir-jazz-loop.mp3"); |
| 61 | + expect(mockElevenLabsFetch).not.toHaveBeenCalled(); |
| 62 | + }); |
| 63 | + |
| 64 | + it("generates, caches, and serves bytes when a key is configured", async () => { |
| 65 | + process.env.ELEVENLABS_API_KEY = "test-key"; |
| 66 | + mockReadAtmosphereMusic.mockResolvedValueOnce(null); |
| 67 | + mockElevenLabsFetch.mockResolvedValueOnce({ |
| 68 | + ok: true, |
| 69 | + arrayBuffer: async () => Buffer.from("fresh-music"), |
| 70 | + }); |
| 71 | + const { GET } = await import("./route"); |
| 72 | + |
| 73 | + const response = await GET(makeRequest("http://localhost/api/music/grand-hotel/atmosphere"), { |
| 74 | + params: Promise.resolve({ world: "grand-hotel" }), |
| 75 | + }); |
| 76 | + |
| 77 | + expect(response.status).toBe(200); |
| 78 | + expect(Buffer.from(await response.arrayBuffer()).toString()).toBe("fresh-music"); |
| 79 | + expect(mockElevenLabsFetch).toHaveBeenCalledWith( |
| 80 | + "/music", |
| 81 | + expect.objectContaining({ method: "POST" }) |
| 82 | + ); |
| 83 | + expect(mockSaveAtmosphereMusic).toHaveBeenCalledWith("grand-hotel", expect.anything()); |
| 84 | + }); |
| 85 | + |
| 86 | + it("404s for an unknown world", async () => { |
| 87 | + const { GET } = await import("./route"); |
| 88 | + |
| 89 | + const response = await GET(makeRequest("http://localhost/api/music/not-a-world/atmosphere"), { |
| 90 | + params: Promise.resolve({ world: "not-a-world" }), |
| 91 | + }); |
| 92 | + |
| 93 | + expect(response.status).toBe(404); |
| 94 | + expect(mockReadAtmosphereMusic).not.toHaveBeenCalled(); |
| 95 | + }); |
| 96 | +}); |
0 commit comments