dsh-progress-union 0.4.0 → 0.5.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/test/harness.mjs CHANGED
@@ -1,216 +1,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 } = {}) {
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
- session: {
68
- id: SESSION,
69
- append(type, data) { appends.push({ type, data }); return Promise.resolve() },
70
- },
71
- ctx: {
72
- effect(fn) {
73
- return fn()
74
- },
75
- systemPrompt: {
76
- section({ name, text }) {
77
- sections.set(name, text)
78
- return () => sections.delete(name)
79
- },
80
- context({ name, text }) {
81
- contexts.set(name, text)
82
- return () => contexts.delete(name)
83
- },
84
- },
85
- on(type, handler) {
86
- agentHooks.push({ type, handler })
87
- return () => {
88
- const index = agentHooks.findIndex((h) => h.type === type && h.handler === handler)
89
- if (index >= 0) agentHooks.splice(index, 1)
90
- }
91
- },
92
- },
93
- }
94
-
95
- const agents = { list: () => [agent] }
96
-
97
- const ctx = {
98
- get(name) {
99
- return { storage, webServer, systemPrompt, tools, agents, commands, llm, settings }[name]
100
- },
101
- on(type, handler) {
102
- listeners.push({ type, handler })
103
- return () => {
104
- const index = listeners.findIndex((h) => h.type === type && h.handler === handler)
105
- if (index >= 0) listeners.splice(index, 1)
106
- }
107
- },
108
- effect(fn) {
109
- const dispose = fn()
110
- return () => {
111
- if (typeof dispose === 'function') dispose()
112
- }
113
- },
114
- inject(deps, callback) {
115
- if (withProjection && deps.includes('sessionProjections')) {
116
- callback({
117
- sessionProjections: {
118
- register(definition) {
119
- projections.set(definition.key, definition)
120
- return () => projections.delete(definition.key)
121
- },
122
- },
123
- })
124
- } else {
125
- callback({})
126
- }
127
- },
128
- }
129
- if (withProvide) {
130
- ctx.provide = (name, factory) => {
131
- provided.set(name, factory())
132
- return () => provided.delete(name)
133
- }
134
- }
135
-
136
- return {
137
- ctx,
138
- config,
139
- agent,
140
- appends,
141
- records,
142
- routes,
143
- toolMap,
144
- commandMap,
145
- sections,
146
- contexts,
147
- listeners,
148
- agentHooks,
149
- provided,
150
- projections,
151
- run: () => apply(ctx, config),
152
- /** 按 (session, {type, data}) 派发一条 session/event */
153
- emit(type, data, session) {
154
- for (const h of listeners.filter((l) => l.type === 'session/event')) h.handler(session ?? { id: SESSION }, { type, data })
155
- },
156
- }
157
- }
158
-
159
- export function jsonReq(method, url, body) {
160
- const payload = Buffer.from(JSON.stringify(body))
161
- return {
162
- method,
163
- url,
164
- headers: { 'content-type': 'application/json' },
165
- async *[Symbol.asyncIterator]() {
166
- yield payload
167
- },
168
- }
169
- }
170
-
171
- export function getReq(url) {
172
- return { method: 'GET', url, headers: {} }
173
- }
174
-
175
- export function fakeRes() {
176
- return {
177
- status: 0,
178
- body: '',
179
- writeHead(status) {
180
- this.status = status
181
- },
182
- end(text) {
183
- this.body = text
184
- },
185
- json() {
186
- return JSON.parse(this.body || '{}')
187
- },
188
- }
189
- }
190
-
191
- export async function callRoute(harness, path, req) {
192
- const route = harness.routes.get(path)
193
- if (!route) throw new Error(`route not registered: ${path}`)
194
- const res = fakeRes()
195
- await route.handler(req, res)
196
- return res
197
- }
198
-
199
- export function withClock(fn) {
200
- const real = Date.now
201
- let offset = 0
202
- Date.now = () => real() + offset
203
- const advance = (ms) => {
204
- offset += ms
205
- }
206
- const restore = () => {
207
- Date.now = real
208
- }
209
- return { advance, restore, real }
210
- }
211
-
212
- export async function flush() {
213
- // 让 persist 写链落完
214
- await new Promise((resolve) => setImmediate(resolve))
215
- await new Promise((resolve) => setImmediate(resolve))
216
- }
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
+ }