@capyclaw/rsp-suite 0.0.0-stage → 0.1.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/README.md +84 -2
- package/cordis.patch.yml +8 -0
- package/dsh-suite.json +81 -0
- package/package.json +39 -3
- package/skill/README.md +7 -0
- package/skill/contract/rsp-contract.json +389 -0
- package/skill/cordis.patch.yml +4 -0
- package/skill/package.json +33 -0
- package/skill/plugin.mjs +15 -0
- package/skill/skills/remote-screen/SKILL.md +120 -0
- package/workbench/contract/capabilities.json +171 -0
- package/workbench/lib/client.mjs +454 -0
- package/workbench/lib/descriptors.mjs +28 -0
- package/workbench/lib/host.mjs +111 -0
- package/workbench/lib/manager.mjs +333 -0
- package/workbench/lib/session-mode.mjs +68 -0
- package/workbench/lib/tools.mjs +385 -0
- package/workbench/lib/workbench.mjs +165 -0
- package/workbench/package.json +47 -0
- package/workbench/plugin.mjs +2 -0
- package/workbench/scripts/build-contract.mjs +41 -0
- package/workbench/scripts/drive.mjs +66 -0
- package/workbench/test/client.test.mjs +218 -0
- package/workbench/test/frames-prune.test.mjs +49 -0
- package/workbench/test/h264.test.mjs +101 -0
- package/workbench/test/live-persistent-smoke.mjs +29 -0
- package/workbench/test/live-smoke.mjs +109 -0
- package/workbench/test/mock-server.mjs +194 -0
- package/workbench/test/persistence.test.mjs +177 -0
- package/workbench/test/persistent.test.mjs +160 -0
- package/workbench/test/session-mode.test.mjs +118 -0
- package/workbench/test/workbench.test.mjs +385 -0
- package/workbench/ui/index.html +475 -0
- package/workbench/verify-e2e.mjs +44 -0
|
@@ -0,0 +1,385 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* rsp-workbench 集成测试:工具面(rsp_*)+ HTTP/MCP 能力面 + 查看器代理 + 描述符发布。
|
|
3
|
+
* 对 mock 被控端跑完整链路:connect → screen → 坐标换算操控 → batch → 断连。
|
|
4
|
+
*/
|
|
5
|
+
import test from 'node:test'
|
|
6
|
+
import assert from 'node:assert/strict'
|
|
7
|
+
import vm from 'node:vm'
|
|
8
|
+
import { mkdtempSync, existsSync, readFileSync } from 'node:fs'
|
|
9
|
+
import { tmpdir } from 'node:os'
|
|
10
|
+
import path from 'node:path'
|
|
11
|
+
import { fileURLToPath } from 'node:url'
|
|
12
|
+
import { createToolHost } from '@capyclaw/tool-host/src'
|
|
13
|
+
import { createServiceHost } from '@capyclaw/service-host/src'
|
|
14
|
+
import { validateCapabilityDescriptor } from '@capyclaw/suite-kit/capability'
|
|
15
|
+
import { createRspWorkbench } from '../lib/workbench.mjs'
|
|
16
|
+
import { RspManager } from '../lib/manager.mjs'
|
|
17
|
+
import { jpegSize } from '../lib/client.mjs'
|
|
18
|
+
import { createMockRspServer } from './mock-server.mjs'
|
|
19
|
+
|
|
20
|
+
async function waitFor(fn, timeout = 4000, step = 20) {
|
|
21
|
+
const end = Date.now() + timeout
|
|
22
|
+
while (Date.now() < end) {
|
|
23
|
+
const v = await fn()
|
|
24
|
+
if (v) return v
|
|
25
|
+
await new Promise((r) => setTimeout(r, step))
|
|
26
|
+
}
|
|
27
|
+
throw new Error('waitFor 超时')
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async function setup(t, opts = {}) {
|
|
31
|
+
const mock = await createMockRspServer(opts)
|
|
32
|
+
const hostDir = mkdtempSync(path.join(tmpdir(), 'rsp-wb-test-'))
|
|
33
|
+
const toolHost = createToolHost({})
|
|
34
|
+
const serviceHost = createServiceHost({})
|
|
35
|
+
const manager = new RspManager({ hostDir })
|
|
36
|
+
const wb = createRspWorkbench({ toolHost, serviceHost, manager, config: { hostDir } })
|
|
37
|
+
const l = await wb.app.listen({ port: 0, hostDir })
|
|
38
|
+
wb.publishDescriptors(l.url, { validate: validateCapabilityDescriptor })
|
|
39
|
+
t.after(async () => { await wb.close(); await mock.close() })
|
|
40
|
+
const call = (name, args = {}) => wb.registry.TOOL_IMPL[name](args)
|
|
41
|
+
const textOf = (r) => r.content.find((b) => b.type === 'text')?.text ?? ''
|
|
42
|
+
const jfetch = async (route, { method = 'GET', body } = {}) => {
|
|
43
|
+
const res = await fetch(l.url + route, {
|
|
44
|
+
method,
|
|
45
|
+
headers: body !== undefined ? { 'content-type': 'application/json' } : {},
|
|
46
|
+
body: body !== undefined ? JSON.stringify(body) : undefined,
|
|
47
|
+
})
|
|
48
|
+
return { status: res.status, body: await res.json().catch(() => null), headers: res.headers }
|
|
49
|
+
}
|
|
50
|
+
const connect = async () => {
|
|
51
|
+
const r = await call('rsp_connect', { url: mock.url, token: mock.token, name: 'mock' })
|
|
52
|
+
assert.equal(r.isError, false, textOf(r))
|
|
53
|
+
await waitFor(() => manager.connected && manager.client.lastFrame)
|
|
54
|
+
}
|
|
55
|
+
return { mock, wb, manager, toolHost, l, call, textOf, jfetch, connect, hostDir }
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
test('HTTP 基面:config/status/surface/静态查看器', async (t) => {
|
|
59
|
+
const { jfetch, l } = await setup(t)
|
|
60
|
+
const cfg = await jfetch('/api/config')
|
|
61
|
+
assert.equal(cfg.status, 200)
|
|
62
|
+
assert.equal(cfg.body.mountPath, '/capyclaw/rsp')
|
|
63
|
+
assert.equal(cfg.body.protocol, 'rsp/1')
|
|
64
|
+
const st = await jfetch('/api/status')
|
|
65
|
+
assert.equal(st.body.ok, true)
|
|
66
|
+
assert.equal(st.body.configured, false)
|
|
67
|
+
const surf = await jfetch('/api/surface')
|
|
68
|
+
assert.equal(surf.body.suite, 'rsp')
|
|
69
|
+
assert.equal(surf.body.embeddable, true)
|
|
70
|
+
assert.equal(surf.body.embedPath, '/capyclaw/rsp')
|
|
71
|
+
const page = await fetch(l.url + '/')
|
|
72
|
+
assert.equal(page.status, 200)
|
|
73
|
+
assert.match(await page.text(), /RSP/)
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
test('rsp_connect:校验协议、持久化目标、错误令牌 401', async (t) => {
|
|
77
|
+
const { mock, manager, call, textOf, jfetch, hostDir } = await setup(t)
|
|
78
|
+
const bad = await jfetch('/api/connect', { method: 'POST', body: { url: mock.url, token: 'wrong' } })
|
|
79
|
+
assert.equal(bad.status, 401)
|
|
80
|
+
assert.equal(bad.body.code, 'unauthorized')
|
|
81
|
+
const r = await call('rsp_connect', { url: mock.url, token: mock.token, name: 'mock' })
|
|
82
|
+
assert.equal(r.isError, false)
|
|
83
|
+
const out = JSON.parse(textOf(r))
|
|
84
|
+
assert.equal(out.info.protocol, 'rsp/1')
|
|
85
|
+
assert.equal(out.info.screen.width, 1920)
|
|
86
|
+
await waitFor(() => manager.connected)
|
|
87
|
+
assert.ok(existsSync(path.join(hostDir, 'targets.json')))
|
|
88
|
+
// 新 manager 从持久化恢复并无参重连
|
|
89
|
+
const m2 = new RspManager({ hostDir })
|
|
90
|
+
assert.equal(m2.status().configured, true)
|
|
91
|
+
assert.equal(m2.status().target.name, 'mock')
|
|
92
|
+
await m2.connect()
|
|
93
|
+
await waitFor(() => m2.connected)
|
|
94
|
+
await m2.close()
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
test('rsp_capabilities / GET /api/capabilities:契约 10 工具', async (t) => {
|
|
98
|
+
const { call, textOf, jfetch, connect } = await setup(t)
|
|
99
|
+
await connect()
|
|
100
|
+
const r = await call('rsp_capabilities')
|
|
101
|
+
const out = JSON.parse(textOf(r))
|
|
102
|
+
assert.equal(out.protocol, 'rsp/1')
|
|
103
|
+
assert.equal(out.contract.toolCount, 10)
|
|
104
|
+
assert.equal(out.status.configured, true)
|
|
105
|
+
const http = await jfetch('/api/capabilities')
|
|
106
|
+
assert.equal(http.body.contract.toolCount, 10)
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
test('rsp_screen:内联 JPEG + 落盘 + 帧/逻辑尺寸与换算比例', async (t) => {
|
|
110
|
+
const { call, connect } = await setup(t)
|
|
111
|
+
await connect()
|
|
112
|
+
const r = await call('rsp_screen')
|
|
113
|
+
assert.equal(r.isError, false)
|
|
114
|
+
const img = r.content.find((b) => b.type === 'image')
|
|
115
|
+
assert.ok(img, '应内联 image 块')
|
|
116
|
+
assert.equal(img.mimeType, 'image/jpeg')
|
|
117
|
+
const bytes = Buffer.from(img.data, 'base64')
|
|
118
|
+
assert.deepEqual(jpegSize(bytes), { width: 960, height: 540 })
|
|
119
|
+
const meta = JSON.parse(r.content.find((b) => b.type === 'text').text)
|
|
120
|
+
assert.deepEqual(meta.frame.width, 960)
|
|
121
|
+
assert.deepEqual(meta.logical, { width: 1920, height: 1080 })
|
|
122
|
+
assert.deepEqual(meta.frameToLogicalScale, { x: 2, y: 2 })
|
|
123
|
+
assert.ok(meta.path && existsSync(meta.path))
|
|
124
|
+
})
|
|
125
|
+
|
|
126
|
+
test('rsp_mouse:frame 像素→逻辑点换算、logical 直传、钳制', async (t) => {
|
|
127
|
+
const { mock, call, textOf, connect } = await setup(t)
|
|
128
|
+
await connect()
|
|
129
|
+
const r = await call('rsp_mouse', { action: 'click', x: 480, y: 270 })
|
|
130
|
+
assert.equal(r.isError, false, textOf(r))
|
|
131
|
+
const out = JSON.parse(textOf(r))
|
|
132
|
+
assert.equal(out.injected.x, 960)
|
|
133
|
+
assert.equal(out.injected.y, 540)
|
|
134
|
+
assert.equal(out.via, 'ws')
|
|
135
|
+
const ev = mock.events.filter((e) => e.cmd === 'mouse').pop()
|
|
136
|
+
assert.equal(ev.x, 960); assert.equal(ev.y, 540)
|
|
137
|
+
// logical 直传
|
|
138
|
+
await call('rsp_mouse', { action: 'move', x: 100, y: 200, space: 'logical' })
|
|
139
|
+
const ev2 = mock.events.filter((e) => e.cmd === 'mouse').pop()
|
|
140
|
+
assert.equal(ev2.x, 100); assert.equal(ev2.y, 200)
|
|
141
|
+
// 越界钳制到逻辑边界
|
|
142
|
+
await call('rsp_mouse', { action: 'click', x: 99999, y: -5 })
|
|
143
|
+
const ev3 = mock.events.filter((e) => e.cmd === 'mouse').pop()
|
|
144
|
+
assert.equal(ev3.x, 1920); assert.equal(ev3.y, 0)
|
|
145
|
+
// scroll 不需要坐标
|
|
146
|
+
const s = await call('rsp_mouse', { action: 'scroll', dy: 120 })
|
|
147
|
+
assert.equal(s.isError, false)
|
|
148
|
+
const ev4 = mock.events.filter((e) => e.cmd === 'mouse').pop()
|
|
149
|
+
assert.equal(ev4.action, 'scroll'); assert.equal(ev4.dy, 120)
|
|
150
|
+
})
|
|
151
|
+
|
|
152
|
+
test('rsp_key:物理键位 + modifiers 成对注入', async (t) => {
|
|
153
|
+
const { mock, call, connect } = await setup(t)
|
|
154
|
+
await connect()
|
|
155
|
+
const down = await call('rsp_key', { action: 'down', code: 'KeyC', modifiers: ['cmd'] })
|
|
156
|
+
assert.equal(down.isError, false)
|
|
157
|
+
await call('rsp_key', { action: 'up', code: 'KeyC' })
|
|
158
|
+
const keys = mock.events.filter((e) => e.cmd === 'key')
|
|
159
|
+
assert.equal(keys.length, 2)
|
|
160
|
+
assert.deepEqual(keys[0].modifiers, ['cmd'])
|
|
161
|
+
assert.equal(keys[1].action, 'up')
|
|
162
|
+
const bad = await call('rsp_key', { action: 'down', code: 'KeyC', modifiers: ['win'] })
|
|
163
|
+
assert.equal(bad.isError, true)
|
|
164
|
+
})
|
|
165
|
+
|
|
166
|
+
test('rsp_text:长文本自动分片(9000 → 4096+4096+808)', async (t) => {
|
|
167
|
+
const { mock, call, textOf, connect } = await setup(t)
|
|
168
|
+
await connect()
|
|
169
|
+
const r = await call('rsp_text', { text: 'x'.repeat(9000) })
|
|
170
|
+
assert.equal(r.isError, false)
|
|
171
|
+
const out = JSON.parse(textOf(r))
|
|
172
|
+
assert.equal(out.chars, 9000)
|
|
173
|
+
assert.equal(out.chunks, 3)
|
|
174
|
+
const texts = mock.events.filter((e) => e.cmd === 'text')
|
|
175
|
+
assert.deepEqual(texts.map((e) => e.text.length), [4096, 4096, 808])
|
|
176
|
+
})
|
|
177
|
+
|
|
178
|
+
test('rsp_drag:down→插值 move→up(拖拽语义),坐标换算', async (t) => {
|
|
179
|
+
const { mock, call, textOf, connect } = await setup(t)
|
|
180
|
+
await connect()
|
|
181
|
+
const r = await call('rsp_drag', { fromX: 100, fromY: 50, toX: 300, toY: 150, steps: 4 })
|
|
182
|
+
assert.equal(r.isError, false, textOf(r))
|
|
183
|
+
const seq = mock.events.filter((e) => e.cmd === 'mouse')
|
|
184
|
+
assert.equal(seq.length, 6) // down + 4 move + up
|
|
185
|
+
assert.equal(seq[0].action, 'down')
|
|
186
|
+
assert.equal(seq[0].x, 200); assert.equal(seq[0].y, 100)
|
|
187
|
+
assert.equal(seq[5].action, 'up')
|
|
188
|
+
assert.equal(seq[5].x, 600); assert.equal(seq[5].y, 300)
|
|
189
|
+
for (let i = 1; i <= 4; i++) {
|
|
190
|
+
assert.equal(seq[i].action, 'move')
|
|
191
|
+
assert.equal(seq[i].x, 200 + 100 * i)
|
|
192
|
+
assert.equal(seq[i].y, 100 + 50 * i)
|
|
193
|
+
}
|
|
194
|
+
})
|
|
195
|
+
|
|
196
|
+
test('rsp_batch:多事件顺序注入 + 末尾截图', async (t) => {
|
|
197
|
+
const { mock, call, textOf, connect } = await setup(t)
|
|
198
|
+
await connect()
|
|
199
|
+
const r = await call('rsp_batch', {
|
|
200
|
+
events: [
|
|
201
|
+
{ action: 'click', x: 10, y: 10 },
|
|
202
|
+
{ cmd: 'key', action: 'down', code: 'KeyA' },
|
|
203
|
+
{ cmd: 'key', action: 'up', code: 'KeyA' },
|
|
204
|
+
{ cmd: 'text', text: '你好' },
|
|
205
|
+
],
|
|
206
|
+
screenshot: true,
|
|
207
|
+
})
|
|
208
|
+
assert.equal(r.isError, false, textOf(r))
|
|
209
|
+
const head = JSON.parse(r.content[0].text)
|
|
210
|
+
assert.equal(head.count, 4)
|
|
211
|
+
assert.deepEqual(head.injected, ['mouse:click', 'key:down', 'key:up', 'text'])
|
|
212
|
+
assert.ok(r.content.some((b) => b.type === 'image'), 'screenshot=true 应回图像块')
|
|
213
|
+
assert.equal(mock.events.filter((e) => e.cmd === 'text').pop().text, '你好')
|
|
214
|
+
const empty = await call('rsp_batch', { events: [] })
|
|
215
|
+
assert.equal(empty.isError, true)
|
|
216
|
+
})
|
|
217
|
+
|
|
218
|
+
test('未连接错误与断开:not_connected / no_target / disconnect', async (t) => {
|
|
219
|
+
const { call, textOf, connect, manager } = await setup(t)
|
|
220
|
+
const noTarget = await call('rsp_connect', {})
|
|
221
|
+
assert.equal(noTarget.isError, true)
|
|
222
|
+
assert.match(textOf(noTarget), /^\[no_target\]/)
|
|
223
|
+
const mouse = await call('rsp_mouse', { action: 'click', x: 1, y: 1 })
|
|
224
|
+
assert.equal(mouse.isError, true)
|
|
225
|
+
assert.match(textOf(mouse), /^\[not_connected\]/)
|
|
226
|
+
await connect()
|
|
227
|
+
const ok = await call('rsp_mouse', { action: 'click', x: 1, y: 1 })
|
|
228
|
+
assert.equal(ok.isError, false)
|
|
229
|
+
const disc = await call('rsp_disconnect', {})
|
|
230
|
+
assert.equal(disc.isError, false)
|
|
231
|
+
assert.equal(manager.connected, false)
|
|
232
|
+
const after = await call('rsp_screen')
|
|
233
|
+
assert.equal(after.isError, true)
|
|
234
|
+
})
|
|
235
|
+
|
|
236
|
+
test('GET /api/frame 与 /api/screen.mjpg 代理(令牌不下发)', async (t) => {
|
|
237
|
+
const { l, connect, manager } = await setup(t)
|
|
238
|
+
const early = await fetch(l.url + '/api/frame')
|
|
239
|
+
assert.equal(early.status, 503)
|
|
240
|
+
await connect()
|
|
241
|
+
const frame = await fetch(l.url + '/api/frame')
|
|
242
|
+
assert.equal(frame.status, 200)
|
|
243
|
+
assert.equal(frame.headers.get('content-type'), 'image/jpeg')
|
|
244
|
+
const bytes = Buffer.from(await frame.arrayBuffer())
|
|
245
|
+
assert.deepEqual(jpegSize(bytes), { width: 960, height: 540 })
|
|
246
|
+
assert.deepEqual(bytes, manager.client.lastFrame.bytes)
|
|
247
|
+
// MJPEG 代理:multipart 边界 + JPEG SOI;URL 不含 token
|
|
248
|
+
const ac = new AbortController()
|
|
249
|
+
const res = await fetch(l.url + '/api/screen.mjpg', { signal: ac.signal })
|
|
250
|
+
assert.equal(res.status, 200)
|
|
251
|
+
assert.match(res.headers.get('content-type'), /multipart\/x-mixed-replace/)
|
|
252
|
+
const reader = res.body.getReader()
|
|
253
|
+
let buf = Buffer.alloc(0)
|
|
254
|
+
while (buf.length < 300) {
|
|
255
|
+
const { value, done } = await reader.read()
|
|
256
|
+
if (done) break
|
|
257
|
+
buf = Buffer.concat([buf, Buffer.from(value)])
|
|
258
|
+
}
|
|
259
|
+
ac.abort()
|
|
260
|
+
assert.ok(buf.includes(Buffer.from('--rspframe')), '应含 MJPEG 边界')
|
|
261
|
+
assert.ok(buf.includes(Buffer.from([0xff, 0xd8])), '应含 JPEG SOI')
|
|
262
|
+
})
|
|
263
|
+
|
|
264
|
+
test('MCP 面:initialize / tools/list / tools/call(保留图像块)', async (t) => {
|
|
265
|
+
const { l, connect } = await setup(t)
|
|
266
|
+
await connect()
|
|
267
|
+
const mcp = async (method, params, id = 1) => {
|
|
268
|
+
const res = await fetch(l.url + '/mcp', {
|
|
269
|
+
method: 'POST', headers: { 'content-type': 'application/json' },
|
|
270
|
+
body: JSON.stringify({ jsonrpc: '2.0', id, method, params }),
|
|
271
|
+
})
|
|
272
|
+
return res.json()
|
|
273
|
+
}
|
|
274
|
+
const init = await mcp('initialize', { protocolVersion: '2024-11-05' })
|
|
275
|
+
assert.equal(init.result.serverInfo.name, 'rsp-workbench')
|
|
276
|
+
const list = await mcp('tools/list', {})
|
|
277
|
+
assert.equal(list.result.tools.length, 10)
|
|
278
|
+
assert.ok(list.result.tools.some((x) => x.name === 'rsp_screen'))
|
|
279
|
+
const st = await mcp('tools/call', { name: 'rsp_status', arguments: {} })
|
|
280
|
+
assert.equal(st.result.isError, false)
|
|
281
|
+
assert.equal(JSON.parse(st.result.content[0].text).configured, true)
|
|
282
|
+
const shot = await mcp('tools/call', { name: 'rsp_screen', arguments: {} })
|
|
283
|
+
assert.ok(shot.result.content.some((b) => b.type === 'image'), 'MCP 应保留图像块')
|
|
284
|
+
const badArgs = await mcp('tools/call', { name: 'rsp_mouse', arguments: { action: 'nope' } })
|
|
285
|
+
assert.equal(badArgs.result.isError, true)
|
|
286
|
+
})
|
|
287
|
+
|
|
288
|
+
test('能力描述符发布:10 个 rsp.* 通过校验', async (t) => {
|
|
289
|
+
const { toolHost } = await setup(t)
|
|
290
|
+
const catalog = toolHost.capabilityCatalog().map((e) => e.descriptor)
|
|
291
|
+
const rsp = catalog.filter((d) => d.name.startsWith('rsp.'))
|
|
292
|
+
assert.equal(rsp.length, 10)
|
|
293
|
+
assert.ok(rsp.every((d) => d.controlMode === 'interface'))
|
|
294
|
+
assert.ok(rsp.some((d) => d.name === 'rsp.screen.capture' && d.produces.includes('artifact:image/jpeg')))
|
|
295
|
+
const surfaces = toolHost.listSurfaces()
|
|
296
|
+
assert.ok(surfaces.some((s) => s.path === '/capyclaw/rsp/' && s.suite === 'rsp'))
|
|
297
|
+
})
|
|
298
|
+
|
|
299
|
+
test('会话隔离:/s/<sid>/ 段(req.sessionId)各自看各自的桶,互不串台', async (t) => {
|
|
300
|
+
const { mock, manager, jfetch, l } = await setup(t)
|
|
301
|
+
// 会话 A 经嵌入地址连接(service-host 剥 /s/<sid> 段后注入 req.sessionId)
|
|
302
|
+
const ca = await jfetch('/s/sess-aaa/api/connect', { method: 'POST', body: { url: mock.url, token: mock.token, name: 'mock-a' } })
|
|
303
|
+
assert.equal(ca.status, 200, JSON.stringify(ca.body))
|
|
304
|
+
await waitFor(() => manager.clients.get('sess-aaa')?.lastFrame)
|
|
305
|
+
// 会话 B 尚未连接:它的查看器必须看到「未连接」,而不是 A 的画面
|
|
306
|
+
const sb = await jfetch('/s/sess-bbb/api/status')
|
|
307
|
+
assert.equal(sb.body.connected, false)
|
|
308
|
+
assert.equal(sb.body.session, 'sess-bbb')
|
|
309
|
+
const fb = await fetch(l.url + '/s/sess-bbb/api/frame')
|
|
310
|
+
assert.equal(fb.status, 503)
|
|
311
|
+
// 会话 A 的查看器看到自己的连接
|
|
312
|
+
const sa = await jfetch('/s/sess-aaa/api/status')
|
|
313
|
+
assert.equal(sa.body.connected, true)
|
|
314
|
+
assert.equal(sa.body.session, 'sess-aaa')
|
|
315
|
+
const fa = await fetch(l.url + '/s/sess-aaa/api/frame')
|
|
316
|
+
assert.equal(fa.status, 200)
|
|
317
|
+
assert.equal(fa.headers.get('content-type'), 'image/jpeg')
|
|
318
|
+
// B 连接同一 mock 不影响 A;A 断开也不影响 B
|
|
319
|
+
const cb = await jfetch('/s/sess-bbb/api/connect', { method: 'POST', body: { url: mock.url, token: mock.token, name: 'mock-b' } })
|
|
320
|
+
assert.equal(cb.status, 200)
|
|
321
|
+
await waitFor(() => manager.clients.get('sess-bbb')?.lastFrame)
|
|
322
|
+
assert.equal((await jfetch('/s/sess-aaa/api/status')).body.connected, true)
|
|
323
|
+
await jfetch('/s/sess-aaa/api/disconnect', { method: 'POST', body: {} })
|
|
324
|
+
assert.equal((await jfetch('/s/sess-aaa/api/status')).body.connected, false)
|
|
325
|
+
assert.equal((await jfetch('/s/sess-bbb/api/status')).body.connected, true)
|
|
326
|
+
// 显式指定优先于路径段:body.session / ?session= / x-rsp-session 都能覆盖
|
|
327
|
+
const override = await jfetch('/s/sess-aaa/api/status?session=sess-bbb')
|
|
328
|
+
assert.equal(override.body.connected, true)
|
|
329
|
+
const hdr = await fetch(l.url + '/s/sess-aaa/api/status', { headers: { 'x-rsp-session': 'sess-bbb' } })
|
|
330
|
+
assert.equal((await hdr.json()).connected, true)
|
|
331
|
+
const bodyOverride = await jfetch('/s/sess-aaa/api/disconnect', { method: 'POST', body: { session: 'sess-bbb' } })
|
|
332
|
+
assert.equal(bodyOverride.status, 200)
|
|
333
|
+
assert.equal((await jfetch('/s/sess-bbb/api/status')).body.connected, false)
|
|
334
|
+
})
|
|
335
|
+
|
|
336
|
+
test('查看器页面:从 URL 认领会话 + postMessage 就地换桶', async (t) => {
|
|
337
|
+
const { l } = await setup(t)
|
|
338
|
+
const page = await fetch(l.url + '/s/sess-xyz/')
|
|
339
|
+
assert.equal(page.status, 200)
|
|
340
|
+
const html = await page.text()
|
|
341
|
+
// 页面 JS 必须从自身地址认领 /s/<sid>/ 段(与 service-host 规则一致),并响应主区换会话消息
|
|
342
|
+
assert.ok(html.includes(String.raw`/\/s\/([A-Za-z0-9][A-Za-z0-9._~-]{0,127})(?=\/|$)/`), '应含会话段正则')
|
|
343
|
+
assert.match(html, /sidFromUrl/)
|
|
344
|
+
assert.match(html, /capyclaw:workbench:session/)
|
|
345
|
+
assert.match(html, /capyclaw:workbench:session-ack/)
|
|
346
|
+
})
|
|
347
|
+
|
|
348
|
+
/* 字符串断言抓不住「脚本一加载就抛错」这类 bug(曾出现 watchSid 初始化先于 SESSION_RE
|
|
349
|
+
声明的 TDZ ReferenceError,整页脚本瘫痪)。这里真的把内联脚本跑起来验证初始化。 */
|
|
350
|
+
function runViewerInit({ pathname, search = '' }) {
|
|
351
|
+
const uiDir = path.join(path.dirname(fileURLToPath(import.meta.url)), '..', 'ui')
|
|
352
|
+
const html = readFileSync(path.join(uiDir, 'index.html'), 'utf8')
|
|
353
|
+
const js = html.match(/<script>([\s\S]*?)<\/script>/)[1]
|
|
354
|
+
const el = () => ({
|
|
355
|
+
style: {}, className: '', textContent: '', innerHTML: '', value: '', open: false,
|
|
356
|
+
options: [], src: '', onload: null, onerror: null,
|
|
357
|
+
setAttribute() {}, removeAttribute() {}, getContext() { return null }, addEventListener() {},
|
|
358
|
+
})
|
|
359
|
+
const elements = {}
|
|
360
|
+
const sandbox = {
|
|
361
|
+
console, URLSearchParams, URL, AbortController, Uint8Array, DataView, performance,
|
|
362
|
+
document: {
|
|
363
|
+
getElementById: (id) => elements[id] || (elements[id] = el()),
|
|
364
|
+
createElement: () => el(), fullscreenElement: null, exitFullscreen() {},
|
|
365
|
+
},
|
|
366
|
+
location: { pathname, search, origin: 'http://127.0.0.1' },
|
|
367
|
+
fetch: async () => ({ ok: true, json: async () => ({ sessions: [] }), blob: async () => ({}) }),
|
|
368
|
+
setInterval: () => 0, setTimeout: () => 0, clearTimeout() {}, clearInterval() {},
|
|
369
|
+
requestAnimationFrame: () => 0,
|
|
370
|
+
}
|
|
371
|
+
sandbox.window = sandbox
|
|
372
|
+
sandbox.window.addEventListener = () => {}
|
|
373
|
+
sandbox.URL.createObjectURL = () => 'blob:x'
|
|
374
|
+
sandbox.URL.revokeObjectURL = () => {}
|
|
375
|
+
vm.createContext(sandbox)
|
|
376
|
+
vm.runInContext(js + '\n;globalThis.__watchSid = watchSid;', sandbox, { filename: 'rsp-ui.js' })
|
|
377
|
+
return sandbox.__watchSid
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
test('查看器脚本可真实初始化:URL 认领 / 独立打开 / query 兜底(防 TDZ 回归)', () => {
|
|
381
|
+
assert.equal(runViewerInit({ pathname: '/capyclaw/rsp/s/sess-abc/' }), 'sess-abc')
|
|
382
|
+
assert.equal(runViewerInit({ pathname: '/capyclaw/rsp/' }), null)
|
|
383
|
+
assert.equal(runViewerInit({ pathname: '/capyclaw/rsp/', search: '?session=sess-q' }), 'sess-q')
|
|
384
|
+
assert.equal(runViewerInit({ pathname: '/capyclaw/rsp/s/sess-abc/', search: '?session=other' }), 'sess-abc')
|
|
385
|
+
})
|