dsh-context-compression-improved 0.7.0-beta.2 → 0.7.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.
- package/CHANGELOG.md +9 -0
- package/package.json +17 -18
- package/packages/selector/dsh.plugin.json +1 -1
- package/packages/selector/lib/client.d.ts +44 -0
- package/packages/selector/lib/client.js +1007 -13
- package/packages/selector/lib/{intent-gate.js → config.js} +799 -136
- package/packages/selector/lib/index.d.ts +13 -1
- package/packages/selector/lib/index.js +204 -3
- package/packages/selector/lib/profiles.d.ts +6 -0
- package/packages/selector/lib/pruner.d.ts +6 -0
- package/packages/selector/lib/pruner.js +90 -258
- package/packages/selector/lib/style.css +42 -0
- package/packages/selector/src/client/CompressionProfileControls.tsx +293 -226
- package/packages/selector/src/client/CompressionProfileSelector.module.css +199 -170
- package/packages/selector/src/client/CompressionProfileSelector.tsx +85 -83
- package/packages/selector/src/client/decode.ts +5 -1
- package/packages/selector/src/client/index.ts +307 -243
- package/packages/selector/src/client/locales/de.ts +43 -0
- package/packages/selector/src/client/locales/es.ts +43 -0
- package/packages/selector/src/client/locales/fr.ts +43 -0
- package/packages/selector/src/client/locales/it.ts +43 -0
- package/packages/selector/src/client/locales/ja.ts +43 -0
- package/packages/selector/src/client/locales/ko.ts +43 -0
- package/packages/selector/src/client/locales/ru.ts +43 -0
- package/packages/selector/src/client/locales.ts +282 -196
- package/packages/selector/src/client/monitor-fab.ts +336 -0
- package/packages/selector/src/client/savings-card.tsx +150 -0
- package/packages/selector/src/client/settings-section.tsx +140 -124
- package/packages/selector/src/index.ts +207 -5
- package/packages/selector/src/profiles.ts +17 -0
- package/packages/selector/src/pruner.ts +96 -2
- package/packages/selector/src/runtime/config.ts +26 -1
- package/packages/selector/src/runtime/monitor.ts +235 -0
- package/packages/selector/src/runtime/retrieve.ts +479 -457
- package/packages/selector/src/runtime/savings.ts +337 -0
- package/packages/selector/src/runtime/types.ts +7 -0
- package/packages/selector/tests/code-skeleton.client.spec.ts +2 -2
- package/packages/selector/tests/custom-contract.client.spec.ts +2 -2
- package/packages/selector/tests/estimator-channel.client.spec.tsx +2 -0
- package/packages/selector/tests/estimator-route-registration.host.spec.ts +194 -188
- package/packages/selector/tests/monitor-fab.client.spec.ts +138 -0
- package/packages/selector/tests/runtime/monitor.spec.ts +301 -0
- package/packages/selector/tests/runtime/savings.spec.ts +120 -0
- package/packages/selector/tests/settings-seat.client.spec.ts +177 -165
- package/scripts/packed-install-e2e.mjs +1 -1
- package/scripts/verify-release.mjs +1 -1
|
@@ -1,188 +1,194 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Host-side guard for the estimator catalog route.
|
|
3
|
-
*
|
|
4
|
-
* The defect this pins is a registration that never happens: the injection gate
|
|
5
|
-
* asked for more services than the handler needs, and an unsatisfied
|
|
6
|
-
* `ctx.inject` callback fails silently, so the plugin simply had no HTTP API
|
|
7
|
-
* and every request fell through to the host 404. These cases assert the two
|
|
8
|
-
* properties that failure violated — the route appears whenever `webServer` is
|
|
9
|
-
* usable, in either arrival order, and its absence is never silent.
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
import { Context } from '@deepseek-ai/cordis'
|
|
13
|
-
import { afterEach, describe, expect, it, vi } from 'vitest'
|
|
14
|
-
import { apply } from '../src/index.ts'
|
|
15
|
-
|
|
16
|
-
const CATALOG_ROUTE = '/api/dsh-context-compression-improved/estimator-catalog'
|
|
17
|
-
const LEGACY_ROUTE = '/endpoint/dsh-context-compression-improved/estimator-catalog'
|
|
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
|
-
table.
|
|
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
|
-
await settle()
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
await settle()
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
const
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
expect(
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
expect(
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Host-side guard for the estimator catalog route.
|
|
3
|
+
*
|
|
4
|
+
* The defect this pins is a registration that never happens: the injection gate
|
|
5
|
+
* asked for more services than the handler needs, and an unsatisfied
|
|
6
|
+
* `ctx.inject` callback fails silently, so the plugin simply had no HTTP API
|
|
7
|
+
* and every request fell through to the host 404. These cases assert the two
|
|
8
|
+
* properties that failure violated — the route appears whenever `webServer` is
|
|
9
|
+
* usable, in either arrival order, and its absence is never silent.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { Context } from '@deepseek-ai/cordis'
|
|
13
|
+
import { afterEach, describe, expect, it, vi } from 'vitest'
|
|
14
|
+
import { apply } from '../src/index.ts'
|
|
15
|
+
|
|
16
|
+
const CATALOG_ROUTE = '/api/dsh-context-compression-improved/estimator-catalog'
|
|
17
|
+
const LEGACY_ROUTE = '/endpoint/dsh-context-compression-improved/estimator-catalog'
|
|
18
|
+
const SAVINGS_ROUTE = '/api/dsh-context-compression-improved/savings'
|
|
19
|
+
const SAVINGS_LEGACY_ROUTE = '/endpoint/dsh-context-compression-improved/savings'
|
|
20
|
+
const MONITOR_ROUTE = '/api/dsh-context-compression-improved/monitor'
|
|
21
|
+
const MONITOR_LEGACY_ROUTE = '/endpoint/dsh-context-compression-improved/monitor'
|
|
22
|
+
|
|
23
|
+
interface RegisteredRoute {
|
|
24
|
+
kind: string
|
|
25
|
+
path: string
|
|
26
|
+
handler: (req: unknown, res: unknown) => void
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
interface FakeResponse {
|
|
30
|
+
status?: number
|
|
31
|
+
body?: string
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** The state `register` reads off its own `this`, mirroring the host service. */
|
|
35
|
+
interface WebServerStub {
|
|
36
|
+
tables: { exact: Map<string, RegisteredRoute> }
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
let ctx: Context | undefined
|
|
40
|
+
|
|
41
|
+
afterEach(async () => {
|
|
42
|
+
await ctx?.fiber.dispose()
|
|
43
|
+
ctx = undefined
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
/** Settle one macrotask so a deferred activation callback can run. */
|
|
47
|
+
const settle = (): Promise<void> => new Promise(resolve => setTimeout(resolve, 20))
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Mount a host web server stand-in that records routes and reproduces the real
|
|
51
|
+
* host's duplicate-`(kind, path)` contract, so a double registration is a
|
|
52
|
+
* failure rather than a silent overwrite.
|
|
53
|
+
*/
|
|
54
|
+
async function mountWebServer(runtime: Context, routes: RegisteredRoute[]): Promise<void> {
|
|
55
|
+
await runtime.plugin({
|
|
56
|
+
name: 'fake-webserver',
|
|
57
|
+
apply(webCtx) {
|
|
58
|
+
webCtx.provide('webServer', {
|
|
59
|
+
// Mirrors dsh-host-webserver: `register` reads its route table off
|
|
60
|
+
// `this`. A caller that detaches the method and invokes it standalone
|
|
61
|
+
// makes `this` the wrapper and fails here, exactly as the host does --
|
|
62
|
+
// which is the defect this spec exists to catch.
|
|
63
|
+
tables: { exact: new Map<string, RegisteredRoute>() },
|
|
64
|
+
register(this: WebServerStub, route: RegisteredRoute) {
|
|
65
|
+
const table = this.tables.exact
|
|
66
|
+
if (table.has(route.path)) {
|
|
67
|
+
throw new Error(`webserver: duplicate ${route.kind} route "${route.path}"`)
|
|
68
|
+
}
|
|
69
|
+
table.set(route.path, route)
|
|
70
|
+
routes.push(route)
|
|
71
|
+
return () => {}
|
|
72
|
+
},
|
|
73
|
+
})
|
|
74
|
+
},
|
|
75
|
+
})
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** Mount the estimator-side services the handler only enriches its payload with. */
|
|
79
|
+
async function mountEstimatorServices(runtime: Context): Promise<void> {
|
|
80
|
+
await runtime.plugin({
|
|
81
|
+
name: 'fake-estimator-services',
|
|
82
|
+
apply(estimatorCtx) {
|
|
83
|
+
estimatorCtx.provide('llm', { listProviders: () => [] })
|
|
84
|
+
estimatorCtx.provide('agentDefaultModel', { currentSelection: () => undefined })
|
|
85
|
+
},
|
|
86
|
+
})
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** Drive one registered route through a stand-in response. */
|
|
90
|
+
async function invoke(route: RegisteredRoute): Promise<FakeResponse> {
|
|
91
|
+
const captured: FakeResponse = {}
|
|
92
|
+
const res = {
|
|
93
|
+
writeHead(code: number) { captured.status = code },
|
|
94
|
+
end(body?: string) { if (body !== undefined) captured.body = body },
|
|
95
|
+
}
|
|
96
|
+
route.handler({ method: 'GET' }, res)
|
|
97
|
+
await settle()
|
|
98
|
+
return captured
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
describe('estimator catalog route registration', () => {
|
|
102
|
+
it('registers both route prefixes with only webServer present', async () => {
|
|
103
|
+
const routes: RegisteredRoute[] = []
|
|
104
|
+
const runtime = new Context()
|
|
105
|
+
ctx = runtime
|
|
106
|
+
await mountWebServer(runtime, routes)
|
|
107
|
+
|
|
108
|
+
apply(runtime, { estimatorCatalogRoute: true })
|
|
109
|
+
await settle()
|
|
110
|
+
|
|
111
|
+
expect(routes.map(route => route.path)).toEqual([LEGACY_ROUTE, CATALOG_ROUTE, SAVINGS_LEGACY_ROUTE, SAVINGS_ROUTE, MONITOR_LEGACY_ROUTE, MONITOR_ROUTE])
|
|
112
|
+
expect(routes.every(route => route.kind === 'exact')).toBe(true)
|
|
113
|
+
})
|
|
114
|
+
|
|
115
|
+
it('registers the route when webServer activates after the plugin applied', async () => {
|
|
116
|
+
const routes: RegisteredRoute[] = []
|
|
117
|
+
const runtime = new Context()
|
|
118
|
+
ctx = runtime
|
|
119
|
+
|
|
120
|
+
apply(runtime, { estimatorCatalogRoute: true })
|
|
121
|
+
await settle()
|
|
122
|
+
expect(routes).toHaveLength(0)
|
|
123
|
+
|
|
124
|
+
await mountWebServer(runtime, routes)
|
|
125
|
+
await settle()
|
|
126
|
+
|
|
127
|
+
expect(routes.map(route => route.path)).toEqual([LEGACY_ROUTE, CATALOG_ROUTE, SAVINGS_LEGACY_ROUTE, SAVINGS_ROUTE, MONITOR_LEGACY_ROUTE, MONITOR_ROUTE])
|
|
128
|
+
})
|
|
129
|
+
|
|
130
|
+
it('registers nothing twice when the estimator services arrive later', async () => {
|
|
131
|
+
const routes: RegisteredRoute[] = []
|
|
132
|
+
const runtime = new Context()
|
|
133
|
+
ctx = runtime
|
|
134
|
+
await mountWebServer(runtime, routes)
|
|
135
|
+
|
|
136
|
+
apply(runtime, { estimatorCatalogRoute: true })
|
|
137
|
+
await settle()
|
|
138
|
+
const afterRegistration = routes.length
|
|
139
|
+
|
|
140
|
+
await mountEstimatorServices(runtime)
|
|
141
|
+
await settle()
|
|
142
|
+
|
|
143
|
+
expect(routes).toHaveLength(afterRegistration)
|
|
144
|
+
})
|
|
145
|
+
|
|
146
|
+
it('keeps the rest of the plugin alive and warns when webServer never arrives', async () => {
|
|
147
|
+
const runtime = new Context()
|
|
148
|
+
ctx = runtime
|
|
149
|
+
// The diagnostic goes to `console`: the host's cordis logger surfaces no
|
|
150
|
+
// plugin output at all, so a lifecycle line published there is invisible.
|
|
151
|
+
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
|
|
152
|
+
|
|
153
|
+
try {
|
|
154
|
+
expect(() => apply(runtime, { estimatorCatalogRoute: true })).not.toThrow()
|
|
155
|
+
await settle()
|
|
156
|
+
|
|
157
|
+
const messages = warn.mock.calls.map(([message]) => String(message))
|
|
158
|
+
expect(messages.some(message => message.includes('estimator catalog route pending'))).toBe(true)
|
|
159
|
+
} finally {
|
|
160
|
+
warn.mockRestore()
|
|
161
|
+
}
|
|
162
|
+
})
|
|
163
|
+
|
|
164
|
+
it('answers 200 without the estimator services, which only enrich the payload', async () => {
|
|
165
|
+
const routes: RegisteredRoute[] = []
|
|
166
|
+
const runtime = new Context()
|
|
167
|
+
ctx = runtime
|
|
168
|
+
await mountWebServer(runtime, routes)
|
|
169
|
+
|
|
170
|
+
apply(runtime, { estimatorCatalogRoute: true })
|
|
171
|
+
await settle()
|
|
172
|
+
|
|
173
|
+
const route = routes.find(candidate => candidate.path === CATALOG_ROUTE)
|
|
174
|
+
expect(route).toBeDefined()
|
|
175
|
+
const response = await invoke(route as RegisteredRoute)
|
|
176
|
+
|
|
177
|
+
expect(response.status).toBe(200)
|
|
178
|
+
expect(JSON.parse(String(response.body))).toMatchObject({ ok: true, providers: [] })
|
|
179
|
+
})
|
|
180
|
+
|
|
181
|
+
it('without the estimator opt-in only the always-on read routes register (savings + monitor)', async () => {
|
|
182
|
+
// savings 与 monitor 路由是有意常开的只读面(纯 token 聚合与折叠状态,无会话内容);estimator
|
|
183
|
+
// catalog 才是 opt-in 行。默认面 = 恰好两条 savings 前缀 + 两条 monitor 前缀。
|
|
184
|
+
const routes: RegisteredRoute[] = []
|
|
185
|
+
const runtime = new Context()
|
|
186
|
+
ctx = runtime
|
|
187
|
+
await mountWebServer(runtime, routes)
|
|
188
|
+
|
|
189
|
+
apply(runtime, { presetOverlay: false })
|
|
190
|
+
await settle()
|
|
191
|
+
|
|
192
|
+
expect(routes.map(route => route.path)).toEqual([SAVINGS_LEGACY_ROUTE, SAVINGS_ROUTE, MONITOR_LEGACY_ROUTE, MONITOR_ROUTE])
|
|
193
|
+
})
|
|
194
|
+
})
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 压缩监控浮动球(FAB)客户端单测——骨架来自 pm `d29bcb1^` 撤下的
|
|
3
|
+
* longtask-fab(计划 cci-monitor-fab task_3.1-3.4),数据层为注入依赖,
|
|
4
|
+
* 本文件自断言消费侧行为。真实时钟 + initialDelayMs=1,不用假时钟
|
|
5
|
+
* (jsdom × fake-timers 的 async advance 有已知坑)。
|
|
6
|
+
*/
|
|
7
|
+
import { afterEach, describe, expect, it, vi } from 'vitest'
|
|
8
|
+
import { initMonitorFab, type MonitorFabDeps, type MonitorSnapshotPayload } from '../src/client/monitor-fab.ts'
|
|
9
|
+
|
|
10
|
+
const FAB_ID = 'dsh-cci-monitor-fab'
|
|
11
|
+
|
|
12
|
+
function snapshotPatch(patch: Partial<MonitorSnapshotPayload> = {}): MonitorSnapshotPayload {
|
|
13
|
+
return {
|
|
14
|
+
ok: true,
|
|
15
|
+
startedAt: new Date().toISOString(),
|
|
16
|
+
gross: { exact: 10_000, estimated: 0 },
|
|
17
|
+
offsets: { exact: 1_200, estimated: 0 },
|
|
18
|
+
net: { exact: 8_800, estimated: 0 },
|
|
19
|
+
usage: { requests: 5, cacheHitRate: 0.812 },
|
|
20
|
+
pricing: { currency: 'USD', actualCost: '0.12', estimatedSavedCost: '1.30' },
|
|
21
|
+
intent: { override: undefined, observedEnabled: false, gate: { floorFraction: 0.45, growthTokens: 50_000 }, lastFold: undefined },
|
|
22
|
+
recentOffsets: [{ component: 'fresh', tokens: 1_200 }],
|
|
23
|
+
sessionScope: null,
|
|
24
|
+
...patch,
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function makeDeps(patch: Partial<MonitorFabDeps> & { snapshots?: MonitorSnapshotPayload[] } = {}): MonitorFabDeps & {
|
|
29
|
+
snapshots: MonitorSnapshotPayload[]
|
|
30
|
+
overrides: Array<{ sessionId: string | undefined, action: 'on' | 'off' | 'clear' }>
|
|
31
|
+
} {
|
|
32
|
+
const deps: MonitorFabDeps & {
|
|
33
|
+
snapshots: MonitorSnapshotPayload[]
|
|
34
|
+
overrides: Array<{ sessionId: string | undefined, action: 'on' | 'off' | 'clear' }>
|
|
35
|
+
} = {
|
|
36
|
+
t: (key: string) => key,
|
|
37
|
+
sessionIdOf: () => undefined,
|
|
38
|
+
enabledOf: () => true,
|
|
39
|
+
fetchSnapshot: vi.fn(async () => deps.snapshots[deps.snapshots.length - 1]!),
|
|
40
|
+
applyOverride: vi.fn(async (_sessionId: string | undefined, action: 'on' | 'off' | 'clear') => {
|
|
41
|
+
deps.overrides.push({ sessionId: undefined, action })
|
|
42
|
+
}),
|
|
43
|
+
snapshots: [snapshotPatch()],
|
|
44
|
+
overrides: [],
|
|
45
|
+
initialDelayMs: 1,
|
|
46
|
+
}
|
|
47
|
+
return Object.assign(deps, patch)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** 真实时钟下等条件成立(轮询 1ms 一轮,上限 500ms)。 */
|
|
51
|
+
async function waitFor(predicate: () => boolean): Promise<void> {
|
|
52
|
+
const deadline = Date.now() + 500
|
|
53
|
+
while (!predicate()) {
|
|
54
|
+
if (Date.now() > deadline) throw new Error('waitFor timeout')
|
|
55
|
+
await new Promise(resolve => setTimeout(resolve, 5))
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
afterEach(() => {
|
|
60
|
+
document.body.innerHTML = ''
|
|
61
|
+
localStorage.clear()
|
|
62
|
+
vi.restoreAllMocks()
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
describe('monitor fab (body-level overlay)', () => {
|
|
66
|
+
it('mounts idempotently — double init keeps a single FAB', () => {
|
|
67
|
+
const deps = makeDeps()
|
|
68
|
+
initMonitorFab(deps)
|
|
69
|
+
initMonitorFab(deps)
|
|
70
|
+
expect(document.querySelectorAll(`#${FAB_ID}`).length).toBe(1)
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
it('enabled gate hides the ball and suppresses polling', async () => {
|
|
74
|
+
const fetchSnapshot = vi.fn(async () => snapshotPatch())
|
|
75
|
+
initMonitorFab(makeDeps({ enabledOf: () => false, fetchSnapshot, initialDelayMs: 5 }))
|
|
76
|
+
const fab = document.getElementById(FAB_ID) as HTMLElement
|
|
77
|
+
await waitFor(() => fab.style.display === 'none')
|
|
78
|
+
expect(fetchSnapshot).not.toHaveBeenCalled()
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
it('enabled: ball visible, first tick renders the snapshot', async () => {
|
|
82
|
+
initMonitorFab(makeDeps())
|
|
83
|
+
const fab = document.getElementById(FAB_ID) as HTMLElement
|
|
84
|
+
await waitFor(() => fab.style.display === 'block')
|
|
85
|
+
const net = document.querySelector('[data-net]') as HTMLElement
|
|
86
|
+
expect(net.textContent).toBe('8,800')
|
|
87
|
+
expect(fab.classList.contains('lit')).toBe(false)
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
it('negative net lights the ball (负节省警示)', async () => {
|
|
91
|
+
initMonitorFab(makeDeps({ snapshots: [snapshotPatch({ net: { exact: -800, estimated: 0 } })] }))
|
|
92
|
+
await waitFor(() => (document.getElementById(FAB_ID) as HTMLElement).classList.contains('lit'))
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
it('fetch failure flips the stale marker without throwing', async () => {
|
|
96
|
+
initMonitorFab(makeDeps({ fetchSnapshot: vi.fn(async () => { throw new Error('down') }) }))
|
|
97
|
+
await waitFor(() => document.querySelector('[data-stale]')?.classList.contains('show') === true)
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
it('override buttons drive applyOverride then refetch', async () => {
|
|
101
|
+
const deps = makeDeps()
|
|
102
|
+
initMonitorFab(deps)
|
|
103
|
+
await waitFor(() => (document.querySelector('[data-overoff]') as HTMLButtonElement)?.disabled === false)
|
|
104
|
+
;(document.querySelector('[data-overoff]') as HTMLButtonElement).click()
|
|
105
|
+
await waitFor(() => deps.overrides.length === 1)
|
|
106
|
+
expect(deps.overrides).toEqual([{ sessionId: undefined, action: 'off' }])
|
|
107
|
+
await waitFor(() => (deps.fetchSnapshot as ReturnType<typeof vi.fn>).mock.calls.length >= 2)
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
it('灾难性遗忘区建议:点亮悬浮球并展示提示行', async () => {
|
|
111
|
+
const deps = makeDeps({
|
|
112
|
+
snapshots: [snapshotPatch({
|
|
113
|
+
net: { exact: 0, estimated: 0 },
|
|
114
|
+
context: { liveTokens: 90_000, contextWindow: 100_000, pct: 0.9 },
|
|
115
|
+
suggestion: { suggest: true, thresholdPct: 0.7, occupancyPct: 0.9 },
|
|
116
|
+
})],
|
|
117
|
+
})
|
|
118
|
+
initMonitorFab(deps)
|
|
119
|
+
await waitFor(() => (document.getElementById(FAB_ID) as HTMLElement).classList.contains('lit'))
|
|
120
|
+
const hint = document.querySelector('[data-hint]') as HTMLElement
|
|
121
|
+
expect(hint.classList.contains('show')).toBe(true)
|
|
122
|
+
expect(hint.textContent).toBe('monitor.panel.suggest')
|
|
123
|
+
})
|
|
124
|
+
|
|
125
|
+
it('无建议时提示行隐藏', async () => {
|
|
126
|
+
initMonitorFab(makeDeps())
|
|
127
|
+
await waitFor(() => (document.querySelector('[data-net]') as HTMLElement).textContent !== '')
|
|
128
|
+
expect(document.querySelector('[data-hint]')?.classList.contains('show')).toBe(false)
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
it('position persists across drag-free re-init via localStorage', () => {
|
|
132
|
+
localStorage.setItem('dsh.cci.monitor.pos', JSON.stringify({ x: 40, y: 60 }))
|
|
133
|
+
initMonitorFab(makeDeps())
|
|
134
|
+
const fab = document.getElementById(FAB_ID) as HTMLElement
|
|
135
|
+
expect(fab.style.left).toBe('40px')
|
|
136
|
+
expect(fab.style.top).toBe('60px')
|
|
137
|
+
})
|
|
138
|
+
})
|