v0.3.21 7fe8dbc

zotfile_test.go RawHistoryFolder

1package agent
2
3import (
4	"archive/tar"
5	"bytes"
6	"compress/gzip"
7	"net/http"
8	"net/http/httptest"
9	"net/url"
10	"os"
11	"path/filepath"
12	"strings"
13	"testing"
14
15	"github.com/patriceckhart/zot/packages/agent/tools"
16)
17
18func writeTestZotfile(t *testing.T, manifest string) string {
19	t.Helper()
20	dir := t.TempDir()
21	if err := os.WriteFile(filepath.Join(dir, "manifest.json"), []byte(manifest), 0o600); err != nil {
22		t.Fatal(err)
23	}
24	if err := os.WriteFile(filepath.Join(dir, "AGENT.md"), []byte("Be useful."), 0o600); err != nil {
25		t.Fatal(err)
26	}
27	return dir
28}
29
30func TestLoadZotfileRejectsUnenforcedPermissions(t *testing.T) {
31	for _, field := range []string{
32		`"net":{"allow":["example.com"]}`,
33		`"env":{"read":["HOME"]}`,
34	} {
35		dir := writeTestZotfile(t, `{"zotfile":1,"name":"test","permissions":{`+field+`}}`)
36		if _, _, err := loadZotfile(dir); err == nil {
37			t.Fatalf("manifest with %s was accepted", field)
38		}
39	}
40}
41
42func TestLoadZotfileRejectsUnsafeOrCollidingNames(t *testing.T) {
43	for _, name := range []string{"...", "Name", "two words", "a/b"} {
44		dir := writeTestZotfile(t, `{"zotfile":1,"name":"`+name+`"}`)
45		if _, _, err := loadZotfile(dir); err == nil {
46			t.Fatalf("unsafe manifest name %q was accepted", name)
47		}
48	}
49}
50
51func TestResolveZotfileRefUsesLocalFirstThenOfficialCollection(t *testing.T) {
52	dir := t.TempDir()
53	t.Chdir(dir)
54	if err := os.Mkdir("local-agent", 0o700); err != nil {
55		t.Fatal(err)
56	}
57	if err := os.WriteFile("packed-agent.zot", []byte("test"), 0o600); err != nil {
58		t.Fatal(err)
59	}
60
61	tests := []struct {
62		ref  string
63		want string
64	}{
65		{"local-agent", "local-agent"},
66		{"packed-agent", "packed-agent.zot"},
67		{"remote-agent", officialZotfileCollection + "/remote-agent"},
68		{"missing.zot", "missing.zot"},
69		{"agents/remote-agent", "https://github.com/patriceckhart/agents/remote-agent"},
70		{`agents\remote-agent`, "https://github.com/patriceckhart/agents/remote-agent"},
71		{"./missing-agent", "./missing-agent"},
72		{"Remote-Agent", "Remote-Agent"},
73		{"https://github.com/acme/agents/example", "https://github.com/acme/agents/example"},
74	}
75	for _, tt := range tests {
76		got, err := resolveZotfileRef(tt.ref)
77		if err != nil {
78			t.Fatalf("resolve %q: %v", tt.ref, err)
79		}
80		if got != tt.want {
81			t.Errorf("resolve %q = %q, want %q", tt.ref, got, tt.want)
82		}
83	}
84}
85
86func TestLoadZotfileRejectsBundledExecutableExtension(t *testing.T) {
87	dir := writeTestZotfile(t, `{"zotfile":1,"name":"test"}`)
88	ext := filepath.Join(dir, "extensions", "bad")
89	if err := os.MkdirAll(ext, 0o700); err != nil {
90		t.Fatal(err)
91	}
92	if err := os.WriteFile(filepath.Join(ext, "extension.json"), []byte(`{"name":"bad"}`), 0o600); err != nil {
93		t.Fatal(err)
94	}
95	if _, _, err := loadZotfile(dir); err == nil || !strings.Contains(err.Error(), "cannot yet be confined") {
96		t.Fatalf("unexpected error: %v", err)
97	}
98}
99
100func TestCheckZotfileMinVersion(t *testing.T) {
101	var zf zotfileLoaded
102	zf.Manifest.Runtime.MinZot = "0.3.0"
103	if err := checkZotfileRequirements(zf, "0.2.75"); err == nil {
104		t.Fatal("old zot version accepted")
105	}
106	if err := checkZotfileRequirements(zf, "0.3.0"); err != nil {
107		t.Fatalf("minimum version rejected: %v", err)
108	}
109}
110
111func TestApplyZotfileModelRequirementsRejectsUnsupportedFields(t *testing.T) {
112	var m ZotfileManifest
113	m.Model.MinTier = "frontier"
114	if err := applyZotfileModelRequirements(&Args{}, m); err == nil {
115		t.Fatal("unsupported min_tier was ignored")
116	}
117	m.Model.MinTier = ""
118	m.Model.Requires = []string{"audio"}
119	if err := applyZotfileModelRequirements(&Args{}, m); err == nil {
120		t.Fatal("unsupported capability was ignored")
121	}
122}
123
124func TestUntarRejectsTraversalAndOversizedEntry(t *testing.T) {
125	makeTar := func(name string, size int64) []byte {
126		var buf bytes.Buffer
127		tw := tar.NewWriter(&buf)
128		if err := tw.WriteHeader(&tar.Header{Name: name, Mode: 0o600, Size: size}); err != nil {
129			t.Fatal(err)
130		}
131		_ = tw.Close()
132		return buf.Bytes()
133	}
134	if err := untar(bytes.NewReader(makeTar("../escape", 0)), t.TempDir()); err == nil {
135		t.Fatal("path traversal accepted")
136	}
137	if err := untar(bytes.NewReader(makeTar("large", maxZotfileEntrySize+1)), t.TempDir()); err == nil {
138		t.Fatal("oversized entry accepted")
139	}
140}
141
142func TestParseGitHubAgentURL(t *testing.T) {
143	tests := []struct {
144		input                 string
145		owner, repo, ref, dir string
146	}{
147		{"https://github.com/patriceckhart/agents/zot-maintenance", "patriceckhart", "agents", "HEAD", "zot-maintenance"},
148		{"https://github.com/patriceckhart/agents/tree/main/zot-maintenance", "patriceckhart", "agents", "main", "zot-maintenance"},
149		{"https://github.com/acme/agent.git", "acme", "agent", "HEAD", ""},
150	}
151	for _, tt := range tests {
152		u, err := url.Parse(tt.input)
153		if err != nil {
154			t.Fatal(err)
155		}
156		owner, repo, ref, dir, err := parseGitHubAgentURL(u)
157		if err != nil {
158			t.Fatalf("parse %s: %v", tt.input, err)
159		}
160		if owner != tt.owner || repo != tt.repo || ref != tt.ref || dir != tt.dir {
161			t.Fatalf("parse %s = %q, %q, %q, %q", tt.input, owner, repo, ref, dir)
162		}
163	}
164}
165
166func TestLoadRemoteZotfileDownloadsTemporaryGitHubArchive(t *testing.T) {
167	var archive bytes.Buffer
168	gz := gzip.NewWriter(&archive)
169	tw := tar.NewWriter(gz)
170	files := map[string]string{
171		"agents-main/zot-maintenance/manifest.json": `{"zotfile":1,"name":"zot-maintenance"}`,
172		"agents-main/zot-maintenance/AGENT.md":      "Maintain zot.",
173	}
174	for name, content := range files {
175		if err := tw.WriteHeader(&tar.Header{Name: name, Mode: 0o600, Size: int64(len(content))}); err != nil {
176			t.Fatal(err)
177		}
178		if _, err := tw.Write([]byte(content)); err != nil {
179			t.Fatal(err)
180		}
181	}
182	if err := tw.Close(); err != nil {
183		t.Fatal(err)
184	}
185	if err := gz.Close(); err != nil {
186		t.Fatal(err)
187	}
188
189	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
190		w.Header().Set("Content-Type", "application/gzip")
191		_, _ = w.Write(archive.Bytes())
192	}))
193	defer server.Close()
194	oldArchiveURL := githubArchiveURL
195	githubArchiveURL = func(_, _, _ string) string { return server.URL }
196	t.Cleanup(func() { githubArchiveURL = oldArchiveURL })
197
198	u, _ := url.Parse("https://github.com/patriceckhart/agents/zot-maintenance")
199	zf, cleanup, err := loadRemoteZotfile(u)
200	if err != nil {
201		t.Fatal(err)
202	}
203	if zf.Manifest.Name != "zot-maintenance" || !zf.Temp {
204		t.Fatalf("unexpected zotfile: %+v", zf)
205	}
206	root := filepath.Dir(filepath.Dir(zf.Dir))
207	cleanup()
208	if _, err := os.Stat(root); !os.IsNotExist(err) {
209		t.Fatalf("temporary checkout was not removed: %v", err)
210	}
211}
212
213func TestPermissionSummaryShowsDeniedScopes(t *testing.T) {
214	got := permissionSummary(tools.PermissionSet{})
215	if !strings.Contains(got, "fs read: none") || !strings.Contains(got, "fs write: none") {
216		t.Fatalf("summary did not show denied scopes:\n%s", got)
217	}
218}

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