|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + *--------------------------------------------------------------------------------------------*/ |
| 4 | + |
| 5 | +package e2e |
| 6 | + |
| 7 | +import ( |
| 8 | + "regexp" |
| 9 | + "strings" |
| 10 | + "testing" |
| 11 | + |
| 12 | + copilot "github.com/github/copilot-sdk/go" |
| 13 | + "github.com/github/copilot-sdk/go/internal/e2e/testharness" |
| 14 | + "github.com/github/copilot-sdk/go/rpc" |
| 15 | +) |
| 16 | + |
| 17 | +// session.provider.getEndpoint is gated behind COPILOT_ALLOW_GET_PROVIDER_ENDPOINT; |
| 18 | +// the harness env passed to the CLI subprocess opts in for this test file. |
| 19 | +func TestProviderEndpointE2E(t *testing.T) { |
| 20 | + ctx := testharness.NewTestContext(t) |
| 21 | + |
| 22 | + client := ctx.NewClient(func(opts *copilot.ClientOptions) { |
| 23 | + opts.Env = append(opts.Env, "COPILOT_ALLOW_GET_PROVIDER_ENDPOINT=true") |
| 24 | + }) |
| 25 | + t.Cleanup(func() { client.ForceStop() }) |
| 26 | + |
| 27 | + t.Run("returns the BYOK provider endpoint when a custom provider is configured", func(t *testing.T) { |
| 28 | + ctx.ConfigureForTest(t) |
| 29 | + session, err := client.CreateSession(t.Context(), &copilot.SessionConfig{ |
| 30 | + OnPermissionRequest: copilot.PermissionHandler.ApproveAll, |
| 31 | + Provider: &copilot.ProviderConfig{ |
| 32 | + Type: "openai", |
| 33 | + WireAPI: "completions", |
| 34 | + BaseURL: "https://api.example.test/v1", |
| 35 | + APIKey: "byok-secret", |
| 36 | + Headers: map[string]string{"X-Custom-Header": "byok-yes"}, |
| 37 | + }, |
| 38 | + }) |
| 39 | + if err != nil { |
| 40 | + t.Fatalf("create session: %v", err) |
| 41 | + } |
| 42 | + // disconnect may fail since the BYOK provider URL is fake. |
| 43 | + defer func() { _ = session.Disconnect() }() |
| 44 | + |
| 45 | + endpoint, err := session.RPC.Provider.GetEndpoint(t.Context()) |
| 46 | + if err != nil { |
| 47 | + t.Fatalf("getEndpoint: %v", err) |
| 48 | + } |
| 49 | + |
| 50 | + if endpoint.Type != rpc.ProviderEndpointTypeOpenai { |
| 51 | + t.Errorf("Type: want %q, got %q", rpc.ProviderEndpointTypeOpenai, endpoint.Type) |
| 52 | + } |
| 53 | + if endpoint.WireAPI == nil || *endpoint.WireAPI != rpc.ProviderEndpointWireAPICompletions { |
| 54 | + t.Errorf("WireAPI: want %q, got %v", rpc.ProviderEndpointWireAPICompletions, endpoint.WireAPI) |
| 55 | + } |
| 56 | + if endpoint.BaseURL != "https://api.example.test/v1" { |
| 57 | + t.Errorf("BaseURL: got %q", endpoint.BaseURL) |
| 58 | + } |
| 59 | + if endpoint.APIKey == nil || *endpoint.APIKey != "byok-secret" { |
| 60 | + t.Errorf("APIKey: got %v", endpoint.APIKey) |
| 61 | + } |
| 62 | + if got := endpoint.Headers["X-Custom-Header"]; got != "byok-yes" { |
| 63 | + t.Errorf("X-Custom-Header: got %q", got) |
| 64 | + } |
| 65 | + // BYOK sessions never issue a CAPI session token. |
| 66 | + if endpoint.SessionToken != nil { |
| 67 | + t.Errorf("SessionToken: expected nil, got %+v", endpoint.SessionToken) |
| 68 | + } |
| 69 | + }) |
| 70 | + |
| 71 | + t.Run("returns the CAPI provider endpoint for an OAuth-authenticated session", func(t *testing.T) { |
| 72 | + ctx.ConfigureForTest(t) |
| 73 | + session, err := client.CreateSession(t.Context(), &copilot.SessionConfig{ |
| 74 | + OnPermissionRequest: copilot.PermissionHandler.ApproveAll, |
| 75 | + }) |
| 76 | + if err != nil { |
| 77 | + t.Fatalf("create session: %v", err) |
| 78 | + } |
| 79 | + defer func() { |
| 80 | + if err := session.Disconnect(); err != nil { |
| 81 | + t.Errorf("disconnect: %v", err) |
| 82 | + } |
| 83 | + }() |
| 84 | + |
| 85 | + endpoint, err := session.RPC.Provider.GetEndpoint(t.Context()) |
| 86 | + if err != nil { |
| 87 | + t.Fatalf("getEndpoint: %v", err) |
| 88 | + } |
| 89 | + |
| 90 | + switch endpoint.Type { |
| 91 | + case rpc.ProviderEndpointTypeOpenai, rpc.ProviderEndpointTypeAzure, rpc.ProviderEndpointTypeAnthropic: |
| 92 | + default: |
| 93 | + t.Errorf("unexpected Type %q", endpoint.Type) |
| 94 | + } |
| 95 | + // wireApi is omitted for anthropic; otherwise one of the OpenAI shapes. |
| 96 | + if endpoint.Type != rpc.ProviderEndpointTypeAnthropic { |
| 97 | + if endpoint.WireAPI == nil || |
| 98 | + (*endpoint.WireAPI != rpc.ProviderEndpointWireAPICompletions && |
| 99 | + *endpoint.WireAPI != rpc.ProviderEndpointWireAPIResponses) { |
| 100 | + t.Errorf("unexpected WireAPI %v for type %q", endpoint.WireAPI, endpoint.Type) |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + // CAPI baseUrl is the (proxy) Copilot API URL injected by the harness. |
| 105 | + if !strings.HasPrefix(endpoint.BaseURL, "http://") && !strings.HasPrefix(endpoint.BaseURL, "https://") { |
| 106 | + t.Errorf("BaseURL not an http(s) URL: %q", endpoint.BaseURL) |
| 107 | + } |
| 108 | + |
| 109 | + // For CAPI OAuth sessions the apiKey is the resolved GitHub bearer. |
| 110 | + if endpoint.APIKey == nil || len(*endpoint.APIKey) == 0 { |
| 111 | + t.Fatalf("APIKey should be a non-empty string, got %v", endpoint.APIKey) |
| 112 | + } |
| 113 | + |
| 114 | + // Standard CAPI headers must be present, and Authorization is surfaced |
| 115 | + // as the runtime sends it (`Bearer <apiKey>`). |
| 116 | + if endpoint.Headers["Copilot-Integration-Id"] == "" { |
| 117 | + t.Errorf("Copilot-Integration-Id header missing") |
| 118 | + } |
| 119 | + if ua := endpoint.Headers["User-Agent"]; !regexp.MustCompile(`(?i)Copilot`).MatchString(ua) { |
| 120 | + t.Errorf("User-Agent should mention Copilot, got %q", ua) |
| 121 | + } |
| 122 | + if endpoint.Headers["X-GitHub-Api-Version"] == "" { |
| 123 | + t.Errorf("X-GitHub-Api-Version header missing") |
| 124 | + } |
| 125 | + if !regexp.MustCompile(`[0-9a-f-]{8,}`).MatchString(endpoint.Headers["X-Interaction-Id"]) { |
| 126 | + t.Errorf("X-Interaction-Id should match interaction-id format, got %q", endpoint.Headers["X-Interaction-Id"]) |
| 127 | + } |
| 128 | + if want, got := "Bearer "+*endpoint.APIKey, endpoint.Headers["Authorization"]; want != got { |
| 129 | + t.Errorf("Authorization: want %q, got %q", want, got) |
| 130 | + } |
| 131 | + |
| 132 | + // When the omit-modelId path returned an auto-mode session token, it |
| 133 | + // must use the documented header name. The harness may have a non-auto |
| 134 | + // model selected, in which case the field is simply omitted. |
| 135 | + if endpoint.SessionToken != nil { |
| 136 | + if endpoint.SessionToken.Header != "Copilot-Session-Token" { |
| 137 | + t.Errorf("SessionToken.Header: got %q", endpoint.SessionToken.Header) |
| 138 | + } |
| 139 | + if endpoint.SessionToken.Token == "" { |
| 140 | + t.Errorf("SessionToken.Token should be non-empty") |
| 141 | + } |
| 142 | + } |
| 143 | + }) |
| 144 | +} |
0 commit comments