From 5c8e2f7515e8c89c843e8615e14a438454f073c5 Mon Sep 17 00:00:00 2001 From: dante01yoon Date: Sat, 19 Sep 2026 03:04:29 +0000 Subject: [PATCH 1/3] FE-2471 refactor(billing): one status transport for the cloud subscription store `performFetchSubscriptionStatus` in `useSubscription.ts` read billing status straight off `workspaceApi` with no rail branch, in parallel with `useWorkspaceBilling.fetchStatus` which already had one. This read now takes `useBillingReadRail()` too, so both readers share one transport. FE-2471 asks which of two options to take. Delegating this read to `useWorkspaceBilling.fetchStatus` is rejected: its response is what writes `setWorkspaceBillingRail`, which `useBillingRouting` reads to pick between the legacy and workspace adapters, so the read cannot sit inside one of the adapters it selects. --- .../composables/useSubscription.test.ts | 140 +++++++++++++----- .../composables/useSubscription.ts | 11 +- 2 files changed, 116 insertions(+), 35 deletions(-) diff --git a/src/platform/cloud/subscription/composables/useSubscription.test.ts b/src/platform/cloud/subscription/composables/useSubscription.test.ts index 885f466be9d..b45e33edfb7 100644 --- a/src/platform/cloud/subscription/composables/useSubscription.test.ts +++ b/src/platform/cloud/subscription/composables/useSubscription.test.ts @@ -6,6 +6,8 @@ import { computed, effectScope } from 'vue' import { useAuthActions } from '@/composables/auth/useAuthActions' import { useCurrentUser } from '@/composables/auth/useCurrentUser' +import type { BillingStatusResponse } from '@/platform/workspace/api/workspaceApi' +import type { BillingReadRail } from '@/platform/workspace/composables/useBillingReadRail' import { useSubscription } from '@/platform/cloud/subscription/composables/useSubscription' import { PENDING_SUBSCRIPTION_CHECKOUT_STORAGE_KEY } from '@/platform/cloud/subscription/utils/subscriptionCheckoutTracker' @@ -135,11 +137,65 @@ vi.mock( vi.mock(import('@/platform/workspace/api/workspaceApi'), () => ({ workspaceApi: { getBillingStatus: mockGetBillingStatus - } + }, + // What a failed rail read throws; only its message reaches the wrapper. + WorkspaceApiError: class extends Error {} +})) + +/** Null is the legacy client; a rail is what the SDK store would hand back. */ +const railState = vi.hoisted(() => ({ + rail: null as Pick | null })) +vi.mock( + import('@/platform/workspace/composables/useBillingReadRail'), + () => ({ useBillingReadRail: () => railState.rail }) +) vi.mock(import('@/services/dialogService')) +const mockReadStatus = vi.fn() + +/** + * The two clients the status read can go through, so the rows below pin one + * behaviour on both rather than one path's behaviour twice. + */ +const statusReadPaths = [ + { + reader: 'the workspace client', + select: () => { + railState.rail = null + }, + resolve: (status: Partial) => { + mockGetBillingStatus.mockResolvedValue(status) + }, + fail: () => { + mockGetBillingStatus.mockRejectedValue( + new Error('Subscription not found') + ) + }, + failure: 'Subscription not found', + idleReader: () => mockReadStatus + }, + { + reader: 'the SDK reader', + select: () => { + railState.rail = { readStatus: mockReadStatus } + }, + resolve: (status: Partial) => { + mockReadStatus.mockResolvedValue({ status: 'ok', value: status }) + }, + fail: () => { + mockReadStatus.mockResolvedValue({ + status: 'error', + code: 'ACCESS_DENIED', + httpStatus: 403 + }) + }, + failure: 'ACCESS_DENIED', + idleReader: () => mockGetBillingStatus + } +] + // Mock fetch global.fetch = vi.fn() @@ -171,6 +227,7 @@ describe('useSubscription', () => { setDistribution('cloud') mockLocalStorage.__reset() + railState.rail = null Object.assign(useAuthStore(), { userId: 'user-123' }) mockIsCloud.value = true Object.assign(useAuthStore(), { isInitialized: true }) @@ -291,50 +348,67 @@ describe('useSubscription', () => { }) describe('fetchStatus', () => { - it('should fetch subscription status successfully', async () => { - const mockStatus = { - is_active: true, - has_funds: true, - renewal_date: '2025-11-16', - team_credit_stop: null - } - - mockGetBillingStatus.mockResolvedValue(mockStatus) + it.for(statusReadPaths)( + 'publishes a status read through $reader and updates the workspace billing rail', + async (path) => { + const status = { + is_active: true, + has_funds: true, + renewal_date: '2025-11-16', + billing_rail: 'stripe' as const + } + path.select() + path.resolve(status) - useCurrentUser().isLoggedIn = computed(() => true) - const { fetchStatus } = useSubscriptionWithScope() + useCurrentUser().isLoggedIn = computed(() => true) + const { subscriptionStatus, fetchStatus } = useSubscriptionWithScope() - await fetchStatus() + await fetchStatus() - expect(mockGetBillingStatus).toHaveBeenCalledOnce() - }) + expect(subscriptionStatus.value).toEqual(status) + expect(mockSetWorkspaceBillingRail).toHaveBeenCalledWith( + 'workspace-123', + 'stripe' + ) + // One transport per read: the rail a read is on is the only client it + // asks, or the panels read one thing and the rail settled another. + expect(path.idleReader()).not.toHaveBeenCalled() + } + ) - it('should handle fetch errors gracefully', async () => { - mockGetBillingStatus.mockRejectedValue( - new Error('Subscription not found') - ) + it.for(statusReadPaths)( + 'reports a failed read through $reader in the same message', + async (path) => { + path.select() + path.fail() - const { fetchStatus } = useSubscriptionWithScope() + const { fetchStatus } = useSubscriptionWithScope() - await expect(fetchStatus()).rejects.toThrow( - 'Failed to fetch subscription status: Subscription not found' - ) - }) + await expect(fetchStatus()).rejects.toThrow( + `Failed to fetch subscription status: ${path.failure}` + ) + } + ) - it('updates the active workspace billing rail from status', async () => { + it('keeps the published status when a rail read is superseded', async () => { mockGetBillingStatus.mockResolvedValue({ is_active: true, - has_funds: true, - billing_rail: 'stripe' + has_funds: true }) - - const { fetchStatus } = useSubscriptionWithScope() + const { subscriptionStatus, fetchStatus } = useSubscriptionWithScope() await fetchStatus() - expect(mockSetWorkspaceBillingRail).toHaveBeenCalledWith( - 'workspace-123', - 'stripe' - ) + railState.rail = { readStatus: mockReadStatus } + mockReadStatus.mockResolvedValue({ + status: 'error', + code: 'SUPERSEDED' + }) + await expect(fetchStatus()).resolves.toBeNull() + + expect(subscriptionStatus.value).toEqual({ + is_active: true, + has_funds: true + }) }) it('does not apply the previous account response after an identity switch', async () => { diff --git a/src/platform/cloud/subscription/composables/useSubscription.ts b/src/platform/cloud/subscription/composables/useSubscription.ts index 370f024fb76..80825343174 100644 --- a/src/platform/cloud/subscription/composables/useSubscription.ts +++ b/src/platform/cloud/subscription/composables/useSubscription.ts @@ -20,6 +20,8 @@ import type { } from '@/platform/telemetry/types' import type { BillingStatusResponse } from '@/platform/workspace/api/workspaceApi' import { workspaceApi } from '@/platform/workspace/api/workspaceApi' +import { readOnRail } from '@/platform/workspace/composables/readOnRail' +import { useBillingReadRail } from '@/platform/workspace/composables/useBillingReadRail' import { useTeamWorkspaceStore } from '@/platform/workspace/stores/teamWorkspaceStore' import { AuthStoreError, useAuthStore } from '@/stores/authStore' import { useDialogService } from '@/services/dialogService' @@ -386,9 +388,12 @@ function useSubscriptionInternal() { ): Promise { if (!isCloud) return null - let statusData: BillingStatusResponse + const rail = useBillingReadRail() + let statusData: BillingStatusResponse | undefined try { - statusData = await workspaceApi.getBillingStatus() + statusData = rail + ? await readOnRail(rail.readStatus) + : await workspaceApi.getBillingStatus() } catch (error) { throw new AuthStoreError( t('toastMessages.failedToFetchSubscription', { @@ -396,6 +401,8 @@ function useSubscriptionInternal() { }) ) } + // A superseded read publishes nothing: the scope moved on under it. + if (statusData === undefined) return null if ( (authStore.userId ?? null) !== ownerId || workspaceStore.activeWorkspaceId !== workspaceId From 694ba04915060c727766bcea51b83ddd29a15524 Mon Sep 17 00:00:00 2001 From: dante01yoon Date: Sat, 19 Sep 2026 03:29:41 +0000 Subject: [PATCH 2/3] FE-2471 refactor(billing): extract the status read out of the publish guard `performFetchSubscriptionStatus` crossed fallow's CRAP threshold once the read gained a rail branch. Reading is its own concern: which client answers and how a failure is shaped now live in `readSubscriptionStatus`, leaving the caller with the scope guards and the publish. --- .../composables/useSubscription.ts | 26 +++++++++++++------ 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/src/platform/cloud/subscription/composables/useSubscription.ts b/src/platform/cloud/subscription/composables/useSubscription.ts index 80825343174..d9dd2ba6ace 100644 --- a/src/platform/cloud/subscription/composables/useSubscription.ts +++ b/src/platform/cloud/subscription/composables/useSubscription.ts @@ -382,16 +382,17 @@ function useSubscriptionInternal() { return fetchPromise } - async function performFetchSubscriptionStatus( - ownerId: string | null, - workspaceId: string | null - ): Promise { - if (!isCloud) return null - + /** + * The status read on whichever rail is on, in the failure shape the legacy + * client threw in. The rail is taken before the read and held for it: a flag + * flip mid-read must not start on one client and publish through the other. + */ + async function readSubscriptionStatus(): Promise< + BillingStatusResponse | undefined + > { const rail = useBillingReadRail() - let statusData: BillingStatusResponse | undefined try { - statusData = rail + return rail ? await readOnRail(rail.readStatus) : await workspaceApi.getBillingStatus() } catch (error) { @@ -401,6 +402,15 @@ function useSubscriptionInternal() { }) ) } + } + + async function performFetchSubscriptionStatus( + ownerId: string | null, + workspaceId: string | null + ): Promise { + if (!isCloud) return null + + const statusData = await readSubscriptionStatus() // A superseded read publishes nothing: the scope moved on under it. if (statusData === undefined) return null if ( From fe7913af2456abaee53b0dcbfdebeada3cfc9480 Mon Sep 17 00:00:00 2001 From: dante01yoon Date: Sat, 19 Sep 2026 04:11:29 +0000 Subject: [PATCH 3/3] FE-2471 test(billing): type the SDK status-reader mock against the rail contract `vi.fn()` checks the ok, ACCESS_DENIED and SUPERSEDED fixtures against `BillingResult`, so a fixture that drifts from the contract fails to compile rather than passing a test the rail could never satisfy. A `buildStatus` builder supplies the required fields the typed `ok` value now needs. Addresses review feedback on #18092. --- .../composables/useSubscription.test.ts | 37 +++++++++++-------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/src/platform/cloud/subscription/composables/useSubscription.test.ts b/src/platform/cloud/subscription/composables/useSubscription.test.ts index b45e33edfb7..f49d29b9ef8 100644 --- a/src/platform/cloud/subscription/composables/useSubscription.test.ts +++ b/src/platform/cloud/subscription/composables/useSubscription.test.ts @@ -153,7 +153,19 @@ vi.mock( vi.mock(import('@/services/dialogService')) -const mockReadStatus = vi.fn() +const mockReadStatus = vi.fn() + +const buildStatus = ( + overrides: Partial = {} +): BillingStatusResponse => ({ + is_active: true, + has_funds: true, + max_seats: 1, + occupied_seats: 1, + scheduled_change: null, + team_credit_stop: null, + ...overrides +}) /** * The two clients the status read can go through, so the rows below pin one @@ -165,7 +177,7 @@ const statusReadPaths = [ select: () => { railState.rail = null }, - resolve: (status: Partial) => { + resolve: (status: BillingStatusResponse) => { mockGetBillingStatus.mockResolvedValue(status) }, fail: () => { @@ -181,7 +193,7 @@ const statusReadPaths = [ select: () => { railState.rail = { readStatus: mockReadStatus } }, - resolve: (status: Partial) => { + resolve: (status: BillingStatusResponse) => { mockReadStatus.mockResolvedValue({ status: 'ok', value: status }) }, fail: () => { @@ -351,12 +363,10 @@ describe('useSubscription', () => { it.for(statusReadPaths)( 'publishes a status read through $reader and updates the workspace billing rail', async (path) => { - const status = { - is_active: true, - has_funds: true, + const status = buildStatus({ renewal_date: '2025-11-16', - billing_rail: 'stripe' as const - } + billing_rail: 'stripe' + }) path.select() path.resolve(status) @@ -391,10 +401,8 @@ describe('useSubscription', () => { ) it('keeps the published status when a rail read is superseded', async () => { - mockGetBillingStatus.mockResolvedValue({ - is_active: true, - has_funds: true - }) + const published = buildStatus({ renewal_date: '2025-11-16' }) + mockGetBillingStatus.mockResolvedValue(published) const { subscriptionStatus, fetchStatus } = useSubscriptionWithScope() await fetchStatus() @@ -405,10 +413,7 @@ describe('useSubscription', () => { }) await expect(fetchStatus()).resolves.toBeNull() - expect(subscriptionStatus.value).toEqual({ - is_active: true, - has_funds: true - }) + expect(subscriptionStatus.value).toEqual(published) }) it('does not apply the previous account response after an identity switch', async () => {