|
| 1 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | + |
| 3 | +const mocks = vi.hoisted(() => ({ |
| 4 | + authState: { |
| 5 | + cloudRegion: "us" as string | null, |
| 6 | + getCloudUrlFromRegion: vi.fn(() => "https://us.posthog.com"), |
| 7 | + oauthAccessToken: "access-token" as string | null, |
| 8 | + projectId: 123 as number | null, |
| 9 | + refreshAccessToken: vi.fn(async () => {}), |
| 10 | + }, |
| 11 | + expoApplication: { |
| 12 | + nativeApplicationVersion: "1.2.3" as string | null, |
| 13 | + }, |
| 14 | + expoConstants: { |
| 15 | + expoConfig: { version: "9.9.9" } as { version?: string } | null, |
| 16 | + }, |
| 17 | + expoFetch: vi.fn(), |
| 18 | + instances: [] as Array<{ |
| 19 | + apiHost: string; |
| 20 | + getAccessToken: () => Promise<string>; |
| 21 | + refreshAccessToken: () => Promise<string>; |
| 22 | + teamId: number | undefined; |
| 23 | + options: Record<string, unknown>; |
| 24 | + setTeamId: ReturnType<typeof vi.fn>; |
| 25 | + }>, |
| 26 | +})); |
| 27 | + |
| 28 | +vi.mock("@posthog/api-client/posthog-client", () => ({ |
| 29 | + PostHogAPIClient: class { |
| 30 | + setTeamId = vi.fn(); |
| 31 | + |
| 32 | + constructor( |
| 33 | + apiHost: string, |
| 34 | + getAccessToken: () => Promise<string>, |
| 35 | + refreshAccessToken: () => Promise<string>, |
| 36 | + teamId: number | undefined, |
| 37 | + options: Record<string, unknown>, |
| 38 | + ) { |
| 39 | + mocks.instances.push({ |
| 40 | + apiHost, |
| 41 | + getAccessToken, |
| 42 | + refreshAccessToken, |
| 43 | + teamId, |
| 44 | + options, |
| 45 | + setTeamId: this.setTeamId, |
| 46 | + }); |
| 47 | + } |
| 48 | + }, |
| 49 | +})); |
| 50 | + |
| 51 | +vi.mock("expo-application", () => ({ |
| 52 | + get nativeApplicationVersion() { |
| 53 | + return mocks.expoApplication.nativeApplicationVersion; |
| 54 | + }, |
| 55 | +})); |
| 56 | + |
| 57 | +vi.mock("expo-constants", () => ({ |
| 58 | + default: { |
| 59 | + get expoConfig() { |
| 60 | + return mocks.expoConstants.expoConfig; |
| 61 | + }, |
| 62 | + }, |
| 63 | +})); |
| 64 | + |
| 65 | +vi.mock("expo/fetch", () => ({ fetch: mocks.expoFetch })); |
| 66 | + |
| 67 | +vi.mock("@/features/auth", () => ({ |
| 68 | + useAuthStore: { |
| 69 | + getState: () => mocks.authState, |
| 70 | + }, |
| 71 | +})); |
| 72 | + |
| 73 | +beforeEach(() => { |
| 74 | + vi.resetModules(); |
| 75 | + vi.clearAllMocks(); |
| 76 | + mocks.instances.length = 0; |
| 77 | + mocks.authState.cloudRegion = "us"; |
| 78 | + mocks.authState.oauthAccessToken = "access-token"; |
| 79 | + mocks.authState.projectId = 123; |
| 80 | + mocks.authState.getCloudUrlFromRegion.mockReturnValue( |
| 81 | + "https://us.posthog.com", |
| 82 | + ); |
| 83 | + mocks.authState.refreshAccessToken.mockImplementation(async () => {}); |
| 84 | + mocks.expoApplication.nativeApplicationVersion = "1.2.3"; |
| 85 | + mocks.expoConstants.expoConfig = { version: "9.9.9" }; |
| 86 | +}); |
| 87 | + |
| 88 | +describe("createPostHogApiClient", () => { |
| 89 | + it("configures the shared client for the mobile host", async () => { |
| 90 | + const { createPostHogApiClient } = await import("./posthogApiClient"); |
| 91 | + |
| 92 | + createPostHogApiClient(); |
| 93 | + |
| 94 | + expect(mocks.instances).toHaveLength(1); |
| 95 | + expect(mocks.instances[0]).toMatchObject({ |
| 96 | + apiHost: "https://us.posthog.com", |
| 97 | + teamId: 123, |
| 98 | + options: { |
| 99 | + appVersion: "1.2.3", |
| 100 | + fetch: mocks.expoFetch, |
| 101 | + githubConnectFrom: "posthog_mobile", |
| 102 | + userAgent: "posthog/mobile.hog.dev; version: 1.2.3", |
| 103 | + }, |
| 104 | + }); |
| 105 | + }); |
| 106 | + |
| 107 | + it("falls back to the Expo config version", async () => { |
| 108 | + mocks.expoApplication.nativeApplicationVersion = null; |
| 109 | + mocks.expoConstants.expoConfig = { version: "4.5.6" }; |
| 110 | + const { createPostHogApiClient } = await import("./posthogApiClient"); |
| 111 | + |
| 112 | + createPostHogApiClient(); |
| 113 | + |
| 114 | + expect(mocks.instances[0]?.options).toMatchObject({ |
| 115 | + appVersion: "4.5.6", |
| 116 | + userAgent: "posthog/mobile.hog.dev; version: 4.5.6", |
| 117 | + }); |
| 118 | + }); |
| 119 | + |
| 120 | + it("returns the refreshed token from the current auth store state", async () => { |
| 121 | + mocks.authState.refreshAccessToken.mockImplementation(async () => { |
| 122 | + mocks.authState.oauthAccessToken = "refreshed-token"; |
| 123 | + }); |
| 124 | + const { createPostHogApiClient } = await import("./posthogApiClient"); |
| 125 | + createPostHogApiClient(); |
| 126 | + |
| 127 | + await expect(mocks.instances[0]?.refreshAccessToken()).resolves.toBe( |
| 128 | + "refreshed-token", |
| 129 | + ); |
| 130 | + expect(mocks.authState.refreshAccessToken).toHaveBeenCalledOnce(); |
| 131 | + }); |
| 132 | + |
| 133 | + it("shares one refresh across concurrent client retries", async () => { |
| 134 | + let resolveRefresh: (() => void) | undefined; |
| 135 | + mocks.authState.refreshAccessToken.mockImplementation( |
| 136 | + () => |
| 137 | + new Promise<void>((resolve) => { |
| 138 | + resolveRefresh = () => { |
| 139 | + mocks.authState.oauthAccessToken = "refreshed-token"; |
| 140 | + resolve(); |
| 141 | + }; |
| 142 | + }), |
| 143 | + ); |
| 144 | + const { createPostHogApiClient } = await import("./posthogApiClient"); |
| 145 | + createPostHogApiClient(); |
| 146 | + |
| 147 | + const refreshes = [ |
| 148 | + mocks.instances[0]?.refreshAccessToken(), |
| 149 | + mocks.instances[0]?.refreshAccessToken(), |
| 150 | + ]; |
| 151 | + expect(mocks.authState.refreshAccessToken).toHaveBeenCalledOnce(); |
| 152 | + resolveRefresh?.(); |
| 153 | + |
| 154 | + await expect(Promise.all(refreshes)).resolves.toEqual([ |
| 155 | + "refreshed-token", |
| 156 | + "refreshed-token", |
| 157 | + ]); |
| 158 | + }); |
| 159 | +}); |
| 160 | + |
| 161 | +describe("getPostHogApiClient", () => { |
| 162 | + it("reuses the regional client and updates its project", async () => { |
| 163 | + const { getPostHogApiClient } = await import("./posthogApiClient"); |
| 164 | + |
| 165 | + const first = getPostHogApiClient(); |
| 166 | + mocks.authState.projectId = 456; |
| 167 | + const second = getPostHogApiClient(); |
| 168 | + |
| 169 | + expect(second).toBe(first); |
| 170 | + expect(mocks.instances).toHaveLength(1); |
| 171 | + expect(mocks.instances[0]?.setTeamId).toHaveBeenCalledWith(456); |
| 172 | + }); |
| 173 | + |
| 174 | + it("creates a new client when the cloud region changes", async () => { |
| 175 | + const { getPostHogApiClient } = await import("./posthogApiClient"); |
| 176 | + |
| 177 | + const first = getPostHogApiClient(); |
| 178 | + mocks.authState.cloudRegion = "eu"; |
| 179 | + mocks.authState.getCloudUrlFromRegion.mockReturnValue( |
| 180 | + "https://eu.posthog.com", |
| 181 | + ); |
| 182 | + const second = getPostHogApiClient(); |
| 183 | + |
| 184 | + expect(second).not.toBe(first); |
| 185 | + expect(mocks.instances.map(({ apiHost }) => apiHost)).toEqual([ |
| 186 | + "https://us.posthog.com", |
| 187 | + "https://eu.posthog.com", |
| 188 | + ]); |
| 189 | + }); |
| 190 | +}); |
0 commit comments