dsh-model-capability-sync 0.1.0 → 0.2.1

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,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
+ })
@@ -0,0 +1,104 @@
1
+ /**
2
+ * Unit tests for carrying a written model list back into the held descriptor.
3
+ *
4
+ * Run with `node --experimental-strip-types --test scripts/test-write-merge.mjs`.
5
+ * The bug this guards against: rename a model, then reorder, and the reorder
6
+ * rebuilds the list from the descriptor — which still held the old names —
7
+ * putting them back. Keeping the descriptor in step is what prevents it.
8
+ */
9
+ import test from 'node:test'
10
+ import assert from 'node:assert/strict'
11
+ import { withWrittenModels } from '../src/client/shared.ts'
12
+
13
+ /** One descriptor as the settings service reports it. */
14
+ function descriptorWith(models, extraProviders = {}) {
15
+ return {
16
+ ns: 'llm-pi-ai',
17
+ revision: 7,
18
+ value: {
19
+ writable: true,
20
+ providers: {
21
+ newapi: { api: 'openai-completions', models },
22
+ ...extraProviders,
23
+ },
24
+ },
25
+ }
26
+ }
27
+
28
+ test('a written list replaces the one the descriptor held', () => {
29
+ const descriptor = descriptorWith([{ id: 'a/b', name: '旧名称' }])
30
+ const written = [{ id: 'a/b', name: '【a】 新名称' }]
31
+ const next = withWrittenModels(descriptor, 'newapi', written, 8)
32
+
33
+ assert.equal(next.value.providers.newapi.models[0].name, '【a】 新名称')
34
+ assert.equal(next.revision, 8)
35
+ })
36
+
37
+ test('the descriptor the page held is not mutated', () => {
38
+ const models = [{ id: 'a/b', name: '旧名称' }]
39
+ const descriptor = descriptorWith(models)
40
+ withWrittenModels(descriptor, 'newapi', [{ id: 'a/b', name: '新名称' }], 8)
41
+
42
+ assert.equal(descriptor.value.providers.newapi.models[0].name, '旧名称')
43
+ assert.equal(descriptor.revision, 7)
44
+ })
45
+
46
+ test('other providers survive a write untouched', () => {
47
+ const other = { api: 'openai-completions', models: [{ id: 'x/y', name: '别的供应商' }] }
48
+ const descriptor = descriptorWith([{ id: 'a/b', name: '旧' }], { free: other })
49
+ const next = withWrittenModels(descriptor, 'newapi', [{ id: 'a/b', name: '新' }], 9)
50
+
51
+ assert.deepEqual(next.value.providers.free, other)
52
+ })
53
+
54
+ test('other fields of the provider and of each model survive', () => {
55
+ const descriptor = {
56
+ ns: 'llm-pi-ai',
57
+ revision: 1,
58
+ value: {
59
+ providers: {
60
+ newapi: {
61
+ api: 'openai-completions',
62
+ baseURL: 'https://example.test/v1',
63
+ models: [{ id: 'a/b', name: '旧', contextWindow: 128000, input: ['text'] }],
64
+ },
65
+ },
66
+ },
67
+ }
68
+ const written = [{ id: 'a/b', name: '【a】 新', contextWindow: 128000, input: ['text'] }]
69
+ const next = withWrittenModels(descriptor, 'newapi', written, 2)
70
+
71
+ assert.equal(next.value.providers.newapi.api, 'openai-completions')
72
+ assert.equal(next.value.providers.newapi.baseURL, 'https://example.test/v1')
73
+ assert.equal(next.value.providers.newapi.models[0].contextWindow, 128000)
74
+ })
75
+
76
+ test('a revision the service did not give is not written', () => {
77
+ const descriptor = descriptorWith([])
78
+ const next = withWrittenModels(descriptor, 'newapi', [], undefined)
79
+ assert.equal(next.revision, 7)
80
+ })
81
+
82
+ test('renaming then reordering keeps the new names', () => {
83
+ // Simulates the report: write the redirect, then write the reorder. The
84
+ // second pass must start from the renamed list, not the descriptor's old one.
85
+ const original = [
86
+ { id: 'z/x', name: 'x' },
87
+ { id: 'a/y', name: 'y' },
88
+ ]
89
+ let descriptor = descriptorWith(original)
90
+
91
+ const renamed = original.map(m => ({ ...m, name: `【${m.id.split('/')[0]}】 ${m.name}` }))
92
+ descriptor = withWrittenModels(descriptor, 'newapi', renamed, 8)
93
+
94
+ // The rebuild a sort-mode switch or a reorder does, from the descriptor.
95
+ const rebuilt = descriptor.value.providers.newapi.models
96
+ assert.deepEqual(rebuilt.map(m => m.name), ['【z】 x', '【a】 y'])
97
+
98
+ const reordered = [...rebuilt].sort((l, r) => (l.id < r.id ? -1 : 1))
99
+ descriptor = withWrittenModels(descriptor, 'newapi', reordered, 9)
100
+
101
+ // Order changed, names did not.
102
+ assert.deepEqual(descriptor.value.providers.newapi.models.map(m => m.id), ['a/y', 'z/x'])
103
+ assert.deepEqual(descriptor.value.providers.newapi.models.map(m => m.name), ['【a】 y', '【z】 x'])
104
+ })