dsh-model-capability-sync 0.0.0-stage → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,42 @@
1
+ /**
2
+ * Browser-half build: one closure-factory module registered on the shell's
3
+ * module loader — `window.__ModuleLoader__.load({ id, factory })`. React and
4
+ * the JSX runtime are the only runtime externals the shell's module table
5
+ * supplies; every `@deepseek-ai/*` import in the source is type-only and is
6
+ * erased here.
7
+ */
8
+ import { build } from 'esbuild'
9
+
10
+ const ID = 'dsh-model-capability-sync'
11
+
12
+ const CLIENT_EXTERNALS = [
13
+ 'react',
14
+ 'react/jsx-runtime',
15
+ 'react-dom',
16
+ 'react-dom/client',
17
+ ]
18
+
19
+ await build({
20
+ entryPoints: ['src/client/index.ts'],
21
+ bundle: true,
22
+ format: 'cjs',
23
+ platform: 'browser',
24
+ target: 'es2022',
25
+ jsx: 'automatic',
26
+ outfile: 'lib/client.js',
27
+ sourcemap: true,
28
+ legalComments: 'none',
29
+ external: CLIENT_EXTERNALS,
30
+ define: {
31
+ 'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV ?? 'production'),
32
+ },
33
+ banner: {
34
+ js: [
35
+ `window.__ModuleLoader__.load({ id: ${JSON.stringify(ID)}, factory: (require) => {`,
36
+ 'var module = { exports: {} }; var exports = module.exports;',
37
+ ].join('\n'),
38
+ },
39
+ footer: { js: 'return module.exports; } });' },
40
+ })
41
+
42
+ console.log(`${ID}: wrote lib/client.js`)
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Host-half build: bundle `src/index.ts` (and its relative modules) into one
3
+ * ESM file. Every bare specifier stays external, so the plugin resolves
4
+ * `@deepseek-ai/*` from the host installation at load time — the same contract
5
+ * the official adapters use.
6
+ */
7
+ import { build } from 'esbuild'
8
+
9
+ await build({
10
+ entryPoints: ['src/index.ts'],
11
+ bundle: true,
12
+ format: 'esm',
13
+ platform: 'node',
14
+ target: 'node22',
15
+ outfile: 'lib/index.js',
16
+ sourcemap: true,
17
+ legalComments: 'none',
18
+ packages: 'external',
19
+ logLevel: 'info',
20
+ })
21
+
22
+ console.log('dsh-model-capability-sync: wrote lib/index.js')
@@ -0,0 +1,56 @@
1
+ /**
2
+ * Offline-ish smoke test: download the real models.dev catalog and run the
3
+ * matcher + mapper over the model ids this machine has configured.
4
+ *
5
+ * Run with `node --experimental-strip-types scripts/smoke.mjs` (Node 24 strips
6
+ * types by default). It reads ids from the desktop profile patch when present
7
+ * and falls back to a built-in sample, so it also works on a fresh checkout.
8
+ */
9
+ import { readFileSync, existsSync } from 'node:fs'
10
+ import { loadCatalog } from '../src/catalog.ts'
11
+ import { matchModel } from '../src/match.ts'
12
+ import { capabilitiesOf } from '../src/mapping.ts'
13
+
14
+ const PATCH = 'C:/Users/19603/.dsh/profiles/desktop/cordis.patch.yml'
15
+
16
+ /** Pull `- id:` lines out of the llm-newapi catalog block, without a YAML dependency. */
17
+ function idsFromPatch(path) {
18
+ if (!existsSync(path)) return undefined
19
+ const text = readFileSync(path, 'utf8')
20
+ const start = text.indexOf('- id: llm-newapi')
21
+ if (start === -1) return undefined
22
+ const block = text.slice(start)
23
+ const ids = [...block.matchAll(/^\s+- id: (\S+)$/gm)].map(match => match[1])
24
+ return ids.length > 0 ? ids : undefined
25
+ }
26
+
27
+ const ids = idsFromPatch(PATCH) ?? [
28
+ 'work-gj/deepseek-v4.1-flash',
29
+ 'gemini/gemini-3.8-flash-high',
30
+ 'qoder-gj/Qwen3.8-Flash',
31
+ 'beichenapi/gpt-6',
32
+ 'openai/gpt-4o',
33
+ ]
34
+
35
+ const catalog = await loadCatalog({ refresh: true })
36
+ console.log(`catalog: ${catalog.modelCount} models, fetched ${new Date(catalog.fetchedAt).toISOString()}`)
37
+
38
+ const stats = { total: 0, matched: 0, image: 0, unmatched: [] }
39
+ const sample = []
40
+ for (const id of ids) {
41
+ stats.total += 1
42
+ const match = matchModel(catalog, id)
43
+ if (match === undefined) {
44
+ stats.unmatched.push(id)
45
+ continue
46
+ }
47
+ stats.matched += 1
48
+ const entry = catalog.byId.get(match.modelId)?.find(candidate => candidate.provider === match.provider)
49
+ const fields = entry === undefined ? {} : capabilitiesOf(entry)
50
+ if (fields.input?.includes('image')) stats.image += 1
51
+ if (sample.length < 6) sample.push({ id, via: match.via, to: `${match.provider}/${match.modelId}`, fields })
52
+ }
53
+
54
+ console.log(`matched ${stats.matched}/${stats.total}, image-capable ${stats.image}, unmatched ${stats.unmatched.length}`)
55
+ console.log('sample:', JSON.stringify(sample, null, 1))
56
+ if (stats.unmatched.length > 0) console.log('unmatched ids:', stats.unmatched.join(', '))
@@ -0,0 +1,56 @@
1
+ /**
2
+ * Unit tests for hand-typed capability values.
3
+ *
4
+ * Run with `node --experimental-strip-types --test scripts/test-fields.mjs`.
5
+ *
6
+ * The table shows token counts in short form (`1M`) and those same cells are
7
+ * typeable, so the short form has to parse back to the number it stands for.
8
+ * The overlay rule matters too: a typed value wins over the catalog even when
9
+ * "overwrite" is off, because that switch is about the catalog replacing what
10
+ * is stored, not about a person deciding what the entry should say.
11
+ */
12
+ import test from 'node:test'
13
+ import assert from 'node:assert/strict'
14
+ import { mergeFields, parseTokenCount } from '../src/client/shared.ts'
15
+
16
+ test('a short token count parses to its number', () => {
17
+ assert.equal(parseTokenCount('128K'), 128_000)
18
+ assert.equal(parseTokenCount('1M'), 1_000_000)
19
+ assert.equal(parseTokenCount('1.5M'), 1_500_000)
20
+ assert.equal(parseTokenCount('1.5m'), 1_500_000)
21
+ assert.equal(parseTokenCount('262k'), 262_000)
22
+ })
23
+
24
+ test('a plain or punctuated count parses', () => {
25
+ assert.equal(parseTokenCount('128000'), 128_000)
26
+ assert.equal(parseTokenCount('1,000,000'), 1_000_000)
27
+ assert.equal(parseTokenCount(' 1000000 '), 1_000_000)
28
+ })
29
+
30
+ test('anything that is not a count reads as no value', () => {
31
+ // Empty means "drop the manual value", not zero.
32
+ for (const text of ['', ' ', 'abc', '1M2', '-5', '0', 'K', '1 T']) {
33
+ assert.equal(parseTokenCount(text), undefined, JSON.stringify(text))
34
+ }
35
+ })
36
+
37
+ test('a typed value overlays the catalog value', () => {
38
+ const merged = mergeFields(
39
+ { contextWindow: 1_000_000, maxTokens: 128_000, input: ['text', 'image'] },
40
+ { contextWindow: 200_000 },
41
+ )
42
+ assert.equal(merged.contextWindow, 200_000)
43
+ assert.equal(merged.maxTokens, 128_000)
44
+ assert.deepEqual(merged.input, ['text', 'image'])
45
+ })
46
+
47
+ test('a model the catalog never listed keeps only what was typed', () => {
48
+ const merged = mergeFields(undefined, { contextWindow: 64_000, input: ['text'] })
49
+ assert.deepEqual(merged, { contextWindow: 64_000, input: ['text'] })
50
+ })
51
+
52
+ test('no typed values leaves the catalog fields untouched', () => {
53
+ const catalog = { contextWindow: 1_000_000 }
54
+ assert.deepEqual(mergeFields(catalog, undefined), catalog)
55
+ assert.equal(mergeFields(undefined, undefined), undefined)
56
+ })
@@ -0,0 +1,54 @@
1
+ /**
2
+ * Unit tests for copy-table completeness.
3
+ *
4
+ * Run with `node --experimental-strip-types --test scripts/test-locale.mjs`.
5
+ *
6
+ * This exists because a column header shipped as the raw key `colName`: the
7
+ * dictionary was missing it, `t()` fell back to the key itself, and nothing
8
+ * failed — the UI just read "colName" where a label belonged. A missing key is
9
+ * silent at runtime, so it has to be loud here.
10
+ */
11
+ import test from 'node:test'
12
+ import assert from 'node:assert/strict'
13
+ import { readFileSync } from 'node:fs'
14
+ import { fileURLToPath } from 'node:url'
15
+ import { dirname, join } from 'node:path'
16
+ import { en, zh } from '../src/client/locale.ts'
17
+
18
+ const here = dirname(fileURLToPath(import.meta.url))
19
+ const clientDir = join(here, '..', 'src', 'client')
20
+
21
+ /** Every client module that calls `t('…')`. */
22
+ const CALLERS = ['SyncSection.tsx', 'apply.ts']
23
+
24
+ /** The copy keys the UI asks for, as written in the source. */
25
+ function keysUsed() {
26
+ const found = new Set()
27
+ for (const file of CALLERS) {
28
+ const text = readFileSync(join(clientDir, file), 'utf8')
29
+ for (const match of text.matchAll(/\bt\('([A-Za-z][A-Za-z0-9]*)'/g)) found.add(match[1])
30
+ }
31
+ return [...found].sort()
32
+ }
33
+
34
+ test('every key the UI asks for exists in both dictionaries', () => {
35
+ const used = keysUsed()
36
+ assert.ok(used.length > 20, `expected the UI to use many keys, found ${used.length}`)
37
+ const missingZh = used.filter(key => !(key in zh))
38
+ const missingEn = used.filter(key => !(key in en))
39
+ assert.deepEqual(missingZh, [], `missing from zh: ${missingZh.join(', ')}`)
40
+ assert.deepEqual(missingEn, [], `missing from en: ${missingEn.join(', ')}`)
41
+ })
42
+
43
+ test('the two dictionaries hold the same keys', () => {
44
+ const zhOnly = Object.keys(zh).filter(key => !(key in en))
45
+ const enOnly = Object.keys(en).filter(key => !(key in zh))
46
+ assert.deepEqual(zhOnly, [], `only in zh: ${zhOnly.join(', ')}`)
47
+ assert.deepEqual(enOnly, [], `only in en: ${enOnly.join(', ')}`)
48
+ })
49
+
50
+ test('the name column is present', () => {
51
+ // The specific key that shipped as a raw identifier.
52
+ assert.equal(zh.colName, '名称')
53
+ assert.equal(en.colName, 'Name')
54
+ })
@@ -0,0 +1,268 @@
1
+ /**
2
+ * Unit tests for id → catalog matching, with the display-name fallback.
3
+ *
4
+ * Run with `node --experimental-strip-types --test scripts/test-match.mjs`.
5
+ * A tiny hand-built catalog stands in for models.dev, so this stays offline.
6
+ */
7
+ import test from 'node:test'
8
+ import assert from 'node:assert/strict'
9
+ import { cleanDisplayName, matchModel } from '../src/match.ts'
10
+
11
+ /** Index a few entries the way `loadCatalog` does. */
12
+ function catalogOf(entries) {
13
+ const byId = new Map()
14
+ const byFolded = new Map()
15
+ for (const [provider, id] of entries) {
16
+ const entry = { provider, id, model: { id, limit: { context: 128000 } } }
17
+ for (const [map, key] of [[byId, id], [byFolded, id.toLowerCase()]]) {
18
+ const list = map.get(key)
19
+ if (list === undefined) map.set(key, [entry])
20
+ else list.push(entry)
21
+ }
22
+ }
23
+ return { fetchedAt: 0, modelCount: entries.length, byId, byFolded }
24
+ }
25
+
26
+ const catalog = catalogOf([
27
+ ['alibaba', 'qwen3.8-flash'],
28
+ ['deepseek', 'deepseek-flash'],
29
+ ['openai', 'gpt-6-astra'],
30
+ ])
31
+
32
+ test('cleanDisplayName drops source annotations', () => {
33
+ assert.equal(cleanDisplayName('Qwen3.8-Flash【qoder】'), 'Qwen3.8-Flash')
34
+ assert.equal(cleanDisplayName('Qwen3.8-Flash (qoder)'), 'Qwen3.8-Flash')
35
+ assert.equal(cleanDisplayName('[cn] Qwen3.8-Flash'), 'Qwen3.8-Flash')
36
+ assert.equal(cleanDisplayName(' GLM-5.3-Flash '), 'GLM-5.3-Flash')
37
+ })
38
+
39
+ test('an exact id still wins, and reports how it matched', () => {
40
+ const match = matchModel(catalog, 'qwen3.8-flash', {}, 'something else')
41
+ assert.equal(match?.modelId, 'qwen3.8-flash')
42
+ assert.equal(match?.via, 'exact')
43
+ })
44
+
45
+ test('an opaque id falls back to the display name', () => {
46
+ const match = matchModel(catalog, 'qoder/qfmodel', {}, 'Qwen3.8-Flash')
47
+ assert.equal(match?.modelId, 'qwen3.8-flash')
48
+ assert.equal(match?.via, 'name')
49
+ assert.equal(match?.nameSource, 'name')
50
+ assert.equal(match?.matchedName, 'Qwen3.8-Flash')
51
+ })
52
+
53
+ test('a renamed label resolves exactly like the original', () => {
54
+ const plain = matchModel(catalog, 'qoder/qfmodel', {}, 'Qwen3.8-Flash')
55
+ const renamed = matchModel(catalog, 'qoder/qfmodel', {}, 'Qwen3.8-Flash【qoder】')
56
+ const halfWidth = matchModel(catalog, 'qoder/qfmodel', {}, 'Qwen3.8-Flash (qoder)')
57
+ assert.deepEqual(renamed, plain)
58
+ assert.deepEqual(halfWidth, plain)
59
+ })
60
+
61
+ test('a name that still carries the prefix keeps its last segment', () => {
62
+ // `qoder-cn/` is a gateway namespace, so dropping it leaves the catalog id.
63
+ const match = matchModel(catalog, 'qoder-cn/qwen3.8-flash', {}, 'qoder-cn/qwen3.8-flash')
64
+ assert.equal(match?.modelId, 'qwen3.8-flash')
65
+ assert.equal(match?.via, 'basename')
66
+ })
67
+
68
+ test('a renamed label spoken for by the name alone reaches its last segment', () => {
69
+ // `vendor-a/` is invented, so the id matches nothing: only the label can.
70
+ const match = matchModel(catalog, 'vendor-a/Qwen3.8-Flash【vendor-a】', {}, 'vendor-a/Qwen3.8-Flash【vendor-a】')
71
+ assert.equal(match?.modelId, 'qwen3.8-flash')
72
+ assert.equal(match?.via, 'name')
73
+ assert.equal(match?.matchedName, 'Qwen3.8-Flash')
74
+ })
75
+
76
+ test('the name is tried for its variant-free form too', () => {
77
+ const match = matchModel(catalog, 'qoder/x', {}, 'DeepSeek-Flash-high')
78
+ assert.equal(match?.modelId, 'deepseek-flash')
79
+ assert.equal(match?.via, 'nameSuffix')
80
+ })
81
+
82
+ test('nothing matches without an id or a name', () => {
83
+ assert.equal(matchModel(catalog, 'qoder/qfmodel', {}, ''), undefined)
84
+ assert.equal(matchModel(catalog, 'qoder/qfmodel', {}, 'Unknown-Model'), undefined)
85
+ })
86
+
87
+ test('an alias remains an explicit override', () => {
88
+ const match = matchModel(catalog, 'mystery', { mystery: 'gpt-6-astra' }, '')
89
+ assert.equal(match?.modelId, 'gpt-6-astra')
90
+ assert.equal(match?.via, 'alias')
91
+ })
92
+
93
+ // --- variant markers, prefixes, and build numbers ---
94
+
95
+ /** A catalog with the shapes gateways actually produce. */
96
+ const wide = catalogOf([
97
+ ['google', 'gemini-3.8-flash'],
98
+ ['google', 'gemini-4-argon'],
99
+ ['cohere', 'north-mini-code-1-0'],
100
+ ['nvidia', 'nemotron-3.5-lightning-30b-a3b'],
101
+ ['nvidia', 'nemotron-3-nano-omni-30b-a3b-reasoning'],
102
+ ['dots-studio', 'dots-3-note-preview'],
103
+ ['anthropic', 'claude-sonnet-4-5'],
104
+ ['moonshotai', 'kimi-k3'],
105
+ ['moark', 'GLM-5.3'],
106
+ ])
107
+
108
+ test('a route tag and a variant suffix come off together', () => {
109
+ const hit = matchModel(wide, 'gemini-3.8-flash-tiered')
110
+ assert.equal(hit?.modelId, 'gemini-3.8-flash')
111
+ // Either rule may report the hit: both describe a loosened match.
112
+ assert.ok(['suffix', 'prefix'].includes(hit?.via), `unexpected via ${hit?.via}`)
113
+ })
114
+
115
+ test('a disabled variant still names the model it disables', () => {
116
+ const hit = matchModel(wide, 'antigravity/gemini-4-argon-disabled')
117
+ assert.equal(hit?.modelId, 'gemini-4-argon')
118
+ })
119
+
120
+ test('a truncated tail matches by prefix', () => {
121
+ const lightning = matchModel(wide, 'cline/nvidia/nemotron-3.5-lightning:free')
122
+ assert.equal(lightning?.modelId, 'nemotron-3.5-lightning-30b-a3b')
123
+ assert.equal(lightning?.via, 'prefix')
124
+
125
+ const north = matchModel(wide, 'cline/cohere/north-mini-code:free')
126
+ assert.equal(north?.modelId, 'north-mini-code-1-0')
127
+ assert.equal(north?.via, 'prefix')
128
+ })
129
+
130
+ test('nested vendor segments are peeled one at a time', () => {
131
+ const hit = matchModel(wide, 'cline/dots-studio/dots-3-note-preview:free')
132
+ assert.equal(hit?.provider, 'dots-studio')
133
+ assert.equal(hit?.modelId, 'dots-3-note-preview')
134
+ })
135
+
136
+ test('a version dot folds to a dash', () => {
137
+ const hit = matchModel(wide, 'kiro/claude-sonnet-4.5')
138
+ assert.equal(hit?.modelId, 'claude-sonnet-4-5')
139
+ assert.equal(hit?.via, 'folded')
140
+ })
141
+
142
+ test('a trailing build number comes off', () => {
143
+ const hit = matchModel(wide, 'work-cn/kimi-k3-1')
144
+ assert.equal(hit?.modelId, 'kimi-k3')
145
+ assert.equal(hit?.via, 'suffix')
146
+ })
147
+
148
+ test('a trial suffix resolves to the model it trials', () => {
149
+ assert.equal(matchModel(wide, 'zcode/GLM-5.3-Trial')?.modelId, 'GLM-5.3')
150
+ assert.equal(matchModel(wide, 'zcode/GLM-5.3-Flash-Trial')?.modelId, 'GLM-5.3')
151
+ })
152
+
153
+ test('an opaque id still resolves through its display name', () => {
154
+ // The fixture holds GLM-5.3; the label adds a `-Flash` marker on top of it.
155
+ const hit = matchModel(wide, 'qoder-cn/gfmodel', {}, 'GLM-5.3-Flash')
156
+ assert.equal(hit?.modelId, 'GLM-5.3')
157
+ assert.equal(hit?.via, 'nameSuffix')
158
+ assert.equal(hit?.matchedName, 'GLM-5.3-Flash')
159
+ })
160
+
161
+ test('nothing matches when no rule reaches the catalog', () => {
162
+ assert.equal(matchModel(wide, '1fmodel'), undefined)
163
+ })
164
+
165
+ // --- a reseller's namespace around an upstream id ---
166
+
167
+ /** Catalog shapes taken from the real one: same model under many providers. */
168
+ const reseller = catalogOf([
169
+ ['empiriolabs', 'glm-5-3'],
170
+ ['empiriolabs', 'glm-5-3-flash'],
171
+ ['alibaba', 'kimi-k3'],
172
+ ['empiriolabs', 'deepseek-v4-1-flash'],
173
+ ['empiriolabs', 'deepseek-v4-1-flash-1017'],
174
+ ['sensenova', 'sensenova-6.8-flash-lite'],
175
+ ['deepseek', 'deepseek-flash'],
176
+ ['302ai', 'deepseek-flash'],
177
+ ['orcarouter', 'kimi/kimi-k3'],
178
+ ])
179
+
180
+ test('a reseller namespace is peeled off when the rest resolves exactly', () => {
181
+ for (const [id, want] of [
182
+ ['raccoon/sn-glm-5-3', 'glm-5-3'],
183
+ ['raccoon/sn-glm-5-3-flash', 'glm-5-3-flash'],
184
+ ['raccoon/sn-kimi-k3', 'kimi-k3'],
185
+ ['raccoon/sn-deepseek-v4-1-flash', 'deepseek-v4-1-flash'],
186
+ ]) {
187
+ const hit = matchModel(reseller, id)
188
+ assert.equal(hit?.modelId, want, id)
189
+ assert.equal(hit?.via, 'vendor', id)
190
+ }
191
+ })
192
+
193
+ test('peeling still strips a build number on what is left', () => {
194
+ const hit = matchModel(reseller, 'raccoon/sn-deepseek-v4-1-flash-1017')
195
+ assert.equal(hit?.modelId, 'deepseek-v4-1-flash-1017')
196
+ assert.equal(hit?.via, 'vendor')
197
+ })
198
+
199
+ test('the version separator folds whichever way the reseller spells it', () => {
200
+ const hit = matchModel(reseller, 'raccoon/sn-sensenova-6-8-flash-lite')
201
+ assert.equal(hit?.modelId, 'sensenova-6.8-flash-lite')
202
+ assert.equal(hit?.via, 'vendor')
203
+ })
204
+
205
+ test('peeling never fires when what is left is not a real id', () => {
206
+ // `chat-ml-5-5` looks like a namespace plus a name, but the catalog has
207
+ // neither, so the id must stay unmatched rather than resolve to a guess.
208
+ assert.equal(matchModel(reseller, 'raccoon/raccoon-chat-ml-5-5'), undefined)
209
+ assert.equal(matchModel(reseller, 'raccoon/raccoon-19b265'), undefined)
210
+ })
211
+
212
+ test('a model whose own name looks prefixed is not peeled', () => {
213
+ // `kimi-k3` matches exactly, so the `kimi-` shape is never treated as a
214
+ // namespace: an exact hit always wins over peeling.
215
+ const hit = matchModel(reseller, 'kimi-k3')
216
+ assert.equal(hit?.modelId, 'kimi-k3')
217
+ assert.notEqual(hit?.via, 'vendor')
218
+ })
219
+
220
+ test('an exact id outranks a reseller peel', () => {
221
+ // `sensenova-6.8-flash-lite` is itself a catalog id, so it is found as one;
222
+ // nothing tries to read `sensenova-` as a namespace.
223
+ const hit = matchModel(reseller, 'sensenova-6.8-flash-lite')
224
+ assert.equal(hit?.provider, 'sensenova')
225
+ assert.equal(hit?.via, 'exact')
226
+ })
227
+
228
+ test('a namespaced entry resolves to a first-party provider when one exists', () => {
229
+ // Both 302ai and deepseek publish `deepseek-flash`; the vendor entry wins.
230
+ const hit = matchModel(reseller, 'deepseek-flash')
231
+ assert.equal(hit?.provider, 'deepseek')
232
+ })
233
+
234
+ // --- a label's word gaps, and casing differences between providers ---
235
+
236
+ /** Catalog shaped like the Qwen entries: one model, two providers, two casings. */
237
+ const qwen = catalogOf([
238
+ ['alibaba', 'qwen3.8-flash'],
239
+ ['scnet-token-plan', 'Qwen3.8-Flash'],
240
+ ['alibaba', 'qwen3.8-max'],
241
+ ])
242
+
243
+ test('a label whose word gap is a space reaches the dashed id', () => {
244
+ // The picker shows `Qwen3.8 Flash`; the catalog has `qwen3.8-flash`. No case,
245
+ // version or marker rule bridges a space, so the gap is written as a dash.
246
+ const hit = matchModel(qwen, 'auto/Qwen3.8 Flash', {}, '【auto】 Qwen3.8 Flash')
247
+ assert.equal(hit?.modelId.toLowerCase(), 'qwen3.8-flash')
248
+ assert.equal(hit?.via, 'spelling')
249
+ })
250
+
251
+ test('the same gap rule works on the id alone', () => {
252
+ const hit = matchModel(qwen, 'Qwen3.8 Max')
253
+ assert.equal(hit?.modelId.toLowerCase(), 'qwen3.8-max')
254
+ assert.equal(hit?.via, 'spelling')
255
+ })
256
+
257
+ test('a gap that leads nowhere leaves the id unmatched', () => {
258
+ // Qwen4 is genuinely absent; the gap rule must not invent a neighbour.
259
+ assert.equal(matchModel(qwen, 'auto/Qwen4', {}, '【auto】 Qwen4'), undefined)
260
+ })
261
+
262
+ test('casing never hands the choice to a reseller', () => {
263
+ // `scnet-token-plan` publishes the id capitalised exactly like the label and
264
+ // `alibaba` publishes it lower-case. The verbatim hit must not narrow the
265
+ // field to the reseller, or its numbers become the model's numbers.
266
+ const hit = matchModel(qwen, 'Qwen3.8 Flash')
267
+ assert.equal(hit?.provider, 'alibaba')
268
+ })
@@ -0,0 +1,88 @@
1
+ /**
2
+ * Unit tests for the name-redirect rule.
3
+ *
4
+ * Run with `node --experimental-strip-types --test scripts/test-rename.mjs`
5
+ * (Node 24 strips types by default). The rule is pure, so this needs no
6
+ * profile, no settings service, and no network.
7
+ */
8
+ import test from 'node:test'
9
+ import assert from 'node:assert/strict'
10
+ import { planNames, redirectName, renameModels, sourcePrefixOf } from '../src/client/rename.ts'
11
+
12
+ test('sourcePrefixOf reads the first path segment', () => {
13
+ assert.equal(sourcePrefixOf('qoder/qfmodel'), 'qoder')
14
+ assert.equal(sourcePrefixOf('qoder-cn/qfmodel'), 'qoder-cn')
15
+ assert.equal(sourcePrefixOf('qoder/gj/qwen3.8'), 'qoder')
16
+ assert.equal(sourcePrefixOf('standalone'), '')
17
+ assert.equal(sourcePrefixOf('/leading'), '')
18
+ })
19
+
20
+ test('redirectName prepends the prefix in full-width brackets', () => {
21
+ const row = redirectName('qoder/qfmodel', 'Qwen3.8-Flash')
22
+ assert.deepEqual(row, { prefix: 'qoder', before: 'Qwen3.8-Flash', after: '【qoder】 Qwen3.8-Flash', changed: true })
23
+ })
24
+
25
+ test('redirectName is idempotent', () => {
26
+ const row = redirectName('qoder/qfmodel', '【qoder】 Qwen3.8-Flash')
27
+ assert.equal(row.after, '【qoder】 Qwen3.8-Flash')
28
+ assert.equal(row.changed, false)
29
+ })
30
+
31
+ test('redirectName normalizes the earlier half-width spelling', () => {
32
+ const row = redirectName('qoder/qfmodel', 'Qwen3.8-Flash (qoder)')
33
+ assert.equal(row.after, '【qoder】 Qwen3.8-Flash')
34
+ assert.equal(row.changed, true)
35
+ })
36
+
37
+ test('redirectName moves a leading prefix to the front as a bracket', () => {
38
+ const row = redirectName('beichen/gpt-6-astra', 'beichen/gpt-6-astra')
39
+ assert.equal(row.before, 'beichen/gpt-6-astra')
40
+ assert.equal(row.after, '【beichen】 gpt-6-astra')
41
+ assert.equal(row.changed, true)
42
+ })
43
+
44
+ test('redirectName cleans a label that has the prefix in it', () => {
45
+ const row = redirectName('qoder-cn/Qwen3.8-Flash', 'qoder-cn/Qwen3.8-Flash')
46
+ assert.equal(row.after, '【qoder-cn】 Qwen3.8-Flash')
47
+ })
48
+
49
+ test('redirectName is idempotent after a leading-prefix cleanup', () => {
50
+ const first = redirectName('beichen/gpt-6-astra', 'beichen/gpt-6-astra')
51
+ const second = redirectName('beichen/gpt-6-astra', first.after)
52
+ assert.equal(second.after, first.after)
53
+ assert.equal(second.changed, false)
54
+ })
55
+
56
+ test('redirectName adds 【other】 prefix when id has no prefix', () => {
57
+ const row = redirectName('standalone', 'Bare')
58
+ assert.equal(row.prefix, 'other')
59
+ assert.equal(row.after, '【other】 Bare')
60
+ assert.equal(row.changed, true)
61
+ })
62
+
63
+ test('redirectName falls back to the bare id when the name is blank', () => {
64
+ const row = redirectName('qoder/unnamed', '')
65
+ assert.equal(row.before, 'qoder/unnamed')
66
+ assert.equal(row.after, '【qoder】 unnamed')
67
+ })
68
+
69
+ test('same-name models from different sources become distinguishable', () => {
70
+ const rows = planNames([
71
+ { id: 'qoder/qfmodel', name: 'Qwen3.8-Flash' },
72
+ { id: 'qoder-cn/qfmodel', name: 'Qwen3.8-Flash' },
73
+ ])
74
+ assert.deepEqual(rows.map(row => row.after), ['【qoder】 Qwen3.8-Flash', '【qoder-cn】 Qwen3.8-Flash'])
75
+ })
76
+
77
+ test('renameModels only touches selected ids that actually change', () => {
78
+ const models = [
79
+ { id: 'qoder/qfmodel', name: 'Qwen3.8-Flash', contextWindow: 128000 },
80
+ { id: 'unknown/m1', name: '【unknown】 already' },
81
+ ]
82
+ const { next, written } = renameModels(models, new Set(['qoder/qfmodel', 'unknown/m1']))
83
+ assert.equal(written, 1)
84
+ assert.equal(next[0].name, '【qoder】 Qwen3.8-Flash')
85
+ assert.equal(next[0].contextWindow, 128000, 'untouched fields survive')
86
+ assert.equal(next[1].name, '【unknown】 already')
87
+ assert.equal(models[0].name, 'Qwen3.8-Flash', 'input is not mutated')
88
+ })
@@ -0,0 +1,82 @@
1
+ /**
2
+ * Unit tests for the two model-list orders.
3
+ *
4
+ * Run with `node --experimental-strip-types --test scripts/test-sort.mjs`.
5
+ */
6
+ import test from 'node:test'
7
+ import assert from 'node:assert/strict'
8
+ import { redirectedSortKey, sortByModelId } from '../src/sort.ts'
9
+
10
+ /** Three models where the two orders disagree on purpose. */
11
+ const models = [
12
+ { id: 'qoder-cn/GLM-5.3', name: 'GLM-5.3【qoder-cn】' },
13
+ { id: 'qoder/qfmodel', name: 'Qwen3.8-Flash【qoder】' },
14
+ { id: 'antigravity/gemini-3.8-flash', name: 'Gemini 3.8 Flash【antigravity】' },
15
+ ]
16
+
17
+ const idsOf = (list) => list.map(model => model.id)
18
+
19
+ test('redirectedSortKey extracts vendor and model name', () => {
20
+ const key1 = redirectedSortKey('qoder/qfmodel', '【qoder】 Qwen3.8-Flash')
21
+ assert.deepEqual(key1, ['qoder', 'qwen3.8-flash'])
22
+
23
+ const key2 = redirectedSortKey('workbuddy-ai/model', '【workbuddy-ai】 GLM-5.3-Flash (Max)')
24
+ assert.deepEqual(key2, ['workbuddy-ai', 'glm-5.3-flash (max)'])
25
+
26
+ const key3 = redirectedSortKey('standalone', '【other】 My Model')
27
+ assert.deepEqual(key3, ['other', 'my model'])
28
+ })
29
+
30
+ test('prefix order groups by vendor first', () => {
31
+ const sorted = sortByModelId(models, model => model.id)
32
+ // `qoder` sorts before `qoder-cn`: a shorter string precedes its own prefix.
33
+ assert.deepEqual(idsOf(sorted), [
34
+ 'antigravity/gemini-3.8-flash',
35
+ 'qoder/qfmodel',
36
+ 'qoder-cn/GLM-5.3',
37
+ ])
38
+ })
39
+
40
+ test('redirected order groups by prefix, then sorts by name', () => {
41
+ // With redirected mode, we group by source prefix first (in brackets), then sort by name.
42
+ // The test models have: antigravity, qoder, qoder-cn prefixes, so alphabetically: antigravity, qoder, qoder-cn
43
+ const sorted = sortByModelId(models, model => model.id, model => model.name, 'redirected')
44
+ assert.deepEqual(idsOf(sorted), [
45
+ 'antigravity/gemini-3.8-flash',
46
+ 'qoder/qfmodel',
47
+ 'qoder-cn/GLM-5.3',
48
+ ])
49
+ })
50
+
51
+ test('the two orders really differ for a mixed list', () => {
52
+ const list = [
53
+ { id: 'zeta/kimi-k3', name: 'Kimi-K3【zeta】' },
54
+ { id: 'alpha/glm-5.3', name: 'GLM-5.3【alpha】' },
55
+ ]
56
+ assert.deepEqual(idsOf(sortByModelId(list, model => model.id)), ['alpha/glm-5.3', 'zeta/kimi-k3'])
57
+ assert.deepEqual(
58
+ idsOf(sortByModelId(list, model => model.id, model => model.name, 'redirected')),
59
+ ['alpha/glm-5.3', 'zeta/kimi-k3'],
60
+ )
61
+ })
62
+
63
+ test('sorting never mutates the input', () => {
64
+ const before = idsOf(models)
65
+ sortByModelId(models, model => model.id, model => model.name, 'redirected')
66
+ assert.deepEqual(idsOf(models), before)
67
+ })
68
+
69
+ test('redirected order is consistent whether names are already renamed', () => {
70
+ const plain = [
71
+ { id: 'qoder-cn/x', name: 'Name' },
72
+ { id: 'qoder/x', name: 'Name' },
73
+ ]
74
+ const renamed = [
75
+ { id: 'qoder-cn/x', name: 'Name【qoder-cn】' },
76
+ { id: 'qoder/x', name: 'Name【qoder】' },
77
+ ]
78
+ // Both should sort with qoder before qoder-cn (vendor prefix order)
79
+ const order = list => idsOf(sortByModelId(list, model => model.id, model => model.name, 'redirected'))
80
+ assert.deepEqual(order(plain), ['qoder/x', 'qoder-cn/x'])
81
+ assert.deepEqual(order(renamed), ['qoder/x', 'qoder-cn/x'])
82
+ })