111 lines
4.5 KiB
Go
111 lines
4.5 KiB
Go
package controlplane
|
|
|
|
import (
|
|
"context"
|
|
"maps"
|
|
"slices"
|
|
"testing"
|
|
|
|
"github.com/samcharles93/archie-core/internal/config"
|
|
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
|
)
|
|
|
|
// resourceStore answers one document per org and kind and reports every other
|
|
// key as absent, the shape a settings read sees on an instance that has never
|
|
// written it.
|
|
type resourceStore map[string]map[string]string
|
|
|
|
func (s resourceStore) Resource(_ context.Context, orgID, kind string) (storecontract.Resource, error) {
|
|
value, ok := s[orgID][kind]
|
|
if !ok {
|
|
return storecontract.Resource{}, storecontract.ErrResourceNotFound
|
|
}
|
|
return storecontract.Resource{OrgID: orgID, Kind: kind, Value: []byte(value), Version: 1}, nil
|
|
}
|
|
|
|
func (resourceStore) ListResources(context.Context) ([]storecontract.Resource, error) {
|
|
return nil, nil
|
|
}
|
|
|
|
func (resourceStore) ResourceHistory(context.Context, string, string, int) ([]storecontract.Resource, error) {
|
|
return nil, nil
|
|
}
|
|
|
|
func (resourceStore) Audit(context.Context, string, []string, int) ([]storecontract.AuditEntry, error) {
|
|
return nil, nil
|
|
}
|
|
|
|
func (resourceStore) PutResource(context.Context, storecontract.ResourceWrite) (storecontract.Resource, error) {
|
|
return storecontract.Resource{}, nil
|
|
}
|
|
|
|
// TestModelsForResolvesTheProvidersItsAliasesName pins what a task builds its
|
|
// model runtime from: an alias is a "provider/model" ref, so a provider the
|
|
// instance configured has to come back beside the alias that names it --
|
|
// otherwise the ref resolves to nothing and no runtime is ever built. The
|
|
// instance's credentials are part of the provider: without them every call is
|
|
// unauthenticated.
|
|
func TestModelsForResolvesTheProvidersItsAliasesName(t *testing.T) {
|
|
store := resourceStore{storecontract.DefaultOrgID: {
|
|
ProviderSettingsKind: `{
|
|
"remote": {"class": "openai-compatible", "base_url": "https://models.example.com/v1", "api_key_ref": {"engine": "vault", "key": "providers/remote"}},
|
|
"local": {"class": "ollama", "api_key_env": "LOCAL_KEY"}
|
|
}`,
|
|
ModelAliasesKind: `{"default": "remote/large", "embedding": "local/embed"}`,
|
|
OrgModelPolicyKind: `{"beta": {"own_providers": true}}`,
|
|
}, "beta": {
|
|
ProviderSettingsKind: `{"own": {"class": "openai-compatible", "base_url": "https://own.example.com/v1", "api_key_ref": {"engine": "vault", "key": "providers/own"}}}`,
|
|
ModelAliasesKind: `{"default": "own/large"}`,
|
|
}}
|
|
tests := []struct {
|
|
name string
|
|
orgID string
|
|
wantAliases []string
|
|
wantProviders []string
|
|
}{
|
|
{name: "the system org holds the instance's aliases and providers", orgID: storecontract.DefaultOrgID, wantAliases: []string{"default", "embedding"}, wantProviders: []string{"local", "remote"}},
|
|
{name: "no org named means the system org", orgID: "", wantAliases: []string{"default", "embedding"}, wantProviders: []string{"local", "remote"}},
|
|
{name: "an org with no policy takes neither the instance's aliases nor its providers", orgID: "acme"},
|
|
{name: "an org allowed its own providers resolves them", orgID: "beta", wantAliases: []string{"default"}, wantProviders: []string{"own"}},
|
|
}
|
|
server := &Server{store: store}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
models, err := server.ModelsFor(t.Context(), tt.orgID)
|
|
if err != nil {
|
|
t.Fatalf("models for %q: %v", tt.orgID, err)
|
|
}
|
|
if got := slices.Sorted(maps.Keys(models.Aliases)); !slices.Equal(got, tt.wantAliases) {
|
|
t.Fatalf("aliases = %v, want %v", got, tt.wantAliases)
|
|
}
|
|
if got := slices.Sorted(maps.Keys(models.Providers)); !slices.Equal(got, tt.wantProviders) {
|
|
t.Fatalf("providers = %v, want %v", got, tt.wantProviders)
|
|
}
|
|
})
|
|
}
|
|
|
|
creds := []struct {
|
|
orgID, provider string
|
|
want config.SecretRef
|
|
}{
|
|
{orgID: storecontract.DefaultOrgID, provider: "remote", want: config.SecretRef{Engine: "vault", Key: "providers/remote"}},
|
|
{orgID: "beta", provider: "own", want: config.SecretRef{Engine: "vault", Key: "providers/own"}},
|
|
}
|
|
for _, c := range creds {
|
|
models, err := server.ModelsFor(t.Context(), c.orgID)
|
|
if err != nil {
|
|
t.Fatalf("models for %q: %v", c.orgID, err)
|
|
}
|
|
if got := models.Providers[c.provider].APIKey; got != c.want {
|
|
t.Fatalf("%s provider %s api_key_ref = %+v, want %+v (its only credential)", c.orgID, c.provider, got, c.want)
|
|
}
|
|
}
|
|
models, err := server.ModelsFor(t.Context(), storecontract.DefaultOrgID)
|
|
if err != nil {
|
|
t.Fatalf("models: %v", err)
|
|
}
|
|
if got := models.Providers["local"]; got.APIKeyEnv != "LOCAL_KEY" || got.Class != "ollama" {
|
|
t.Fatalf("local provider = %+v, want its class and api_key_env", got)
|
|
}
|
|
}
|