-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Expand file tree
/
Copy pathinitializer.test.ts
More file actions
214 lines (165 loc) · 6.38 KB
/
Copy pathinitializer.test.ts
File metadata and controls
214 lines (165 loc) · 6.38 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
/**
* @license
* Copyright 2025 Qwen Code
* SPDX-License-Identifier: Apache-2.0
*/
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { connectIdeForStartup, initializeApp } from './initializer.js';
const mockPerformInitialAuth = vi.fn();
const mockValidateTheme = vi.fn();
const mockInitializeI18n = vi.fn();
vi.mock('./auth.js', () => ({
performInitialAuth: (...args: unknown[]) => mockPerformInitialAuth(...args),
}));
vi.mock('./theme.js', () => ({
validateTheme: (...args: unknown[]) => mockValidateTheme(...args),
}));
vi.mock('../i18n/index.js', () => ({
initializeI18n: (...args: unknown[]) => mockInitializeI18n(...args),
resolveLanguageSetting: (settingsLang?: string) =>
process.env['QWEN_CODE_LANG'] || settingsLang || 'auto',
}));
const mockConnect = vi.fn();
const mockGetInstance = vi.fn().mockResolvedValue({ connect: mockConnect });
const mockLogIdeConnection = vi.fn();
vi.mock('@qwen-code/qwen-code-core', async (importOriginal) => {
const actual = await importOriginal<Record<string, unknown>>();
return {
...actual,
IdeClient: { getInstance: () => mockGetInstance() },
IdeConnectionEvent: vi.fn().mockImplementation((type) => ({ type })),
IdeConnectionType: { START: 'start' },
logIdeConnection: (...args: unknown[]) => mockLogIdeConnection(...args),
};
});
describe('initializeApp', () => {
let mockConfig: {
getModelsConfig: ReturnType<typeof vi.fn>;
getIdeMode: ReturnType<typeof vi.fn>;
getGeminiMdFileCount: ReturnType<typeof vi.fn>;
};
let mockSettings: {
merged: Record<string, unknown>;
setValue: ReturnType<typeof vi.fn>;
};
beforeEach(() => {
vi.clearAllMocks();
mockConfig = {
getModelsConfig: vi.fn().mockReturnValue({
getCurrentAuthType: vi.fn().mockReturnValue('api_key'),
wasAuthTypeExplicitlyProvided: vi.fn().mockReturnValue(false),
}),
getIdeMode: vi.fn().mockReturnValue(false),
getGeminiMdFileCount: vi.fn().mockReturnValue(0),
};
mockSettings = {
merged: { general: { language: 'en' } },
setValue: vi.fn(),
};
mockPerformInitialAuth.mockResolvedValue(null);
mockValidateTheme.mockReturnValue(null);
mockInitializeI18n.mockResolvedValue(undefined);
});
it('should initialize i18n with language from settings', async () => {
await initializeApp(mockConfig as never, mockSettings as never);
expect(mockInitializeI18n).toHaveBeenCalledWith('en');
});
it('should initialize i18n with QWEN_CODE_LANG env var if set', async () => {
vi.stubEnv('QWEN_CODE_LANG', 'zh');
await initializeApp(mockConfig as never, mockSettings as never);
expect(mockInitializeI18n).toHaveBeenCalledWith('zh');
vi.unstubAllEnvs();
});
it('should return no errors on successful initialization', async () => {
const result = await initializeApp(
mockConfig as never,
mockSettings as never,
);
expect(result.authError).toBeNull();
expect(result.themeError).toBeNull();
expect(result.geminiMdFileCount).toBe(0);
});
it('should return authError when auth fails', async () => {
mockPerformInitialAuth.mockResolvedValue('Auth failed');
const result = await initializeApp(
mockConfig as never,
mockSettings as never,
);
expect(result.authError).toBe('Auth failed');
expect(result.shouldOpenAuthDialog).toBe(true);
// initializeApp does not clear the selected auth type on failure
expect(mockSettings.setValue).not.toHaveBeenCalled();
});
it('should return themeError when theme validation fails', async () => {
mockValidateTheme.mockReturnValue('Theme not found');
const result = await initializeApp(
mockConfig as never,
mockSettings as never,
);
expect(result.themeError).toBe('Theme not found');
});
it('should set shouldOpenAuthDialog when auth was not explicitly provided', async () => {
mockConfig
.getModelsConfig()
.wasAuthTypeExplicitlyProvided.mockReturnValue(false);
const result = await initializeApp(
mockConfig as never,
mockSettings as never,
);
expect(result.shouldOpenAuthDialog).toBe(true);
});
it('should set shouldOpenAuthDialog when auth error occurs', async () => {
mockConfig
.getModelsConfig()
.wasAuthTypeExplicitlyProvided.mockReturnValue(true);
mockPerformInitialAuth.mockResolvedValue('Auth failed');
const result = await initializeApp(
mockConfig as never,
mockSettings as never,
);
expect(result.shouldOpenAuthDialog).toBe(true);
});
it('should not open auth dialog when auth was explicitly provided and succeeds', async () => {
mockConfig
.getModelsConfig()
.wasAuthTypeExplicitlyProvided.mockReturnValue(true);
const result = await initializeApp(
mockConfig as never,
mockSettings as never,
);
expect(result.shouldOpenAuthDialog).toBe(false);
});
it('should connect to IDE by default when in IDE mode', async () => {
mockConfig.getIdeMode.mockReturnValue(true);
await initializeApp(mockConfig as never, mockSettings as never);
expect(mockGetInstance).toHaveBeenCalled();
expect(mockConnect).toHaveBeenCalled();
expect(mockLogIdeConnection).toHaveBeenCalled();
});
it('should not connect to IDE when deferred', async () => {
mockConfig.getIdeMode.mockReturnValue(true);
await initializeApp(mockConfig as never, mockSettings as never, {
deferIdeConnection: true,
});
expect(mockGetInstance).not.toHaveBeenCalled();
expect(mockConnect).not.toHaveBeenCalled();
expect(mockLogIdeConnection).not.toHaveBeenCalled();
});
it('should connect to IDE through startup helper', async () => {
mockConfig.getIdeMode.mockReturnValue(true);
await connectIdeForStartup(mockConfig as never);
expect(mockGetInstance).toHaveBeenCalled();
expect(mockConnect).toHaveBeenCalled();
expect(mockLogIdeConnection).toHaveBeenCalled();
});
it('should not connect to IDE when not in IDE mode', async () => {
mockConfig.getIdeMode.mockReturnValue(false);
await initializeApp(mockConfig as never, mockSettings as never);
expect(mockGetInstance).not.toHaveBeenCalled();
});
it('should default language to auto when no setting is provided', async () => {
mockSettings.merged = {};
await initializeApp(mockConfig as never, mockSettings as never);
expect(mockInitializeI18n).toHaveBeenCalledWith('auto');
});
});