|
1 | 1 | package e2e |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "strings" |
4 | 5 | "testing" |
5 | 6 |
|
6 | 7 | copilot "github.com/github/copilot-sdk/go" |
@@ -185,4 +186,185 @@ func TestSessionRpc(t *testing.T) { |
185 | 186 | t.Errorf("Expected modelId 'gpt-4.1' after switch, got %v", after.ModelID) |
186 | 187 | } |
187 | 188 | }) |
| 189 | + |
| 190 | + t.Run("should get and set session mode", func(t *testing.T) { |
| 191 | + session, err := client.CreateSession(t.Context(), nil) |
| 192 | + if err != nil { |
| 193 | + t.Fatalf("Failed to create session: %v", err) |
| 194 | + } |
| 195 | + |
| 196 | + // Get initial mode (default should be interactive) |
| 197 | + initial, err := session.RPC.Mode.Get(t.Context()) |
| 198 | + if err != nil { |
| 199 | + t.Fatalf("Failed to get mode: %v", err) |
| 200 | + } |
| 201 | + if initial.Mode != rpc.Interactive { |
| 202 | + t.Errorf("Expected initial mode 'interactive', got %q", initial.Mode) |
| 203 | + } |
| 204 | + |
| 205 | + // Switch to plan mode |
| 206 | + planResult, err := session.RPC.Mode.Set(t.Context(), &rpc.SessionModeSetParams{Mode: rpc.Plan}) |
| 207 | + if err != nil { |
| 208 | + t.Fatalf("Failed to set mode to plan: %v", err) |
| 209 | + } |
| 210 | + if planResult.Mode != rpc.Plan { |
| 211 | + t.Errorf("Expected mode 'plan', got %q", planResult.Mode) |
| 212 | + } |
| 213 | + |
| 214 | + // Verify mode persisted |
| 215 | + afterPlan, err := session.RPC.Mode.Get(t.Context()) |
| 216 | + if err != nil { |
| 217 | + t.Fatalf("Failed to get mode after plan: %v", err) |
| 218 | + } |
| 219 | + if afterPlan.Mode != rpc.Plan { |
| 220 | + t.Errorf("Expected mode 'plan' after set, got %q", afterPlan.Mode) |
| 221 | + } |
| 222 | + |
| 223 | + // Switch back to interactive |
| 224 | + interactiveResult, err := session.RPC.Mode.Set(t.Context(), &rpc.SessionModeSetParams{Mode: rpc.Interactive}) |
| 225 | + if err != nil { |
| 226 | + t.Fatalf("Failed to set mode to interactive: %v", err) |
| 227 | + } |
| 228 | + if interactiveResult.Mode != rpc.Interactive { |
| 229 | + t.Errorf("Expected mode 'interactive', got %q", interactiveResult.Mode) |
| 230 | + } |
| 231 | + }) |
| 232 | + |
| 233 | + t.Run("should read, update, and delete plan", func(t *testing.T) { |
| 234 | + session, err := client.CreateSession(t.Context(), nil) |
| 235 | + if err != nil { |
| 236 | + t.Fatalf("Failed to create session: %v", err) |
| 237 | + } |
| 238 | + |
| 239 | + // Initially plan should not exist |
| 240 | + initial, err := session.RPC.Plan.Read(t.Context()) |
| 241 | + if err != nil { |
| 242 | + t.Fatalf("Failed to read plan: %v", err) |
| 243 | + } |
| 244 | + if initial.Exists { |
| 245 | + t.Error("Expected plan to not exist initially") |
| 246 | + } |
| 247 | + if initial.Content != nil { |
| 248 | + t.Error("Expected content to be nil initially") |
| 249 | + } |
| 250 | + |
| 251 | + // Create/update plan |
| 252 | + planContent := "# Test Plan\n\n- Step 1\n- Step 2" |
| 253 | + _, err = session.RPC.Plan.Update(t.Context(), &rpc.SessionPlanUpdateParams{Content: planContent}) |
| 254 | + if err != nil { |
| 255 | + t.Fatalf("Failed to update plan: %v", err) |
| 256 | + } |
| 257 | + |
| 258 | + // Verify plan exists and has correct content |
| 259 | + afterUpdate, err := session.RPC.Plan.Read(t.Context()) |
| 260 | + if err != nil { |
| 261 | + t.Fatalf("Failed to read plan after update: %v", err) |
| 262 | + } |
| 263 | + if !afterUpdate.Exists { |
| 264 | + t.Error("Expected plan to exist after update") |
| 265 | + } |
| 266 | + if afterUpdate.Content == nil || *afterUpdate.Content != planContent { |
| 267 | + t.Errorf("Expected content %q, got %v", planContent, afterUpdate.Content) |
| 268 | + } |
| 269 | + |
| 270 | + // Delete plan |
| 271 | + _, err = session.RPC.Plan.Delete(t.Context()) |
| 272 | + if err != nil { |
| 273 | + t.Fatalf("Failed to delete plan: %v", err) |
| 274 | + } |
| 275 | + |
| 276 | + // Verify plan is deleted |
| 277 | + afterDelete, err := session.RPC.Plan.Read(t.Context()) |
| 278 | + if err != nil { |
| 279 | + t.Fatalf("Failed to read plan after delete: %v", err) |
| 280 | + } |
| 281 | + if afterDelete.Exists { |
| 282 | + t.Error("Expected plan to not exist after delete") |
| 283 | + } |
| 284 | + if afterDelete.Content != nil { |
| 285 | + t.Error("Expected content to be nil after delete") |
| 286 | + } |
| 287 | + }) |
| 288 | + |
| 289 | + t.Run("should create, list, and read workspace files", func(t *testing.T) { |
| 290 | + session, err := client.CreateSession(t.Context(), nil) |
| 291 | + if err != nil { |
| 292 | + t.Fatalf("Failed to create session: %v", err) |
| 293 | + } |
| 294 | + |
| 295 | + // Initially no files |
| 296 | + initialFiles, err := session.RPC.Workspace.ListFiles(t.Context()) |
| 297 | + if err != nil { |
| 298 | + t.Fatalf("Failed to list files: %v", err) |
| 299 | + } |
| 300 | + if len(initialFiles.Files) != 0 { |
| 301 | + t.Errorf("Expected no files initially, got %v", initialFiles.Files) |
| 302 | + } |
| 303 | + |
| 304 | + // Create a file |
| 305 | + fileContent := "Hello, workspace!" |
| 306 | + _, err = session.RPC.Workspace.CreateFile(t.Context(), &rpc.SessionWorkspaceCreateFileParams{ |
| 307 | + Path: "test.txt", |
| 308 | + Content: fileContent, |
| 309 | + }) |
| 310 | + if err != nil { |
| 311 | + t.Fatalf("Failed to create file: %v", err) |
| 312 | + } |
| 313 | + |
| 314 | + // List files |
| 315 | + afterCreate, err := session.RPC.Workspace.ListFiles(t.Context()) |
| 316 | + if err != nil { |
| 317 | + t.Fatalf("Failed to list files after create: %v", err) |
| 318 | + } |
| 319 | + if !containsString(afterCreate.Files, "test.txt") { |
| 320 | + t.Errorf("Expected files to contain 'test.txt', got %v", afterCreate.Files) |
| 321 | + } |
| 322 | + |
| 323 | + // Read file |
| 324 | + readResult, err := session.RPC.Workspace.ReadFile(t.Context(), &rpc.SessionWorkspaceReadFileParams{ |
| 325 | + Path: "test.txt", |
| 326 | + }) |
| 327 | + if err != nil { |
| 328 | + t.Fatalf("Failed to read file: %v", err) |
| 329 | + } |
| 330 | + if readResult.Content != fileContent { |
| 331 | + t.Errorf("Expected content %q, got %q", fileContent, readResult.Content) |
| 332 | + } |
| 333 | + |
| 334 | + // Create nested file |
| 335 | + _, err = session.RPC.Workspace.CreateFile(t.Context(), &rpc.SessionWorkspaceCreateFileParams{ |
| 336 | + Path: "subdir/nested.txt", |
| 337 | + Content: "Nested content", |
| 338 | + }) |
| 339 | + if err != nil { |
| 340 | + t.Fatalf("Failed to create nested file: %v", err) |
| 341 | + } |
| 342 | + |
| 343 | + afterNested, err := session.RPC.Workspace.ListFiles(t.Context()) |
| 344 | + if err != nil { |
| 345 | + t.Fatalf("Failed to list files after nested: %v", err) |
| 346 | + } |
| 347 | + if !containsString(afterNested.Files, "test.txt") { |
| 348 | + t.Errorf("Expected files to contain 'test.txt', got %v", afterNested.Files) |
| 349 | + } |
| 350 | + hasNested := false |
| 351 | + for _, f := range afterNested.Files { |
| 352 | + if strings.Contains(f, "nested.txt") { |
| 353 | + hasNested = true |
| 354 | + break |
| 355 | + } |
| 356 | + } |
| 357 | + if !hasNested { |
| 358 | + t.Errorf("Expected files to contain 'nested.txt', got %v", afterNested.Files) |
| 359 | + } |
| 360 | + }) |
| 361 | +} |
| 362 | + |
| 363 | +func containsString(slice []string, str string) bool { |
| 364 | + for _, s := range slice { |
| 365 | + if s == str { |
| 366 | + return true |
| 367 | + } |
| 368 | + } |
| 369 | + return false |
188 | 370 | } |
0 commit comments