From 95afaa42a8da1e0fb40dbb62dde61671c602add2 Mon Sep 17 00:00:00 2001 From: dante01yoon Date: Fri, 13 Nov 2026 21:44:23 -0500 Subject: [PATCH 1/2] fix(billing): re-read the local Billing tab when the user returns to it The Billing tab read plan, balance and capabilities only on mount, and again on return only after this tab had opened a billing handoff or a Plans link. A payment made in another tab, browser or device left the open tab showing the old plan and credits until settings was reopened. On local the tab now arms registerRefreshOnReturn, the return signal every hosted billing opener already uses: window focus, which is what Desktop raises on app focus, and a visible visibilitychange, counted once per return. It runs the account refresh and the capability revalidation the handoff return runs, which keep the rendered data in place until the new read lands. It skips while billing has not initialized, so it never doubles the first load. Cloud is unchanged. Part of FRONT-3856 --- .../dialogs/settings/BillingTab.test.ts | 21 ++++ .../dialogs/settings/BillingTab.vue | 3 + .../dialogs/settings/BillingTabLegacy.test.ts | 21 ++++ .../dialogs/settings/BillingTabLegacy.vue | 3 + .../useRefreshBillingOnReturn.test.ts | 118 ++++++++++++++++++ .../composables/useRefreshBillingOnReturn.ts | 23 ++++ 6 files changed, 189 insertions(+) create mode 100644 src/platform/billing/composables/useRefreshBillingOnReturn.test.ts create mode 100644 src/platform/billing/composables/useRefreshBillingOnReturn.ts diff --git a/src/platform/account/components/dialogs/settings/BillingTab.test.ts b/src/platform/account/components/dialogs/settings/BillingTab.test.ts index 2d6ed0e8999..62a8f09b47a 100644 --- a/src/platform/account/components/dialogs/settings/BillingTab.test.ts +++ b/src/platform/account/components/dialogs/settings/BillingTab.test.ts @@ -2300,3 +2300,24 @@ describe('BillingTab banner stack parity with selectBillingBanners', () => { } ) }) + +describe('BillingTab refresh on return', () => { + it.for([ + { where: 'local', isCloud: false, reads: 1 }, + { where: 'Cloud', isCloud: true, reads: 0 } + ])( + 're-reads capabilities $reads time(s) when the user returns to the tab on $where', + ({ isCloud, reads }) => { + distribution.isCloud = isCloud + onTestFinished(() => { + distribution.isCloud = false + }) + const { refresh } = arrange() + + window.dispatchEvent(new Event('blur')) + window.dispatchEvent(new Event('focus')) + + expect(refresh).toHaveBeenCalledTimes(reads) + } + ) +}) diff --git a/src/platform/account/components/dialogs/settings/BillingTab.vue b/src/platform/account/components/dialogs/settings/BillingTab.vue index 9759941f7c0..78d50cd59f7 100644 --- a/src/platform/account/components/dialogs/settings/BillingTab.vue +++ b/src/platform/account/components/dialogs/settings/BillingTab.vue @@ -219,6 +219,7 @@ import { useBillingCreditsCard } from '@/platform/billing/components/billingTab/ import { useBillingPlanRow } from '@/platform/billing/components/billingTab/useBillingPlanRow' import { useBillingAutoTopUpCard } from '@/platform/billing/composables/useBillingAutoTopUpCard' import { useBillingCapabilities } from '@/platform/billing/composables/useBillingCapabilities' +import { useRefreshBillingOnReturn } from '@/platform/billing/composables/useRefreshBillingOnReturn' import { useTopUpPlanCreditCopy } from '@/platform/billing/composables/useTopUpPlanCreditCopy' import { usePausedBanner } from '@/platform/billing/composables/usePausedBanner' import { useTopupCheckoutBanner } from '@/platform/billing/composables/useTopupCheckoutBanner' @@ -389,4 +390,6 @@ onMounted(() => { } void capabilities.initialize() }) + +useRefreshBillingOnReturn() diff --git a/src/platform/account/components/dialogs/settings/BillingTabLegacy.test.ts b/src/platform/account/components/dialogs/settings/BillingTabLegacy.test.ts index 2d2fca9fe96..223ea9f78b0 100644 --- a/src/platform/account/components/dialogs/settings/BillingTabLegacy.test.ts +++ b/src/platform/account/components/dialogs/settings/BillingTabLegacy.test.ts @@ -2083,3 +2083,24 @@ describe('BillingTab', () => { }) }) }) + +describe('BillingTab refresh on return', () => { + it.for([ + { where: 'local', isCloud: false, reads: 1 }, + { where: 'Cloud', isCloud: true, reads: 0 } + ])( + 're-reads capabilities $reads time(s) when the user returns to the tab on $where', + ({ isCloud, reads }) => { + distribution.isCloud = isCloud + onTestFinished(() => { + distribution.isCloud = false + }) + const { refresh } = arrange() + + window.dispatchEvent(new Event('blur')) + window.dispatchEvent(new Event('focus')) + + expect(refresh).toHaveBeenCalledTimes(reads) + } + ) +}) diff --git a/src/platform/account/components/dialogs/settings/BillingTabLegacy.vue b/src/platform/account/components/dialogs/settings/BillingTabLegacy.vue index a36733288b1..fa3f0347b8a 100644 --- a/src/platform/account/components/dialogs/settings/BillingTabLegacy.vue +++ b/src/platform/account/components/dialogs/settings/BillingTabLegacy.vue @@ -208,6 +208,7 @@ import { useBillingCreditsCard } from '@/platform/billing/legacy160/platform/bil import { useBillingPlanRow } from '@/platform/billing/legacy160/platform/billing/components/billingTab/useBillingPlanRow' import { useBillingAutoTopUpCard } from '@/platform/billing/legacy160/platform/billing/composables/useBillingAutoTopUpCard' import { useBillingCapabilities } from '@/platform/billing/composables/useBillingCapabilities' +import { useRefreshBillingOnReturn } from '@/platform/billing/composables/useRefreshBillingOnReturn' import { useTopUpPlanCreditCopy } from '@/platform/billing/legacy160/platform/billing/composables/useTopUpPlanCreditCopy' import { usePausedBanner } from '@/platform/billing/composables/usePausedBanner' import { useTopupCheckoutBanner } from '@/platform/billing/legacy160/platform/billing/composables/useTopupCheckoutBanner' @@ -330,4 +331,6 @@ onMounted(() => { } void capabilities.initialize() }) + +useRefreshBillingOnReturn() diff --git a/src/platform/billing/composables/useRefreshBillingOnReturn.test.ts b/src/platform/billing/composables/useRefreshBillingOnReturn.test.ts new file mode 100644 index 00000000000..9fcc246b57f --- /dev/null +++ b/src/platform/billing/composables/useRefreshBillingOnReturn.test.ts @@ -0,0 +1,118 @@ +import { fromPartial } from '@total-typescript/shoehorn' +import { describe, expect, it, onTestFinished, vi } from 'vitest' +import { effectScope, ref } from 'vue' + +import { onBillingRefresh } from '@/platform/billing/billingRefresh' +import { useBillingCapabilities } from '@/platform/billing/composables/useBillingCapabilities' +import { useBillingContext } from '@/platform/billing/composables/useBillingContext' +import { useRefreshBillingOnReturn } from '@/platform/billing/composables/useRefreshBillingOnReturn' + +const distribution = vi.hoisted(() => ({ isCloud: false })) +vi.mock(import('@/platform/distribution/types'), () => ({ + get isCloud() { + return distribution.isCloud + } +})) +vi.mock(import('@/platform/billing/composables/useBillingContext')) +vi.mock(import('@/platform/billing/composables/useBillingCapabilities')) + +const leave = () => window.dispatchEvent(new Event('blur')) +const comeBack = () => window.dispatchEvent(new Event('focus')) + +function openBillingTab({ isCloud = false, isInitialized = true } = {}) { + distribution.isCloud = isCloud + onTestFinished(() => { + distribution.isCloud = false + }) + const settles: Array<() => void> = [] + const readAccount = vi.fn( + () => + new Promise((resolve) => { + settles.push(resolve) + }) + ) + onTestFinished(onBillingRefresh(readAccount)) + const readCapabilities = vi.fn(async () => {}) + vi.mocked(useBillingContext).mockReturnValue( + fromPartial({ isInitialized: ref(isInitialized) }) + ) + vi.mocked(useBillingCapabilities).mockReturnValue( + fromPartial({ refresh: readCapabilities }) + ) + const scope = effectScope() + scope.run(useRefreshBillingOnReturn) + onTestFinished(() => scope.stop()) + const settle = async (index: number) => { + settles[index]() + await new Promise((resolve) => setTimeout(resolve)) + } + return { readAccount, readCapabilities, settle, close: () => scope.stop() } +} + +describe('useRefreshBillingOnReturn', () => { + it('re-reads plan, balance and capabilities once when the user returns to a local tab', () => { + const { readAccount, readCapabilities } = openBillingTab() + + leave() + comeBack() + document.dispatchEvent(new Event('visibilitychange')) + + expect(readAccount).toHaveBeenCalledExactlyOnceWith('account') + expect(readCapabilities).toHaveBeenCalledOnce() + }) + + it('re-reads on every later return', async () => { + const { readAccount, settle } = openBillingTab() + + leave() + comeBack() + await settle(0) + leave() + comeBack() + + expect(readAccount).toHaveBeenCalledTimes(2) + }) + + it('reads nothing on Cloud', () => { + const { readAccount, readCapabilities } = openBillingTab({ isCloud: true }) + + leave() + comeBack() + + expect(readAccount).not.toHaveBeenCalled() + expect(readCapabilities).not.toHaveBeenCalled() + }) + + it('does not start a second read while the first is in flight', () => { + const { readAccount } = openBillingTab() + + leave() + comeBack() + leave() + comeBack() + + expect(readAccount).toHaveBeenCalledOnce() + }) + + it('leaves the first load alone while billing has not initialized', () => { + const { readAccount, readCapabilities } = openBillingTab({ + isInitialized: false + }) + + leave() + comeBack() + + expect(readAccount).not.toHaveBeenCalled() + expect(readCapabilities).not.toHaveBeenCalled() + }) + + it('stops reading once the tab is closed', () => { + const { readAccount, close } = openBillingTab() + + close() + leave() + comeBack() + + expect(readAccount).not.toHaveBeenCalled() + }) +}) diff --git a/src/platform/billing/composables/useRefreshBillingOnReturn.ts b/src/platform/billing/composables/useRefreshBillingOnReturn.ts new file mode 100644 index 00000000000..6fdb40c8726 --- /dev/null +++ b/src/platform/billing/composables/useRefreshBillingOnReturn.ts @@ -0,0 +1,23 @@ +import { onScopeDispose } from 'vue' + +import { refreshBilling } from '@/platform/billing/billingRefresh' +import { useBillingCapabilities } from '@/platform/billing/composables/useBillingCapabilities' +import { useBillingContext } from '@/platform/billing/composables/useBillingContext' +import { registerRefreshOnReturn } from '@/platform/billing/refreshOnReturn' +import { isCloud } from '@/platform/distribution/types' + +/** On local, re-reads plan, balance and capabilities on each return to this tab or window. */ +export function useRefreshBillingOnReturn(): void { + if (isCloud) return + const billing = useBillingContext() + const capabilities = useBillingCapabilities() + onScopeDispose( + registerRefreshOnReturn(async () => { + if (!billing.isInitialized.value) return + await Promise.allSettled([ + refreshBilling('account'), + capabilities.refresh() + ]) + }) + ) +} From 394b02b9cde72b8156e56e75e9c9dab4efa8bf2a Mon Sep 17 00:00:00 2001 From: dante01yoon Date: Sat, 14 Nov 2026 08:58:03 -0500 Subject: [PATCH 2/2] fix(billing): drop a queued return refresh when the Billing tab closes registerRefreshOnReturn kept its trailing flag after disposal, so closing settings while a return was queued behind a read in flight started one more account and capabilities read after the tab was gone. The disposer now clears the flag. Tests: the in-flight case holds the capabilities read on its own, so it fails if the callback stops awaiting it. The fixture configures the shared billing mocks instead of partial replacements, and the component suites keep only the local wiring row, since the composable suite owns the Cloud exclusion. --- .../dialogs/settings/BillingTab.test.ts | 22 ++----- .../dialogs/settings/BillingTabLegacy.test.ts | 22 ++----- .../useRefreshBillingOnReturn.test.ts | 65 ++++++++++++------- src/platform/billing/refreshOnReturn.test.ts | 13 ++++ src/platform/billing/refreshOnReturn.ts | 12 +--- 5 files changed, 70 insertions(+), 64 deletions(-) diff --git a/src/platform/account/components/dialogs/settings/BillingTab.test.ts b/src/platform/account/components/dialogs/settings/BillingTab.test.ts index 62a8f09b47a..8a4bc267361 100644 --- a/src/platform/account/components/dialogs/settings/BillingTab.test.ts +++ b/src/platform/account/components/dialogs/settings/BillingTab.test.ts @@ -2302,22 +2302,12 @@ describe('BillingTab banner stack parity with selectBillingBanners', () => { }) describe('BillingTab refresh on return', () => { - it.for([ - { where: 'local', isCloud: false, reads: 1 }, - { where: 'Cloud', isCloud: true, reads: 0 } - ])( - 're-reads capabilities $reads time(s) when the user returns to the tab on $where', - ({ isCloud, reads }) => { - distribution.isCloud = isCloud - onTestFinished(() => { - distribution.isCloud = false - }) - const { refresh } = arrange() + it('re-reads capabilities when the user returns to the tab on local', () => { + const { refresh } = arrange() - window.dispatchEvent(new Event('blur')) - window.dispatchEvent(new Event('focus')) + window.dispatchEvent(new Event('blur')) + window.dispatchEvent(new Event('focus')) - expect(refresh).toHaveBeenCalledTimes(reads) - } - ) + expect(refresh).toHaveBeenCalledOnce() + }) }) diff --git a/src/platform/account/components/dialogs/settings/BillingTabLegacy.test.ts b/src/platform/account/components/dialogs/settings/BillingTabLegacy.test.ts index 223ea9f78b0..8b5dcc09975 100644 --- a/src/platform/account/components/dialogs/settings/BillingTabLegacy.test.ts +++ b/src/platform/account/components/dialogs/settings/BillingTabLegacy.test.ts @@ -2085,22 +2085,12 @@ describe('BillingTab', () => { }) describe('BillingTab refresh on return', () => { - it.for([ - { where: 'local', isCloud: false, reads: 1 }, - { where: 'Cloud', isCloud: true, reads: 0 } - ])( - 're-reads capabilities $reads time(s) when the user returns to the tab on $where', - ({ isCloud, reads }) => { - distribution.isCloud = isCloud - onTestFinished(() => { - distribution.isCloud = false - }) - const { refresh } = arrange() + it('re-reads capabilities when the user returns to the tab on local', () => { + const { refresh } = arrange() - window.dispatchEvent(new Event('blur')) - window.dispatchEvent(new Event('focus')) + window.dispatchEvent(new Event('blur')) + window.dispatchEvent(new Event('focus')) - expect(refresh).toHaveBeenCalledTimes(reads) - } - ) + expect(refresh).toHaveBeenCalledOnce() + }) }) diff --git a/src/platform/billing/composables/useRefreshBillingOnReturn.test.ts b/src/platform/billing/composables/useRefreshBillingOnReturn.test.ts index 9fcc246b57f..c5f203bf4ae 100644 --- a/src/platform/billing/composables/useRefreshBillingOnReturn.test.ts +++ b/src/platform/billing/composables/useRefreshBillingOnReturn.test.ts @@ -1,6 +1,5 @@ -import { fromPartial } from '@total-typescript/shoehorn' import { describe, expect, it, onTestFinished, vi } from 'vitest' -import { effectScope, ref } from 'vue' +import { effectScope } from 'vue' import { onBillingRefresh } from '@/platform/billing/billingRefresh' import { useBillingCapabilities } from '@/platform/billing/composables/useBillingCapabilities' @@ -19,34 +18,45 @@ vi.mock(import('@/platform/billing/composables/useBillingCapabilities')) const leave = () => window.dispatchEvent(new Event('blur')) const comeBack = () => window.dispatchEvent(new Event('focus')) -function openBillingTab({ isCloud = false, isInitialized = true } = {}) { - distribution.isCloud = isCloud - onTestFinished(() => { - distribution.isCloud = false - }) +function heldRead() { const settles: Array<() => void> = [] - const readAccount = vi.fn( + const read = vi.fn( () => new Promise((resolve) => { settles.push(resolve) }) ) - onTestFinished(onBillingRefresh(readAccount)) - const readCapabilities = vi.fn(async () => {}) - vi.mocked(useBillingContext).mockReturnValue( - fromPartial({ isInitialized: ref(isInitialized) }) - ) - vi.mocked(useBillingCapabilities).mockReturnValue( - fromPartial({ refresh: readCapabilities }) + const settle = async (index: number) => { + settles[index]() + await vi.advanceTimersByTimeAsync(0) + } + return { read, settle } +} + +function openBillingTab({ isCloud = false, isInitialized = true } = {}) { + distribution.isCloud = isCloud + onTestFinished(() => { + distribution.isCloud = false + }) + const account = heldRead() + onTestFinished(onBillingRefresh(account.read)) + const capabilities = heldRead() + const billing = useBillingContext() + billing.isInitialized.value = isInitialized + vi.mocked(useBillingContext).mockReturnValue(billing) + vi.mocked(useBillingCapabilities().refresh).mockImplementation( + capabilities.read ) const scope = effectScope() scope.run(useRefreshBillingOnReturn) onTestFinished(() => scope.stop()) - const settle = async (index: number) => { - settles[index]() - await new Promise((resolve) => setTimeout(resolve)) + return { + readAccount: account.read, + readCapabilities: capabilities.read, + settleAccount: account.settle, + settleCapabilities: capabilities.settle, + close: () => scope.stop() } - return { readAccount, readCapabilities, settle, close: () => scope.stop() } } describe('useRefreshBillingOnReturn', () => { @@ -62,11 +72,12 @@ describe('useRefreshBillingOnReturn', () => { }) it('re-reads on every later return', async () => { - const { readAccount, settle } = openBillingTab() + const { readAccount, settleAccount, settleCapabilities } = openBillingTab() leave() comeBack() - await settle(0) + await settleAccount(0) + await settleCapabilities(0) leave() comeBack() @@ -83,15 +94,23 @@ describe('useRefreshBillingOnReturn', () => { expect(readCapabilities).not.toHaveBeenCalled() }) - it('does not start a second read while the first is in flight', () => { - const { readAccount } = openBillingTab() + it('holds the read of a return that lands in flight until account and capabilities both settle', async () => { + const { readAccount, readCapabilities, settleAccount, settleCapabilities } = + openBillingTab() leave() comeBack() leave() comeBack() + await settleAccount(0) expect(readAccount).toHaveBeenCalledOnce() + expect(readCapabilities).toHaveBeenCalledOnce() + + await settleCapabilities(0) + + expect(readAccount).toHaveBeenCalledTimes(2) + expect(readCapabilities).toHaveBeenCalledTimes(2) }) it('leaves the first load alone while billing has not initialized', () => { diff --git a/src/platform/billing/refreshOnReturn.test.ts b/src/platform/billing/refreshOnReturn.test.ts index b6a4fc997b3..88c314a1f8d 100644 --- a/src/platform/billing/refreshOnReturn.test.ts +++ b/src/platform/billing/refreshOnReturn.test.ts @@ -92,6 +92,19 @@ describe('registerRefreshOnReturn', () => { expect(refresh).toHaveBeenCalledTimes(2) }) + it('drops a queued trailing refresh once the returned callback runs', async () => { + const { refresh, settle } = held() + const stop = registerRefreshOnReturn(refresh) + + come() + leave() + come() + stop() + await settle(0) + + expect(refresh).toHaveBeenCalledTimes(1) + }) + it('stops listening once the returned callback runs', () => { const refresh = vi.fn(async () => {}) const stop = registerRefreshOnReturn(refresh) diff --git a/src/platform/billing/refreshOnReturn.ts b/src/platform/billing/refreshOnReturn.ts index e72b4d44f98..491752cf3a5 100644 --- a/src/platform/billing/refreshOnReturn.ts +++ b/src/platform/billing/refreshOnReturn.ts @@ -1,13 +1,6 @@ /** - * A cancellation, card change, or payment made in a hosted tab never pushes - * back to this one, so every return to this tab re-reads whatever that tab - * could have changed. A return is leaving (`blur` or hidden) and coming back - * (`focus` or visible): the focus and visibility events of one return count - * once, and a return that lands while a refresh is still in flight gets one - * trailing refresh, because that read may predate what the hosted tab just - * did. It stays armed until the returned callback runs. Shared by every - * opener of a hosted billing tab or window so they arm the same return - * signal one way. + * Runs `refresh` on each return to this tab or window; a return during a + * refresh in flight gets one trailing refresh. Returns how to disarm it. */ export function registerRefreshOnReturn( refresh: () => Promise @@ -46,6 +39,7 @@ export function registerRefreshOnReturn( window.addEventListener('focus', onReturn) window.addEventListener('blur', onLeave) return () => { + trailing = false document.removeEventListener('visibilitychange', onVisibilityChange) window.removeEventListener('focus', onReturn) window.removeEventListener('blur', onLeave)