|
| 1 | +import { describe, it, expect, vi, beforeEach } from "vitest"; |
| 2 | +import { NextRequest } from "next/server"; |
| 3 | + |
| 4 | +// Mock next/headers cookies before importing the route |
| 5 | +vi.mock("next/headers", () => ({ |
| 6 | + cookies: vi.fn().mockResolvedValue({ |
| 7 | + getAll: () => [], |
| 8 | + set: vi.fn(), |
| 9 | + }), |
| 10 | +})); |
| 11 | + |
| 12 | +// Mock the Supabase server client |
| 13 | +vi.mock("@/lib/supabase/server", () => ({ |
| 14 | + createClient: vi.fn(), |
| 15 | +})); |
| 16 | + |
| 17 | +import { GET } from "./route"; |
| 18 | +import { createClient } from "@/lib/supabase/server"; |
| 19 | + |
| 20 | +const mockedCreateClient = vi.mocked(createClient); |
| 21 | + |
| 22 | +function makeRequest(params: Record<string, string> = {}): NextRequest { |
| 23 | + const url = new URL("http://localhost:3000/api/search"); |
| 24 | + for (const [key, value] of Object.entries(params)) { |
| 25 | + url.searchParams.set(key, value); |
| 26 | + } |
| 27 | + return new NextRequest(url); |
| 28 | +} |
| 29 | + |
| 30 | +beforeEach(() => { |
| 31 | + vi.restoreAllMocks(); |
| 32 | + process.env.NEXT_PUBLIC_SUPABASE_URL = "https://example.supabase.co"; |
| 33 | + process.env.NEXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY = "test-key"; |
| 34 | +}); |
| 35 | + |
| 36 | +describe("GET /api/search", () => { |
| 37 | + it("returns 503 when Supabase is not configured", async () => { |
| 38 | + delete process.env.NEXT_PUBLIC_SUPABASE_URL; |
| 39 | + |
| 40 | + const response = await GET(makeRequest({ q: "test", workspace_id: "ws-1" }) ); |
| 41 | + const body = await response.json(); |
| 42 | + |
| 43 | + expect(response.status).toBe(503); |
| 44 | + expect(body.error).toBe("Supabase not configured"); |
| 45 | + }); |
| 46 | + |
| 47 | + it("returns 400 when q parameter is missing", async () => { |
| 48 | + const response = await GET(makeRequest({ workspace_id: "ws-1" }) ); |
| 49 | + const body = await response.json(); |
| 50 | + |
| 51 | + expect(response.status).toBe(400); |
| 52 | + expect(body.error).toContain("Missing required parameters"); |
| 53 | + }); |
| 54 | + |
| 55 | + it("returns 400 when workspace_id parameter is missing", async () => { |
| 56 | + const response = await GET(makeRequest({ q: "test" }) ); |
| 57 | + const body = await response.json(); |
| 58 | + |
| 59 | + expect(response.status).toBe(400); |
| 60 | + expect(body.error).toContain("Missing required parameters"); |
| 61 | + }); |
| 62 | + |
| 63 | + it("returns 400 when query exceeds 200 characters", async () => { |
| 64 | + const longQuery = "a".repeat(201); |
| 65 | + const response = await GET( |
| 66 | + makeRequest({ q: longQuery, workspace_id: "ws-1" }) as never, |
| 67 | + ); |
| 68 | + const body = await response.json(); |
| 69 | + |
| 70 | + expect(response.status).toBe(400); |
| 71 | + expect(body.error).toContain("Query too long"); |
| 72 | + }); |
| 73 | + |
| 74 | + it("returns 401 when user is not authenticated", async () => { |
| 75 | + const mockGetUser = vi.fn().mockResolvedValue({ data: { user: null } }); |
| 76 | + mockedCreateClient.mockResolvedValue({ |
| 77 | + auth: { getUser: mockGetUser }, |
| 78 | + } as unknown as Awaited<ReturnType<typeof createClient>>); |
| 79 | + |
| 80 | + const response = await GET( |
| 81 | + makeRequest({ q: "test", workspace_id: "ws-1" }) as never, |
| 82 | + ); |
| 83 | + const body = await response.json(); |
| 84 | + |
| 85 | + expect(response.status).toBe(401); |
| 86 | + expect(body.error).toBe("Unauthorized"); |
| 87 | + }); |
| 88 | + |
| 89 | + it("returns 500 when RPC call fails", async () => { |
| 90 | + const mockGetUser = vi |
| 91 | + .fn() |
| 92 | + .mockResolvedValue({ data: { user: { id: "user-1" } } }); |
| 93 | + const mockRpc = vi |
| 94 | + .fn() |
| 95 | + .mockResolvedValue({ data: null, error: { message: "RPC error" } }); |
| 96 | + mockedCreateClient.mockResolvedValue({ |
| 97 | + auth: { getUser: mockGetUser }, |
| 98 | + rpc: mockRpc, |
| 99 | + } as unknown as Awaited<ReturnType<typeof createClient>>); |
| 100 | + |
| 101 | + const response = await GET( |
| 102 | + makeRequest({ q: "test", workspace_id: "ws-1" }) as never, |
| 103 | + ); |
| 104 | + const body = await response.json(); |
| 105 | + |
| 106 | + expect(response.status).toBe(500); |
| 107 | + expect(body.error).toBe("Search failed"); |
| 108 | + expect(mockRpc).toHaveBeenCalledWith("search_pages", { |
| 109 | + query: "test", |
| 110 | + ws_id: "ws-1", |
| 111 | + result_limit: 20, |
| 112 | + }); |
| 113 | + }); |
| 114 | + |
| 115 | + it("returns 200 with results on success", async () => { |
| 116 | + const mockResults = [ |
| 117 | + { |
| 118 | + id: "page-1", |
| 119 | + workspace_id: "ws-1", |
| 120 | + parent_id: null, |
| 121 | + title: "Test Page", |
| 122 | + icon: "📄", |
| 123 | + snippet: "<<test>> content here", |
| 124 | + rank: 0.5, |
| 125 | + }, |
| 126 | + ]; |
| 127 | + const mockGetUser = vi |
| 128 | + .fn() |
| 129 | + .mockResolvedValue({ data: { user: { id: "user-1" } } }); |
| 130 | + const mockRpc = vi |
| 131 | + .fn() |
| 132 | + .mockResolvedValue({ data: mockResults, error: null }); |
| 133 | + mockedCreateClient.mockResolvedValue({ |
| 134 | + auth: { getUser: mockGetUser }, |
| 135 | + rpc: mockRpc, |
| 136 | + } as unknown as Awaited<ReturnType<typeof createClient>>); |
| 137 | + |
| 138 | + const response = await GET( |
| 139 | + makeRequest({ q: "test", workspace_id: "ws-1" }) as never, |
| 140 | + ); |
| 141 | + const body = await response.json(); |
| 142 | + |
| 143 | + expect(response.status).toBe(200); |
| 144 | + expect(body.results).toEqual(mockResults); |
| 145 | + }); |
| 146 | + |
| 147 | + it("returns 200 with empty array when no results", async () => { |
| 148 | + const mockGetUser = vi |
| 149 | + .fn() |
| 150 | + .mockResolvedValue({ data: { user: { id: "user-1" } } }); |
| 151 | + const mockRpc = vi.fn().mockResolvedValue({ data: null, error: null }); |
| 152 | + mockedCreateClient.mockResolvedValue({ |
| 153 | + auth: { getUser: mockGetUser }, |
| 154 | + rpc: mockRpc, |
| 155 | + } as unknown as Awaited<ReturnType<typeof createClient>>); |
| 156 | + |
| 157 | + const response = await GET( |
| 158 | + makeRequest({ q: "test", workspace_id: "ws-1" }) as never, |
| 159 | + ); |
| 160 | + const body = await response.json(); |
| 161 | + |
| 162 | + expect(response.status).toBe(200); |
| 163 | + expect(body.results).toEqual([]); |
| 164 | + }); |
| 165 | +}); |
0 commit comments