From 57160280fd9530e560d73bbf4da25cde9be426b8 Mon Sep 17 00:00:00 2001 From: Neil Date: Mon, 9 Nov 2026 05:29:00 -0800 Subject: [PATCH] wip(team): tests, account menu org line, i18n sync, dev url handoff --- src/main/billing/billing-service.ts | 9 ++++- .../billing/billing-summary-store.ts | 12 ++++-- .../billing/checkout/checkout-flow.test.ts | 36 ++++++++++++++++++ .../account-menu/SidebarAccountMenu.tsx | 12 ++++++ .../src/i18n/en-runtime-required.json | 21 ---------- src/shared/orca-billing-return-link.test.ts | 38 +++++++++++++++++++ 6 files changed, 102 insertions(+), 26 deletions(-) create mode 100644 src/renderer/src/components/billing/checkout/checkout-flow.test.ts create mode 100644 src/shared/orca-billing-return-link.test.ts diff --git a/src/main/billing/billing-service.ts b/src/main/billing/billing-service.ts index b941bb275a8..424d2e2280a 100644 --- a/src/main/billing/billing-service.ts +++ b/src/main/billing/billing-service.ts @@ -2,6 +2,7 @@ import { withCloudBuildDefaults } from '../cloud-build-defaults' // Plan & billing for the active Orca profile's signed-in account. Results are values, never // throws, so the renderer can show a precise state. A billing-service refusal never signs the // user out; only Orca Cloud auth rejecting the session does (via runWithFreshOrcaCloudSession). +import { appendFile } from 'node:fs/promises' import { app, shell } from 'electron' import type { OrcaBillingCheckoutArgs, @@ -147,7 +148,13 @@ async function openHosted( return { status: 'signed-out' } } // createUrl already restricted this to Stripe's hosted Checkout/Portal hosts. - await shell.openExternal(url) + const devUrlFile = app.isPackaged + ? '' + : (process.env.ORCA_BILLING_DEV_OPEN_URL_FILE?.trim() ?? '') + // Dev automation only: hand the Stripe URL to a test driver instead of the user's browser. + await (devUrlFile + ? appendFile(devUrlFile, `${url}\n`, { mode: 0o600 }) + : shell.openExternal(url)) return { status: 'opened' } } catch (error) { return openResultForError(error) diff --git a/src/renderer/src/components/billing/billing-summary-store.ts b/src/renderer/src/components/billing/billing-summary-store.ts index 97615db918e..bf86e26d05c 100644 --- a/src/renderer/src/components/billing/billing-summary-store.ts +++ b/src/renderer/src/components/billing/billing-summary-store.ts @@ -8,9 +8,11 @@ export type BillingSummary = { /** null until the first read answers (or when billing is off for this build/account). */ subscribed: boolean | null canManageBilling: boolean + /** "Team" when subscribed; null otherwise. */ + planLabel: string | null } -let summary: BillingSummary = { subscribed: null, canManageBilling: false } +let summary: BillingSummary = { subscribed: null, canManageBilling: false, planLabel: null } const listeners = new Set<() => void>() let lastReadAt = 0 let inFlight: Promise | null = null @@ -21,14 +23,16 @@ export function recordBillingStatus(result: OrcaBillingStatusResult): void { result.status === 'ok' ? { subscribed: isActivePlan(result.entitlement), - canManageBilling: result.entitlement.canManageBilling + canManageBilling: result.entitlement.canManageBilling, + planLabel: isActivePlan(result.entitlement) ? result.entitlement.planLabel : null } : result.status === 'failed' ? summary - : { subscribed: null, canManageBilling: false } + : { subscribed: null, canManageBilling: false, planLabel: null } if ( next.subscribed !== summary.subscribed || - next.canManageBilling !== summary.canManageBilling + next.canManageBilling !== summary.canManageBilling || + next.planLabel !== summary.planLabel ) { summary = next for (const listener of listeners) { diff --git a/src/renderer/src/components/billing/checkout/checkout-flow.test.ts b/src/renderer/src/components/billing/checkout/checkout-flow.test.ts new file mode 100644 index 00000000000..903fffde202 --- /dev/null +++ b/src/renderer/src/components/billing/checkout/checkout-flow.test.ts @@ -0,0 +1,36 @@ +import { describe, expect, it } from 'vitest' +import { sampleTeam } from '../plan-billing-fixtures' +import { checkoutPollVerdict, nextPhaseAfterOpen } from './checkout-flow' + +const order = { interval: 'month' as const, seats: 2 } + +describe('checkoutPollVerdict', () => { + it('waits until a subscription exists', () => { + expect(checkoutPollVerdict({ ...sampleTeam, plan: null, status: 'none' })).toBe('waiting') + }) + it('is paid once the subscription is active or trialing', () => { + expect(checkoutPollVerdict(sampleTeam)).toBe('paid') + expect(checkoutPollVerdict({ ...sampleTeam, status: 'trialing' })).toBe('paid') + }) + it('flags a first invoice that needs action (3-D Secure, decline)', () => { + expect(checkoutPollVerdict({ ...sampleTeam, status: 'incomplete' })).toBe('needs-attention') + expect(checkoutPollVerdict({ ...sampleTeam, status: 'past_due' })).toBe('needs-attention') + }) +}) + +describe('nextPhaseAfterOpen', () => { + it('awaits payment when the browser opened', () => { + expect(nextPhaseAfterOpen(order, { status: 'opened' }, 5)).toEqual({ + kind: 'awaiting-payment', + order, + since: 5 + }) + }) + it('reports why Checkout could not open', () => { + expect(nextPhaseAfterOpen(order, { status: 'forbidden' }, 5)).toEqual({ + kind: 'open-failed', + order, + result: { status: 'forbidden' } + }) + }) +}) diff --git a/src/renderer/src/components/sidebar/account-menu/SidebarAccountMenu.tsx b/src/renderer/src/components/sidebar/account-menu/SidebarAccountMenu.tsx index a3ea1739f23..6831ef9c665 100644 --- a/src/renderer/src/components/sidebar/account-menu/SidebarAccountMenu.tsx +++ b/src/renderer/src/components/sidebar/account-menu/SidebarAccountMenu.tsx @@ -16,6 +16,7 @@ import { useMountedRef } from '@/hooks/useMountedRef' import { useOptionalShortcutLabel } from '@/hooks/useShortcutLabel' import { useTranslate } from '@/i18n/use-translate' import { cn } from '@/lib/utils' +import { useBillingSummary } from '../../billing/billing-summary-store' import { useSetupGuideProgress } from '../../setup-guide/use-setup-guide-progress' import { getAccountMenuIdentity, type AccountMenuIdentity } from './account-menu-identity' import { selectVisibleAccountMenuItems, useRegisteredAccountMenuItems } from './account-menu-items' @@ -66,6 +67,14 @@ export function SidebarAccountMenu(): React.JSX.Element { const mountedRef = useMountedRef() const identity = getAccountMenuIdentity(authStatus) + const billingSummary = useBillingSummary() + const orgName = authStatus?.state === 'connected' ? authStatus.cloud?.activeOrgName : undefined + // "Acme · Team": which team this account acts for, and its plan once billing has answered. + const orgLine = orgName + ? billingSummary.planLabel + ? `${orgName} · ${billingSummary.planLabel}` + : orgName + : null const extensionItems = selectVisibleAccountMenuItems(registeredItems, authStatus) const milestones = setupProgress.ready && setupProgress.coreDoneCount < setupProgress.coreTotal @@ -158,6 +167,9 @@ export function SidebarAccountMenu(): React.JSX.Element { {identity.email} ) : null} + {orgLine ? ( + {orgLine} + ) : null} ) : ( <> diff --git a/src/renderer/src/i18n/en-runtime-required.json b/src/renderer/src/i18n/en-runtime-required.json index 82d0ce9d385..a7339c5466d 100644 --- a/src/renderer/src/i18n/en-runtime-required.json +++ b/src/renderer/src/i18n/en-runtime-required.json @@ -209,27 +209,6 @@ "tokensAndCost_other": "{{tokens}} tokens · {{cost}}" } }, - "billing": { - "team": { - "askAdmin": "Only owners and admins can invite or remove members.", - "cannotChangeOwnRole": "You cannot change your own role.", - "cannotRemoveSelf": "You cannot remove yourself.", - "invite": "Invite", - "inviteEmail": "Email to invite", - "invitePlaceholder": "teammate@company.com", - "invited": "Invite sent to {{value0}}. They join (and become a billed seat) when they accept.", - "memberCount": "{{value0}} members", - "memberSeats": "{{value0}} members · {{value1}} seats billed. Seats follow membership; pending invites are free until accepted.", - "pending": "Pending invites", - "remove": "Remove {{value0}}", - "removeDescription2": "They lose access to the org right away. Their seat stays paid until your plan renews, then drops off.", - "removed": "Member removed.", - "revoke": "Revoke", - "revoked": "Invite revoked.", - "roleChanged": "Role updated.", - "seatsDrop": "Seats paid this period stay until {{value0}}, then drop to {{value1}}." - } - }, "browser-pane": { "markup": { "errorAttach": "Could not attach the markup screenshot.", diff --git a/src/shared/orca-billing-return-link.test.ts b/src/shared/orca-billing-return-link.test.ts new file mode 100644 index 00000000000..86c11b43796 --- /dev/null +++ b/src/shared/orca-billing-return-link.test.ts @@ -0,0 +1,38 @@ +import { describe, expect, it } from 'vitest' +import { findOrcaBillingReturnLink, parseOrcaBillingReturnLink } from './orca-billing' + +describe('parseOrcaBillingReturnLink', () => { + it('accepts only the three return outcomes', () => { + expect(parseOrcaBillingReturnLink('orca://billing/return?checkout=success')).toEqual({ + kind: 'checkout-success' + }) + expect(parseOrcaBillingReturnLink('orca://billing/return?checkout=cancel')).toEqual({ + kind: 'checkout-cancel' + }) + expect(parseOrcaBillingReturnLink('orca://billing/return?portal=return')).toEqual({ + kind: 'portal' + }) + }) + it('rejects other hosts, paths, schemes and junk', () => { + for (const raw of [ + 'orca://billing/return', + 'orca://billing/return?checkout=paid', + 'orca://evil/return?checkout=success', + 'orca://billing/other?checkout=success', + 'https://billing/return?checkout=success', + 'not a url' + ]) { + expect(parseOrcaBillingReturnLink(raw)).toBeNull() + } + }) + it('finds the link among launch arguments', () => { + expect( + findOrcaBillingReturnLink([ + '/Applications/Orca.app', + '--x', + 'orca://billing/return?checkout=cancel' + ]) + ).toEqual({ kind: 'checkout-cancel' }) + expect(findOrcaBillingReturnLink(['--flag'])).toBeNull() + }) +})