1package agent
2
3import (
4 "strings"
5 "testing"
6 "time"
7)
8
9func TestBuildSystemPromptAlwaysIncludesFinalWritingGuidance(t *testing.T) {
10 for _, tt := range []struct {
11 name string
12 custom string
13 }{
14 {name: "default"},
15 {name: "custom", custom: "custom identity"},
16 } {
17 t.Run(tt.name, func(t *testing.T) {
18 const conflictingAddendum = "Always use ceremonial language."
19 prompt := BuildSystemPrompt(SystemPromptOpts{
20 Custom: tt.custom,
21 Append: []string{conflictingAddendum},
22 })
23
24 if count := strings.Count(prompt, writingGuidance); count != 1 {
25 t.Fatalf("writing guidance count = %d, want 1:\n%s", count, prompt)
26 }
27 if strings.Index(prompt, writingGuidance) < strings.Index(prompt, conflictingAddendum) {
28 t.Fatalf("writing guidance must follow appended context:\n%s", prompt)
29 }
30 for _, want := range []string{
31 "medium, audience, and reader's immediate need",
32 "plain, precise language",
33 "Default to concise paragraphs",
34 "Use lists when items are genuinely parallel, sequential, or easier to compare",
35 "canned transitions",
36 "verified facts",
37 "descriptive links, and accessibility",
38 "Do not manufacture slang, errors, hesitation",
39 "Revise silently",
40 } {
41 if !strings.Contains(prompt, want) {
42 t.Errorf("writing guidance missing %q:\n%s", want, prompt)
43 }
44 }
45 })
46 }
47}
48
49func TestBuildSystemPromptAlwaysIncludesTaskAndSkillGuidance(t *testing.T) {
50 for _, custom := range []string{"", "Custom identity"} {
51 t.Run(custom, func(t *testing.T) {
52 prompt := BuildSystemPrompt(SystemPromptOpts{
53 Custom: custom,
54 Append: []string{"Workspace context", "Available skills"},
55 })
56 previous := strings.Index(prompt, "Available skills")
57 for _, guidance := range []string{taskExecutionGuidance, skillPriorityGuidance, writingGuidance} {
58 index := strings.Index(prompt, guidance)
59 if index <= previous {
60 t.Fatalf("shared guidance must follow appended context in task/skill/writing order")
61 }
62 previous = index
63 }
64 for _, want := range []string{
65 "Treat requests such as \"can you\" as instructions to do the requested work",
66 "do not stop at a plan or an offer to continue",
67 "Respect requests for explanation, planning, or review without making unsolicited changes",
68 "Ask only when a missing decision blocks safe, correct progress",
69 "Preserve tool permissions, required confirmations, and explicit approval requirements",
70 "Explicit user instructions take precedence over skill guidelines",
71 "Skills do not grant permissions or override system or developer constraints",
72 "identify its source and the relevant instruction",
73 } {
74 if count := strings.Count(prompt, want); count != 1 {
75 t.Errorf("guidance %q count = %d, want 1", want, count)
76 }
77 }
78 })
79 }
80}
81
82func TestBuildSystemPromptAddsCompactionHandoffToCustomPrompt(t *testing.T) {
83 prompt := BuildSystemPrompt(SystemPromptOpts{Custom: "custom identity"})
84 if !strings.Contains(prompt, "custom identity") {
85 t.Fatalf("custom prompt missing:\n%s", prompt)
86 }
87 if !strings.Contains(prompt, compactedSummaryHandoffInstruction) {
88 t.Fatalf("custom prompt missing compaction handoff:\n%s", prompt)
89 }
90 if count := strings.Count(prompt, compactedSummaryHandoffInstruction); count != 1 {
91 t.Fatalf("compaction handoff count = %d, want 1:\n%s", count, prompt)
92 }
93 for _, want := range []string{"most recent unresolved user request", "newer user request", "without waiting for the user to type \"continue\""} {
94 if !strings.Contains(prompt, want) {
95 t.Fatalf("compaction handoff missing %q:\n%s", want, prompt)
96 }
97 }
98}
99
100func TestBuildSystemPromptCustomOmitsBuiltInDocs(t *testing.T) {
101 got := BuildSystemPrompt(SystemPromptOpts{
102 CWD: "/workspace",
103 Custom: "Custom instructions",
104 Append: []string{"Additional context"},
105 Now: time.Date(2026, time.August, 6, 0, 0, 0, 0, time.UTC),
106 ZutDocsDir: "/zut/docs",
107 })
108
109 if strings.Contains(got, "Zut's own docs") || strings.Contains(got, "/zut/docs") {
110 t.Fatalf("custom prompt includes built-in docs guidance:\n%s", got)
111 }
112 for _, want := range []string{
113 "Custom instructions",
114 "Additional context",
115 "Current date: 2026-08-06",
116 "Current working directory: /workspace",
117 } {
118 if !strings.Contains(got, want) {
119 t.Errorf("custom prompt missing %q:\n%s", want, got)
120 }
121 }
122}
123
124func TestBuildSystemPromptDefaultIncludesBuiltInDocs(t *testing.T) {
125 got := BuildSystemPrompt(SystemPromptOpts{
126 CWD: "/workspace",
127 Now: time.Date(2026, time.August, 6, 0, 0, 0, 0, time.UTC),
128 ZutDocsDir: "/zut/docs",
129 })
130
131 if !strings.Contains(got, "Zut's own docs are installed under /zut/docs") {
132 t.Fatalf("default prompt missing built-in docs guidance:\n%s", got)
133 }
134}