clean up of the codebase
This commit is contained in:
1 parent
7822529a38
commit
0ac61b4ece
52 files changed
+2545
-2274
No files matched your search
@@ -0,0 +1,249 @@
|
||||
package bot
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/slack-go/slack"
|
||||
|
||||
"slackerbot/internal/acp"
|
||||
"slackerbot/internal/manager"
|
||||
"slackerbot/internal/model"
|
||||
"slackerbot/internal/state"
|
||||
"slackerbot/internal/testutil"
|
||||
"slackerbot/internal/testutil/clustertest"
|
||||
)
|
||||
|
||||
func TestOwnership(t *testing.T) {
|
||||
dm := incoming{user: "U42", channel: "D9", dm: true}
|
||||
if id, kind := dm.owner(); id != "U42" || kind != model.OwnerUser {
|
||||
t.Fatalf("DM should be owned by the user, got %s %s", id, kind)
|
||||
}
|
||||
ch := incoming{user: "U42", channel: "C7"}
|
||||
if id, kind := ch.owner(); id != "C7" || kind != model.OwnerChannel {
|
||||
t.Fatalf("channel message should be owned by the channel, got %s %s", id, kind)
|
||||
}
|
||||
other := incoming{user: "U99", channel: "C7"}
|
||||
a, ka := ch.owner()
|
||||
b, kb := other.owner()
|
||||
if model.MachineName(a, ka) != model.MachineName(b, kb) || model.MachineName(a, ka) != "c-c7" {
|
||||
t.Fatal("two people in one channel should share a container")
|
||||
}
|
||||
if model.MachineName("U42", model.OwnerUser) != "u-u42" || !model.IsOurs("u-u42") || !model.IsOurs("c-c7") || model.IsOurs("web") {
|
||||
t.Fatal("machine naming is off")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTitle(t *testing.T) {
|
||||
if got := title(" fix the build\nplease"); got != "fix the build" {
|
||||
t.Fatal(got)
|
||||
}
|
||||
if got := title(strings.Repeat("é", 80)); !strings.HasSuffix(got, "…") || len([]rune(got)) != 61 {
|
||||
t.Fatal(got)
|
||||
}
|
||||
}
|
||||
|
||||
type fakeSlack struct {
|
||||
mu sync.Mutex
|
||||
msgs map[string]string
|
||||
order []string
|
||||
n int
|
||||
}
|
||||
|
||||
func (f *fakeSlack) handler(w http.ResponseWriter, r *http.Request) {
|
||||
_ = r.ParseForm()
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
ts := r.Form.Get("ts")
|
||||
if strings.HasSuffix(r.URL.Path, "chat.postMessage") {
|
||||
f.n++
|
||||
ts = "ts" + string(rune('0'+f.n))
|
||||
f.order = append(f.order, ts)
|
||||
}
|
||||
f.msgs[ts] = r.Form.Get("text")
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{"ok": true, "channel": "D1", "ts": ts, "text": f.msgs[ts]})
|
||||
}
|
||||
|
||||
func TestStreamerSplitsLongReplies(t *testing.T) {
|
||||
f := &fakeSlack{msgs: map[string]string{"ph": ""}, order: []string{"ph"}}
|
||||
srv := httptest.NewServer(http.HandlerFunc(f.handler))
|
||||
defer srv.Close()
|
||||
api := slack.New("xoxb-test", slack.OptionAPIURL(srv.URL+"/"))
|
||||
|
||||
st := NewStreamer(api, "D1", "root", "ph")
|
||||
line := strings.Repeat("x", 99) + "\n"
|
||||
for i := 0; i < 80; i++ {
|
||||
st.Update(acp.Update{Kind: "agent_message_chunk", Text: line})
|
||||
}
|
||||
st.Finish("end_turn", nil)
|
||||
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
if len(f.order) != 3 {
|
||||
t.Fatalf("want 3 messages, got %d", len(f.order))
|
||||
}
|
||||
var all strings.Builder
|
||||
for _, ts := range f.order {
|
||||
if len(f.msgs[ts]) > slackMsgLimit {
|
||||
t.Fatalf("message %s too long: %d", ts, len(f.msgs[ts]))
|
||||
}
|
||||
all.WriteString(f.msgs[ts])
|
||||
}
|
||||
if all.Len() != 8000 {
|
||||
t.Fatalf("text lost or duplicated: %d chars", all.Len())
|
||||
}
|
||||
}
|
||||
|
||||
func TestMoveTarget(t *testing.T) {
|
||||
b := &Bot{}
|
||||
for in, want := range map[string]string{"": "", "auto": "", "Slave-1": "slave-1", "to node-b": "node-b"} {
|
||||
if got, ok := b.moveTarget(in); !ok || got != want {
|
||||
t.Fatalf("move %q = %q %v, want %q", in, got, ok, want)
|
||||
}
|
||||
}
|
||||
if _, ok := b.moveTarget("the files into the build folder"); ok {
|
||||
t.Fatal("a normal sentence should go to the agent")
|
||||
}
|
||||
}
|
||||
|
||||
type slackLog struct {
|
||||
mu sync.Mutex
|
||||
n int
|
||||
text map[string]string
|
||||
under map[string]string
|
||||
order []string
|
||||
}
|
||||
|
||||
func (s *slackLog) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
_ = r.ParseForm()
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
ts := r.Form.Get("ts")
|
||||
if strings.HasSuffix(r.URL.Path, "chat.postMessage") {
|
||||
s.n++
|
||||
ts = fmt.Sprintf("%d.0", s.n)
|
||||
s.order = append(s.order, ts)
|
||||
s.under[ts] = r.Form.Get("thread_ts")
|
||||
}
|
||||
if !strings.HasSuffix(r.URL.Path, "chat.getPermalink") {
|
||||
s.text[ts] = r.Form.Get("text")
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{"ok": true, "channel": "C1", "ts": ts, "permalink": "https://slack.example/" + ts})
|
||||
}
|
||||
|
||||
func TestEveryMoveStartsAnAnnouncedThread(t *testing.T) {
|
||||
c := clustertest.New(t)
|
||||
c.AddOwner(t, testutil.HomeData(), false)
|
||||
log := &slackLog{text: map[string]string{}, under: map[string]string{}}
|
||||
srv := httptest.NewServer(log)
|
||||
defer srv.Close()
|
||||
b := &Bot{cfg: c.Cfg, store: c.Store, mgr: c.Mgr, api: slack.New("xoxb", slack.OptionAPIURL(srv.URL+"/")),
|
||||
acps: map[string]*acp.ACP{}, busy: map[string]int{}}
|
||||
|
||||
b.moveFor(c.Ctx, "C1", "C1", "slave-1", "<@U1>", "100.0")
|
||||
if _, err := c.Mgr.Migrate(c.Ctx, "C1", manager.MoveOptions{Target: "master"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
b.runMove(c.Ctx, "C1", "C1", manager.MoveOptions{}, "auto process (master was full), for <@U2>'s message", nil)
|
||||
|
||||
log.mu.Lock()
|
||||
defer log.mu.Unlock()
|
||||
var roots []string
|
||||
for _, ts := range log.order {
|
||||
if log.under[ts] == "" {
|
||||
roots = append(roots, ts)
|
||||
}
|
||||
}
|
||||
linked := 0
|
||||
for ts, parent := range log.under {
|
||||
if parent == "100.0" && strings.Contains(log.text[ts], "Follow the move here") {
|
||||
linked++
|
||||
}
|
||||
}
|
||||
if linked != 1 {
|
||||
t.Fatalf("the thread where the move started should get one link to the move thread, got %d", linked)
|
||||
}
|
||||
if len(roots) != 2 {
|
||||
t.Fatalf("want one new top-level message per move, got %d", len(roots))
|
||||
}
|
||||
checks := []string{"started by <@U1>: master → slave-1", "started by auto process (master was full), for <@U2>'s message"}
|
||||
for i, root := range roots {
|
||||
if !strings.Contains(log.text[root], checks[i]) || !strings.Contains(log.text[root], ":white_check_mark: done") {
|
||||
t.Fatalf("root %d = %q", i, log.text[root])
|
||||
}
|
||||
replies := 0
|
||||
for ts, parent := range log.under {
|
||||
if parent == root {
|
||||
replies++
|
||||
if !strings.Contains(log.text[ts], "Moved from") {
|
||||
t.Fatalf("progress reply = %q", log.text[ts])
|
||||
}
|
||||
}
|
||||
}
|
||||
if replies != 1 {
|
||||
t.Fatalf("want progress in one thread reply, got %d", replies)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestManageCommandsInSlack(t *testing.T) {
|
||||
log := &slackLog{text: map[string]string{}, under: map[string]string{}}
|
||||
srv := httptest.NewServer(log)
|
||||
defer srv.Close()
|
||||
store, _ := state.Open(filepath.Join(t.TempDir(), "state.json"))
|
||||
store.SeedPermissions(model.PermissionsConfig{Managers: []string{"UBOSS"}, Default: map[string][]string{"UBOSS": {"all"}}})
|
||||
b := &Bot{store: store, api: slack.New("xoxb", slack.OptionAPIURL(srv.URL+"/"))}
|
||||
ctx := context.Background()
|
||||
say := func(user, text string) string {
|
||||
m := incoming{user: user, channel: "C1", ts: "1.0", text: text}
|
||||
if !b.command(ctx, m) {
|
||||
return "(went to the agent)"
|
||||
}
|
||||
log.mu.Lock()
|
||||
defer log.mu.Unlock()
|
||||
return log.text[log.order[len(log.order)-1]]
|
||||
}
|
||||
|
||||
if got := say("UANA", "grant <@UANA> all"); !strings.Contains(got, ":lock:") {
|
||||
t.Fatalf("non-managers can't grant: %q", got)
|
||||
}
|
||||
if got := say("UBOSS", "grant <@UANA> move manage"); !strings.Contains(got, "<@UANA> can now: move, manage") {
|
||||
t.Fatalf("manager grant: %q", got)
|
||||
}
|
||||
if got := say("UANA", "grant <@UCAL> scale"); !strings.Contains(got, "<@UCAL> can now: scale") {
|
||||
t.Fatalf("channel manager grant: %q", got)
|
||||
}
|
||||
if got := say("UANA", "grant default <@UCAL> all"); !strings.Contains(got, ":lock:") {
|
||||
t.Fatalf("channel managers can't touch the default list: %q", got)
|
||||
}
|
||||
if got := say("UANA", "managers add <@UCAL>"); !strings.Contains(got, ":lock:") {
|
||||
t.Fatalf("only user managers add managers: %q", got)
|
||||
}
|
||||
if got := say("UBOSS", "managers remove <@UBOSS>"); !strings.Contains(got, "nobody able to manage") {
|
||||
t.Fatalf("last manager guard: %q", got)
|
||||
}
|
||||
if got := say("UBOSS", "dm storage off"); !strings.Contains(got, "now turned off") {
|
||||
t.Fatalf("dm toggle: %q", got)
|
||||
}
|
||||
if store.Permissions().Allowed(model.OwnerUser, "D1", "UX", model.ActStorage) {
|
||||
t.Fatal("dm toggle not saved")
|
||||
}
|
||||
if got := say("UANA", "grant me access to the repo"); got != "(went to the agent)" {
|
||||
t.Fatalf("plain sentence: %q", got)
|
||||
}
|
||||
if got := say("UANA", "permissions reset"); !strings.Contains(got, "default super-user list again") {
|
||||
t.Fatalf("reset: %q", got)
|
||||
}
|
||||
if store.Permissions().Allowed(model.OwnerChannel, "C1", "UCAL", model.ActScale) {
|
||||
t.Fatal("reset should drop the channel's own list")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user