package bot import ( "context" "net/http/httptest" "os" "path/filepath" "strings" "testing" "time" "github.com/slack-go/slack" "slackerbot/internal/acp" "slackerbot/internal/model" "slackerbot/internal/testutil" "slackerbot/internal/testutil/clustertest" ) type turnHarness struct { c *clustertest.Cluster log *slackLog url string goose *testutil.FakeGoose } func newTurnHarness(t *testing.T) *turnHarness { c := clustertest.New(t) c.Cfg.AgentCwd = "/root" c.AddOwner(t, testutil.HomeData(), true) u := c.Store.Owner("C1") u.Secret = "agent-secret" _ = c.Store.PutOwner(u) c.Store.SeedPermissions(model.PermissionsConfig{Managers: []string{"UBOSS"}, Default: map[string][]string{"UBOSS": {"all"}, "USUPER": {"keep"}}}) goose := testutil.NewFakeGoose() c.Master.Goose = goose log := &slackLog{text: map[string]string{}, under: map[string]string{}} srv := httptest.NewServer(log) t.Cleanup(srv.Close) return &turnHarness{c: c, log: log, url: srv.URL, goose: goose} } // Each bot is a fresh process: nothing in memory carries over, only the store. func (h *turnHarness) bot() *Bot { return &Bot{cfg: h.c.Cfg, store: h.c.Store, mgr: h.c.Mgr, api: slack.New("xoxb", slack.OptionAPIURL(h.url+"/")), acps: map[string]*acp.ACP{}, busy: map[string]int{}} } func (s *slackLog) anyText(want string) bool { s.mu.Lock() defer s.mu.Unlock() for _, text := range s.text { if strings.Contains(text, want) { return true } } return false } func TestRestartResumesInFlightTurn(t *testing.T) { h := newTurnHarness(t) c := h.c h.goose.Hold(true) ctx, shutdown := context.WithCancel(c.Ctx) done := make(chan struct{}) go func() { defer close(done) h.bot().onMessage(ctx, incoming{user: "UANA", channel: "C1", ts: "5.0", text: "build the thing"}) }() select { case <-h.goose.Holding: case <-time.After(10 * time.Second): t.Fatal("the prompt never reached the agent") } turns := c.Store.Turns() if len(turns) != 1 || !turns[0].Prompted || turns[0].ThreadTS != "5.0" || turns[0].User != "UANA" { t.Fatalf("the running turn should be recorded: %+v", turns) } if _, err := os.Stat(filepath.Join(c.Cfg.StateDir, "in-flight-C1.json")); err != nil { t.Fatal(err) } shutdown() <-done if len(c.Store.Turns()) != 1 { t.Fatal("a turn cut off by shutdown must stay recorded") } if !h.log.anyText("slackerbot is restarting") { t.Fatal("the cut-off reply should say it'll be picked back up") } h.goose.Hold(false) h.bot().resumeTurns(c.Ctx) testutil.WaitFor(t, "the turn to be resumed and finish", func() bool { return len(c.Store.Turns()) == 0 }) prompts := h.goose.Prompts() if len(prompts) != 2 || !strings.Contains(prompts[0], "build the thing") || prompts[1] != resumeNote { t.Fatalf("the agent should be told to carry on, not get the message twice: %q", prompts) } if !h.log.anyText("picking it back up") { t.Fatal("the thread should say the reply is being picked back up") } if _, err := os.Stat(filepath.Join(c.Cfg.StateDir, "in-flight-C1.json")); !os.IsNotExist(err) { t.Fatal("the in-flight file should go once the turn is done") } } func TestResumeSendsMessageTheAgentNeverGot(t *testing.T) { h := newTurnHarness(t) _ = h.c.Store.PutTurn(&model.Turn{Channel: "C1", ThreadTS: "7.0", MessageTS: "7.0", User: "UANA", OwnerID: "C1", Text: "check the logs", StartedAt: time.Now()}) h.bot().resumeTurns(h.c.Ctx) testutil.WaitFor(t, "the turn to finish", func() bool { return len(h.c.Store.Turns()) == 0 }) if prompts := h.goose.Prompts(); len(prompts) != 1 || !strings.Contains(prompts[0], "check the logs") { t.Fatalf("a turn cut off before the prompt should send the message: %q", prompts) } } func TestStaleTurnIsDroppedNotResumed(t *testing.T) { h := newTurnHarness(t) _ = h.c.Store.PutTurn(&model.Turn{Channel: "C1", ThreadTS: "8.0", MessageTS: "8.0", User: "UANA", OwnerID: "C1", Text: "old", Prompted: true, StartedAt: time.Now().Add(-2 * resumeWithin)}) _ = h.c.Store.PutTurn(&model.Turn{Channel: "C1", ThreadTS: "9.0", MessageTS: "9.0", User: "UANA", OwnerID: "C1", Text: "looping", Prompted: true, Resumes: maxResumes, StartedAt: time.Now()}) h.bot().resumeTurns(h.c.Ctx) testutil.WaitFor(t, "the turns to be dropped", func() bool { return len(h.c.Store.Turns()) == 0 }) if len(h.goose.Prompts()) != 0 { t.Fatal("stale turns must not reach the agent") } if !h.log.anyText("too late to pick it back up") { t.Fatal("the thread should say to send the message again") } } func TestInFlightIsForSuperUsers(t *testing.T) { h := newTurnHarness(t) _ = h.c.Store.PutTurn(&model.Turn{Channel: "C1", ThreadTS: "1.0", MessageTS: "1.0", Text: "secret plans", StartedAt: time.Now()}) _ = h.c.Store.PutTurn(&model.Turn{Channel: "D2", ThreadTS: "2.0", MessageTS: "2.0", Text: "dm plans", DM: true, StartedAt: time.Now()}) b := h.bot() ask := func(user, channel string, all bool) string { return b.inFlightFor(incoming{user: user, channel: channel}, all, true) } if got := ask("UANA", "C1", false); !strings.Contains(got, ":lock:") || strings.Contains(got, "secret plans") { t.Fatalf("ordinary users can't read it: %q", got) } if got := ask("USUPER", "C1", false); !strings.Contains(got, "in-flight-C1.json") || !strings.Contains(got, "secret plans") || strings.Contains(got, "dm plans") { t.Fatalf("a super user sees this channel's file: %q", got) } if got := ask("USUPER", "C1", true); !strings.Contains(got, "in-flight-D2.json") || !strings.Contains(got, "secret plans") { t.Fatalf("a default-list super user sees every file: %q", got) } if got := ask("UBOSS", "C3", false); !strings.Contains(got, "No replies are in flight here") { t.Fatalf("empty channel: %q", got) } _ = h.c.Store.UpdatePermissions(func(p *model.PermissionsConfig) error { p.Grant("C1", []string{"UANA"}, []model.Action{model.ActMove}) return nil }) if got := ask("UANA", "C1", false); !strings.Contains(got, "secret plans") { t.Fatalf("a channel's own super users see its file: %q", got) } if got := ask("UANA", "C1", true); !strings.Contains(got, ":lock:") { t.Fatalf("every channel needs the default list or a user manager: %q", got) } } func TestInFlightCommandIsPrivate(t *testing.T) { h := newTurnHarness(t) b := h.bot() reply, ok := b.runCommand(h.c.Ctx, incoming{user: "UBOSS", channel: "C1", ts: "1.0"}, "in-flight", "", false) if !ok || reply != "" { t.Fatalf("in-flight should be answered ephemerally: %q %v", reply, ok) } if _, ok := b.runCommand(h.c.Ctx, incoming{user: "UBOSS", channel: "C1", ts: "1.0"}, "in-flight", "entertainment is great", false); ok { t.Fatal("a sentence that starts with in-flight goes to the agent") } }