diff --git a/src/platform/cloud/subscription/composables/useSubscription.test.ts b/src/platform/cloud/subscription/composables/useSubscription.test.ts index bbc49db0e79..ac892650373 100644 --- a/src/platform/cloud/subscription/composables/useSubscription.test.ts +++ b/src/platform/cloud/subscription/composables/useSubscription.test.ts @@ -2506,6 +2506,105 @@ describe('useSubscription', () => { }) }) + describe('boot read before a workspace is selected', () => { + const SESSION_USER = { id: 'user-123', email: 'user@example.com' } + const flushPromises = () => new Promise((resolve) => setTimeout(resolve)) + + beforeEach(() => { + railState.rail = { readStatus: mockReadStatus } + mockReadStatus.mockImplementation(async () => + useTeamWorkspaceStore().activeWorkspaceId + ? { status: 'ok', value: buildStatus() } + : { status: 'error', code: 'NOT_AUTHENTICATED' } + ) + Object.assign(useTeamWorkspaceStore(), { activeWorkspaceId: null }) + }) + + afterEach(() => { + Object.assign(useAuthStore(), { sessionUser: undefined }) + }) + + it('waits on a web session until the workspace is selected', async () => { + const consoleError = vi + .spyOn(console, 'error') + .mockImplementation(() => {}) + Object.assign(useAuthStore(), { sessionUser: SESSION_USER }) + mockIsLoggedIn.value = true + + const { subscriptionStatus, isInitialized } = useSubscriptionWithScope() + await flushPromises() + + expect(mockReadStatus).not.toHaveBeenCalled() + expect(isInitialized.value).toBe(false) + + Object.assign(useTeamWorkspaceStore(), { + activeWorkspaceId: 'workspace-123' + }) + await flushPromises() + + expect(mockReadStatus).toHaveBeenCalledOnce() + expect(subscriptionStatus.value).toEqual(buildStatus()) + expect(isInitialized.value).toBe(true) + expect(consoleError).not.toHaveBeenCalled() + }) + + it('recovers a pending checkout on a web session only once the workspace is selected', async () => { + localStorage.setItem( + PENDING_SUBSCRIPTION_CHECKOUT_STORAGE_KEY, + JSON.stringify({ + attempt_id: 'attempt-session-boot', + started_at_ms: Date.now(), + tier: 'standard', + cycle: 'monthly', + checkout_type: 'new' + }) + ) + let resolveRead!: ( + result: Awaited> + ) => void + mockReadStatus.mockReturnValue( + new Promise((resolve) => { + resolveRead = resolve + }) + ) + vi.spyOn(console, 'error').mockImplementation(() => {}) + Object.assign(useAuthStore(), { sessionUser: SESSION_USER }) + mockIsLoggedIn.value = true + + const { subscriptionStatus, isInitialized } = useSubscriptionWithScope() + window.dispatchEvent(new Event('pageshow')) + await flushPromises() + + expect(mockReadStatus).not.toHaveBeenCalled() + + Object.assign(useTeamWorkspaceStore(), { + activeWorkspaceId: 'workspace-123' + }) + await flushPromises() + + expect(mockReadStatus).toHaveBeenCalledOnce() + expect(isInitialized.value).toBe(false) + + resolveRead({ status: 'ok', value: buildStatus() }) + await flushPromises() + + expect(subscriptionStatus.value).toEqual(buildStatus()) + expect(isInitialized.value).toBe(true) + }) + + it('reads at once without a web session, as before', async () => { + mockReadStatus.mockResolvedValue({ status: 'ok', value: buildStatus() }) + mockIsLoggedIn.value = true + + const { subscriptionStatus, isInitialized } = useSubscriptionWithScope() + await flushPromises() + + expect(mockReadStatus).toHaveBeenCalledOnce() + expect(subscriptionStatus.value).toEqual(buildStatus()) + expect(isInitialized.value).toBe(true) + }) + }) + describe('requireActiveSubscription', () => { it('should not show dialog when subscription is active', async () => { mockGetBillingStatus.mockResolvedValue({ diff --git a/src/platform/cloud/subscription/composables/useSubscription.ts b/src/platform/cloud/subscription/composables/useSubscription.ts index 347ba6235be..90cde4d7376 100644 --- a/src/platform/cloud/subscription/composables/useSubscription.ts +++ b/src/platform/cloud/subscription/composables/useSubscription.ts @@ -123,6 +123,11 @@ function useSubscriptionInternal() { const { isLoggedIn } = useCurrentUser() + // Web-session billing reads need the workspace the gate selects after sign-in. + const awaitingSessionWorkspace = computed( + () => !!authStore.sessionUser && !workspaceStore.activeWorkspaceId + ) + const isCancelled = computed(() => { return !!subscriptionStatus.value?.cancel_at }) @@ -614,7 +619,10 @@ function useSubscriptionInternal() { | 'deadline' const canRecoverPendingCheckout = () => - isCloud && isLoggedIn.value && hasOwnedPendingCheckoutAttempt() + isCloud && + isLoggedIn.value && + !awaitingSessionWorkspace.value && + hasOwnedPendingCheckoutAttempt() const hasOwnedPendingCheckoutAttempt = () => { const attempt = getPendingSubscriptionCheckoutAttempt() @@ -837,9 +845,16 @@ function useSubscriptionInternal() { } watch( - () => [authStore.userId, workspaceStore.activeWorkspaceId] as const, - ([ownerId, workspaceId]) => { + () => + [ + authStore.userId, + workspaceStore.activeWorkspaceId, + awaitingSessionWorkspace.value + ] as const, + ([ownerId, workspaceId], [, , wasAwaitingWorkspace]) => { observeStatusScope(ownerId ?? null, workspaceId) + // The bootstrap watcher reads for the session's first workspace. + if (wasAwaitingWorkspace) return if ( workspaceId && hasPendingSubscriptionCheckoutAttempt() && @@ -1009,13 +1024,19 @@ function useSubscriptionInternal() { watch( () => - [authStore.isInitialized, isLoggedIn.value, authStore.userId] as const, - async ([authInitialized, loggedIn]) => { + [ + authStore.isInitialized, + isLoggedIn.value, + authStore.userId, + awaitingSessionWorkspace.value + ] as const, + async ([authInitialized, loggedIn, , awaitingWorkspace]) => { if (!authInitialized) { return } if (loggedIn && isCloud) { + if (awaitingWorkspace) return try { if (hasOwnedPendingCheckoutAttempt()) { await recoverPendingSubscriptionCheckout('bootstrap')