dsh-progress-union 0.5.1 → 0.5.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.
package/test/harness.mjs CHANGED
@@ -1,220 +1,225 @@
1
- /**
2
- * 共享 mock 基座:纯内存 services 驱动 apply(),不起真宿主。
3
- * 形状对齐 l3j3y3/dsh-progress-board test/host.test.mjs 的 createHarness。
4
- */
5
- import { apply } from '../index.js'
6
-
7
- export const SESSION = 'session-union-test'
8
-
9
- export function createHarness({ seed = {}, config = {}, withProjection = true, withProvide = true, storageless = false, llm = undefined, settings = undefined, cwd = '' } = {}) {
10
- const records = { ...seed }
11
- const routes = new Map()
12
- const toolMap = new Map()
13
- const commandMap = new Map()
14
- const sections = new Map()
15
- const contexts = new Map()
16
- const listeners = []
17
- const agentHooks = []
18
- const provided = new Map()
19
- const projections = new Map()
20
-
21
- const unit = {
22
- async loadAll() {
23
- return { tables: { sessions: { ...records } } }
24
- },
25
- async putRecord(_table, key, record) {
26
- records[key] = record
27
- },
28
- async close() {},
29
- }
30
-
31
- const storage = storageless
32
- ? { backend: { names: () => [], get: () => ({}) } }
33
- : {
34
- backend: {
35
- names: () => ['json'],
36
- get: () => ({ kv: { open: async () => unit } }),
37
- },
38
- }
39
-
40
- const webServer = {
41
- register(route) {
42
- routes.set(route.path, route)
43
- return () => routes.delete(route.path)
44
- },
45
- }
46
-
47
- const tools = {
48
- register(definition, options) {
49
- toolMap.set(definition.name, { definition, options })
50
- return () => toolMap.delete(definition.name)
51
- },
52
- }
53
-
54
- const commands = {
55
- register(definition) {
56
- commandMap.set(definition.name, definition)
57
- return () => commandMap.delete(definition.name)
58
- },
59
- }
60
-
61
- const systemPrompt = {}
62
-
63
- const appends = []
64
- const agent = {
65
- id: SESSION,
66
- // v0.3.0:todo 镜像写通道。默认记录调用;测试可覆写 append 注入失败。
67
- // v0.5.0:cwd 放在 session **顶层**(实测 shape,不是 header.cwd)——plan 扫描靠它。
68
- session: {
69
- id: SESSION,
70
- ...(cwd ? { cwd } : {}),
71
- append(type, data) { appends.push({ type, data }); return Promise.resolve() },
72
- },
73
- ctx: {
74
- effect(fn) {
75
- return fn()
76
- },
77
- systemPrompt: {
78
- section({ name, text }) {
79
- sections.set(name, text)
80
- return () => sections.delete(name)
81
- },
82
- context({ name, text }) {
83
- contexts.set(name, text)
84
- return () => contexts.delete(name)
85
- },
86
- },
87
- on(type, handler) {
88
- agentHooks.push({ type, handler })
89
- return () => {
90
- const index = agentHooks.findIndex((h) => h.type === type && h.handler === handler)
91
- if (index >= 0) agentHooks.splice(index, 1)
92
- }
93
- },
94
- },
95
- }
96
-
97
- const agents = { list: () => [agent] }
98
-
99
- const ctx = {
100
- get(name) {
101
- return { storage, webServer, systemPrompt, tools, agents, commands, llm, settings }[name]
102
- },
103
- on(type, handler) {
104
- listeners.push({ type, handler })
105
- return () => {
106
- const index = listeners.findIndex((h) => h.type === type && h.handler === handler)
107
- if (index >= 0) listeners.splice(index, 1)
108
- }
109
- },
110
- effect(fn) {
111
- const dispose = fn()
112
- return () => {
113
- if (typeof dispose === 'function') dispose()
114
- }
115
- },
116
- inject(deps, callback) {
117
- if (withProjection && deps.includes('sessionProjections')) {
118
- callback({
119
- sessionProjections: {
120
- register(definition) {
121
- projections.set(definition.key, definition)
122
- return () => projections.delete(definition.key)
123
- },
124
- },
125
- })
126
- } else {
127
- callback({})
128
- }
129
- },
130
- }
131
- if (withProvide) {
132
- ctx.provide = (name, factory) => {
133
- provided.set(name, factory())
134
- return () => provided.delete(name)
135
- }
136
- }
137
-
138
- return {
139
- ctx,
140
- config,
141
- agent,
142
- appends,
143
- records,
144
- routes,
145
- toolMap,
146
- commandMap,
147
- sections,
148
- contexts,
149
- listeners,
150
- agentHooks,
151
- provided,
152
- projections,
153
- run: () => apply(ctx, config),
154
- /** 按 (session, {type, data}) 派发一条 session/event */
155
- emit(type, data, session) {
156
- // 默认 session 带顶层 cwd(实测 shape):plan 扫描靠事件流里的 session。
157
- const target = session ?? (cwd ? { id: SESSION, cwd } : { id: SESSION })
158
- for (const h of listeners.filter((l) => l.type === 'session/event')) h.handler(target, { type, data })
159
- },
160
- }
161
- }
162
-
163
- export function jsonReq(method, url, body) {
164
- const payload = Buffer.from(JSON.stringify(body))
165
- return {
166
- method,
167
- url,
168
- headers: { 'content-type': 'application/json' },
169
- async *[Symbol.asyncIterator]() {
170
- yield payload
171
- },
172
- }
173
- }
174
-
175
- export function getReq(url) {
176
- return { method: 'GET', url, headers: {} }
177
- }
178
-
179
- export function fakeRes() {
180
- return {
181
- status: 0,
182
- body: '',
183
- writeHead(status) {
184
- this.status = status
185
- },
186
- end(text) {
187
- this.body = text
188
- },
189
- json() {
190
- return JSON.parse(this.body || '{}')
191
- },
192
- }
193
- }
194
-
195
- export async function callRoute(harness, path, req) {
196
- const route = harness.routes.get(path)
197
- if (!route) throw new Error(`route not registered: ${path}`)
198
- const res = fakeRes()
199
- await route.handler(req, res)
200
- return res
201
- }
202
-
203
- export function withClock(fn) {
204
- const real = Date.now
205
- let offset = 0
206
- Date.now = () => real() + offset
207
- const advance = (ms) => {
208
- offset += ms
209
- }
210
- const restore = () => {
211
- Date.now = real
212
- }
213
- return { advance, restore, real }
214
- }
215
-
216
- export async function flush() {
217
- // 让 persist 写链落完
218
- await new Promise((resolve) => setImmediate(resolve))
219
- await new Promise((resolve) => setImmediate(resolve))
220
- }
1
+ /**
2
+ * 共享 mock 基座:纯内存 services 驱动 apply(),不起真宿主。
3
+ * 形状对齐 l3j3y3/dsh-progress-board test/host.test.mjs 的 createHarness。
4
+ */
5
+ import { apply } from '../index.js'
6
+
7
+ export const SESSION = 'session-union-test'
8
+
9
+ export function createHarness({ seed = {}, config = {}, withProjection = true, withProvide = true, storageless = false, llm = undefined, settings = undefined, cwd = '' } = {}) {
10
+ const records = { ...seed }
11
+ const routes = new Map()
12
+ const toolMap = new Map()
13
+ const commandMap = new Map()
14
+ const sections = new Map()
15
+ const contexts = new Map()
16
+ const listeners = []
17
+ const agentHooks = []
18
+ const provided = new Map()
19
+ const projections = new Map()
20
+
21
+ const unit = {
22
+ async loadAll() {
23
+ return { tables: { sessions: { ...records } } }
24
+ },
25
+ async putRecord(_table, key, record) {
26
+ records[key] = record
27
+ },
28
+ async close() {},
29
+ }
30
+
31
+ const storage = storageless
32
+ ? { backend: { names: () => [], get: () => ({}) } }
33
+ : {
34
+ backend: {
35
+ names: () => ['json'],
36
+ get: () => ({ kv: { open: async () => unit } }),
37
+ },
38
+ }
39
+
40
+ const webServer = {
41
+ register(route) {
42
+ routes.set(route.path, route)
43
+ return () => routes.delete(route.path)
44
+ },
45
+ }
46
+
47
+ const tools = {
48
+ register(definition, options) {
49
+ toolMap.set(definition.name, { definition, options })
50
+ return () => toolMap.delete(definition.name)
51
+ },
52
+ }
53
+
54
+ const commands = {
55
+ register(definition) {
56
+ commandMap.set(definition.name, definition)
57
+ return () => commandMap.delete(definition.name)
58
+ },
59
+ }
60
+
61
+ const systemPrompt = {}
62
+
63
+ const appends = []
64
+ const followups = []
65
+ const agent = {
66
+ id: SESSION,
67
+ // /progress-goal 续轮驱动器经 agent.followup 排队;这里记录被派发的轮次消息。
68
+ followups,
69
+ followup(message) { followups.push(message) },
70
+ // v0.3.0:todo 镜像写通道。默认记录调用;测试可覆写 append 注入失败。
71
+ // v0.5.0:cwd 放在 session **顶层**(实测 shape,不是 header.cwd)——plan 扫描靠它。
72
+ session: {
73
+ id: SESSION,
74
+ ...(cwd ? { cwd } : {}),
75
+ append(type, data) { appends.push({ type, data }); return Promise.resolve() },
76
+ },
77
+ ctx: {
78
+ effect(fn) {
79
+ return fn()
80
+ },
81
+ systemPrompt: {
82
+ section({ name, text }) {
83
+ sections.set(name, text)
84
+ return () => sections.delete(name)
85
+ },
86
+ context({ name, text }) {
87
+ contexts.set(name, text)
88
+ return () => contexts.delete(name)
89
+ },
90
+ },
91
+ on(type, handler) {
92
+ agentHooks.push({ type, handler })
93
+ return () => {
94
+ const index = agentHooks.findIndex((h) => h.type === type && h.handler === handler)
95
+ if (index >= 0) agentHooks.splice(index, 1)
96
+ }
97
+ },
98
+ },
99
+ }
100
+
101
+ const agents = { list: () => [agent] }
102
+
103
+ const ctx = {
104
+ get(name) {
105
+ return { storage, webServer, systemPrompt, tools, agents, commands, llm, settings }[name]
106
+ },
107
+ on(type, handler) {
108
+ listeners.push({ type, handler })
109
+ return () => {
110
+ const index = listeners.findIndex((h) => h.type === type && h.handler === handler)
111
+ if (index >= 0) listeners.splice(index, 1)
112
+ }
113
+ },
114
+ effect(fn) {
115
+ const dispose = fn()
116
+ return () => {
117
+ if (typeof dispose === 'function') dispose()
118
+ }
119
+ },
120
+ inject(deps, callback) {
121
+ if (withProjection && deps.includes('sessionProjections')) {
122
+ callback({
123
+ sessionProjections: {
124
+ register(definition) {
125
+ projections.set(definition.key, definition)
126
+ return () => projections.delete(definition.key)
127
+ },
128
+ },
129
+ })
130
+ } else {
131
+ callback({})
132
+ }
133
+ },
134
+ }
135
+ if (withProvide) {
136
+ ctx.provide = (name, factory) => {
137
+ provided.set(name, factory())
138
+ return () => provided.delete(name)
139
+ }
140
+ }
141
+
142
+ return {
143
+ ctx,
144
+ config,
145
+ agent,
146
+ appends,
147
+ followups,
148
+ records,
149
+ routes,
150
+ toolMap,
151
+ commandMap,
152
+ sections,
153
+ contexts,
154
+ listeners,
155
+ agentHooks,
156
+ provided,
157
+ projections,
158
+ run: () => apply(ctx, config),
159
+ /** 按 (session, {type, data}) 派发一条 session/event */
160
+ emit(type, data, session) {
161
+ // 默认 session 带顶层 cwd(实测 shape):plan 扫描靠事件流里的 session。
162
+ const target = session ?? (cwd ? { id: SESSION, cwd } : { id: SESSION })
163
+ for (const h of listeners.filter((l) => l.type === 'session/event')) h.handler(target, { type, data })
164
+ },
165
+ }
166
+ }
167
+
168
+ export function jsonReq(method, url, body) {
169
+ const payload = Buffer.from(JSON.stringify(body))
170
+ return {
171
+ method,
172
+ url,
173
+ headers: { 'content-type': 'application/json' },
174
+ async *[Symbol.asyncIterator]() {
175
+ yield payload
176
+ },
177
+ }
178
+ }
179
+
180
+ export function getReq(url) {
181
+ return { method: 'GET', url, headers: {} }
182
+ }
183
+
184
+ export function fakeRes() {
185
+ return {
186
+ status: 0,
187
+ body: '',
188
+ writeHead(status) {
189
+ this.status = status
190
+ },
191
+ end(text) {
192
+ this.body = text
193
+ },
194
+ json() {
195
+ return JSON.parse(this.body || '{}')
196
+ },
197
+ }
198
+ }
199
+
200
+ export async function callRoute(harness, path, req) {
201
+ const route = harness.routes.get(path)
202
+ if (!route) throw new Error(`route not registered: ${path}`)
203
+ const res = fakeRes()
204
+ await route.handler(req, res)
205
+ return res
206
+ }
207
+
208
+ export function withClock(fn) {
209
+ const real = Date.now
210
+ let offset = 0
211
+ Date.now = () => real() + offset
212
+ const advance = (ms) => {
213
+ offset += ms
214
+ }
215
+ const restore = () => {
216
+ Date.now = real
217
+ }
218
+ return { advance, restore, real }
219
+ }
220
+
221
+ export async function flush() {
222
+ // 让 persist 写链落完
223
+ await new Promise((resolve) => setImmediate(resolve))
224
+ await new Promise((resolve) => setImmediate(resolve))
225
+ }