From 108cfeda691e7f23af3a34137e316e63783b4398 Mon Sep 17 00:00:00 2001 From: Benjamin Lu Date: Mon, 19 Oct 2026 23:08:32 +0000 Subject: [PATCH] fix: refresh balance when the profile menu opens (#21392) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Summary Fixes the stale profile-menu balance: request fresh credits when the workspace menu contents mount, on both first opening and reopening. The overlay migration in #16272 made `show` fire before those contents exist, so the parent's optional refresh call silently did nothing. ## Verification - `pnpm test:unit src/platform/workspace/components/CurrentUserPopoverWorkspace.test.ts src/components/topbar/CurrentUserButton.test.ts src/components/topbar/CurrentUserPopoverLegacy.test.ts` — 76 passed. Personal/team cases exercise the real button, menu content, and popover; both new regression cases fail with the original Vue files. The test substitutes the real content synchronously to avoid happy-dom's empty async focus behavior. - `pnpm lint:unstaged:fix`, formatting, commit hooks, and push hooks passed, including application typecheck, staged server-fact checks, dependency audit, and knip. The advisory fallow audit flags cross-domain imports in the integration test; no production boundary changes or suppressions were added. - Chrome, disposable local responses with the real async menu: first open sends one request and displays 422 credits; closing sends none; reopening sends the second request and displays 158. No real billing account or production data used. Screenshots are retained in the owning Codex chat; GitHub attachment upload was declined by the browser approval gate. ## Decisions made without Ben - Move refresh ownership into the mounted contents, matching the legacy menu. Waiting one Vue tick in the parent would still depend on async component timing; changing the shared popover's event timing would affect unrelated consumers. - Keep the integration test beside the profile button, with real menu/popover and mocked billing/dialog boundaries, despite advisory cross-domain test-import warnings; this directly catches the mount-timing regression without changing production architecture. - Keep the existing account-actions-only guard. Leave shared overlays, billing APIs, and loading-to-ready behavior outside this focused fix. No release operations or backports performed; any 1.57 backport remains Ben's decision. ## Risk Low: restores the intended read-only balance request per open. Mocked component tests and local Chrome verification do not prove either live billing backend or production network timing. Refresh relies on the current popover unmounting its contents when closed. ## How to revert Revert this PR; no data or settings migration. ## Claude review 0 blocking findings at `8d35b555fa9a`; recommendation: merge. The two optional observations (loading-to-ready refresh while already open and existing rejection handling shared with the legacy menu) remain outside this fix. --- .../topbar/CurrentUserButton.test.ts | 73 ++++++++++++++++++- src/components/topbar/CurrentUserButton.vue | 10 --- .../CurrentUserPopoverWorkspace.test.ts | 1 + .../CurrentUserPopoverWorkspace.vue | 8 +- 4 files changed, 73 insertions(+), 19 deletions(-) diff --git a/src/components/topbar/CurrentUserButton.test.ts b/src/components/topbar/CurrentUserButton.test.ts index a62d9d7cc55..54bd3d4a7b1 100644 --- a/src/components/topbar/CurrentUserButton.test.ts +++ b/src/components/topbar/CurrentUserButton.test.ts @@ -1,9 +1,13 @@ import userEvent from '@testing-library/user-event' -import { render, screen } from '@testing-library/vue' +import { render, screen, waitFor } from '@testing-library/vue' import { beforeEach, describe, expect, it, vi } from 'vitest' -import { computed, defineComponent, h } from 'vue' +import type { Component } from 'vue' +import { computed, defineComponent, h, ref } from 'vue' import { createI18n } from 'vue-i18n' +import { useBillingContext } from '@/composables/billing/useBillingContext' +import type { BalanceInfo } from '@/composables/billing/types' +import CurrentUserPopoverWorkspace from '@/platform/workspace/components/CurrentUserPopoverWorkspace.vue' import { useCurrentUser } from '@/composables/auth/useCurrentUser' import enMessages from '@/locales/en/main.json' with { type: 'json' } import { useTeamWorkspaceStore } from '@/platform/workspace/stores/teamWorkspaceStore' @@ -20,6 +24,16 @@ vi.mock(import('@/platform/distribution/types'), () => ({ })) vi.mock(import('@/composables/auth/useCurrentUser')) +vi.mock(import('@/composables/billing/useBillingContext')) +vi.mock(import('@/platform/workspace/composables/useWorkspaceUI')) +vi.mock(import('@/platform/workspace/composables/useBillingCapabilities')) +vi.mock( + import('@/platform/cloud/subscription/composables/useSubscriptionDialog') +) +vi.mock(import('@/platform/settings/composables/useSettingsDialog')) +vi.mock(import('@/composables/billing/useBillingDialogs')) +vi.mock(import('@/platform/workspace/composables/useWorkspaceDialogs')) +vi.mock(import('@/platform/telemetry')) // Mock the WorkspaceProfilePic component vi.mock( @@ -85,7 +99,9 @@ describe('CurrentUserButton', () => { mockIsCloud.value = false }) - function renderComponent() { + function renderComponent( + workspacePopover: Component = CurrentUserPopoverWorkspaceStub + ) { const user = userEvent.setup() const i18n = createI18n({ legacy: false, @@ -97,7 +113,10 @@ describe('CurrentUserButton', () => { global: { plugins: [i18n], stubs: { - CurrentUserPopoverWorkspace: CurrentUserPopoverWorkspaceStub + CurrentUserPopoverWorkspace: workspacePopover, + WorkspaceSwitcherPopover: true, + SubscribeButton: true, + DeploymentSwitcher: true } } }) @@ -105,6 +124,52 @@ describe('CurrentUserButton', () => { return { user, ...result } } + it.for(['personal', 'team'] as const)( + 'refreshes the %s balance on first opening and reopening the profile menu', + async (workspaceType) => { + mockIsCloud.value = true + const balance = ref({ amountMicros: 100, currency: 'USD' }) + const billing = useBillingContext() + billing.balance = computed(() => balance.value) + billing.billingStatus = computed(() => 'paid') + billing.canAccessSubscriptionFeatures = computed(() => true) + vi.mocked(useBillingContext).mockReturnValue(billing) + vi.mocked(billing.fetchBalance) + .mockImplementationOnce(async () => { + balance.value = { amountMicros: 200, currency: 'USD' } + }) + .mockImplementationOnce(async () => { + balance.value = { amountMicros: 75, currency: 'USD' } + }) + useTeamWorkspaceStore().initState = 'ready' + Object.assign(useTeamWorkspaceStore(), { + workspaceName: `${workspaceType} workspace`, + isInPersonalWorkspace: workspaceType === 'personal' + }) + const { user } = renderComponent(CurrentUserPopoverWorkspace) + + expect(billing.fetchBalance).not.toHaveBeenCalled() + await user.click(screen.getByRole('button', { name: 'Current user' })) + + expect(await screen.findByText('422')).toBeInTheDocument() + expect(billing.fetchBalance).toHaveBeenCalledOnce() + + await user.click(screen.getByRole('button', { name: 'Current user' })) + + await waitFor(() => + expect( + screen.queryByTestId('credits-info-button') + ).not.toBeInTheDocument() + ) + expect(billing.fetchBalance).toHaveBeenCalledOnce() + + await user.click(screen.getByRole('button', { name: 'Current user' })) + + expect(await screen.findByText('158')).toBeInTheDocument() + expect(billing.fetchBalance).toHaveBeenCalledTimes(2) + } + ) + it('toggles popover on button click', async () => { const { user } = renderComponent() diff --git a/src/components/topbar/CurrentUserButton.vue b/src/components/topbar/CurrentUserButton.vue index db9c4aa6d96..19f1a95746d 100644 --- a/src/components/topbar/CurrentUserButton.vue +++ b/src/components/topbar/CurrentUserButton.vue @@ -43,11 +43,9 @@ showWorkspacePopover ? 'w-fit max-w-96 min-w-80' : 'w-80' ) " - @show="onPopoverShow" > @@ -111,15 +109,7 @@ const workspaceName = computed(() => { }) const popover = ref | null>(null) -const workspacePopoverContent = ref<{ - refreshBalance: () => void -} | null>(null) - const closePopover = () => { popover.value?.hide() } - -const onPopoverShow = () => { - workspacePopoverContent.value?.refreshBalance() -} diff --git a/src/platform/workspace/components/CurrentUserPopoverWorkspace.test.ts b/src/platform/workspace/components/CurrentUserPopoverWorkspace.test.ts index 7353b43bcef..6e2968357a6 100644 --- a/src/platform/workspace/components/CurrentUserPopoverWorkspace.test.ts +++ b/src/platform/workspace/components/CurrentUserPopoverWorkspace.test.ts @@ -273,6 +273,7 @@ describe('CurrentUserPopoverWorkspace', () => { expect( screen.queryByTestId('workspace-settings-menu-item') ).not.toBeInTheDocument() + expect(useBillingContext().fetchBalance).not.toHaveBeenCalled() }) describe('with a Desktop host session', () => { diff --git a/src/platform/workspace/components/CurrentUserPopoverWorkspace.vue b/src/platform/workspace/components/CurrentUserPopoverWorkspace.vue index 5d9cff0175b..ade9bf7d80e 100644 --- a/src/platform/workspace/components/CurrentUserPopoverWorkspace.vue +++ b/src/platform/workspace/components/CurrentUserPopoverWorkspace.vue @@ -257,7 +257,7 @@ import { BILLING_ACTION_NAME } from '@/platform/telemetry/billingActionNames' import { onClickOutside } from '@vueuse/core' import { storeToRefs } from 'pinia' -import { computed, ref, useTemplateRef } from 'vue' +import { computed, onMounted, ref, useTemplateRef } from 'vue' import { useI18n } from 'vue-i18n' import { formatCreditsFromCents } from '@/base/credits/comfyCredits' @@ -472,9 +472,7 @@ const closeWorkspaceSwitcherFromKeyboard = () => { isWorkspaceNameTooltipOpen.value = false } -const refreshBalance = () => { +onMounted(() => { if (!accountActionsOnly) void fetchBalance() -} - -defineExpose({ refreshBalance }) +})