-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject-manager_test.go
More file actions
340 lines (307 loc) · 8.38 KB
/
project-manager_test.go
File metadata and controls
340 lines (307 loc) · 8.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package main
import (
"fmt"
"io/ioutil"
"os"
"testing"
)
func TestParseTickets(t *testing.T) {
tests := []struct {
name string
content string
expectedCount int
expectedNumbers []int
expectedDescs []string
}{
{
name: "Standard double hash format",
content: `# Project Tickets
## Ticket 1: First task
Some description
## Ticket 2: Second task
## Ticket 3: Third task`,
expectedCount: 3,
expectedNumbers: []int{1, 2, 3},
expectedDescs: []string{"First task", "Second task", "Third task"},
},
{
name: "Mixed hash levels",
content: `# Ticket 1: Single hash
## Ticket 2: Double hash
### Ticket 3: Triple hash
#### Ticket 4: Four hashes`,
expectedCount: 4,
expectedNumbers: []int{1, 2, 3, 4},
expectedDescs: []string{"Single hash", "Double hash", "Triple hash", "Four hashes"},
},
{
name: "Case insensitive",
content: `# ticket 1: lowercase
## TICKET 2: UPPERCASE
### TiCkEt 3: MixedCase`,
expectedCount: 3,
expectedNumbers: []int{1, 2, 3},
expectedDescs: []string{"lowercase", "UPPERCASE", "MixedCase"},
},
{
name: "With hash symbol in number",
content: `## Ticket #1: With hash
### Ticket #2: Another with hash
# Ticket#3: No space before hash`,
expectedCount: 3,
expectedNumbers: []int{1, 2, 3},
expectedDescs: []string{"With hash", "Another with hash", "No space before hash"},
},
{
name: "Different separators",
content: `## Ticket 1: Colon separator
### Ticket 2 - Dash separator
# Ticket 3 – Em dash
## Ticket 4 — Long dash`,
expectedCount: 4,
expectedNumbers: []int{1, 2, 3, 4},
expectedDescs: []string{"Colon separator", "Dash separator", "Em dash", "Long dash"},
},
{
name: "No separator or description",
content: `## Ticket 1
### Ticket 2
# Ticket 3`,
expectedCount: 3,
expectedNumbers: []int{1, 2, 3},
expectedDescs: []string{"", "", ""},
},
{
name: "Out of order numbers",
content: `## Ticket 3: Third
### Ticket 1: First
# Ticket 2: Second`,
expectedCount: 3,
expectedNumbers: []int{1, 2, 3},
expectedDescs: []string{"First", "Second", "Third"},
},
{
name: "Duplicate numbers (should auto-assign)",
content: `## Ticket 1: First
### Ticket 1: Duplicate
# Ticket 2: Second`,
expectedCount: 3,
expectedNumbers: []int{1, 2, 3},
expectedDescs: []string{"First", "Duplicate", "Second"},
},
{
name: "No numbers (should auto-assign)",
content: `## Ticket: No number
### Ticket: Another no number
# Ticket: Third no number`,
expectedCount: 3,
expectedNumbers: []int{1, 2, 3},
expectedDescs: []string{"No number", "Another no number", "Third no number"},
},
{
name: "Mixed with non-ticket headers",
content: `# Project Overview
## Ticket 1: Real ticket
### Some other header
## Ticket 2: Another ticket
# Not a ticket header
### Ticket 3: Third ticket`,
expectedCount: 3,
expectedNumbers: []int{1, 2, 3},
expectedDescs: []string{"Real ticket", "Another ticket", "Third ticket"},
},
{
name: "Extra spaces",
content: `## Ticket 1 : Extra spaces
### Ticket 2 - More spaces
# Ticket 3`,
expectedCount: 3,
expectedNumbers: []int{1, 2, 3},
expectedDescs: []string{"Extra spaces", "More spaces", ""},
},
{
name: "Empty file",
content: ``,
expectedCount: 0,
expectedNumbers: []int{},
expectedDescs: []string{},
},
{
name: "Only newlines and spaces",
content: `
`,
expectedCount: 0,
expectedNumbers: []int{},
expectedDescs: []string{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Create a temporary file
tmpfile, err := ioutil.TempFile("", "test-tickets-*.md")
if err != nil {
t.Fatal(err)
}
defer os.Remove(tmpfile.Name())
// Write test content
if _, err := tmpfile.Write([]byte(tt.content)); err != nil {
t.Fatal(err)
}
if err := tmpfile.Close(); err != nil {
t.Fatal(err)
}
// Parse tickets
tickets, err := parseTickets(tmpfile.Name())
if err != nil {
t.Fatalf("parseTickets() error = %v", err)
}
// Check count
if len(tickets) != tt.expectedCount {
t.Errorf("parseTickets() returned %d tickets, want %d", len(tickets), tt.expectedCount)
}
// Check ticket details
for i, ticket := range tickets {
if i >= len(tt.expectedNumbers) {
break
}
if ticket.Number != tt.expectedNumbers[i] {
t.Errorf("ticket[%d].Number = %d, want %d", i, ticket.Number, tt.expectedNumbers[i])
}
if ticket.Description != tt.expectedDescs[i] {
t.Errorf("ticket[%d].Description = %q, want %q", i, ticket.Description, tt.expectedDescs[i])
}
}
})
}
}
func TestParseTicketsFileError(t *testing.T) {
// Test with non-existent file
_, err := parseTickets("/non/existent/file.md")
if err == nil {
t.Error("parseTickets() with non-existent file should return error")
}
}
func TestCheckFiles(t *testing.T) {
// Create a temporary directory structure
tmpDir, err := ioutil.TempDir("", "test-project-*")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpDir)
// Create input directory
inputDir := tmpDir + "/input"
if err := os.Mkdir(inputDir, 0755); err != nil {
t.Fatal(err)
}
// Create project directory
projectDir := inputDir + "/test-project"
if err := os.Mkdir(projectDir, 0755); err != nil {
t.Fatal(err)
}
// Create test files
specContent := "# Specification"
ticketsContent := `## Ticket 1: First
### Ticket 2: Second`
promptContent := "Standard prompt"
if err := ioutil.WriteFile(projectDir+"/specification.md", []byte(specContent), 0644); err != nil {
t.Fatal(err)
}
if err := ioutil.WriteFile(projectDir+"/tickets.md", []byte(ticketsContent), 0644); err != nil {
t.Fatal(err)
}
if err := ioutil.WriteFile(projectDir+"/standard-prompt.md", []byte(promptContent), 0644); err != nil {
t.Fatal(err)
}
// Change to temp directory for test
oldWd, _ := os.Getwd()
os.Chdir(tmpDir)
defer os.Chdir(oldWd)
// Test checkFiles
result := checkFiles("test-project")
// Verify results
if !result.SpecificationFound {
t.Error("specification.md should be found")
}
if !result.TicketsFound {
t.Error("tickets.md should be found")
}
if !result.StandardPromptFound {
t.Error("standard-prompt.md should be found")
}
if len(result.MissingFiles) != 0 {
t.Errorf("No files should be missing, got %v", result.MissingFiles)
}
if result.TicketCount != 2 {
t.Errorf("Should have found 2 tickets, got %d", result.TicketCount)
}
if len(result.ParsedTickets) != 2 {
t.Errorf("Should have parsed 2 tickets, got %d", len(result.ParsedTickets))
}
}
func TestCheckFilesMissing(t *testing.T) {
// Create a temporary directory structure
tmpDir, err := ioutil.TempDir("", "test-project-missing-*")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpDir)
// Create input directory
inputDir := tmpDir + "/input"
if err := os.Mkdir(inputDir, 0755); err != nil {
t.Fatal(err)
}
// Create project directory
projectDir := inputDir + "/test-project"
if err := os.Mkdir(projectDir, 0755); err != nil {
t.Fatal(err)
}
// Only create specification.md
if err := ioutil.WriteFile(projectDir+"/specification.md", []byte("# Spec"), 0644); err != nil {
t.Fatal(err)
}
// Change to temp directory for test
oldWd, _ := os.Getwd()
os.Chdir(tmpDir)
defer os.Chdir(oldWd)
// Test checkFiles
result := checkFiles("test-project")
// Verify results
if !result.SpecificationFound {
t.Error("specification.md should be found")
}
if result.TicketsFound {
t.Error("tickets.md should not be found")
}
if result.StandardPromptFound {
t.Error("standard-prompt.md should not be found")
}
if len(result.MissingFiles) != 2 {
t.Errorf("Should have 2 missing files, got %d", len(result.MissingFiles))
}
if result.TicketCount != 0 {
t.Errorf("Should have found 0 tickets, got %d", result.TicketCount)
}
}
func BenchmarkParseTickets(b *testing.B) {
// Create a test file with many tickets
content := ""
for i := 1; i <= 100; i++ {
content += fmt.Sprintf("## Ticket %d: Task number %d\n", i, i)
content += "Some description for this ticket\n\n"
}
tmpfile, err := ioutil.TempFile("", "bench-tickets-*.md")
if err != nil {
b.Fatal(err)
}
defer os.Remove(tmpfile.Name())
if _, err := tmpfile.Write([]byte(content)); err != nil {
b.Fatal(err)
}
if err := tmpfile.Close(); err != nil {
b.Fatal(err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
parseTickets(tmpfile.Name())
}
}