Add OpenRouter authentication diagnostics

This commit is contained in:
2026-10-13 17:23:47 +00:00
parent 2e81c9e61b
commit edaaecc982
7 changed files with 551 additions and 4 deletions

View File

@@ -12,6 +12,8 @@ import (
"sync"
"testing"
"time"
"gitea.maximumdirect.net/eric/promptkit/internal/domain"
)
func TestGetWithEvidenceCapturesResponseAndOmitsURLUserInfo(t *testing.T) {
@@ -54,6 +56,69 @@ func TestGetWithEvidenceCapturesResponseAndOmitsURLUserInfo(t *testing.T) {
}
}
func TestGetWithAuthenticationEvidenceResolvesOptionalCredentials(t *testing.T) {
tests := []struct {
name string
target domain.ExecutionTarget
environment string
wantAuth string
}{
{
name: "direct key takes precedence",
target: domain.ExecutionTarget{
APIKey: " direct-key ",
APIKeyEnv: "PROMPTKIT_DIAGNOSTIC_KEY",
},
environment: "environment-key",
wantAuth: "Bearer direct-key",
},
{
name: "environment key is used",
target: domain.ExecutionTarget{APIKeyEnv: "PROMPTKIT_DIAGNOSTIC_KEY"},
environment: " environment-key ",
wantAuth: "Bearer environment-key",
},
{
name: "missing required key is optional here",
target: domain.ExecutionTarget{APIKeyEnv: "PROMPTKIT_DIAGNOSTIC_KEY", APIKeyRequired: true},
environment: " ",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
t.Setenv("PROMPTKIT_DIAGNOSTIC_KEY", tc.environment)
var authorization string
client, err := NewOpenAICompatibleClient(OpenAICompatibleConfig{
HTTPClient: &http.Client{Transport: roundTripFunc(func(request *http.Request) (*http.Response, error) {
authorization = request.Header.Get("Authorization")
return &http.Response{
StatusCode: http.StatusUnauthorized,
Header: make(http.Header),
Body: io.NopCloser(strings.NewReader("")),
}, nil
})},
})
if err != nil {
t.Fatal(err)
}
response, evidence, err := client.GetWithAuthenticationEvidence(
context.Background(), "https://example.invalid/key", 0, tc.target,
)
if err != nil || response == nil {
t.Fatalf("response=%v error=%v", response, err)
}
response.Body.Close()
if authorization != tc.wantAuth || evidence.CredentialsSupplied != (tc.wantAuth != "") {
t.Fatalf("authorization=%q evidence=%+v", authorization, evidence)
}
if evidence.Endpoint != "https://example.invalid/key" || evidence.HTTPStatus == nil || *evidence.HTTPStatus != http.StatusUnauthorized {
t.Fatalf("HTTP evidence = %+v", evidence)
}
})
}
}
func TestDoWithEvidenceRecordsRequestWithoutResponse(t *testing.T) {
transportErr := errors.New("transport unavailable")
client := &http.Client{Transport: roundTripFunc(func(*http.Request) (*http.Response, error) {