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.
- package/adapters/common/__tests__/chat-queue.test.ts +61 -0
- package/adapters/common/__tests__/format.test.ts +148 -0
- package/adapters/common/__tests__/http-client.test.ts +105 -0
- package/adapters/common/__tests__/message-buffer.test.ts +84 -0
- package/adapters/common/__tests__/message-dedup.test.ts +57 -0
- package/adapters/common/__tests__/session-store.test.ts +62 -0
- package/adapters/common/__tests__/ws-bridge.test.ts +177 -0
- package/adapters/common/attachment/__tests__/attachment-limits.test.ts +52 -0
- package/adapters/common/attachment/__tests__/attachment-store.test.ts +108 -0
- package/adapters/common/attachment/__tests__/image-block-watcher.test.ts +115 -0
- package/adapters/feishu/__tests__/card-errors.test.ts +194 -0
- package/adapters/feishu/__tests__/cardkit.test.ts +295 -0
- package/adapters/feishu/__tests__/extract-payload.test.ts +77 -0
- package/adapters/feishu/__tests__/feishu.test.ts +907 -0
- package/adapters/feishu/__tests__/flush-controller.test.ts +290 -0
- package/adapters/feishu/__tests__/markdown-style.test.ts +353 -0
- package/adapters/feishu/__tests__/media.test.ts +120 -0
- package/adapters/feishu/__tests__/streaming-card.test.ts +914 -0
- package/adapters/telegram/__tests__/media.test.ts +86 -0
- package/adapters/telegram/__tests__/telegram.test.ts +115 -0
- package/adapters/tsconfig.json +18 -0
- package/bunfig.toml +1 -0
- package/package.json +1 -1
- package/preload.ts +30 -0
- package/scripts/count-app-loc.ts +256 -0
- package/scripts/release.ts +130 -0
- package/src/server/__tests__/conversation-service.test.ts +173 -0
- package/src/server/__tests__/conversations.test.ts +458 -0
- package/src/server/__tests__/cron-scheduler.test.ts +575 -0
- package/src/server/__tests__/e2e/business-flow.test.ts +841 -0
- package/src/server/__tests__/e2e/full-flow.test.ts +357 -0
- package/src/server/__tests__/fixtures/mock-sdk-cli.ts +123 -0
- package/src/server/__tests__/haha-oauth-api.test.ts +146 -0
- package/src/server/__tests__/haha-oauth-service.test.ts +185 -0
- package/src/server/__tests__/providers-real.test.ts +244 -0
- package/src/server/__tests__/providers.test.ts +579 -0
- package/src/server/__tests__/proxy-streaming.test.ts +317 -0
- package/src/server/__tests__/proxy-transform.test.ts +469 -0
- package/src/server/__tests__/real-llm-test.ts +526 -0
- package/src/server/__tests__/scheduled-tasks.test.ts +371 -0
- package/src/server/__tests__/sessions.test.ts +786 -0
- package/src/server/__tests__/settings.test.ts +376 -0
- package/src/server/__tests__/skills.test.ts +125 -0
- package/src/server/__tests__/tasks.test.ts +171 -0
- package/src/server/__tests__/team-watcher.test.ts +400 -0
- package/src/server/__tests__/teams.test.ts +627 -0
- package/src/server/middleware/cors.test.ts +27 -0
- package/src/utils/__tests__/cronFrequency.test.ts +153 -0
- package/src/utils/__tests__/cronTasks.test.ts +204 -0
- package/src/utils/computerUse/permissions.test.ts +44 -0
- package/stubs/ant-claude-for-chrome-mcp.ts +24 -0
- package/stubs/color-diff-napi.ts +45 -0
- package/tsconfig.json +24 -0
|
@@ -0,0 +1,357 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* E2E Test — 完整流程测试
|
|
3
|
+
*
|
|
4
|
+
* 启动真实服务器,模拟 UI 前端的完整操作流程。
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { describe, it, expect, beforeAll, afterAll } from 'bun:test'
|
|
8
|
+
import * as fs from 'fs/promises'
|
|
9
|
+
import * as path from 'path'
|
|
10
|
+
import * as os from 'os'
|
|
11
|
+
|
|
12
|
+
let server: ReturnType<typeof Bun.serve>
|
|
13
|
+
let baseUrl: string
|
|
14
|
+
let tmpDir: string
|
|
15
|
+
|
|
16
|
+
// Use dynamic import to avoid bundling issues
|
|
17
|
+
async function startTestServer() {
|
|
18
|
+
tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'claude-e2e-'))
|
|
19
|
+
process.env.CLAUDE_CONFIG_DIR = tmpDir
|
|
20
|
+
|
|
21
|
+
// Create required directories
|
|
22
|
+
await fs.mkdir(path.join(tmpDir, 'projects'), { recursive: true })
|
|
23
|
+
|
|
24
|
+
const { startServer } = await import('../../index.js')
|
|
25
|
+
const port = 13456 + Math.floor(Math.random() * 1000)
|
|
26
|
+
server = startServer(port, '127.0.0.1')
|
|
27
|
+
baseUrl = `http://127.0.0.1:${port}`
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async function api(method: string, path: string, body?: unknown): Promise<{ status: number; data: any }> {
|
|
31
|
+
const res = await fetch(`${baseUrl}${path}`, {
|
|
32
|
+
method,
|
|
33
|
+
headers: { 'Content-Type': 'application/json' },
|
|
34
|
+
body: body ? JSON.stringify(body) : undefined,
|
|
35
|
+
})
|
|
36
|
+
const data = await res.json().catch(() => null)
|
|
37
|
+
return { status: res.status, data }
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
describe('E2E: Full Flow', () => {
|
|
41
|
+
beforeAll(async () => {
|
|
42
|
+
await startTestServer()
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
afterAll(async () => {
|
|
46
|
+
server?.stop()
|
|
47
|
+
await fs.rm(tmpDir, { recursive: true, force: true })
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
// =============================================
|
|
51
|
+
// 1. Health & Status
|
|
52
|
+
// =============================================
|
|
53
|
+
|
|
54
|
+
it('should return healthy status', async () => {
|
|
55
|
+
const res = await fetch(`${baseUrl}/health`)
|
|
56
|
+
const data = await res.json()
|
|
57
|
+
expect(data.status).toBe('ok')
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
it('should return server status', async () => {
|
|
61
|
+
const { data } = await api('GET', '/api/status')
|
|
62
|
+
expect(data.status).toBe('ok')
|
|
63
|
+
expect(data.version).toBeDefined()
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
it('should return diagnostics', async () => {
|
|
67
|
+
const { data } = await api('GET', '/api/status/diagnostics')
|
|
68
|
+
expect(data.platform).toBe('darwin')
|
|
69
|
+
expect(data.configDir).toBe(tmpDir)
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
// =============================================
|
|
73
|
+
// 2. Sessions CRUD
|
|
74
|
+
// =============================================
|
|
75
|
+
|
|
76
|
+
let sessionId: string
|
|
77
|
+
|
|
78
|
+
it('should start with empty session list', async () => {
|
|
79
|
+
const { data } = await api('GET', '/api/sessions')
|
|
80
|
+
expect(data.sessions).toEqual([])
|
|
81
|
+
expect(data.total).toBe(0)
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
it('should create a new session', async () => {
|
|
85
|
+
const { status, data } = await api('POST', '/api/sessions', { workDir: tmpDir })
|
|
86
|
+
expect(status).toBe(201)
|
|
87
|
+
expect(data.sessionId).toBeDefined()
|
|
88
|
+
expect(data.sessionId).toMatch(/^[0-9a-f-]{36}$/)
|
|
89
|
+
sessionId = data.sessionId
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
it('should list the created session', async () => {
|
|
93
|
+
const { data } = await api('GET', '/api/sessions')
|
|
94
|
+
expect(data.sessions.length).toBe(1)
|
|
95
|
+
expect(data.sessions[0].id).toBe(sessionId)
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
it('should get session detail', async () => {
|
|
99
|
+
const { status, data } = await api('GET', `/api/sessions/${sessionId}`)
|
|
100
|
+
expect(status).toBe(200)
|
|
101
|
+
expect(data.id).toBe(sessionId)
|
|
102
|
+
})
|
|
103
|
+
|
|
104
|
+
it('should rename session', async () => {
|
|
105
|
+
const { status } = await api('PATCH', `/api/sessions/${sessionId}`, { title: 'My Test Session' })
|
|
106
|
+
expect(status).toBe(200)
|
|
107
|
+
|
|
108
|
+
const { data } = await api('GET', `/api/sessions/${sessionId}`)
|
|
109
|
+
expect(data.title).toBe('My Test Session')
|
|
110
|
+
})
|
|
111
|
+
|
|
112
|
+
it('should get session messages', async () => {
|
|
113
|
+
const { status, data } = await api('GET', `/api/sessions/${sessionId}/messages`)
|
|
114
|
+
expect(status).toBe(200)
|
|
115
|
+
expect(Array.isArray(data.messages)).toBe(true)
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
it('should delete session', async () => {
|
|
119
|
+
const { status } = await api('DELETE', `/api/sessions/${sessionId}`)
|
|
120
|
+
expect(status).toBe(200)
|
|
121
|
+
|
|
122
|
+
const { data } = await api('GET', '/api/sessions')
|
|
123
|
+
expect(data.sessions.length).toBe(0)
|
|
124
|
+
})
|
|
125
|
+
|
|
126
|
+
// =============================================
|
|
127
|
+
// 3. Settings
|
|
128
|
+
// =============================================
|
|
129
|
+
|
|
130
|
+
it('should get empty settings initially', async () => {
|
|
131
|
+
const { data } = await api('GET', '/api/settings/user')
|
|
132
|
+
expect(data).toEqual({})
|
|
133
|
+
})
|
|
134
|
+
|
|
135
|
+
it('should update and read user settings', async () => {
|
|
136
|
+
await api('PUT', '/api/settings/user', { theme: 'dark', model: 'claude-sonnet-4-6' })
|
|
137
|
+
|
|
138
|
+
const { data } = await api('GET', '/api/settings/user')
|
|
139
|
+
expect(data.theme).toBe('dark')
|
|
140
|
+
expect(data.model).toBe('claude-sonnet-4-6')
|
|
141
|
+
})
|
|
142
|
+
|
|
143
|
+
it('should get and set permission mode', async () => {
|
|
144
|
+
await api('PUT', '/api/permissions/mode', { mode: 'plan' })
|
|
145
|
+
|
|
146
|
+
const { data } = await api('GET', '/api/permissions/mode')
|
|
147
|
+
expect(data.mode).toBe('plan')
|
|
148
|
+
})
|
|
149
|
+
|
|
150
|
+
it('should reject invalid permission mode', async () => {
|
|
151
|
+
const { status } = await api('PUT', '/api/permissions/mode', { mode: 'invalid' })
|
|
152
|
+
expect(status).toBe(400)
|
|
153
|
+
})
|
|
154
|
+
|
|
155
|
+
// =============================================
|
|
156
|
+
// 4. Models
|
|
157
|
+
// =============================================
|
|
158
|
+
|
|
159
|
+
it('should list available models', async () => {
|
|
160
|
+
const { data } = await api('GET', '/api/models')
|
|
161
|
+
expect(data.models.length).toBe(4)
|
|
162
|
+
expect(data.models[0].name).toBe('Opus 4.7')
|
|
163
|
+
})
|
|
164
|
+
|
|
165
|
+
it('should switch model', async () => {
|
|
166
|
+
await api('PUT', '/api/models/current', { modelId: 'claude-haiku-4-5' })
|
|
167
|
+
|
|
168
|
+
const { data } = await api('GET', '/api/models/current')
|
|
169
|
+
expect(data.model.id).toBe('claude-haiku-4-5')
|
|
170
|
+
})
|
|
171
|
+
|
|
172
|
+
it('should get and set effort level', async () => {
|
|
173
|
+
await api('PUT', '/api/effort', { level: 'high' })
|
|
174
|
+
|
|
175
|
+
const { data } = await api('GET', '/api/effort')
|
|
176
|
+
expect(data.level).toBe('high')
|
|
177
|
+
})
|
|
178
|
+
|
|
179
|
+
// =============================================
|
|
180
|
+
// 5. Scheduled Tasks
|
|
181
|
+
// =============================================
|
|
182
|
+
|
|
183
|
+
let taskId: string
|
|
184
|
+
|
|
185
|
+
it('should start with empty task list', async () => {
|
|
186
|
+
const { data } = await api('GET', '/api/scheduled-tasks')
|
|
187
|
+
expect(data.tasks).toEqual([])
|
|
188
|
+
})
|
|
189
|
+
|
|
190
|
+
it('should create a scheduled task', async () => {
|
|
191
|
+
const { status, data } = await api('POST', '/api/scheduled-tasks', {
|
|
192
|
+
cron: '0 9 * * *',
|
|
193
|
+
prompt: 'Review commits from last 24h',
|
|
194
|
+
recurring: true,
|
|
195
|
+
name: 'daily-review',
|
|
196
|
+
description: 'Daily code review',
|
|
197
|
+
})
|
|
198
|
+
expect(status).toBe(201)
|
|
199
|
+
expect(data.task.id).toBeDefined()
|
|
200
|
+
expect(data.task.cron).toBe('0 9 * * *')
|
|
201
|
+
taskId = data.task.id
|
|
202
|
+
})
|
|
203
|
+
|
|
204
|
+
it('should list the created task', async () => {
|
|
205
|
+
const { data } = await api('GET', '/api/scheduled-tasks')
|
|
206
|
+
expect(data.tasks.length).toBe(1)
|
|
207
|
+
expect(data.tasks[0].id).toBe(taskId)
|
|
208
|
+
})
|
|
209
|
+
|
|
210
|
+
it('should update a task', async () => {
|
|
211
|
+
const { status, data } = await api('PUT', `/api/scheduled-tasks/${taskId}`, {
|
|
212
|
+
cron: '0 10 * * 1-5',
|
|
213
|
+
})
|
|
214
|
+
expect(status).toBe(200)
|
|
215
|
+
expect(data.task.cron).toBe('0 10 * * 1-5')
|
|
216
|
+
})
|
|
217
|
+
|
|
218
|
+
it('should delete a task', async () => {
|
|
219
|
+
const { status } = await api('DELETE', `/api/scheduled-tasks/${taskId}`)
|
|
220
|
+
expect([200, 204]).toContain(status)
|
|
221
|
+
|
|
222
|
+
const { data } = await api('GET', '/api/scheduled-tasks')
|
|
223
|
+
expect(data.tasks).toEqual([])
|
|
224
|
+
})
|
|
225
|
+
|
|
226
|
+
// =============================================
|
|
227
|
+
// 6. Search
|
|
228
|
+
// =============================================
|
|
229
|
+
|
|
230
|
+
it('should search workspace', async () => {
|
|
231
|
+
// Create a test file to search
|
|
232
|
+
await fs.writeFile(path.join(tmpDir, 'test-search.txt'), 'Hello World\nFoo Bar Baz\n')
|
|
233
|
+
|
|
234
|
+
const { status, data } = await api('POST', '/api/search', {
|
|
235
|
+
query: 'Hello',
|
|
236
|
+
cwd: tmpDir,
|
|
237
|
+
})
|
|
238
|
+
expect(status).toBe(200)
|
|
239
|
+
expect(data.results.length).toBeGreaterThan(0)
|
|
240
|
+
expect(data.results[0].text).toContain('Hello')
|
|
241
|
+
})
|
|
242
|
+
|
|
243
|
+
// =============================================
|
|
244
|
+
// 7. Agents
|
|
245
|
+
// =============================================
|
|
246
|
+
|
|
247
|
+
it('should start with shared active/all agent payload', async () => {
|
|
248
|
+
const { data } = await api('GET', '/api/agents')
|
|
249
|
+
expect(Array.isArray(data.activeAgents)).toBe(true)
|
|
250
|
+
expect(Array.isArray(data.allAgents)).toBe(true)
|
|
251
|
+
expect(data.activeAgents.length).toBeGreaterThan(0)
|
|
252
|
+
expect(data.activeAgents.some((agent: any) => agent.source === 'built-in')).toBe(true)
|
|
253
|
+
})
|
|
254
|
+
|
|
255
|
+
it('should create an agent', async () => {
|
|
256
|
+
const { status } = await api('POST', '/api/agents', {
|
|
257
|
+
name: 'test-agent',
|
|
258
|
+
description: 'A test agent',
|
|
259
|
+
model: 'claude-sonnet-4-6',
|
|
260
|
+
})
|
|
261
|
+
expect(status).toBe(201)
|
|
262
|
+
})
|
|
263
|
+
|
|
264
|
+
it('should expose shared active/all agent payload independent of CRUD storage', async () => {
|
|
265
|
+
const { data } = await api('GET', '/api/agents')
|
|
266
|
+
expect(Array.isArray(data.activeAgents)).toBe(true)
|
|
267
|
+
expect(Array.isArray(data.allAgents)).toBe(true)
|
|
268
|
+
expect(data.activeAgents.length).toBeGreaterThan(0)
|
|
269
|
+
expect(data.activeAgents.some((agent: any) => agent.source === 'built-in')).toBe(true)
|
|
270
|
+
expect(data.activeAgents.some((agent: any) => agent.agentType === 'test-agent')).toBe(false)
|
|
271
|
+
})
|
|
272
|
+
|
|
273
|
+
it('should delete an agent', async () => {
|
|
274
|
+
const { status } = await api('DELETE', '/api/agents/test-agent')
|
|
275
|
+
expect([200, 204]).toContain(status)
|
|
276
|
+
})
|
|
277
|
+
|
|
278
|
+
// =============================================
|
|
279
|
+
// 8. WebSocket Chat
|
|
280
|
+
// =============================================
|
|
281
|
+
|
|
282
|
+
it('should connect via WebSocket', async () => {
|
|
283
|
+
const wsUrl = baseUrl.replace('http://', 'ws://') + '/ws/test-ws-session'
|
|
284
|
+
|
|
285
|
+
const messages: any[] = []
|
|
286
|
+
const ws = new WebSocket(wsUrl)
|
|
287
|
+
|
|
288
|
+
await new Promise<void>((resolve, reject) => {
|
|
289
|
+
ws.onopen = () => {
|
|
290
|
+
// Should receive connected message
|
|
291
|
+
}
|
|
292
|
+
ws.onmessage = (event) => {
|
|
293
|
+
const msg = JSON.parse(event.data as string)
|
|
294
|
+
messages.push(msg)
|
|
295
|
+
if (msg.type === 'connected') {
|
|
296
|
+
// Send a test message
|
|
297
|
+
ws.send(JSON.stringify({ type: 'user_message', content: 'Hello' }))
|
|
298
|
+
}
|
|
299
|
+
if (msg.type === 'status' && msg.state === 'idle' && messages.length > 2) {
|
|
300
|
+
ws.close()
|
|
301
|
+
resolve()
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
ws.onerror = reject
|
|
305
|
+
setTimeout(() => {
|
|
306
|
+
ws.close()
|
|
307
|
+
resolve()
|
|
308
|
+
}, 3000)
|
|
309
|
+
})
|
|
310
|
+
|
|
311
|
+
expect(messages[0].type).toBe('connected')
|
|
312
|
+
expect(messages[0].sessionId).toBe('test-ws-session')
|
|
313
|
+
})
|
|
314
|
+
|
|
315
|
+
// =============================================
|
|
316
|
+
// 9. Conversation Status
|
|
317
|
+
// =============================================
|
|
318
|
+
|
|
319
|
+
it('should get chat status', async () => {
|
|
320
|
+
// Create a session first
|
|
321
|
+
const { data: created } = await api('POST', '/api/sessions', { workDir: tmpDir })
|
|
322
|
+
|
|
323
|
+
const { status, data } = await api('GET', `/api/sessions/${created.sessionId}/chat/status`)
|
|
324
|
+
expect(status).toBe(200)
|
|
325
|
+
expect(data.state).toBe('idle')
|
|
326
|
+
|
|
327
|
+
// Cleanup
|
|
328
|
+
await api('DELETE', `/api/sessions/${created.sessionId}`)
|
|
329
|
+
})
|
|
330
|
+
|
|
331
|
+
// =============================================
|
|
332
|
+
// 10. CORS
|
|
333
|
+
// =============================================
|
|
334
|
+
|
|
335
|
+
it('should handle CORS preflight', async () => {
|
|
336
|
+
const res = await fetch(`${baseUrl}/api/status`, {
|
|
337
|
+
method: 'OPTIONS',
|
|
338
|
+
headers: { 'Origin': 'http://localhost:3000' },
|
|
339
|
+
})
|
|
340
|
+
expect(res.status).toBe(204)
|
|
341
|
+
expect(res.headers.get('Access-Control-Allow-Origin')).toBe('http://localhost:3000')
|
|
342
|
+
})
|
|
343
|
+
|
|
344
|
+
// =============================================
|
|
345
|
+
// 11. Error Handling
|
|
346
|
+
// =============================================
|
|
347
|
+
|
|
348
|
+
it('should return 404 for unknown API', async () => {
|
|
349
|
+
const { status } = await api('GET', '/api/nonexistent')
|
|
350
|
+
expect(status).toBe(404)
|
|
351
|
+
})
|
|
352
|
+
|
|
353
|
+
it('should return 404 for unknown session', async () => {
|
|
354
|
+
const { status } = await api('GET', '/api/sessions/00000000-0000-0000-0000-000000000000')
|
|
355
|
+
expect(status).toBe(404)
|
|
356
|
+
})
|
|
357
|
+
})
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
const args = process.argv.slice(2)
|
|
2
|
+
|
|
3
|
+
function getArg(name: string): string | undefined {
|
|
4
|
+
const index = args.indexOf(name)
|
|
5
|
+
return index >= 0 ? args[index + 1] : undefined
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
function emit(ws: WebSocket, payload: Record<string, unknown>) {
|
|
9
|
+
ws.send(JSON.stringify(payload) + '\n')
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function extractUserText(message: any): string {
|
|
13
|
+
const content = message?.message?.content
|
|
14
|
+
if (!Array.isArray(content)) return ''
|
|
15
|
+
return content
|
|
16
|
+
.filter((block: any) => block?.type === 'text' && typeof block.text === 'string')
|
|
17
|
+
.map((block: any) => block.text)
|
|
18
|
+
.join(' ')
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const sdkUrl = getArg('--sdk-url')
|
|
22
|
+
const sessionId = getArg('--session-id') || crypto.randomUUID()
|
|
23
|
+
const initMode = process.env.MOCK_SDK_INIT_MODE || 'on_open'
|
|
24
|
+
let initSent = false
|
|
25
|
+
|
|
26
|
+
if (!sdkUrl) {
|
|
27
|
+
console.error('Missing --sdk-url')
|
|
28
|
+
process.exit(1)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const ws = new WebSocket(sdkUrl)
|
|
32
|
+
|
|
33
|
+
function sendInit() {
|
|
34
|
+
if (initSent) return
|
|
35
|
+
initSent = true
|
|
36
|
+
emit(ws, {
|
|
37
|
+
type: 'system',
|
|
38
|
+
subtype: 'init',
|
|
39
|
+
model: 'mock-opus',
|
|
40
|
+
slash_commands: [{ name: 'help', description: 'Show help' }],
|
|
41
|
+
session_id: sessionId,
|
|
42
|
+
})
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
ws.addEventListener('open', () => {
|
|
46
|
+
if (initMode !== 'on_first_user') {
|
|
47
|
+
sendInit()
|
|
48
|
+
}
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
ws.addEventListener('message', (event) => {
|
|
52
|
+
const payload = typeof event.data === 'string' ? event.data : String(event.data)
|
|
53
|
+
const lines = payload.split('\n').map(line => line.trim()).filter(Boolean)
|
|
54
|
+
|
|
55
|
+
for (const line of lines) {
|
|
56
|
+
const parsed = JSON.parse(line)
|
|
57
|
+
|
|
58
|
+
if (parsed.type === 'user') {
|
|
59
|
+
sendInit()
|
|
60
|
+
const text = extractUserText(parsed)
|
|
61
|
+
emit(ws, {
|
|
62
|
+
type: 'stream_event',
|
|
63
|
+
event: { type: 'message_start' },
|
|
64
|
+
session_id: sessionId,
|
|
65
|
+
})
|
|
66
|
+
emit(ws, {
|
|
67
|
+
type: 'stream_event',
|
|
68
|
+
event: {
|
|
69
|
+
type: 'content_block_start',
|
|
70
|
+
index: 0,
|
|
71
|
+
content_block: { type: 'text', text: '' },
|
|
72
|
+
},
|
|
73
|
+
session_id: sessionId,
|
|
74
|
+
})
|
|
75
|
+
emit(ws, {
|
|
76
|
+
type: 'stream_event',
|
|
77
|
+
event: {
|
|
78
|
+
type: 'content_block_delta',
|
|
79
|
+
index: 0,
|
|
80
|
+
delta: { type: 'thinking_delta', thinking: 'Mock thinking...' },
|
|
81
|
+
},
|
|
82
|
+
session_id: sessionId,
|
|
83
|
+
})
|
|
84
|
+
emit(ws, {
|
|
85
|
+
type: 'stream_event',
|
|
86
|
+
event: {
|
|
87
|
+
type: 'content_block_delta',
|
|
88
|
+
index: 0,
|
|
89
|
+
delta: { type: 'text_delta', text: `Echo: ${text}` },
|
|
90
|
+
},
|
|
91
|
+
session_id: sessionId,
|
|
92
|
+
})
|
|
93
|
+
emit(ws, {
|
|
94
|
+
type: 'stream_event',
|
|
95
|
+
event: { type: 'content_block_stop', index: 0 },
|
|
96
|
+
session_id: sessionId,
|
|
97
|
+
})
|
|
98
|
+
emit(ws, {
|
|
99
|
+
type: 'result',
|
|
100
|
+
subtype: 'success',
|
|
101
|
+
is_error: false,
|
|
102
|
+
result: `Echo: ${text}`,
|
|
103
|
+
usage: { input_tokens: 3, output_tokens: 2 },
|
|
104
|
+
session_id: sessionId,
|
|
105
|
+
})
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if (parsed.type === 'control_request' && parsed.request?.subtype === 'interrupt') {
|
|
109
|
+
emit(ws, {
|
|
110
|
+
type: 'result',
|
|
111
|
+
subtype: 'success',
|
|
112
|
+
is_error: false,
|
|
113
|
+
result: 'Interrupted',
|
|
114
|
+
usage: { input_tokens: 0, output_tokens: 0 },
|
|
115
|
+
session_id: sessionId,
|
|
116
|
+
})
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
})
|
|
120
|
+
|
|
121
|
+
ws.addEventListener('close', () => {
|
|
122
|
+
process.exit(0)
|
|
123
|
+
})
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Integration tests for /api/haha-oauth/* endpoints.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { describe, test, expect, beforeEach, afterEach } from 'bun:test'
|
|
6
|
+
import * as fs from 'fs/promises'
|
|
7
|
+
import * as path from 'path'
|
|
8
|
+
import * as os from 'os'
|
|
9
|
+
import { handleHahaOAuthApi } from '../api/haha-oauth.js'
|
|
10
|
+
import { hahaOAuthService } from '../services/hahaOAuthService.js'
|
|
11
|
+
|
|
12
|
+
let tmpDir: string
|
|
13
|
+
let originalConfigDir: string | undefined
|
|
14
|
+
|
|
15
|
+
async function setup() {
|
|
16
|
+
tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'haha-oauth-api-test-'))
|
|
17
|
+
originalConfigDir = process.env.CLAUDE_CONFIG_DIR
|
|
18
|
+
process.env.CLAUDE_CONFIG_DIR = tmpDir
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
async function teardown() {
|
|
22
|
+
if (originalConfigDir === undefined) {
|
|
23
|
+
delete process.env.CLAUDE_CONFIG_DIR
|
|
24
|
+
} else {
|
|
25
|
+
process.env.CLAUDE_CONFIG_DIR = originalConfigDir
|
|
26
|
+
}
|
|
27
|
+
await fs.rm(tmpDir, { recursive: true, force: true })
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function buildReq(
|
|
31
|
+
method: string,
|
|
32
|
+
pathname: string,
|
|
33
|
+
body?: unknown,
|
|
34
|
+
): { req: Request; url: URL; segments: string[] } {
|
|
35
|
+
const url = new URL(`http://localhost:3456${pathname}`)
|
|
36
|
+
const req = new Request(url.toString(), {
|
|
37
|
+
method,
|
|
38
|
+
headers: body ? { 'Content-Type': 'application/json' } : undefined,
|
|
39
|
+
body: body ? JSON.stringify(body) : undefined,
|
|
40
|
+
})
|
|
41
|
+
const segments = url.pathname.split('/').filter(Boolean)
|
|
42
|
+
return { req, url, segments }
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
describe('POST /api/haha-oauth/start', () => {
|
|
46
|
+
beforeEach(setup)
|
|
47
|
+
afterEach(teardown)
|
|
48
|
+
|
|
49
|
+
test('returns authorize URL with PKCE challenge', async () => {
|
|
50
|
+
const { req, url, segments } = buildReq('POST', '/api/haha-oauth/start', {
|
|
51
|
+
serverPort: 54321,
|
|
52
|
+
})
|
|
53
|
+
const res = await handleHahaOAuthApi(req, url, segments)
|
|
54
|
+
expect(res.status).toBe(200)
|
|
55
|
+
const data = (await res.json()) as { authorizeUrl: string; state: string }
|
|
56
|
+
expect(data.authorizeUrl).toContain('code_challenge_method=S256')
|
|
57
|
+
expect(data.authorizeUrl).toContain(
|
|
58
|
+
encodeURIComponent('http://localhost:54321/callback'),
|
|
59
|
+
)
|
|
60
|
+
expect(data.state).toMatch(/^[A-Za-z0-9_-]+$/)
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
test('400 if serverPort missing', async () => {
|
|
64
|
+
const { req, url, segments } = buildReq('POST', '/api/haha-oauth/start', {})
|
|
65
|
+
const res = await handleHahaOAuthApi(req, url, segments)
|
|
66
|
+
expect(res.status).toBe(400)
|
|
67
|
+
const body = (await res.json()) as { error: string; message?: string }
|
|
68
|
+
expect(body.error).toBe('BAD_REQUEST')
|
|
69
|
+
})
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
describe('GET /api/haha-oauth/status', () => {
|
|
73
|
+
beforeEach(setup)
|
|
74
|
+
afterEach(teardown)
|
|
75
|
+
|
|
76
|
+
test('returns loggedIn=false when no token file', async () => {
|
|
77
|
+
const { req, url, segments } = buildReq('GET', '/api/haha-oauth/status')
|
|
78
|
+
const res = await handleHahaOAuthApi(req, url, segments)
|
|
79
|
+
expect(res.status).toBe(200)
|
|
80
|
+
const data = (await res.json()) as { loggedIn: boolean }
|
|
81
|
+
expect(data.loggedIn).toBe(false)
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
test('returns loggedIn=true + metadata when token saved', async () => {
|
|
85
|
+
await hahaOAuthService.saveTokens({
|
|
86
|
+
accessToken: 'sk-ant-oat01-xxx',
|
|
87
|
+
refreshToken: 'sk-ant-ort01-xxx',
|
|
88
|
+
expiresAt: Date.now() + 3600_000,
|
|
89
|
+
scopes: ['user:inference'],
|
|
90
|
+
subscriptionType: 'max',
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
const { req, url, segments } = buildReq('GET', '/api/haha-oauth/status')
|
|
94
|
+
const res = await handleHahaOAuthApi(req, url, segments)
|
|
95
|
+
expect(res.status).toBe(200)
|
|
96
|
+
const data = (await res.json()) as {
|
|
97
|
+
loggedIn: boolean
|
|
98
|
+
subscriptionType: string | null
|
|
99
|
+
scopes: string[]
|
|
100
|
+
}
|
|
101
|
+
expect(data.loggedIn).toBe(true)
|
|
102
|
+
expect(data.subscriptionType).toBe('max')
|
|
103
|
+
expect(data.scopes).toEqual(['user:inference'])
|
|
104
|
+
expect(JSON.stringify(data)).not.toContain('sk-ant-oat01')
|
|
105
|
+
expect(JSON.stringify(data)).not.toContain('sk-ant-ort01')
|
|
106
|
+
})
|
|
107
|
+
|
|
108
|
+
test('returns loggedIn=false when stored token is expired and refresh fails', async () => {
|
|
109
|
+
await hahaOAuthService.saveTokens({
|
|
110
|
+
accessToken: 'expired-token',
|
|
111
|
+
refreshToken: 'revoked-refresh-token',
|
|
112
|
+
expiresAt: Date.now() - 1_000,
|
|
113
|
+
scopes: ['user:inference'],
|
|
114
|
+
subscriptionType: 'max',
|
|
115
|
+
})
|
|
116
|
+
hahaOAuthService.setRefreshFn(async () => {
|
|
117
|
+
throw new Error('refresh revoked')
|
|
118
|
+
})
|
|
119
|
+
|
|
120
|
+
const { req, url, segments } = buildReq('GET', '/api/haha-oauth/status')
|
|
121
|
+
const res = await handleHahaOAuthApi(req, url, segments)
|
|
122
|
+
|
|
123
|
+
expect(res.status).toBe(200)
|
|
124
|
+
expect(await res.json()).toEqual({ loggedIn: false })
|
|
125
|
+
})
|
|
126
|
+
})
|
|
127
|
+
|
|
128
|
+
describe('DELETE /api/haha-oauth', () => {
|
|
129
|
+
beforeEach(setup)
|
|
130
|
+
afterEach(teardown)
|
|
131
|
+
|
|
132
|
+
test('clears token file', async () => {
|
|
133
|
+
await hahaOAuthService.saveTokens({
|
|
134
|
+
accessToken: 'a',
|
|
135
|
+
refreshToken: null,
|
|
136
|
+
expiresAt: null,
|
|
137
|
+
scopes: [],
|
|
138
|
+
subscriptionType: null,
|
|
139
|
+
})
|
|
140
|
+
|
|
141
|
+
const { req, url, segments } = buildReq('DELETE', '/api/haha-oauth')
|
|
142
|
+
const res = await handleHahaOAuthApi(req, url, segments)
|
|
143
|
+
expect(res.status).toBe(200)
|
|
144
|
+
expect(await hahaOAuthService.loadTokens()).toBeNull()
|
|
145
|
+
})
|
|
146
|
+
})
|