bingocode 1.0.29 → 1.0.31

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.
Files changed (52) hide show
  1. package/adapters/common/__tests__/chat-queue.test.ts +61 -0
  2. package/adapters/common/__tests__/format.test.ts +148 -0
  3. package/adapters/common/__tests__/http-client.test.ts +105 -0
  4. package/adapters/common/__tests__/message-buffer.test.ts +84 -0
  5. package/adapters/common/__tests__/message-dedup.test.ts +57 -0
  6. package/adapters/common/__tests__/session-store.test.ts +62 -0
  7. package/adapters/common/__tests__/ws-bridge.test.ts +177 -0
  8. package/adapters/common/attachment/__tests__/attachment-limits.test.ts +52 -0
  9. package/adapters/common/attachment/__tests__/attachment-store.test.ts +108 -0
  10. package/adapters/common/attachment/__tests__/image-block-watcher.test.ts +115 -0
  11. package/adapters/feishu/__tests__/card-errors.test.ts +194 -0
  12. package/adapters/feishu/__tests__/cardkit.test.ts +295 -0
  13. package/adapters/feishu/__tests__/extract-payload.test.ts +77 -0
  14. package/adapters/feishu/__tests__/feishu.test.ts +907 -0
  15. package/adapters/feishu/__tests__/flush-controller.test.ts +290 -0
  16. package/adapters/feishu/__tests__/markdown-style.test.ts +353 -0
  17. package/adapters/feishu/__tests__/media.test.ts +120 -0
  18. package/adapters/feishu/__tests__/streaming-card.test.ts +914 -0
  19. package/adapters/telegram/__tests__/media.test.ts +86 -0
  20. package/adapters/telegram/__tests__/telegram.test.ts +115 -0
  21. package/bin/bingo-win.cjs +26 -0
  22. package/bin/bingocode-win.cjs +55 -3
  23. package/bin/claude-win.cjs +55 -3
  24. package/package.json +1 -1
  25. package/src/entrypoints/cli.tsx +4 -2
  26. package/src/manager/CliMenuManager.tsx +48 -17
  27. package/src/server/__tests__/conversation-service.test.ts +173 -0
  28. package/src/server/__tests__/conversations.test.ts +458 -0
  29. package/src/server/__tests__/cron-scheduler.test.ts +575 -0
  30. package/src/server/__tests__/e2e/business-flow.test.ts +841 -0
  31. package/src/server/__tests__/e2e/full-flow.test.ts +357 -0
  32. package/src/server/__tests__/fixtures/mock-sdk-cli.ts +123 -0
  33. package/src/server/__tests__/haha-oauth-api.test.ts +146 -0
  34. package/src/server/__tests__/haha-oauth-service.test.ts +185 -0
  35. package/src/server/__tests__/providers-real.test.ts +244 -0
  36. package/src/server/__tests__/providers.test.ts +579 -0
  37. package/src/server/__tests__/proxy-streaming.test.ts +317 -0
  38. package/src/server/__tests__/proxy-transform.test.ts +469 -0
  39. package/src/server/__tests__/real-llm-test.ts +526 -0
  40. package/src/server/__tests__/scheduled-tasks.test.ts +371 -0
  41. package/src/server/__tests__/sessions.test.ts +786 -0
  42. package/src/server/__tests__/settings.test.ts +376 -0
  43. package/src/server/__tests__/skills.test.ts +125 -0
  44. package/src/server/__tests__/tasks.test.ts +171 -0
  45. package/src/server/__tests__/team-watcher.test.ts +400 -0
  46. package/src/server/__tests__/teams.test.ts +627 -0
  47. package/src/server/ensureSingletonLocalServer.ts +1 -1
  48. package/src/server/middleware/cors.test.ts +27 -0
  49. package/src/utils/__tests__/cronFrequency.test.ts +153 -0
  50. package/src/utils/__tests__/cronTasks.test.ts +204 -0
  51. package/src/utils/computerUse/permissions.test.ts +44 -0
  52. package/src/utils/config.ts +15 -0
@@ -0,0 +1,841 @@
1
+ /**
2
+ * Business Flow E2E Tests
3
+ *
4
+ * 完整的业务流程测试:涵盖定时任务、权限模式、Agent 管理、
5
+ * WebSocket 对话、搜索、会话历史互通等所有核心业务逻辑。
6
+ */
7
+
8
+ import { describe, it, expect, beforeAll, afterAll } from 'bun:test'
9
+ import * as fs from 'fs/promises'
10
+ import * as path from 'path'
11
+ import * as os from 'os'
12
+
13
+ let server: ReturnType<typeof Bun.serve>
14
+ let baseUrl: string
15
+ let wsUrl: string
16
+ let tmpDir: string
17
+
18
+ async function startTestServer() {
19
+ tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'claude-biz-'))
20
+ process.env.CLAUDE_CONFIG_DIR = tmpDir
21
+ await fs.mkdir(path.join(tmpDir, 'projects'), { recursive: true })
22
+ await fs.mkdir(path.join(tmpDir, 'agents'), { recursive: true })
23
+
24
+ const { startServer } = await import('../../index.js')
25
+ const port = 14000 + Math.floor(Math.random() * 1000)
26
+ server = startServer(port, '127.0.0.1')
27
+ baseUrl = `http://127.0.0.1:${port}`
28
+ wsUrl = `ws://127.0.0.1:${port}`
29
+ }
30
+
31
+ async function api(method: string, urlPath: string, body?: unknown) {
32
+ const res = await fetch(`${baseUrl}${urlPath}`, {
33
+ method,
34
+ headers: { 'Content-Type': 'application/json' },
35
+ body: body ? JSON.stringify(body) : undefined,
36
+ })
37
+ const data = await res.json().catch(() => null)
38
+ return { status: res.status, data }
39
+ }
40
+
41
+ describe('Business Flow: Scheduled Tasks', () => {
42
+ beforeAll(startTestServer)
43
+ afterAll(async () => {
44
+ server?.stop()
45
+ await fs.rm(tmpDir, { recursive: true, force: true })
46
+ })
47
+
48
+ // ==========================================================================
49
+ // 定时任务完整生命周期
50
+ // ==========================================================================
51
+
52
+ it('should start with no scheduled tasks', async () => {
53
+ const { status, data } = await api('GET', '/api/scheduled-tasks')
54
+ expect(status).toBe(200)
55
+ expect(data.tasks).toEqual([])
56
+ })
57
+
58
+ it('should create a daily task with all fields', async () => {
59
+ const { status, data } = await api('POST', '/api/scheduled-tasks', {
60
+ name: 'morning-standup',
61
+ description: 'Generate standup report from yesterday',
62
+ cron: '0 9 * * 1-5',
63
+ prompt: 'Look at git log from yesterday, summarize changes, list blockers',
64
+ recurring: true,
65
+ permissionMode: 'default',
66
+ model: 'claude-sonnet-4-6',
67
+ folderPath: '/Users/dev/project',
68
+ useWorktree: true,
69
+ })
70
+ expect(status).toBe(201)
71
+ expect(data.task).toBeDefined()
72
+ expect(data.task.id).toMatch(/^[0-9a-f]{8}$/)
73
+ expect(data.task.name).toBe('morning-standup')
74
+ expect(data.task.cron).toBe('0 9 * * 1-5')
75
+ expect(data.task.prompt).toContain('git log')
76
+ expect(data.task.recurring).toBe(true)
77
+ expect(data.task.permissionMode).toBe('default')
78
+ expect(data.task.model).toBe('claude-sonnet-4-6')
79
+ expect(data.task.createdAt).toBeGreaterThan(0)
80
+ })
81
+
82
+ it('should create a second one-shot task', async () => {
83
+ const { status, data } = await api('POST', '/api/scheduled-tasks', {
84
+ cron: '30 14 5 4 *',
85
+ prompt: 'Run security audit',
86
+ recurring: false,
87
+ })
88
+ expect(status).toBe(201)
89
+ expect(data.task.recurring).toBe(false)
90
+ })
91
+
92
+ it('should list both tasks', async () => {
93
+ const { data } = await api('GET', '/api/scheduled-tasks')
94
+ expect(data.tasks.length).toBe(2)
95
+ expect(data.tasks[0].name).toBe('morning-standup')
96
+ })
97
+
98
+ it('should update task schedule', async () => {
99
+ const { data: listData } = await api('GET', '/api/scheduled-tasks')
100
+ const taskId = listData.tasks[0].id
101
+
102
+ const { status, data } = await api('PUT', `/api/scheduled-tasks/${taskId}`, {
103
+ cron: '0 8 * * 1-5',
104
+ description: 'Updated: earlier standup',
105
+ })
106
+ expect(status).toBe(200)
107
+ expect(data.task.cron).toBe('0 8 * * 1-5')
108
+ expect(data.task.description).toBe('Updated: earlier standup')
109
+ // Other fields should remain unchanged
110
+ expect(data.task.name).toBe('morning-standup')
111
+ expect(data.task.prompt).toContain('git log')
112
+ })
113
+
114
+ it('should reject creating task without cron', async () => {
115
+ const { status, data } = await api('POST', '/api/scheduled-tasks', {
116
+ prompt: 'missing cron field',
117
+ })
118
+ expect(status).toBe(400)
119
+ expect(data.error).toBeDefined()
120
+ })
121
+
122
+ it('should reject creating task without prompt', async () => {
123
+ const { status } = await api('POST', '/api/scheduled-tasks', {
124
+ cron: '0 * * * *',
125
+ })
126
+ expect(status).toBe(400)
127
+ })
128
+
129
+ it('should reject updating non-existent task', async () => {
130
+ const { status } = await api('PUT', '/api/scheduled-tasks/nonexistent', {
131
+ cron: '0 * * * *',
132
+ })
133
+ expect(status).toBe(404)
134
+ })
135
+
136
+ it('should reject deleting non-existent task', async () => {
137
+ const { status } = await api('DELETE', '/api/scheduled-tasks/nonexistent')
138
+ expect(status).toBe(404)
139
+ })
140
+
141
+ it('should delete one task', async () => {
142
+ const { data: listData } = await api('GET', '/api/scheduled-tasks')
143
+ const taskId = listData.tasks[1].id
144
+
145
+ const { status } = await api('DELETE', `/api/scheduled-tasks/${taskId}`)
146
+ expect([200, 204]).toContain(status)
147
+
148
+ const { data: afterDelete } = await api('GET', '/api/scheduled-tasks')
149
+ expect(afterDelete.tasks.length).toBe(1)
150
+ })
151
+
152
+ it('should persist tasks to disk', async () => {
153
+ const filePath = path.join(tmpDir, 'scheduled_tasks.json')
154
+ const raw = await fs.readFile(filePath, 'utf-8')
155
+ const parsed = JSON.parse(raw)
156
+ expect(parsed.tasks.length).toBe(1)
157
+ expect(parsed.tasks[0].name).toBe('morning-standup')
158
+ })
159
+ })
160
+
161
+ describe('Business Flow: Permission Modes', () => {
162
+ beforeAll(startTestServer)
163
+ afterAll(async () => {
164
+ server?.stop()
165
+ await fs.rm(tmpDir, { recursive: true, force: true })
166
+ })
167
+
168
+ const VALID_MODES = ['default', 'acceptEdits', 'plan', 'bypassPermissions', 'dontAsk']
169
+
170
+ it('should default to "default" mode', async () => {
171
+ const { data } = await api('GET', '/api/permissions/mode')
172
+ expect(data.mode).toBe('default')
173
+ })
174
+
175
+ for (const mode of VALID_MODES) {
176
+ it(`should switch to "${mode}" mode and persist`, async () => {
177
+ const { status, data } = await api('PUT', '/api/permissions/mode', { mode })
178
+ expect(status).toBe(200)
179
+ expect(data.mode).toBe(mode)
180
+
181
+ // Verify it persisted
182
+ const { data: verify } = await api('GET', '/api/permissions/mode')
183
+ expect(verify.mode).toBe(mode)
184
+ })
185
+ }
186
+
187
+ it('should reject invalid mode "auto"', async () => {
188
+ const { status, data } = await api('PUT', '/api/permissions/mode', { mode: 'auto' })
189
+ expect(status).toBe(400)
190
+ expect(data.message).toContain('Invalid permission mode')
191
+ })
192
+
193
+ it('should reject missing mode field', async () => {
194
+ const { status } = await api('PUT', '/api/permissions/mode', {})
195
+ expect(status).toBe(400)
196
+ })
197
+
198
+ it('should persist mode to settings file', async () => {
199
+ await api('PUT', '/api/permissions/mode', { mode: 'plan' })
200
+ const settingsPath = path.join(tmpDir, 'settings.json')
201
+ const raw = await fs.readFile(settingsPath, 'utf-8')
202
+ const settings = JSON.parse(raw)
203
+ expect(settings.defaultMode).toBe('plan')
204
+ })
205
+ })
206
+
207
+ describe('Business Flow: Agent Management', () => {
208
+ beforeAll(startTestServer)
209
+ afterAll(async () => {
210
+ server?.stop()
211
+ await fs.rm(tmpDir, { recursive: true, force: true })
212
+ })
213
+
214
+ it('should start with shared active/all agent payload', async () => {
215
+ const { data } = await api('GET', '/api/agents')
216
+ expect(Array.isArray(data.activeAgents)).toBe(true)
217
+ expect(Array.isArray(data.allAgents)).toBe(true)
218
+ expect(data.activeAgents.length).toBeGreaterThan(0)
219
+ expect(data.activeAgents.some((agent: any) => agent.source === 'built-in')).toBe(true)
220
+ })
221
+
222
+ it('should create a new agent with full config', async () => {
223
+ const { status, data } = await api('POST', '/api/agents', {
224
+ name: 'security-auditor',
225
+ description: 'Audits code for security vulnerabilities',
226
+ model: 'claude-opus-4-7',
227
+ tools: ['Read', 'Grep', 'Glob', 'Bash'],
228
+ systemPrompt: 'You are a security expert. Focus on OWASP top 10.',
229
+ color: 'red',
230
+ })
231
+ expect(status).toBe(201)
232
+ })
233
+
234
+ it('should create a second agent', async () => {
235
+ const { status } = await api('POST', '/api/agents', {
236
+ name: 'test-writer',
237
+ description: 'Writes unit tests',
238
+ model: 'claude-sonnet-4-6',
239
+ tools: ['Read', 'Write', 'Bash'],
240
+ })
241
+ expect(status).toBe(201)
242
+ })
243
+
244
+ it('should list both created agents in CRUD detail endpoint while shared list stays source-based', async () => {
245
+ const { data } = await api('GET', '/api/agents')
246
+ expect(data.activeAgents.length).toBeGreaterThan(0)
247
+ expect(data.activeAgents.some((agent: any) => agent.source === 'built-in')).toBe(true)
248
+ expect(data.activeAgents.some((agent: any) => agent.agentType === 'security-auditor')).toBe(false)
249
+ expect(data.activeAgents.some((agent: any) => agent.agentType === 'test-writer')).toBe(false)
250
+
251
+ const securityAuditor = await api('GET', '/api/agents/security-auditor')
252
+ const testWriter = await api('GET', '/api/agents/test-writer')
253
+ expect(securityAuditor.data.agent.name).toBe('security-auditor')
254
+ expect(testWriter.data.agent.name).toBe('test-writer')
255
+ })
256
+
257
+ it('should get agent details', async () => {
258
+ const { data } = await api('GET', '/api/agents/security-auditor')
259
+ expect(data.agent.name).toBe('security-auditor')
260
+ expect(data.agent.description).toContain('security')
261
+ expect(data.agent.model).toBe('claude-opus-4-7')
262
+ expect(data.agent.systemPrompt).toContain('OWASP')
263
+ })
264
+
265
+ it('should update agent tools', async () => {
266
+ const { status, data } = await api('PUT', '/api/agents/security-auditor', {
267
+ tools: ['Read', 'Grep', 'Glob', 'Bash', 'WebFetch'],
268
+ description: 'Updated: now with web access',
269
+ })
270
+ expect(status).toBe(200)
271
+ expect(data.agent).toBeDefined()
272
+ expect(data.agent.name).toBe('security-auditor')
273
+ expect(data.agent.description).toBe('Updated: now with web access')
274
+ })
275
+
276
+ it('should reject creating duplicate agent', async () => {
277
+ const { status, data } = await api('POST', '/api/agents', {
278
+ name: 'security-auditor',
279
+ description: 'duplicate',
280
+ })
281
+ expect(status).toBe(409)
282
+ expect(data.error).toBe('CONFLICT')
283
+ })
284
+
285
+ it('should reject getting non-existent agent', async () => {
286
+ const { status } = await api('GET', '/api/agents/nonexistent')
287
+ expect(status).toBe(404)
288
+ })
289
+
290
+ it('should keep deleted agent out of shared active list while built-ins remain', async () => {
291
+ const { status } = await api('DELETE', '/api/agents/test-writer')
292
+ expect([200, 204]).toContain(status)
293
+
294
+ const { data } = await api('GET', '/api/agents')
295
+ expect(data.activeAgents.some((agent: any) => agent.agentType === 'test-writer')).toBe(false)
296
+ expect(data.activeAgents.some((agent: any) => agent.source === 'built-in')).toBe(true)
297
+
298
+ const deleted = await api('GET', '/api/agents/test-writer')
299
+ expect(deleted.status).toBe(404)
300
+ })
301
+
302
+ it('should persist agent to YAML file on disk', async () => {
303
+ const filePath = path.join(tmpDir, 'agents', 'security-auditor.yaml')
304
+ const raw = await fs.readFile(filePath, 'utf-8')
305
+ expect(raw).toContain('security-auditor')
306
+ expect(raw).toContain('OWASP')
307
+ })
308
+
309
+ it('should reject deleting non-existent agent', async () => {
310
+ const { status } = await api('DELETE', '/api/agents/nonexistent')
311
+ expect(status).toBe(404)
312
+ })
313
+ })
314
+
315
+ describe('Business Flow: Models & Effort', () => {
316
+ beforeAll(startTestServer)
317
+ afterAll(async () => {
318
+ server?.stop()
319
+ await fs.rm(tmpDir, { recursive: true, force: true })
320
+ })
321
+
322
+ it('should return 4 available models', async () => {
323
+ const { data } = await api('GET', '/api/models')
324
+ expect(data.models.length).toBe(4)
325
+ const names = data.models.map((m: any) => m.name)
326
+ expect(names).toContain('Opus 4.7')
327
+ expect(names).toContain('Opus 4.7 1M')
328
+ expect(names).toContain('Sonnet 4.6')
329
+ expect(names).toContain('Haiku 4.5')
330
+ })
331
+
332
+ it('should default to Sonnet model', async () => {
333
+ const { data } = await api('GET', '/api/models/current')
334
+ expect(data.model.id).toBe('claude-sonnet-4-6')
335
+ })
336
+
337
+ it('should switch to Opus 4.7', async () => {
338
+ const { status } = await api('PUT', '/api/models/current', {
339
+ modelId: 'claude-opus-4-7',
340
+ })
341
+ expect(status).toBe(200)
342
+
343
+ const { data } = await api('GET', '/api/models/current')
344
+ expect(data.model.id).toBe('claude-opus-4-7')
345
+ expect(data.model.name).toBe('Opus 4.7')
346
+ })
347
+
348
+ it('should switch to Haiku 4.5', async () => {
349
+ await api('PUT', '/api/models/current', { modelId: 'claude-haiku-4-5' })
350
+ const { data } = await api('GET', '/api/models/current')
351
+ expect(data.model.name).toBe('Haiku 4.5')
352
+ })
353
+
354
+ it('should reject empty model ID', async () => {
355
+ const { status } = await api('PUT', '/api/models/current', { modelId: '' })
356
+ expect(status).toBe(400)
357
+ })
358
+
359
+ it('should reject missing model ID', async () => {
360
+ const { status } = await api('PUT', '/api/models/current', {})
361
+ expect(status).toBe(400)
362
+ })
363
+
364
+ it('should default effort to medium', async () => {
365
+ const { data } = await api('GET', '/api/effort')
366
+ expect(data.level).toBe('medium')
367
+ expect(data.available).toEqual(['low', 'medium', 'high', 'max'])
368
+ })
369
+
370
+ it('should set effort to max', async () => {
371
+ const { status, data } = await api('PUT', '/api/effort', { level: 'max' })
372
+ expect(status).toBe(200)
373
+ expect(data.level).toBe('max')
374
+
375
+ const { data: verify } = await api('GET', '/api/effort')
376
+ expect(verify.level).toBe('max')
377
+ })
378
+
379
+ it('should set effort to low', async () => {
380
+ await api('PUT', '/api/effort', { level: 'low' })
381
+ const { data } = await api('GET', '/api/effort')
382
+ expect(data.level).toBe('low')
383
+ })
384
+
385
+ it('should reject invalid effort level', async () => {
386
+ const { status, data } = await api('PUT', '/api/effort', { level: 'extreme' })
387
+ expect(status).toBe(400)
388
+ expect(data.message).toContain('Invalid effort level')
389
+ })
390
+
391
+ it('should persist model and effort to settings file', async () => {
392
+ await api('PUT', '/api/models/current', { modelId: 'claude-opus-4-7' })
393
+ await api('PUT', '/api/effort', { level: 'high' })
394
+
395
+ const settingsPath = path.join(tmpDir, 'settings.json')
396
+ const raw = await fs.readFile(settingsPath, 'utf-8')
397
+ const settings = JSON.parse(raw)
398
+ expect(settings.model).toBe('claude-opus-4-7')
399
+ expect(settings.effort).toBe('high')
400
+ })
401
+ })
402
+
403
+ describe('Business Flow: Sessions & CLI Interop', () => {
404
+ beforeAll(startTestServer)
405
+ afterAll(async () => {
406
+ server?.stop()
407
+ await fs.rm(tmpDir, { recursive: true, force: true })
408
+ })
409
+
410
+ let sessionId: string
411
+
412
+ it('should create a session', async () => {
413
+ const { status, data } = await api('POST', '/api/sessions', {
414
+ workDir: '/Users/dev/my-project',
415
+ })
416
+ expect(status).toBe(201)
417
+ expect(data.sessionId).toMatch(/^[0-9a-f-]{36}$/)
418
+ sessionId = data.sessionId
419
+ })
420
+
421
+ it('should create session JSONL file on disk (CLI compatible)', async () => {
422
+ const projectDir = path.join(tmpDir, 'projects')
423
+ const dirs = await fs.readdir(projectDir)
424
+ expect(dirs.length).toBeGreaterThan(0)
425
+
426
+ // Find the session file
427
+ let found = false
428
+ for (const dir of dirs) {
429
+ const files = await fs.readdir(path.join(projectDir, dir))
430
+ if (files.some((f) => f === `${sessionId}.jsonl`)) {
431
+ found = true
432
+ break
433
+ }
434
+ }
435
+ expect(found).toBe(true)
436
+ })
437
+
438
+ it('should simulate CLI writing messages (JSONL format)', async () => {
439
+ // Simulate what CLI does: append JSONL entries
440
+ const projectDir = path.join(tmpDir, 'projects')
441
+ const dirs = await fs.readdir(projectDir)
442
+ let sessionFile = ''
443
+ for (const dir of dirs) {
444
+ const candidate = path.join(projectDir, dir, `${sessionId}.jsonl`)
445
+ try {
446
+ await fs.access(candidate)
447
+ sessionFile = candidate
448
+ break
449
+ } catch {}
450
+ }
451
+ expect(sessionFile).not.toBe('')
452
+
453
+ // Append user message (mimicking CLI JSONL format - must include message.role)
454
+ const userEntry = {
455
+ type: 'user',
456
+ uuid: 'msg-001',
457
+ message: { role: 'user', content: [{ type: 'text', text: 'Hello from CLI' }] },
458
+ timestamp: new Date().toISOString(),
459
+ sessionId,
460
+ }
461
+ await fs.appendFile(sessionFile, JSON.stringify(userEntry) + '\n')
462
+
463
+ // Append assistant message
464
+ const assistantEntry = {
465
+ type: 'assistant',
466
+ uuid: 'msg-002',
467
+ message: { role: 'assistant', content: [{ type: 'text', text: 'Hello! How can I help you today?' }] },
468
+ timestamp: new Date().toISOString(),
469
+ sessionId,
470
+ parentUuid: 'msg-001',
471
+ }
472
+ await fs.appendFile(sessionFile, JSON.stringify(assistantEntry) + '\n')
473
+ })
474
+
475
+ it('should read CLI-written messages via API', async () => {
476
+ const { status, data } = await api('GET', `/api/sessions/${sessionId}/messages`)
477
+ expect(status).toBe(200)
478
+ expect(data.messages.length).toBe(2)
479
+ expect(data.messages[0].type).toBe('user')
480
+ expect(data.messages[0].content).toBeDefined()
481
+ expect(data.messages[1].type).toBe('assistant')
482
+ })
483
+
484
+ it('should show CLI messages in session list', async () => {
485
+ const { data } = await api('GET', '/api/sessions')
486
+ const session = data.sessions.find((s: any) => s.id === sessionId)
487
+ expect(session).toBeDefined()
488
+ expect(session.messageCount).toBeGreaterThanOrEqual(2)
489
+ expect(session.title).toContain('Hello from CLI')
490
+ })
491
+
492
+ it('should rename session and verify', async () => {
493
+ await api('PATCH', `/api/sessions/${sessionId}`, { title: 'CLI Test Session' })
494
+ const { data } = await api('GET', `/api/sessions/${sessionId}`)
495
+ expect(data.title).toBe('CLI Test Session')
496
+ })
497
+
498
+ it('should rename be persisted as JSONL entry (CLI compatible)', async () => {
499
+ const projectDir = path.join(tmpDir, 'projects')
500
+ const dirs = await fs.readdir(projectDir)
501
+ let sessionFile = ''
502
+ for (const dir of dirs) {
503
+ const candidate = path.join(projectDir, dir, `${sessionId}.jsonl`)
504
+ try {
505
+ await fs.access(candidate)
506
+ sessionFile = candidate
507
+ break
508
+ } catch {}
509
+ }
510
+
511
+ const raw = await fs.readFile(sessionFile, 'utf-8')
512
+ const lines = raw.trim().split('\n')
513
+ const lastEntry = JSON.parse(lines[lines.length - 1])
514
+ expect(lastEntry.type).toBe('custom-title')
515
+ expect(lastEntry.customTitle).toBe('CLI Test Session')
516
+ })
517
+ })
518
+
519
+ describe('Business Flow: Search', () => {
520
+ beforeAll(async () => {
521
+ await startTestServer()
522
+ // Create test files to search
523
+ const testDir = path.join(tmpDir, 'test-workspace')
524
+ await fs.mkdir(testDir, { recursive: true })
525
+ await fs.writeFile(path.join(testDir, 'main.ts'), 'export function startServer() {\n console.log("starting")\n}\n')
526
+ await fs.writeFile(path.join(testDir, 'utils.ts'), 'export function helper() { return 42 }\n')
527
+ await fs.writeFile(path.join(testDir, 'config.json'), '{"port": 3456}\n')
528
+ })
529
+ afterAll(async () => {
530
+ server?.stop()
531
+ await fs.rm(tmpDir, { recursive: true, force: true })
532
+ })
533
+
534
+ it('should find matches in workspace files', async () => {
535
+ const { status, data } = await api('POST', '/api/search', {
536
+ query: 'startServer',
537
+ cwd: path.join(tmpDir, 'test-workspace'),
538
+ })
539
+ expect(status).toBe(200)
540
+ expect(data.results.length).toBeGreaterThan(0)
541
+ expect(data.results[0].text).toContain('startServer')
542
+ })
543
+
544
+ it('should respect maxResults', async () => {
545
+ const { data } = await api('POST', '/api/search', {
546
+ query: 'export',
547
+ cwd: path.join(tmpDir, 'test-workspace'),
548
+ maxResults: 1,
549
+ })
550
+ expect(data.results.length).toBeLessThanOrEqual(1)
551
+ })
552
+
553
+ it('should return empty for non-matching query', async () => {
554
+ const { data } = await api('POST', '/api/search', {
555
+ query: 'nonexistent_string_xyz123',
556
+ cwd: path.join(tmpDir, 'test-workspace'),
557
+ })
558
+ expect(data.results.length).toBe(0)
559
+ })
560
+
561
+ it('should reject empty query', async () => {
562
+ const { status } = await api('POST', '/api/search', {
563
+ query: '',
564
+ cwd: tmpDir,
565
+ })
566
+ expect(status).toBe(400)
567
+ })
568
+ })
569
+
570
+ describe('Business Flow: WebSocket Chat', () => {
571
+ beforeAll(startTestServer)
572
+ afterAll(async () => {
573
+ server?.stop()
574
+ await fs.rm(tmpDir, { recursive: true, force: true })
575
+ })
576
+
577
+ it('should establish WebSocket connection and receive connected event', async () => {
578
+ const messages: any[] = []
579
+ const ws = new WebSocket(`${wsUrl}/ws/ws-test-1`)
580
+
581
+ await new Promise<void>((resolve) => {
582
+ ws.onmessage = (event) => {
583
+ messages.push(JSON.parse(event.data as string))
584
+ if (messages.length >= 1) {
585
+ ws.close()
586
+ resolve()
587
+ }
588
+ }
589
+ ws.onerror = () => { ws.close(); resolve() }
590
+ setTimeout(() => { ws.close(); resolve() }, 3000)
591
+ })
592
+
593
+ expect(messages[0].type).toBe('connected')
594
+ expect(messages[0].sessionId).toBe('ws-test-1')
595
+ })
596
+
597
+ it('should echo message and transition through states', async () => {
598
+ const messages: any[] = []
599
+ const ws = new WebSocket(`${wsUrl}/ws/ws-test-2`)
600
+
601
+ await new Promise<void>((resolve) => {
602
+ ws.onopen = () => {}
603
+ ws.onmessage = (event) => {
604
+ const msg = JSON.parse(event.data as string)
605
+ messages.push(msg)
606
+ if (msg.type === 'connected') {
607
+ ws.send(JSON.stringify({ type: 'user_message', content: 'test message' }))
608
+ }
609
+ if (msg.type === 'status' && msg.state === 'idle' && messages.length > 3) {
610
+ ws.close()
611
+ resolve()
612
+ }
613
+ }
614
+ ws.onerror = () => { ws.close(); resolve() }
615
+ setTimeout(() => { ws.close(); resolve() }, 5000)
616
+ })
617
+
618
+ const types = messages.map((m) => m.type)
619
+ expect(types).toContain('connected')
620
+ expect(types).toContain('status')
621
+ expect(types).toContain('content_start')
622
+ expect(types).toContain('content_delta')
623
+ expect(types).toContain('message_complete')
624
+
625
+ // Should have thinking state first
626
+ const statusMsgs = messages.filter((m) => m.type === 'status')
627
+ expect(statusMsgs[0].state).toBe('thinking')
628
+ })
629
+
630
+ it('should handle ping/pong', async () => {
631
+ const messages: any[] = []
632
+ const ws = new WebSocket(`${wsUrl}/ws/ws-test-3`)
633
+
634
+ await new Promise<void>((resolve) => {
635
+ ws.onmessage = (event) => {
636
+ const msg = JSON.parse(event.data as string)
637
+ messages.push(msg)
638
+ if (msg.type === 'connected') {
639
+ ws.send(JSON.stringify({ type: 'ping' }))
640
+ }
641
+ if (msg.type === 'pong') {
642
+ ws.close()
643
+ resolve()
644
+ }
645
+ }
646
+ setTimeout(() => { ws.close(); resolve() }, 3000)
647
+ })
648
+
649
+ expect(messages.some((m) => m.type === 'pong')).toBe(true)
650
+ })
651
+
652
+ it('should handle stop_generation', async () => {
653
+ const messages: any[] = []
654
+ const ws = new WebSocket(`${wsUrl}/ws/ws-test-4`)
655
+
656
+ await new Promise<void>((resolve) => {
657
+ ws.onmessage = (event) => {
658
+ const msg = JSON.parse(event.data as string)
659
+ messages.push(msg)
660
+ if (msg.type === 'connected') {
661
+ ws.send(JSON.stringify({ type: 'stop_generation' }))
662
+ }
663
+ if (msg.type === 'status' && msg.state === 'idle') {
664
+ ws.close()
665
+ resolve()
666
+ }
667
+ }
668
+ setTimeout(() => { ws.close(); resolve() }, 3000)
669
+ })
670
+
671
+ const idleStatus = messages.find((m) => m.type === 'status' && m.state === 'idle')
672
+ expect(idleStatus).toBeDefined()
673
+ })
674
+
675
+ it('should handle invalid message gracefully', async () => {
676
+ const messages: any[] = []
677
+ const ws = new WebSocket(`${wsUrl}/ws/ws-test-5`)
678
+
679
+ await new Promise<void>((resolve) => {
680
+ ws.onmessage = (event) => {
681
+ const msg = JSON.parse(event.data as string)
682
+ messages.push(msg)
683
+ if (msg.type === 'connected') {
684
+ ws.send('not valid json {{{')
685
+ }
686
+ if (msg.type === 'error') {
687
+ ws.close()
688
+ resolve()
689
+ }
690
+ }
691
+ setTimeout(() => { ws.close(); resolve() }, 3000)
692
+ })
693
+
694
+ const errorMsg = messages.find((m) => m.type === 'error')
695
+ expect(errorMsg).toBeDefined()
696
+ expect(errorMsg.code).toBe('PARSE_ERROR')
697
+ })
698
+
699
+ it('should reject invalid session ID in WebSocket URL', async () => {
700
+ // Path traversal gets resolved by URL parser, so test with special chars
701
+ const res = await fetch(`${baseUrl}/ws/invalid session!@#`, {
702
+ headers: { 'Upgrade': 'websocket', 'Connection': 'Upgrade' },
703
+ })
704
+ // URL with special chars either returns 400 (invalid ID) or 404 (path resolution)
705
+ expect([400, 404]).toContain(res.status)
706
+ })
707
+ })
708
+
709
+ describe('Business Flow: Settings Persistence', () => {
710
+ beforeAll(startTestServer)
711
+ afterAll(async () => {
712
+ server?.stop()
713
+ await fs.rm(tmpDir, { recursive: true, force: true })
714
+ })
715
+
716
+ it('should write and read complex settings', async () => {
717
+ const settings = {
718
+ theme: 'dark',
719
+ model: 'claude-opus-4-7',
720
+ effort: 'high',
721
+ outputStyle: 'verbose',
722
+ permissions: {
723
+ allow: ['Bash(npm test)', 'Bash(npm run build)', 'Read'],
724
+ deny: ['Bash(rm -rf /)'],
725
+ },
726
+ }
727
+
728
+ await api('PUT', '/api/settings/user', settings)
729
+ const { data } = await api('GET', '/api/settings/user')
730
+
731
+ expect(data.theme).toBe('dark')
732
+ expect(data.model).toBe('claude-opus-4-7')
733
+ expect(data.permissions.allow).toContain('Read')
734
+ expect(data.permissions.deny).toContain('Bash(rm -rf /)')
735
+ })
736
+
737
+ it('should merge settings (not overwrite)', async () => {
738
+ // First write
739
+ await api('PUT', '/api/settings/user', { theme: 'dark' })
740
+ // Second write (should merge, not overwrite)
741
+ await api('PUT', '/api/settings/user', { outputStyle: 'concise' })
742
+
743
+ const { data } = await api('GET', '/api/settings/user')
744
+ expect(data.theme).toBe('dark') // Should still be there
745
+ expect(data.outputStyle).toBe('concise')
746
+ })
747
+
748
+ it('should support project-level settings', async () => {
749
+ const projectRoot = path.join(tmpDir, 'test-project')
750
+ await fs.mkdir(path.join(projectRoot, '.claude'), { recursive: true })
751
+
752
+ await api('PUT', `/api/settings/project?projectRoot=${encodeURIComponent(projectRoot)}`, {
753
+ permissions: { allow: ['Bash(make)'] },
754
+ })
755
+
756
+ const { data } = await api('GET', `/api/settings/project?projectRoot=${encodeURIComponent(projectRoot)}`)
757
+ expect(data.permissions.allow).toContain('Bash(make)')
758
+ })
759
+
760
+ it('should merge user and project settings', async () => {
761
+ await api('PUT', '/api/settings/user', { theme: 'light', model: 'claude-sonnet-4-6' })
762
+
763
+ const { data } = await api('GET', '/api/settings')
764
+ expect(data.theme).toBeDefined()
765
+ expect(data.model).toBeDefined()
766
+ })
767
+ })
768
+
769
+ describe('Business Flow: Status & Diagnostics', () => {
770
+ beforeAll(startTestServer)
771
+ afterAll(async () => {
772
+ server?.stop()
773
+ await fs.rm(tmpDir, { recursive: true, force: true })
774
+ })
775
+
776
+ it('should return health with uptime', async () => {
777
+ const { data } = await api('GET', '/api/status')
778
+ expect(data.status).toBe('ok')
779
+ expect(data.uptime).toBeGreaterThanOrEqual(0)
780
+ })
781
+
782
+ it('should return diagnostics with system info', async () => {
783
+ const { data } = await api('GET', '/api/status/diagnostics')
784
+ expect(data.platform).toBe(process.platform)
785
+ expect(data.arch).toBe(process.arch)
786
+ expect(data.configDir).toBe(tmpDir)
787
+ expect(data.memory.rss).toBeGreaterThan(0)
788
+ })
789
+
790
+ it('should return usage stats', async () => {
791
+ const { data } = await api('GET', '/api/status/usage')
792
+ expect(data).toHaveProperty('totalInputTokens')
793
+ expect(data).toHaveProperty('totalOutputTokens')
794
+ expect(data).toHaveProperty('totalCost')
795
+ })
796
+
797
+ it('should return user info with project list', async () => {
798
+ const { data } = await api('GET', '/api/status/user')
799
+ expect(data.configDir).toBe(tmpDir)
800
+ expect(Array.isArray(data.projects)).toBe(true)
801
+ })
802
+
803
+ it('should reject non-GET methods', async () => {
804
+ const { status } = await api('POST', '/api/status')
805
+ expect(status).toBe(405)
806
+ })
807
+ })
808
+
809
+ describe('Business Flow: Error Handling', () => {
810
+ beforeAll(startTestServer)
811
+ afterAll(async () => {
812
+ server?.stop()
813
+ await fs.rm(tmpDir, { recursive: true, force: true })
814
+ })
815
+
816
+ it('should return 404 for unknown API resource', async () => {
817
+ const { status, data } = await api('GET', '/api/unknown')
818
+ expect(status).toBe(404)
819
+ expect(data.error).toBeDefined()
820
+ })
821
+
822
+ it('should return 404 for unknown session', async () => {
823
+ const { status } = await api('GET', '/api/sessions/00000000-0000-0000-0000-000000000000')
824
+ expect(status).toBe(404)
825
+ })
826
+
827
+ it('should handle malformed JSON body gracefully', async () => {
828
+ const res = await fetch(`${baseUrl}/api/sessions`, {
829
+ method: 'POST',
830
+ headers: { 'Content-Type': 'application/json' },
831
+ body: '{invalid json',
832
+ })
833
+ expect(res.status).toBe(400)
834
+ })
835
+
836
+ it('should return proper error structure', async () => {
837
+ const { data } = await api('GET', '/api/sessions/nonexistent')
838
+ expect(data).toHaveProperty('error')
839
+ expect(data).toHaveProperty('message')
840
+ })
841
+ })