-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuthStatusIndicator.test.tsx
More file actions
252 lines (219 loc) · 8.05 KB
/
Copy pathAuthStatusIndicator.test.tsx
File metadata and controls
252 lines (219 loc) · 8.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/**
* @vitest-environment jsdom
*/
/**
* Tests for AuthStatusIndicator component
*/
import { describe, it, expect, vi, beforeEach } from 'vitest';
import '@testing-library/jest-dom/vitest';
import { render, screen } from '@testing-library/react';
import { AuthStatusIndicator } from './AuthStatusIndicator';
import { useSettingsStore } from '../stores/settings-store';
import type { APIProfile } from '../../shared/types/profile';
// Mock the settings store
vi.mock('../stores/settings-store', () => ({
useSettingsStore: vi.fn()
}));
// Mock i18n translation function
vi.mock('react-i18next', () => ({
useTranslation: vi.fn(() => ({
t: (key: string, params?: Record<string, unknown>) => {
// For translation keys, return values for testing
const translations: Record<string, string> = {
'common:usage.authentication': 'Authentication',
'common:usage.oauth': 'OAuth',
'common:usage.apiProfile': 'API Profile',
'common:usage.provider': 'Provider',
'common:usage.providerAnthropic': 'Anthropic',
'common:usage.providerZai': 'z.ai',
'common:usage.providerZhipu': 'ZHIPU AI',
'common:usage.authenticationAriaLabel': 'Authentication: {{provider}}',
'common:usage.profile': 'Profile',
'common:usage.id': 'ID',
'common:usage.apiEndpoint': 'API Endpoint',
'common:usage.claudeCode': 'Claude Code',
'common:usage.apiKey': 'API Key'
};
// Handle interpolation (e.g., "Authentication: {{provider}}")
if (params && Object.keys(params).length > 0) {
const translated = translations[key] || key;
if (translated.includes('{{provider}}')) {
return translated.replace('{{provider}}', String(params.provider));
}
return translated;
}
return translations[key] || key;
}
}))
}));
/**
* Creates a mock settings store with optional overrides
* @param overrides - Partial store state to override defaults
* @returns Complete mock settings store object
*/
function createUseSettingsStoreMock(overrides?: Partial<ReturnType<typeof useSettingsStore>>) {
return {
profiles: testProfiles,
activeProfileId: null,
deleteProfile: vi.fn().mockResolvedValue(true),
setActiveProfile: vi.fn().mockResolvedValue(true),
profilesLoading: false,
settings: {} as Parameters<typeof useSettingsStore>[0],
isLoading: false,
error: null,
setSettings: vi.fn(),
updateSettings: vi.fn(),
setLoading: vi.fn(),
setError: vi.fn(),
setProfiles: vi.fn(),
setProfilesLoading: vi.fn(),
setProfilesError: vi.fn(),
saveProfile: vi.fn().mockResolvedValue(true),
updateProfile: vi.fn().mockResolvedValue(true),
profilesError: null,
...overrides
};
}
// Test profile data
const testProfiles: APIProfile[] = [
{
id: 'profile-1',
name: 'Production API',
baseUrl: 'https://api.anthropic.com',
apiKey: 'sk-ant-prod-key-1234',
models: { default: 'claude-sonnet-4-5-20250929' },
createdAt: Date.now(),
updatedAt: Date.now()
},
{
id: 'profile-2',
name: 'Development API',
baseUrl: 'https://dev-api.example.com/v1',
apiKey: 'sk-ant-test-key-5678',
models: undefined,
createdAt: Date.now(),
updatedAt: Date.now()
},
{
id: 'profile-3',
name: 'z.ai Global',
baseUrl: 'https://api.z.ai/api/anthropic',
apiKey: 'sk-zai-key-1234',
models: undefined,
createdAt: Date.now(),
updatedAt: Date.now()
},
{
id: 'profile-4',
name: 'ZHIPU China',
baseUrl: 'https://open.bigmodel.cn/api/paas/v4',
apiKey: 'zhipu-key-5678',
models: undefined,
createdAt: Date.now(),
updatedAt: Date.now()
}
];
describe('AuthStatusIndicator', () => {
beforeEach(() => {
vi.clearAllMocks();
// Mock window.electronAPI usage functions
(window as unknown as { electronAPI: unknown }).electronAPI = {
onUsageUpdated: vi.fn(() => vi.fn()), // Returns unsubscribe function
requestUsageUpdate: vi.fn().mockResolvedValue({ success: false, data: null })
};
});
describe('when using OAuth (no active profile)', () => {
beforeEach(() => {
vi.mocked(useSettingsStore).mockReturnValue(
createUseSettingsStoreMock({ activeProfileId: null })
);
});
it('should display Claude Code badge with Lock icon for OAuth', () => {
render(<AuthStatusIndicator />);
expect(screen.getByText('Claude Code')).toBeInTheDocument();
expect(screen.getByRole('button', { name: /authentication: claude code/i })).toBeInTheDocument();
});
it('should have correct aria-label for OAuth', () => {
render(<AuthStatusIndicator />);
expect(screen.getByRole('button')).toHaveAttribute('aria-label', 'Authentication: Claude Code');
});
});
describe('when using API profile', () => {
beforeEach(() => {
vi.mocked(useSettingsStore).mockReturnValue(
createUseSettingsStoreMock({ activeProfileId: 'profile-1' })
);
});
it('should display API Key badge with Key icon for API profile', () => {
render(<AuthStatusIndicator />);
expect(screen.getByText('API Key')).toBeInTheDocument();
expect(screen.getByRole('button', { name: /authentication: api key/i })).toBeInTheDocument();
});
it('should have correct aria-label for profile', () => {
render(<AuthStatusIndicator />);
expect(screen.getByRole('button')).toHaveAttribute('aria-label', 'Authentication: API Key');
});
});
describe('when active profile ID references non-existent profile', () => {
beforeEach(() => {
vi.mocked(useSettingsStore).mockReturnValue(
createUseSettingsStoreMock({ activeProfileId: 'non-existent-id' })
);
});
it('should fallback to OAuth (Claude Code) when profile not found', () => {
render(<AuthStatusIndicator />);
expect(screen.getByText('Claude Code')).toBeInTheDocument();
});
});
describe('provider detection for different API profiles', () => {
it('should display API Key badge for z.ai profile', () => {
vi.mocked(useSettingsStore).mockReturnValue(
createUseSettingsStoreMock({ activeProfileId: 'profile-3' })
);
render(<AuthStatusIndicator />);
expect(screen.getByText('API Key')).toBeInTheDocument();
expect(screen.getByRole('button')).toHaveAttribute('aria-label', 'Authentication: API Key');
});
it('should display API Key badge for ZHIPU profile', () => {
vi.mocked(useSettingsStore).mockReturnValue(
createUseSettingsStoreMock({ activeProfileId: 'profile-4' })
);
render(<AuthStatusIndicator />);
expect(screen.getByText('API Key')).toBeInTheDocument();
expect(screen.getByRole('button')).toHaveAttribute('aria-label', 'Authentication: API Key');
});
it('should apply correct color classes for each provider', () => {
// Test Anthropic (orange)
vi.mocked(useSettingsStore).mockReturnValue(
createUseSettingsStoreMock({ activeProfileId: 'profile-1' })
);
const { rerender } = render(<AuthStatusIndicator />);
const anthropicButton = screen.getByRole('button');
expect(anthropicButton.className).toContain('text-orange-500');
// Test z.ai (blue)
vi.mocked(useSettingsStore).mockReturnValue(
createUseSettingsStoreMock({ activeProfileId: 'profile-3' })
);
rerender(<AuthStatusIndicator />);
const zaiButton = screen.getByRole('button');
expect(zaiButton.className).toContain('text-blue-500');
// Test ZHIPU (purple)
vi.mocked(useSettingsStore).mockReturnValue(
createUseSettingsStoreMock({ activeProfileId: 'profile-4' })
);
rerender(<AuthStatusIndicator />);
const zhipuButton = screen.getByRole('button');
expect(zhipuButton.className).toContain('text-purple-500');
});
});
describe('component structure', () => {
beforeEach(() => {
vi.mocked(useSettingsStore).mockReturnValue(
createUseSettingsStoreMock()
);
});
it('should be a valid React component', () => {
expect(() => render(<AuthStatusIndicator />)).not.toThrow();
});
});
});