|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + * |
| 4 | + * Tests for favorites cache functions |
| 5 | + * |
| 6 | + * REGRESSION TEST: This test suite specifically validates that getConvexClerkToken |
| 7 | + * is called OUTSIDE the unstable_cache callback to prevent the runtime error: |
| 8 | + * "Route used headers() inside a function cached with unstable_cache()" |
| 9 | + * |
| 10 | + * Root cause: Clerk's auth() uses headers() which is dynamic data. |
| 11 | + * Dynamic data cannot be accessed inside unstable_cache scope. |
| 12 | + * |
| 13 | + * Fix: Call getConvexClerkToken() BEFORE unstable_cache() and capture token in closure. |
| 14 | + */ |
| 15 | +import { beforeEach, describe, expect, it, vi } from "vitest" |
| 16 | + |
| 17 | +// Mock server-only to allow testing |
| 18 | +vi.mock("server-only", () => ({})) |
| 19 | + |
| 20 | +// Track when getConvexClerkToken and unstable_cache are called |
| 21 | +const callOrder: string[] = [] |
| 22 | + |
| 23 | +// Mock getConvexClerkToken to track call order |
| 24 | +const mockGetConvexClerkToken = vi.fn().mockImplementation(async () => { |
| 25 | + callOrder.push("getConvexClerkToken") |
| 26 | + return "mock-token" |
| 27 | +}) |
| 28 | + |
| 29 | +vi.mock("../convex/client", () => ({ |
| 30 | + getConvexClerkToken: () => mockGetConvexClerkToken(), |
| 31 | +})) |
| 32 | + |
| 33 | +// Mock fetchQuery |
| 34 | +const mockFetchQuery = vi.fn().mockResolvedValue({ |
| 35 | + page: [], |
| 36 | + continueCursor: null, |
| 37 | + isDone: true, |
| 38 | +}) |
| 39 | + |
| 40 | +vi.mock("convex/nextjs", () => ({ |
| 41 | + fetchQuery: (...args: unknown[]) => mockFetchQuery(...args), |
| 42 | +})) |
| 43 | + |
| 44 | +// Mock unstable_cache to track when its callback is executed |
| 45 | +vi.mock("next/cache", () => ({ |
| 46 | + unstable_cache: (fn: () => Promise<unknown>, _keys: string[], _opts: unknown) => { |
| 47 | + // Return a function that, when called, executes the cached function |
| 48 | + return async () => { |
| 49 | + callOrder.push("unstable_cache_callback_start") |
| 50 | + const result = await fn() |
| 51 | + callOrder.push("unstable_cache_callback_end") |
| 52 | + return result |
| 53 | + } |
| 54 | + }, |
| 55 | +})) |
| 56 | + |
| 57 | +// Mock API |
| 58 | +vi.mock("@/convex/_generated/api", () => ({ |
| 59 | + api: { |
| 60 | + favorites: { |
| 61 | + list: "favorites:list", |
| 62 | + }, |
| 63 | + }, |
| 64 | +})) |
| 65 | + |
| 66 | +// Mock config |
| 67 | +vi.mock("./config", () => ({ |
| 68 | + CACHE_TTL: { |
| 69 | + FAVORITES_FIRST_PAGE: 30, |
| 70 | + FAVORITES_LATER_PAGES: 120, |
| 71 | + }, |
| 72 | + CACHE_TAGS: { |
| 73 | + FAVORITES_USER: (userId: string) => `favorites:${userId}`, |
| 74 | + }, |
| 75 | + PAGE_SIZES: { |
| 76 | + FAVORITES: 20, |
| 77 | + }, |
| 78 | +})) |
| 79 | + |
| 80 | +// Import after mocks |
| 81 | +import { getFavoritesPageCached } from "./favorites" |
| 82 | + |
| 83 | +describe("favorites cache", () => { |
| 84 | + beforeEach(() => { |
| 85 | + vi.clearAllMocks() |
| 86 | + callOrder.length = 0 // Clear array |
| 87 | + }) |
| 88 | + |
| 89 | + describe("getFavoritesPageCached", () => { |
| 90 | + it("calls getConvexClerkToken BEFORE unstable_cache callback", async () => { |
| 91 | + await getFavoritesPageCached("user_123", null, 20) |
| 92 | + |
| 93 | + // This is the critical assertion for the regression fix. |
| 94 | + // getConvexClerkToken MUST be called BEFORE the unstable_cache callback starts. |
| 95 | + // Otherwise, we get: "Route used headers() inside unstable_cache()" |
| 96 | + const tokenCallIndex = callOrder.indexOf("getConvexClerkToken") |
| 97 | + const cacheCallbackIndex = callOrder.indexOf("unstable_cache_callback_start") |
| 98 | + |
| 99 | + expect(tokenCallIndex).toBeGreaterThan(-1) |
| 100 | + expect(cacheCallbackIndex).toBeGreaterThan(-1) |
| 101 | + expect(tokenCallIndex).toBeLessThan(cacheCallbackIndex) |
| 102 | + }) |
| 103 | + |
| 104 | + it("passes token to fetchQuery inside cache callback", async () => { |
| 105 | + await getFavoritesPageCached("user_123", null, 20) |
| 106 | + |
| 107 | + expect(mockFetchQuery).toHaveBeenCalledWith( |
| 108 | + "favorites:list", |
| 109 | + { paginationOpts: { numItems: 20, cursor: null } }, |
| 110 | + { token: "mock-token" } |
| 111 | + ) |
| 112 | + }) |
| 113 | + |
| 114 | + it("uses correct cache configuration for first page", async () => { |
| 115 | + await getFavoritesPageCached("user_123", null, 20) |
| 116 | + |
| 117 | + expect(mockFetchQuery).toHaveBeenCalled() |
| 118 | + }) |
| 119 | + |
| 120 | + it("uses correct cache configuration for later pages", async () => { |
| 121 | + await getFavoritesPageCached("user_123", "cursor_abc", 20) |
| 122 | + |
| 123 | + expect(mockFetchQuery).toHaveBeenCalledWith( |
| 124 | + "favorites:list", |
| 125 | + { paginationOpts: { numItems: 20, cursor: "cursor_abc" } }, |
| 126 | + { token: "mock-token" } |
| 127 | + ) |
| 128 | + }) |
| 129 | + }) |
| 130 | +}) |
0 commit comments