dsh-context-compression-improved 0.6.5 → 0.7.0-beta.2

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.
Files changed (70) hide show
  1. package/.github/workflows/ci.yml +39 -39
  2. package/CHANGELOG.ja.md +217 -211
  3. package/CHANGELOG.ko.md +211 -205
  4. package/CHANGELOG.md +392 -350
  5. package/CHANGELOG.zh.md +196 -190
  6. package/README.md +97 -96
  7. package/README.zh.md +86 -85
  8. package/eslint.config.js +76 -30
  9. package/package.json +3 -1
  10. package/packages/selector/dsh.plugin.json +1 -1
  11. package/packages/selector/lib/client.js +747 -3
  12. package/packages/selector/lib/index.js +92 -1
  13. package/packages/selector/lib/{advisor-state.js → intent-gate.js} +91 -3
  14. package/packages/selector/lib/profiles.d.ts +9 -0
  15. package/packages/selector/lib/pruner.d.ts +59 -0
  16. package/packages/selector/lib/pruner.js +410 -8
  17. package/packages/selector/src/client/decode.ts +53 -49
  18. package/packages/selector/src/client/index.ts +30 -0
  19. package/packages/selector/src/client/locales/de.ts +98 -0
  20. package/packages/selector/src/client/locales/es.ts +98 -0
  21. package/packages/selector/src/client/locales/fr.ts +98 -0
  22. package/packages/selector/src/client/locales/it.ts +98 -0
  23. package/packages/selector/src/client/locales/ja.ts +98 -0
  24. package/packages/selector/src/client/locales/ko.ts +98 -0
  25. package/packages/selector/src/client/locales/ru.ts +98 -0
  26. package/packages/selector/src/deepseek-v4-tokenizer.ts +205 -205
  27. package/packages/selector/src/index.ts +418 -411
  28. package/packages/selector/src/preset-overlay.ts +629 -626
  29. package/packages/selector/src/profiles.ts +414 -394
  30. package/packages/selector/src/pruner/content.ts +198 -198
  31. package/packages/selector/src/pruner/state.ts +57 -50
  32. package/packages/selector/src/pruner.ts +2665 -2421
  33. package/packages/selector/src/runtime/audit.ts +303 -286
  34. package/packages/selector/src/runtime/config.ts +731 -706
  35. package/packages/selector/src/runtime/reducers.ts +1674 -1674
  36. package/packages/selector/src/runtime/summary-command.ts +133 -0
  37. package/packages/selector/src/runtime/tokenpilot/advisor-prompt.ts +233 -188
  38. package/packages/selector/src/runtime/tokenpilot/advisor-state.ts +204 -149
  39. package/packages/selector/src/runtime/tokenpilot/intent-fold.ts +53 -0
  40. package/packages/selector/src/runtime/tokenpilot/intent-gate.ts +65 -0
  41. package/packages/selector/src/runtime/tokenpilot/intent-input-mask.ts +125 -0
  42. package/packages/selector/src/runtime/tokenpilot/intent-range.ts +37 -0
  43. package/packages/selector/src/runtime/tokenpilot/locator.ts +128 -128
  44. package/packages/selector/src/runtime/tokenpilot/sidechannel.ts +306 -303
  45. package/packages/selector/src/runtime/types.ts +358 -346
  46. package/packages/selector/tests/auto-compact.client.spec.tsx +214 -213
  47. package/packages/selector/tests/built/client-artifact.spec.ts +61 -59
  48. package/packages/selector/tests/code-skeleton.client.spec.ts +94 -93
  49. package/packages/selector/tests/custom-contract.client.spec.ts +223 -222
  50. package/packages/selector/tests/estimator-channel.client.spec.tsx +252 -252
  51. package/packages/selector/tests/preset-options-write.client.spec.ts +188 -187
  52. package/packages/selector/tests/profiles.client.spec.tsx +436 -434
  53. package/packages/selector/tests/runtime/audit.spec.ts +275 -274
  54. package/packages/selector/tests/runtime/auto-compact-config.spec.ts +523 -523
  55. package/packages/selector/tests/runtime/deepseek-v4-tokenizer.spec.ts +122 -122
  56. package/packages/selector/tests/runtime/intent-gate.spec.ts +62 -0
  57. package/packages/selector/tests/runtime/intent-summary.spec.ts +156 -0
  58. package/packages/selector/tests/runtime/public/public-runtime.spec.ts +5 -0
  59. package/packages/selector/tests/runtime/summary-command.spec.ts +154 -0
  60. package/packages/selector/tests/standing-generation.host.spec.ts +15 -35
  61. package/scripts/packed-components-smoke.mjs +734 -732
  62. package/scripts/packed-install-e2e.mjs +1275 -1273
  63. package/scripts/verify-release.mjs +318 -317
  64. package/tests/TEST_INVENTORY.md +41 -41
  65. package/tsconfig.tests.json +17 -15
  66. package/vitest.config.ts +57 -43
  67. package/.agents-stash-064.patch +0 -39
  68. package/_dsh_devdeps.json +0 -50
  69. package/_verify064/package/package.json +0 -162
  70. package/_verify064/package/packages/selector/src/client/index.ts +0 -232
@@ -1,252 +1,252 @@
1
- // @vitest-environment jsdom
2
-
3
- import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react'
4
- import { useSyncExternalStore } from 'react'
5
- import { afterEach, describe, expect, it, vi } from 'vitest'
6
- import {
7
- ContextCompressionSettingsSection,
8
- type CompressionProfileSelectorProps,
9
- type ContextCompressionSettings,
10
- } from '../src/client/CompressionProfileSelector.tsx'
11
- import { en } from '../src/client/locales.ts'
12
-
13
- afterEach(() => {
14
- cleanup()
15
- vi.unstubAllGlobals()
16
- })
17
-
18
- interface SettingsScopeSnapshot<T> {
19
- status: 'loading' | 'ready' | 'unavailable'
20
- value: T | undefined
21
- base: T | undefined
22
- user: T | undefined
23
- revision: number | undefined
24
- writable: boolean
25
- mode: 'host' | 'memory'
26
- }
27
-
28
- interface SnapshotStore<T> {
29
- readonly getSnapshot: () => T
30
- readonly subscribe: (listener: () => void) => () => void
31
- }
32
-
33
- function createSnapshotStore<T>(initial: T): SnapshotStore<T> {
34
- const snapshot = structuredClone(initial)
35
- const listeners = new Set<() => void>()
36
- return {
37
- getSnapshot: () => snapshot,
38
- subscribe(listener) {
39
- listeners.add(listener)
40
- return () => { listeners.delete(listener) }
41
- },
42
- }
43
- }
44
-
45
- function bindSnapshotSelector<T>(store: SnapshotStore<T>) {
46
- return function useSnapshotSelector<U>(selector: (snapshot: T) => U): U {
47
- return useSyncExternalStore(
48
- store.subscribe,
49
- () => selector(store.getSnapshot()),
50
- () => selector(store.getSnapshot()),
51
- )
52
- }
53
- }
54
-
55
- const DEFAULT_CUSTOM = {
56
- version: 1,
57
- unit: 'tokens',
58
- fresh: { enabled: true, trigger: 4_096, target: 2_048 },
59
- aggregate: { enabled: true, trigger: 16_384, target: 8_192 },
60
- history: {
61
- enabled: true,
62
- trigger: 32_768,
63
- keepRecentTurns: 2,
64
- keepRecent: 16_384,
65
- minReclaim: 8_192,
66
- },
67
- prefixPolicy: 'pressure-break',
68
- } as const satisfies ContextCompressionSettings['custom']
69
-
70
- const t = (key: string) => en[key as keyof typeof en] ?? key
71
-
72
- const PROVIDER_LABEL = 'Provider'
73
- const CHANNEL_LABEL = 'Channel'
74
- const MODEL_LABEL = 'Model'
75
- const API_KEY_LABEL = 'API key (write-only, never echoed)'
76
- const BASE_URL_LABEL = 'Endpoint base URL (/v1)'
77
-
78
- const CATALOG = {
79
- ok: true,
80
- providers: [
81
- {
82
- id: 'local-35b',
83
- name: 'local-35b',
84
- models: [{ id: 'Qwen3.6-35B-A3B', name: 'Qwen3.6-35B-A3B' }, { id: 'Qwen38-27B', name: 'Qwen38-27B' }],
85
- },
86
- { id: 'deepseek-official', name: 'DeepSeek', models: [{ id: 'deepseek-flash', name: 'DeepSeek-V41-Flash' }] },
87
- ],
88
- selection: { provider: 'deepseek-official', model: 'deepseek-flash' },
89
- }
90
-
91
- function mountEstimator(
92
- presetOptions: ContextCompressionSettings['presetOptions'],
93
- options: { readonly catalog?: unknown, readonly profile?: ContextCompressionSettings['profile'] } = {},
94
- ) {
95
- const state = createSnapshotStore<SettingsScopeSnapshot<ContextCompressionSettings>>({
96
- status: 'ready',
97
- value: {
98
- profile: options.profile ?? 'tokenpilot-inspired',
99
- custom: structuredClone(DEFAULT_CUSTOM),
100
- autoCompact: { thresholdPercent: 80 },
101
- codeSkeleton: { enabled: false },
102
- presetOptions: presetOptions ?? {},
103
- },
104
- base: undefined,
105
- user: undefined,
106
- revision: 0,
107
- writable: true,
108
- mode: 'host',
109
- })
110
- const savePresetOptions = vi.fn(() => Promise.resolve())
111
- if (options.catalog !== undefined) {
112
- vi.stubGlobal('fetch', vi.fn(() => Promise.resolve({
113
- ok: true,
114
- json: () => Promise.resolve(options.catalog),
115
- })))
116
- }
117
- render(<ContextCompressionSettingsSection {...({
118
- useCompression: bindSnapshotSelector(state),
119
- select: vi.fn(() => Promise.resolve()),
120
- saveCustom: vi.fn(() => Promise.resolve()),
121
- resetCustom: vi.fn(() => Promise.resolve()),
122
- saveAutoCompact: vi.fn(() => Promise.resolve()),
123
- saveCodeSkeleton: vi.fn(() => Promise.resolve()),
124
- savePresetOptions,
125
- t,
126
- } as unknown as CompressionProfileSelectorProps)} />)
127
- return { savePresetOptions }
128
- }
129
-
130
- describe('estimator channel card', () => {
131
- it('host mode fills comboboxes from the catalog, keeps custom typing, and asks for no API key', async () => {
132
- const { savePresetOptions } = mountEstimator({ estimatorMode: 'host' }, { catalog: CATALOG })
133
-
134
- // Provider/model are always comboboxes (dsh-perm-gate receiver pattern):
135
- // a catalog pick fills the field, and the same input accepts a custom id.
136
- const provider = screen.getByLabelText<HTMLInputElement>(PROVIDER_LABEL)
137
- const model = screen.getByLabelText<HTMLInputElement>(MODEL_LABEL)
138
- await waitFor(() => {
139
- const providerOptions = document.querySelectorAll('#estimator-provider-options option')
140
- expect([...providerOptions].map(option => (option as HTMLOptionElement).value))
141
- .toEqual(['local-35b', 'deepseek-official'])
142
- expect(provider.getAttribute('list')).toBe('estimator-provider-options')
143
- expect(model.getAttribute('list')).toBe('estimator-model-options')
144
- })
145
- expect(screen.getByText(/Effective route: deepseek-official \/ deepseek-flash/)).not.toBeNull()
146
- expect(screen.getByText(/no API key is needed/)).not.toBeNull()
147
-
148
- // No credential input and no base-URL field on this channel.
149
- expect(screen.queryByLabelText(API_KEY_LABEL)).toBeNull()
150
- expect(screen.queryByLabelText(BASE_URL_LABEL)).toBeNull()
151
- expect(document.querySelectorAll('input[type="password"]').length).toBe(0)
152
-
153
- // A datalist pick fires change without blur: it commits immediately.
154
- fireEvent.change(provider, { target: { value: 'local-35b' } })
155
- await waitFor(() => {
156
- expect(savePresetOptions).toHaveBeenCalledWith({ estimatorProvider: 'local-35b' })
157
- })
158
- // Free typing only commits on blur, so mid-edit keystrokes never save.
159
- fireEvent.change(provider, { target: { value: 'my-custom-group' } })
160
- expect(savePresetOptions).not.toHaveBeenCalledWith({ estimatorProvider: 'my-custom-group' })
161
- fireEvent.blur(provider)
162
- await waitFor(() => {
163
- expect(savePresetOptions).toHaveBeenCalledWith({ estimatorProvider: 'my-custom-group' })
164
- })
165
- })
166
-
167
- it('offers the catalog models of the chosen provider and commits one', async () => {
168
- const { savePresetOptions } = mountEstimator(
169
- { estimatorMode: 'host', estimatorProvider: 'local-35b' },
170
- { catalog: CATALOG },
171
- )
172
- // Query before the catalog lands: once the datalist has options, the
173
- // label's textContent includes them and getByLabelText's exact match
174
- // would no longer see a bare "Model".
175
- const model = screen.getByLabelText<HTMLInputElement>(MODEL_LABEL)
176
- expect(model.value).toBe('')
177
- await waitFor(() => {
178
- const modelOptions = document.querySelectorAll('#estimator-model-options option')
179
- expect([...modelOptions].map(option => (option as HTMLOptionElement).value))
180
- .toEqual(['Qwen3.6-35B-A3B', 'Qwen38-27B'])
181
- })
182
- expect(screen.getByText(/Effective route: local-35b/)).not.toBeNull()
183
-
184
- fireEvent.change(model, { target: { value: 'Qwen38-27B' } })
185
- await waitFor(() => {
186
- expect(savePresetOptions).toHaveBeenCalledWith({ estimatorModel: 'Qwen38-27B' })
187
- })
188
- })
189
-
190
- it('still accepts custom names — without a credential field — when no catalog is served', async () => {
191
- vi.stubGlobal('fetch', vi.fn(() => Promise.reject(new Error('no route'))))
192
- const { savePresetOptions } = mountEstimator({ estimatorMode: 'host' })
193
-
194
- const provider = screen.getByLabelText<HTMLInputElement>(PROVIDER_LABEL)
195
- // The model field stays present and editable without a served catalog.
196
- expect(screen.getByLabelText<HTMLInputElement>(MODEL_LABEL)).toBeTruthy()
197
- expect(document.querySelectorAll('#estimator-provider-options option').length).toBe(0)
198
- expect(document.querySelectorAll('#estimator-model-options option').length).toBe(0)
199
- fireEvent.change(provider, { target: { value: 'manual-group' } })
200
- fireEvent.blur(provider)
201
- await waitFor(() => {
202
- expect(savePresetOptions).toHaveBeenCalledWith({ estimatorProvider: 'manual-group' })
203
- })
204
- expect(screen.queryByLabelText(API_KEY_LABEL)).toBeNull()
205
- expect(screen.getByText(/no API key is needed/)).not.toBeNull()
206
- expect(screen.getByText(/not determined yet/)).not.toBeNull()
207
- })
208
-
209
- it('keeps the endpoint and key fields on the direct channel alone', () => {
210
- mountEstimator({ estimatorMode: 'direct' })
211
-
212
- expect(screen.getByLabelText(BASE_URL_LABEL)).not.toBeNull()
213
- expect(screen.getByLabelText(MODEL_LABEL).tagName).toBe('INPUT')
214
- expect(screen.getByLabelText(API_KEY_LABEL)).not.toBeNull()
215
- expect(document.querySelectorAll('input[type="password"]').length).toBe(1)
216
- expect(screen.queryByLabelText(PROVIDER_LABEL)).toBeNull()
217
- expect(screen.queryByText(/no API key is needed/)).toBeNull()
218
- })
219
-
220
- it('renders no channel controls while the estimator is off', () => {
221
- mountEstimator({})
222
- expect(screen.queryByLabelText(API_KEY_LABEL)).toBeNull()
223
- expect(screen.queryByLabelText(BASE_URL_LABEL)).toBeNull()
224
- expect(screen.queryByLabelText(PROVIDER_LABEL)).toBeNull()
225
- })
226
-
227
- // Regression guard for the first real-machine report: the card used to render
228
- // nothing at all off tokenpilot-inspired, which reads as a missing feature.
229
- it('keeps the estimator section, and names the unlocking profile, off tokenpilot-inspired', () => {
230
- mountEstimator({}, { profile: 'balanced' })
231
-
232
- // The heading and its stable anchor survive, so the panel is still findable
233
- // where the reader last saw it.
234
- expect(document.getElementById('context-compression-estimator-title')?.textContent)
235
- .toBe('Estimator (optional)')
236
- // The notice names the current profile and the profile that unlocks the card.
237
- const notice = screen.getByText(/ships only with the TokenPilot-inspired profile/)
238
- expect(notice.textContent).toContain('“Balanced”')
239
- expect(notice.textContent).toContain('Select TokenPilot-inspired')
240
- // Gating semantics are unchanged: no channel control exists here.
241
- expect(screen.queryByLabelText(CHANNEL_LABEL)).toBeNull()
242
- expect(screen.queryByLabelText(PROVIDER_LABEL)).toBeNull()
243
- expect(screen.queryByLabelText(API_KEY_LABEL)).toBeNull()
244
- expect(document.querySelectorAll('input[list]').length).toBe(0)
245
- })
246
-
247
- it('drops the inactive notice once tokenpilot-inspired owns the selector', () => {
248
- mountEstimator({})
249
- expect(screen.queryByText(/ships only with the TokenPilot-inspired profile/)).toBeNull()
250
- expect(screen.getByLabelText(CHANNEL_LABEL)).not.toBeNull()
251
- })
252
- })
1
+ // @vitest-environment jsdom
2
+
3
+ import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react'
4
+ import { useSyncExternalStore } from 'react'
5
+ import { afterEach, describe, expect, it, vi } from 'vitest'
6
+ import {
7
+ ContextCompressionSettingsSection,
8
+ type ContextCompressionSettings,
9
+ } from '../src/client/CompressionProfileSelector.tsx'
10
+ import { en } from '../src/client/locales.ts'
11
+
12
+ afterEach(() => {
13
+ cleanup()
14
+ vi.unstubAllGlobals()
15
+ })
16
+
17
+ interface SettingsScopeSnapshot<T> {
18
+ status: 'loading' | 'ready' | 'unavailable'
19
+ value: T | undefined
20
+ base: T | undefined
21
+ user: T | undefined
22
+ revision: number | undefined
23
+ writable: boolean
24
+ mode: 'host' | 'memory'
25
+ }
26
+
27
+ interface SnapshotStore<T> {
28
+ readonly getSnapshot: () => T
29
+ readonly subscribe: (listener: () => void) => () => void
30
+ }
31
+
32
+ function createSnapshotStore<T>(initial: T): SnapshotStore<T> {
33
+ const snapshot = structuredClone(initial)
34
+ const listeners = new Set<() => void>()
35
+ return {
36
+ getSnapshot: () => snapshot,
37
+ subscribe(listener) {
38
+ listeners.add(listener)
39
+ return () => { listeners.delete(listener) }
40
+ },
41
+ }
42
+ }
43
+
44
+ function bindSnapshotSelector<T>(store: SnapshotStore<T>) {
45
+ return function useSnapshotSelector<U>(selector: (snapshot: T) => U): U {
46
+ return useSyncExternalStore(
47
+ store.subscribe,
48
+ () => selector(store.getSnapshot()),
49
+ () => selector(store.getSnapshot()),
50
+ )
51
+ }
52
+ }
53
+
54
+ const DEFAULT_CUSTOM = {
55
+ version: 1,
56
+ unit: 'tokens',
57
+ fresh: { enabled: true, trigger: 4_096, target: 2_048 },
58
+ aggregate: { enabled: true, trigger: 16_384, target: 8_192 },
59
+ history: {
60
+ enabled: true,
61
+ trigger: 32_768,
62
+ keepRecentTurns: 2,
63
+ keepRecent: 16_384,
64
+ minReclaim: 8_192,
65
+ },
66
+ prefixPolicy: 'pressure-break',
67
+ } as const satisfies ContextCompressionSettings['custom']
68
+
69
+ const t = (key: string) => en[key as keyof typeof en] ?? key
70
+
71
+ const PROVIDER_LABEL = 'Provider'
72
+ const CHANNEL_LABEL = 'Channel'
73
+ const MODEL_LABEL = 'Model'
74
+ const API_KEY_LABEL = 'API key (write-only, never echoed)'
75
+ const BASE_URL_LABEL = 'Endpoint base URL (/v1)'
76
+
77
+ const CATALOG = {
78
+ ok: true,
79
+ providers: [
80
+ {
81
+ id: 'local-35b',
82
+ name: 'local-35b',
83
+ models: [{ id: 'Qwen3.6-35B-A3B', name: 'Qwen3.6-35B-A3B' }, { id: 'Qwen38-27B', name: 'Qwen38-27B' }],
84
+ },
85
+ { id: 'deepseek-official', name: 'DeepSeek', models: [{ id: 'deepseek-flash', name: 'DeepSeek-V41-Flash' }] },
86
+ ],
87
+ selection: { provider: 'deepseek-official', model: 'deepseek-flash' },
88
+ }
89
+
90
+ function mountEstimator(
91
+ presetOptions: ContextCompressionSettings['presetOptions'],
92
+ options: { readonly catalog?: unknown, readonly profile?: ContextCompressionSettings['profile'] } = {},
93
+ ) {
94
+ const state = createSnapshotStore<SettingsScopeSnapshot<ContextCompressionSettings>>({
95
+ status: 'ready',
96
+ value: {
97
+ profile: options.profile ?? 'tokenpilot-inspired',
98
+ custom: structuredClone(DEFAULT_CUSTOM),
99
+ autoCompact: { thresholdPercent: 80 },
100
+ codeSkeleton: { enabled: false },
101
+ intentSummary: { enabled: false },
102
+ presetOptions: presetOptions ?? {},
103
+ },
104
+ base: undefined,
105
+ user: undefined,
106
+ revision: 0,
107
+ writable: true,
108
+ mode: 'host',
109
+ })
110
+ const savePresetOptions = vi.fn(() => Promise.resolve())
111
+ if (options.catalog !== undefined) {
112
+ vi.stubGlobal('fetch', vi.fn(() => Promise.resolve({
113
+ ok: true,
114
+ json: () => Promise.resolve(options.catalog),
115
+ })))
116
+ }
117
+ render(<ContextCompressionSettingsSection {...({
118
+ useCompression: bindSnapshotSelector(state),
119
+ select: vi.fn(() => Promise.resolve()),
120
+ saveCustom: vi.fn(() => Promise.resolve()),
121
+ resetCustom: vi.fn(() => Promise.resolve()),
122
+ saveAutoCompact: vi.fn(() => Promise.resolve()),
123
+ saveCodeSkeleton: vi.fn(() => Promise.resolve()),
124
+ savePresetOptions,
125
+ t,
126
+ })} />)
127
+ return { savePresetOptions }
128
+ }
129
+
130
+ describe('estimator channel card', () => {
131
+ it('host mode fills comboboxes from the catalog, keeps custom typing, and asks for no API key', async () => {
132
+ const { savePresetOptions } = mountEstimator({ estimatorMode: 'host' }, { catalog: CATALOG })
133
+
134
+ // Provider/model are always comboboxes (dsh-perm-gate receiver pattern):
135
+ // a catalog pick fills the field, and the same input accepts a custom id.
136
+ const provider = screen.getByLabelText<HTMLInputElement>(PROVIDER_LABEL)
137
+ const model = screen.getByLabelText<HTMLInputElement>(MODEL_LABEL)
138
+ await waitFor(() => {
139
+ const providerOptions = document.querySelectorAll('#estimator-provider-options option')
140
+ expect([...providerOptions].map(option => (option as HTMLOptionElement).value))
141
+ .toEqual(['local-35b', 'deepseek-official'])
142
+ expect(provider.getAttribute('list')).toBe('estimator-provider-options')
143
+ expect(model.getAttribute('list')).toBe('estimator-model-options')
144
+ })
145
+ expect(screen.getByText(/Effective route: deepseek-official \/ deepseek-flash/)).not.toBeNull()
146
+ expect(screen.getByText(/no API key is needed/)).not.toBeNull()
147
+
148
+ // No credential input and no base-URL field on this channel.
149
+ expect(screen.queryByLabelText(API_KEY_LABEL)).toBeNull()
150
+ expect(screen.queryByLabelText(BASE_URL_LABEL)).toBeNull()
151
+ expect(document.querySelectorAll('input[type="password"]').length).toBe(0)
152
+
153
+ // A datalist pick fires change without blur: it commits immediately.
154
+ fireEvent.change(provider, { target: { value: 'local-35b' } })
155
+ await waitFor(() => {
156
+ expect(savePresetOptions).toHaveBeenCalledWith({ estimatorProvider: 'local-35b' })
157
+ })
158
+ // Free typing only commits on blur, so mid-edit keystrokes never save.
159
+ fireEvent.change(provider, { target: { value: 'my-custom-group' } })
160
+ expect(savePresetOptions).not.toHaveBeenCalledWith({ estimatorProvider: 'my-custom-group' })
161
+ fireEvent.blur(provider)
162
+ await waitFor(() => {
163
+ expect(savePresetOptions).toHaveBeenCalledWith({ estimatorProvider: 'my-custom-group' })
164
+ })
165
+ })
166
+
167
+ it('offers the catalog models of the chosen provider and commits one', async () => {
168
+ const { savePresetOptions } = mountEstimator(
169
+ { estimatorMode: 'host', estimatorProvider: 'local-35b' },
170
+ { catalog: CATALOG },
171
+ )
172
+ // Query before the catalog lands: once the datalist has options, the
173
+ // label's textContent includes them and getByLabelText's exact match
174
+ // would no longer see a bare "Model".
175
+ const model = screen.getByLabelText<HTMLInputElement>(MODEL_LABEL)
176
+ expect(model.value).toBe('')
177
+ await waitFor(() => {
178
+ const modelOptions = document.querySelectorAll('#estimator-model-options option')
179
+ expect([...modelOptions].map(option => (option as HTMLOptionElement).value))
180
+ .toEqual(['Qwen3.6-35B-A3B', 'Qwen38-27B'])
181
+ })
182
+ expect(screen.getByText(/Effective route: local-35b/)).not.toBeNull()
183
+
184
+ fireEvent.change(model, { target: { value: 'Qwen38-27B' } })
185
+ await waitFor(() => {
186
+ expect(savePresetOptions).toHaveBeenCalledWith({ estimatorModel: 'Qwen38-27B' })
187
+ })
188
+ })
189
+
190
+ it('still accepts custom names — without a credential field — when no catalog is served', async () => {
191
+ vi.stubGlobal('fetch', vi.fn(() => Promise.reject(new Error('no route'))))
192
+ const { savePresetOptions } = mountEstimator({ estimatorMode: 'host' })
193
+
194
+ const provider = screen.getByLabelText<HTMLInputElement>(PROVIDER_LABEL)
195
+ // The model field stays present and editable without a served catalog.
196
+ expect(screen.getByLabelText<HTMLInputElement>(MODEL_LABEL)).toBeTruthy()
197
+ expect(document.querySelectorAll('#estimator-provider-options option').length).toBe(0)
198
+ expect(document.querySelectorAll('#estimator-model-options option').length).toBe(0)
199
+ fireEvent.change(provider, { target: { value: 'manual-group' } })
200
+ fireEvent.blur(provider)
201
+ await waitFor(() => {
202
+ expect(savePresetOptions).toHaveBeenCalledWith({ estimatorProvider: 'manual-group' })
203
+ })
204
+ expect(screen.queryByLabelText(API_KEY_LABEL)).toBeNull()
205
+ expect(screen.getByText(/no API key is needed/)).not.toBeNull()
206
+ expect(screen.getByText(/not determined yet/)).not.toBeNull()
207
+ })
208
+
209
+ it('keeps the endpoint and key fields on the direct channel alone', () => {
210
+ mountEstimator({ estimatorMode: 'direct' })
211
+
212
+ expect(screen.getByLabelText(BASE_URL_LABEL)).not.toBeNull()
213
+ expect(screen.getByLabelText(MODEL_LABEL).tagName).toBe('INPUT')
214
+ expect(screen.getByLabelText(API_KEY_LABEL)).not.toBeNull()
215
+ expect(document.querySelectorAll('input[type="password"]').length).toBe(1)
216
+ expect(screen.queryByLabelText(PROVIDER_LABEL)).toBeNull()
217
+ expect(screen.queryByText(/no API key is needed/)).toBeNull()
218
+ })
219
+
220
+ it('renders no channel controls while the estimator is off', () => {
221
+ mountEstimator({})
222
+ expect(screen.queryByLabelText(API_KEY_LABEL)).toBeNull()
223
+ expect(screen.queryByLabelText(BASE_URL_LABEL)).toBeNull()
224
+ expect(screen.queryByLabelText(PROVIDER_LABEL)).toBeNull()
225
+ })
226
+
227
+ // Regression guard for the first real-machine report: the card used to render
228
+ // nothing at all off tokenpilot-inspired, which reads as a missing feature.
229
+ it('keeps the estimator section, and names the unlocking profile, off tokenpilot-inspired', () => {
230
+ mountEstimator({}, { profile: 'balanced' })
231
+
232
+ // The heading and its stable anchor survive, so the panel is still findable
233
+ // where the reader last saw it.
234
+ expect(document.getElementById('context-compression-estimator-title')?.textContent)
235
+ .toBe('Estimator (optional)')
236
+ // The notice names the current profile and the profile that unlocks the card.
237
+ const notice = screen.getByText(/ships only with the TokenPilot-inspired profile/)
238
+ expect(notice.textContent).toContain('“Balanced”')
239
+ expect(notice.textContent).toContain('Select TokenPilot-inspired')
240
+ // Gating semantics are unchanged: no channel control exists here.
241
+ expect(screen.queryByLabelText(CHANNEL_LABEL)).toBeNull()
242
+ expect(screen.queryByLabelText(PROVIDER_LABEL)).toBeNull()
243
+ expect(screen.queryByLabelText(API_KEY_LABEL)).toBeNull()
244
+ expect(document.querySelectorAll('input[list]').length).toBe(0)
245
+ })
246
+
247
+ it('drops the inactive notice once tokenpilot-inspired owns the selector', () => {
248
+ mountEstimator({})
249
+ expect(screen.queryByText(/ships only with the TokenPilot-inspired profile/)).toBeNull()
250
+ expect(screen.getByLabelText(CHANNEL_LABEL)).not.toBeNull()
251
+ })
252
+ })