95 lines
3.2 KiB
Go
95 lines
3.2 KiB
Go
package tools
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"io"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"slackerbot/internal/acp"
|
|
)
|
|
|
|
func TestEveryCommandIsListed(t *testing.T) {
|
|
s := &Server{Home: t.TempDir(), MaxMB: 1}
|
|
var names []string
|
|
for _, tool := range s.tools() {
|
|
names = append(names, tool.(map[string]any)["name"].(string))
|
|
}
|
|
for _, want := range []string{"send_file", "status", "sessions", "nodes", "permissions", "stop", "scale", "keep",
|
|
"storage", "move", "listen", "grant", "revoke", "reset_permissions", "dm_permission", "managers"} {
|
|
if !strings.Contains(" "+strings.Join(names, " ")+" ", " "+want+" ") {
|
|
t.Fatalf("%s missing from %v", want, names)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestCommandWaitsForTheBotsReply(t *testing.T) {
|
|
home := t.TempDir()
|
|
s := &Server{Home: home, MaxMB: 1, Wait: 300 * time.Millisecond}
|
|
os.MkdirAll(replyDir(home), 0o755)
|
|
old := ReplyPath(home, "keep", "24h")
|
|
os.WriteFile(old, []byte(`{"text":"stale"}`), 0o644)
|
|
os.Chtimes(old, time.Now().Add(-time.Hour), time.Now().Add(-time.Hour))
|
|
|
|
inR, inW := io.Pipe()
|
|
var out bytes.Buffer
|
|
done := make(chan error)
|
|
go func() { done <- s.Serve(inR, &out) }()
|
|
inW.Write([]byte(`{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"keep","arguments":{"args":" 24h"}}}` + "\n"))
|
|
inW.Write([]byte(`{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"scale","arguments":{}}}` + "\n"))
|
|
inW.Write([]byte(`{"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"nodes","arguments":{}}}` + "\n"))
|
|
time.Sleep(50 * time.Millisecond)
|
|
data, _ := json.Marshal(Reply{Text: "Done! 24h."})
|
|
os.WriteFile(ReplyPath(home, "keep", "24h"), data, 0o644)
|
|
inW.Close()
|
|
if err := <-done; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
got := map[int]string{}
|
|
errs := map[int]bool{}
|
|
for _, line := range strings.Split(strings.TrimSpace(out.String()), "\n") {
|
|
var r struct {
|
|
ID int
|
|
Result struct {
|
|
Content []struct{ Text string }
|
|
IsError bool
|
|
}
|
|
}
|
|
_ = json.Unmarshal([]byte(line), &r)
|
|
got[r.ID], errs[r.ID] = r.Result.Content[0].Text, r.Result.IsError
|
|
}
|
|
if got[1] != "Done! 24h." || errs[1] {
|
|
t.Fatalf("keep: %q", got[1])
|
|
}
|
|
if !strings.Contains(got[2], "args is required") || !errs[2] {
|
|
t.Fatalf("scale without args: %q", got[2])
|
|
}
|
|
if !strings.Contains(got[3], "didn't answer in time") || !errs[3] {
|
|
t.Fatalf("unanswered: %q", got[3])
|
|
}
|
|
if left, _ := os.ReadDir(replyDir(home)); len(left) != 0 {
|
|
t.Fatalf("replies should be cleaned up, left %v", left)
|
|
}
|
|
}
|
|
|
|
func TestCommandCall(t *testing.T) {
|
|
c, args, ok := CommandCall(acp.Update{Kind: "tool_call", ToolName: "slackerbot__reset_permissions"})
|
|
if !ok || c.Text(args) != "permissions reset" {
|
|
t.Fatalf("by tool name: %+v %q %v", c, args, ok)
|
|
}
|
|
c, args, ok = CommandCall(acp.Update{Kind: "tool_call", Title: "slackerbot: scale · up", RawInput: json.RawMessage(`{"args":"up"}`)})
|
|
if !ok || c.Text(args) != "scale up" {
|
|
t.Fatalf("by title: %+v %q %v", c, args, ok)
|
|
}
|
|
if _, _, ok := CommandCall(acp.Update{Kind: "tool_call", ToolName: "slackerbot__send_file"}); ok {
|
|
t.Fatal("send_file isn't a command")
|
|
}
|
|
if _, _, ok := CommandCall(acp.Update{Kind: "tool_call", ToolName: "developer__shell", Title: "status"}); ok {
|
|
t.Fatal("other servers' tools aren't commands")
|
|
}
|
|
}
|