@capyclaw/rsp-suite 0.1.0 → 0.1.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.
- package/dsh-suite.json +13 -3
- package/node_modules/@capyclaw/foundation/README.md +51 -0
- package/node_modules/@capyclaw/foundation/abi/assets.mjs +26 -0
- package/node_modules/@capyclaw/foundation/abi/bridge.mjs +88 -0
- package/node_modules/@capyclaw/foundation/abi/errors.mjs +63 -0
- package/node_modules/@capyclaw/foundation/abi/index.mjs +50 -0
- package/node_modules/@capyclaw/foundation/abi/kinds.mjs +271 -0
- package/node_modules/@capyclaw/foundation/abi/loader.mjs +352 -0
- package/node_modules/@capyclaw/foundation/abi/schema.mjs +98 -0
- package/node_modules/@capyclaw/foundation/abi/spec.mjs +258 -0
- package/node_modules/@capyclaw/foundation/abi/validate.mjs +24 -0
- package/node_modules/@capyclaw/foundation/abi/version.mjs +140 -0
- package/node_modules/@capyclaw/foundation/abi/visibility.mjs +90 -0
- package/node_modules/@capyclaw/foundation/cordis.patch.yml +5 -0
- package/node_modules/@capyclaw/foundation/kit/index.mjs +6 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/client-panel.mjs +98 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/host-event.mjs +35 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/http-router.mjs +69 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/lifecycle-hook.mjs +72 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/own-server.mjs +113 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/raw.mjs +69 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/runtime-driver.mjs +120 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/service-provider.mjs +77 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/skill-provider.mjs +56 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/tool-provider.mjs +50 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/ui-panel.mjs +64 -0
- package/node_modules/@capyclaw/foundation/lib/ctx-service.mjs +42 -0
- package/node_modules/@capyclaw/foundation/lib/host/caps.mjs +38 -0
- package/node_modules/@capyclaw/foundation/lib/host/contract.mjs +88 -0
- package/node_modules/@capyclaw/foundation/lib/host/dsh.mjs +54 -0
- package/node_modules/@capyclaw/foundation/lib/host/memory.mjs +187 -0
- package/node_modules/@capyclaw/foundation/lib/host.mjs +23 -0
- package/node_modules/@capyclaw/foundation/lib/kernel.mjs +195 -0
- package/node_modules/@capyclaw/foundation/lib/skill/scan.mjs +115 -0
- package/node_modules/@capyclaw/foundation/lib/subsystems/events.mjs +60 -0
- package/node_modules/@capyclaw/foundation/lib/subsystems/log.mjs +51 -0
- package/node_modules/@capyclaw/foundation/lib/subsystems/net.mjs +198 -0
- package/node_modules/@capyclaw/foundation/package.json +53 -0
- package/node_modules/@capyclaw/foundation/runtime/index.mjs +20 -0
- package/node_modules/@capyclaw/plugin-kit/INTEGRATION.md +100 -0
- package/node_modules/@capyclaw/plugin-kit/README.md +70 -0
- package/node_modules/@capyclaw/plugin-kit/bin/sync.mjs +103 -0
- package/node_modules/@capyclaw/plugin-kit/package.json +72 -0
- package/node_modules/@capyclaw/plugin-kit/src/cli.mjs +56 -0
- package/node_modules/@capyclaw/plugin-kit/src/config.mjs +142 -0
- package/node_modules/@capyclaw/plugin-kit/src/editor/heartbeat-worker.js +132 -0
- package/node_modules/@capyclaw/plugin-kit/src/editor/heartbeat.mjs +26 -0
- package/node_modules/@capyclaw/plugin-kit/src/editor/lease.mjs +60 -0
- package/node_modules/@capyclaw/plugin-kit/src/editor/shadow.mjs +219 -0
- package/node_modules/@capyclaw/plugin-kit/src/editor/transport.js +13 -0
- package/node_modules/@capyclaw/plugin-kit/src/index.mjs +13 -0
- package/node_modules/@capyclaw/plugin-kit/src/ledger.mjs +151 -0
- package/node_modules/@capyclaw/plugin-kit/src/pack.mjs +230 -0
- package/node_modules/@capyclaw/plugin-kit/src/runtime/context.mjs +128 -0
- package/node_modules/@capyclaw/plugin-kit/src/self-contained.mjs +307 -0
- package/node_modules/@capyclaw/plugin-kit/src/service/api.mjs +45 -0
- package/node_modules/@capyclaw/plugin-kit/src/service/http.mjs +113 -0
- package/node_modules/@capyclaw/plugin-kit/src/service/mcp-http.mjs +150 -0
- package/node_modules/@capyclaw/plugin-kit/src/service/server.mjs +131 -0
- package/node_modules/@capyclaw/plugin-kit/src/service/static.mjs +79 -0
- package/node_modules/@capyclaw/plugin-kit/src/skills/provider.mjs +82 -0
- package/node_modules/@capyclaw/plugin-kit/src/store/blob-store.mjs +109 -0
- package/node_modules/@capyclaw/plugin-kit/src/store/content-store.mjs +17 -0
- package/node_modules/@capyclaw/plugin-kit/src/store/journal-store.mjs +89 -0
- package/node_modules/@capyclaw/plugin-kit/src/tools/result.mjs +57 -0
- package/node_modules/@capyclaw/plugin-kit/src/tools/validate.mjs +150 -0
- package/node_modules/@capyclaw/plugin-kit/src/tuning.mjs +82 -0
- package/node_modules/@capyclaw/plugin-kit/src/util/fs-lock.mjs +132 -0
- package/node_modules/@capyclaw/plugin-kit/src/util/http.mjs +130 -0
- package/node_modules/@capyclaw/plugin-kit/src/workspace/manager.mjs +664 -0
- package/node_modules/@capyclaw/plugin-kit/src/zip.mjs +181 -0
- package/{skill → node_modules/@capyclaw/rsp-skill-remote-screen}/contract/rsp-contract.json +102 -13
- package/{skill → node_modules/@capyclaw/rsp-skill-remote-screen}/skills/remote-screen/SKILL.md +13 -2
- package/{workbench → node_modules/@capyclaw/rsp-workbench}/contract/capabilities.json +32 -0
- package/{workbench → node_modules/@capyclaw/rsp-workbench}/lib/client.mjs +238 -11
- package/node_modules/@capyclaw/rsp-workbench/lib/host.mjs +51 -0
- package/{workbench → node_modules/@capyclaw/rsp-workbench}/lib/tools.mjs +116 -16
- package/{workbench → node_modules/@capyclaw/rsp-workbench}/lib/workbench.mjs +21 -2
- package/node_modules/@capyclaw/rsp-workbench/plugin.mjs +17 -0
- package/{workbench → node_modules/@capyclaw/rsp-workbench}/ui/index.html +113 -10
- package/node_modules/@capyclaw/service-host/README.md +86 -0
- package/node_modules/@capyclaw/service-host/cordis.patch.yml +4 -0
- package/node_modules/@capyclaw/service-host/dsh-suite.json +29 -0
- package/node_modules/@capyclaw/service-host/lib/host.mjs +22 -0
- package/node_modules/@capyclaw/service-host/package.json +60 -0
- package/node_modules/@capyclaw/service-host/src/index.mjs +597 -0
- package/node_modules/@capyclaw/service-host/src/tuning.mjs +63 -0
- package/node_modules/@capyclaw/suite-kit/README.md +289 -0
- package/node_modules/@capyclaw/suite-kit/bin/dsh-domain.mjs +98 -0
- package/node_modules/@capyclaw/suite-kit/bin/dsh-suite.mjs +174 -0
- package/node_modules/@capyclaw/suite-kit/client/embed.js +35 -0
- package/node_modules/@capyclaw/suite-kit/domain.schema.json +129 -0
- package/node_modules/@capyclaw/suite-kit/package.json +58 -0
- package/node_modules/@capyclaw/suite-kit/src/activation-registry.mjs +184 -0
- package/node_modules/@capyclaw/suite-kit/src/artifacts.mjs +131 -0
- package/node_modules/@capyclaw/suite-kit/src/capability.mjs +181 -0
- package/node_modules/@capyclaw/suite-kit/src/compose.mjs +201 -0
- package/node_modules/@capyclaw/suite-kit/src/domain.mjs +138 -0
- package/node_modules/@capyclaw/suite-kit/src/embed.mjs +81 -0
- package/node_modules/@capyclaw/suite-kit/src/host.mjs +251 -0
- package/node_modules/@capyclaw/suite-kit/src/index.mjs +24 -0
- package/node_modules/@capyclaw/suite-kit/src/manifest.mjs +263 -0
- package/node_modules/@capyclaw/suite-kit/src/orchestration.mjs +100 -0
- package/node_modules/@capyclaw/suite-kit/src/profile.mjs +584 -0
- package/node_modules/@capyclaw/suite-kit/src/surface.mjs +28 -0
- package/node_modules/@capyclaw/suite-kit/suite.schema.json +128 -0
- package/node_modules/@capyclaw/tool-host/README.md +56 -0
- package/node_modules/@capyclaw/tool-host/cordis.patch.yml +4 -0
- package/node_modules/@capyclaw/tool-host/dsh-suite.json +30 -0
- package/node_modules/@capyclaw/tool-host/lib/host.mjs +46 -0
- package/node_modules/@capyclaw/tool-host/package.json +59 -0
- package/node_modules/@capyclaw/tool-host/src/admit-images.mjs +80 -0
- package/node_modules/@capyclaw/tool-host/src/caller-session.mjs +25 -0
- package/node_modules/@capyclaw/tool-host/src/descriptors.mjs +166 -0
- package/node_modules/@capyclaw/tool-host/src/index.mjs +461 -0
- package/node_modules/@capyclaw/tool-host/src/mode-gate.mjs +272 -0
- package/node_modules/ws/LICENSE +20 -0
- package/node_modules/ws/README.md +539 -0
- package/node_modules/ws/browser.js +8 -0
- package/node_modules/ws/index.js +22 -0
- package/node_modules/ws/lib/buffer-util.js +142 -0
- package/node_modules/ws/lib/constants.js +19 -0
- package/node_modules/ws/lib/event-target.js +292 -0
- package/node_modules/ws/lib/extension.js +203 -0
- package/node_modules/ws/lib/limiter.js +55 -0
- package/node_modules/ws/lib/permessage-deflate.js +530 -0
- package/node_modules/ws/lib/receiver.js +751 -0
- package/node_modules/ws/lib/sender.js +610 -0
- package/node_modules/ws/lib/stream.js +161 -0
- package/node_modules/ws/lib/subprotocol.js +62 -0
- package/node_modules/ws/lib/validation.js +152 -0
- package/node_modules/ws/lib/websocket-server.js +562 -0
- package/node_modules/ws/lib/websocket.js +1440 -0
- package/node_modules/ws/package.json +74 -0
- package/node_modules/ws/wrapper.mjs +21 -0
- package/package.json +22 -9
- package/workbench/lib/host.mjs +0 -111
- package/workbench/plugin.mjs +0 -2
- package/workbench/scripts/build-contract.mjs +0 -41
- package/workbench/scripts/drive.mjs +0 -66
- package/workbench/test/client.test.mjs +0 -218
- package/workbench/test/frames-prune.test.mjs +0 -49
- package/workbench/test/h264.test.mjs +0 -101
- package/workbench/test/live-persistent-smoke.mjs +0 -29
- package/workbench/test/live-smoke.mjs +0 -109
- package/workbench/test/mock-server.mjs +0 -194
- package/workbench/test/persistence.test.mjs +0 -177
- package/workbench/test/persistent.test.mjs +0 -160
- package/workbench/test/session-mode.test.mjs +0 -118
- package/workbench/test/workbench.test.mjs +0 -385
- package/workbench/verify-e2e.mjs +0 -44
- /package/{skill → node_modules/@capyclaw/rsp-skill-remote-screen}/README.md +0 -0
- /package/{skill → node_modules/@capyclaw/rsp-skill-remote-screen}/cordis.patch.yml +0 -0
- /package/{skill → node_modules/@capyclaw/rsp-skill-remote-screen}/package.json +0 -0
- /package/{skill → node_modules/@capyclaw/rsp-skill-remote-screen}/plugin.mjs +0 -0
- /package/{workbench → node_modules/@capyclaw/rsp-workbench}/lib/descriptors.mjs +0 -0
- /package/{workbench → node_modules/@capyclaw/rsp-workbench}/lib/manager.mjs +0 -0
- /package/{workbench → node_modules/@capyclaw/rsp-workbench}/lib/session-mode.mjs +0 -0
- /package/{workbench → node_modules/@capyclaw/rsp-workbench}/package.json +0 -0
|
@@ -1,160 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 进程级常驻连接(持久化连接态)专项测试。
|
|
3
|
-
* 语义:激活即连 vault.active;未知会话 status/clientFor/requireClient 回落常驻;
|
|
4
|
-
* 显式 disconnect 的会话不回落;瞬态错误退避重试、永久错误停止;connect 换目标时常驻跟随。
|
|
5
|
-
*/
|
|
6
|
-
import test from 'node:test'
|
|
7
|
-
import assert from 'node:assert/strict'
|
|
8
|
-
import { mkdtempSync, writeFileSync } from 'node:fs'
|
|
9
|
-
import { tmpdir } from 'node:os'
|
|
10
|
-
import path from 'node:path'
|
|
11
|
-
import { EventEmitter } from 'node:events'
|
|
12
|
-
import { RspManager, PERSISTENT_BUCKET, DEFAULT_BUCKET, VAULT_VERSION } from '../lib/manager.mjs'
|
|
13
|
-
import { createMockRspServer } from './mock-server.mjs'
|
|
14
|
-
|
|
15
|
-
async function waitFor(fn, timeout = 5000, step = 20) {
|
|
16
|
-
const end = Date.now() + timeout
|
|
17
|
-
while (Date.now() < end) {
|
|
18
|
-
const v = await fn()
|
|
19
|
-
if (v) return v
|
|
20
|
-
await new Promise((r) => setTimeout(r, step))
|
|
21
|
-
}
|
|
22
|
-
throw new Error('waitFor 超时')
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
function tmpHost() { return mkdtempSync(path.join(tmpdir(), 'rsp-persistent-test-')) }
|
|
26
|
-
|
|
27
|
-
/** 预置保险库:一个 active 目标(模拟「上次记住的连接」)。 */
|
|
28
|
-
function seedVault(hostDir, target) {
|
|
29
|
-
writeFileSync(path.join(hostDir, 'targets.json'), JSON.stringify({
|
|
30
|
-
version: VAULT_VERSION,
|
|
31
|
-
targets: { [target.name]: { ...target, savedAt: '2026-01-01T00:00:00.000Z', lastUsedAt: '2026-01-01T00:00:00.000Z' } },
|
|
32
|
-
sessions: {}, active: target.name, updatedAt: '2026-01-01T00:00:00.000Z',
|
|
33
|
-
}))
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/** 假 client:connect 按脚本失败/成功,用于离线重试与永久错误分支(不依赖真实端口)。 */
|
|
37
|
-
class FakeClient extends EventEmitter {
|
|
38
|
-
constructor(script) { super(); this.script = [...script]; this.closed = false; this.screenState = 'closed'; this._authFailed = false; this.connectCalls = 0 }
|
|
39
|
-
async connect() {
|
|
40
|
-
this.connectCalls++
|
|
41
|
-
const step = this.script.shift() || { ok: true }
|
|
42
|
-
if (step.ok) { this.closed = false; this.screenState = 'open'; return { protocol: 'rsp/1' } }
|
|
43
|
-
const e = new Error(step.message || 'fake connect failure'); e.code = step.code; throw e
|
|
44
|
-
}
|
|
45
|
-
async disconnect() { this.closed = true; this.screenState = 'closed' }
|
|
46
|
-
status() { return { screenState: this.screenState } }
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
test('激活即连:startPersistent 连上记住的 active;未知会话 status/clientFor 回落常驻', async (t) => {
|
|
50
|
-
const mock = await createMockRspServer()
|
|
51
|
-
t.after(async () => { await mock.close() })
|
|
52
|
-
const hostDir = tmpHost()
|
|
53
|
-
seedVault(hostDir, { name: 'mac', baseUrl: mock.url, token: mock.token })
|
|
54
|
-
const m = new RspManager({ hostDir })
|
|
55
|
-
t.after(async () => { await m.close() })
|
|
56
|
-
assert.equal(m.status().connected, false, '激活前应未连接')
|
|
57
|
-
m.startPersistent()
|
|
58
|
-
await waitFor(() => m.status().persistent.connected)
|
|
59
|
-
const st = m.status('brand-new-session')
|
|
60
|
-
assert.equal(st.connected, true, '新会话缺省即已连接')
|
|
61
|
-
assert.equal(st.viaPersistent, true)
|
|
62
|
-
assert.equal(st.target.baseUrl, mock.url)
|
|
63
|
-
assert.ok(m.clientFor('brand-new-session'), '查看器按未知 sid 也能取到画面源')
|
|
64
|
-
assert.equal(m.clientFor('brand-new-session'), m.clients.get(PERSISTENT_BUCKET))
|
|
65
|
-
assert.equal(m.connected, false, '兼容 getter 不含常驻回落(disconnect 语义)')
|
|
66
|
-
})
|
|
67
|
-
|
|
68
|
-
test('requireClient 复用常驻:不重复建通道;显式 disconnect 的会话不回落', async (t) => {
|
|
69
|
-
const mock = await createMockRspServer()
|
|
70
|
-
t.after(async () => { await mock.close() })
|
|
71
|
-
const hostDir = tmpHost()
|
|
72
|
-
seedVault(hostDir, { name: 'mac', baseUrl: mock.url, token: mock.token })
|
|
73
|
-
const m = new RspManager({ hostDir })
|
|
74
|
-
t.after(async () => { await m.close() })
|
|
75
|
-
m.startPersistent()
|
|
76
|
-
await waitFor(() => m.status().persistent.connected)
|
|
77
|
-
const c = await m.requireClient('sess-new')
|
|
78
|
-
assert.equal(c, m.clients.get(PERSISTENT_BUCKET), '同目标直接复用常驻连接')
|
|
79
|
-
assert.equal(m.clients.get('sess-new'), undefined, '不为复用会话另建 client')
|
|
80
|
-
await m.disconnect({}, 'sess-new')
|
|
81
|
-
await assert.rejects(() => m.requireClient('sess-new'), (e) => e.code === 'not_connected')
|
|
82
|
-
assert.equal(m.status('sess-new').connected, false, '显式断开后不冒领常驻连接')
|
|
83
|
-
assert.equal(m.status('sess-new').viaPersistent, false)
|
|
84
|
-
assert.equal(m.status('other-session').connected, true, '其它会话仍回落常驻')
|
|
85
|
-
})
|
|
86
|
-
|
|
87
|
-
test('会话显式 connect 不同目标 → 独立 client;常驻跟随 active 目标切换', async (t) => {
|
|
88
|
-
const a = await createMockRspServer()
|
|
89
|
-
const b = await createMockRspServer()
|
|
90
|
-
t.after(async () => { await a.close(); await b.close() })
|
|
91
|
-
const hostDir = tmpHost()
|
|
92
|
-
seedVault(hostDir, { name: 'a', baseUrl: a.url, token: a.token })
|
|
93
|
-
const m = new RspManager({ hostDir })
|
|
94
|
-
t.after(async () => { await m.close() })
|
|
95
|
-
m.startPersistent()
|
|
96
|
-
await waitFor(() => m.status().persistent.connected)
|
|
97
|
-
await m.connect({ url: b.url, token: b.token, name: 'b' }, 'sess-B')
|
|
98
|
-
assert.ok(m.clients.get('sess-B'), '不同目标单独建连')
|
|
99
|
-
assert.notEqual(m.clients.get('sess-B'), m.clients.get(PERSISTENT_BUCKET))
|
|
100
|
-
await waitFor(() => m.bindings.get(PERSISTENT_BUCKET)?.baseUrl === b.url)
|
|
101
|
-
assert.equal(m.status('sess-B').viaPersistent, false)
|
|
102
|
-
assert.equal(m.status('sess-B').target.baseUrl, b.url)
|
|
103
|
-
})
|
|
104
|
-
|
|
105
|
-
test('瞬态错误退避重试:离线启动 → 恢复后自动连上', async (t) => {
|
|
106
|
-
const hostDir = tmpHost()
|
|
107
|
-
seedVault(hostDir, { name: 'mac', baseUrl: 'http://127.0.0.1:9', token: 'tok' })
|
|
108
|
-
const fake = new FakeClient([{ ok: false, code: 'ECONNREFUSED' }, { ok: false, code: 'ECONNREFUSED' }, { ok: true }])
|
|
109
|
-
const m = new RspManager({ hostDir, clientFactory: () => fake })
|
|
110
|
-
t.after(async () => { await m.close() })
|
|
111
|
-
m.startPersistent()
|
|
112
|
-
await waitFor(() => m.status().persistent.connected, 8000)
|
|
113
|
-
assert.equal(fake.connectCalls, 3, '前两次瞬态失败后第三次成功')
|
|
114
|
-
assert.equal(m._persistentRetry, 0, '成功后重试计数清零')
|
|
115
|
-
assert.equal(m.status().persistent.wanted, true)
|
|
116
|
-
})
|
|
117
|
-
|
|
118
|
-
test('永久错误停止:unauthorized 不再重试;stopPersistent/disconnect 常驻桶可手动停', async (t) => {
|
|
119
|
-
const hostDir = tmpHost()
|
|
120
|
-
seedVault(hostDir, { name: 'mac', baseUrl: 'http://127.0.0.1:9', token: 'bad' })
|
|
121
|
-
const fake = new FakeClient([{ ok: false, code: 'unauthorized' }])
|
|
122
|
-
const m = new RspManager({ hostDir, clientFactory: () => fake })
|
|
123
|
-
t.after(async () => { await m.close() })
|
|
124
|
-
m.startPersistent()
|
|
125
|
-
await waitFor(() => m._persistentWanted === false)
|
|
126
|
-
assert.equal(fake.connectCalls, 1, '永久错误只试一次')
|
|
127
|
-
assert.equal(m._persistentTimer, null, '不留重试定时器')
|
|
128
|
-
assert.equal(m.status().persistent.connected, false)
|
|
129
|
-
// 手动停:恢复 wanted 后再经 disconnect(PERSISTENT_BUCKET) 停止
|
|
130
|
-
m._persistentWanted = true
|
|
131
|
-
await m.disconnect({}, PERSISTENT_BUCKET)
|
|
132
|
-
assert.equal(m._persistentWanted, false)
|
|
133
|
-
assert.equal(m.clients.get(PERSISTENT_BUCKET), undefined)
|
|
134
|
-
})
|
|
135
|
-
|
|
136
|
-
test('DSH_RSP_TARGET_VAULT=false:startPersistent 不生效(整机关闭持久化)', async (t) => {
|
|
137
|
-
const hostDir = tmpHost()
|
|
138
|
-
seedVault(hostDir, { name: 'mac', baseUrl: 'http://127.0.0.1:9', token: 'tok' })
|
|
139
|
-
const m = new RspManager({ hostDir })
|
|
140
|
-
t.after(async () => { await m.close() })
|
|
141
|
-
process.env.DSH_RSP_TARGET_VAULT = 'false'
|
|
142
|
-
try {
|
|
143
|
-
m.startPersistent()
|
|
144
|
-
assert.equal(m._persistentWanted, false)
|
|
145
|
-
assert.equal(m.status().persistent.connected, false)
|
|
146
|
-
} finally { delete process.env.DSH_RSP_TARGET_VAULT }
|
|
147
|
-
})
|
|
148
|
-
|
|
149
|
-
test('无记忆目标:requireClient 立即 not_connected(不空等 6s);connect 后常驻跟随', async (t) => {
|
|
150
|
-
const mock = await createMockRspServer()
|
|
151
|
-
t.after(async () => { await mock.close() })
|
|
152
|
-
const m = new RspManager({ hostDir: tmpHost() })
|
|
153
|
-
t.after(async () => { await m.close() })
|
|
154
|
-
const t0 = Date.now()
|
|
155
|
-
await assert.rejects(() => m.requireClient('sess-x'), (e) => e.code === 'not_connected')
|
|
156
|
-
assert.ok(Date.now() - t0 < 2000, '无目标应立即报错而非轮询等待')
|
|
157
|
-
await m.connect({ url: mock.url, token: mock.token, name: 'mac' }, DEFAULT_BUCKET)
|
|
158
|
-
await waitFor(() => m.status().persistent.connected)
|
|
159
|
-
assert.equal(m.bindings.get(PERSISTENT_BUCKET).baseUrl, mock.url)
|
|
160
|
-
})
|
|
@@ -1,118 +0,0 @@
|
|
|
1
|
-
import test from 'node:test'
|
|
2
|
-
import assert from 'node:assert/strict'
|
|
3
|
-
import {
|
|
4
|
-
sessionFromExec,
|
|
5
|
-
assertSessionMode,
|
|
6
|
-
bindToolsToSessionMode,
|
|
7
|
-
} from '../lib/session-mode.mjs'
|
|
8
|
-
|
|
9
|
-
const exec = (sid) => ({ agent: { session: { id: sid } } })
|
|
10
|
-
|
|
11
|
-
function fakeWorkStore(behaviour) {
|
|
12
|
-
const calls = []
|
|
13
|
-
return {
|
|
14
|
-
calls,
|
|
15
|
-
claimSessionMode(sid, kind, by) {
|
|
16
|
-
calls.push([sid, kind, by])
|
|
17
|
-
if (behaviour === 'throw') throw new Error('db down')
|
|
18
|
-
if (behaviour === 'locked') return { ok: false, kind, lockedTo: 'ppt' }
|
|
19
|
-
return { ok: true, kind, fresh: true }
|
|
20
|
-
},
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
test('sessionFromExec:只有 exec.agent.session.id 才算调用者会话', () => {
|
|
25
|
-
assert.equal(sessionFromExec(exec('s-1')), 's-1')
|
|
26
|
-
assert.equal(sessionFromExec({ agent: {} }), '')
|
|
27
|
-
assert.equal(sessionFromExec(undefined), '')
|
|
28
|
-
assert.equal(sessionFromExec({ agent: { session: { id: ' s-2 ' } } }), 's-2')
|
|
29
|
-
})
|
|
30
|
-
|
|
31
|
-
test('assertSessionMode:底座缺席 / 无会话 → skipped,不报错', () => {
|
|
32
|
-
assert.deepEqual(assertSessionMode('s1', 'rsp', null), { ok: true, skipped: true })
|
|
33
|
-
assert.deepEqual(assertSessionMode('', 'rsp', fakeWorkStore()), { ok: true, skipped: true })
|
|
34
|
-
})
|
|
35
|
-
|
|
36
|
-
test('assertSessionMode:认领成功 → 透传 work-store 结果', () => {
|
|
37
|
-
const ws = fakeWorkStore()
|
|
38
|
-
const r = assertSessionMode('s1', 'rsp', () => ws)
|
|
39
|
-
assert.equal(r.ok, true)
|
|
40
|
-
assert.deepEqual(ws.calls, [['s1', 'rsp', 'rsp-workbench']])
|
|
41
|
-
})
|
|
42
|
-
|
|
43
|
-
test('assertSessionMode:已固化别种 → 抛 SESSION_MODE_LOCKED 且带 lockedTo', () => {
|
|
44
|
-
let err
|
|
45
|
-
try { assertSessionMode('s1', 'rsp', () => fakeWorkStore('locked')) } catch (e) { err = e }
|
|
46
|
-
assert.ok(err, '应该抛错')
|
|
47
|
-
assert.equal(err.code, 'SESSION_MODE_LOCKED')
|
|
48
|
-
assert.equal(err.lockedTo, 'ppt')
|
|
49
|
-
assert.match(err.message, /新开一个会话/)
|
|
50
|
-
})
|
|
51
|
-
|
|
52
|
-
test('assertSessionMode:存储故障降级为 skipped(不阻断操控)', () => {
|
|
53
|
-
assert.deepEqual(assertSessionMode('s1', 'rsp', () => fakeWorkStore('throw')), { ok: true, skipped: true })
|
|
54
|
-
})
|
|
55
|
-
|
|
56
|
-
test('bindToolsToSessionMode:read 不包,非 read 执行前认领;args/返回值原样透传', async () => {
|
|
57
|
-
const ws = fakeWorkStore()
|
|
58
|
-
const seen = []
|
|
59
|
-
const registry = {
|
|
60
|
-
TOOL_META: {
|
|
61
|
-
rsp_status: { kind: 'read' },
|
|
62
|
-
rsp_mouse: { kind: 'write' },
|
|
63
|
-
},
|
|
64
|
-
TOOL_IMPL: {
|
|
65
|
-
rsp_status: (args, e) => { seen.push(['read', args, e]); return 'status-ok' },
|
|
66
|
-
rsp_mouse: (args, e) => { seen.push(['write', args, e]); return 'mouse-ok' },
|
|
67
|
-
},
|
|
68
|
-
}
|
|
69
|
-
const rawStatus = registry.TOOL_IMPL.rsp_status
|
|
70
|
-
bindToolsToSessionMode(registry, { getWorkStore: () => ws })
|
|
71
|
-
|
|
72
|
-
assert.equal(registry.TOOL_IMPL.rsp_status, rawStatus, 'read 工具不该被包')
|
|
73
|
-
|
|
74
|
-
const out = await registry.TOOL_IMPL.rsp_mouse({ action: 'click' }, exec('sess-9'))
|
|
75
|
-
assert.equal(out, 'mouse-ok')
|
|
76
|
-
assert.deepEqual(ws.calls, [['sess-9', 'rsp', 'rsp-workbench']])
|
|
77
|
-
assert.equal(seen.at(-1)[1].action, 'click')
|
|
78
|
-
|
|
79
|
-
/* 同一次调用里 exec 也要原样透传给实现体(未来工具要用执行上下文)。 */
|
|
80
|
-
assert.equal(seen.at(-1)[2].agent.session.id, 'sess-9')
|
|
81
|
-
})
|
|
82
|
-
|
|
83
|
-
test('bindToolsToSessionMode:没有 exec(MCP/HTTP 形态)→ 不认领,绝不误锁', async () => {
|
|
84
|
-
const ws = fakeWorkStore()
|
|
85
|
-
const registry = {
|
|
86
|
-
TOOL_META: { rsp_key: { kind: 'write' } },
|
|
87
|
-
TOOL_IMPL: { rsp_key: () => 'ok' },
|
|
88
|
-
}
|
|
89
|
-
bindToolsToSessionMode(registry, { getWorkStore: () => ws })
|
|
90
|
-
assert.equal(await registry.TOOL_IMPL.rsp_key({ action: 'down' }), 'ok')
|
|
91
|
-
assert.equal(ws.calls.length, 0)
|
|
92
|
-
})
|
|
93
|
-
|
|
94
|
-
test('bindToolsToSessionMode:幂等,重复绑定不会叠加多层', async () => {
|
|
95
|
-
const ws = fakeWorkStore()
|
|
96
|
-
const registry = {
|
|
97
|
-
TOOL_META: { rsp_drag: { kind: 'write' } },
|
|
98
|
-
TOOL_IMPL: { rsp_drag: () => 'ok' },
|
|
99
|
-
}
|
|
100
|
-
bindToolsToSessionMode(registry, { getWorkStore: () => ws })
|
|
101
|
-
const once = registry.TOOL_IMPL.rsp_drag
|
|
102
|
-
bindToolsToSessionMode(registry, { getWorkStore: () => ws })
|
|
103
|
-
assert.equal(registry.TOOL_IMPL.rsp_drag, once)
|
|
104
|
-
await registry.TOOL_IMPL.rsp_drag({}, exec('s1'))
|
|
105
|
-
assert.equal(ws.calls.length, 1, '一次调用只认领一次')
|
|
106
|
-
})
|
|
107
|
-
|
|
108
|
-
test('bindToolsToSessionMode:锁定会话时工具调用直接抛错(不偷偷写错工作台)', async () => {
|
|
109
|
-
const registry = {
|
|
110
|
-
TOOL_META: { rsp_text: { kind: 'write' } },
|
|
111
|
-
TOOL_IMPL: { rsp_text: () => 'ok' },
|
|
112
|
-
}
|
|
113
|
-
bindToolsToSessionMode(registry, { getWorkStore: () => fakeWorkStore('locked') })
|
|
114
|
-
await assert.rejects(() => registry.TOOL_IMPL.rsp_text({ text: 'x' }, exec('s1')), (err) => {
|
|
115
|
-
assert.equal(err.code, 'SESSION_MODE_LOCKED')
|
|
116
|
-
return true
|
|
117
|
-
})
|
|
118
|
-
})
|
|
@@ -1,385 +0,0 @@
|
|
|
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
|
-
})
|