Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions mobile/src/accounts/DeepSeekAccountUsageSection.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<View style={styles.section}>
<View style={styles.sectionHeader}>
<Wallet size={14} color={colors.textMuted} />
<Text style={styles.sectionHeading}>DeepSeek API</Text>
</View>
<View style={styles.card}>
<View style={styles.row}>
<View style={styles.rowMain}>
<Text style={styles.rowTitle}>Balance</Text>
<Text accessibilityLiveRegion="polite" style={styles.rowSubtitle} numberOfLines={1}>
{usage.balanceLabel ?? usage.statusLabel}
</Text>
<Text style={styles.rowSubtitle}>Read from the connected host</Text>
{usage.balanceLabel && usage.status !== 'available' ? (
<Text accessibilityLiveRegion="polite" style={styles.errorText}>
{usage.statusLabel}
</Text>
) : null}
</View>
</View>
</View>
</View>
)
}
2 changes: 2 additions & 0 deletions mobile/src/accounts/MobileAccountsScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -354,6 +355,7 @@ export function MobileAccountsScreen() {
<>
{renderProviderSection('claude')}
{renderProviderSection('codex')}
<DeepSeekAccountUsageSection snapshot={snapshot} />
{renderOtherProviders()}
<View style={styles.footerHint}>
<User size={14} color={colors.textMuted} />
Expand Down
229 changes: 229 additions & 0 deletions mobile/src/components/deepseek-account-usage.test.ts
Original file line number Diff line number Diff line change
@@ -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<string, unknown>) => 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<string, unknown> = {}) {
return {
provider: 'deepseek',
session: null,
weekly: null,
monthly: null,
updatedAt: 10,
error: null,
status: 'ok',
...overrides
}
}

function decode(rateLimits: Record<string, unknown>) {
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<ReactTestRenderer> {
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('<DeepSeekAccountUsageSection snapshot={snapshot} />')
expect(section).toContain('getDeepSeekAccountUsage(snapshot)')
})
})

function balanceWindow(usedPercent: number, resetDescription: string) {
return { usedPercent, windowMinutes: 43_200, resetsAt: null, resetDescription }
}
87 changes: 87 additions & 0 deletions mobile/src/components/deepseek-account-usage.ts
Original file line number Diff line number Diff line change
@@ -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<typeof DeepSeekProviderRateLimitsSchema> | 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 }
}
Comment thread
innocarpe marked this conversation as resolved.
Resolved
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'
}
}