bingocode 1.0.28 → 1.0.30

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 (53) 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/adapters/tsconfig.json +18 -0
  22. package/bunfig.toml +1 -0
  23. package/package.json +1 -1
  24. package/preload.ts +30 -0
  25. package/scripts/count-app-loc.ts +256 -0
  26. package/scripts/release.ts +130 -0
  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/middleware/cors.test.ts +27 -0
  48. package/src/utils/__tests__/cronFrequency.test.ts +153 -0
  49. package/src/utils/__tests__/cronTasks.test.ts +204 -0
  50. package/src/utils/computerUse/permissions.test.ts +44 -0
  51. package/stubs/ant-claude-for-chrome-mcp.ts +24 -0
  52. package/stubs/color-diff-napi.ts +45 -0
  53. package/tsconfig.json +24 -0
@@ -0,0 +1,627 @@
1
+ /**
2
+ * Unit tests for TeamService and Teams API
3
+ */
4
+
5
+ import { describe, it, expect, beforeEach, afterEach } from 'bun:test'
6
+ import * as fs from 'node:fs/promises'
7
+ import * as path from 'node:path'
8
+ import * as os from 'node:os'
9
+ import { TeamService } from '../services/teamService.js'
10
+
11
+ // ============================================================================
12
+ // Test helpers
13
+ // ============================================================================
14
+
15
+ let tmpDir: string
16
+ let service: TeamService
17
+
18
+ async function setupTmpConfigDir(): Promise<string> {
19
+ tmpDir = path.join(
20
+ os.tmpdir(),
21
+ `claude-teams-test-${Date.now()}-${Math.random().toString(36).slice(2)}`,
22
+ )
23
+ await fs.mkdir(path.join(tmpDir, 'teams'), { recursive: true })
24
+ await fs.mkdir(path.join(tmpDir, 'projects'), { recursive: true })
25
+ process.env.CLAUDE_CONFIG_DIR = tmpDir
26
+ return tmpDir
27
+ }
28
+
29
+ async function cleanupTmpDir(): Promise<void> {
30
+ if (tmpDir) {
31
+ await fs.rm(tmpDir, { recursive: true, force: true })
32
+ }
33
+ delete process.env.CLAUDE_CONFIG_DIR
34
+ }
35
+
36
+ /** Write a team config.json to the temp directory. */
37
+ async function writeTeamConfig(
38
+ teamName: string,
39
+ config: Record<string, unknown>,
40
+ ): Promise<string> {
41
+ const teamDir = path.join(tmpDir, 'teams', teamName)
42
+ await fs.mkdir(teamDir, { recursive: true })
43
+ const configPath = path.join(teamDir, 'config.json')
44
+ await fs.writeFile(configPath, JSON.stringify(config), 'utf-8')
45
+ return configPath
46
+ }
47
+
48
+ /** Write a mock JSONL transcript file under projects. */
49
+ async function writeTranscriptFile(
50
+ projectDir: string,
51
+ sessionId: string,
52
+ entries: Record<string, unknown>[],
53
+ ): Promise<string> {
54
+ const dir = path.join(tmpDir, 'projects', projectDir)
55
+ await fs.mkdir(dir, { recursive: true })
56
+ const filePath = path.join(dir, `${sessionId}.jsonl`)
57
+ const content = entries.map((e) => JSON.stringify(e)).join('\n') + '\n'
58
+ await fs.writeFile(filePath, content, 'utf-8')
59
+ return filePath
60
+ }
61
+
62
+ async function writeSubagentTranscriptFile(
63
+ projectDir: string,
64
+ leadSessionId: string,
65
+ fileName: string,
66
+ entries: Record<string, unknown>[],
67
+ ): Promise<string> {
68
+ const dir = path.join(tmpDir, 'projects', projectDir, leadSessionId, 'subagents')
69
+ await fs.mkdir(dir, { recursive: true })
70
+ const filePath = path.join(dir, fileName)
71
+ const content = entries.map((e) => JSON.stringify(e)).join('\n') + '\n'
72
+ await fs.writeFile(filePath, content, 'utf-8')
73
+ return filePath
74
+ }
75
+
76
+ /** Create a standard team config for testing. */
77
+ function makeTeamConfig(overrides?: Record<string, unknown>) {
78
+ return {
79
+ name: 'test-team',
80
+ description: 'A test team',
81
+ createdAt: 1700000000000,
82
+ leadAgentId: 'agent-lead',
83
+ members: [
84
+ {
85
+ agentId: 'agent-lead',
86
+ name: 'Lead Agent',
87
+ agentType: 'lead',
88
+ model: 'claude-opus-4-7',
89
+ color: '#ff0000',
90
+ joinedAt: 1700000000000,
91
+ tmuxPaneId: '%0',
92
+ cwd: '/tmp/project',
93
+ sessionId: 'session-lead-001',
94
+ isActive: true,
95
+ },
96
+ {
97
+ agentId: 'agent-worker',
98
+ name: 'Worker Agent',
99
+ agentType: 'worker',
100
+ model: 'claude-sonnet-4-20250514',
101
+ color: '#00ff00',
102
+ joinedAt: 1700000001000,
103
+ tmuxPaneId: '%1',
104
+ cwd: '/tmp/project/src',
105
+ sessionId: 'session-worker-001',
106
+ isActive: false,
107
+ },
108
+ ],
109
+ ...overrides,
110
+ }
111
+ }
112
+
113
+ // ============================================================================
114
+ // TeamService tests
115
+ // ============================================================================
116
+
117
+ describe('TeamService', () => {
118
+ beforeEach(async () => {
119
+ await setupTmpConfigDir()
120
+ service = new TeamService()
121
+ })
122
+
123
+ afterEach(async () => {
124
+ await cleanupTmpDir()
125
+ })
126
+
127
+ // --------------------------------------------------------------------------
128
+ // listTeams
129
+ // --------------------------------------------------------------------------
130
+
131
+ it('should return empty list when no teams exist', async () => {
132
+ const teams = await service.listTeams()
133
+ expect(teams).toEqual([])
134
+ })
135
+
136
+ it('should return empty list when teams directory does not exist', async () => {
137
+ await fs.rm(path.join(tmpDir, 'teams'), { recursive: true, force: true })
138
+ const teams = await service.listTeams()
139
+ expect(teams).toEqual([])
140
+ })
141
+
142
+ it('should list teams from config files', async () => {
143
+ await writeTeamConfig('alpha', makeTeamConfig({ name: 'alpha' }))
144
+ await writeTeamConfig('beta', makeTeamConfig({ name: 'beta', description: 'Beta team' }))
145
+
146
+ const teams = await service.listTeams()
147
+ expect(teams).toHaveLength(2)
148
+
149
+ const names = teams.map((t) => t.name).sort()
150
+ expect(names).toEqual(['alpha', 'beta'])
151
+ })
152
+
153
+ it('should compute memberCount and activeMemberCount', async () => {
154
+ await writeTeamConfig('gamma', makeTeamConfig({ name: 'gamma' }))
155
+
156
+ const teams = await service.listTeams()
157
+ expect(teams).toHaveLength(1)
158
+ expect(teams[0]!.memberCount).toBe(2)
159
+ expect(teams[0]!.activeMemberCount).toBe(1) // only lead is active
160
+ })
161
+
162
+ it('should skip malformed team directories', async () => {
163
+ // Create a team dir with invalid JSON
164
+ const badDir = path.join(tmpDir, 'teams', 'bad-team')
165
+ await fs.mkdir(badDir, { recursive: true })
166
+ await fs.writeFile(path.join(badDir, 'config.json'), 'not json', 'utf-8')
167
+
168
+ // Also create a valid team
169
+ await writeTeamConfig('good-team', makeTeamConfig({ name: 'good-team' }))
170
+
171
+ const teams = await service.listTeams()
172
+ expect(teams).toHaveLength(1)
173
+ expect(teams[0]!.name).toBe('good-team')
174
+ })
175
+
176
+ // --------------------------------------------------------------------------
177
+ // getTeam
178
+ // --------------------------------------------------------------------------
179
+
180
+ it('should return team detail with members', async () => {
181
+ await writeTeamConfig(
182
+ 'detail-team',
183
+ makeTeamConfig({
184
+ name: 'detail-team',
185
+ leadSessionId: 'lead-session-xyz',
186
+ }),
187
+ )
188
+
189
+ const detail = await service.getTeam('detail-team')
190
+ expect(detail.name).toBe('detail-team')
191
+ expect(detail.leadAgentId).toBe('agent-lead')
192
+ expect(detail.leadSessionId).toBe('lead-session-xyz')
193
+ expect(detail.members).toHaveLength(2)
194
+ expect(detail.members[0]!.agentId).toBe('agent-lead')
195
+ expect(detail.members[1]!.agentId).toBe('agent-worker')
196
+ })
197
+
198
+ it('should discover missing in-process members from subagent transcripts', async () => {
199
+ await writeTeamConfig(
200
+ 'subagent-team',
201
+ makeTeamConfig({
202
+ name: 'subagent-team',
203
+ leadSessionId: 'lead-session-subagents',
204
+ members: [
205
+ {
206
+ agentId: 'agent-lead',
207
+ name: 'Lead Agent',
208
+ agentType: 'lead',
209
+ joinedAt: 1700000000000,
210
+ tmuxPaneId: '%0',
211
+ cwd: '/tmp/project',
212
+ sessionId: 'session-lead-001',
213
+ isActive: true,
214
+ },
215
+ ],
216
+ }),
217
+ )
218
+
219
+ await writeSubagentTranscriptFile(
220
+ '-tmp-project',
221
+ 'lead-session-subagents',
222
+ 'agent-1.jsonl',
223
+ [
224
+ {
225
+ agentName: 'security-reviewer',
226
+ agentId: 'security-reviewer@subagent-team',
227
+ timestamp: '2026-01-01T00:00:00.000Z',
228
+ },
229
+ ],
230
+ )
231
+
232
+ const detail = await service.getTeam('subagent-team')
233
+ expect(detail.members.some((member) => member.name === 'security-reviewer')).toBe(true)
234
+ })
235
+
236
+ it('should derive running status for active member', async () => {
237
+ await writeTeamConfig('status-team', makeTeamConfig({ name: 'status-team' }))
238
+
239
+ const detail = await service.getTeam('status-team')
240
+ const lead = detail.members.find((m) => m.agentId === 'agent-lead')!
241
+ expect(lead.status).toBe('running')
242
+ })
243
+
244
+ it('should derive idle status for inactive member', async () => {
245
+ await writeTeamConfig('status-team', makeTeamConfig({ name: 'status-team' }))
246
+
247
+ const detail = await service.getTeam('status-team')
248
+ const worker = detail.members.find((m) => m.agentId === 'agent-worker')!
249
+ expect(worker.status).toBe('idle')
250
+ })
251
+
252
+ it('should derive running status when isActive is undefined', async () => {
253
+ const config = makeTeamConfig({ name: 'undef-team' })
254
+ // Remove isActive from the first member to simulate undefined
255
+ delete (config.members[0] as Record<string, unknown>).isActive
256
+ await writeTeamConfig('undef-team', config)
257
+
258
+ const detail = await service.getTeam('undef-team')
259
+ const lead = detail.members.find((m) => m.agentId === 'agent-lead')!
260
+ expect(lead.status).toBe('running')
261
+ })
262
+
263
+ it('should throw 404 for non-existent team', async () => {
264
+ expect(service.getTeam('nonexistent')).rejects.toThrow('Team not found')
265
+ })
266
+
267
+ // --------------------------------------------------------------------------
268
+ // getMemberTranscript
269
+ // --------------------------------------------------------------------------
270
+
271
+ it('should return transcript messages for a member', async () => {
272
+ await writeTeamConfig('transcript-team', makeTeamConfig({ name: 'transcript-team' }))
273
+
274
+ // Write a mock transcript JSONL for the lead session
275
+ await writeTranscriptFile('-tmp-project', 'session-lead-001', [
276
+ {
277
+ type: 'file-history-snapshot',
278
+ messageId: 'snap-1',
279
+ snapshot: {},
280
+ },
281
+ {
282
+ type: 'user',
283
+ uuid: 'msg-user-1',
284
+ message: { role: 'user', content: 'Hello team' },
285
+ timestamp: '2026-01-01T00:01:00.000Z',
286
+ },
287
+ {
288
+ type: 'assistant',
289
+ uuid: 'msg-asst-1',
290
+ message: {
291
+ role: 'assistant',
292
+ content: [{ type: 'text', text: 'Hi! Ready to help.' }],
293
+ },
294
+ timestamp: '2026-01-01T00:02:00.000Z',
295
+ },
296
+ ])
297
+
298
+ const messages = await service.getMemberTranscript(
299
+ 'transcript-team',
300
+ 'agent-lead',
301
+ )
302
+ expect(messages).toHaveLength(2)
303
+ expect(messages[0]!.type).toBe('user')
304
+ expect(messages[0]!.id).toBe('msg-user-1')
305
+ expect(messages[1]!.type).toBe('assistant')
306
+ })
307
+
308
+ it('should return empty array when member has no sessionId', async () => {
309
+ const config = makeTeamConfig({ name: 'no-session-team' })
310
+ delete (config.members[0] as Record<string, unknown>).sessionId
311
+ await writeTeamConfig('no-session-team', config)
312
+
313
+ const messages = await service.getMemberTranscript(
314
+ 'no-session-team',
315
+ 'agent-lead',
316
+ )
317
+ expect(messages).toEqual([])
318
+ })
319
+
320
+ it('should return empty array when transcript file not found', async () => {
321
+ await writeTeamConfig('no-file-team', makeTeamConfig({ name: 'no-file-team' }))
322
+
323
+ // Don't write any transcript file
324
+ const messages = await service.getMemberTranscript(
325
+ 'no-file-team',
326
+ 'agent-lead',
327
+ )
328
+ expect(messages).toEqual([])
329
+ })
330
+
331
+ it('should throw 404 for unknown member', async () => {
332
+ await writeTeamConfig('member-team', makeTeamConfig({ name: 'member-team' }))
333
+
334
+ expect(
335
+ service.getMemberTranscript('member-team', 'nonexistent-agent'),
336
+ ).rejects.toThrow('Team member not found')
337
+ })
338
+
339
+ it('should skip meta entries in transcript', async () => {
340
+ await writeTeamConfig('meta-team', makeTeamConfig({ name: 'meta-team' }))
341
+
342
+ await writeTranscriptFile('-tmp-project', 'session-lead-001', [
343
+ {
344
+ type: 'user',
345
+ uuid: 'msg-meta',
346
+ message: { role: 'user', content: 'internal meta' },
347
+ isMeta: true,
348
+ timestamp: '2026-01-01T00:00:30.000Z',
349
+ },
350
+ {
351
+ type: 'user',
352
+ uuid: 'msg-real',
353
+ message: { role: 'user', content: 'Real message' },
354
+ timestamp: '2026-01-01T00:01:00.000Z',
355
+ },
356
+ ])
357
+
358
+ const messages = await service.getMemberTranscript('meta-team', 'agent-lead')
359
+ expect(messages).toHaveLength(1)
360
+ expect(messages[0]!.id).toBe('msg-real')
361
+ })
362
+
363
+ // --------------------------------------------------------------------------
364
+ // sendMemberMessage
365
+ // --------------------------------------------------------------------------
366
+
367
+ it('should write member messages into the teammate inbox', async () => {
368
+ await writeTeamConfig('mailbox-team', makeTeamConfig({ name: 'mailbox-team' }))
369
+
370
+ await service.sendMemberMessage(
371
+ 'mailbox-team',
372
+ 'agent-worker',
373
+ 'Please review the latest diff',
374
+ )
375
+
376
+ const inboxPath = path.join(
377
+ tmpDir,
378
+ 'teams',
379
+ 'mailbox-team',
380
+ 'inboxes',
381
+ 'Worker-Agent.json',
382
+ )
383
+ const rawInbox = await fs.readFile(inboxPath, 'utf-8')
384
+ const inbox = JSON.parse(rawInbox) as Array<{
385
+ from: string
386
+ text: string
387
+ read: boolean
388
+ }>
389
+
390
+ expect(inbox).toHaveLength(1)
391
+ expect(inbox[0]).toMatchObject({
392
+ from: 'user',
393
+ text: 'Please review the latest diff',
394
+ read: false,
395
+ })
396
+ })
397
+
398
+ it('should send messages to inbox-discovered members', async () => {
399
+ await writeTeamConfig('inbox-team', makeTeamConfig({ name: 'inbox-team' }))
400
+ const inboxDir = path.join(tmpDir, 'teams', 'inbox-team', 'inboxes')
401
+ await fs.mkdir(inboxDir, { recursive: true })
402
+ await fs.writeFile(path.join(inboxDir, 'security-reviewer.json'), '[]', 'utf-8')
403
+
404
+ await service.sendMemberMessage(
405
+ 'inbox-team',
406
+ 'security-reviewer@inbox-team',
407
+ 'Check the auth changes',
408
+ )
409
+
410
+ const rawInbox = await fs.readFile(
411
+ path.join(inboxDir, 'security-reviewer.json'),
412
+ 'utf-8',
413
+ )
414
+ const inbox = JSON.parse(rawInbox) as Array<{ text: string }>
415
+ expect(inbox.at(-1)?.text).toBe('Check the auth changes')
416
+ })
417
+
418
+ // --------------------------------------------------------------------------
419
+ // deleteTeam
420
+ // --------------------------------------------------------------------------
421
+
422
+ it('should delete a team with no active members', async () => {
423
+ const config = makeTeamConfig({ name: 'deletable' })
424
+ // Set all members to inactive
425
+ for (const member of config.members) {
426
+ ;(member as Record<string, unknown>).isActive = false
427
+ }
428
+ await writeTeamConfig('deletable', config)
429
+
430
+ await service.deleteTeam('deletable')
431
+
432
+ // Team dir should be gone
433
+ const teamDir = path.join(tmpDir, 'teams', 'deletable')
434
+ expect(fs.access(teamDir)).rejects.toThrow()
435
+ })
436
+
437
+ it('should refuse to delete a team with active members', async () => {
438
+ await writeTeamConfig('active-team', makeTeamConfig({ name: 'active-team' }))
439
+
440
+ expect(service.deleteTeam('active-team')).rejects.toThrow(
441
+ 'has active members',
442
+ )
443
+ })
444
+
445
+ it('should throw 404 when deleting non-existent team', async () => {
446
+ expect(service.deleteTeam('ghost')).rejects.toThrow('Team not found')
447
+ })
448
+ })
449
+
450
+ // ============================================================================
451
+ // Teams API integration tests
452
+ // ============================================================================
453
+
454
+ describe('Teams API', () => {
455
+ let baseUrl: string
456
+ let server: ReturnType<typeof Bun.serve> | null = null
457
+
458
+ beforeEach(async () => {
459
+ await setupTmpConfigDir()
460
+ service = new TeamService()
461
+
462
+ const { handleTeamsApi } = await import('../api/teams.js')
463
+
464
+ const port = 40000 + Math.floor(Math.random() * 10000)
465
+ baseUrl = `http://127.0.0.1:${port}`
466
+
467
+ server = Bun.serve({
468
+ port,
469
+ hostname: '127.0.0.1',
470
+
471
+ async fetch(req) {
472
+ const url = new URL(req.url)
473
+ const segments = url.pathname.split('/').filter(Boolean)
474
+
475
+ if (segments[0] === 'api' && segments[1] === 'teams') {
476
+ return handleTeamsApi(req, url, segments)
477
+ }
478
+
479
+ return new Response('Not Found', { status: 404 })
480
+ },
481
+ })
482
+ })
483
+
484
+ afterEach(async () => {
485
+ if (server) {
486
+ server.stop(true)
487
+ server = null
488
+ }
489
+ await cleanupTmpDir()
490
+ })
491
+
492
+ it('GET /api/teams should return empty list', async () => {
493
+ const res = await fetch(`${baseUrl}/api/teams`)
494
+ expect(res.status).toBe(200)
495
+
496
+ const body = (await res.json()) as { teams: unknown[] }
497
+ expect(body.teams).toEqual([])
498
+ })
499
+
500
+ it('GET /api/teams should list teams', async () => {
501
+ await writeTeamConfig('api-team', makeTeamConfig({ name: 'api-team' }))
502
+
503
+ const res = await fetch(`${baseUrl}/api/teams`)
504
+ expect(res.status).toBe(200)
505
+
506
+ const body = (await res.json()) as { teams: Array<{ name: string }> }
507
+ expect(body.teams).toHaveLength(1)
508
+ expect(body.teams[0]!.name).toBe('api-team')
509
+ })
510
+
511
+ it('GET /api/teams/:name should return team detail', async () => {
512
+ await writeTeamConfig(
513
+ 'detail',
514
+ makeTeamConfig({ name: 'detail', leadSessionId: 'leader-session-id' }),
515
+ )
516
+
517
+ const res = await fetch(`${baseUrl}/api/teams/detail`)
518
+ expect(res.status).toBe(200)
519
+
520
+ const body = (await res.json()) as {
521
+ name: string
522
+ leadAgentId: string
523
+ leadSessionId?: string
524
+ members: Array<{ agentId: string }>
525
+ }
526
+ expect(body.name).toBe('detail')
527
+ expect(body.leadAgentId).toBe('agent-lead')
528
+ expect(body.leadSessionId).toBe('leader-session-id')
529
+ expect(body.members).toHaveLength(2)
530
+ })
531
+
532
+ it('GET /api/teams/:name should 404 for unknown team', async () => {
533
+ const res = await fetch(`${baseUrl}/api/teams/nonexistent`)
534
+ expect(res.status).toBe(404)
535
+ })
536
+
537
+ it('GET /api/teams/:name/members/:id/transcript should return messages', async () => {
538
+ await writeTeamConfig('t-team', makeTeamConfig({ name: 't-team' }))
539
+
540
+ await writeTranscriptFile('-tmp-project', 'session-lead-001', [
541
+ {
542
+ type: 'user',
543
+ uuid: 'u1',
544
+ message: { role: 'user', content: 'Hello' },
545
+ timestamp: '2026-01-01T00:01:00.000Z',
546
+ },
547
+ ])
548
+
549
+ const res = await fetch(
550
+ `${baseUrl}/api/teams/t-team/members/agent-lead/transcript`,
551
+ )
552
+ expect(res.status).toBe(200)
553
+
554
+ const body = (await res.json()) as {
555
+ messages: Array<{ id: string; type: string }>
556
+ }
557
+ expect(body.messages).toHaveLength(1)
558
+ expect(body.messages[0]!.type).toBe('user')
559
+ })
560
+
561
+ it('GET /api/teams/:name/members/:id/transcript should 404 for unknown member', async () => {
562
+ await writeTeamConfig('t2-team', makeTeamConfig({ name: 't2-team' }))
563
+
564
+ const res = await fetch(
565
+ `${baseUrl}/api/teams/t2-team/members/unknown-agent/transcript`,
566
+ )
567
+ expect(res.status).toBe(404)
568
+ })
569
+
570
+ it('POST /api/teams/:name/members/:id/messages should enqueue a mailbox message', async () => {
571
+ await writeTeamConfig('send-team', makeTeamConfig({ name: 'send-team' }))
572
+
573
+ const res = await fetch(
574
+ `${baseUrl}/api/teams/send-team/members/agent-worker/messages`,
575
+ {
576
+ method: 'POST',
577
+ headers: { 'Content-Type': 'application/json' },
578
+ body: JSON.stringify({ content: 'Please continue with the failing test' }),
579
+ },
580
+ )
581
+
582
+ expect(res.status).toBe(200)
583
+ const body = (await res.json()) as { ok: boolean }
584
+ expect(body.ok).toBe(true)
585
+
586
+ const rawInbox = await fs.readFile(
587
+ path.join(tmpDir, 'teams', 'send-team', 'inboxes', 'Worker-Agent.json'),
588
+ 'utf-8',
589
+ )
590
+ const inbox = JSON.parse(rawInbox) as Array<{ text: string }>
591
+ expect(inbox.at(-1)?.text).toBe('Please continue with the failing test')
592
+ })
593
+
594
+ it('DELETE /api/teams/:name should delete team', async () => {
595
+ const config = makeTeamConfig({ name: 'del-team' })
596
+ for (const member of (config as { members: Array<Record<string, unknown>> }).members) {
597
+ member.isActive = false
598
+ }
599
+ await writeTeamConfig('del-team', config)
600
+
601
+ const res = await fetch(`${baseUrl}/api/teams/del-team`, {
602
+ method: 'DELETE',
603
+ })
604
+ expect(res.status).toBe(200)
605
+
606
+ const body = (await res.json()) as { ok: boolean }
607
+ expect(body.ok).toBe(true)
608
+
609
+ // Verify it's gone
610
+ const res2 = await fetch(`${baseUrl}/api/teams/del-team`)
611
+ expect(res2.status).toBe(404)
612
+ })
613
+
614
+ it('DELETE /api/teams/:name should 409 when team has active members', async () => {
615
+ await writeTeamConfig('active', makeTeamConfig({ name: 'active' }))
616
+
617
+ const res = await fetch(`${baseUrl}/api/teams/active`, {
618
+ method: 'DELETE',
619
+ })
620
+ expect(res.status).toBe(409)
621
+ })
622
+
623
+ it('POST /api/teams should return 405', async () => {
624
+ const res = await fetch(`${baseUrl}/api/teams`, { method: 'POST' })
625
+ expect(res.status).toBe(405)
626
+ })
627
+ })
@@ -0,0 +1,27 @@
1
+ //@C:ID=M.CT.corsTest;K=M;V=1.0;P=Import test dependencies;D=API;M=CORS;S=Testing
2
+ import { describe, expect, it } from 'bun:test'
3
+ import { corsHeaders } from './cors'
4
+
5
+ //@C:ID=F.CT.testCorsHeaders;K=F;V=1.0;P=Test CORS header generation functionality;D=API;M=CORS;S=Testing;In=void;Out=void
6
+ describe('corsHeaders', () => {
7
+ console.log("F.CT.testCorsHeaders");
8
+
9
+ ///@C:CT.TestLocalhostOrigins
10
+ it('allows localhost browser origins', () => {
11
+ expect(corsHeaders('http://127.0.0.1:1420')['Access-Control-Allow-Origin']).toBe('http://127.0.0.1:1420')
12
+ expect(corsHeaders('http://localhost:3000')['Access-Control-Allow-Origin']).toBe('http://localhost:3000')
13
+ })
14
+
15
+ ///@C:CT.TestTauriOrigins
16
+ it('allows tauri webview origins used in production builds', () => {
17
+ expect(corsHeaders('http://tauri.localhost')['Access-Control-Allow-Origin']).toBe('http://tauri.localhost')
18
+ expect(corsHeaders('https://tauri.localhost')['Access-Control-Allow-Origin']).toBe('https://tauri.localhost')
19
+ expect(corsHeaders('tauri://localhost')['Access-Control-Allow-Origin']).toBe('tauri://localhost')
20
+ })
21
+
22
+ ///@C:CT.TestFallbackOrigins
23
+ it('falls back for unknown origins', () => {
24
+ expect(corsHeaders('https://example.com')['Access-Control-Allow-Origin']).toBe('http://localhost:3000')
25
+ expect(corsHeaders(null)['Access-Control-Allow-Origin']).toBe('http://localhost:3000')
26
+ })
27
+ })