231 lines
7.1 KiB
Go
231 lines
7.1 KiB
Go
package webui
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
controlpb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1"
|
|
"github.com/samcharles93/archie-core/internal/domain/harnesssecret"
|
|
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
|
"github.com/samcharles93/archie-core/internal/secret"
|
|
)
|
|
|
|
type setupStore struct {
|
|
storecontract.TaskStore
|
|
}
|
|
|
|
func (setupStore) StatusCounts(context.Context) (map[string]int, error) {
|
|
return map[string]int{"done": 1}, nil
|
|
}
|
|
|
|
type setupModels struct {
|
|
aliases map[string]string
|
|
err error
|
|
}
|
|
|
|
func (m setupModels) CallerModels(context.Context) (string, map[string]string, error) {
|
|
return "org", m.aliases, m.err
|
|
}
|
|
|
|
// setupControl answers provider-settings and credential-bindings queries from
|
|
// fixed documents, the way the State Store would for one org.
|
|
type setupControl struct {
|
|
controlpb.ControlPlaneServiceClient
|
|
providers string
|
|
bindings string
|
|
}
|
|
|
|
func (c setupControl) Query(_ context.Context, r *controlpb.QueryRequest, _ ...grpc.CallOption) (*controlpb.QueryResponse, error) {
|
|
var value string
|
|
switch r.Kind {
|
|
case providerSettingsKind:
|
|
value = c.providers
|
|
case credentialBindingsKind:
|
|
value = c.bindings
|
|
default:
|
|
value = "null"
|
|
}
|
|
return &controlpb.QueryResponse{Resource: &controlpb.Resource{Kind: r.Kind, ValueJson: []byte(value)}}, nil
|
|
}
|
|
|
|
type setupHarnessSecrets struct {
|
|
storecontract.HarnessSecretStore
|
|
tokens map[string]harnesssecret.Secret
|
|
}
|
|
|
|
func (h setupHarnessSecrets) GetHarnessSecret(_ context.Context, _, service string) (harnesssecret.Secret, error) {
|
|
if token, ok := h.tokens[service]; ok {
|
|
return token, nil
|
|
}
|
|
return harnesssecret.Secret{}, storecontract.ErrHarnessSecretNotFound
|
|
}
|
|
|
|
func setupRequest(t *testing.T, srv *Server) []SetupStep {
|
|
t.Helper()
|
|
req := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/api/setup", nil)
|
|
response := httptest.NewRecorder()
|
|
srv.handleSetup(response, req)
|
|
if response.Code != http.StatusOK {
|
|
t.Fatalf("response = %d %s", response.Code, response.Body.String())
|
|
}
|
|
var body struct {
|
|
Steps []SetupStep `json:"steps"`
|
|
}
|
|
if err := json.Unmarshal(response.Body.Bytes(), &body); err != nil {
|
|
t.Fatalf("decode response %q: %v", response.Body.String(), err)
|
|
}
|
|
return body.Steps
|
|
}
|
|
|
|
func modelStep(steps []SetupStep) *SetupStep {
|
|
for i := range steps {
|
|
if steps[i].Title == "Model" {
|
|
return &steps[i]
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func TestHandleSetupModelStep(t *testing.T) {
|
|
resolving := func(_ context.Context, ref secret.SecretRef) (bool, error) {
|
|
return ref == (secret.SecretRef{Engine: "env", Key: "GOOD_KEY"}), nil
|
|
}
|
|
for _, tt := range []struct {
|
|
name string
|
|
aliases map[string]string
|
|
providers string
|
|
bindings string
|
|
tokens map[string]harnesssecret.Secret
|
|
check func(context.Context, secret.SecretRef) (bool, error)
|
|
open bool
|
|
detail string
|
|
}{
|
|
{
|
|
name: "no aliases leaves the step open",
|
|
aliases: map[string]string{},
|
|
open: true,
|
|
detail: "no model alias configured",
|
|
},
|
|
{
|
|
name: "resolving api_key_ref completes the step",
|
|
aliases: map[string]string{"default": "openai/gpt-5"},
|
|
providers: `{"openai":{"class":"openai","api_key_ref":{"engine":"env","key":"GOOD_KEY"}}}`,
|
|
check: resolving,
|
|
open: false,
|
|
},
|
|
{
|
|
name: "unresolving api_key_ref keeps the step open and names it",
|
|
aliases: map[string]string{"default": "openai/gpt-5"},
|
|
providers: `{"openai":{"class":"openai","api_key_ref":{"engine":"env","key":"BAD_KEY"}}}`,
|
|
check: resolving,
|
|
open: true,
|
|
detail: `provider "openai" credential env:BAD_KEY does not resolve`,
|
|
},
|
|
{
|
|
name: "provider declaring no credential keeps the step open",
|
|
aliases: map[string]string{"default": "openai/gpt-5"},
|
|
providers: `{"openai":{"class":"openai"}}`,
|
|
check: resolving,
|
|
open: true,
|
|
detail: `provider "openai" declares no credential`,
|
|
},
|
|
{
|
|
name: "credential binding secret resolves for an org provider",
|
|
aliases: map[string]string{"default": "acme/gpt-5"},
|
|
providers: `{"acme":{"class":"openai"}}`,
|
|
bindings: `[{"service":"acme","secret":{"engine":"env","key":"GOOD_KEY"}}]`,
|
|
check: resolving,
|
|
open: false,
|
|
},
|
|
{
|
|
name: "credential binding secret that does not resolve keeps the step open",
|
|
aliases: map[string]string{"default": "acme/gpt-5"},
|
|
providers: `{"acme":{"class":"openai"}}`,
|
|
bindings: `[{"service":"acme","secret":{"engine":"env","key":"BAD_KEY"}}]`,
|
|
check: resolving,
|
|
open: true,
|
|
detail: `provider "acme" credential env:BAD_KEY does not resolve`,
|
|
},
|
|
{
|
|
name: "org provider without a binding keeps the step open",
|
|
aliases: map[string]string{"default": "acme/gpt-5"},
|
|
providers: `{"acme":{"class":"openai"}}`,
|
|
check: resolving,
|
|
open: true,
|
|
detail: `provider "acme" declares no credential`,
|
|
},
|
|
{
|
|
name: "instance provider is not the org's credential to configure",
|
|
aliases: map[string]string{"default": "shared/gpt-5"},
|
|
providers: `{}`,
|
|
check: resolving,
|
|
open: false,
|
|
},
|
|
{
|
|
name: "oauth binding with a captured token set completes the step",
|
|
aliases: map[string]string{"default": "acme/gpt-5"},
|
|
providers: `{"acme":{"class":"openai"}}`,
|
|
bindings: `[{"service":"acme"}]`,
|
|
tokens: map[string]harnesssecret.Secret{"acme": {Org: "org", Service: "acme", AccessToken: "token"}},
|
|
check: resolving,
|
|
open: false,
|
|
},
|
|
{
|
|
name: "oauth binding without a captured token set keeps the step open",
|
|
aliases: map[string]string{"default": "acme/gpt-5"},
|
|
providers: `{"acme":{"class":"openai"}}`,
|
|
bindings: `[{"service":"acme"}]`,
|
|
tokens: map[string]harnesssecret.Secret{},
|
|
check: resolving,
|
|
open: true,
|
|
detail: `provider "acme" has no captured sign-in for credential binding "acme"`,
|
|
},
|
|
{
|
|
name: "api_key_env resolves through the env engine",
|
|
aliases: map[string]string{"default": "openai/gpt-5"},
|
|
providers: `{"openai":{"class":"openai","api_key_env":"GOOD_KEY"}}`,
|
|
check: resolving,
|
|
open: false,
|
|
},
|
|
{
|
|
name: "without the control plane the aliases alone decide",
|
|
aliases: map[string]string{"default": "openai/gpt-5"},
|
|
check: resolving,
|
|
open: false,
|
|
},
|
|
} {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
srv := &Server{Store: setupStore{}, Models: setupModels{aliases: tt.aliases}}
|
|
if tt.providers != "" || tt.bindings != "" {
|
|
srv.ControlPlane = setupControl{providers: tt.providers, bindings: tt.bindings}
|
|
}
|
|
srv.CheckSecret = tt.check
|
|
if tt.tokens != nil {
|
|
srv.HarnessSecrets = setupHarnessSecrets{tokens: tt.tokens}
|
|
}
|
|
step := modelStep(setupRequest(t, srv))
|
|
if !tt.open {
|
|
if step != nil {
|
|
t.Fatalf("Model step = %+v, want it complete (absent)", *step)
|
|
}
|
|
return
|
|
}
|
|
if step == nil {
|
|
t.Fatalf("Model step absent, want it open with detail %q", tt.detail)
|
|
}
|
|
if step.Done {
|
|
t.Fatalf("Model step done, want it open")
|
|
}
|
|
if !strings.Contains(step.Detail, tt.detail) {
|
|
t.Fatalf("Model step detail = %q, want it to contain %q", step.Detail, tt.detail)
|
|
}
|
|
})
|
|
}
|
|
}
|