Files

331 lines
13 KiB
Go

package server
import (
"context"
"encoding/json"
"os"
"path/filepath"
"strings"
"testing"
"time"
"git.a-wan.net/alan/pipelines/internal/chattools"
"git.a-wan.net/alan/pipelines/internal/pipeline"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
type chatToolFixture struct {
store *pipeline.Store
service *chatToolService
hub *chatHub
home string
notified []string
}
func newChatToolFixture(t *testing.T) *chatToolFixture {
t.Helper()
store, err := pipeline.Open(filepath.Join(t.TempDir(), "tools.db"))
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { store.Close() })
f := &chatToolFixture{store: store, hub: newChatHub(), home: t.TempDir()}
f.hub.shown = store
f.service = newChatToolService(store, store, f.hub, store)
f.service.home = func() (string, error) { return f.home, nil }
f.service.notify = func(_ context.Context, sessionID, title, message string) (bool, string) {
f.notified = append(f.notified, sessionID+": "+title)
return true, ""
}
return f
}
func (f *chatToolFixture) token(t *testing.T, scope string) string {
t.Helper()
token, err := f.store.ChatToolToken(context.Background(), scope)
if err != nil {
t.Fatal(err)
}
return token
}
func (f *chatToolFixture) call(token, tool, arguments string) chattools.Result {
return f.service.Call(context.Background(), token, tool, json.RawMessage(arguments))
}
func (f *chatToolFixture) items(t *testing.T, sessionID string) []protocol.ChatShownItem {
t.Helper()
items, err := f.store.ChatShownItems(context.Background(), sessionID)
if err != nil {
t.Fatal(err)
}
return items
}
func TestChatToolTokensAttributeCallsToExactlyOneChat(t *testing.T) {
f := newChatToolFixture(t)
a, b := f.token(t, chatToolSessionScope+"ses_a"), f.token(t, chatToolSessionScope+"ses_b")
if a == b || len(a) != 64 || !f.service.Known(context.Background(), a) {
t.Fatalf("tokens must be distinct 256-bit values: %q %q", a, b)
}
if again := f.token(t, chatToolSessionScope+"ses_a"); again != a {
t.Fatal("a session keeps its token (a detached harness keeps its endpoint)")
}
if f.service.Known(context.Background(), strings.Repeat("0", 64)) {
t.Fatal("an unknown token must be refused")
}
if r := f.call(a, "show_html", `{"html":"<p>A</p>","title":"For A"}`); r.IsError {
t.Fatal(r.Text)
}
if got := f.items(t, "ses_a"); len(got) != 1 || got[0].Kind != protocol.ChatShownHTML || got[0].Title != "For A" || got[0].HTML != "<p>A</p>" || got[0].ID == "" || got[0].CreatedAt == 0 {
t.Fatalf("ses_a items = %+v", got)
}
if got := f.items(t, "ses_b"); len(got) != 0 {
t.Fatalf("another chat must not receive the item: %+v", got)
}
frame := f.hub.shownFrame("ses_a")
if frame == nil || frame.SessionID != "ses_a" || len(frame.Items) != 1 {
t.Fatalf("subscribe frame = %+v", frame)
}
if f.hub.shownFrame("ses_b") != nil {
t.Fatal("a chat with no items has no frame")
}
registry := chatToolRegistry{store: f.store, base: "http://127.0.0.1:1"}
url, bind, err := registry.PendingURL()
if err != nil {
t.Fatal(err)
}
pending := strings.TrimPrefix(url, "http://127.0.0.1:1"+chatToolPath)
if r := f.call(pending, "show_html", `{"html":"x"}`); !r.IsError {
t.Fatal("an unbound pending token must not show anything")
}
if err := bind("019a6f7e-1c2d-7e3f-8a9b-0c1d2e3f4a5b"); err != nil {
t.Fatal(err)
}
if r := f.call(pending, "show_html", `{"html":"x"}`); r.IsError || len(f.items(t, "019a6f7e-1c2d-7e3f-8a9b-0c1d2e3f4a5b")) != 1 {
t.Fatalf("a bound token serves its thread: %v", r)
}
}
func TestShowFileStaysInsideHome(t *testing.T) {
f := newChatToolFixture(t)
token := f.token(t, chatToolSessionScope+"ses_f")
inside := filepath.Join(f.home, "work", "report.md")
if err := os.MkdirAll(filepath.Dir(inside), 0o700); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(inside, []byte("# Report\n"), 0o600); err != nil {
t.Fatal(err)
}
outside := filepath.Join(t.TempDir(), "secret.txt")
if err := os.WriteFile(outside, []byte("secret"), 0o600); err != nil {
t.Fatal(err)
}
if err := os.Symlink(outside, filepath.Join(f.home, "link.txt")); err != nil {
t.Fatal(err)
}
if err := f.store.RecordOperatorSession(context.Background(), "ses_f", filepath.Join(f.home, "work"), "Files", protocol.ChatSessionTagOperator); err != nil {
t.Fatal(err)
}
r := f.call(token, "show_file", `{"path":"`+inside+`","title":"The report"}`)
if r.IsError || !strings.Contains(r.Text, "report.md") {
t.Fatalf("inside home: %v", r)
}
realHome, _ := filepath.EvalSymlinks(f.home)
got := f.items(t, "ses_f")
if len(got) != 1 || got[0].Path != filepath.Join(realHome, "work", "report.md") || got[0].Name != "report.md" || got[0].Size != 9 || !strings.HasPrefix(got[0].Mime, "text/") {
t.Fatalf("item = %+v", got)
}
if r := f.call(token, "show_file", `{"path":"report.md"}`); r.IsError {
t.Fatalf("a relative path resolves against the chat's directory: %v", r)
}
if r := f.call(token, "show_file", `{"path":"~/work/report.md"}`); r.IsError {
t.Fatalf("a ~/ path resolves in home: %v", r)
}
for _, refused := range []string{outside, filepath.Join(f.home, "link.txt"), filepath.Join(f.home, "work"), filepath.Join(f.home, "missing.png"), "../../../etc/passwd", ""} {
if r := f.call(token, "show_file", `{"path":"`+refused+`"}`); !r.IsError {
t.Fatalf("%q must be refused: %v", refused, r)
}
}
if len(f.items(t, "ses_f")) != 3 {
t.Fatal("refused paths must record nothing")
}
}
func TestShowImageAcceptsHomeImagesAndHTTPSOnly(t *testing.T) {
f := newChatToolFixture(t)
token := f.token(t, chatToolSessionScope+"ses_i")
image := filepath.Join(f.home, "plot.png")
if err := os.WriteFile(image, []byte("\x89PNG\r\n\x1a\n"), 0o600); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(f.home, "notes.txt"), []byte("x"), 0o600); err != nil {
t.Fatal(err)
}
if r := f.call(token, "show_image", `{"url_or_path":"`+image+`","alt":"A plot"}`); r.IsError {
t.Fatal(r.Text)
}
if r := f.call(token, "show_image", `{"url_or_path":"https://example.com/a.png","title":"Remote"}`); r.IsError {
t.Fatal(r.Text)
}
for _, refused := range []string{"http://example.com/a.png", "https://user:pw@example.com/a.png", "javascript:alert(1)", "file:///etc/passwd", filepath.Join(f.home, "notes.txt")} {
if r := f.call(token, "show_image", `{"url_or_path":"`+refused+`"}`); !r.IsError {
t.Fatalf("%q must be refused: %v", refused, r)
}
}
got := f.items(t, "ses_i")
if len(got) != 2 || got[0].Kind != protocol.ChatShownImage || got[0].Mime != "image/png" || got[0].Alt != "A plot" || got[1].URL != "https://example.com/a.png" || got[1].Path != "" {
t.Fatalf("items = %+v", got)
}
}
func TestShowHTMLSizeLimitAndRetention(t *testing.T) {
f := newChatToolFixture(t)
token := f.token(t, chatToolSessionScope+"ses_h")
if r := f.call(token, "show_html", `{"html":"`+strings.Repeat("a", chattools.MaxHTMLBytes+1)+`"}`); !r.IsError || !strings.Contains(r.Text, "512 KiB") {
t.Fatalf("an oversized document must be refused with the limit: %v", r)
}
if r := f.call(token, "show_html", `{"html":" "}`); !r.IsError {
t.Fatal("an empty document must be refused")
}
if len(f.items(t, "ses_h")) != 0 {
t.Fatal("refused documents must record nothing")
}
full := strings.Repeat("b", chattools.MaxHTMLBytes)
for range 5 {
if r := f.call(token, "show_html", `{"html":"`+full+`","height_hint":5000}`); r.IsError {
t.Fatal(r.Text)
}
}
got := f.items(t, "ses_h")
if len(got) != 5 || got[0].HeightHint != 600 {
t.Fatalf("items = %d, hint %d", len(got), got[0].HeightHint)
}
// 2 MiB of HTML is retained: the newest four bodies, the oldest expired.
if !got[0].Expired || got[0].HTML != "" || got[0].HTMLBytes != chattools.MaxHTMLBytes {
t.Fatalf("the oldest body must be dropped under the budget: expired=%v len=%d", got[0].Expired, len(got[0].HTML))
}
for _, item := range got[1:] {
if item.Expired || len(item.HTML) != chattools.MaxHTMLBytes {
t.Fatal("the newest bodies must be kept")
}
}
for range protocol.ChatShownLimit + 5 {
f.service.now = func() time.Time { return time.Now().Add(time.Hour) }
if r := f.call(token, "show_html", `{"html":"<i>x</i>"}`); r.IsError {
t.Fatal(r.Text)
}
}
if got := f.items(t, "ses_h"); len(got) != protocol.ChatShownLimit {
t.Fatalf("a chat retains at most %d items, got %d", protocol.ChatShownLimit, len(got))
}
}
func TestNotifyIsRateLimitedPerChat(t *testing.T) {
f := newChatToolFixture(t)
a, b := f.token(t, chatToolSessionScope+"ses_n1"), f.token(t, chatToolSessionScope+"ses_n2")
now := time.Unix(1_800_000_000, 0)
f.service.now = func() time.Time { return now }
for i := range 5 {
if r := f.call(a, "notify", `{"title":"Done","message":"Build finished","priority":"high"}`); r.IsError {
t.Fatalf("notification %d: %v", i, r)
}
}
if r := f.call(a, "notify", `{"title":"Done","message":"again"}`); !r.IsError || !strings.Contains(r.Text, "5 notifications") {
t.Fatalf("the sixth in a minute must be refused: %v", r)
}
if r := f.call(b, "notify", `{"title":"Other","message":"chat"}`); r.IsError {
t.Fatalf("another chat has its own limit: %v", r)
}
now = now.Add(61 * time.Second)
if r := f.call(a, "notify", `{"title":"Later","message":"ok"}`); r.IsError {
t.Fatalf("the window slides: %v", r)
}
if r := f.call(a, "notify", `{"title":"Bad","message":"x","priority":"urgent"}`); !r.IsError {
t.Fatal("an unknown priority must be refused")
}
if len(f.notified) != 7 || f.notified[0] != "ses_n1: Done" {
t.Fatalf("notified = %v", f.notified)
}
got := f.items(t, "ses_n1")
if len(got) != 6 || got[0].Kind != protocol.ChatShownNotify || !got[0].Delivered || got[0].Priority != "high" {
t.Fatalf("items = %+v", got)
}
f.service.notify = func(context.Context, string, string, string) (bool, string) { return false, "ntfy is not configured" }
r := f.call(b, "notify", `{"title":"Fails","message":"x"}`)
if !r.IsError || !strings.Contains(r.Text, "ntfy is not configured") {
t.Fatalf("an undelivered notification is reported: %v", r)
}
if last := f.items(t, "ses_n2"); len(last) != 2 || last[1].Delivered || last[1].Detail != "ntfy is not configured" {
t.Fatalf("the undelivered entry is recorded: %+v", last)
}
}
func TestInstanceTokenCallsNeedOneMatchingRunningToolCall(t *testing.T) {
f := newChatToolFixture(t)
token := f.token(t, chatToolInstanceScope+"opencode")
saved := chatToolCorrelationWait
chatToolCorrelationWait = 100 * time.Millisecond
t.Cleanup(func() { chatToolCorrelationWait = saved })
arguments := `{"html":"<p>diagram</p>","title":"Plan"}`
if r := f.call(token, "show_html", arguments); !r.IsError {
t.Fatal("a shared-instance call with no running tool call must be refused")
}
running := func(sessionID, name, input string) {
f.hub.apply(protocol.ChatEventFrame{SessionID: sessionID, MessageID: "msg_" + sessionID, Kind: protocol.ChatEventTool,
Part: &protocol.ChatPart{Type: "tool", ID: "prt_" + sessionID, Name: name, State: &protocol.ChatToolState{Status: protocol.ChatToolRunning, Input: json.RawMessage(input)}}})
}
running("ses_x", "pipelines_show_html", `{"title":"Plan","html":"<p>diagram</p>"}`)
running("ses_y", "pipelines_show_html", `{"html":"<p>other</p>"}`)
running("ses_z", "bash", arguments)
if r := f.call(token, "show_html", arguments); r.IsError {
t.Fatal(r.Text)
}
if len(f.items(t, "ses_x")) != 1 || len(f.items(t, "ses_y"))+len(f.items(t, "ses_z")) != 0 {
t.Fatal("the call belongs to the chat running exactly this call")
}
running("ses_y", "pipelines_show_html", arguments)
if r := f.call(token, "show_html", arguments); !r.IsError || !strings.Contains(r.Text, "identical") {
t.Fatalf("two chats running the same call must be refused: %v", r)
}
}
// OpenCode V2 calls MCP tools from its code-mode "execute" script, so the
// stream shows the script, not a tool part of the Pipelines tool's own name.
func TestInstanceTokenCallsMatchOpenCodeCodeModeScripts(t *testing.T) {
f := newChatToolFixture(t)
token := f.token(t, chatToolInstanceScope+"opencode")
saved := chatToolCorrelationWait
chatToolCorrelationWait = 100 * time.Millisecond
t.Cleanup(func() { chatToolCorrelationWait = saved })
script := func(sessionID, part, code string) {
input, _ := json.Marshal(map[string]string{"code": code})
f.hub.apply(protocol.ChatEventFrame{SessionID: sessionID, MessageID: "msg_" + sessionID, Kind: protocol.ChatEventTool,
Part: &protocol.ChatPart{Type: "tool", ID: part, Name: "execute", State: &protocol.ChatToolState{Status: protocol.ChatToolRunning, Input: input}}})
}
if err := os.WriteFile(filepath.Join(f.home, "a.png"), []byte("\x89PNG\r\n\x1a\n"), 0o600); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(f.home, "b.png"), []byte("\x89PNG\r\n\x1a\n"), 0o600); err != nil {
t.Fatal(err)
}
script("ses_p", "prt_p", `return tools.pipelines.show_file({ path: "~/a.png", title: "Checker" })`)
script("ses_q", "prt_q", `return tools.other.show_file({ path: "~/a.png" })`)
if r := f.call(token, "show_file", `{"path":"~/a.png","title":"Checker"}`); r.IsError {
t.Fatal(r.Text)
}
if len(f.items(t, "ses_p")) != 1 || len(f.items(t, "ses_q")) != 0 {
t.Fatal("the script calling the Pipelines tool owns the call")
}
script("ses_q", "prt_q2", `await tools.pipelines.show_file({ path: "~/b.png" })`)
if r := f.call(token, "show_file", `{"path":"~/b.png"}`); r.IsError || len(f.items(t, "ses_q")) != 1 {
t.Fatalf("two scripts are told apart by their arguments: %v", r)
}
if r := f.call(token, "show_file", `{"path":"~/c.png"}`); !r.IsError {
t.Fatal("arguments neither script contains must not be guessed into a chat")
}
}