|
| 1 | +import { vi } from 'vitest'; |
| 2 | + |
| 3 | +// These globals are normally injected at build time by tsup. |
| 4 | +(globalThis as any).PACKAGE_NAME = '@clerk/nuxt'; |
| 5 | +(globalThis as any).PACKAGE_VERSION = '0.0.0-test'; |
| 6 | + |
| 7 | +vi.mock('#imports', () => { |
| 8 | + return { |
| 9 | + useRuntimeConfig: vi.fn(), |
| 10 | + }; |
| 11 | +}); |
| 12 | + |
| 13 | +vi.mock('@clerk/backend', () => { |
| 14 | + return { |
| 15 | + createClerkClient: vi.fn().mockReturnValue({}), |
| 16 | + }; |
| 17 | +}); |
| 18 | + |
| 19 | +import { createClerkClient } from '@clerk/backend'; |
| 20 | + |
| 21 | +// @ts-expect-error: Nitro import. Handled by Nuxt. |
| 22 | +import { useRuntimeConfig } from '#imports'; |
| 23 | + |
| 24 | +import { clerkClient } from '../clerkClient'; |
| 25 | + |
| 26 | +const useRuntimeConfigMock = vi.mocked(useRuntimeConfig); |
| 27 | +const createClerkClientMock = vi.mocked(createClerkClient); |
| 28 | + |
| 29 | +function mockRuntimeConfig(overrides: { publishableKey?: string; apiUrl?: string } = {}) { |
| 30 | + useRuntimeConfigMock.mockReturnValue({ |
| 31 | + public: { |
| 32 | + clerk: { |
| 33 | + publishableKey: overrides.publishableKey ?? 'pk_test_Y2xlcmsuY2xlcmsuY29tJA', |
| 34 | + apiUrl: overrides.apiUrl ?? '', |
| 35 | + apiVersion: 'v1', |
| 36 | + proxyUrl: '', |
| 37 | + domain: '', |
| 38 | + isSatellite: false, |
| 39 | + telemetry: {}, |
| 40 | + }, |
| 41 | + }, |
| 42 | + clerk: { |
| 43 | + secretKey: 'sk_test_xxx', |
| 44 | + machineSecretKey: '', |
| 45 | + jwtKey: '', |
| 46 | + }, |
| 47 | + } as any); |
| 48 | +} |
| 49 | + |
| 50 | +describe('clerkClient', () => { |
| 51 | + beforeEach(() => { |
| 52 | + vi.clearAllMocks(); |
| 53 | + }); |
| 54 | + |
| 55 | + it('derives staging API URL from staging publishable key', () => { |
| 56 | + // pk_test_ + base64("safe-egret-46.clerk.accountsstage.dev$") |
| 57 | + const stagingPk = 'pk_test_c2FmZS1lZ3JldC00Ni5jbGVyay5hY2NvdW50c3N0YWdlLmRldiQ'; |
| 58 | + mockRuntimeConfig({ publishableKey: stagingPk }); |
| 59 | + |
| 60 | + clerkClient({} as any); |
| 61 | + |
| 62 | + expect(createClerkClientMock).toHaveBeenCalledWith( |
| 63 | + expect.objectContaining({ |
| 64 | + apiUrl: 'https://api.clerkstage.dev', |
| 65 | + }), |
| 66 | + ); |
| 67 | + }); |
| 68 | + |
| 69 | + it('uses production API URL for production publishable key', () => { |
| 70 | + const prodPk = 'pk_test_Y2xlcmsuY2xlcmsuY29tJA'; |
| 71 | + mockRuntimeConfig({ publishableKey: prodPk }); |
| 72 | + |
| 73 | + clerkClient({} as any); |
| 74 | + |
| 75 | + expect(createClerkClientMock).toHaveBeenCalledWith( |
| 76 | + expect.objectContaining({ |
| 77 | + apiUrl: 'https://api.clerk.com', |
| 78 | + }), |
| 79 | + ); |
| 80 | + }); |
| 81 | + |
| 82 | + it('prefers explicit apiUrl over derived value', () => { |
| 83 | + const stagingPk = 'pk_test_c2FmZS1lZ3JldC00Ni5jbGVyay5hY2NvdW50c3N0YWdlLmRldiQ'; |
| 84 | + mockRuntimeConfig({ publishableKey: stagingPk, apiUrl: 'https://custom.api.example.com' }); |
| 85 | + |
| 86 | + clerkClient({} as any); |
| 87 | + |
| 88 | + expect(createClerkClientMock).toHaveBeenCalledWith( |
| 89 | + expect.objectContaining({ |
| 90 | + apiUrl: 'https://custom.api.example.com', |
| 91 | + }), |
| 92 | + ); |
| 93 | + }); |
| 94 | +}); |
0 commit comments