Files
archie/internal/webui/api_curators_test.go
T
sam a00c578cb7 refactor(ui): sever the archie-ui binary from every daemon runtime package
Completes the objective deletion gate for the UI Service
(archie-core-8cda.5.6): go list -deps ./cmd/archie-ui now links zero
banned packages -- no modernc.org/sqlite, no internal/store, gateway,
channels, memory, secret, agentexec, tools, skill, yaegiutil, curator,
or the workflow engine. The closure shrinks from 68 to 26 archie-core
packages. Each cut is a producer-side move; no consumer-side defensive
shims.

- Store contracts (TaskStore family, value types, sentinels) moved from
  internal/store to internal/domain/storecontract -- the approved contract
  location per state-store-contract rev. 2d, and NOT internal/contracts:
  that tree is buf generate output and tools/proto.sh check deletes
  hand-written files there. internal/store keeps aliases; sentinel
  message strings are byte-identical and remain the wire contract.
- Chat contracts (ChatContract, sessions, turns, streaming, commands)
  moved from internal/gateway to internal/domain/messaging; gateway keeps
  aliases. ToolCallEvent rendering helpers become gateway functions
  (methods cannot live on alias types).
- Task vocabulary (Task, statuses, mid-run Store, Definition) split into
  internal/domain/workflow/task; the pipeline engine stays in package
  workflow with aliases, so daemon-side callers are unchanged.
- overlay.DeniedKeys moved to internal/infrastructure/configuration:
  webui referenced the constant and that dragged the overlay SQLite
  store in.
- webui.Server's concrete runtime handles become webui-owned view
  interfaces with daemon-side bootstrap adapters: channel lifecycle
  (internal/channels/status), MemoryStatus, CuratorStatus, and a
  SkillCatalog seam replacing the direct skill.DefaultRoots/CatalogRoots
  calls. Each degrades to an empty page when unwired.
- config.SecretRef moved into internal/config (config vocabulary);
  internal/secret keeps an alias and Registry.Resolve absorbs the
  zero-value short-circuit.

Docs: state-store-contract rev. 2d relocation record; ui-service-boundary
deletion gate marked satisfied; ARCHITECTURE.md package map rows.
2026-09-10 03:03:33 +10:00

178 lines
6.0 KiB
Go

package webui
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/samcharles93/archie-core/internal/domain/curator"
)
type stubCuratorEngine struct {
name string
manifest curator.Manifest
health curator.Health
}
func (c *stubCuratorEngine) Name() string { return c.name }
func (c *stubCuratorEngine) Version() string { return "v1" }
func (c *stubCuratorEngine) Manifest() curator.Manifest { return c.manifest }
func (c *stubCuratorEngine) Bind(curator.Registrar) {}
func (c *stubCuratorEngine) Start(context.Context) error { return nil }
func (c *stubCuratorEngine) Stop(context.Context) error { return nil }
func (c *stubCuratorEngine) Health(context.Context) curator.Health { return c.health }
func (c *stubCuratorEngine) Check(context.Context) (bool, error) { return false, nil }
func (c *stubCuratorEngine) Pass(context.Context, curator.PassInput) (curator.PassResult, error) {
return curator.PassResult{}, nil
}
func TestHandleCuratorsNilRegistryReturnsEmpty(t *testing.T) {
srv := newTestServer(t)
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/api/curators", nil))
if w.Code != http.StatusOK {
t.Fatalf("status = %d, body = %s", w.Code, w.Body)
}
var response struct {
Curators []CuratorView `json:"curators"`
}
if err := json.Unmarshal(w.Body.Bytes(), &response); err != nil {
t.Fatal(err)
}
if len(response.Curators) != 0 {
t.Fatalf("curators = %#v, want empty", response.Curators)
}
}
func TestHandleCuratorsReportsNamesHealthAndActivity(t *testing.T) {
registry := curator.NewRegistry(curator.Registrar{})
c := &stubCuratorEngine{
name: "skill-curator",
manifest: curator.Manifest{Interval: time.Minute},
health: curator.Health{Status: curator.HealthHealthy, Message: "ok"},
}
if err := registry.Register(c); err != nil {
t.Fatal(err)
}
at := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)
registry.RecordActivity("skill-curator", at, []curator.Action{
{At: at, Type: "skill.updated", Detail: "rewrote foo", Reason: "stale content"},
})
srv := newTestServer(t)
srv.Curators = curatorStatusAdapter{registry}
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/api/curators", nil))
if w.Code != http.StatusOK {
t.Fatalf("status = %d, body = %s", w.Code, w.Body)
}
var response struct {
Curators []CuratorView `json:"curators"`
}
if err := json.Unmarshal(w.Body.Bytes(), &response); err != nil {
t.Fatal(err)
}
if len(response.Curators) != 1 {
t.Fatalf("curators = %#v, want 1", response.Curators)
}
got := response.Curators[0]
if got.Name != "skill-curator" {
t.Fatalf("Name = %q, want skill-curator", got.Name)
}
if got.Health.Status != string(curator.HealthHealthy) || got.Health.Message != "ok" {
t.Fatalf("Health = %#v", got.Health)
}
if got.LastRunAt == nil || !got.LastRunAt.Equal(at) {
t.Fatalf("LastRunAt = %v, want %v", got.LastRunAt, at)
}
if got.LastRunActions != 1 {
t.Fatalf("LastRunActions = %d, want 1", got.LastRunActions)
}
if len(got.RecentActions) != 1 || got.RecentActions[0].Type != "skill.updated" || got.RecentActions[0].Reason != "stale content" {
t.Fatalf("RecentActions = %#v", got.RecentActions)
}
}
func TestHandleCuratorsReportsRegisteredCuratorWithNoActivityYet(t *testing.T) {
registry := curator.NewRegistry(curator.Registrar{})
c := &stubCuratorEngine{
name: "never-run",
manifest: curator.Manifest{Interval: time.Minute},
health: curator.Health{Status: curator.HealthHealthy},
}
if err := registry.Register(c); err != nil {
t.Fatal(err)
}
srv := newTestServer(t)
srv.Curators = curatorStatusAdapter{registry}
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/api/curators", nil))
if w.Code != http.StatusOK {
t.Fatalf("status = %d, body = %s", w.Code, w.Body)
}
var response struct {
Curators []CuratorView `json:"curators"`
}
if err := json.Unmarshal(w.Body.Bytes(), &response); err != nil {
t.Fatal(err)
}
if len(response.Curators) != 1 {
t.Fatalf("curators = %#v, want 1", response.Curators)
}
if got := response.Curators[0]; got.LastRunAt != nil || got.LastRunActions != 0 || len(got.RecentActions) != 0 {
t.Fatalf("expected zero-value activity for a never-run curator, got %#v", got)
}
// Regression guard: time.Time's zero value is not "empty" to
// encoding/json, so a naive time.Time field would serialize as
// "0001-01-01T00:00:00Z" (a truthy string on the frontend) instead of
// being omitted. Assert the raw JSON, not just the unmarshalled Go
// value, so a future revert of the *time.Time fix is caught here.
if body := w.Body.String(); strings.Contains(body, "0001-01-01") {
t.Fatalf("response leaks zero-value time.Time: %s", body)
}
}
// curatorStatusAdapter adapts the real registry to the webui-owned
// CuratorStatus view, mirroring the daemon's bootstrap adapter.
type curatorStatusAdapter struct{ r *curator.Registry }
func (a curatorStatusAdapter) Names() []string { return a.r.Names() }
func (a curatorStatusAdapter) Health(ctx context.Context) map[string]CuratorHealthView {
health := a.r.Health(ctx)
views := make(map[string]CuratorHealthView, len(health))
for name, h := range health {
views[name] = CuratorHealthView{Status: string(h.Status), Message: h.Message}
}
return views
}
func (a curatorStatusAdapter) Activity(name string) (CuratorActivity, bool) {
activity, ok := a.r.Activity(name)
if !ok {
return CuratorActivity{}, false
}
view := CuratorActivity{
LastRunAt: activity.LastRunAt,
LastRunActions: activity.LastRunActions,
Recent: make([]CuratorActionView, 0, len(activity.Recent)),
}
for _, a2 := range activity.Recent {
view.Recent = append(view.Recent, CuratorActionView{
At: a2.At, Type: a2.Type, Detail: a2.Detail, Reason: a2.Reason,
})
}
return view, true
}