forked from anomalyco/opencode-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession_init_command_revert_test.go
More file actions
313 lines (283 loc) · 9.41 KB
/
session_init_command_revert_test.go
File metadata and controls
313 lines (283 loc) · 9.41 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
package opencode
import (
"context"
"encoding/json"
"errors"
"io"
"net/http"
"net/http/httptest"
"testing"
)
// TestSessionInit_Success verifies Init sends POST to /session/{id}/init with correct body and decodes bool response
func TestSessionInit_Success(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
t.Errorf("expected POST, got %s", r.Method)
}
if r.URL.Path != "/session/sess_123/init" {
t.Errorf("expected path /session/sess_123/init, got %s", r.URL.Path)
}
body, _ := io.ReadAll(r.Body)
var parsed map[string]interface{}
if err := json.Unmarshal(body, &parsed); err != nil {
t.Fatalf("failed to unmarshal request body: %v", err)
}
if parsed["messageID"] != "msg_001" {
t.Errorf("expected messageID msg_001, got %v", parsed["messageID"])
}
if parsed["modelID"] != "gpt-4" {
t.Errorf("expected modelID gpt-4, got %v", parsed["modelID"])
}
if parsed["providerID"] != "openai" {
t.Errorf("expected providerID openai, got %v", parsed["providerID"])
}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(true)
}))
defer server.Close()
client, err := NewClient(WithBaseURL(server.URL))
if err != nil {
t.Fatalf("failed to create client: %v", err)
}
result, err := client.Session.Init(context.Background(), "sess_123", &SessionInitParams{
MessageID: "msg_001",
ModelID: "gpt-4",
ProviderID: "openai",
})
if err != nil {
t.Fatalf("Init failed: %v", err)
}
if !result {
t.Error("expected Init to return true, got false")
}
}
// TestSessionCommand_Success verifies Command sends POST to /session/{id}/command with correct body and decodes response
func TestSessionCommand_Success(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
t.Errorf("expected POST, got %s", r.Method)
}
if r.URL.Path != "/session/sess_123/command" {
t.Errorf("expected path /session/sess_123/command, got %s", r.URL.Path)
}
body, _ := io.ReadAll(r.Body)
var parsed map[string]interface{}
if err := json.Unmarshal(body, &parsed); err != nil {
t.Fatalf("failed to unmarshal request body: %v", err)
}
if parsed["command"] != "/ask" {
t.Errorf("expected command /ask, got %v", parsed["command"])
}
if parsed["arguments"] != "what is this project" {
t.Errorf("expected arguments 'what is this project', got %v", parsed["arguments"])
}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(map[string]interface{}{
"info": map[string]interface{}{
"id": "msg_001",
"sessionID": "sess_123",
"role": "assistant",
"cost": 0.0,
"mode": "",
"modelID": "",
"parentID": "",
"path": map[string]interface{}{"cwd": "", "root": ""},
"providerID": "",
"system": []string{},
"time": map[string]interface{}{"created": 0.0, "completed": 0.0},
"tokens": map[string]interface{}{"input": 0, "output": 0, "reasoning": 0, "cache": map[string]interface{}{"read": 0, "write": 0}},
},
"parts": []interface{}{},
})
}))
defer server.Close()
client, err := NewClient(WithBaseURL(server.URL))
if err != nil {
t.Fatalf("failed to create client: %v", err)
}
resp, err := client.Session.Command(context.Background(), "sess_123", &SessionCommandParams{
Command: "/ask",
Arguments: "what is this project",
})
if err != nil {
t.Fatalf("Command failed: %v", err)
}
if resp.Info.ID != "msg_001" {
t.Errorf("expected info ID msg_001, got %s", resp.Info.ID)
}
if resp.Info.SessionID != "sess_123" {
t.Errorf("expected info sessionID sess_123, got %s", resp.Info.SessionID)
}
if resp.Info.Role != AssistantMessageRoleAssistant {
t.Errorf("expected info role assistant, got %s", resp.Info.Role)
}
}
// TestSessionRevert_Success verifies Revert sends POST to /session/{id}/revert with correct body and decodes session response
func TestSessionRevert_Success(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
t.Errorf("expected POST, got %s", r.Method)
}
if r.URL.Path != "/session/sess_123/revert" {
t.Errorf("expected path /session/sess_123/revert, got %s", r.URL.Path)
}
body, _ := io.ReadAll(r.Body)
var parsed map[string]interface{}
if err := json.Unmarshal(body, &parsed); err != nil {
t.Fatalf("failed to unmarshal request body: %v", err)
}
if parsed["messageID"] != "msg_to_revert" {
t.Errorf("expected messageID msg_to_revert, got %v", parsed["messageID"])
}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(map[string]interface{}{
"id": "sess_123",
"directory": "/test/path",
"projectID": "proj_456",
"title": "Reverted Session",
"version": "1.0.0",
"time": map[string]interface{}{
"created": 1234567890.0,
"updated": 1234567900.0,
},
})
}))
defer server.Close()
client, err := NewClient(WithBaseURL(server.URL))
if err != nil {
t.Fatalf("failed to create client: %v", err)
}
session, err := client.Session.Revert(context.Background(), "sess_123", &SessionRevertParams{
MessageID: "msg_to_revert",
})
if err != nil {
t.Fatalf("Revert failed: %v", err)
}
if session.ID != "sess_123" {
t.Errorf("expected session ID sess_123, got %s", session.ID)
}
if session.Title != "Reverted Session" {
t.Errorf("expected title 'Reverted Session', got %s", session.Title)
}
if session.ProjectID != "proj_456" {
t.Errorf("expected projectID proj_456, got %s", session.ProjectID)
}
}
func TestSessionCommand_RequiresCommand(t *testing.T) {
client, err := NewClient()
if err != nil {
t.Fatalf("failed to create client: %v", err)
}
_, err = client.Session.Command(context.Background(), "sess_123", &SessionCommandParams{})
if err == nil {
t.Fatal("expected error for missing command")
}
if !errors.Is(err, &MissingRequiredParameterError{Parameter: "command"}) {
t.Fatalf("unexpected error: %v", err)
}
}
func TestSessionCommand_AllowsEmptyArguments(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
t.Errorf("expected POST, got %s", r.Method)
}
if r.URL.Path != "/session/sess_123/command" {
t.Errorf("expected path /session/sess_123/command, got %s", r.URL.Path)
}
body, _ := io.ReadAll(r.Body)
var parsed map[string]interface{}
if err := json.Unmarshal(body, &parsed); err != nil {
t.Fatalf("failed to unmarshal request body: %v", err)
}
if parsed["command"] != "/ask" {
t.Errorf("expected command /ask, got %v", parsed["command"])
}
if parsed["arguments"] != "" {
t.Errorf("expected empty arguments string, got %v", parsed["arguments"])
}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(map[string]interface{}{
"info": map[string]interface{}{
"id": "msg_001",
"sessionID": "sess_123",
"role": "assistant",
"cost": 0.0,
"mode": "",
"modelID": "",
"parentID": "",
"path": map[string]interface{}{"cwd": "", "root": ""},
"providerID": "",
"system": []string{},
"time": map[string]interface{}{"created": 0.0, "completed": 0.0},
"tokens": map[string]interface{}{"input": 0, "output": 0, "reasoning": 0, "cache": map[string]interface{}{"read": 0, "write": 0}},
},
"parts": []interface{}{},
})
}))
defer server.Close()
client, err := NewClient(WithBaseURL(server.URL))
if err != nil {
t.Fatalf("failed to create client: %v", err)
}
resp, err := client.Session.Command(context.Background(), "sess_123", &SessionCommandParams{
Command: "/ask",
Arguments: "",
})
if err != nil {
t.Fatalf("expected empty arguments to be accepted, got %v", err)
}
if resp.Info.ID != "msg_001" {
t.Errorf("expected info ID msg_001, got %s", resp.Info.ID)
}
}
func TestSessionInit_RequiresFields(t *testing.T) {
tests := []struct {
name string
params *SessionInitParams
wantParam string
}{
{
name: "missing message id",
params: &SessionInitParams{ModelID: "gpt-4", ProviderID: "openai"},
wantParam: "messageID",
},
{
name: "missing model id",
params: &SessionInitParams{MessageID: "msg_001", ProviderID: "openai"},
wantParam: "modelID",
},
{
name: "missing provider id",
params: &SessionInitParams{MessageID: "msg_001", ModelID: "gpt-4"},
wantParam: "providerID",
},
}
client, err := NewClient()
if err != nil {
t.Fatalf("failed to create client: %v", err)
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := client.Session.Init(context.Background(), "sess_123", tt.params)
if err == nil {
t.Fatal("expected validation error")
}
if !errors.Is(err, &MissingRequiredParameterError{Parameter: tt.wantParam}) {
t.Fatalf("expected missing required %s error, got %v", tt.wantParam, err)
}
})
}
}
func TestSessionRevert_RequiresMessageID(t *testing.T) {
client, err := NewClient()
if err != nil {
t.Fatalf("failed to create client: %v", err)
}
_, err = client.Session.Revert(context.Background(), "sess_123", &SessionRevertParams{})
if err == nil {
t.Fatal("expected error for missing messageID")
}
if !errors.Is(err, &MissingRequiredParameterError{Parameter: "messageID"}) {
t.Fatalf("unexpected error: %v", err)
}
}