Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

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<ReturnType<BillingReadRail['readStatus']>>
) => 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({
Expand Down
31 changes: 26 additions & 5 deletions src/platform/cloud/subscription/composables/useSubscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
Expand Down Expand Up @@ -614,7 +619,10 @@ function useSubscriptionInternal() {
| 'deadline'

const canRecoverPendingCheckout = () =>
isCloud && isLoggedIn.value && hasOwnedPendingCheckoutAttempt()
isCloud &&
isLoggedIn.value &&
!awaitingSessionWorkspace.value &&
hasOwnedPendingCheckoutAttempt()

const hasOwnedPendingCheckoutAttempt = () => {
const attempt = getPendingSubscriptionCheckoutAttempt()
Expand Down Expand Up @@ -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() &&
Expand Down Expand Up @@ -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
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
] as const,
async ([authInitialized, loggedIn, , awaitingWorkspace]) => {
if (!authInitialized) {
return
}

if (loggedIn && isCloud) {
if (awaitingWorkspace) return
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
try {
if (hasOwnedPendingCheckoutAttempt()) {
await recoverPendingSubscriptionCheckout('bootstrap')
Expand Down
Loading