Services was a struct with one field per service, so each service's name was stated three times: the field, the loader's defaulting branch, and every consumer's field access. Adding one meant editing all three, and nothing stopped them disagreeing. The drift was already there: stateStoreResolvedToken and gatewayResolvedToken were identical apart from the env var each hard-coded. Services becomes map[string]ServiceConnection, and RegisterService declares a service once with the five fields it has. Everything that differs between services is now an argument rather than a branch elsewhere: an empty registered target means the operator must supply one, which is how services.state.target stays required while the gateway's is defaulted, and the registered token env var replaces the two copied resolvers with Services.ResolvedToken. Defaulting iterates the registry, so a new service needs no loader edit. Consumers read Services.Get(name). Keeping a typo'd section reported took explicit work. [services.gatway] used to land in toml.MetaData.Undecoded() because the struct had no such field; a map decodes any section, so it would have loaded silently and left the daemon dialing the default. unregisteredServiceKeys asks the registry which names are real, and the test for it fails with an empty UnknownKeys when that check is removed. The TOML shape is unchanged, so no deployment profile, config.example.toml or install.sh edit is needed. Refs: archie-core-u23g
126 lines
4.0 KiB
Go
126 lines
4.0 KiB
Go
package archied
|
|
|
|
import (
|
|
"context"
|
|
"net"
|
|
"testing"
|
|
"time"
|
|
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
|
|
"github.com/samcharles93/archie-core/internal/config"
|
|
"github.com/samcharles93/archie-core/internal/gateway"
|
|
"github.com/samcharles93/archie-core/internal/infrastructure/gatewayrpc"
|
|
"github.com/samcharles93/archie-core/internal/secret"
|
|
)
|
|
|
|
func TestComposeChatContractRejectsInvalidSettings(t *testing.T) {
|
|
for _, tt := range []struct {
|
|
name string
|
|
settings config.Services
|
|
}{
|
|
{name: "missing target", settings: config.Services{}},
|
|
{name: "blank target", settings: config.Services{config.ServiceNameGateway: {Target: " \t\n"}}},
|
|
} {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
chat, cleanup, err := composeChatContract(tt.settings, &secret.Registry{})
|
|
if cleanup != nil {
|
|
t.Cleanup(cleanup)
|
|
}
|
|
if err == nil {
|
|
t.Fatal("invalid settings were accepted")
|
|
}
|
|
if chat != nil || cleanup != nil {
|
|
t.Fatal("invalid settings published an adapter or cleanup")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestComposeChatContractNonLoopbackFailsClosed(t *testing.T) {
|
|
_, _, err := composeChatContract(
|
|
config.Services{config.ServiceNameGateway: {Target: "gateway.example.com:8585"}},
|
|
&secret.Registry{},
|
|
)
|
|
if err == nil {
|
|
t.Fatal("non-loopback target without a token should fail closed")
|
|
}
|
|
}
|
|
|
|
func TestComposeChatContractRemoteAndCleanup(t *testing.T) {
|
|
listener, err := (&net.ListenConfig{}).Listen(t.Context(), "tcp", "127.0.0.1:0")
|
|
if err != nil {
|
|
t.Fatalf("listen: %v", err)
|
|
}
|
|
server := grpc.NewServer()
|
|
gatewayrpc.RegisterServer(server, compositionChat{snapshot: gateway.ChatSnapshot{ActiveModel: "remote-model"}})
|
|
serveDone := make(chan error, 1)
|
|
go func() { serveDone <- server.Serve(listener) }()
|
|
t.Cleanup(func() {
|
|
server.Stop()
|
|
_ = listener.Close()
|
|
if err := <-serveDone; err != nil {
|
|
t.Errorf("serve chat: %v", err)
|
|
}
|
|
})
|
|
|
|
chat, cleanup, err := composeChatContract(
|
|
config.Services{config.ServiceNameGateway: {Target: listener.Addr().String()}},
|
|
&secret.Registry{},
|
|
)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if cleanup == nil {
|
|
t.Fatal("remote composition returned no cleanup")
|
|
}
|
|
t.Cleanup(cleanup)
|
|
if _, ok := chat.(*gatewayrpc.Client); !ok {
|
|
t.Fatalf("remote composition returned %T", chat)
|
|
}
|
|
ctx, cancel := context.WithTimeout(t.Context(), 5*time.Second)
|
|
defer cancel()
|
|
snapshot, err := chat.Snapshot(ctx)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if snapshot.ActiveModel != "remote-model" {
|
|
t.Fatalf("Snapshot used wrong adapter: ActiveModel = %q", snapshot.ActiveModel)
|
|
}
|
|
cleanup()
|
|
if _, err := chat.Snapshot(ctx); status.Code(err) != codes.Canceled {
|
|
t.Fatalf("Snapshot after cleanup: code = %v, want Canceled; error = %v", status.Code(err), err)
|
|
}
|
|
}
|
|
|
|
// TestGatewayResolvedToken pins the daemon-side wiring rather than the
|
|
// precedence rule itself: config.Services.ResolvedToken owns the rule and is
|
|
// tested there, but only this level proves the gateway reads it through the
|
|
// secret registry and lands on the env var its registration names.
|
|
func TestGatewayResolvedToken(t *testing.T) {
|
|
secrets := &secret.Registry{}
|
|
explicit := config.Services{config.ServiceNameGateway: {TargetToken: "file-token"}}
|
|
if got := explicit.ResolvedToken(config.ServiceNameGateway, secrets.Getenv); got != "file-token" {
|
|
t.Fatalf("resolved token = %q, want file-token", got)
|
|
}
|
|
t.Setenv("GATEWAY_TOKEN", "environment-token")
|
|
empty := config.Services{config.ServiceNameGateway: {}}
|
|
if got := empty.ResolvedToken(config.ServiceNameGateway, secrets.Getenv); got != "environment-token" {
|
|
t.Fatalf("resolved token = %q, want environment-token", got)
|
|
}
|
|
if got := explicit.ResolvedToken(config.ServiceNameGateway, secrets.Getenv); got != "file-token" {
|
|
t.Fatalf("explicit target_token should win over the environment, got %q", got)
|
|
}
|
|
}
|
|
|
|
type compositionChat struct {
|
|
gateway.ChatContract
|
|
snapshot gateway.ChatSnapshot
|
|
}
|
|
|
|
func (c compositionChat) Snapshot(context.Context) (gateway.ChatSnapshot, error) {
|
|
return c.snapshot, nil
|
|
}
|