File size: 11,591 Bytes
1f21206
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/**
 * E2E Test — 完整流程测试
 *
 * 启动真实服务器,模拟 UI 前端的完整操作流程。
 */

import { describe, it, expect, beforeAll, afterAll } from 'bun:test'
import * as fs from 'fs/promises'
import * as path from 'path'
import * as os from 'os'

let server: ReturnType<typeof Bun.serve>
let baseUrl: string
let tmpDir: string

// Use dynamic import to avoid bundling issues
async function startTestServer() {
  tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'claude-e2e-'))
  process.env.CLAUDE_CONFIG_DIR = tmpDir

  // Create required directories
  await fs.mkdir(path.join(tmpDir, 'projects'), { recursive: true })

  const { startServer } = await import('../../index.js')
  const port = 13456 + Math.floor(Math.random() * 1000)
  server = startServer(port, '127.0.0.1')
  baseUrl = `http://127.0.0.1:${port}`
}

async function api(method: string, path: string, body?: unknown): Promise<{ status: number; data: any }> {
  const res = await fetch(`${baseUrl}${path}`, {
    method,
    headers: { 'Content-Type': 'application/json' },
    body: body ? JSON.stringify(body) : undefined,
  })
  const data = await res.json().catch(() => null)
  return { status: res.status, data }
}

describe('E2E: Full Flow', () => {
  beforeAll(async () => {
    await startTestServer()
  })

  afterAll(async () => {
    server?.stop()
    await fs.rm(tmpDir, { recursive: true, force: true })
  })

  // =============================================
  // 1. Health & Status
  // =============================================

  it('should return healthy status', async () => {
    const res = await fetch(`${baseUrl}/health`)
    const data = await res.json()
    expect(data.status).toBe('ok')
  })

  it('should return server status', async () => {
    const { data } = await api('GET', '/api/status')
    expect(data.status).toBe('ok')
    expect(data.version).toBeDefined()
  })

  it('should return diagnostics', async () => {
    const { data } = await api('GET', '/api/status/diagnostics')
    expect(data.platform).toBe('darwin')
    expect(data.configDir).toBe(tmpDir)
  })

  // =============================================
  // 2. Sessions CRUD
  // =============================================

  let sessionId: string

  it('should start with empty session list', async () => {
    const { data } = await api('GET', '/api/sessions')
    expect(data.sessions).toEqual([])
    expect(data.total).toBe(0)
  })

  it('should create a new session', async () => {
    const { status, data } = await api('POST', '/api/sessions', { workDir: tmpDir })
    expect(status).toBe(201)
    expect(data.sessionId).toBeDefined()
    expect(data.sessionId).toMatch(/^[0-9a-f-]{36}$/)
    sessionId = data.sessionId
  })

  it('should list the created session', async () => {
    const { data } = await api('GET', '/api/sessions')
    expect(data.sessions.length).toBe(1)
    expect(data.sessions[0].id).toBe(sessionId)
  })

  it('should get session detail', async () => {
    const { status, data } = await api('GET', `/api/sessions/${sessionId}`)
    expect(status).toBe(200)
    expect(data.id).toBe(sessionId)
  })

  it('should rename session', async () => {
    const { status } = await api('PATCH', `/api/sessions/${sessionId}`, { title: 'My Test Session' })
    expect(status).toBe(200)

    const { data } = await api('GET', `/api/sessions/${sessionId}`)
    expect(data.title).toBe('My Test Session')
  })

  it('should get session messages', async () => {
    const { status, data } = await api('GET', `/api/sessions/${sessionId}/messages`)
    expect(status).toBe(200)
    expect(Array.isArray(data.messages)).toBe(true)
  })

  it('should delete session', async () => {
    const { status } = await api('DELETE', `/api/sessions/${sessionId}`)
    expect(status).toBe(200)

    const { data } = await api('GET', '/api/sessions')
    expect(data.sessions.length).toBe(0)
  })

  // =============================================
  // 3. Settings
  // =============================================

  it('should get empty settings initially', async () => {
    const { data } = await api('GET', '/api/settings/user')
    expect(data).toEqual({})
  })

  it('should update and read user settings', async () => {
    await api('PUT', '/api/settings/user', { theme: 'dark', model: 'claude-sonnet-4-6' })

    const { data } = await api('GET', '/api/settings/user')
    expect(data.theme).toBe('dark')
    expect(data.model).toBe('claude-sonnet-4-6')
  })

  it('should get and set permission mode', async () => {
    await api('PUT', '/api/permissions/mode', { mode: 'plan' })

    const { data } = await api('GET', '/api/permissions/mode')
    expect(data.mode).toBe('plan')
  })

  it('should reject invalid permission mode', async () => {
    const { status } = await api('PUT', '/api/permissions/mode', { mode: 'invalid' })
    expect(status).toBe(400)
  })

  // =============================================
  // 4. Models
  // =============================================

  it('should list available models', async () => {
    const { data } = await api('GET', '/api/models')
    expect(data.models.length).toBe(4)
    expect(data.models[0].name).toBe('Opus 4.7')
  })

  it('should switch model', async () => {
    await api('PUT', '/api/models/current', { modelId: 'claude-haiku-4-5' })

    const { data } = await api('GET', '/api/models/current')
    expect(data.model.id).toBe('claude-haiku-4-5')
  })

  it('should get and set effort level', async () => {
    await api('PUT', '/api/effort', { level: 'high' })

    const { data } = await api('GET', '/api/effort')
    expect(data.level).toBe('high')
  })

  // =============================================
  // 5. Scheduled Tasks
  // =============================================

  let taskId: string

  it('should start with empty task list', async () => {
    const { data } = await api('GET', '/api/scheduled-tasks')
    expect(data.tasks).toEqual([])
  })

  it('should create a scheduled task', async () => {
    const { status, data } = await api('POST', '/api/scheduled-tasks', {
      cron: '0 9 * * *',
      prompt: 'Review commits from last 24h',
      recurring: true,
      name: 'daily-review',
      description: 'Daily code review',
    })
    expect(status).toBe(201)
    expect(data.task.id).toBeDefined()
    expect(data.task.cron).toBe('0 9 * * *')
    taskId = data.task.id
  })

  it('should list the created task', async () => {
    const { data } = await api('GET', '/api/scheduled-tasks')
    expect(data.tasks.length).toBe(1)
    expect(data.tasks[0].id).toBe(taskId)
  })

  it('should update a task', async () => {
    const { status, data } = await api('PUT', `/api/scheduled-tasks/${taskId}`, {
      cron: '0 10 * * 1-5',
    })
    expect(status).toBe(200)
    expect(data.task.cron).toBe('0 10 * * 1-5')
  })

  it('should delete a task', async () => {
    const { status } = await api('DELETE', `/api/scheduled-tasks/${taskId}`)
    expect([200, 204]).toContain(status)

    const { data } = await api('GET', '/api/scheduled-tasks')
    expect(data.tasks).toEqual([])
  })

  // =============================================
  // 6. Search
  // =============================================

  it('should search workspace', async () => {
    // Create a test file to search
    await fs.writeFile(path.join(tmpDir, 'test-search.txt'), 'Hello World\nFoo Bar Baz\n')

    const { status, data } = await api('POST', '/api/search', {
      query: 'Hello',
      cwd: tmpDir,
    })
    expect(status).toBe(200)
    expect(data.results.length).toBeGreaterThan(0)
    expect(data.results[0].text).toContain('Hello')
  })

  // =============================================
  // 7. Agents
  // =============================================

  it('should start with shared active/all agent payload', async () => {
    const { data } = await api('GET', '/api/agents')
    expect(Array.isArray(data.activeAgents)).toBe(true)
    expect(Array.isArray(data.allAgents)).toBe(true)
    expect(data.activeAgents.length).toBeGreaterThan(0)
    expect(data.activeAgents.some((agent: any) => agent.source === 'built-in')).toBe(true)
  })

  it('should create an agent', async () => {
    const { status } = await api('POST', '/api/agents', {
      name: 'test-agent',
      description: 'A test agent',
      model: 'claude-sonnet-4-6',
    })
    expect(status).toBe(201)
  })

  it('should expose shared active/all agent payload independent of CRUD storage', async () => {
    const { data } = await api('GET', '/api/agents')
    expect(Array.isArray(data.activeAgents)).toBe(true)
    expect(Array.isArray(data.allAgents)).toBe(true)
    expect(data.activeAgents.length).toBeGreaterThan(0)
    expect(data.activeAgents.some((agent: any) => agent.source === 'built-in')).toBe(true)
    expect(data.activeAgents.some((agent: any) => agent.agentType === 'test-agent')).toBe(false)
  })

  it('should delete an agent', async () => {
    const { status } = await api('DELETE', '/api/agents/test-agent')
    expect([200, 204]).toContain(status)
  })

  // =============================================
  // 8. WebSocket Chat
  // =============================================

  it('should connect via WebSocket', async () => {
    const wsUrl = baseUrl.replace('http://', 'ws://') + '/ws/test-ws-session'

    const messages: any[] = []
    const ws = new WebSocket(wsUrl)

    await new Promise<void>((resolve, reject) => {
      ws.onopen = () => {
        // Should receive connected message
      }
      ws.onmessage = (event) => {
        const msg = JSON.parse(event.data as string)
        messages.push(msg)
        if (msg.type === 'connected') {
          // Send a test message
          ws.send(JSON.stringify({ type: 'user_message', content: 'Hello' }))
        }
        if (msg.type === 'status' && msg.state === 'idle' && messages.length > 2) {
          ws.close()
          resolve()
        }
      }
      ws.onerror = reject
      setTimeout(() => {
        ws.close()
        resolve()
      }, 3000)
    })

    expect(messages[0].type).toBe('connected')
    expect(messages[0].sessionId).toBe('test-ws-session')
  })

  // =============================================
  // 9. Conversation Status
  // =============================================

  it('should get chat status', async () => {
    // Create a session first
    const { data: created } = await api('POST', '/api/sessions', { workDir: tmpDir })

    const { status, data } = await api('GET', `/api/sessions/${created.sessionId}/chat/status`)
    expect(status).toBe(200)
    expect(data.state).toBe('idle')

    // Cleanup
    await api('DELETE', `/api/sessions/${created.sessionId}`)
  })

  // =============================================
  // 10. CORS
  // =============================================

  it('should handle CORS preflight', async () => {
    const res = await fetch(`${baseUrl}/api/status`, {
      method: 'OPTIONS',
      headers: { 'Origin': 'http://localhost:3000' },
    })
    expect(res.status).toBe(204)
    expect(res.headers.get('Access-Control-Allow-Origin')).toBe('http://localhost:3000')
  })

  // =============================================
  // 11. Error Handling
  // =============================================

  it('should return 404 for unknown API', async () => {
    const { status } = await api('GET', '/api/nonexistent')
    expect(status).toBe(404)
  })

  it('should return 404 for unknown session', async () => {
    const { status } = await api('GET', '/api/sessions/00000000-0000-0000-0000-000000000000')
    expect(status).toBe(404)
  })
})