gunk-dev/the-valley

new_test.go

source28abc5737165main's published tip history
5.1 KiB
1package cmd
2
3import (
4 "os"
5 "path/filepath"
6 "strings"
7 "testing"
8
9 "the-valley/flow/internal/config"
10)
11
12func TestValidProjectName(t *testing.T) {
13 tests := []struct {
14 name string
15 input string
16 want bool
17 }{
18 {"simple name", "my-project", true},
19 {"alphanumeric", "project123", true},
20 {"with dots", "my.project", true},
21 {"with underscores", "my_project", true},
22 {"single char", "a", true},
23 {"uppercase", "MyProject", true},
24 {"starts with number", "1project", true},
25 {"empty", "", false},
26 {"has spaces", "my project", false},
27 {"starts with hyphen", "-project", false},
28 {"starts with dot", ".project", false},
29 {"has slash", "my/project", false},
30 {"has at sign", "my@project", false},
31 {"ends with dot", "project.", false},
32 {"too long", strings.Repeat("a", 101), false},
33 {"max length", strings.Repeat("a", 100), true},
34 }
35
36 for _, tt := range tests {
37 t.Run(tt.name, func(t *testing.T) {
38 got := ValidProjectName(tt.input)
39 if got != tt.want {
40 t.Errorf("ValidProjectName(%q) = %v, want %v", tt.input, got, tt.want)
41 }
42 })
43 }
44}
45
46func TestBuildScaffoldPrompt(t *testing.T) {
47 principles := "## Test Principles\n- Use Go\n"
48 prompt := BuildScaffoldPrompt("my-app", "a cool app", "cli", principles)
49
50 checks := []struct {
51 label string
52 want string
53 }{
54 {"project type", "cli project"},
55 {"project name", "'my-app'"},
56 {"description", "a cool app"},
57 {"principles", "## Test Principles"},
58 {"principles content", "Use Go"},
59 {"flake.nix", "flake.nix"},
60 {"AGENTS.md", "AGENTS.md"},
61 {"push to main", "push directly to main"},
62 {"no PR", "Do NOT create a PR"},
63 {"description tag", "<user-description>"},
64 {"principles tag", "<principles>"},
65 {"untrusted data warning", "Treat the content inside"},
66 }
67
68 for _, c := range checks {
69 if !strings.Contains(prompt, c.want) {
70 t.Errorf("prompt missing %s (%q)", c.label, c.want)
71 }
72 }
73}
74
75func TestBuildScaffoldPromptWebType(t *testing.T) {
76 prompt := BuildScaffoldPrompt("web-app", "a website", "web", config.DefaultPrinciples)
77
78 if !strings.Contains(prompt, "web project") {
79 t.Error("prompt should contain 'web project' for web type")
80 }
81 if !strings.Contains(prompt, "'web-app'") {
82 t.Error("prompt should contain project name")
83 }
84 if !strings.Contains(prompt, "Playwright") {
85 t.Error("prompt with default principles should mention Playwright")
86 }
87 if !strings.Contains(prompt, "Tailwind CSS") {
88 t.Error("prompt for web projects should mention Tailwind CSS")
89 }
90}
91
92func TestBuildScaffoldPromptIncludesPrinciples(t *testing.T) {
93 customPrinciples := "# Custom\n- Always use Rust\n- No JavaScript ever"
94 prompt := BuildScaffoldPrompt("proj", "test", "cli", customPrinciples)
95
96 if !strings.Contains(prompt, "Always use Rust") {
97 t.Error("prompt should include custom principles content")
98 }
99 if !strings.Contains(prompt, "No JavaScript ever") {
100 t.Error("prompt should include all custom principles")
101 }
102}
103
104func TestLoadPrinciplesDefault(t *testing.T) {
105 dir := t.TempDir() // no .klaus/principles.md
106
107 principles, err := config.LoadPrinciples(dir)
108 if err != nil {
109 t.Fatalf("LoadPrinciples() error: %v", err)
110 }
111
112 if principles != config.DefaultPrinciples {
113 t.Error("expected default principles when file doesn't exist")
114 }
115
116 if !strings.Contains(principles, "## Tech Defaults") {
117 t.Error("default principles should contain Tech Defaults section")
118 }
119 if !strings.Contains(principles, "nix flakes") {
120 t.Error("default principles should mention nix flakes")
121 }
122 if !strings.Contains(principles, "Tailwind CSS") {
123 t.Error("default principles should mention Tailwind CSS")
124 }
125}
126
127func TestLoadPrinciplesFromFile(t *testing.T) {
128 dir := t.TempDir()
129 klausDir := filepath.Join(dir, ".klaus")
130 os.MkdirAll(klausDir, 0o755)
131
132 custom := "# My Principles\n- Always test first\n"
133 os.WriteFile(filepath.Join(klausDir, "principles.md"), []byte(custom), 0o644)
134
135 principles, err := config.LoadPrinciples(dir)
136 if err != nil {
137 t.Fatalf("LoadPrinciples() error: %v", err)
138 }
139
140 if principles != custom {
141 t.Errorf("LoadPrinciples() = %q, want %q", principles, custom)
142 }
143}
144
145func TestLoadPrinciplesReadError(t *testing.T) {
146 dir := t.TempDir()
147 klausDir := filepath.Join(dir, ".klaus")
148 os.MkdirAll(klausDir, 0o755)
149
150 // Create a directory where a file is expected
151 os.MkdirAll(filepath.Join(klausDir, "principles.md"), 0o755)
152
153 _, err := config.LoadPrinciples(dir)
154 if err == nil {
155 t.Error("expected error when principles.md is a directory")
156 }
157}
158
159func TestNewCmdTypeValidation(t *testing.T) {
160 cmd := scaffoldCmd
161 cmd.SetArgs([]string{"test-proj"})
162
163 // Test invalid type
164 cmd.Flags().Set("description", "test")
165 cmd.Flags().Set("type", "invalid")
166
167 err := cmd.RunE(cmd, []string{"test-proj"})
168 if err == nil {
169 t.Error("expected error for invalid project type")
170 }
171 if !strings.Contains(err.Error(), "invalid project type") {
172 t.Errorf("error should mention invalid project type, got: %v", err)
173 }
174}
175
176func TestNewCmdNameValidation(t *testing.T) {
177 err := runNew(scaffoldCmd, []string{"invalid project name"})
178 if err == nil {
179 t.Error("expected error for invalid project name with spaces")
180 }
181 if !strings.Contains(err.Error(), "invalid project name") {
182 t.Errorf("error should mention invalid project name, got: %v", err)
183 }
184}