Files
archie/internal/app/archied/state_store_test.go
T
sam 3d611f18ab feat(archieui): standalone UI process over remote Gateway and State Store
Add cmd/archie-ui and internal/app/archieui: a second composition root that
serves the dashboard and the compiled SPA against the two contracts the daemon
already dials, so the UI stops needing the daemon's in-process objects.

The composition root lives outside internal/app/archied deliberately. That
package pulls 88 internal packages (daemon, forge, containers, NATS, secrets,
workflow); reusing its boot the way RunGateway and RunStateStore do would make
the PRD's deletion gate unreachable. archieui links 41, and cmd/archie-ui 42.

Configuration ownership follows docs/prds/ui-service-boundary.md: the process
reads the shared file, but only [services.gateway].{target,target_token},
[services.state].{target,target_token} and [web].{listen,
trust_forwarded_headers}, with flags taking precedence. Options has no field
for anything else in that file to land in, and no config.Holder.

compose() sets only the contract-backed and process-local fields. Every daemon
runtime handle stays nil on purpose and its route degrades through the
existing 501/503/empty paths. Captures, CaptureLimiter and BindingDispatcher
are withheld for the duration of the seam: the daemon's dashboard still serves
POST /webhooks/capture/{source}, and two intakes are not permitted.

Readiness is the UI's own registry over its two dependencies via a new bounded
readiness.ContractProbe. The existing GatewayProbe was unusable here: with no
channel manager to inject it reports "no gateway channels configured", i.e.
permanently degraded.

The State Store's fail-closed non-loopback rule and the two dial paths move to
staterpc.Dial/TargetIsLoopback and gatewayrpc.Dial, so the server side and
every client share one implementation instead of three copies.

The Gateway remains loopback-only with no auth interceptor, so this process is
scoped to being colocated with it; filed as archie-core-juqx.

Refs: archie-core-8cda.5.2
2026-09-08 22:54:26 +10:00

169 lines
5.7 KiB
Go

package archied
import (
"context"
"net"
"path/filepath"
"testing"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"github.com/samcharles93/archie-core/internal/config"
pb "github.com/samcharles93/archie-core/internal/contracts/state/v1"
"github.com/samcharles93/archie-core/internal/infrastructure/staterpc"
"github.com/samcharles93/archie-core/internal/store"
)
func TestStateStoreServerOptsLoopbackIsInsecure(t *testing.T) {
opts, loopback, err := stateStoreServerOpts("127.0.0.1:9090", "", &staterpc.TaskGrants{})
if err != nil {
t.Fatalf("stateStoreServerOpts(loopback, no token) error: %v", err)
}
if !loopback {
t.Fatal("loopback listener should report loopback")
}
if len(opts) != 0 {
t.Fatalf("loopback listener should have no server opts, got %d", len(opts))
}
}
func TestStateStoreServerOptsNonLoopbackRequiresToken(t *testing.T) {
if _, _, err := stateStoreServerOpts("0.0.0.0:9090", "", &staterpc.TaskGrants{}); err == nil {
t.Fatal("non-loopback listener without a token should fail closed")
}
opts, loopback, err := stateStoreServerOpts("0.0.0.0:9090", "secret", &staterpc.TaskGrants{})
if err != nil {
t.Fatalf("non-loopback listener with a token error: %v", err)
}
if loopback {
t.Fatal("non-loopback listener should not report loopback")
}
if len(opts) != 2 {
t.Fatalf("non-loopback listener should install two server options (the unary and stream task-grant interceptors), got %d", len(opts))
}
}
func TestStateStoreServerOptsMalformedListen(t *testing.T) {
if _, _, err := stateStoreServerOpts("not-an-address", "", &staterpc.TaskGrants{}); err == nil {
t.Fatal("malformed listen address should error")
}
}
func TestConstantTimeTokenValidator(t *testing.T) {
validate := constantTimeTokenValidator("correct-token")
if !validate("correct-token") {
t.Fatal("validator should accept the configured token")
}
if validate("wrong-token") {
t.Fatal("validator should reject a different token")
}
if validate("") {
t.Fatal("validator should reject an empty token")
}
}
// TestServeStateStoreServesContract verifies the standalone composition's
// serveStateStore + stateStoreDeps wiring actually serves the StateStore gRPC
// contract over a real loopback listener: the same service .4.2 serves
// in-process, now extracted into its own process. A read-only StatusCounts
// call proves the service is registered and wired to the opened store.
func TestServeStateStoreServesContract(t *testing.T) {
dir := t.TempDir()
st, err := store.Open(t.Context(), filepath.Join(dir, "tasks.sqlite"))
if err != nil {
t.Fatalf("open temp store: %v", err)
}
defer st.Close()
b := newBootstrap()
b.st = st
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
listener, err := (&net.ListenConfig{}).Listen(ctx, "tcp", "127.0.0.1:0")
if err != nil {
t.Fatalf("listen: %v", err)
}
defer listener.Close()
serveCh := make(chan error, 1)
go func() { serveCh <- serveStateStore(ctx, listener, b.stateStoreDeps(&staterpc.TaskGrants{}), nil) }()
conn, err := grpc.NewClient("passthrough:///"+listener.Addr().String(), grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
t.Fatalf("dial: %v", err)
}
defer conn.Close()
client := pb.NewStateStoreServiceClient(conn)
if _, err := client.StatusCounts(t.Context(), &pb.StatusCountsRequest{}); err != nil {
t.Fatalf("StatusCounts over gRPC: %v", err)
}
cancel()
select {
case err := <-serveCh:
if err != nil {
t.Fatalf("serveStateStore returned: %v", err)
}
case <-t.Context().Done():
t.Fatal("serveStateStore did not return after cancel")
}
}
// TestStateStoreDataSurvivesRestart is the .4.7 restart/recovery check: a
// task written by one archie-state-store process must still be there for a
// fresh process opening the same archie.db path, proving b.openStateStore
// (RunStateStore's own opener, not a test-only shortcut) round-trips through
// the file rather than an in-memory or per-process store.
func TestStateStoreDataSurvivesRestart(t *testing.T) {
cfg := config.Config{DBPath: filepath.Join(t.TempDir(), "archie")}
first := newBootstrap()
first.cfg = cfg
if err := first.openStateStore(t.Context()); err != nil {
t.Fatalf("first openStateStore: %v", err)
}
task, err := first.st.EnqueueChatTask(t.Context(), "acme", "widget", "restart check", "body", "implement", "")
if err != nil {
t.Fatalf("EnqueueChatTask: %v", err)
}
first.cleanup() // closes the SQLite handle, simulating process exit
second := newBootstrap()
second.cfg = cfg
if err := second.openStateStore(t.Context()); err != nil {
t.Fatalf("second openStateStore: %v", err)
}
defer second.cleanup()
got, err := second.st.TaskByID(t.Context(), task.ID)
if err != nil {
t.Fatalf("TaskByID after restart: %v", err)
}
if got == nil {
t.Fatal("task did not survive a restart against the same db path")
}
if got.Title != task.Title {
t.Fatalf("restarted task title = %q, want %q", got.Title, task.Title)
}
}
// TestOpenStoresNeverOwnsTaskDB is the .4.7 single-owner-SQLite assertion:
// the daemon/gateway composition path (openStores, run by Run/RunGateway)
// must never populate b.st -- that field is reserved for the standalone
// archie-state-store binary's openStateStore (state_store.go). Regressing
// this would mean archie.db is opened by two processes at once.
func TestOpenStoresNeverOwnsTaskDB(t *testing.T) {
b := newBootstrap()
b.cfg = config.Config{DBPath: filepath.Join(t.TempDir(), "archie")}
if err := b.openStores(t.Context()); err != nil {
t.Fatalf("openStores: %v", err)
}
defer b.cleanup()
if b.st != nil {
t.Fatal("openStores populated b.st -- the daemon/gateway path must not own archie.db directly (docs/prds/state-store-contract.md §12 step 7)")
}
}