Files
slacker-bot/slackerbot/internal/tools/server_test.go
T
2026-11-01 15:21:43 -05:00

52 lines
1.6 KiB
Go

package tools
import (
"bytes"
"encoding/json"
"os"
"path/filepath"
"strings"
"testing"
)
func TestSendFileTool(t *testing.T) {
home := t.TempDir()
os.WriteFile(filepath.Join(home, "chart.png"), []byte("png"), 0o644)
os.Mkdir(filepath.Join(home, "dir"), 0o755)
os.Symlink("/etc/passwd", filepath.Join(home, "escape"))
s := &Server{Home: home, MaxMB: 1}
call := func(path string) string {
return `{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"send_file","arguments":{"path":"` + path + `"}}}`
}
in := strings.Join([]string{
`{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-06-18"}}`,
`{"jsonrpc":"2.0","method":"notifications/initialized"}`,
call("chart.png"), call(home + "/dir"), call(home + "/escape"), call("missing"),
}, "\n")
var out bytes.Buffer
if err := s.Serve(strings.NewReader(in), &out); err != nil {
t.Fatal(err)
}
lines := strings.Split(strings.TrimSpace(out.String()), "\n")
if len(lines) != 5 || !strings.Contains(lines[0], `"protocolVersion":"2025-06-18"`) || !strings.Contains(lines[0], "devShells.default") {
t.Fatalf("replies: %v", lines)
}
want := []struct {
text string
isErr bool
}{{"queued", false}, {"regular file", true}, {"isn't under", true}, {"can't find", true}}
for i, w := range want {
var r struct {
Result struct {
Content []struct{ Text string }
IsError bool
}
}
_ = json.Unmarshal([]byte(lines[i+1]), &r)
if r.Result.IsError != w.isErr || !strings.Contains(r.Result.Content[0].Text, w.text) {
t.Fatalf("call %d: %s", i, lines[i+1])
}
}
}