Files
archie/internal/app/archieui/degraded_routes_test.go
T
sam 04d4d8f1d8 chore(memory): delete internal/memory (unification slice 5) (#889)
Deletes internal/memory (Manager, builtin.Provider, memory_edit tool) now
that slices 2-4 built the CRUD engine family and its per-turn read/write
seams. Also deletes internal/infrastructure/memory/builtin/provider.go (the
memory_edit adapter over the old package -- its markdown store already
relocated in slice 2), internal/tools/provider/memory (the old tool
registration, redundant with slice 4's memory_create/update/delete/list),
config.Memory.{Provider,ProviderConfig} and the validateMemory rejection
that existed only to complain about Provider, and webui.MemoryStatus/
MemoryProviderHandle plus its dashboard page -- never wired in production
(s.Memory was always nil), confirmed by /api/capabilities always reporting
"memory": false.

Rewrites ARCHITECTURE.md's "Memory engine family" section to describe the
current engine (internal/domain/memory contract, internal/infrastructure/
memory builtin engine + its builtin/ markdown store, the per-turn read seam
in gateway/turn_memory.go, the per-turn write tools in
gateway/turn_memory_tool.go) instead of the deleted Manager. Fixes two
docs/architecture/configuration.md rows that described the deleted
manager's literals and SaveConfig mechanism.
2026-09-19 13:43:16 +10:00

193 lines
6.8 KiB
Go

package archieui
import (
"encoding/json"
"io"
"log/slog"
"net/http"
"net/http/httptest"
"path/filepath"
"strconv"
"strings"
"testing"
"google.golang.org/grpc"
"github.com/samcharles93/archie-core/internal/infrastructure/staterpc"
"github.com/samcharles93/archie-core/internal/logging"
"github.com/samcharles93/archie-core/internal/store"
"github.com/samcharles93/archie-core/internal/webui"
)
// TestRoutesWithoutAContractDegradeExplicitly enumerates every dashboard
// route whose backing capability lives in the daemon and has no contract the
// UI process can reach (archie-core-8cda.5.4 requirement 4).
//
// The enumeration is the point. Each of these is nil in the UI process, and a
// nil that reaches a handler by accident is a panic, a hang, or an empty
// answer indistinguishable from real emptiness. Pinning the response here
// means adding a route without an owner fails this test rather than the
// operator's browser, and gives the SPA a documented status to hide on.
func TestRoutesWithoutAContractDegradeExplicitly(t *testing.T) {
srv, taskID := composeUIProcess(t)
ts := httptest.NewServer(srv.Handler())
defer ts.Close()
for _, tc := range []struct {
name string
method string
path string
body string
want int
// emptyJSON asserts the response carries a well-formed empty
// answer rather than a fabricated one.
emptyJSON string
}{
{
name: "daemon log feed", method: http.MethodGet, path: "/api/logs",
want: http.StatusOK, emptyJSON: `"disabled":true`,
},
{
name: "daemon log stream", method: http.MethodGet, path: "/api/logs/stream",
want: http.StatusServiceUnavailable,
},
{
// Task logs are a State Store read contract now
// (archie-core-iaqx), so the route answers with real data
// rather than the degrade. The task exists and its attempt
// has no log file, which is the found=false answer -- the
// page says the attempt has no log, not that logging is off.
name: "per-task logs", method: http.MethodGet, path: "/api/tasks/" + strconv.FormatInt(taskID, 10) + "/logs",
want: http.StatusOK, emptyJSON: `"found":false`,
},
{
name: "release version and update flow", method: http.MethodGet, path: "/api/version",
want: http.StatusNotImplemented,
},
{
name: "chat update check", method: http.MethodGet, path: "/api/chat/update",
want: http.StatusNotImplemented,
},
{
name: "channel lifecycle", method: http.MethodGet, path: "/api/channels",
want: http.StatusOK, emptyJSON: `[]`,
},
{
name: "channel reload", method: http.MethodPost, path: "/api/channels/telegram/reload",
want: http.StatusNotImplemented,
},
{
name: "curator registry", method: http.MethodGet, path: "/api/curators",
want: http.StatusOK, emptyJSON: `[]`,
},
{
name: "skills catalog", method: http.MethodGet, path: "/api/skills",
want: http.StatusOK, emptyJSON: `[]`,
},
{
// Statistics come from the State Store, the definition list from
// the daemon's registry: the remote half answers, the local half
// is absent, and both are absent-shaped rather than missing keys.
name: "workflow registry", method: http.MethodGet, path: "/api/workflows",
want: http.StatusOK, emptyJSON: `"definitions":null`,
},
{
name: "dashboard work request", method: http.MethodPost, path: "/api/work-requests",
body: `{"repository":"acme/widget","workflow":"implement","title":"do it"}`,
want: http.StatusServiceUnavailable,
},
{
// The capture surfaces are wired in this process from the cutover
// (archie-core-8cda.5.4): the read resolves from the State Store,
// not from a daemon handle, so the inspector answers enabled with
// an empty list rather than the degraded shape.
name: "capture inspector", method: http.MethodGet, path: "/api/captures",
want: http.StatusOK, emptyJSON: `"enabled":true`,
},
{
// The browser reads this to decide which sections to show at
// all, so it is the one route that must answer here.
name: "capabilities", method: http.MethodGet, path: "/api/capabilities",
want: http.StatusOK, emptyJSON: `"logs":false`,
},
} {
t.Run(tc.name, func(t *testing.T) {
var reader *strings.Reader
if tc.body != "" {
reader = strings.NewReader(tc.body)
} else {
reader = strings.NewReader("")
}
req, err := http.NewRequestWithContext(t.Context(), tc.method, ts.URL+tc.path, reader)
if err != nil {
t.Fatalf("request: %v", err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-Archie-CSRF", "1")
resp, err := ts.Client().Do(req)
if err != nil {
t.Fatalf("%s %s: %v", tc.method, tc.path, err)
}
defer resp.Body.Close()
raw, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("read body: %v", err)
}
body := string(raw)
if resp.StatusCode != tc.want {
t.Fatalf("%s %s = %d (%s), want %d", tc.method, tc.path, resp.StatusCode, body, tc.want)
}
if tc.emptyJSON != "" && !strings.Contains(body, tc.emptyJSON) {
t.Fatalf("%s %s body = %s, want it to carry %s", tc.method, tc.path, body, tc.emptyJSON)
}
if resp.StatusCode == http.StatusOK && !json.Valid([]byte(body)) {
t.Fatalf("%s %s returned invalid JSON: %s", tc.method, tc.path, body)
}
})
}
}
// composeUIProcess builds the dashboard exactly as the UI process does, over
// a State Store with nothing published and a Gateway that is not wired at
// all -- the shape every route below has to survive.
func composeUIProcess(t *testing.T) (*webui.Server, int64) {
t.Helper()
st, err := store.Open(t.Context(), filepath.Join(t.TempDir(), "tasks.sqlite"))
if err != nil {
t.Fatalf("open store: %v", err)
}
t.Cleanup(func() { _ = st.Close() })
// The task-log reader lives on the state-directory side of the boundary:
// the State Store process holds a registry over the directory it owns, and
// the dashboard reads through it (archie-core-iaqx). Nothing is written to
// it here, so every attempt reads as found=false.
logs := logging.NewTaskRegistry(filepath.Join(t.TempDir(), "logs", "tasks"), logging.NewFeed(10), logging.TaskSinkOptions{})
target, stop := serveGRPC(t, func(r grpc.ServiceRegistrar) {
staterpc.RegisterServer(r, staterpc.Deps{
Tasks: st, Captures: st, BindingDispatcher: st, ConfigSnapshots: st,
TaskLogs: logs, Log: slog.New(slog.DiscardHandler),
})
})
t.Cleanup(stop)
client, closeClient, err := staterpc.Dial(target, "")
if err != nil {
t.Fatalf("dial state store: %v", err)
}
t.Cleanup(closeClient)
task, err := st.EnqueueChatTask(t.Context(), "acme", "widget", "a task", "body", "implement", "")
if err != nil {
t.Fatalf("seed task: %v", err)
}
options := Options{Listen: "127.0.0.1:0", DependencyTimeout: defaultDependencyTimeout}
return compose(deps{
Options: options,
Log: slog.New(slog.DiscardHandler),
Store: client,
Health: newReadinessRegistry(options, client, nil),
}), task.ID
}