package server import ( "os/exec" "strings" "testing" ) func TestShowDiffHeadAndPaths(t *testing.T) { f := newChatToolFixture(t) repo := showDiffRepo(t, f.home) token := f.token(t, chatToolSessionScope+"ses_o") if out, err := exec.Command("git", "-C", repo, "switch", "-q", "--discard-changes", "main").CombinedOutput(); err != nil { t.Fatalf("switch to main: %v %s", err, out) } // head instead of the work tree: feature against HEAD (main). r := f.call(token, "show_diff", `{"path":"`+repo+`","head":"feature"}`) if r.IsError { t.Fatal(r.Text) } item := diffItem(t, f, "ses_o") if got := diffPaths(item); len(got) != 2 || got["a.txt"] != "modified" || got["b.txt"] != "added" { t.Fatalf("head compares the revision, without untracked files: %v", got) } if item.Diff.HeadRevision != "feature" || item.Diff.Head != "feature" || !strings.Contains(r.Text, "showing feature") { t.Fatalf("summary: %+v %s", item.Diff, r.Text) } // head against a base, limited to a path and a glob. if r := f.call(token, "show_diff", `{"path":"`+repo+`","head":"feature","base":"main","paths":["b.txt"]}`); r.IsError { t.Fatal(r.Text) } item = diffItem(t, f, "ses_o") if got := diffPaths(item); len(got) != 1 || got["b.txt"] != "added" || len(item.Diff.Paths) != 1 { t.Fatalf("paths limits the diff: %v %+v", got, item.Diff) } if r := f.call(token, "show_diff", `{"path":"`+repo+`","paths":["*.txt"]}`); r.IsError { t.Fatal(r.Text) } if got := diffPaths(diffItem(t, f, "ses_o")); len(got) != 1 || got["u.txt"] != "added" { t.Fatalf("a glob matches untracked work too: %v", got) } for _, tc := range []struct{ args, want string }{ {`{"path":"` + repo + `","head":"--output=/tmp/x"}`, "head \"--output=/tmp/x\" is not a branch"}, {`{"path":"` + repo + `","head":"nope"}`, "is not a commit"}, {`{"path":"` + repo + `","head":"feature","base":"main","committed_only":true}`, "committed_only does not apply"}, {`{"path":"` + repo + `","paths":["../outside"]}`, "leaves the repository"}, {`{"path":"` + repo + `","paths":["/etc/passwd"]}`, "must be a path inside the repository"}, {`{"path":"` + repo + `","paths":[":(top)x"]}`, "must be a path inside the repository"}, {`{"path":"` + repo + `","paths":["-x"]}`, "must be a path inside the repository"}, {`{"path":"` + repo + `","paths":"a.txt"}`, "paths must be an array of strings"}, } { 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) } } }