1package agent
2
3import (
4 "strings"
5 "testing"
6)
7
8func TestSubagentsSystemAddendaListOnlyEnabledLifecycleTools(t *testing.T) {
9 // Substrings that identify the lifecycle action guidance.
10 const (
11 stopAction = "use stop to request termination of a stuck worker"
12 resumeAction = "use resume with an agent id and follow-up prompt"
13 interruptAction = "use interrupt to cancel only a worker's running turn"
14 noSpawnNotice = "Spawning new workers is unavailable"
15 )
16 builders := []struct {
17 name string
18 build func(SubagentActions) string
19 noSpawnFallback string
20 }{
21 {name: "proactive", build: ProactiveSubagentsSystemAddendumFor, noSpawnFallback: "Continue non-delegated work locally."},
22 {name: "strict", build: StrictOrchestratorSystemAddendumFor, noSpawnFallback: "Do not implement, debug, test, or review directly."},
23 }
24 for _, builder := range builders {
25 t.Run(builder.name, func(t *testing.T) {
26 for _, tc := range []struct {
27 name string
28 spawn bool
29 stop bool
30 resume bool
31 interrupt bool
32 want []string
33 unwanted []string
34 available bool
35 }{
36 {name: "none", spawn: true, unwanted: []string{stopAction, resumeAction, interruptAction}, available: true},
37 {name: "fully unavailable", unwanted: []string{stopAction, resumeAction, interruptAction}},
38 {name: "stop", spawn: true, stop: true, want: []string{stopAction}, unwanted: []string{resumeAction, interruptAction}, available: true},
39 {name: "resume", spawn: true, resume: true, want: []string{resumeAction}, unwanted: []string{stopAction, interruptAction}, available: true},
40 {name: "interrupt", spawn: true, interrupt: true, want: []string{interruptAction}, unwanted: []string{stopAction, resumeAction}, available: true},
41 {name: "all", spawn: true, stop: true, resume: true, interrupt: true, want: []string{stopAction, resumeAction, interruptAction}, available: true},
42 {name: "stop without spawn", stop: true, want: []string{stopAction, noSpawnNotice}, unwanted: []string{resumeAction, interruptAction}},
43 {name: "resume without spawn", resume: true, want: []string{resumeAction, noSpawnNotice}, unwanted: []string{stopAction, interruptAction}},
44 {name: "interrupt without spawn", interrupt: true, want: []string{interruptAction, noSpawnNotice}, unwanted: []string{stopAction, resumeAction}},
45 {name: "all without spawn", stop: true, resume: true, interrupt: true, want: []string{stopAction, resumeAction, interruptAction, noSpawnNotice}},
46 } {
47 t.Run(tc.name, func(t *testing.T) {
48 got := builder.build(SubagentActions{Spawn: tc.spawn, Stop: tc.stop, Resume: tc.resume, Interrupt: tc.interrupt})
49 if !tc.spawn && (tc.stop || tc.resume || tc.interrupt) && !strings.Contains(got, builder.noSpawnFallback) {
50 t.Fatalf("addendum missing mode-specific no-spawn fallback %q:\n%s", builder.noSpawnFallback, got)
51 }
52 for _, want := range tc.want {
53 if !strings.Contains(got, want) {
54 t.Fatalf("addendum missing enabled lifecycle tool %q:\n%s", want, got)
55 }
56 }
57 for _, unwanted := range tc.unwanted {
58 if strings.Contains(got, unwanted) {
59 t.Fatalf("addendum mentions unavailable lifecycle tool %q:\n%s", unwanted, got)
60 }
61 }
62 if gotAvailable := !strings.Contains(got, "unavailable in this session") && !strings.Contains(got, "Spawning new workers is unavailable"); gotAvailable != tc.available {
63 t.Fatalf("delegation available = %t, want %t:\n%s", gotAvailable, tc.available, got)
64 }
65 })
66 }
67 })
68 }
69}
70
71func TestSubagentPoliciesSeparateProactiveAndStrictOwnership(t *testing.T) {
72 proactive := ProactiveSubagentsSystemAddendumFor(SubagentActions{Spawn: true, Stop: false, Resume: false})
73 for _, want := range []string{"primary owner and implementer", "immediate next task you will perform locally", "If you cannot name useful non-overlapping local work, do not delegate", "Do not search, read, test, review, edit"} {
74 if !strings.Contains(proactive, want) {
75 t.Fatalf("proactive addendum missing %q:\n%s", want, proactive)
76 }
77 }
78 if strings.Contains(proactive, "not an implementer") {
79 t.Fatalf("proactive addendum retained strict orchestrator contract:\n%s", proactive)
80 }
81
82 strict := StrictOrchestratorSystemAddendumFor(SubagentActions{Spawn: true, Stop: false, Resume: false})
83 for _, want := range []string{"not an implementer", "non-overlapping worker scopes", "Once a worker is active", "Only coordinate workers"} {
84 if !strings.Contains(strict, want) {
85 t.Fatalf("strict addendum missing %q:\n%s", want, strict)
86 }
87 }
88}
89
90func TestProactiveAddendumRoutesBeforeWorkOnlyWhenSpawningIsAvailable(t *testing.T) {
91 proactive := ProactiveSubagentsSystemAddendumFor(SubagentActions{Spawn: true, Stop: false, Resume: false})
92 for _, want := range []string{
93 "Route before you work:",
94 "routing happens when the user states the request you are about to work on",
95 "the [subagents_list] profile that owns each delegable part",
96 "let the tool calls that follow speak for themselves",
97 "Say plainly when nothing is worth delegating",
98 } {
99 if !strings.Contains(proactive, want) {
100 t.Fatalf("proactive addendum missing %q:\n%s", want, proactive)
101 }
102 }
103
104 // Without the spawn action there is nothing to route to, and the
105 // spawn-withheld branches in the same prompt tell the primary to stay
106 // local; naming profiles there would contradict them.
107 for _, tc := range []struct {
108 name string
109 stop, resume bool
110 }{
111 {name: "spawn withheld"},
112 {name: "stop only", stop: true},
113 {name: "resume only", resume: true},
114 {name: "stop and resume", stop: true, resume: true},
115 } {
116 t.Run(tc.name, func(t *testing.T) {
117 got := ProactiveSubagentsSystemAddendumFor(SubagentActions{Spawn: false, Stop: tc.stop, Resume: tc.resume})
118 for _, unwanted := range []string{"Route before you work:", "[subagents_list]", "spawn a clearly described general worker"} {
119 if strings.Contains(got, unwanted) {
120 t.Fatalf("spawn-withheld addendum kept %q:\n%s", unwanted, got)
121 }
122 }
123 if !strings.Contains(got, ProactiveSubagentsSystemAddendum) {
124 t.Fatalf("spawn-withheld addendum lost the ownership contract:\n%s", got)
125 }
126 // Fully unavailable delegation falls back to the dedicated notice;
127 // an enabled lifecycle action uses the shorter local-work line.
128 fallback := "Continue non-delegated work locally."
129 if !tc.stop && !tc.resume {
130 fallback = ProactiveSubagentsDelegationUnavailableAddendum
131 }
132 if !strings.Contains(got, fallback) {
133 t.Fatalf("spawn-withheld addendum lost its local-work fallback %q:\n%s", fallback, got)
134 }
135 })
136 }
137
138 // Routing is the interactive collaboration step; the headless strict
139 // contract divides scopes itself and must not inherit it.
140 strict := StrictOrchestratorSystemAddendumFor(SubagentActions{Spawn: true, Stop: false, Resume: false})
141 if strings.Contains(strict, "Route before you work:") || strings.Contains(strict, "[subagents_list]") {
142 t.Fatalf("strict orchestrator inherited interactive routing guidance:\n%s", strict)
143 }
144}
145
146func TestSubagentPoliciesHandleUnavailableDelegationByMode(t *testing.T) {
147 proactive := ProactiveSubagentsSystemAddendumFor(SubagentActions{Spawn: false, Stop: false, Resume: false})
148 if !strings.Contains(proactive, "Continue the user's task locally") || strings.Contains(proactive, "report this limitation") {
149 t.Fatalf("proactive unavailable guidance blocks local work:\n%s", proactive)
150 }
151 strict := StrictOrchestratorSystemAddendumFor(SubagentActions{Spawn: false, Stop: false, Resume: false})
152 if !strings.Contains(strict, "report this limitation") || !strings.Contains(strict, "rather than implementing") {
153 t.Fatalf("strict unavailable guidance permits local implementation:\n%s", strict)
154 }
155}
156
157func TestConfigSettingsStorePersistsShowInstructionsAtStartup(t *testing.T) {
158 t.Setenv("ZUT_HOME", t.TempDir())
159 if err := SaveConfig(Config{Theme: "dark"}); err != nil {
160 t.Fatal(err)
161 }
162
163 if err := (configSettingsStore{}).SetShowInstructionsAtStartup(true); err != nil {
164 t.Fatal(err)
165 }
166 cfg, err := LoadConfig()
167 if err != nil {
168 t.Fatal(err)
169 }
170 if cfg.ShowInstructionsAtStartup == nil || !*cfg.ShowInstructionsAtStartup {
171 t.Fatal("show_instructions_at_startup was not persisted as enabled")
172 }
173 if cfg.Theme != "dark" {
174 t.Fatalf("unrelated config changed: theme = %q, want dark", cfg.Theme)
175 }
176}
177
178func TestConfigSettingsStorePersistsTerminalAlerts(t *testing.T) {
179 t.Setenv("ZUT_HOME", t.TempDir())
180 if err := SaveConfig(Config{Theme: "dark"}); err != nil {
181 t.Fatal(err)
182 }
183
184 if err := (configSettingsStore{}).SetTerminalAlertsEnabled(false); err != nil {
185 t.Fatal(err)
186 }
187 cfg, err := LoadConfig()
188 if err != nil {
189 t.Fatal(err)
190 }
191 if cfg.TerminalAlertsEnabled == nil || *cfg.TerminalAlertsEnabled {
192 t.Fatal("terminal_alerts_enabled was not persisted as disabled")
193 }
194 if cfg.Theme != "dark" {
195 t.Fatalf("unrelated config changed: theme = %q, want dark", cfg.Theme)
196 }
197}
198
199func TestConfigSettingsStorePersistsTerminalTitle(t *testing.T) {
200 t.Setenv("ZUT_HOME", t.TempDir())
201 if err := SaveConfig(Config{Theme: "dark"}); err != nil {
202 t.Fatal(err)
203 }
204
205 if err := (configSettingsStore{}).SetTerminalTitleEnabled(false); err != nil {
206 t.Fatal(err)
207 }
208 cfg, err := LoadConfig()
209 if err != nil {
210 t.Fatal(err)
211 }
212 if cfg.TerminalTitleEnabled == nil || *cfg.TerminalTitleEnabled {
213 t.Fatal("terminal_title_enabled was not persisted as disabled")
214 }
215 if cfg.Theme != "dark" {
216 t.Fatalf("unrelated config changed: theme = %q, want dark", cfg.Theme)
217 }
218}
219
220func TestConfigSettingsStorePersistsJailByDefault(t *testing.T) {
221 t.Setenv("ZUT_HOME", t.TempDir())
222 if err := SaveConfig(Config{Theme: "dark"}); err != nil {
223 t.Fatal(err)
224 }
225
226 if err := (configSettingsStore{}).SetJailByDefault(true); err != nil {
227 t.Fatal(err)
228 }
229 cfg, err := LoadConfig()
230 if err != nil {
231 t.Fatal(err)
232 }
233 if cfg.JailByDefault == nil || !*cfg.JailByDefault {
234 t.Fatal("jail_by_default was not persisted as enabled")
235 }
236 if cfg.Theme != "dark" {
237 t.Fatalf("unrelated config changed: theme = %q, want dark", cfg.Theme)
238 }
239}
240
241func TestConfigSettingsStorePersistsAutoCompactThreshold(t *testing.T) {
242 t.Setenv("ZUT_HOME", t.TempDir())
243 if err := SaveConfig(Config{Theme: "dark"}); err != nil {
244 t.Fatal(err)
245 }
246
247 if err := (configSettingsStore{}).SetAutoCompactThreshold(70); err != nil {
248 t.Fatal(err)
249 }
250 cfg, err := LoadConfig()
251 if err != nil {
252 t.Fatal(err)
253 }
254 if cfg.AutoCompactThreshold == nil || *cfg.AutoCompactThreshold != 70 {
255 t.Fatalf("auto_compact_threshold = %v, want 70", cfg.AutoCompactThreshold)
256 }
257 if cfg.Theme != "dark" {
258 t.Fatalf("unrelated config changed: theme = %q, want dark", cfg.Theme)
259 }
260}
261
262func TestConfigSettingsStorePersistsLSPDefaults(t *testing.T) {
263 t.Setenv("ZUT_HOME", t.TempDir())
264 if err := SaveConfig(Config{Theme: "dark"}); err != nil {
265 t.Fatal(err)
266 }
267
268 if err := (configSettingsStore{}).SetLSPEnabled(false); err != nil {
269 t.Fatal(err)
270 }
271 if err := (configSettingsStore{}).SetSubagentLSPEnabled(false); err != nil {
272 t.Fatal(err)
273 }
274 cfg, err := LoadConfig()
275 if err != nil {
276 t.Fatal(err)
277 }
278 if cfg.LSPEnabled == nil || *cfg.LSPEnabled {
279 t.Fatal("lsp_enabled was not persisted as disabled")
280 }
281 if cfg.SubagentLSPEnabled == nil || *cfg.SubagentLSPEnabled {
282 t.Fatal("subagent_lsp_enabled was not persisted as disabled")
283 }
284 if cfg.Theme != "dark" {
285 t.Fatalf("unrelated config changed: theme = %q, want dark", cfg.Theme)
286 }
287}
288
289func TestConfigLSPEnabledForDefaultsToTrue(t *testing.T) {
290 cfg := Config{}
291 if !cfg.LSPEnabledFor(false) {
292 t.Fatal("main-session LSP should default to enabled")
293 }
294 if !cfg.LSPEnabledFor(true) {
295 t.Fatal("sub-agent LSP should default to enabled")
296 }
297
298 noMain, noSub := false, false
299 cfg.LSPEnabled = &noMain
300 cfg.SubagentLSPEnabled = &noSub
301 if cfg.LSPEnabledFor(false) || cfg.LSPEnabledFor(true) {
302 t.Fatal("explicitly disabled LSP settings were ignored")
303 }
304 yesSub := true
305 cfg.SubagentLSPEnabled = &yesSub
306 write, edit := true, true
307 cfg.LSPDiagnosticsOnWrite = &write
308 cfg.LSPDiagnosticsOnEdit = &edit
309 if cfg.LSPDiagnosticsOnWriteEnabled(false) || cfg.LSPDiagnosticsOnEditEnabled(false) {
310 t.Fatal("diagnostics remained enabled with main-session LSP disabled")
311 }
312 if !cfg.LSPDiagnosticsOnWriteEnabled(true) || !cfg.LSPDiagnosticsOnEditEnabled(true) {
313 t.Fatal("sub-agent diagnostics did not follow sub-agent LSP settings")
314 }
315}
316
317func TestConfigSettingsStoreResetsRemovedInheritedThemeToAuto(t *testing.T) {
318 t.Setenv("ZUT_HOME", t.TempDir())
319 if err := SaveConfig(Config{Theme: "dark"}); err != nil {
320 t.Fatal(err)
321 }
322
323 if err := (configSettingsStore{}).SetTheme("inherited"); err != nil {
324 t.Fatal(err)
325 }
326 cfg, err := LoadConfig()
327 if err != nil {
328 t.Fatal(err)
329 }
330 if cfg.Theme != "" {
331 t.Fatalf("theme = %q, want auto", cfg.Theme)
332 }
333}
334
335func TestConfigWebSearchEnabledForCLIDefaultsToTrue(t *testing.T) {
336 cfg := Config{}
337 if !cfg.WebSearchEnabledForCLI() {
338 t.Fatal("web search should default to enabled when the config field is absent")
339 }
340 disabled := false
341 cfg.WebSearchEnabled = &disabled
342 if cfg.WebSearchEnabledForCLI() {
343 t.Fatal("explicitly disabled web search setting was ignored")
344 }
345}
346
347func TestConfigSettingsStorePersistsWebSearch(t *testing.T) {
348 t.Setenv("ZUT_HOME", t.TempDir())
349 if err := SaveConfig(Config{Theme: "dark"}); err != nil {
350 t.Fatal(err)
351 }
352
353 if err := (configSettingsStore{}).SetWebSearchEnabled(false); err != nil {
354 t.Fatal(err)
355 }
356 cfg, err := LoadConfig()
357 if err != nil {
358 t.Fatal(err)
359 }
360 if cfg.WebSearchEnabled == nil || *cfg.WebSearchEnabled {
361 t.Fatal("web_search_enabled was not persisted as disabled")
362 }
363 if cfg.Theme != "dark" {
364 t.Fatalf("unrelated config changed: theme = %q, want dark", cfg.Theme)
365 }
366}