v0.5.0 c35564a

profiles_test.go RawHistoryFolder

1package subagents
2
3import (
4	"fmt"
5	"os"
6	"path/filepath"
7	"strings"
8	"testing"
9)
10
11func TestDiscoverLoadsGlobalAgentsProfilesAndPiFallback(t *testing.T) {
12	home := t.TempDir()
13	agentsDir := filepath.Join(home, ".agents", "agents")
14	piDir := filepath.Join(home, ".pi", "agent", "agents")
15	if err := os.MkdirAll(agentsDir, 0o755); err != nil {
16		t.Fatal(err)
17	}
18	if err := os.MkdirAll(piDir, 0o755); err != nil {
19		t.Fatal(err)
20	}
21	writeProfile(t, filepath.Join(agentsDir, "reviewer.md"), `---
22name: reviewer
23description: Read-only code reviewer
24tools: read
25model: openai-codex/gpt-5.6-luna
26thinking: max
27systemPromptMode: replace
28inheritProjectContext: false
29inheritSkills: false
30fastMode: false
31---
32Review the requested scope without editing it.
33`)
34	writeProfile(t, filepath.Join(piDir, "implementer.md"), `---
35name: implementer
36description: Primary implementation agent
37tools: [read, bash, edit, write]
38model: openai-codex/gpt-5.6-luna
39thinking: xhigh
40---
41Implement the requested change.
42`)
43
44	profiles, errs := Discover("", home)
45	if len(errs) != 0 {
46		t.Fatalf("Discover errors = %v", errs)
47	}
48	if len(profiles) != 2 {
49		t.Fatalf("profiles = %d, want 2: %#v", len(profiles), profiles)
50	}
51	if profiles[0].Name != "implementer" || profiles[1].Name != "reviewer" {
52		t.Fatalf("profiles not sorted by name: %#v", profiles)
53	}
54	reviewer := Find(profiles, "reviewer")
55	if reviewer == nil {
56		t.Fatal("reviewer profile missing")
57	}
58	if reviewer.SystemPromptMode != "replace" {
59		t.Fatalf("system prompt mode = %q, want replace", reviewer.SystemPromptMode)
60	}
61	if reviewer.Model != "openai-codex/gpt-5.6-luna" || reviewer.Thinking != "max" {
62		t.Fatalf("profile metadata = %#v", reviewer)
63	}
64	if reviewer.InheritProjectContext == nil || *reviewer.InheritProjectContext {
65		t.Fatalf("inherit project context = %#v, want false", reviewer.InheritProjectContext)
66	}
67	if reviewer.InheritSkills == nil || *reviewer.InheritSkills {
68		t.Fatalf("inherit skills = %#v, want false", reviewer.InheritSkills)
69	}
70	if reviewer.FastMode == nil || *reviewer.FastMode {
71		t.Fatalf("fast mode = %#v, want false", reviewer.FastMode)
72	}
73	implementer := Find(profiles, "implementer")
74	if implementer == nil || implementer.FastMode != nil {
75		t.Fatalf("unset fast mode = %#v, want nil", implementer)
76	}
77}
78
79func TestDiscoverResolvesConfiguredRelativePathAgainstCWD(t *testing.T) {
80	first := t.TempDir()
81	second := t.TempDir()
82	t.Setenv("ZUT_AGENT_PROFILES", ".agents")
83	writeProfile(t, filepath.Join(first, ".agents", "reviewer.md"), `---
84name: reviewer
85description: First session profile
86---
87First.
88`)
89	writeProfile(t, filepath.Join(second, ".agents", "reviewer.md"), `---
90name: reviewer
91description: Second session profile
92---
93Second.
94`)
95
96	for _, tc := range []struct {
97		cwd         string
98		description string
99	}{
100		{cwd: first, description: "First session profile"},
101		{cwd: second, description: "Second session profile"},
102	} {
103		profiles, errs := Discover(tc.cwd, t.TempDir())
104		if len(errs) != 0 {
105			t.Fatalf("Discover(%q) errors = %v", tc.cwd, errs)
106		}
107		if len(profiles) != 1 || profiles[0].Description != tc.description {
108			t.Fatalf("Discover(%q) = %#v, want %q", tc.cwd, profiles, tc.description)
109		}
110	}
111}
112
113func TestDiscoverConfiguredDirectoryWinsAndRejectsUnsafeNames(t *testing.T) {
114	configured := t.TempDir()
115	home := t.TempDir()
116	t.Setenv("ZUT_AGENT_PROFILES", configured)
117	writeProfile(t, filepath.Join(configured, "reviewer.md"), `---
118name: reviewer
119description: Configured reviewer
120---
121Configured instructions.
122`)
123	writeProfile(t, filepath.Join(filepath.Join(home, ".agents", "agents"), "reviewer.md"), `---
124name: reviewer
125description: Global reviewer
126---
127Global instructions.
128`)
129	writeProfile(t, filepath.Join(configured, "unsafe.md"), `---
130name: ../unsafe
131description: Should not load
132---
133No.
134`)
135
136	profiles, errs := Discover("", home)
137	if len(errs) != 0 {
138		t.Fatalf("Discover errors = %v", errs)
139	}
140	if len(profiles) != 1 || profiles[0].Description != "Configured reviewer" {
141		t.Fatalf("configured precedence = %#v", profiles)
142	}
143}
144
145func TestLoadRejectsUnclosedFrontmatter(t *testing.T) {
146	path := filepath.Join(t.TempDir(), "profile.md")
147	if err := os.WriteFile(path, []byte("---\nname: reviewer\nInstructions without a closing delimiter.\n"), 0o600); err != nil {
148		t.Fatal(err)
149	}
150	if _, err := load(path, "test"); err == nil || !strings.Contains(err.Error(), "missing closing delimiter") {
151		t.Fatalf("load error = %v, want missing closing delimiter", err)
152	}
153}
154
155func TestLoadRejectsInvalidClosedSchemaMetadata(t *testing.T) {
156	cases := []struct {
157		name  string
158		field string
159	}{
160		{name: "invalid-thinking", field: "thinking: extreme"},
161		{name: "invalid-reasoning", field: "reasoning: extreme"},
162		{name: "qualified-model-with-provider", field: "provider: openai\nmodel: openai-codex/gpt-5"},
163		{name: "invalid-prompt-mode", field: "systemPromptMode: merge"},
164		{name: "invalid-project-context", field: "inheritProjectContext: sometimes"},
165		{name: "invalid-skills", field: "inheritSkills: sometimes"},
166		{name: "invalid-fast-mode", field: "fastMode: sometimes"},
167	}
168	for _, tc := range cases {
169		t.Run(tc.name, func(t *testing.T) {
170			path := filepath.Join(t.TempDir(), "profile.md")
171			writeProfile(t, path, fmt.Sprintf("---\nname: test\n%s\n---\nInstructions.\n", tc.field))
172			if _, err := load(path, "test"); err == nil {
173				t.Fatalf("load succeeded for invalid metadata %q", tc.field)
174			}
175		})
176	}
177}
178
179func TestProfileModelSelection(t *testing.T) {
180	profile := &Profile{Model: "openrouter/anthropic/claude-sonnet"}
181	provider, model := profile.ModelSelection()
182	if provider != "openrouter" || model != "anthropic/claude-sonnet" {
183		t.Fatalf("selection = (%q, %q)", provider, model)
184	}
185
186	profile = &Profile{Provider: "openai", Model: "gpt-5"}
187	provider, model = profile.ModelSelection()
188	if provider != "openai" || model != "gpt-5" {
189		t.Fatalf("explicit selection = (%q, %q)", provider, model)
190	}
191}
192
193func TestSystemPromptAddendumIsCompactAndDoesNotExposeBodyOrPath(t *testing.T) {
194	profiles := []*Profile{{
195		Name:         "reviewer",
196		Description:  "Read-only\nreviewer",
197		SystemPrompt: "secret instructions that belong only to the child",
198		Model:        "openai-codex/gpt-5",
199		Thinking:     "max",
200		FastMode:     boolPtr(false),
201		Tools:        []string{"read", "bash"},
202		Path:         "/private/profile.md",
203	}}
204	got := SystemPromptAddendum(profiles)
205	for _, want := range []string{"[subagents_list]", "[/subagents_list]", "reviewer", "Read-only reviewer", "model=openai-codex/gpt-5", "thinking=max", "fastMode=false"} {
206		if !strings.Contains(got, want) {
207			t.Fatalf("addendum missing %q:\n%s", want, got)
208		}
209	}
210	for _, unwanted := range []string{"secret instructions", "/private/profile.md", "\nreviewer"} {
211		if strings.Contains(got, unwanted) {
212			t.Fatalf("addendum contains %q:\n%s", unwanted, got)
213		}
214	}
215}
216
217func boolPtr(value bool) *bool { return &value }
218
219func writeProfile(t *testing.T, path, content string) {
220	t.Helper()
221	if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
222		t.Fatal(err)
223	}
224	if err := os.WriteFile(path, []byte(content), 0o600); err != nil {
225		t.Fatal(err)
226	}
227}

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