Files
archie/internal/webui/remote_store_test.go
T
sam ae552db7e0 refactor(store)!: remove the SQLite task store, the PocketBase event store and the SQLite session store
The State Store, event capture and Gateway conversations already serve from
PostgreSQL. This deletes internal/store and internal/infrastructure/edastore,
the SQLite gateway session store and openProductionTaskStore, and the -db
recovery path on a legacy task file; backup, restore, validate and rollback
work on the database database_url names.

- The binding-secret cipher moves to internal/infrastructure/bindingcipher and
  ToolCall to internal/infrastructure/postgres.
- NewSessionStoreMemory is now a pure in-memory store that passes the shared
  SessionStore conformance suite.
- Tests run against PostgreSQL through internal/infrastructure/postgres/pgstore;
  legacyimport/legacyfixture writes the legacy files the import and the boot
  gate read.
- The cron dialect is owned in internal/domain/scheduling, so
  github.com/pocketbase/pocketbase and pocketbase/dbx leave go.mod.
  modernc.org/sqlite stays for the one-time import (legacyread).
- The UI and Messaging architecture gates ban the Postgres and legacy readers
  in place of internal/store.
2026-09-24 14:43:50 +10:00

166 lines
6.0 KiB
Go

package webui
// Remote-contract coverage for the dashboard data paths. The production UI
// process (archie-ui, and the daemon's webui) serves task, capture, mapping
// and binding routes against a *staterpc.Client — the remote read contract —
// not a concrete *pgstore.TaskDB. Every other webui test feeds a local store
// directly, so none of them prove the handlers work once the data crosses the
// gRPC wire. These do: they stand up a real SQLite store behind
// staterpc.RegisterServer, dial it, and assert the handlers serve correctly.
import (
"context"
"log/slog"
"net"
"net/http"
"net/http/httptest"
"strconv"
"strings"
"testing"
"google.golang.org/grpc"
"github.com/samcharles93/archie-core/internal/domain/workflow"
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore"
"github.com/samcharles93/archie-core/internal/infrastructure/staterpc"
)
// newRemoteTestServer serves a real SQLite store behind a gRPC listener and
// returns a webui.Server whose store fields are all the remote *staterpc.Client.
// This is the composition the UI Service uses; it exercises the full
// round-trip (handler -> narrow interface -> client -> wire -> server -> store)
// rather than a fake.
func newRemoteTestServer(t *testing.T) *Server {
t.Helper()
st := pgstore.Open(t)
t.Cleanup(func() { _ = st.Close() })
listener, err := (&net.ListenConfig{}).Listen(t.Context(), "tcp", "127.0.0.1:0")
if err != nil {
t.Fatalf("listen: %v", err)
}
server := grpc.NewServer()
// Two stores, as in the real composition: the task store serves the task
// surfaces, the event-capture store serves captures/mappings/bindings.
// Leaving one nil makes ListMappings/ListBindings fail closed with
// err*Unavailable, which is also the real wiring.
eda := pgstore.EDA(t, nil)
staterpc.RegisterServer(server, staterpc.Deps{
Tasks: st,
Captures: eda,
Mappings: eda,
Bindings: eda,
BindingDispatcher: eda,
BindingTaskCreator: st,
Log: slog.New(slog.DiscardHandler),
})
go func() { _ = server.Serve(listener) }()
t.Cleanup(server.Stop)
client, closeConn, err := staterpc.Dial(listener.Addr().String(), "")
if err != nil {
t.Fatalf("dial state store: %v", err)
}
t.Cleanup(closeConn)
return &Server{
Store: client,
Captures: client,
Mappings: client,
Bindings: client,
Log: slog.New(slog.DiscardHandler),
CaptureMaxEvents: 10,
}
}
func TestRemoteEndpointFamiliesServeOverReadContracts(t *testing.T) {
srv := newRemoteTestServer(t)
ctx := context.Background()
t.Run("summary", func(t *testing.T) {
rr := httptest.NewRecorder()
srv.handleSummary(rr, httptest.NewRequestWithContext(ctx, http.MethodGet, "/api/summary", nil))
if rr.Code != http.StatusOK {
t.Fatalf("GET /api/summary = %d, body %s", rr.Code, rr.Body.String())
}
})
t.Run("tasks_and_timeline", func(t *testing.T) {
if _, err := srv.Store.EnqueueIssue(ctx, "acme", "widget", 1, "remote task", "", "", ""); err != nil {
t.Fatalf("enqueue: %v", err)
}
task, err := srv.Store.ClaimNext(ctx)
if err != nil || task == nil {
t.Fatalf("claim (%v, %v)", task, err)
}
if err := srv.Store.Transition(ctx, task.ID, workflow.StatusRunning, workflow.StatusWaitingHuman, "needs info"); err != nil {
t.Fatalf("transition: %v", err)
}
rr := httptest.NewRecorder()
srv.handleTasks(rr, httptest.NewRequestWithContext(ctx, http.MethodGet, "/api/tasks", nil))
if rr.Code != http.StatusOK {
t.Fatalf("GET /api/tasks = %d, body %s", rr.Code, rr.Body.String())
}
if !strings.Contains(rr.Body.String(), "remote task") {
t.Fatalf("task not in response: %s", rr.Body.String())
}
// The {id} route reads the task's events over the wire. handleTask
// reads r.PathValue("id"), which only the mux populates, so this goes
// through Server.Handler() rather than a direct handler call — the
// realistic path and the one that exercises routing too.
handler := srv.Handler()
rr2 := httptest.NewRecorder()
handler.ServeHTTP(rr2, httptest.NewRequestWithContext(ctx, http.MethodGet, "/api/tasks/"+strconv.FormatInt(task.ID, 10), nil))
if rr2.Code != http.StatusOK {
t.Fatalf("GET /api/tasks/{id} = %d, body %s", rr2.Code, rr2.Body.String())
}
})
t.Run("mappings", func(t *testing.T) {
rr := httptest.NewRecorder()
srv.handleMappingsList(rr, httptest.NewRequestWithContext(ctx, http.MethodGet, "/api/mappings", nil))
if rr.Code != http.StatusOK {
t.Fatalf("GET /api/mappings = %d, body %s", rr.Code, rr.Body.String())
}
})
t.Run("bindings", func(t *testing.T) {
rr := httptest.NewRecorder()
srv.handleBindingsList(rr, httptest.NewRequestWithContext(ctx, http.MethodGet, "/api/bindings", nil))
if rr.Code != http.StatusOK {
t.Fatalf("GET /api/bindings = %d, body %s", rr.Code, rr.Body.String())
}
})
}
// TestRemoteLogAndHealthDegrade covers the two paths with no State Store
// contract: logs (in-process feed, nil in a remote composition) report
// disabled, and health consults the local registry (nil here) rather than a
// concrete store. Both are deliberate degradation, not gaps to fill by
// inventing a contract.
func TestRemoteLogAndHealthDegrade(t *testing.T) {
srv := newRemoteTestServer(t)
ctx := context.Background()
t.Run("logs_disabled", func(t *testing.T) {
rr := httptest.NewRecorder()
srv.handleLogs(rr, httptest.NewRequestWithContext(ctx, http.MethodGet, "/api/logs", nil))
if rr.Code != http.StatusOK {
t.Fatalf("GET /api/logs = %d, body %s", rr.Code, rr.Body.String())
}
if !strings.Contains(rr.Body.String(), "disabled") {
t.Fatalf("expected logs to report disabled, got %s", rr.Body.String())
}
})
t.Run("health_without_registry", func(t *testing.T) {
rr := httptest.NewRecorder()
srv.handleHealthDetailed(rr, httptest.NewRequestWithContext(ctx, http.MethodGet, "/health/detailed", nil))
if rr.Code != http.StatusOK && rr.Code != http.StatusServiceUnavailable {
t.Fatalf("GET /health/detailed = %d, body %s", rr.Code, rr.Body.String())
}
})
}