system_prompt_test.go

 1package server
 2
 3import (
 4	"os"
 5	"path/filepath"
 6	"strings"
 7	"testing"
 8)
 9
10// TestSystemPromptIncludesCwdGuidanceFiles verifies that AGENTS.md from the working directory
11// is included in the generated system prompt.
12func TestSystemPromptIncludesCwdGuidanceFiles(t *testing.T) {
13	// Create a temp directory to serve as our "context directory"
14	tmpDir, err := os.MkdirTemp("", "shelley_test")
15	if err != nil {
16		t.Fatalf("failed to create temp dir: %v", err)
17	}
18	defer os.RemoveAll(tmpDir)
19
20	// Create an AGENTS.md file in the temp directory
21	agentsContent := "TEST_UNIQUE_CONTENT_12345: Always use Go for everything."
22	agentsFile := filepath.Join(tmpDir, "AGENTS.md")
23	if err := os.WriteFile(agentsFile, []byte(agentsContent), 0o644); err != nil {
24		t.Fatalf("failed to write AGENTS.md: %v", err)
25	}
26
27	// Generate system prompt for this directory
28	prompt, err := GenerateSystemPrompt(tmpDir)
29	if err != nil {
30		t.Fatalf("GenerateSystemPrompt failed: %v", err)
31	}
32
33	// Verify the unique content from AGENTS.md is included in the prompt
34	if !strings.Contains(prompt, "TEST_UNIQUE_CONTENT_12345") {
35		t.Errorf("system prompt should contain content from AGENTS.md in the working directory")
36		t.Logf("AGENTS.md content: %s", agentsContent)
37		t.Logf("Generated prompt (first 2000 chars): %s", prompt[:min(len(prompt), 2000)])
38	}
39
40	// Verify the file path is mentioned in guidance section
41	if !strings.Contains(prompt, agentsFile) {
42		t.Errorf("system prompt should reference the AGENTS.md file path")
43	}
44}
45
46// TestSystemPromptEmptyCwdFallsBackToCurrentDir verifies that an empty workingDir
47// causes GenerateSystemPrompt to use the current directory.
48func TestSystemPromptEmptyCwdFallsBackToCurrentDir(t *testing.T) {
49	// Get current directory for comparison
50	currentDir, err := os.Getwd()
51	if err != nil {
52		t.Fatalf("failed to get current directory: %v", err)
53	}
54
55	// Generate system prompt with empty workingDir
56	prompt, err := GenerateSystemPrompt("")
57	if err != nil {
58		t.Fatalf("GenerateSystemPrompt failed: %v", err)
59	}
60
61	// Verify the current directory is mentioned in the prompt
62	if !strings.Contains(prompt, currentDir) {
63		t.Errorf("system prompt should contain current directory when cwd is empty")
64	}
65}
66
67func min(a, b int) int {
68	if a < b {
69		return a
70	}
71	return b
72}