dsh-progress-union 0.5.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/e2e.test.mjs CHANGED
@@ -1,77 +1,77 @@
1
- /**
2
- * Phase 5 端到端冒烟(task_22):建板 → 用户改 → 不 ack 读锚点 → ack 后再读 →
3
- * 时钟推进 12 分钟读 staleMin;另验 KV 缺席时锚点仍出且 persistent=false。
4
- */
5
- import assert from 'node:assert/strict'
6
- import fs from 'node:fs'
7
- import os from 'node:os'
8
- import path from 'node:path'
9
- import test from 'node:test'
10
-
11
- process.env.DSH_HOME = fs.mkdtempSync(path.join(os.tmpdir(), 'union-test-'))
12
- const { SESSION, createHarness, jsonReq, callRoute, withClock } = await import('./harness.mjs')
13
-
14
- function execUpdate(harness, args) {
15
- return harness.toolMap.get('progress_update').definition.execute(args, { agent: harness.agent })
16
- }
17
-
18
- test('端到端:建板→用户改→ack→陈旧推进,锚点各阶段状态正确迁移', async () => {
19
- const harness = createHarness()
20
- await harness.run()
21
- await callRoute(harness, '/progress-union/active', jsonReq('POST', '/progress-union/active', { sessionId: SESSION, active: true, source: 'command' }))
22
- const anchor = () => harness.provided.get('progressAnchor').get(SESSION)
23
-
24
- // 阶段 0:启用但空板
25
- assert.equal(anchor().board.summary.total, 0)
26
- assert.match(harness.contexts.get('progress-union-kickoff')(), /第一个工具调用必须是 progress_update/)
27
-
28
- // 阶段 1:建板(3 项,1 doing)
29
- const added = await execUpdate(harness, {
30
- changes: [{ title: '抓帧', status: 'doing', current: '渲染第 1/900 帧' }, { title: '合成' }, { title: '字幕' }],
31
- })
32
- assert.equal(added.summary.total, 3)
33
- assert.equal(anchor().board.inFlight.length, 1)
34
- assert.equal(anchor().board.inFlight[0].title, '抓帧')
35
-
36
- // 阶段 2:用户面板改"合成"备注 → pending=1,userEdits 出现,时间戳推进
37
- const itemId = (await callRoute(harness, '/progress-union/snapshot', { method: 'GET', url: `/progress-union/snapshot?sessionId=${SESSION}`, headers: {} })).json().items[1].id
38
- await callRoute(harness, '/progress-union/action', jsonReq('POST', '/progress-union/action', { sessionId: SESSION, changes: [{ id: itemId, note: '用 4K' }] }))
39
- const pending = anchor()
40
- assert.equal(pending.board.pendingUserChanges, 1)
41
- assert.equal(pending.userEdits[0].fields[0].field, 'note')
42
- assert.ok(pending.revision > added.revision)
43
-
44
- // 阶段 3:agent ack → pending=0,锚点 revision 不因确认增长
45
- const acked = await execUpdate(harness, { changes: [], ackUserRevision: pending.revision })
46
- assert.equal(acked.revision, pending.revision)
47
- assert.equal(anchor().board.pendingUserChanges, 0)
48
- assert.equal(anchor().userEdits.length, 0)
49
-
50
- // 阶段 4:真实时钟推进 12 分钟无更新 → staleMin=12 且 kickoff 提醒复现
51
- const clock = withClock()
52
- try {
53
- clock.advance(12 * 60 * 1000)
54
- const later = harness.provided.get('progressAnchor').get(SESSION)
55
- assert.equal(later.time.staleMin, 12)
56
- assert.match(harness.contexts.get('progress-union-kickoff')(), /12 分钟没有更新/)
57
- } finally {
58
- clock.restore()
59
- }
60
-
61
- // 阶段 5:完成推进 → percent 迁移
62
- await execUpdate(harness, { changes: [{ id: added.changed[0].id, status: 'done' }] })
63
- const afterDone = anchor()
64
- assert.equal(afterDone.board.summary.done, 1)
65
- assert.equal(afterDone.board.summary.percent, 33) // (100+0+0)/3
66
- })
67
-
68
- test('KV 缺席:内存态可用,锚点照出且 persistent=false', async () => {
69
- const harness = createHarness({ storageless: true })
70
- await harness.run()
71
- await callRoute(harness, '/progress-union/active', jsonReq('POST', '/progress-union/active', { sessionId: SESSION, active: true, source: 'command' }))
72
- await execUpdate(harness, { changes: [{ title: '仅内存', status: 'doing' }] })
73
- const anchor = harness.provided.get('progressAnchor').get(SESSION)
74
- assert.equal(anchor.persistent, false)
75
- assert.equal(anchor.board.summary.total, 1)
76
- assert.equal(anchor.board.inFlight[0].title, '仅内存')
77
- })
1
+ /**
2
+ * Phase 5 端到端冒烟(task_22):建板 → 用户改 → 不 ack 读锚点 → ack 后再读 →
3
+ * 时钟推进 12 分钟读 staleMin;另验 KV 缺席时锚点仍出且 persistent=false。
4
+ */
5
+ import assert from 'node:assert/strict'
6
+ import fs from 'node:fs'
7
+ import os from 'node:os'
8
+ import path from 'node:path'
9
+ import test from 'node:test'
10
+
11
+ process.env.DSH_HOME = fs.mkdtempSync(path.join(os.tmpdir(), 'union-test-'))
12
+ const { SESSION, createHarness, jsonReq, callRoute, withClock } = await import('./harness.mjs')
13
+
14
+ function execUpdate(harness, args) {
15
+ return harness.toolMap.get('progress_update').definition.execute(args, { agent: harness.agent })
16
+ }
17
+
18
+ test('端到端:建板→用户改→ack→陈旧推进,锚点各阶段状态正确迁移', async () => {
19
+ const harness = createHarness()
20
+ await harness.run()
21
+ await callRoute(harness, '/progress-union/active', jsonReq('POST', '/progress-union/active', { sessionId: SESSION, active: true, source: 'command' }))
22
+ const anchor = () => harness.provided.get('progressAnchor').get(SESSION)
23
+
24
+ // 阶段 0:启用但空板
25
+ assert.equal(anchor().board.summary.total, 0)
26
+ assert.match(harness.contexts.get('progress-union-kickoff')(), /第一个工具调用必须是 progress_update/)
27
+
28
+ // 阶段 1:建板(3 项,1 doing)
29
+ const added = await execUpdate(harness, {
30
+ changes: [{ title: '抓帧', status: 'doing', current: '渲染第 1/900 帧' }, { title: '合成' }, { title: '字幕' }],
31
+ })
32
+ assert.equal(added.summary.total, 3)
33
+ assert.equal(anchor().board.inFlight.length, 1)
34
+ assert.equal(anchor().board.inFlight[0].title, '抓帧')
35
+
36
+ // 阶段 2:用户面板改"合成"备注 → pending=1,userEdits 出现,时间戳推进
37
+ const itemId = (await callRoute(harness, '/progress-union/snapshot', { method: 'GET', url: `/progress-union/snapshot?sessionId=${SESSION}`, headers: {} })).json().items[1].id
38
+ await callRoute(harness, '/progress-union/action', jsonReq('POST', '/progress-union/action', { sessionId: SESSION, changes: [{ id: itemId, note: '用 4K' }] }))
39
+ const pending = anchor()
40
+ assert.equal(pending.board.pendingUserChanges, 1)
41
+ assert.equal(pending.userEdits[0].fields[0].field, 'note')
42
+ assert.ok(pending.revision > added.revision)
43
+
44
+ // 阶段 3:agent ack → pending=0,锚点 revision 不因确认增长
45
+ const acked = await execUpdate(harness, { changes: [], ackUserRevision: pending.revision })
46
+ assert.equal(acked.revision, pending.revision)
47
+ assert.equal(anchor().board.pendingUserChanges, 0)
48
+ assert.equal(anchor().userEdits.length, 0)
49
+
50
+ // 阶段 4:真实时钟推进 12 分钟无更新 → staleMin=12 且 kickoff 提醒复现
51
+ const clock = withClock()
52
+ try {
53
+ clock.advance(12 * 60 * 1000)
54
+ const later = harness.provided.get('progressAnchor').get(SESSION)
55
+ assert.equal(later.time.staleMin, 12)
56
+ assert.match(harness.contexts.get('progress-union-kickoff')(), /12 分钟没有更新/)
57
+ } finally {
58
+ clock.restore()
59
+ }
60
+
61
+ // 阶段 5:完成推进 → percent 迁移
62
+ await execUpdate(harness, { changes: [{ id: added.changed[0].id, status: 'done' }] })
63
+ const afterDone = anchor()
64
+ assert.equal(afterDone.board.summary.done, 1)
65
+ assert.equal(afterDone.board.summary.percent, 33) // (100+0+0)/3
66
+ })
67
+
68
+ test('KV 缺席:内存态可用,锚点照出且 persistent=false', async () => {
69
+ const harness = createHarness({ storageless: true })
70
+ await harness.run()
71
+ await callRoute(harness, '/progress-union/active', jsonReq('POST', '/progress-union/active', { sessionId: SESSION, active: true, source: 'command' }))
72
+ await execUpdate(harness, { changes: [{ title: '仅内存', status: 'doing' }] })
73
+ const anchor = harness.provided.get('progressAnchor').get(SESSION)
74
+ assert.equal(anchor.persistent, false)
75
+ assert.equal(anchor.board.summary.total, 1)
76
+ assert.equal(anchor.board.inFlight[0].title, '仅内存')
77
+ })
package/test/harness.mjs CHANGED
@@ -1,220 +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, 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 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
+ }