Files
archie/internal/gateway/gateway_test.go
T
sam f73b2d1c8c feat: memory manager sync/prefetch/scan/shutdown + broad in-flight epic work
Checkpoint commit covering concurrent work across several archie-core-abg
epics to avoid losing in-progress state:

- Memory manager (18.7-18.16): background sync pipeline, prefetch with
  timeout, memory-write notification, threat scanning, shutdown drain
  with abandoned-write tracking, frozen snapshot pattern. Wired
  ScanContent and NotifyMemoryToolWrite into HandleToolCall so scanning
  and notification actually apply to writes, not just exist as unused
  primitives.
- Fixed a literal unresolved merge-conflict that had been committed to
  internal/memory/scrubber/scrubber.go.
- Tool provider/registry, MCP client/transport (stdio, HTTP, SSE),
  multimodal results, disclosure.
- Pairing device auth store, rate limiting.
- Gateway compression, session store, tasks.
- Various workflow, plugin host, and agentexec updates.
2026-07-27 03:34:46 +10:00

611 lines
17 KiB
Go

package gateway
import (
"context"
"errors"
"fmt"
"slices"
"strings"
"testing"
)
type fakeStore struct {
counts map[string]int
err error
}
func (f *fakeStore) StatusCounts(ctx context.Context) (map[string]int, error) {
return f.counts, f.err
}
func fakeLLM(ctx context.Context, msg Message) (string, error) {
return "llm: " + msg.Text, nil
}
func TestRouteStatusWithTasks(t *testing.T) {
r := NewRouter(&fakeStore{counts: map[string]int{"queued": 2, "pr_open": 1}}, nil, "test")
reply, err := r.Route(context.Background(), Message{Text: "/status"})
if err != nil {
t.Fatalf("Route: %v", err)
}
if !strings.Contains(reply, "queued: 2") || !strings.Contains(reply, "pr_open: 1") {
t.Errorf("reply missing counts: %q", reply)
}
}
func TestRouteStatusAtMention(t *testing.T) {
r := NewRouter(&fakeStore{counts: map[string]int{"running": 3}}, nil, "archie")
reply, err := r.Route(context.Background(), Message{Text: "/status@archie"})
if err != nil {
t.Fatalf("Route: %v", err)
}
if !strings.Contains(reply, "running: 3") {
t.Errorf("reply missing counts: %q", reply)
}
}
func TestRouteStatusEmpty(t *testing.T) {
r := NewRouter(&fakeStore{counts: map[string]int{}}, nil, "test")
reply, err := r.Route(context.Background(), Message{Text: "/status"})
if err != nil {
t.Fatalf("Route: %v", err)
}
if reply != "No tasks yet." {
t.Errorf("reply = %q, want %q", reply, "No tasks yet.")
}
}
func TestRouteStatusError(t *testing.T) {
r := NewRouter(&fakeStore{err: errors.New("db down")}, nil, "test")
if _, err := r.Route(context.Background(), Message{Text: "/status"}); err == nil {
t.Error("expected error from Route when StatusCounts fails")
}
}
func TestRouteNonCommandWithLLM(t *testing.T) {
r := NewRouter(nil, fakeLLM, "test")
reply, err := r.Route(context.Background(), Message{Text: "hello"})
if err != nil {
t.Fatalf("Route: %v", err)
}
if reply != "llm: hello" {
t.Errorf("reply = %q, want %q", reply, "llm: hello")
}
}
func TestRouteNonCommandWithoutLLM(t *testing.T) {
r := NewRouter(nil, nil, "test")
reply, err := r.Route(context.Background(), Message{Text: "hello"})
if err != nil {
t.Fatalf("Route: %v", err)
}
if !strings.Contains(reply, "/status") {
t.Errorf("reply = %q, want mention of /status", reply)
}
}
func TestRouterRejectsUnknownCommand(t *testing.T) {
r := NewRouter(nil, nil, "test")
reply, err := r.Route(context.Background(), Message{Text: "/frobnicate"})
if err != nil {
t.Fatalf("Route: %v", err)
}
if !strings.Contains(reply, "/status") {
t.Errorf("reply = %q, want mention of /status", reply)
}
}
// -- ModelManager fakes for /model and /models tests --
type fakeModelManager struct {
models []string
activeModel string
setErr error
}
func (f *fakeModelManager) Models() []string {
return f.models
}
func (f *fakeModelManager) ActiveModel() string {
return f.activeModel
}
func (f *fakeModelManager) SetActiveModel(_ context.Context, ref string) error {
if f.setErr != nil {
return f.setErr
}
if slices.Contains(f.models, ref) {
f.activeModel = ref
return nil
}
return fmt.Errorf("unknown model: %s", ref)
}
// -- /models tests --
func TestRouteModels(t *testing.T) {
mgr := &fakeModelManager{
models: []string{"a/b", "c/d"},
}
r := NewRouter(nil, nil, "test-gw")
r.Models = mgr
reply, err := r.Route(context.Background(), Message{Text: "/models"})
if err != nil {
t.Fatalf("Route: %v", err)
}
if !strings.Contains(reply, "a/b") || !strings.Contains(reply, "c/d") {
t.Errorf("reply missing model names: %q", reply)
}
if strings.Contains(reply, "active") {
t.Errorf("reply should not flag an active model when none is set: %q", reply)
}
}
func TestRouteModelsWithActive(t *testing.T) {
mgr := &fakeModelManager{
models: []string{"a/b", "c/d"},
activeModel: "c/d",
}
r := NewRouter(nil, nil, "test-gw")
r.Models = mgr
reply, err := r.Route(context.Background(), Message{Text: "/models"})
if err != nil {
t.Fatalf("Route: %v", err)
}
if !strings.Contains(reply, "a/b") || !strings.Contains(reply, "c/d (active)") {
t.Errorf("reply should flag active model: %q", reply)
}
}
func TestRouteModelsAtMention(t *testing.T) {
mgr := &fakeModelManager{
models: []string{"a/b"},
}
r := NewRouter(nil, nil, "test-gw")
r.Models = mgr
reply, err := r.Route(context.Background(), Message{Text: "/models@test-gw"})
if err != nil {
t.Fatalf("Route: %v", err)
}
if !strings.Contains(reply, "a/b") {
t.Errorf("reply missing model names: %q", reply)
}
}
func TestRouteModelsEmpty(t *testing.T) {
mgr := &fakeModelManager{}
r := NewRouter(nil, nil, "test-gw")
r.Models = mgr
reply, err := r.Route(context.Background(), Message{Text: "/models"})
if err != nil {
t.Fatalf("Route: %v", err)
}
if !strings.Contains(reply, "No models") {
t.Errorf("reply = %q, want 'No models' message", reply)
}
}
func TestRouteModelsNoManager(t *testing.T) {
r := NewRouter(nil, nil, "test-gw")
reply, err := r.Route(context.Background(), Message{Text: "/models"})
if err != nil {
t.Fatalf("Route: %v", err)
}
if !strings.Contains(reply, "not configured") {
t.Errorf("reply = %q, want 'not configured' message", reply)
}
}
// -- /model tests --
func TestRouteModelSwitch(t *testing.T) {
mgr := &fakeModelManager{
models: []string{"a/b", "c/d"},
}
r := NewRouter(nil, nil, "test-gw")
r.Models = mgr
reply, err := r.Route(context.Background(), Message{Text: "/model a/b"})
if err != nil {
t.Fatalf("Route: %v", err)
}
if !strings.Contains(reply, "a/b") || !strings.Contains(reply, "set to") {
t.Errorf("reply should confirm model switch: %q", reply)
}
if mgr.activeModel != "a/b" {
t.Errorf("active model = %q, want %q", mgr.activeModel, "a/b")
}
}
func TestRouteModelAtMention(t *testing.T) {
mgr := &fakeModelManager{
models: []string{"a/b"},
}
r := NewRouter(nil, nil, "test-gw")
r.Models = mgr
reply, err := r.Route(context.Background(), Message{Text: "/model@test-gw a/b"})
if err != nil {
t.Fatalf("Route: %v", err)
}
if !strings.Contains(reply, "a/b") {
t.Errorf("reply should confirm model switch: %q", reply)
}
}
func TestRouteModelUnknown(t *testing.T) {
mgr := &fakeModelManager{
models: []string{"a/b"},
}
r := NewRouter(nil, nil, "test-gw")
r.Models = mgr
reply, err := r.Route(context.Background(), Message{Text: "/model unknown/unknown"})
if err != nil {
t.Fatalf("Route: %v", err)
}
if !strings.Contains(reply, "Cannot switch") {
t.Errorf("reply should mention cannot switch: %q", reply)
}
}
func TestRouteModelNoArg(t *testing.T) {
mgr := &fakeModelManager{
models: []string{"a/b", "c/d"},
}
r := NewRouter(nil, nil, "test-gw")
r.Models = mgr
reply, err := r.Route(context.Background(), Message{Text: "/model"})
if err != nil {
t.Fatalf("Route: %v", err)
}
if !strings.Contains(reply, "Usage") || !strings.Contains(reply, "a/b") || !strings.Contains(reply, "c/d") {
t.Errorf("reply should show usage and available models: %q", reply)
}
}
func TestRouteModelNoManager(t *testing.T) {
r := NewRouter(nil, nil, "test-gw")
reply, err := r.Route(context.Background(), Message{Text: "/model a/b"})
if err != nil {
t.Fatalf("Route: %v", err)
}
if !strings.Contains(reply, "not configured") {
t.Errorf("reply = %q, want 'not configured' message", reply)
}
}
// -- TaskCreator fakes for /spawn tests --
type fakeTaskCreator struct {
tasks []string
requests []SpawnRequest
createFn func(ctx context.Context, req SpawnRequest) (int64, error)
}
func (f *fakeTaskCreator) CreateTask(ctx context.Context, req SpawnRequest) (int64, error) {
f.tasks = append(f.tasks, req.Title)
f.requests = append(f.requests, req)
if f.createFn != nil {
return f.createFn(ctx, req)
}
return int64(len(f.tasks)), nil
}
func TestRouteSpawnCreatesTask(t *testing.T) {
tc := &fakeTaskCreator{}
r := NewRouter(nil, nil, "test-gw")
r.Tasks = tc
reply, err := r.Route(context.Background(), Message{Text: "/spawn Fix the login bug"})
if err != nil {
t.Fatalf("Route: %v", err)
}
if !strings.Contains(reply, "Created task") || !strings.Contains(reply, "1") {
t.Errorf("reply should confirm task creation: %q", reply)
}
if len(tc.tasks) != 1 || tc.tasks[0] != "Fix the login bug" {
t.Errorf("tasks = %v, want [Fix the login bug]", tc.tasks)
}
}
func TestRouteSpawnAtMention(t *testing.T) {
tc := &fakeTaskCreator{}
r := NewRouter(nil, nil, "archie-bot")
r.Tasks = tc
reply, err := r.Route(context.Background(), Message{Text: "/spawn@archie-bot Deploy the release"})
if err != nil {
t.Fatalf("Route: %v", err)
}
if !strings.Contains(reply, "Created task") {
t.Errorf("reply should confirm creation: %q", reply)
}
}
func TestRouteSpawnNoTitle(t *testing.T) {
tc := &fakeTaskCreator{}
r := NewRouter(nil, nil, "test-gw")
r.Tasks = tc
reply, err := r.Route(context.Background(), Message{Text: "/spawn"})
if err != nil {
t.Fatalf("Route: %v", err)
}
if !strings.Contains(reply, "Usage") {
t.Errorf("reply should show usage: %q", reply)
}
if len(tc.tasks) != 0 {
t.Errorf("no task should be created without a title")
}
}
func TestRouteSpawnNoManager(t *testing.T) {
r := NewRouter(nil, nil, "test-gw")
reply, err := r.Route(context.Background(), Message{Text: "/spawn something"})
if err != nil {
t.Fatalf("Route: %v", err)
}
if !strings.Contains(reply, "not configured") {
t.Errorf("reply = %q, want 'not configured'", reply)
}
}
func TestRouteSpawnHandlesCreationError(t *testing.T) {
tc := &fakeTaskCreator{
createFn: func(ctx context.Context, req SpawnRequest) (int64, error) {
return 0, fmt.Errorf("store unavailable")
},
}
r := NewRouter(nil, nil, "test-gw")
r.Tasks = tc
reply, err := r.Route(context.Background(), Message{Text: "/spawn Break things"})
if err != nil {
t.Fatalf("Route: %v", err)
}
if !strings.Contains(reply, "Failed") {
t.Errorf("reply should mention failure: %q", reply)
}
}
func TestRouteSpawnParsesRepoAndWorkflow(t *testing.T) {
tc := &fakeTaskCreator{}
r := NewRouter(nil, nil, "test-gw")
r.Tasks = tc
r.Identity = "archie"
reply, err := r.Route(context.Background(), Message{Text: "/spawn repo=sam/tau workflow=tdd Fix the flaky test"})
if err != nil {
t.Fatalf("Route: %v", err)
}
if !strings.Contains(reply, "Created task") {
t.Errorf("reply = %q, want task creation confirmation", reply)
}
if len(tc.requests) != 1 {
t.Fatalf("requests = %d, want 1", len(tc.requests))
}
got := tc.requests[0]
if got.Repo != "sam/tau" || got.Workflow != "tdd" || got.Title != "Fix the flaky test" || got.Identity != "archie" {
t.Errorf("request = %+v, want {Repo:sam/tau Workflow:tdd Title:\"Fix the flaky test\" Identity:archie}", got)
}
}
func TestRouteSpawnParsesIdentity(t *testing.T) {
tc := &fakeTaskCreator{}
r := NewRouter(nil, nil, "test-gw")
r.Tasks = tc
r.Identity = "default"
reply, err := r.Route(context.Background(), Message{
Text: "/spawn identity=reviewer repo=sam/tau workflow=tdd Fix the flaky test",
})
if err != nil {
t.Fatalf("Route: %v", err)
}
if !strings.Contains(reply, "Created task") {
t.Errorf("reply = %q, want task creation confirmation", reply)
}
got := tc.requests[0]
if got.Identity != "reviewer" || got.Repo != "sam/tau" || got.Workflow != "tdd" || got.Title != "Fix the flaky test" {
t.Errorf("request = %+v, want selected identity, repo, workflow, and full title", got)
}
}
func TestRouteSpawnWithoutRepoOrWorkflowUsesDefaults(t *testing.T) {
tc := &fakeTaskCreator{}
r := NewRouter(nil, nil, "test-gw")
r.Tasks = tc
reply, err := r.Route(context.Background(), Message{Text: "/spawn Fix the login bug"})
if err != nil {
t.Fatalf("Route: %v", err)
}
if !strings.Contains(reply, "Created task") {
t.Errorf("reply = %q", reply)
}
got := tc.requests[0]
if got.Repo != "" || got.Workflow != "" || got.Title != "Fix the login bug" {
t.Errorf("request = %+v, want empty Repo/Workflow and full title", got)
}
}
func TestRouteSpawnTitleLooksLikeKeyValueIsPreserved(t *testing.T) {
// A title that happens to contain "=" after the repo=/workflow=
// tokens must not be mistaken for another key=value token.
tc := &fakeTaskCreator{}
r := NewRouter(nil, nil, "test-gw")
r.Tasks = tc
if _, err := r.Route(context.Background(), Message{Text: "/spawn repo=sam/tau Fix x=y in config"}); err != nil {
t.Fatalf("Route: %v", err)
}
got := tc.requests[0]
if got.Repo != "sam/tau" || got.Title != "Fix x=y in config" {
t.Errorf("request = %+v, want Repo:sam/tau Title:\"Fix x=y in config\"", got)
}
}
// -- TaskController fakes for /approve and /cancel tests --
type fakeTaskController struct {
approveFn func(ctx context.Context, taskID int64, identity string) error
cancelFn func(ctx context.Context, taskID int64, identity string) error
approved []int64
cancelled []int64
identities []string
}
func (f *fakeTaskController) Approve(ctx context.Context, taskID int64, identity string) error {
f.approved = append(f.approved, taskID)
f.identities = append(f.identities, identity)
if f.approveFn != nil {
return f.approveFn(ctx, taskID, identity)
}
return nil
}
func (f *fakeTaskController) Cancel(ctx context.Context, taskID int64, identity string) error {
f.cancelled = append(f.cancelled, taskID)
f.identities = append(f.identities, identity)
if f.cancelFn != nil {
return f.cancelFn(ctx, taskID, identity)
}
return nil
}
func TestRouteApproveParsesIdentity(t *testing.T) {
fc := &fakeTaskController{}
r := NewRouter(nil, nil, "test-gw")
r.Controller = fc
r.Identity = "default"
reply, err := r.Route(context.Background(), Message{Text: "/approve identity=reviewer 42"})
if err != nil {
t.Fatalf("Route: %v", err)
}
if !strings.Contains(reply, "approved") {
t.Errorf("reply = %q, want mention of approval", reply)
}
if len(fc.approved) != 1 || fc.approved[0] != 42 || fc.identities[0] != "reviewer" {
t.Errorf("approved/identities = %v/%v, want [42]/[reviewer]", fc.approved, fc.identities)
}
}
func TestRouteApproveSuccess(t *testing.T) {
fc := &fakeTaskController{}
r := NewRouter(nil, nil, "test-gw")
r.Controller = fc
reply, err := r.Route(context.Background(), Message{Text: "/approve 42"})
if err != nil {
t.Fatalf("Route: %v", err)
}
if !strings.Contains(reply, "approved") {
t.Errorf("reply = %q, want mention of approval", reply)
}
if len(fc.approved) != 1 || fc.approved[0] != 42 {
t.Errorf("approved = %v, want [42]", fc.approved)
}
}
func TestRouteApproveNotConfigured(t *testing.T) {
r := NewRouter(nil, nil, "test-gw")
reply, err := r.Route(context.Background(), Message{Text: "/approve 1"})
if err != nil {
t.Fatalf("Route: %v", err)
}
if !strings.Contains(reply, "not configured") {
t.Errorf("reply = %q, want 'not configured'", reply)
}
}
func TestRouteApproveBadID(t *testing.T) {
fc := &fakeTaskController{}
r := NewRouter(nil, nil, "test-gw")
r.Controller = fc
reply, err := r.Route(context.Background(), Message{Text: "/approve notanumber"})
if err != nil {
t.Fatalf("Route: %v", err)
}
if !strings.Contains(reply, "Usage") {
t.Errorf("reply = %q, want usage message", reply)
}
if len(fc.approved) != 0 {
t.Error("Approve must not be called with an invalid ID")
}
}
func TestRouteApproveWrongStateSurfacesError(t *testing.T) {
fc := &fakeTaskController{
approveFn: func(ctx context.Context, taskID int64, identity string) error {
return fmt.Errorf("task is queued, not waiting_human")
},
}
r := NewRouter(nil, nil, "test-gw")
r.Controller = fc
reply, err := r.Route(context.Background(), Message{Text: "/approve 7"})
if err != nil {
t.Fatalf("Route: %v", err)
}
if !strings.Contains(reply, "Cannot approve") || !strings.Contains(reply, "waiting_human") {
t.Errorf("reply = %q, want error surfaced to user", reply)
}
}
func TestRouteCancelSuccess(t *testing.T) {
fc := &fakeTaskController{}
r := NewRouter(nil, nil, "test-gw")
r.Controller = fc
reply, err := r.Route(context.Background(), Message{Text: "/cancel 5"})
if err != nil {
t.Fatalf("Route: %v", err)
}
if !strings.Contains(reply, "cancelled") {
t.Errorf("reply = %q, want mention of cancellation", reply)
}
if len(fc.cancelled) != 1 || fc.cancelled[0] != 5 {
t.Errorf("cancelled = %v, want [5]", fc.cancelled)
}
}
func TestRouteCancelParsesIdentity(t *testing.T) {
fc := &fakeTaskController{}
r := NewRouter(nil, nil, "test-gw")
r.Controller = fc
r.Identity = "default"
reply, err := r.Route(context.Background(), Message{Text: "/cancel identity=reviewer 5"})
if err != nil {
t.Fatalf("Route: %v", err)
}
if !strings.Contains(reply, "cancelled") {
t.Errorf("reply = %q, want mention of cancellation", reply)
}
if len(fc.cancelled) != 1 || fc.cancelled[0] != 5 || fc.identities[0] != "reviewer" {
t.Errorf("cancelled/identities = %v/%v, want [5]/[reviewer]", fc.cancelled, fc.identities)
}
}
func TestRouteCancelCrossIdentityRejected(t *testing.T) {
fc := &fakeTaskController{
cancelFn: func(ctx context.Context, taskID int64, identity string) error {
if identity != "archie" {
return fmt.Errorf("task belongs to a different identity")
}
return nil
},
}
r := NewRouter(nil, nil, "test-gw")
r.Controller = fc
r.Identity = "winter"
reply, err := r.Route(context.Background(), Message{Text: "/cancel 9"})
if err != nil {
t.Fatalf("Route: %v", err)
}
if !strings.Contains(reply, "Cannot cancel") || !strings.Contains(reply, "different identity") {
t.Errorf("reply = %q, want cross-identity rejection surfaced", reply)
}
}
func TestRouteCancelNotConfigured(t *testing.T) {
r := NewRouter(nil, nil, "test-gw")
reply, err := r.Route(context.Background(), Message{Text: "/cancel 1"})
if err != nil {
t.Fatalf("Route: %v", err)
}
if !strings.Contains(reply, "not configured") {
t.Errorf("reply = %q, want 'not configured'", reply)
}
}