-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession_test.go
More file actions
456 lines (396 loc) · 11.2 KB
/
session_test.go
File metadata and controls
456 lines (396 loc) · 11.2 KB
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
package session
import (
"context"
"encoding/json"
"os"
"testing"
"github.com/anomalyco/oho/internal/client"
"github.com/anomalyco/oho/internal/config"
"github.com/anomalyco/oho/internal/testutil"
"github.com/anomalyco/oho/internal/types"
)
func TestMain(m *testing.M) {
os.Setenv("OPENCODE_SERVER_HOST", "127.0.0.1")
os.Setenv("OPENCODE_SERVER_PORT", "4096")
os.Setenv("OPENCODE_SERVER_USERNAME", "opencode")
os.Setenv("OPENCODE_SERVER_PASSWORD", "test")
_ = config.Init()
m.Run()
}
func TestSessionListCmd(t *testing.T) {
tests := []struct {
name string
mockResp []byte
mockErr error
statusCode int
wantErr bool
}{
{
name: "success with sessions",
mockResp: testutil.MockSessionsResponse(),
mockErr: nil,
statusCode: 200,
wantErr: false,
},
{
name: "empty sessions",
mockResp: testutil.MockResponse([]types.Session{}),
mockErr: nil,
statusCode: 200,
wantErr: false,
},
{
name: "server error",
mockResp: nil,
mockErr: &client.APIError{StatusCode: 500, Message: "Internal Error"},
statusCode: 500,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mock := &client.MockClient{
GetFunc: func(ctx context.Context, path string) ([]byte, error) {
return tt.mockResp, tt.mockErr
},
}
resp, err := mock.Get(context.Background(), "/session")
if tt.wantErr && err == nil {
t.Error("Expected error but got nil")
}
if !tt.wantErr && err != nil {
t.Errorf("Unexpected error: %v", err)
}
if !tt.wantErr && resp != nil {
var sessions []types.Session
if err := json.Unmarshal(resp, &sessions); err != nil {
t.Errorf("Failed to unmarshal: %v", err)
}
}
})
}
}
func TestSessionCreateCmd(t *testing.T) {
tests := []struct {
name string
parentID string
title string
mockResp []byte
mockErr error
statusCode int
wantErr bool
}{
{
name: "create simple session",
title: "Test Session",
mockResp: testutil.MockSessionResponse(),
mockErr: nil,
statusCode: 200,
wantErr: false,
},
{
name: "create with parent",
parentID: "parent-123",
title: "Child Session",
mockResp: testutil.MockSessionResponse(),
mockErr: nil,
statusCode: 200,
wantErr: false,
},
{
name: "server error",
title: "Test Session",
mockResp: nil,
mockErr: &client.APIError{StatusCode: 500, Message: "Internal Error"},
statusCode: 500,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mock := &client.MockClient{
PostFunc: func(ctx context.Context, path string, body interface{}) ([]byte, error) {
return tt.mockResp, tt.mockErr
},
}
resp, err := mock.Post(context.Background(), "/session", map[string]interface{}{"title": tt.title})
if tt.wantErr && err == nil {
t.Error("Expected error but got nil")
}
if !tt.wantErr && err != nil {
t.Errorf("Unexpected error: %v", err)
}
if !tt.wantErr && resp != nil {
var session types.Session
if err := json.Unmarshal(resp, &session); err != nil {
t.Errorf("Failed to unmarshal: %v", err)
}
}
})
}
}
func TestSessionStatusCmd(t *testing.T) {
mock := &client.MockClient{
GetFunc: func(ctx context.Context, path string) ([]byte, error) {
return testutil.MockSessionStatusResponse(), nil
},
}
resp, err := mock.Get(context.Background(), "/session/status")
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
var status map[string]types.SessionStatus
if err := json.Unmarshal(resp, &status); err != nil {
t.Errorf("Failed to unmarshal: %v", err)
}
if len(status) != 2 {
t.Errorf("Expected 2 sessions, got %d", len(status))
}
}
func TestSessionGetCmd(t *testing.T) {
mock := &client.MockClient{
GetFunc: func(ctx context.Context, path string) ([]byte, error) {
return testutil.MockSessionResponse(), nil
},
}
resp, err := mock.Get(context.Background(), "/session/session1")
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
var session types.Session
if err := json.Unmarshal(resp, &session); err != nil {
t.Errorf("Failed to unmarshal: %v", err)
}
if session.ID != "session1" {
t.Errorf("Expected session ID 'session1', got %s", session.ID)
}
}
func TestSessionDeleteCmd(t *testing.T) {
tests := []struct {
name string
mockErr error
wantErr bool
}{
{
name: "delete success",
mockErr: nil,
wantErr: false,
},
{
name: "delete error",
mockErr: &client.APIError{StatusCode: 404, Message: "Session not found"},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mock := &client.MockClient{
DeleteFunc: func(ctx context.Context, path string) ([]byte, error) {
return testutil.MockBoolResponse(true), tt.mockErr
},
}
resp, err := mock.Delete(context.Background(), "/session/session1")
if tt.wantErr && err == nil {
t.Error("Expected error but got nil")
}
if !tt.wantErr && err != nil {
t.Errorf("Unexpected error: %v", err)
}
if !tt.wantErr && resp != nil {
var deleted bool
if err := json.Unmarshal(resp, &deleted); err != nil {
t.Errorf("Failed to unmarshal: %v", err)
}
if !deleted {
t.Error("Expected deleted=true")
}
}
})
}
}
func TestSessionUpdateCmd(t *testing.T) {
mock := &client.MockClient{
PatchFunc: func(ctx context.Context, path string, body interface{}) ([]byte, error) {
return testutil.MockSessionResponse(), nil
},
}
resp, err := mock.Patch(context.Background(), "/session/session1", map[string]interface{}{"title": "New Title"})
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
var session types.Session
if err := json.Unmarshal(resp, &session); err != nil {
t.Errorf("Failed to unmarshal: %v", err)
}
}
func TestSessionChildrenCmd(t *testing.T) {
mock := &client.MockClient{
GetFunc: func(ctx context.Context, path string) ([]byte, error) {
return testutil.MockSessionsResponse(), nil
},
}
resp, err := mock.Get(context.Background(), "/session/session1/children")
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
var sessions []types.Session
if err := json.Unmarshal(resp, &sessions); err != nil {
t.Errorf("Failed to unmarshal: %v", err)
}
}
func TestSessionTodoCmd(t *testing.T) {
mock := &client.MockClient{
GetFunc: func(ctx context.Context, path string) ([]byte, error) {
return testutil.MockTodoResponse(), nil
},
}
resp, err := mock.Get(context.Background(), "/session/session1/todo")
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
var todos []types.Todo
if err := json.Unmarshal(resp, &todos); err != nil {
t.Errorf("Failed to unmarshal: %v", err)
}
if len(todos) != 2 {
t.Errorf("Expected 2 todos, got %d", len(todos))
}
}
func TestSessionForkCmd(t *testing.T) {
mock := &client.MockClient{
PostFunc: func(ctx context.Context, path string, body interface{}) ([]byte, error) {
return testutil.MockSessionResponse(), nil
},
}
resp, err := mock.Post(context.Background(), "/session/session1/fork", nil)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
var session types.Session
if err := json.Unmarshal(resp, &session); err != nil {
t.Errorf("Failed to unmarshal: %v", err)
}
}
func TestSessionAbortCmd(t *testing.T) {
mock := &client.MockClient{
PostFunc: func(ctx context.Context, path string, body interface{}) ([]byte, error) {
return testutil.MockBoolResponse(true), nil
},
}
resp, err := mock.Post(context.Background(), "/session/session1/abort", nil)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
var success bool
if err := json.Unmarshal(resp, &success); err != nil {
t.Errorf("Failed to unmarshal: %v", err)
}
if !success {
t.Error("Expected success=true")
}
}
func TestSessionShareCmd(t *testing.T) {
mock := &client.MockClient{
PostFunc: func(ctx context.Context, path string, body interface{}) ([]byte, error) {
return testutil.MockSessionResponse(), nil
},
}
resp, err := mock.Post(context.Background(), "/session/session1/share", nil)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
var session types.Session
if err := json.Unmarshal(resp, &session); err != nil {
t.Errorf("Failed to unmarshal: %v", err)
}
}
func TestSessionUnshareCmd(t *testing.T) {
mock := &client.MockClient{
DeleteFunc: func(ctx context.Context, path string) ([]byte, error) {
return testutil.MockSessionResponse(), nil
},
}
resp, err := mock.Delete(context.Background(), "/session/session1/share")
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
var session types.Session
if err := json.Unmarshal(resp, &session); err != nil {
t.Errorf("Failed to unmarshal: %v", err)
}
}
func TestSessionDiffCmd(t *testing.T) {
mock := &client.MockClient{
GetWithQueryFunc: func(ctx context.Context, path string, queryParams map[string]string) ([]byte, error) {
return testutil.MockDiffResponse(), nil
},
}
resp, err := mock.GetWithQuery(context.Background(), "/session/session1/diff", nil)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
var diffs []types.FileDiff
if err := json.Unmarshal(resp, &diffs); err != nil {
t.Errorf("Failed to unmarshal: %v", err)
}
}
func TestSessionSummarizeCmd(t *testing.T) {
mock := &client.MockClient{
PostFunc: func(ctx context.Context, path string, body interface{}) ([]byte, error) {
return testutil.MockBoolResponse(true), nil
},
}
resp, err := mock.Post(context.Background(), "/session/session1/summarize", map[string]interface{}{"providerID": "openai", "modelID": "gpt-4"})
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
var success bool
if err := json.Unmarshal(resp, &success); err != nil {
t.Errorf("Failed to unmarshal: %v", err)
}
}
func TestSessionRevertCmd(t *testing.T) {
mock := &client.MockClient{
PostFunc: func(ctx context.Context, path string, body interface{}) ([]byte, error) {
return testutil.MockBoolResponse(true), nil
},
}
resp, err := mock.Post(context.Background(), "/session/session1/revert", map[string]interface{}{"messageID": "msg1"})
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
var success bool
if err := json.Unmarshal(resp, &success); err != nil {
t.Errorf("Failed to unmarshal: %v", err)
}
}
func TestSessionUnrevertCmd(t *testing.T) {
mock := &client.MockClient{
PostFunc: func(ctx context.Context, path string, body interface{}) ([]byte, error) {
return testutil.MockBoolResponse(true), nil
},
}
resp, err := mock.Post(context.Background(), "/session/session1/unrevert", nil)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
var success bool
if err := json.Unmarshal(resp, &success); err != nil {
t.Errorf("Failed to unmarshal: %v", err)
}
}
func TestSessionPermissionsCmd(t *testing.T) {
mock := &client.MockClient{
PostFunc: func(ctx context.Context, path string, body interface{}) ([]byte, error) {
return testutil.MockBoolResponse(true), nil
},
}
resp, err := mock.Post(context.Background(), "/session/session1/permissions/perm1", map[string]interface{}{"response": "allow"})
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
var success bool
if err := json.Unmarshal(resp, &success); err != nil {
t.Errorf("Failed to unmarshal: %v", err)
}
}