package chat import ( "context" "encoding/json/jsontext" "errors" "io" "log/slog" "net/http" "net/http/httptest" "strings" "testing" "github.com/google/uuid" "github.com/samcharles93/archie-core/internal/platform/httpapi" "github.com/samcharles93/archie-core/internal/platform/hub" "github.com/samcharles93/archie-core/modules/chat/internal/store" "github.com/samcharles93/archie-core/modules/connectors" "github.com/samcharles93/archie-core/modules/governance" "github.com/samcharles93/archie-core/modules/judgement" "github.com/samcharles93/archie-core/modules/memory" ) const ( alice = "alice" bob = "bob" ) // person is a principal holding role in the operating org. func person(id string, role governance.Role) governance.Principal { return governance.Principal{ IdentityID: governance.IdentityID(id), Org: governance.DefaultOrgID, Memberships: []governance.Membership{{IdentityID: governance.IdentityID(id), OrgID: governance.DefaultOrgID, Role: role}}, } } // TestConversationsBelongToTheirOwner guards the chat API's ownership // boundary: a person reads and continues their own conversations, and every // chat route answers anyone else as if the conversation did not exist. func TestConversationsBelongToTheirOwner(t *testing.T) { session, turn := uuid.New(), uuid.New() cases := []struct { name, method, path, body string ok int }{ {"rename", http.MethodPatch, "/api/chat/sessions/" + session.String(), `{"title":"mine"}`, http.StatusOK}, {"list turns", http.MethodGet, "/api/chat/sessions/" + session.String() + "/turns", "", http.StatusOK}, {"send", http.MethodPost, "/api/chat/sessions/" + session.String() + "/turns", `{"text":"hello"}`, http.StatusAccepted}, {"get turn", http.MethodGet, "/api/chat/turns/" + turn.String(), "", http.StatusOK}, {"cancel", http.MethodPost, "/api/chat/turns/" + turn.String() + "/cancel", "", http.StatusOK}, {"stream", http.MethodGet, "/api/chat/turns/" + turn.String() + "/events", "", http.StatusOK}, {"delete", http.MethodDelete, "/api/chat/sessions/" + session.String(), "", http.StatusNoContent}, } for _, tc := range cases { for _, as := range []string{alice, bob} { t.Run(tc.name+" as "+as, func(t *testing.T) { srv := chatServer(t, session, turn) want := tc.ok if as != alice { want = http.StatusNotFound } if got, _ := call(t, srv, as, tc.method, tc.path, tc.body); got != want { t.Fatalf("%s %s as %s = %d, want %d", tc.method, tc.path, as, got, want) } }) } } t.Run("list sessions", func(t *testing.T) { srv := chatServer(t, session, turn) for as, sees := range map[string]bool{alice: true, bob: false} { code, body := call(t, srv, as, http.MethodGet, "/api/chat/sessions", "") if code != http.StatusOK || strings.Contains(body, session.String()) != sees { t.Fatalf("list as %s = %d %s, want it to show the conversation: %v", as, code, body, sees) } } }) } // TestTurnActsWithItsOwnersAccess guards the tool boundary: the same tool // call runs in an administrator's conversation and is refused in a viewer's, // so the agent never reaches further than the person chatting. func TestTurnActsWithItsOwnersAccess(t *testing.T) { people := fakePeople{alice: person(alice, governance.RoleAdmin), bob: person(bob, governance.RoleViewer)} d := Deps{ People: people, Access: governance.NewEngine(governance.ShippedOrgPolicies(governance.DefaultOrgID)), Toolbox: fakeToolbox{}, Judge: fakeJudge{}, Memory: fakeMemory{}, } m := &Module{orgID: "default", log: slog.New(slog.DiscardHandler), hub: hub.New()} action := connectors.Declaration{Connector: "pager", Kind: connectors.KindAction, Name: "page"} lookup := connectors.Declaration{Connector: "pager", Kind: connectors.KindLookup, Name: "who"} cases := []struct { name, owner string run func(context.Context) error allowed bool }{ {"admin runs an action", alice, callTool(m, d, action), true}, {"viewer can't run an action", bob, callTool(m, d, action), false}, {"viewer looks up", bob, callTool(m, d, lookup), true}, {"admin saves a memory", alice, builtin(m, d, "memory_save"), true}, {"viewer can't save a memory", bob, builtin(m, d, "memory_save"), false}, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { ctx, err := m.actingAs(t.Context(), d, tc.owner) if err != nil { t.Fatal(err) } if err := tc.run(ctx); (err == nil) != tc.allowed { t.Fatalf("allowed = %v (err %v), want %v", err == nil, err, tc.allowed) } }) } } func callTool(m *Module, d Deps, decl connectors.Declaration) func(context.Context) error { return func(ctx context.Context) error { _, err := m.call(ctx, d, store.Turn{ID: uuid.New()}, decl, jsontext.Value(`{}`)) return err } } func builtin(m *Module, d Deps, name string) func(context.Context) error { return func(ctx context.Context) error { for _, tool := range m.builtins(d, store.Turn{ID: uuid.New()}) { if tool.Name == name { _, err := tool.Execute(ctx, `{"text":"a fact"}`) return err } } return errors.New("no tool " + name) } } // chatServer serves the chat API over a store holding one finished turn in a // conversation alice owns. The X-As header names who calls. func chatServer(t *testing.T, session, turn uuid.UUID) *httptest.Server { t.Helper() fs := &fakeStore{ sessions: map[uuid.UUID]store.Session{session: {ID: session, Owner: alice}}, turns: map[uuid.UUID]store.Turn{turn: {ID: turn, SessionID: session, Owner: alice, State: stateSucceeded}}, events: map[uuid.UUID][]store.Event{turn: {{Seq: 1, TurnID: turn, Kind: kindDone, Data: jsontext.Value(`{}`)}}}, } m := &Module{ orgID: "default", store: fs, log: slog.New(slog.DiscardHandler), hub: hub.New(), wake: make(chan struct{}, 1), running: map[uuid.UUID]context.CancelFunc{}, } mux := http.NewServeMux() m.Register(httpapi.New(mux, "test", headerAuth{})) srv := httptest.NewServer(mux) t.Cleanup(srv.Close) return srv } func call(t *testing.T, srv *httptest.Server, as, method, path, body string) (int, string) { t.Helper() req, err := http.NewRequestWithContext(t.Context(), method, srv.URL+path, strings.NewReader(body)) if err != nil { t.Fatal(err) } req.Header.Set("X-As", as) if body != "" { req.Header.Set("Content-Type", "application/json") } res, err := srv.Client().Do(req) if err != nil { t.Fatal(err) } defer func() { _ = res.Body.Close() }() out, _ := io.ReadAll(res.Body) return res.StatusCode, string(out) } // headerAuth signs in whoever the X-As header names, as a viewer. type headerAuth struct{} func (headerAuth) Authenticate(ctx context.Context, r *http.Request) (context.Context, bool, error) { as := r.Header.Get("X-As") if as == "" { return nil, false, httpapi.ErrUnauthenticated } return governance.WithPrincipal(ctx, person(as, governance.RoleViewer)), false, nil } type fakePeople map[string]governance.Principal func (f fakePeople) PrincipalFor(_ context.Context, id governance.IdentityID) (governance.Principal, error) { p, ok := f[string(id)] if !ok { return governance.Principal{}, errors.New("no such person") } return p, nil } type fakeToolbox struct{ Toolbox } func (fakeToolbox) Tool(string) (connectors.Tool, bool) { return fakeTool{}, true } type fakeTool struct{ connectors.Tool } func (fakeTool) Lookup(context.Context, string, jsontext.Value) (string, bool, error) { return "someone", true, nil } type fakeJudge struct{ Judge } func (fakeJudge) Assess(_ context.Context, req judgement.Request) (judgement.Decision, error) { return judgement.Decision{Key: req.Key, Level: judgement.ActQuietly}, nil } func (fakeJudge) Execute(context.Context, judgement.Decision, jsontext.Value, string) (string, error) { return "ran", nil } type fakeMemory struct{ Memory } func (fakeMemory) Remember(context.Context, string) (memory.Note, error) { return memory.Note{}, nil } // fakeStore keeps conversations in memory with the store's ownership rule: // a read or change for an owner finds only that owner's conversations. type fakeStore struct { conversations sessions map[uuid.UUID]store.Session turns map[uuid.UUID]store.Turn events map[uuid.UUID][]store.Event } func (f *fakeStore) Sessions(_ context.Context, _, owner string, _ int) ([]store.Session, error) { var out []store.Session for _, s := range f.sessions { if s.Owner == owner { out = append(out, s) } } return out, nil } func (f *fakeStore) Session(_ context.Context, _, owner string, id uuid.UUID) (store.Session, error) { s, ok := f.sessions[id] if !ok || s.Owner != owner { return store.Session{}, store.ErrNotFound } return s, nil } func (f *fakeStore) Rename(ctx context.Context, org, owner string, id uuid.UUID, title string) error { s, err := f.Session(ctx, org, owner, id) if err == nil { s.Title = title f.sessions[id] = s } return err } func (f *fakeStore) DeleteSession(ctx context.Context, org, owner string, id uuid.UUID) error { if _, err := f.Session(ctx, org, owner, id); err != nil { return err } delete(f.sessions, id) return nil } func (f *fakeStore) AddTurn(ctx context.Context, org, owner string, t store.Turn) (store.Turn, error) { if _, err := f.Session(ctx, org, owner, t.SessionID); err != nil { return store.Turn{}, err } t.Owner, t.State = owner, stateQueued f.turns[t.ID] = t return t, nil } func (f *fakeStore) Turns(_ context.Context, _, owner string, session uuid.UUID) ([]store.Turn, error) { var out []store.Turn for _, t := range f.turns { if t.SessionID == session && t.Owner == owner { out = append(out, t) } } return out, nil } func (f *fakeStore) Turn(_ context.Context, _, owner string, id uuid.UUID) (store.Turn, error) { t, ok := f.turns[id] if !ok || t.Owner != owner { return store.Turn{}, store.ErrNotFound } return t, nil } func (f *fakeStore) RequestCancel(ctx context.Context, org, owner string, id uuid.UUID) (bool, error) { _, err := f.Turn(ctx, org, owner, id) return false, err } func (f *fakeStore) Events(_ context.Context, turn uuid.UUID, after int64) ([]store.Event, error) { var out []store.Event for _, e := range f.events[turn] { if e.Seq > after { out = append(out, e) } } return out, nil } func (f *fakeStore) EventsOf(context.Context, uuid.UUID, []string) ([]store.Event, error) { return nil, nil }