059eb1bb0d
The Pipelines MCP server gains show_diff: it runs Git in a repository inside the operator's home (default: the chat's directory) and records the per-file patches. Without base it shows uncommitted work against HEAD; with base it shows everything since the merge base (a pull request preview), optionally committed changes only. Bounded at 300 files, 256 KiB per patch and 1 MiB total, with partial diffs labelled; diff bodies share the per-chat retention budget with HTML views. The chat draws the item as a card listing each changed file with its stat; a row opens the existing file diff viewer. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
200 lines
6.8 KiB
Go
200 lines
6.8 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
)
|
|
|
|
// showDiffRepo makes a repository inside the fixture home: main has a.txt, the
|
|
// checked-out feature branch modifies it and adds b.txt in commits, and the
|
|
// work tree has an unstaged change to b.txt plus an untracked u.txt.
|
|
func showDiffRepo(t *testing.T, home string) string {
|
|
t.Helper()
|
|
repo := filepath.Join(home, "repo")
|
|
if err := os.MkdirAll(repo, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
git := func(args ...string) {
|
|
t.Helper()
|
|
cmd := exec.Command("git", append([]string{"-c", "user.name=t", "-c", "user.email=t@example.com", "-c", "commit.gpgsign=false", "-C", repo}, args...)...)
|
|
if out, err := cmd.CombinedOutput(); err != nil {
|
|
t.Fatalf("git %v: %v\n%s", args, err, out)
|
|
}
|
|
}
|
|
write := func(name, content string) {
|
|
t.Helper()
|
|
if err := os.WriteFile(filepath.Join(repo, name), []byte(content), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
git("init", "-q", "-b", "main")
|
|
write("a.txt", "one\ntwo\n")
|
|
git("add", ".")
|
|
git("commit", "-q", "-m", "base")
|
|
git("switch", "-q", "-c", "feature")
|
|
write("a.txt", "one\nTWO\n")
|
|
write("b.txt", "new\n")
|
|
git("add", ".")
|
|
git("commit", "-q", "-m", "feature")
|
|
write("b.txt", "new\nmore\n")
|
|
write("u.txt", "untracked\n")
|
|
return repo
|
|
}
|
|
|
|
func diffItem(t *testing.T, f *chatToolFixture, session string) protocol.ChatShownItem {
|
|
t.Helper()
|
|
items := f.items(t, session)
|
|
if len(items) == 0 {
|
|
t.Fatal("no item recorded")
|
|
}
|
|
item := items[len(items)-1]
|
|
if item.Kind != protocol.ChatShownDiff || item.Diff == nil || item.Diff.Changes == nil {
|
|
t.Fatalf("want a diff item, got %+v", item)
|
|
}
|
|
return item
|
|
}
|
|
|
|
func diffPaths(item protocol.ChatShownItem) map[string]string {
|
|
paths := map[string]string{}
|
|
for _, file := range item.Diff.Changes.Files {
|
|
paths[file.Path] = file.Kind
|
|
}
|
|
return paths
|
|
}
|
|
|
|
func TestShowDiffComparesAgainstHeadOrABase(t *testing.T) {
|
|
f := newChatToolFixture(t)
|
|
repo := showDiffRepo(t, f.home)
|
|
token := f.token(t, chatToolSessionScope+"ses_d")
|
|
|
|
r := f.call(token, "show_diff", `{"path":"`+repo+`"}`)
|
|
if r.IsError {
|
|
t.Fatal(r.Text)
|
|
}
|
|
item := diffItem(t, f, "ses_d")
|
|
if got := diffPaths(item); len(got) != 2 || got["b.txt"] != "modified" || got["u.txt"] != "added" {
|
|
t.Fatalf("uncommitted diff lists the work tree change and the untracked file: %v", got)
|
|
}
|
|
if item.Diff.Head != "feature" || item.Diff.Base != "" || item.Diff.Additions != 2 || item.Diff.Deletions != 0 {
|
|
t.Fatalf("summary: %+v", item.Diff)
|
|
}
|
|
if !strings.Contains(r.Text, "2 changed files (+2 -0)") {
|
|
t.Fatalf("model text: %s", r.Text)
|
|
}
|
|
|
|
if r := f.call(token, "show_diff", `{"path":"~/repo","base":"main","title":"PR preview"}`); r.IsError {
|
|
t.Fatal(r.Text)
|
|
}
|
|
item = diffItem(t, f, "ses_d")
|
|
if got := diffPaths(item); len(got) != 3 || got["a.txt"] != "modified" || got["b.txt"] != "added" || got["u.txt"] != "added" {
|
|
t.Fatalf("a base diff covers the branch and uncommitted work: %v", got)
|
|
}
|
|
if item.Title != "PR preview" || item.Diff.Base != "main" || item.Diff.BaseCommit == "" || item.DiffBytes == 0 {
|
|
t.Fatalf("summary: %+v", item)
|
|
}
|
|
for _, file := range item.Diff.Changes.Files {
|
|
if file.Path == "a.txt" && !strings.Contains(file.Patch, "-two\n+TWO") {
|
|
t.Fatalf("a.txt patch: %q", file.Patch)
|
|
}
|
|
}
|
|
|
|
if r := f.call(token, "show_diff", `{"path":"`+repo+`","base":"main","committed_only":true}`); r.IsError {
|
|
t.Fatal(r.Text)
|
|
}
|
|
item = diffItem(t, f, "ses_d")
|
|
got := diffPaths(item)
|
|
if len(got) != 2 || got["a.txt"] != "modified" || got["b.txt"] != "added" {
|
|
t.Fatalf("committed_only leaves out the work tree: %v", got)
|
|
}
|
|
for _, file := range item.Diff.Changes.Files {
|
|
if file.Path == "b.txt" && strings.Contains(file.Patch, "more") {
|
|
t.Fatal("committed_only must not include the unstaged line")
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestShowDiffRefusals(t *testing.T) {
|
|
f := newChatToolFixture(t)
|
|
repo := showDiffRepo(t, f.home)
|
|
plain := filepath.Join(f.home, "plain")
|
|
if err := os.MkdirAll(plain, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
outside := t.TempDir()
|
|
token := f.token(t, chatToolSessionScope+"ses_r")
|
|
for _, tc := range []struct{ args, want string }{
|
|
{`{"path":"` + outside + `"}`, "outside the operator's home"},
|
|
{`{"path":"` + plain + `"}`, "not inside a Git repository"},
|
|
{`{"path":"` + repo + `","base":"--output=/tmp/x"}`, "is not a branch"},
|
|
{`{"path":"` + repo + `","base":"main..feature"}`, "is not a branch"},
|
|
{`{"path":"` + repo + `","base":"nope"}`, "is not a commit"},
|
|
{`{"path":"` + repo + `","committed_only":true}`, "needs a base"},
|
|
{`{}`, "no recorded working directory"},
|
|
} {
|
|
r := f.call(token, "show_diff", tc.args)
|
|
if !r.IsError || !strings.Contains(r.Text, tc.want) {
|
|
t.Errorf("%s: want refusal %q, got %+v", tc.args, tc.want, r)
|
|
}
|
|
}
|
|
if items := f.items(t, "ses_r"); len(items) != 0 {
|
|
t.Fatalf("refusals record nothing: %+v", items)
|
|
}
|
|
}
|
|
|
|
func TestShowDiffLeavesOutOversizedPatchesAndSaysSo(t *testing.T) {
|
|
f := newChatToolFixture(t)
|
|
repo := showDiffRepo(t, f.home)
|
|
big := strings.Repeat("line of text\n", (showDiffMaxFileBytes/13)+10)
|
|
if err := os.WriteFile(filepath.Join(repo, "big.txt"), []byte(big), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(repo, "blob.bin"), []byte{0, 1, 2, 0, 3}, 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
token := f.token(t, chatToolSessionScope+"ses_big")
|
|
r := f.call(token, "show_diff", `{"path":"`+repo+`"}`)
|
|
if r.IsError {
|
|
t.Fatal(r.Text)
|
|
}
|
|
item := diffItem(t, f, "ses_big")
|
|
if item.Diff.Changes.Status != "partial" || !strings.Contains(r.Text, "partial") {
|
|
t.Fatalf("an oversized patch makes the diff partial: %+v / %s", item.Diff.Changes, r.Text)
|
|
}
|
|
for _, file := range item.Diff.Changes.Files {
|
|
switch file.Path {
|
|
case "big.txt":
|
|
if file.Patch != "" || file.NoPatchReason != "file_size" || !file.Truncated {
|
|
t.Fatalf("big.txt: %+v", file)
|
|
}
|
|
case "blob.bin":
|
|
if !file.Binary || file.Patch != "" {
|
|
t.Fatalf("blob.bin: %+v", file)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestShowDiffBodiesShareTheRetentionBudget(t *testing.T) {
|
|
f := newChatToolFixture(t)
|
|
ctx := context.Background()
|
|
body := func() *protocol.ChatShownDiffBody {
|
|
return &protocol.ChatShownDiffBody{Repo: "/r", Files: 1, Changes: &protocol.StageFileChanges{Status: "complete", Files: []protocol.FileChange{{Path: "a", Kind: "modified", Patch: "@@"}}}}
|
|
}
|
|
for i, id := range []string{"old", "new"} {
|
|
item := protocol.ChatShownItem{ID: id, Kind: protocol.ChatShownDiff, CreatedAt: int64(i + 1), Diff: body(), DiffBytes: protocol.ChatShownHTMLBudget/2 + 1}
|
|
if err := f.store.AppendChatShownItem(ctx, "ses_ret", item); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
items := f.items(t, "ses_ret")
|
|
if len(items) != 2 || !items[0].Expired || items[0].Diff == nil || items[0].Diff.Changes != nil || items[1].Expired || items[1].Diff.Changes == nil {
|
|
t.Fatalf("the older diff drops its patches past the budget: %+v", items)
|
|
}
|
|
}
|