diff --git a/mobile/src/accounts/DeepSeekAccountUsageSection.tsx b/mobile/src/accounts/DeepSeekAccountUsageSection.tsx new file mode 100644 index 000000000000..d2fd705ec9b8 --- /dev/null +++ b/mobile/src/accounts/DeepSeekAccountUsageSection.tsx @@ -0,0 +1,38 @@ +import { Wallet } from 'lucide-react-native' +import { Text, View } from 'react-native' +import type { AccountsSnapshot } from '../components/accounts-snapshot' +import { getDeepSeekAccountUsage } from '../components/deepseek-account-usage' +import { colors } from '../theme/mobile-theme' +import { styles } from './mobile-accounts-screen-styles' + +export function DeepSeekAccountUsageSection({ snapshot }: { snapshot: AccountsSnapshot }) { + const usage = getDeepSeekAccountUsage(snapshot) + if (!usage) { + return null + } + + return ( + + + + DeepSeek API + + + + + Balance + + {usage.balanceLabel ?? usage.statusLabel} + + Read from the connected host + {usage.balanceLabel && usage.status !== 'available' ? ( + + {usage.statusLabel} + + ) : null} + + + + + ) +} diff --git a/mobile/src/accounts/MobileAccountsScreen.tsx b/mobile/src/accounts/MobileAccountsScreen.tsx index 9ea9c69b79af..0a73f2cb7fd0 100644 --- a/mobile/src/accounts/MobileAccountsScreen.tsx +++ b/mobile/src/accounts/MobileAccountsScreen.tsx @@ -10,6 +10,7 @@ import { colors, spacing } from '../theme/mobile-theme' import { useMobileColorScheme } from '../theme/use-mobile-color-scheme' import { useMobileTranslation } from '../i18n/use-mobile-translation' import { styles } from './mobile-accounts-screen-styles' +import { DeepSeekAccountUsageSection } from './DeepSeekAccountUsageSection' import { useNow } from '../hooks/use-now' import { ClaudeIcon, OpenAIIcon } from '../components/AgentIcons' import { AccountsSnapshotError, decodeAccountsSnapshot } from '../components/accounts-snapshot' @@ -354,6 +355,7 @@ export function MobileAccountsScreen() { <> {renderProviderSection('claude')} {renderProviderSection('codex')} + {renderOtherProviders()} diff --git a/mobile/src/components/deepseek-account-usage.test.ts b/mobile/src/components/deepseek-account-usage.test.ts new file mode 100644 index 000000000000..1e05f75d34b6 --- /dev/null +++ b/mobile/src/components/deepseek-account-usage.test.ts @@ -0,0 +1,229 @@ +import { readFileSync } from 'node:fs' +import { createElement } from 'react' +import { Text } from 'react-native' +import { act, create, type ReactTestRenderer } from 'react-test-renderer' +import { afterEach, describe, expect, it, vi } from 'vitest' + +vi.mock('react-native', () => ({ + Text: 'Text', + View: 'View', + Pressable: 'Pressable', + ActivityIndicator: 'ActivityIndicator', + StyleSheet: { create: (styles: Record) => styles } +})) +vi.mock('lucide-react-native', () => ({ Wallet: 'Wallet' })) +vi.mock('./AgentIcons', () => ({ ClaudeIcon: 'ClaudeIcon', OpenAIIcon: 'OpenAIIcon' })) + +import { decodeAccountsSnapshot, type AccountsSnapshot } from './accounts-snapshot' +import { getDeepSeekAccountUsage, hasDeepSeekAccountUsage } from './deepseek-account-usage' +import { DeepSeekAccountUsageSection } from '../accounts/DeepSeekAccountUsageSection' + +function deepSeekLimits(overrides: Record = {}) { + return { + provider: 'deepseek', + session: null, + weekly: null, + monthly: null, + updatedAt: 10, + error: null, + status: 'ok', + ...overrides + } +} + +function decode(rateLimits: Record) { + return decodeAccountsSnapshot({ + claude: { accounts: [], activeAccountId: null }, + codex: { accounts: [], activeAccountId: null }, + rateLimits: { + claude: null, + codex: null, + inactiveClaudeAccounts: [], + inactiveCodexAccounts: [], + ...rateLimits + } + }) +} + +describe('getDeepSeekAccountUsage', () => { + it('shows the remaining balance from the host usage snapshot', () => { + const snapshot = decode({ + deepseekAuthConfigured: true, + deepseek: deepSeekLimits({ + monthly: { + usedPercent: 0, + windowMinutes: 43_200, + resetsAt: null, + resetDescription: 'USD 110.00' + } + }) + }) + + expect(getDeepSeekAccountUsage(snapshot)).toEqual({ + balanceLabel: 'USD 110.00', + status: 'available', + statusLabel: 'Balance available' + }) + expect(hasDeepSeekAccountUsage(snapshot)).toBe(true) + }) + + it('shows configured DeepSeek while the first balance request is pending', () => { + const snapshot = decode({ deepseekAuthConfigured: true }) + + expect(getDeepSeekAccountUsage(snapshot)).toEqual({ + balanceLabel: null, + status: 'loading', + statusLabel: 'Checking balance…' + }) + }) + + it('shows a depleted balance state', () => { + const snapshot = decode({ + deepseek: deepSeekLimits({ + monthly: { + usedPercent: 100, + windowMinutes: 43_200, + resetsAt: null, + resetDescription: 'USD 0.00' + } + }) + }) + + expect(getDeepSeekAccountUsage(snapshot)).toMatchObject({ + balanceLabel: 'USD 0.00', + status: 'depleted', + statusLabel: 'No balance remaining' + }) + }) + + it('keeps the last balance visible while reporting a refresh error', () => { + const snapshot = decode({ + deepseekAuthConfigured: true, + deepseek: deepSeekLimits({ + status: 'error', + error: 'temporary network failure', + monthly: { + usedPercent: 0, + windowMinutes: 43_200, + resetsAt: null, + resetDescription: 'USD 72.00' + } + }) + }) + + expect(getDeepSeekAccountUsage(snapshot)).toMatchObject({ + balanceLabel: 'USD 72.00', + status: 'refresh-error', + statusLabel: 'Balance refresh failed' + }) + }) + + it('hides DeepSeek when the host has no configured key and no balance window', () => { + const snapshot = decode({ deepseek: deepSeekLimits({ monthly: null }) }) + + expect(getDeepSeekAccountUsage(snapshot)).toBeNull() + }) + + it('ignores a malformed DeepSeek slot without dropping Claude usage', () => { + const snapshot = decode({ + claude: { + ...deepSeekLimits(), + provider: 'claude', + session: { + usedPercent: 25, + windowMinutes: 300, + resetsAt: null, + resetDescription: null + } + }, + deepseek: { provider: 'claude', status: 'ok' }, + deepseekAuthConfigured: true + }) + + expect(snapshot.rateLimits.claude?.status).toBe('ok') + expect(getDeepSeekAccountUsage(snapshot)).toMatchObject({ + balanceLabel: null, + status: 'unavailable', + statusLabel: 'Balance unavailable' + }) + }) +}) + +describe('DeepSeek usage surfaces', () => { + let mounted: ReactTestRenderer | null = null + + afterEach(async () => { + await act(async () => mounted?.unmount()) + mounted = null + }) + + it.each([ + { name: 'pending', raw: undefined, text: 'Checking balance…', balance: null }, + { name: 'malformed', raw: { provider: 'claude' }, text: 'Balance unavailable', balance: null }, + { + name: 'available', + raw: deepSeekLimits({ monthly: balanceWindow(0, 'USD 72.00') }), + text: null, + balance: 'USD 72.00' + }, + { + name: 'depleted', + raw: deepSeekLimits({ monthly: balanceWindow(100, 'USD 0.00') }), + text: 'No balance remaining', + balance: 'USD 0.00' + }, + { + name: 'refresh error', + raw: deepSeekLimits({ status: 'error', monthly: balanceWindow(0, 'USD 72.00') }), + text: 'Balance refresh failed', + balance: 'USD 72.00' + } + ])('renders the $name balance on Accounts', async ({ raw, text, balance }) => { + const snapshot = decode({ deepseekAuthConfigured: true, deepseek: raw }) + const tree = await renderSection(snapshot) + const texts = tree.root.findAll((node) => node.type === Text) + const shown = texts.map((node) => node.children.join('')) + expect(shown.filter((value) => value === 'DeepSeek API')).toHaveLength(1) + const primary = balance ?? text + if (primary) { + expect(shown).toContain(primary) + expect( + texts.find((node) => node.children.join('') === primary)?.props.accessibilityLiveRegion + ).toBe('polite') + } + if (text && balance) { + expect(shown).toContain(text) + expect( + texts.find((node) => node.children.join('') === text)?.props.accessibilityLiveRegion + ).toBe('polite') + } + }) + + async function renderSection(snapshot: AccountsSnapshot): Promise { + await act(async () => { + mounted = create(createElement(DeepSeekAccountUsageSection, { snapshot })) + }) + if (!mounted) { + throw new Error('usage section did not mount') + } + return mounted + } + + it('mounts the read-only DeepSeek section on Accounts', () => { + const accounts = readFileSync( + new URL('../accounts/MobileAccountsScreen.tsx', import.meta.url), + 'utf8' + ) + const section = readFileSync( + new URL('../accounts/DeepSeekAccountUsageSection.tsx', import.meta.url), + 'utf8' + ) + + expect(accounts).toContain('') + expect(section).toContain('getDeepSeekAccountUsage(snapshot)') + }) +}) + +function balanceWindow(usedPercent: number, resetDescription: string) { + return { usedPercent, windowMinutes: 43_200, resetsAt: null, resetDescription } +} diff --git a/mobile/src/components/deepseek-account-usage.ts b/mobile/src/components/deepseek-account-usage.ts new file mode 100644 index 000000000000..0a9a83062452 --- /dev/null +++ b/mobile/src/components/deepseek-account-usage.ts @@ -0,0 +1,87 @@ +import { z } from 'zod' +import { ProviderRateLimitsSchema, type AccountsSnapshot } from './accounts-snapshot' + +const DeepSeekProviderRateLimitsSchema = ProviderRateLimitsSchema.extend({ + provider: z.literal('deepseek') +}) + +export type DeepSeekAccountUsageStatus = + | 'loading' + | 'available' + | 'depleted' + | 'refresh-error' + | 'unavailable' + +export type DeepSeekAccountUsage = { + balanceLabel: string | null + status: DeepSeekAccountUsageStatus + statusLabel: string +} + +function readDeepSeekAuthConfigured(snapshot: AccountsSnapshot): boolean { + return snapshot.rateLimits['deepseekAuthConfigured'] === true +} + +// Why: isolate the optional provider slot so a malformed DeepSeek payload cannot +// make the existing Claude/Codex account snapshot unreadable. +function readDeepSeekLimits(snapshot: AccountsSnapshot): { + limits: z.infer | null + invalid: boolean +} { + const raw = snapshot.rateLimits['deepseek'] + if (raw == null) { + return { limits: null, invalid: false } + } + const parsed = DeepSeekProviderRateLimitsSchema.safeParse(raw) + if (!parsed.success) { + return { limits: null, invalid: true } + } + return { limits: parsed.data, invalid: false } +} + +export function getDeepSeekAccountUsage(snapshot: AccountsSnapshot): DeepSeekAccountUsage | null { + const { limits, invalid } = readDeepSeekLimits(snapshot) + const monthly = limits?.monthly ?? null + if (!readDeepSeekAuthConfigured(snapshot) && !monthly) { + return null + } + + const balanceLabel = monthly?.resetDescription?.trim() || null + const isLoading = !limits || limits.status === 'idle' || limits.status === 'fetching' + const status: DeepSeekAccountUsageStatus = invalid + ? 'unavailable' + : limits?.status === 'error' && monthly + ? 'refresh-error' + : monthly + ? monthly.usedPercent >= 100 + ? 'depleted' + : 'available' + : isLoading + ? 'loading' + : 'unavailable' + + return { + balanceLabel, + status, + statusLabel: getDeepSeekAccountUsageStatusLabel(status) + } +} + +export function hasDeepSeekAccountUsage(snapshot: AccountsSnapshot): boolean { + return getDeepSeekAccountUsage(snapshot) !== null +} + +function getDeepSeekAccountUsageStatusLabel(status: DeepSeekAccountUsageStatus): string { + switch (status) { + case 'loading': + return 'Checking balance…' + case 'available': + return 'Balance available' + case 'depleted': + return 'No balance remaining' + case 'refresh-error': + return 'Balance refresh failed' + case 'unavailable': + return 'Balance unavailable' + } +}