-
Notifications
You must be signed in to change notification settings - Fork 854
Expand file tree
/
Copy pathconfig-predicates.test.mjs
More file actions
273 lines (234 loc) · 9.89 KB
/
config-predicates.test.mjs
File metadata and controls
273 lines (234 loc) · 9.89 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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import assert from 'node:assert/strict'
import { afterEach, beforeEach, describe, test } from 'node:test'
import {
getNavigatorLanguage,
getPreferredLanguageKey,
chatgptApiModelKeys,
gptApiModelKeys,
claudeApiModelKeys,
openRouterApiModelKeys,
aimlApiModelKeys,
isUsingAimlApiModel,
isUsingAzureOpenAiApiModel,
isUsingBingWebModel,
isUsingChatGLMApiModel,
isUsingChatgptApiModel,
isUsingClaudeApiModel,
isUsingCustomModel,
isUsingCustomNameOnlyModel,
isUsingDeepSeekApiModel,
isUsingGeminiWebModel,
isUsingGithubThirdPartyApiModel,
isUsingMoonshotApiModel,
isUsingMoonshotWebModel,
isUsingMultiModeModel,
isUsingOllamaApiModel,
isUsingOpenAiApiModel,
isUsingGptCompletionApiModel,
isUsingOpenRouterApiModel,
} from '../../../src/config/index.mjs'
const representativeChatgptApiModelNames = [
'chatgptApi4oMini',
'chatgptApi5',
'chatgptApi5_1',
'chatgptApi5_2',
'chatgptApi5_4',
]
const representativeGptCompletionApiModelNames = ['gptApiInstruct']
const representativeClaudeApiModelNames = ['claude37SonnetApi', 'claudeOpus4Api']
const representativeOpenRouterApiModelNames = [
'openRouter_anthropic_claude_sonnet4',
'openRouter_openai_o3',
]
const representativeAimlApiModelNames = [
'aiml_claude_3_7_sonnet_20250219',
'aiml_openai_o3_2025_04_16',
]
const originalNavigatorDescriptor = Object.getOwnPropertyDescriptor(globalThis, 'navigator')
const restoreNavigator = () => {
if (originalNavigatorDescriptor) {
Object.defineProperty(globalThis, 'navigator', originalNavigatorDescriptor)
} else {
delete globalThis.navigator
}
}
const setNavigatorLanguage = (language) => {
Object.defineProperty(globalThis, 'navigator', {
value: { language },
configurable: true,
})
}
afterEach(() => {
restoreNavigator()
})
test('getNavigatorLanguage returns zhHant for zh-TW style locales', () => {
setNavigatorLanguage('zh-TW')
assert.equal(getNavigatorLanguage(), 'zhHant')
})
test('getNavigatorLanguage returns first two letters for non-zhHant locales', () => {
setNavigatorLanguage('en-US')
assert.equal(getNavigatorLanguage(), 'en')
})
test('getNavigatorLanguage normalizes mixed-case zh-TW locale to zhHant', () => {
setNavigatorLanguage('ZH-TW')
assert.equal(getNavigatorLanguage(), 'zhHant')
})
test('getNavigatorLanguage treats zh-Hant locale as zhHant', () => {
setNavigatorLanguage('zh-Hant')
assert.equal(getNavigatorLanguage(), 'zhHant')
})
test('isUsingChatgptApiModel matches representative chatgpt API keys', () => {
for (const modelName of representativeChatgptApiModelNames) {
assert.equal(isUsingChatgptApiModel({ modelName }), true)
}
assert.equal(isUsingChatgptApiModel({ modelName: 'customModel' }), false)
})
test('isUsingChatgptApiModel accepts exported chatgpt API model keys', () => {
for (const modelName of chatgptApiModelKeys) {
assert.equal(isUsingChatgptApiModel({ modelName }), true)
}
})
test('isUsingOpenAiApiModel matches representative chat and completion API keys', () => {
for (const modelName of representativeChatgptApiModelNames) {
assert.equal(isUsingOpenAiApiModel({ modelName }), true)
}
for (const modelName of representativeGptCompletionApiModelNames) {
assert.equal(isUsingOpenAiApiModel({ modelName }), true)
}
assert.equal(isUsingOpenAiApiModel({ modelName: 'customModel' }), false)
})
test('isUsingOpenAiApiModel accepts exported chat and completion API model groups', () => {
for (const modelName of chatgptApiModelKeys) {
assert.equal(isUsingOpenAiApiModel({ modelName }), true)
}
for (const modelName of gptApiModelKeys) {
assert.equal(isUsingOpenAiApiModel({ modelName }), true)
}
})
test('isUsingGptCompletionApiModel matches representative completion API keys', () => {
for (const modelName of representativeGptCompletionApiModelNames) {
assert.equal(isUsingGptCompletionApiModel({ modelName }), true)
}
assert.equal(isUsingGptCompletionApiModel({ modelName: 'chatgptApi4oMini' }), false)
})
test('isUsingGptCompletionApiModel accepts exported completion API model keys', () => {
for (const modelName of gptApiModelKeys) {
assert.equal(isUsingGptCompletionApiModel({ modelName }), true)
}
})
test('isUsingCustomModel works with modelName and apiMode forms', () => {
assert.equal(isUsingCustomModel({ modelName: 'customModel' }), true)
const apiMode = {
groupName: 'customApiModelKeys',
itemName: 'customModel',
isCustom: true,
customName: 'my-custom-model',
customUrl: '',
apiKey: '',
active: true,
}
assert.equal(isUsingCustomModel({ apiMode }), true)
})
test('isUsingMultiModeModel currently follows Bing web group behavior', () => {
assert.equal(isUsingBingWebModel({ modelName: 'bingFree4' }), true)
assert.equal(isUsingMultiModeModel({ modelName: 'bingFree4' }), true)
assert.equal(isUsingBingWebModel({ modelName: 'chatgptFree35' }), false)
assert.equal(isUsingMultiModeModel({ modelName: 'chatgptFree35' }), false)
})
// ── isUsing* predicate wrappers for remaining providers ──────────────
test('isUsingMoonshotWebModel detects moonshot web models', () => {
assert.equal(isUsingMoonshotWebModel({ modelName: 'moonshotWebFree' }), true)
assert.equal(isUsingMoonshotWebModel({ modelName: 'moonshotWebFreeK15' }), true)
assert.equal(isUsingMoonshotWebModel({ modelName: 'chatgptFree35' }), false)
})
test('isUsingGeminiWebModel detects bard/gemini web models', () => {
assert.equal(isUsingGeminiWebModel({ modelName: 'bardWebFree' }), true)
assert.equal(isUsingGeminiWebModel({ modelName: 'chatgptFree35' }), false)
})
test('isUsingClaudeApiModel matches representative Claude API keys', () => {
for (const modelName of representativeClaudeApiModelNames) {
assert.equal(isUsingClaudeApiModel({ modelName }), true)
}
assert.equal(isUsingClaudeApiModel({ modelName: 'claude2WebFree' }), false)
})
test('isUsingClaudeApiModel accepts exported Claude API model keys', () => {
for (const modelName of claudeApiModelKeys) {
assert.equal(isUsingClaudeApiModel({ modelName }), true)
}
})
test('isUsingMoonshotApiModel detects moonshot API models', () => {
assert.equal(isUsingMoonshotApiModel({ modelName: 'moonshot_v1_8k' }), true)
assert.equal(isUsingMoonshotApiModel({ modelName: 'moonshot_k2' }), true)
assert.equal(isUsingMoonshotApiModel({ modelName: 'moonshotWebFree' }), false)
})
test('isUsingDeepSeekApiModel detects DeepSeek models', () => {
assert.equal(isUsingDeepSeekApiModel({ modelName: 'deepseek_chat' }), true)
assert.equal(isUsingDeepSeekApiModel({ modelName: 'deepseek_reasoner' }), true)
assert.equal(isUsingDeepSeekApiModel({ modelName: 'chatgptApi4oMini' }), false)
})
test('isUsingOpenRouterApiModel matches representative OpenRouter API keys', () => {
for (const modelName of representativeOpenRouterApiModelNames) {
assert.equal(isUsingOpenRouterApiModel({ modelName }), true)
}
assert.equal(isUsingOpenRouterApiModel({ modelName: 'chatgptApi4oMini' }), false)
})
test('isUsingOpenRouterApiModel accepts exported OpenRouter API model keys', () => {
for (const modelName of openRouterApiModelKeys) {
assert.equal(isUsingOpenRouterApiModel({ modelName }), true)
}
})
test('isUsingAimlApiModel matches representative AI/ML API keys', () => {
for (const modelName of representativeAimlApiModelNames) {
assert.equal(isUsingAimlApiModel({ modelName }), true)
}
assert.equal(isUsingAimlApiModel({ modelName: 'chatgptApi4oMini' }), false)
})
test('isUsingAimlApiModel accepts exported AI/ML model keys', () => {
for (const modelName of aimlApiModelKeys) {
assert.equal(isUsingAimlApiModel({ modelName }), true)
}
})
test('isUsingChatGLMApiModel detects ChatGLM models', () => {
assert.equal(isUsingChatGLMApiModel({ modelName: 'chatglmTurbo' }), true)
assert.equal(isUsingChatGLMApiModel({ modelName: 'chatglm4' }), true)
assert.equal(isUsingChatGLMApiModel({ modelName: 'chatgptApi4oMini' }), false)
})
test('isUsingOllamaApiModel detects Ollama models', () => {
assert.equal(isUsingOllamaApiModel({ modelName: 'ollamaModel' }), true)
assert.equal(isUsingOllamaApiModel({ modelName: 'customModel' }), false)
})
test('isUsingAzureOpenAiApiModel detects Azure OpenAI models', () => {
assert.equal(isUsingAzureOpenAiApiModel({ modelName: 'azureOpenAi' }), true)
assert.equal(isUsingAzureOpenAiApiModel({ modelName: 'chatgptApi4oMini' }), false)
})
test('isUsingGithubThirdPartyApiModel detects waylaidwanderer models', () => {
assert.equal(isUsingGithubThirdPartyApiModel({ modelName: 'waylaidwandererApi' }), true)
assert.equal(isUsingGithubThirdPartyApiModel({ modelName: 'chatgptApi4oMini' }), false)
})
test('isUsingCustomNameOnlyModel detects poeAiWebCustom', () => {
assert.equal(isUsingCustomNameOnlyModel({ modelName: 'poeAiWebCustom' }), true)
assert.equal(isUsingCustomNameOnlyModel({ modelName: 'poeAiWebSage' }), false)
assert.equal(isUsingCustomNameOnlyModel({ modelName: 'customModel' }), false)
})
// ── getPreferredLanguageKey ──────────────────────────────────────────
describe('getPreferredLanguageKey', () => {
beforeEach(() => {
globalThis.__TEST_BROWSER_SHIM__.clearStorage()
})
test('returns stored preferredLanguage', async () => {
globalThis.__TEST_BROWSER_SHIM__.setStorage({ preferredLanguage: 'fr' })
const key = await getPreferredLanguageKey()
assert.equal(key, 'fr')
})
test('falls back to userLanguage when preference is auto', async () => {
globalThis.__TEST_BROWSER_SHIM__.setStorage({ preferredLanguage: 'auto' })
const key = await getPreferredLanguageKey()
// defaultConfig.userLanguage is derived from navigator.language ('en-US' → 'en')
assert.equal(key, 'en')
})
test('uses defaultConfig when storage is empty', async () => {
// defaultConfig.preferredLanguage = getNavigatorLanguage() which is 'en' in the shim
const key = await getPreferredLanguageKey()
assert.equal(key, 'en')
})
})