v0.2.2 872a312

tools_test.go RawHistoryFolder

1package tools
2
3import (
4	"context"
5	"encoding/json"
6	"os"
7	"path/filepath"
8	"runtime"
9	"strings"
10	"testing"
11
12	"github.com/patriceckhart/zot/packages/provider"
13)
14
15func mustJSON(t *testing.T, v any) json.RawMessage {
16	t.Helper()
17	b, err := json.Marshal(v)
18	if err != nil {
19		t.Fatal(err)
20	}
21	return b
22}
23
24func TestReadText(t *testing.T) {
25	dir := t.TempDir()
26	p := filepath.Join(dir, "a.txt")
27	if err := os.WriteFile(p, []byte("hello\nworld\n"), 0o644); err != nil {
28		t.Fatal(err)
29	}
30	tool := &ReadTool{CWD: dir}
31	res, err := tool.Execute(context.Background(), mustJSON(t, map[string]any{"path": "a.txt"}), nil)
32	if err != nil {
33		t.Fatal(err)
34	}
35	got := res.Content[0].(provider.TextBlock).Text
36	if !strings.Contains(got, "hello") || !strings.Contains(got, "world") {
37		t.Fatalf("got %q", got)
38	}
39}
40
41func TestReadOffsetLimit(t *testing.T) {
42	dir := t.TempDir()
43	p := filepath.Join(dir, "a.txt")
44	os.WriteFile(p, []byte("1\n2\n3\n4\n5\n"), 0o644)
45	tool := &ReadTool{CWD: dir}
46	res, _ := tool.Execute(context.Background(), mustJSON(t, map[string]any{"path": "a.txt", "offset": 2, "limit": 2}), nil)
47	got := res.Content[0].(provider.TextBlock).Text
48	// Current output format is raw bytes (no embedded line numbers):
49	// the tui draws its own gutter from the `start_line` detail.
50	if got != "2\n3\n" {
51		t.Fatalf("want \"2\\n3\\n\", got %q", got)
52	}
53	if start, ok := res.Details.(map[string]any)["start_line"]; !ok || start != 2 {
54		t.Errorf("start_line detail want 2, got %v", start)
55	}
56}
57
58func TestReadBinaryRejected(t *testing.T) {
59	dir := t.TempDir()
60	p := filepath.Join(dir, "b.bin")
61	os.WriteFile(p, []byte{0x00, 0x01, 0x02}, 0o644)
62	tool := &ReadTool{CWD: dir}
63	if _, err := tool.Execute(context.Background(), mustJSON(t, map[string]any{"path": "b.bin"}), nil); err == nil {
64		t.Fatal("want binary rejection")
65	}
66}
67
68func TestWriteCreatesDirs(t *testing.T) {
69	dir := t.TempDir()
70	tool := &WriteTool{CWD: dir}
71	_, err := tool.Execute(context.Background(), mustJSON(t, map[string]any{"path": "sub/a.txt", "content": "hi"}), nil)
72	if err != nil {
73		t.Fatal(err)
74	}
75	b, err := os.ReadFile(filepath.Join(dir, "sub", "a.txt"))
76	if err != nil {
77		t.Fatal(err)
78	}
79	if string(b) != "hi" {
80		t.Fatalf("got %q", string(b))
81	}
82}
83
84func TestEditSingle(t *testing.T) {
85	dir := t.TempDir()
86	p := filepath.Join(dir, "a.txt")
87	os.WriteFile(p, []byte("hello world\n"), 0o644)
88	tool := &EditTool{CWD: dir}
89	_, err := tool.Execute(context.Background(), mustJSON(t, map[string]any{
90		"path":  "a.txt",
91		"edits": []map[string]any{{"oldText": "world", "newText": "gopher"}},
92	}), nil)
93	if err != nil {
94		t.Fatal(err)
95	}
96	b, _ := os.ReadFile(p)
97	if string(b) != "hello gopher\n" {
98		t.Fatalf("got %q", string(b))
99	}
100}
101
102func TestEditMultiple(t *testing.T) {
103	dir := t.TempDir()
104	p := filepath.Join(dir, "a.txt")
105	os.WriteFile(p, []byte("a\nb\nc\n"), 0o644)
106	tool := &EditTool{CWD: dir}
107	_, err := tool.Execute(context.Background(), mustJSON(t, map[string]any{
108		"path": "a.txt",
109		"edits": []map[string]any{
110			{"oldText": "a", "newText": "A"},
111			{"oldText": "c", "newText": "C"},
112		},
113	}), nil)
114	if err != nil {
115		t.Fatal(err)
116	}
117	b, _ := os.ReadFile(p)
118	if string(b) != "A\nb\nC\n" {
119		t.Fatalf("got %q", string(b))
120	}
121}
122
123func TestEditAmbiguous(t *testing.T) {
124	dir := t.TempDir()
125	p := filepath.Join(dir, "a.txt")
126	os.WriteFile(p, []byte("x\nx\n"), 0o644)
127	tool := &EditTool{CWD: dir}
128	_, err := tool.Execute(context.Background(), mustJSON(t, map[string]any{
129		"path":  "a.txt",
130		"edits": []map[string]any{{"oldText": "x", "newText": "y"}},
131	}), nil)
132	if err == nil {
133		t.Fatal("want ambiguous error")
134	}
135}
136
137func TestEditPreservesCRLF(t *testing.T) {
138	dir := t.TempDir()
139	p := filepath.Join(dir, "a.txt")
140	os.WriteFile(p, []byte("hello\r\nworld\r\n"), 0o644)
141	tool := &EditTool{CWD: dir}
142	_, err := tool.Execute(context.Background(), mustJSON(t, map[string]any{
143		"path":  "a.txt",
144		"edits": []map[string]any{{"oldText": "world", "newText": "gopher"}},
145	}), nil)
146	if err != nil {
147		t.Fatal(err)
148	}
149	b, _ := os.ReadFile(p)
150	if string(b) != "hello\r\ngopher\r\n" {
151		t.Fatalf("got %q", string(b))
152	}
153}
154
155func TestBashSuccess(t *testing.T) {
156	if runtime.GOOS == "windows" {
157		t.Skip("posix shell only")
158	}
159	tool := &BashTool{CWD: t.TempDir()}
160	res, err := tool.Execute(context.Background(), mustJSON(t, map[string]any{"command": "echo hi"}), nil)
161	if err != nil {
162		t.Fatal(err)
163	}
164	got := res.Content[0].(provider.TextBlock).Text
165	if !strings.Contains(got, "hi") || !strings.Contains(got, "[exit 0]") {
166		t.Fatalf("got %q", got)
167	}
168	if res.IsError {
169		t.Fatal("unexpected error flag")
170	}
171}
172
173func TestBashFailure(t *testing.T) {
174	if runtime.GOOS == "windows" {
175		t.Skip("posix shell only")
176	}
177	tool := &BashTool{CWD: t.TempDir()}
178	res, _ := tool.Execute(context.Background(), mustJSON(t, map[string]any{"command": "false"}), nil)
179	if !res.IsError {
180		t.Fatal("want error")
181	}
182	got := res.Content[0].(provider.TextBlock).Text
183	if !strings.Contains(got, "[exit 1]") {
184		t.Fatalf("got %q", got)
185	}
186}

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 1

Tags 476

↑↓ moveenter openesc close