|
| 1 | +import { describe, it, expect, vi, beforeEach } from "vitest"; |
| 2 | +import { NextRequest, NextResponse } from "next/server"; |
| 3 | + |
| 4 | +import { getResearchDiscoverHandler } from "../getResearchDiscoverHandler"; |
| 5 | +import { validateAuthContext } from "@/lib/auth/validateAuthContext"; |
| 6 | +import { proxyToChartmetric } from "@/lib/research/proxyToChartmetric"; |
| 7 | + |
| 8 | +vi.mock("@/lib/networking/getCorsHeaders", () => ({ |
| 9 | + getCorsHeaders: vi.fn(() => ({ "Access-Control-Allow-Origin": "*" })), |
| 10 | +})); |
| 11 | + |
| 12 | +vi.mock("@/lib/auth/validateAuthContext", () => ({ |
| 13 | + validateAuthContext: vi.fn(), |
| 14 | +})); |
| 15 | + |
| 16 | +vi.mock("@/lib/research/proxyToChartmetric", () => ({ |
| 17 | + proxyToChartmetric: vi.fn(), |
| 18 | +})); |
| 19 | + |
| 20 | +vi.mock("@/lib/credits/deductCredits", () => ({ |
| 21 | + deductCredits: vi.fn(), |
| 22 | +})); |
| 23 | + |
| 24 | +describe("getResearchDiscoverHandler", () => { |
| 25 | + beforeEach(() => { |
| 26 | + vi.clearAllMocks(); |
| 27 | + }); |
| 28 | + |
| 29 | + it("returns 401 when auth fails", async () => { |
| 30 | + const errorResponse = NextResponse.json({ error: "Unauthorized" }, { status: 401 }); |
| 31 | + vi.mocked(validateAuthContext).mockResolvedValue(errorResponse); |
| 32 | + |
| 33 | + const req = new NextRequest("http://localhost/api/research/discover?country=US"); |
| 34 | + const res = await getResearchDiscoverHandler(req); |
| 35 | + expect(res.status).toBe(401); |
| 36 | + }); |
| 37 | + |
| 38 | + it("returns 400 when country is not 2 letters", async () => { |
| 39 | + vi.mocked(validateAuthContext).mockResolvedValue({ |
| 40 | + accountId: "test-id", |
| 41 | + orgId: null, |
| 42 | + authToken: "tok", |
| 43 | + } as ReturnType<typeof validateAuthContext> extends Promise<infer T> |
| 44 | + ? Exclude<T, NextResponse> |
| 45 | + : never); |
| 46 | + |
| 47 | + const req = new NextRequest("http://localhost/api/research/discover?country=USA"); |
| 48 | + const res = await getResearchDiscoverHandler(req); |
| 49 | + expect(res.status).toBe(400); |
| 50 | + const body = await res.json(); |
| 51 | + expect(body.status).toBe("error"); |
| 52 | + expect(body.error).toContain("2-letter"); |
| 53 | + }); |
| 54 | + |
| 55 | + it("returns 400 when limit is negative", async () => { |
| 56 | + vi.mocked(validateAuthContext).mockResolvedValue({ |
| 57 | + accountId: "test-id", |
| 58 | + orgId: null, |
| 59 | + authToken: "tok", |
| 60 | + } as ReturnType<typeof validateAuthContext> extends Promise<infer T> |
| 61 | + ? Exclude<T, NextResponse> |
| 62 | + : never); |
| 63 | + |
| 64 | + const req = new NextRequest("http://localhost/api/research/discover?limit=-5"); |
| 65 | + const res = await getResearchDiscoverHandler(req); |
| 66 | + expect(res.status).toBe(400); |
| 67 | + }); |
| 68 | + |
| 69 | + it("returns artists on success", async () => { |
| 70 | + vi.mocked(validateAuthContext).mockResolvedValue({ |
| 71 | + accountId: "test-id", |
| 72 | + orgId: null, |
| 73 | + authToken: "tok", |
| 74 | + } as ReturnType<typeof validateAuthContext> extends Promise<infer T> |
| 75 | + ? Exclude<T, NextResponse> |
| 76 | + : never); |
| 77 | + |
| 78 | + vi.mocked(proxyToChartmetric).mockResolvedValue({ |
| 79 | + data: [ |
| 80 | + { name: "Artist A", sp_monthly_listeners: 100000 }, |
| 81 | + { name: "Artist B", sp_monthly_listeners: 200000 }, |
| 82 | + ], |
| 83 | + status: 200, |
| 84 | + }); |
| 85 | + |
| 86 | + const req = new NextRequest("http://localhost/api/research/discover?country=US&limit=10"); |
| 87 | + const res = await getResearchDiscoverHandler(req); |
| 88 | + expect(res.status).toBe(200); |
| 89 | + const body = await res.json(); |
| 90 | + expect(body.status).toBe("success"); |
| 91 | + expect(body.artists).toHaveLength(2); |
| 92 | + expect(body.artists[0].name).toBe("Artist A"); |
| 93 | + }); |
| 94 | + |
| 95 | + it("passes sp_ml range when both min and max provided", async () => { |
| 96 | + vi.mocked(validateAuthContext).mockResolvedValue({ |
| 97 | + accountId: "test-id", |
| 98 | + orgId: null, |
| 99 | + authToken: "tok", |
| 100 | + } as ReturnType<typeof validateAuthContext> extends Promise<infer T> |
| 101 | + ? Exclude<T, NextResponse> |
| 102 | + : never); |
| 103 | + |
| 104 | + vi.mocked(proxyToChartmetric).mockResolvedValue({ |
| 105 | + data: [], |
| 106 | + status: 200, |
| 107 | + }); |
| 108 | + |
| 109 | + const req = new NextRequest( |
| 110 | + "http://localhost/api/research/discover?sp_monthly_listeners_min=50000&sp_monthly_listeners_max=200000", |
| 111 | + ); |
| 112 | + await getResearchDiscoverHandler(req); |
| 113 | + |
| 114 | + expect(proxyToChartmetric).toHaveBeenCalledWith( |
| 115 | + "/artist/list/filter", |
| 116 | + expect.objectContaining({ "sp_ml[]": "50000,200000" }), |
| 117 | + ); |
| 118 | + }); |
| 119 | + |
| 120 | + it("returns empty array when proxy fails", async () => { |
| 121 | + vi.mocked(validateAuthContext).mockResolvedValue({ |
| 122 | + accountId: "test-id", |
| 123 | + orgId: null, |
| 124 | + authToken: "tok", |
| 125 | + } as ReturnType<typeof validateAuthContext> extends Promise<infer T> |
| 126 | + ? Exclude<T, NextResponse> |
| 127 | + : never); |
| 128 | + |
| 129 | + vi.mocked(proxyToChartmetric).mockResolvedValue({ |
| 130 | + data: null, |
| 131 | + status: 500, |
| 132 | + }); |
| 133 | + |
| 134 | + const req = new NextRequest("http://localhost/api/research/discover?country=US"); |
| 135 | + const res = await getResearchDiscoverHandler(req); |
| 136 | + expect(res.status).toBe(500); |
| 137 | + }); |
| 138 | +}); |
0 commit comments