|
| 1 | +package e2e |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + copilot "github.com/github/copilot-sdk/go" |
| 7 | + "github.com/github/copilot-sdk/go/internal/e2e/testharness" |
| 8 | + "github.com/github/copilot-sdk/go/rpc" |
| 9 | +) |
| 10 | + |
| 11 | +func TestRpc(t *testing.T) { |
| 12 | + cliPath := testharness.CLIPath() |
| 13 | + if cliPath == "" { |
| 14 | + t.Fatal("CLI not found. Run 'npm install' in the nodejs directory first.") |
| 15 | + } |
| 16 | + |
| 17 | + t.Run("should call RPC.Ping with typed params and result", func(t *testing.T) { |
| 18 | + client := copilot.NewClient(&copilot.ClientOptions{ |
| 19 | + CLIPath: cliPath, |
| 20 | + UseStdio: copilot.Bool(true), |
| 21 | + }) |
| 22 | + t.Cleanup(func() { client.ForceStop() }) |
| 23 | + |
| 24 | + if err := client.Start(t.Context()); err != nil { |
| 25 | + t.Fatalf("Failed to start client: %v", err) |
| 26 | + } |
| 27 | + |
| 28 | + result, err := client.RPC.Ping(t.Context(), &rpc.PingParams{Message: copilot.String("typed rpc test")}) |
| 29 | + if err != nil { |
| 30 | + t.Fatalf("Failed to call RPC.Ping: %v", err) |
| 31 | + } |
| 32 | + |
| 33 | + if result.Message != "pong: typed rpc test" { |
| 34 | + t.Errorf("Expected message 'pong: typed rpc test', got %q", result.Message) |
| 35 | + } |
| 36 | + |
| 37 | + if result.Timestamp < 0 { |
| 38 | + t.Errorf("Expected timestamp >= 0, got %d", result.Timestamp) |
| 39 | + } |
| 40 | + |
| 41 | + if err := client.Stop(); err != nil { |
| 42 | + t.Errorf("Expected no errors on stop, got %v", err) |
| 43 | + } |
| 44 | + }) |
| 45 | + |
| 46 | + t.Run("should call RPC.Models.List with typed result", func(t *testing.T) { |
| 47 | + client := copilot.NewClient(&copilot.ClientOptions{ |
| 48 | + CLIPath: cliPath, |
| 49 | + UseStdio: copilot.Bool(true), |
| 50 | + }) |
| 51 | + t.Cleanup(func() { client.ForceStop() }) |
| 52 | + |
| 53 | + if err := client.Start(t.Context()); err != nil { |
| 54 | + t.Fatalf("Failed to start client: %v", err) |
| 55 | + } |
| 56 | + |
| 57 | + authStatus, err := client.GetAuthStatus(t.Context()) |
| 58 | + if err != nil { |
| 59 | + t.Fatalf("Failed to get auth status: %v", err) |
| 60 | + } |
| 61 | + |
| 62 | + if !authStatus.IsAuthenticated { |
| 63 | + t.Skip("Not authenticated - skipping models.list test") |
| 64 | + } |
| 65 | + |
| 66 | + result, err := client.RPC.Models.List(t.Context()) |
| 67 | + if err != nil { |
| 68 | + t.Fatalf("Failed to call RPC.Models.List: %v", err) |
| 69 | + } |
| 70 | + |
| 71 | + if result.Models == nil { |
| 72 | + t.Error("Expected models to be defined") |
| 73 | + } |
| 74 | + |
| 75 | + if err := client.Stop(); err != nil { |
| 76 | + t.Errorf("Expected no errors on stop, got %v", err) |
| 77 | + } |
| 78 | + }) |
| 79 | + |
| 80 | + // account.getQuota is defined in schema but not yet implemented in CLI |
| 81 | + t.Run("should call RPC.Account.GetQuota when authenticated", func(t *testing.T) { |
| 82 | + t.Skip("account.getQuota not yet implemented in CLI") |
| 83 | + |
| 84 | + client := copilot.NewClient(&copilot.ClientOptions{ |
| 85 | + CLIPath: cliPath, |
| 86 | + UseStdio: copilot.Bool(true), |
| 87 | + }) |
| 88 | + t.Cleanup(func() { client.ForceStop() }) |
| 89 | + |
| 90 | + if err := client.Start(t.Context()); err != nil { |
| 91 | + t.Fatalf("Failed to start client: %v", err) |
| 92 | + } |
| 93 | + |
| 94 | + authStatus, err := client.GetAuthStatus(t.Context()) |
| 95 | + if err != nil { |
| 96 | + t.Fatalf("Failed to get auth status: %v", err) |
| 97 | + } |
| 98 | + |
| 99 | + if !authStatus.IsAuthenticated { |
| 100 | + t.Skip("Not authenticated - skipping account.getQuota test") |
| 101 | + } |
| 102 | + |
| 103 | + result, err := client.RPC.Account.GetQuota(t.Context()) |
| 104 | + if err != nil { |
| 105 | + t.Fatalf("Failed to call RPC.Account.GetQuota: %v", err) |
| 106 | + } |
| 107 | + |
| 108 | + if result.QuotaSnapshots == nil { |
| 109 | + t.Error("Expected quotaSnapshots to be defined") |
| 110 | + } |
| 111 | + |
| 112 | + if err := client.Stop(); err != nil { |
| 113 | + t.Errorf("Expected no errors on stop, got %v", err) |
| 114 | + } |
| 115 | + }) |
| 116 | +} |
| 117 | + |
| 118 | +func TestSessionRpc(t *testing.T) { |
| 119 | + ctx := testharness.NewE2ETestContext(t) |
| 120 | + |
| 121 | + // session.model.getCurrent is defined in schema but not yet implemented in CLI |
| 122 | + t.Run("should call session.RPC.Model.GetCurrent", func(t *testing.T) { |
| 123 | + t.Skip("session.model.getCurrent not yet implemented in CLI") |
| 124 | + |
| 125 | + session, err := ctx.Client.CreateSession(t.Context(), &copilot.SessionConfig{ |
| 126 | + Model: "claude-sonnet-4.5", |
| 127 | + }) |
| 128 | + if err != nil { |
| 129 | + t.Fatalf("Failed to create session: %v", err) |
| 130 | + } |
| 131 | + |
| 132 | + result, err := session.RPC.Model.GetCurrent(t.Context()) |
| 133 | + if err != nil { |
| 134 | + t.Fatalf("Failed to call session.RPC.Model.GetCurrent: %v", err) |
| 135 | + } |
| 136 | + |
| 137 | + if result.ModelID == "" { |
| 138 | + t.Error("Expected modelId to be defined") |
| 139 | + } |
| 140 | + }) |
| 141 | + |
| 142 | + // session.model.switchTo is defined in schema but not yet implemented in CLI |
| 143 | + t.Run("should call session.RPC.Model.SwitchTo", func(t *testing.T) { |
| 144 | + t.Skip("session.model.switchTo not yet implemented in CLI") |
| 145 | + |
| 146 | + session, err := ctx.Client.CreateSession(t.Context(), &copilot.SessionConfig{ |
| 147 | + Model: "claude-sonnet-4.5", |
| 148 | + }) |
| 149 | + if err != nil { |
| 150 | + t.Fatalf("Failed to create session: %v", err) |
| 151 | + } |
| 152 | + |
| 153 | + // Get initial model |
| 154 | + before, err := session.RPC.Model.GetCurrent(t.Context()) |
| 155 | + if err != nil { |
| 156 | + t.Fatalf("Failed to get current model: %v", err) |
| 157 | + } |
| 158 | + if before.ModelID == "" { |
| 159 | + t.Error("Expected initial modelId to be defined") |
| 160 | + } |
| 161 | + |
| 162 | + // Switch to a different model |
| 163 | + result, err := session.RPC.Model.SwitchTo(t.Context(), &rpc.SessionModelSwitchToParams{ |
| 164 | + ModelID: "gpt-4.1", |
| 165 | + }) |
| 166 | + if err != nil { |
| 167 | + t.Fatalf("Failed to switch model: %v", err) |
| 168 | + } |
| 169 | + if result.ModelID != "gpt-4.1" { |
| 170 | + t.Errorf("Expected modelId 'gpt-4.1', got %q", result.ModelID) |
| 171 | + } |
| 172 | + |
| 173 | + // Verify the switch persisted |
| 174 | + after, err := session.RPC.Model.GetCurrent(t.Context()) |
| 175 | + if err != nil { |
| 176 | + t.Fatalf("Failed to get current model after switch: %v", err) |
| 177 | + } |
| 178 | + if after.ModelID != "gpt-4.1" { |
| 179 | + t.Errorf("Expected modelId 'gpt-4.1' after switch, got %q", after.ModelID) |
| 180 | + } |
| 181 | + }) |
| 182 | +} |
0 commit comments