package main import ( "bytes" "context" "encoding/json" "net/http" "net/http/httptest" "os" "strings" "sync" "sync/atomic" "testing" "time" "lchat/internal/bot" "lchat/internal/e2ee" ) // provisionNamed provisions an identity whose roster label matches its payload // name — how real members and bots look. (provisionTestBot's shared "TestBot" // label would defeat the agent-skip rule these tests pin.) func provisionNamed(t *testing.T, tsURL, name string, agent bool) *bot.Session { t.Helper() secret := mintInviteForTest(t, tsURL, name, agent) sess, err := bot.Provision(tsURL, secret, name) if err != nil { t.Fatalf("provision %s: %v", name, err) } return sess } // createThreadKind is createGroupWith for an arbitrary thread kind ("dm" for // the bot-DM tests below). func createThreadKind(t *testing.T, tsURL string, s *Server, kind string, sessions ...*bot.Session) (string, []byte) { t.Helper() threadKey, err := e2ee.GenerateKey() if err != nil { t.Fatal(err) } members := []map[string]string{} for _, sess := range sessions { uid, _ := s.db.deviceTokenUser(sess.Token) wrapped, err := e2ee.WrapThreadKey(e2ee.B64uEncode(threadKey), sess.UserPub) if err != nil { t.Fatal(err) } members = append(members, map[string]string{"user_id": uid, "wrapped": wrapped}) } body, _ := json.Marshal(map[string]any{"token": sessions[0].Token, "kind": kind, "members": members}) resp, err := http.Post(tsURL+"/api/threads", "application/json", bytes.NewReader(body)) if err != nil { t.Fatal(err) } defer resp.Body.Close() var out struct { ThreadID string `json:"thread_id"` } if err := json.NewDecoder(resp.Body).Decode(&out); err != nil || out.ThreadID == "" { t.Fatalf("create %s thread: status %d err %v", kind, resp.StatusCode, err) } return out.ThreadID, threadKey } // postAs encrypts a plain chat payload under the thread key and posts it over // the REST doorway (stores + broadcasts, same path as a WS "msg" frame). func postAs(t *testing.T, tsURL string, sess *bot.Session, threadID string, threadKey []byte, text string) { t.Helper() blob, err := e2ee.EncryptJSON(threadKey, map[string]any{"name": sess.Name, "text": text, "ts": time.Now().UnixMilli()}) if err != nil { t.Fatal(err) } body, _ := json.Marshal(map[string]string{"thread_id": threadID, "blob": blob}) resp, err := http.Post(tsURL+"/api/messages?token="+sess.Token, "application/json", bytes.NewReader(body)) if err != nil { t.Fatal(err) } resp.Body.Close() if resp.StatusCode != http.StatusOK { t.Fatalf("post message: %d", resp.StatusCode) } } // botReplies returns the decrypted payloads authored by `name` in a thread. func botReplies(t *testing.T, s *Server, threadID string, threadKey []byte, name string) []struct { Text string Actions []map[string]any } { t.Helper() msgs, err := s.db.messagesAfter(threadID, 0, 200) if err != nil { t.Fatal(err) } var out []struct { Text string Actions []map[string]any } for _, m := range msgs { var p struct { Name string `json:"name"` Text string `json:"text"` Actions []map[string]any `json:"actions"` } if e2ee.DecryptJSON(threadKey, m.Blob, &p) != nil || p.Name != name { continue } out = append(out, struct { Text string Actions []map[string]any }{p.Text, p.Actions}) } return out } func waitFor(t *testing.T, what string, cond func() bool) { t.Helper() deadline := time.Now().Add(5 * time.Second) for time.Now().Before(deadline) { if cond() { return } time.Sleep(20 * time.Millisecond) } t.Fatalf("timed out waiting for %s", what) } // awaitReady blocks until the bot is live (reading frames). A message posted // before then is past the connect-time cursor and would only surface on a // reconnect — so tests that post-then-assert must wait for this first. func awaitReady(t *testing.T, ready <-chan struct{}) { t.Helper() select { case <-ready: case <-time.After(5 * time.Second): t.Fatal("bot never became ready") } } // TestBotRunCommandFloor drives the live runtime end-to-end against the real // router: a provisioned bot connects over WebSocket, and the deterministic // command floor answers a group @mention and a DM — with the non-mention group // message provably ignored (replies are processed in order, so one reply after // two posts means the first was skipped, no sleep needed). func TestBotRunCommandFloor(t *testing.T) { ts, s := newTestServer(t) owner := provisionNamed(t, ts.URL, "Owner", false) eleven := provisionNamed(t, ts.URL, "Eleven", true) groupID, groupKey := createGroupWith(t, ts.URL, s, owner, eleven) ready := make(chan struct{}) ctx, cancel := context.WithCancel(context.Background()) defer cancel() rt := &bot.Runtime{Base: ts.URL, Session: eleven, Handler: bot.CommandFloor("Eleven", false), Ready: func() { close(ready) }, Logf: t.Logf} go rt.Run(ctx) awaitReady(t, ready) // A plain group message must not trigger the bot; the @mention after it must. postAs(t, ts.URL, owner, groupID, groupKey, "just chatting amongst ourselves") postAs(t, ts.URL, owner, groupID, groupKey, "@Eleven /theme lagoon") waitFor(t, "group reply", func() bool { return len(botReplies(t, s, groupID, groupKey, "Eleven")) > 0 }) replies := botReplies(t, s, groupID, groupKey, "Eleven") if len(replies) != 1 { t.Fatalf("got %d replies, want 1 (the plain message must be ignored)", len(replies)) } if !strings.Contains(replies[0].Text, "Lagoon") || len(replies[0].Actions) != 1 || replies[0].Actions[0]["theme"] != "lagoon" { t.Fatalf("bad /theme reply: %+v", replies[0]) } // A DM created while the bot is already connected exercises the // unknown-thread path — and its FIRST message must get a reply (the cursor // rewind in onFrame; the Node runtime drops this one). dmID, dmKey := createThreadKind(t, ts.URL, s, "dm", owner, eleven) postAs(t, ts.URL, owner, dmID, dmKey, "hello there") waitFor(t, "dm reply", func() bool { return len(botReplies(t, s, dmID, dmKey, "Eleven")) > 0 }) dmReplies := botReplies(t, s, dmID, dmKey, "Eleven") if !strings.Contains(dmReplies[0].Text, "/help") { t.Fatalf("dm to a no-AI bot should degrade to the commands hint: %+v", dmReplies[0]) } // Bare command in the DM answers deterministically. postAs(t, ts.URL, owner, dmID, dmKey, "/help") waitFor(t, "help reply", func() bool { return len(botReplies(t, s, dmID, dmKey, "Eleven")) >= 2 }) dmReplies = botReplies(t, s, dmID, dmKey, "Eleven") help := dmReplies[len(dmReplies)-1] if !strings.Contains(help.Text, "/theme") || !strings.Contains(help.Text, "aren't enabled") { t.Fatalf("bad /help reply: %+v", help) } } // TestBotRunIgnoresOtherAgents pins the bots-never-talk-to-bots rule: a message // authored under another agent's roster label gets no reply, even in a DM. func TestBotRunIgnoresOtherAgents(t *testing.T) { ts, s := newTestServer(t) owner := provisionNamed(t, ts.URL, "Owner", false) eleven := provisionNamed(t, ts.URL, "Eleven", true) announcer := provisionNamed(t, ts.URL, "Announcer", true) // All three share the group: the ping-pong risk the rule prevents only // exists between bots in the same thread — which is also what puts the // Announcer in Eleven's (connections-only) roster. threadID, threadKey := createThreadKind(t, ts.URL, s, "group", owner, eleven, announcer) ready := make(chan struct{}) ctx, cancel := context.WithCancel(context.Background()) defer cancel() rt := &bot.Runtime{Base: ts.URL, Session: eleven, Handler: bot.CommandFloor("Eleven", false), Ready: func() { close(ready) }, Logf: t.Logf} go rt.Run(ctx) awaitReady(t, ready) // An agent-authored mention must be skipped; prove it with an ordinary // mention after it that gets the (single) reply. postAs(t, ts.URL, announcer, threadID, threadKey, "@Eleven /help") postAs(t, ts.URL, owner, threadID, threadKey, "@Eleven /light") waitFor(t, "reply", func() bool { return len(botReplies(t, s, threadID, threadKey, "Eleven")) > 0 }) replies := botReplies(t, s, threadID, threadKey, "Eleven") if len(replies) != 1 || replies[0].Actions[0]["appearance"] != "light" { t.Fatalf("agent-authored mention must be ignored; got %+v", replies) } } // TestBotEngineRecipe drives a recipe end-to-end: a watcher rule nudges on a // keyword in an unaddressed group message, the commands rule still answers // mentions, and a changelog rule announces fresh entries — one Engine, one // identity, all three behaviours live. func TestBotEngineRecipe(t *testing.T) { ts, s := newTestServer(t) if len(s.changelog) < 1 { t.Fatal("embedded changelog is empty") } owner := provisionNamed(t, ts.URL, "Owner", false) eleven := provisionNamed(t, ts.URL, "Eleven", true) groupID, groupKey := createGroupWith(t, ts.URL, s, owner, eleven) dir := t.TempDir() sessionPath := dir + "/eleven.session.json" if err := eleven.Save(sessionPath); err != nil { t.Fatal(err) } recipeSrc := `{"rules":[ {"on":"message","match":"any","regex":"(?i)\\bguys\\b","action":"reply","text":"Hey {name} — how about \"folks\"?"}, {"on":"message","action":"commands"}, {"on":"changelog","action":"announce","every":"50ms"} ]}` if err := os.WriteFile(bot.RecipePathFor(sessionPath), []byte(recipeSrc), 0o600); err != nil { t.Fatal(err) } recipe, err := bot.LoadRecipe(bot.RecipePathFor(sessionPath)) if err != nil { t.Fatal(err) } ready := make(chan struct{}) ctx, cancel := context.WithCancel(context.Background()) defer cancel() eng := &bot.Engine{Base: ts.URL, Session: eleven, SessionPath: sessionPath, Recipe: recipe, Ready: func() { close(ready) }, Logf: t.Logf} go eng.Run(ctx) awaitReady(t, ready) // The changelog poller baselines silently on its first tick. statePath := bot.StatePathFor(sessionPath) waitFor(t, "announce baseline", func() bool { _, err := os.Stat(statePath); return err == nil }) // Unaddressed keyword → the watcher's templated nudge. postAs(t, ts.URL, owner, groupID, groupKey, "morning guys") waitFor(t, "nudge", func() bool { return len(botReplies(t, s, groupID, groupKey, "Eleven")) > 0 }) replies := botReplies(t, s, groupID, groupKey, "Eleven") if replies[0].Text != "Hey Owner — how about \"folks\"?" { t.Fatalf("bad nudge: %+v", replies[0]) } // Mentioned command still reaches the floor through the recipe. postAs(t, ts.URL, owner, groupID, groupKey, "@Eleven /dark") waitFor(t, "command reply", func() bool { return len(botReplies(t, s, groupID, groupKey, "Eleven")) >= 2 }) replies = botReplies(t, s, groupID, groupKey, "Eleven") if replies[1].Actions[0]["appearance"] != "dark" { t.Fatalf("bad command reply: %+v", replies[1]) } // Forget the newest changelog entry → the poller announces it within a tick. raw, err := os.ReadFile(statePath) if err != nil { t.Fatal(err) } var state struct { Announced []string `json:"announced"` } if err := json.Unmarshal(raw, &state); err != nil { t.Fatal(err) } state.Announced = state.Announced[:len(state.Announced)-1] raw, _ = json.Marshal(state) if err := os.WriteFile(statePath, raw, 0o600); err != nil { t.Fatal(err) } waitFor(t, "announcement", func() bool { for _, r := range botReplies(t, s, groupID, groupKey, "Eleven") { if strings.Contains(r.Text, "🚀 Just shipped:") { return true } } return false }) } // fakeAnthropic stands in for the Messages API: it records the last request and // returns a scripted reply (optionally with a tool_use chip). Pointed at via // ANTHROPIC_BASE_URL, it lets the model path run end-to-end with no network. type fakeAnthropic struct { srv *httptest.Server mu sync.Mutex lastReq map[string]any reply string tool map[string]any // e.g. {"name":"set_theme","input":{"theme":"lagoon"}} calls int } func newFakeAnthropic(t *testing.T, reply string, tool map[string]any) *fakeAnthropic { f := &fakeAnthropic{reply: reply, tool: tool} f.srv = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { var req map[string]any json.NewDecoder(r.Body).Decode(&req) f.mu.Lock() f.lastReq = req f.calls++ f.mu.Unlock() content := []map[string]any{} if f.reply != "" { content = append(content, map[string]any{"type": "text", "text": f.reply}) } if f.tool != nil { content = append(content, map[string]any{ "type": "tool_use", "name": f.tool["name"], "input": f.tool["input"], }) } json.NewEncoder(w).Encode(map[string]any{"content": content, "stop_reason": "end_turn"}) })) t.Cleanup(f.srv.Close) return f } // TestBotEngineModelReply drives the model action end-to-end: a mentioned // message with no matching command reaches the "model" rule, which asks the // (fake) Claude endpoint and posts its reply with the chip it proposed. func TestBotEngineModelReply(t *testing.T) { fake := newFakeAnthropic(t, "Sure — switching you to Lagoon.", map[string]any{"name": "set_theme", "input": map[string]any{"theme": "lagoon"}}) t.Setenv("ANTHROPIC_BASE_URL", fake.srv.URL) ts, s := newTestServer(t) owner := provisionNamed(t, ts.URL, "Owner", false) eleven := provisionNamed(t, ts.URL, "Eleven", true) groupID, groupKey := createGroupWith(t, ts.URL, s, owner, eleven) dir := t.TempDir() sessionPath := dir + "/eleven.session.json" if err := eleven.Save(sessionPath); err != nil { t.Fatal(err) } // Default recipe (commands then model) covers this — no recipe file needed. ready := make(chan struct{}) ctx, cancel := context.WithCancel(context.Background()) defer cancel() eng := &bot.Engine{Base: ts.URL, Session: eleven, SessionPath: sessionPath, Recipe: bot.DefaultRecipe(), AIKey: "test-key", Ready: func() { close(ready) }, Logf: t.Logf} go eng.Run(ctx) awaitReady(t, ready) // A model brain badges "ai". uid, _ := s.db.deviceTokenUser(eleven.Token) waitFor(t, "bot badge ai", func() bool { var kind string s.db.sql.QueryRow("SELECT COALESCE(kind, '') FROM bot_configs WHERE user_id = ?", uid).Scan(&kind) return kind == "ai" }) postAs(t, ts.URL, owner, groupID, groupKey, "@Eleven make it lagoon please") waitFor(t, "model reply", func() bool { return len(botReplies(t, s, groupID, groupKey, "Eleven")) > 0 }) replies := botReplies(t, s, groupID, groupKey, "Eleven") if replies[0].Text != "Sure — switching you to Lagoon." { t.Fatalf("bad model reply text: %+v", replies[0]) } if len(replies[0].Actions) != 1 || replies[0].Actions[0]["theme"] != "lagoon" { t.Fatalf("model reply missing chip: %+v", replies[0]) } // The request carried the cacheable system prompt and the triggering line. fake.mu.Lock() defer fake.mu.Unlock() sys, _ := fake.lastReq["system"].([]any) if len(sys) == 0 { t.Fatal("no system blocks sent") } block := sys[0].(map[string]any) if _, ok := block["cache_control"]; !ok { t.Error("system block not marked cacheable") } // Default model is Sonnet, which supports adaptive thinking. if th, _ := fake.lastReq["thinking"].(map[string]any); th["type"] != "adaptive" { t.Errorf("Sonnet reply should send adaptive thinking, got %v", fake.lastReq["thinking"]) } } // TestBotEngineModelReplyHaikuNoThinking is the regression guard for the live // outage: Haiku 400s on adaptive thinking, so a Haiku bot must send NO thinking // field. Same flow as above with LCHAT_MODEL=haiku; asserts the request omits // thinking (and still produces a reply). func TestBotEngineModelReplyHaikuNoThinking(t *testing.T) { fake := newFakeAnthropic(t, "On it.", nil) t.Setenv("ANTHROPIC_BASE_URL", fake.srv.URL) t.Setenv("LCHAT_MODEL", "claude-haiku-4-5") ts, s := newTestServer(t) owner := provisionNamed(t, ts.URL, "Owner", false) eleven := provisionNamed(t, ts.URL, "Eleven", true) groupID, groupKey := createGroupWith(t, ts.URL, s, owner, eleven) dir := t.TempDir() sessionPath := dir + "/eleven.session.json" if err := eleven.Save(sessionPath); err != nil { t.Fatal(err) } ready := make(chan struct{}) ctx, cancel := context.WithCancel(context.Background()) defer cancel() eng := &bot.Engine{Base: ts.URL, Session: eleven, SessionPath: sessionPath, Recipe: bot.DefaultRecipe(), AIKey: "test-key", Ready: func() { close(ready) }, Logf: t.Logf} go eng.Run(ctx) awaitReady(t, ready) postAs(t, ts.URL, owner, groupID, groupKey, "@Eleven hello") waitFor(t, "haiku reply", func() bool { return len(botReplies(t, s, groupID, groupKey, "Eleven")) > 0 }) fake.mu.Lock() defer fake.mu.Unlock() if fake.lastReq["model"] != "claude-haiku-4-5" { t.Fatalf("expected haiku model, got %v", fake.lastReq["model"]) } if _, present := fake.lastReq["thinking"]; present { t.Fatalf("Haiku request must omit thinking, got %v", fake.lastReq["thinking"]) } } // TestBotEngineChangelogBadge pins the recipe-driven badge: a bot running a // changelog-only recipe with the shared ANTHROPIC_API_KEY present must still // badge BOT (write-only, no model rule), not AI — the bug that would appear // when several identities run from one `lchat bot run` with one key. func TestBotEngineChangelogBadge(t *testing.T) { ts, s := newTestServer(t) owner := provisionNamed(t, ts.URL, "Owner", false) announcer := provisionNamed(t, ts.URL, "Deploys", true) createGroupWith(t, ts.URL, s, owner, announcer) dir := t.TempDir() sessionPath := dir + "/deploys.session.json" if err := announcer.Save(sessionPath); err != nil { t.Fatal(err) } recipePath := dir + "/deploys.recipe.json" if err := os.WriteFile(recipePath, []byte(`{"rules":[{"on":"changelog","action":"announce"}]}`), 0o600); err != nil { t.Fatal(err) } recipe, err := bot.LoadRecipe(recipePath) if err != nil { t.Fatal(err) } ready := make(chan struct{}) ctx, cancel := context.WithCancel(context.Background()) defer cancel() eng := &bot.Engine{Base: ts.URL, Session: announcer, SessionPath: sessionPath, Recipe: recipe, AIKey: "a-key-that-should-be-ignored", Ready: func() { close(ready) }, Logf: t.Logf} go eng.Run(ctx) awaitReady(t, ready) uid, _ := s.db.deviceTokenUser(announcer.Token) waitFor(t, "announcer badge", func() bool { var kind string s.db.sql.QueryRow("SELECT COALESCE(kind, '') FROM bot_configs WHERE user_id = ?", uid).Scan(&kind) return kind == "bot" }) // Give it a beat to (not) declare ai — if the badge ever became "ai" the // wait above would have to have seen "bot" first anyway; assert it stays bot. var kind string s.db.sql.QueryRow("SELECT COALESCE(kind, '') FROM bot_configs WHERE user_id = ?", uid).Scan(&kind) if kind != "bot" { t.Fatalf("announcer with a key present should badge bot, got %q", kind) } } // The live sidecar marks EVERY request it makes to its instance — the // WebSocket dial and all REST calls — with bot.SidecarHeader, so a hibernating // instance's router can exclude bot traffic from its idle clock and refuse to // be woken by a sidecar reconnect loop (activator.go). Pinned at the wire: a // recording front door in the engine's path fails the test on any unmarked // request. (Provisioning happens against the unwrapped server on purpose — // claiming an invite is an operator action and stays unmarked.) func TestBotRuntimeMarksAllRequests(t *testing.T) { ts, s := newTestServer(t) handler := s.handler() var seen atomic.Int32 front := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { seen.Add(1) if r.Header.Get(bot.SidecarHeader) == "" { t.Errorf("unmarked sidecar request: %s %s", r.Method, r.URL.Path) } handler.ServeHTTP(w, r) })) t.Cleanup(front.Close) eleven := provisionNamed(t, ts.URL, "Eleven", true) sessionPath := t.TempDir() + "/eleven.session.json" if err := eleven.Save(sessionPath); err != nil { t.Fatal(err) } ready := make(chan struct{}) ctx, cancel := context.WithCancel(context.Background()) defer cancel() eng := &bot.Engine{Base: front.URL, Session: eleven, SessionPath: sessionPath, Recipe: bot.DefaultRecipe(), Ready: func() { close(ready) }, Logf: t.Logf} go eng.Run(ctx) awaitReady(t, ready) // Ready fires after the connect sequence — config, roster, threads, // declare-kind, catch-up, and the WS upgrade itself — all through the // front door; make sure it actually saw them. if seen.Load() < 5 { t.Fatalf("front door saw only %d engine requests", seen.Load()) } }