Skip to content

Commit 679dd13

Browse files
dante01yoonampagent
andcommitted
fix(billing): scope operation completion to workspace
Amp-Thread-ID: https://ampcode.com/threads/T-01a01a57-6259-75bd-bb52-c149e0f7bb9c Co-authored-by: Amp <amp@ampcode.com>
1 parent 6d0ad50 commit 679dd13

2 files changed

Lines changed: 36 additions & 0 deletions

File tree

src/platform/workspace/stores/billingOperationStore.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1448,6 +1448,32 @@ describe('billingOperationStore', () => {
14481448
})
14491449
})
14501450

1451+
it('does not update a new workspace after cancellation completes', async () => {
1452+
let finishRefresh: () => void = () => {}
1453+
mockFetchStatus.mockImplementationOnce(
1454+
() =>
1455+
new Promise<void>((resolve) => {
1456+
finishRefresh = resolve
1457+
})
1458+
)
1459+
vi.mocked(workspaceApi.getBillingOpStatus).mockResolvedValue({
1460+
id: 'op-1',
1461+
status: 'succeeded',
1462+
started_at: new Date().toISOString()
1463+
})
1464+
1465+
const store = useBillingOperationStore()
1466+
const terminal = store.startOperation('op-1', 'cancel')
1467+
await vi.advanceTimersByTimeAsync(0)
1468+
expect(mockFetchStatus).toHaveBeenCalledOnce()
1469+
1470+
mockActiveWorkspaceId.value = 'workspace-2'
1471+
finishRefresh()
1472+
await terminal
1473+
1474+
expect(mockUpdateActiveWorkspace).not.toHaveBeenCalled()
1475+
})
1476+
14511477
it('resolves the terminal outcome even when the post-success refresh fails', async () => {
14521478
mockFetchStatus.mockRejectedValueOnce(new Error('refresh failed'))
14531479
vi.mocked(workspaceApi.getBillingOpStatus).mockResolvedValue({

src/platform/workspace/stores/billingOperationStore.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,11 @@ export const useBillingOperationStore = defineStore('billingOperation', () => {
404404
})
405405
}
406406

407+
if (operation.workspaceId !== workspaceStore.activeWorkspaceId) {
408+
resolveTerminal(opId)
409+
return
410+
}
411+
407412
const billingContext = useBillingContext()
408413
if (operation.type === 'subscription') {
409414
await Promise.allSettled([billingContext.reconcileSubscriptionSuccess()])
@@ -414,6 +419,11 @@ export const useBillingOperationStore = defineStore('billingOperation', () => {
414419
])
415420
}
416421

422+
if (operation.workspaceId !== workspaceStore.activeWorkspaceId) {
423+
resolveTerminal(opId)
424+
return
425+
}
426+
417427
if (operation.type === 'cancel') {
418428
useTeamWorkspaceStore().updateActiveWorkspace({ isSubscribed: false })
419429
resolveTerminal(opId)

0 commit comments

Comments
 (0)