-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathe2e_cli_test.go
More file actions
635 lines (525 loc) · 19.8 KB
/
e2e_cli_test.go
File metadata and controls
635 lines (525 loc) · 19.8 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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
package main_test
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TestE2E_CLI_Register tests the register command variants
func TestE2E_CLI_Register(t *testing.T) {
env := setupE2E(t)
t.Run("register with explicit project flag", func(t *testing.T) {
output, err := env.runCLI(t, "register", "--project", env.ProjectDir)
require.NoError(t, err)
assert.Contains(t, output, "Registered project")
assert.Contains(t, output, env.ProjectDir)
})
t.Run("register without project flag uses current directory", func(t *testing.T) {
// Change to project directory
oldDir, _ := os.Getwd()
defer func() { _ = os.Chdir(oldDir) }()
_ = os.Chdir(env.ProjectDir)
output, err := env.runCLI(t, "register")
require.NoError(t, err)
assert.Contains(t, output, "Registered project")
assert.Contains(t, output, env.ProjectDir)
})
t.Run("register same project twice is idempotent", func(t *testing.T) {
// First registration
output1, err1 := env.runCLI(t, "register", "--project", env.ProjectDir)
require.NoError(t, err1)
assert.Contains(t, output1, "Registered project")
// Second registration should also succeed (idempotent)
output2, err2 := env.runCLI(t, "register", "--project", env.ProjectDir)
require.NoError(t, err2)
assert.Contains(t, output2, "Registered project")
})
t.Run("register with dot as project directory", func(t *testing.T) {
oldDir, _ := os.Getwd()
defer func() { _ = os.Chdir(oldDir) }()
_ = os.Chdir(env.ProjectDir)
output, err := env.runCLI(t, "register", "--project", ".")
require.NoError(t, err)
assert.Contains(t, output, "Registered project")
assert.Contains(t, output, env.ProjectDir)
})
}
// TestE2E_CLI_Address tests the address command edge cases
// Note: Full workflow testing is covered in TestE2E_CommentWorkflow
func TestE2E_CLI_Address(t *testing.T) {
t.Run("address without file flag shows error", func(t *testing.T) {
env := setupE2E(t)
_, err := env.runCLI(t, "register", "--project", env.ProjectDir)
require.NoError(t, err)
output, err := env.runCLI(t, "address", "--project", env.ProjectDir)
require.Error(t, err)
assert.Contains(t, output, "--file flag is required")
})
t.Run("address with no comments", func(t *testing.T) {
env := setupE2E(t)
_, err := env.runCLI(t, "register", "--project", env.ProjectDir)
require.NoError(t, err)
output, err := env.runCLI(t, "address", "--file", "test.md", "--project", env.ProjectDir)
require.NoError(t, err)
assert.Contains(t, output, "No unresolved comments")
})
t.Run("address strips @ prefix from file path", func(t *testing.T) {
env := setupE2E(t)
_, err := env.runCLI(t, "register", "--project", env.ProjectDir)
require.NoError(t, err)
// Use @ prefix in CLI (should work identically to without prefix)
output, err := env.runCLI(t, "address", "--file", "@test.md", "--project", env.ProjectDir)
require.NoError(t, err)
assert.Contains(t, output, "No unresolved comments")
})
t.Run("address without project flag uses current directory", func(t *testing.T) {
env := setupE2E(t)
oldDir, _ := os.Getwd()
defer func() { _ = os.Chdir(oldDir) }()
_ = os.Chdir(env.ProjectDir)
_, err := env.runCLI(t, "register")
require.NoError(t, err)
output, err := env.runCLI(t, "address", "--file", "test.md")
require.NoError(t, err)
assert.Contains(t, output, "No unresolved comments")
})
}
// TestE2E_CLI_Resolve tests the resolve command edge cases
// Note: Full workflow testing is covered in TestE2E_CommentWorkflow
func TestE2E_CLI_Resolve(t *testing.T) {
t.Run("resolve without file flag shows error", func(t *testing.T) {
env := setupE2E(t)
_, err := env.runCLI(t, "register", "--project", env.ProjectDir)
require.NoError(t, err)
output, err := env.runCLI(t, "resolve", "--project", env.ProjectDir)
require.Error(t, err)
assert.Contains(t, output, "--file flag is required")
})
t.Run("resolve with no comments", func(t *testing.T) {
env := setupE2E(t)
_, err := env.runCLI(t, "register", "--project", env.ProjectDir)
require.NoError(t, err)
output, err := env.runCLI(t, "resolve", "--file", "test.md", "--project", env.ProjectDir)
require.NoError(t, err)
assert.Contains(t, output, "No unresolved comments")
})
t.Run("resolve strips @ prefix from file path", func(t *testing.T) {
env := setupE2E(t)
_, err := env.runCLI(t, "register", "--project", env.ProjectDir)
require.NoError(t, err)
// Resolve with @ prefix (should work identically to without prefix)
output, err := env.runCLI(t, "resolve", "--file", "@test.md", "--project", env.ProjectDir)
require.NoError(t, err)
assert.Contains(t, output, "No unresolved comments")
})
t.Run("resolve without project flag uses current directory", func(t *testing.T) {
env := setupE2E(t)
oldDir, _ := os.Getwd()
defer func() { _ = os.Chdir(oldDir) }()
_ = os.Chdir(env.ProjectDir)
_, err := env.runCLI(t, "register")
require.NoError(t, err)
output, err := env.runCLI(t, "resolve", "--file", "test.md")
require.NoError(t, err)
assert.Contains(t, output, "No unresolved comments")
})
}
// TestE2E_CLI_Review tests the review command
func TestE2E_CLI_Review(t *testing.T) {
// Create isolated environment without starting server
tempDir := t.TempDir()
dataDir := filepath.Join(tempDir, "data")
projectDir := filepath.Join(tempDir, "project")
binaryPath := filepath.Join(tempDir, "claude-review")
require.NoError(t, os.MkdirAll(dataDir, 0755))
require.NoError(t, os.MkdirAll(projectDir, 0755))
createTestMarkdownFiles(t, projectDir)
// Build binary
buildCmd := exec.Command("go", "build", "-cover", "-o", binaryPath, ".")
require.NoError(t, buildCmd.Run())
env := &TestEnv{
TempDir: tempDir,
DataDir: dataDir,
ProjectDir: projectDir,
Port: "14780", // Different port to avoid conflict
BinaryPath: binaryPath,
}
// Ensure daemon is stopped on test completion
t.Cleanup(func() {
_, _ = env.runCLI(t, "server", "--stop")
_ = waitForPIDFileRemoved(env.PIDFile(), 2*time.Second)
})
t.Run("review without file flag shows error", func(t *testing.T) {
output, err := env.runCLI(t, "review", "--project", env.ProjectDir)
require.Error(t, err)
assert.Contains(t, output, "--file flag is required")
})
t.Run("review outputs URL and starts daemon", func(t *testing.T) {
output, err := env.runCLI(t, "review", "--file", "test.md", "--project", env.ProjectDir)
require.NoError(t, err)
assert.Contains(t, output, "Open this URL")
assert.Contains(t, output, fmt.Sprintf("http://localhost:%s/projects", env.Port))
assert.Contains(t, output, "test.md")
})
t.Run("review strips @ prefix from file path", func(t *testing.T) {
output, err := env.runCLI(t, "review", "--file", "@simple.md", "--project", env.ProjectDir)
require.NoError(t, err)
assert.Contains(t, output, "simple.md")
assert.NotContains(t, output, "@simple.md")
})
}
// TestE2E_CLI_Version tests the version command
func TestE2E_CLI_Version(t *testing.T) {
env := setupE2E(t)
output, err := env.runCLI(t, "version")
require.NoError(t, err)
// Should output version string (currently "dev" or semver)
assert.NotEmpty(t, strings.TrimSpace(output))
}
// TestE2E_CLI_UnknownCommand tests error handling for unknown commands
func TestE2E_CLI_UnknownCommand(t *testing.T) {
env := setupE2E(t)
output, err := env.runCLI(t, "nonexistent-command")
require.Error(t, err)
assert.Contains(t, output, "Unknown command")
}
// TestE2E_CLI_NoCommand tests help output when no command is provided
func TestE2E_CLI_NoCommand(t *testing.T) {
env := setupE2E(t)
// Run with no arguments
cmd := exec.Command(env.BinaryPath)
cmd.Env = append(os.Environ(),
"CR_DATA_DIR="+env.DataDir,
"CR_LISTEN_PORT="+env.Port,
"GOCOVERDIR=tmp/coverage",
)
output, err := cmd.CombinedOutput()
require.Error(t, err) // Should exit with error
outputStr := string(output)
assert.Contains(t, outputStr, "Usage: claude-review <command>")
assert.Contains(t, outputStr, "Commands:")
assert.Contains(t, outputStr, "server")
assert.Contains(t, outputStr, "register")
assert.Contains(t, outputStr, "address")
assert.Contains(t, outputStr, "resolve")
assert.Contains(t, outputStr, "review")
}
// TestE2E_CLI_Install tests the install command for slash commands
func TestE2E_CLI_Install(t *testing.T) {
// Create isolated environment with custom home directory
tempDir := t.TempDir()
homeDir := filepath.Join(tempDir, "home")
require.NoError(t, os.MkdirAll(homeDir, 0755))
// Build binary
binaryPath := filepath.Join(tempDir, "claude-review")
buildCmd := exec.Command("go", "build", "-cover", "-o", binaryPath, ".")
require.NoError(t, buildCmd.Run())
t.Run("install creates commands directory and files", func(t *testing.T) {
// Run install command with custom HOME
cmd := exec.Command(binaryPath, "install")
cmd.Env = append(os.Environ(),
"HOME="+homeDir,
"GOCOVERDIR=tmp/coverage",
)
output, err := cmd.CombinedOutput()
require.NoError(t, err)
outputStr := string(output)
// Check output messages
assert.Contains(t, outputStr, "Successfully installed")
assert.Contains(t, outputStr, "slash command")
assert.Contains(t, outputStr, filepath.Join(homeDir, ".claude", "commands"))
// Verify commands directory was created
commandsDir := filepath.Join(homeDir, ".claude", "commands")
stat, err := os.Stat(commandsDir)
require.NoError(t, err)
assert.True(t, stat.IsDir())
// Verify slash command files were installed
entries, err := os.ReadDir(commandsDir)
require.NoError(t, err)
assert.Greater(t, len(entries), 0, "Should install at least one slash command")
// Check for expected slash commands
var foundCommands []string
for _, entry := range entries {
if strings.HasSuffix(entry.Name(), ".md") {
foundCommands = append(foundCommands, entry.Name())
}
}
// Verify expected commands exist
assert.Contains(t, foundCommands, "cr-review.md")
assert.Contains(t, foundCommands, "cr-address.md")
// Verify output lists the installed commands
assert.Contains(t, outputStr, "/cr-review")
assert.Contains(t, outputStr, "/cr-address")
})
t.Run("install is idempotent and overwrites existing files", func(t *testing.T) {
commandsDir := filepath.Join(homeDir, ".claude", "commands")
// Get initial file count
initialEntries, err := os.ReadDir(commandsDir)
require.NoError(t, err)
initialCount := len(initialEntries)
// Modify one of the files
testFile := filepath.Join(commandsDir, "cr-review.md")
require.NoError(t, os.WriteFile(testFile, []byte("MODIFIED CONTENT"), 0644))
// Run install again
cmd := exec.Command(binaryPath, "install")
cmd.Env = append(os.Environ(),
"HOME="+homeDir,
"GOCOVERDIR=tmp/coverage",
)
output, err := cmd.CombinedOutput()
require.NoError(t, err)
// Verify file was overwritten
content, err := os.ReadFile(testFile)
require.NoError(t, err)
assert.NotContains(t, string(content), "MODIFIED CONTENT")
// Verify same number of files (no duplicates)
finalEntries, err := os.ReadDir(commandsDir)
require.NoError(t, err)
assert.Equal(t, initialCount, len(finalEntries), "Should not create duplicate files")
// Verify output still shows success
outputStr := string(output)
assert.Contains(t, outputStr, "Successfully installed")
})
t.Run("install creates missing parent directories", func(t *testing.T) {
// Create fresh home directory without .claude
freshHome := filepath.Join(tempDir, "fresh-home")
require.NoError(t, os.MkdirAll(freshHome, 0755))
cmd := exec.Command(binaryPath, "install")
cmd.Env = append(os.Environ(),
"HOME="+freshHome,
"GOCOVERDIR=tmp/coverage",
)
output, err := cmd.CombinedOutput()
require.NoError(t, err)
// Verify .claude/commands was created
commandsDir := filepath.Join(freshHome, ".claude", "commands")
stat, err := os.Stat(commandsDir)
require.NoError(t, err)
assert.True(t, stat.IsDir())
// Verify files were installed
outputStr := string(output)
assert.Contains(t, outputStr, "Successfully installed")
})
t.Run("install verifies file contents are correct", func(t *testing.T) {
commandsDir := filepath.Join(homeDir, ".claude", "commands")
// Read installed cr-address.md
addressFile := filepath.Join(commandsDir, "cr-address.md")
content, err := os.ReadFile(addressFile)
require.NoError(t, err)
contentStr := string(content)
// Verify it contains expected content (should reference claude-review command)
assert.Contains(t, contentStr, "claude-review")
assert.Contains(t, contentStr, "address")
// Read installed cr-review.md
reviewFile := filepath.Join(commandsDir, "cr-review.md")
content, err = os.ReadFile(reviewFile)
require.NoError(t, err)
contentStr = string(content)
// Verify it contains expected content
assert.Contains(t, contentStr, "claude-review")
assert.Contains(t, contentStr, "review")
})
t.Run("install file permissions are correct", func(t *testing.T) {
commandsDir := filepath.Join(homeDir, ".claude", "commands")
// Check directory permissions
dirStat, err := os.Stat(commandsDir)
require.NoError(t, err)
assert.Equal(t, os.FileMode(0755), dirStat.Mode().Perm())
// Check file permissions
entries, err := os.ReadDir(commandsDir)
require.NoError(t, err)
for _, entry := range entries {
if strings.HasSuffix(entry.Name(), ".md") {
filePath := filepath.Join(commandsDir, entry.Name())
fileStat, err := os.Stat(filePath)
require.NoError(t, err)
assert.Equal(t, os.FileMode(0644), fileStat.Mode().Perm(),
"File %s should have 0644 permissions", entry.Name())
}
}
})
}
// TestE2E_CLI_Uninstall tests the uninstall command for slash commands
func TestE2E_CLI_Uninstall(t *testing.T) {
// Create isolated environment with custom home directory
tempDir := t.TempDir()
homeDir := filepath.Join(tempDir, "home")
require.NoError(t, os.MkdirAll(homeDir, 0755))
// Build binary
binaryPath := filepath.Join(tempDir, "claude-review")
buildCmd := exec.Command("go", "build", "-cover", "-o", binaryPath, ".")
require.NoError(t, buildCmd.Run())
// Helper to run install
runInstall := func() {
cmd := exec.Command(binaryPath, "install")
cmd.Env = append(os.Environ(),
"HOME="+homeDir,
"GOCOVERDIR=tmp/coverage",
)
output, err := cmd.CombinedOutput()
require.NoError(t, err, "Install failed: %s", string(output))
}
// Helper to run uninstall
runUninstall := func() (string, error) {
cmd := exec.Command(binaryPath, "uninstall")
cmd.Env = append(os.Environ(),
"HOME="+homeDir,
"GOCOVERDIR=tmp/coverage",
)
output, err := cmd.CombinedOutput()
return string(output), err
}
t.Run("uninstall removes installed slash commands", func(t *testing.T) {
// First install commands
runInstall()
commandsDir := filepath.Join(homeDir, ".claude", "commands")
// Verify files exist before uninstall
entries, err := os.ReadDir(commandsDir)
require.NoError(t, err)
require.Greater(t, len(entries), 0, "Should have installed files")
// Run uninstall
output, err := runUninstall()
require.NoError(t, err)
// Check output messages
assert.Contains(t, output, "Successfully uninstalled")
assert.Contains(t, output, "slash command")
assert.Contains(t, output, filepath.Join(homeDir, ".claude", "commands"))
assert.Contains(t, output, "/cr-review")
assert.Contains(t, output, "/cr-address")
// Verify files were removed
crReviewPath := filepath.Join(commandsDir, "cr-review.md")
_, err = os.Stat(crReviewPath)
assert.True(t, os.IsNotExist(err), "cr-review.md should be removed")
crAddressPath := filepath.Join(commandsDir, "cr-address.md")
_, err = os.Stat(crAddressPath)
assert.True(t, os.IsNotExist(err), "cr-address.md should be removed")
})
t.Run("uninstall when no commands are installed", func(t *testing.T) {
// Create fresh home directory
freshHome := filepath.Join(tempDir, "fresh-home-uninstall")
require.NoError(t, os.MkdirAll(freshHome, 0755))
cmd := exec.Command(binaryPath, "uninstall")
cmd.Env = append(os.Environ(),
"HOME="+freshHome,
"GOCOVERDIR=tmp/coverage",
)
output, err := cmd.CombinedOutput()
require.NoError(t, err)
outputStr := string(output)
// Should report that no commands were installed
assert.Contains(t, outputStr, "No slash commands were installed")
})
t.Run("uninstall is idempotent", func(t *testing.T) {
// Create fresh home and install
freshHome := filepath.Join(tempDir, "home-idempotent")
require.NoError(t, os.MkdirAll(freshHome, 0755))
// Install
cmd := exec.Command(binaryPath, "install")
cmd.Env = append(os.Environ(),
"HOME="+freshHome,
"GOCOVERDIR=tmp/coverage",
)
_, err := cmd.CombinedOutput()
require.NoError(t, err)
// First uninstall
cmd = exec.Command(binaryPath, "uninstall")
cmd.Env = append(os.Environ(),
"HOME="+freshHome,
"GOCOVERDIR=tmp/coverage",
)
output1, err := cmd.CombinedOutput()
require.NoError(t, err)
assert.Contains(t, string(output1), "Successfully uninstalled")
// Second uninstall (should succeed without error)
cmd = exec.Command(binaryPath, "uninstall")
cmd.Env = append(os.Environ(),
"HOME="+freshHome,
"GOCOVERDIR=tmp/coverage",
)
output2, err := cmd.CombinedOutput()
require.NoError(t, err)
assert.Contains(t, string(output2), "No slash commands were installed")
})
t.Run("uninstall only removes managed commands", func(t *testing.T) {
// Create fresh home and install
freshHome := filepath.Join(tempDir, "home-selective")
require.NoError(t, os.MkdirAll(freshHome, 0755))
cmd := exec.Command(binaryPath, "install")
cmd.Env = append(os.Environ(),
"HOME="+freshHome,
"GOCOVERDIR=tmp/coverage",
)
_, err := cmd.CombinedOutput()
require.NoError(t, err)
// Add a user's custom command
commandsDir := filepath.Join(freshHome, ".claude", "commands")
customFile := filepath.Join(commandsDir, "custom-command.md")
require.NoError(t, os.WriteFile(customFile, []byte("# Custom command"), 0644))
// Run uninstall
cmd = exec.Command(binaryPath, "uninstall")
cmd.Env = append(os.Environ(),
"HOME="+freshHome,
"GOCOVERDIR=tmp/coverage",
)
output, err := cmd.CombinedOutput()
require.NoError(t, err)
assert.Contains(t, string(output), "Successfully uninstalled")
// Verify custom command still exists
_, err = os.Stat(customFile)
assert.NoError(t, err, "Custom command should not be removed")
// Verify managed commands are removed
crReviewPath := filepath.Join(commandsDir, "cr-review.md")
_, err = os.Stat(crReviewPath)
assert.True(t, os.IsNotExist(err), "cr-review.md should be removed")
})
t.Run("uninstall and reinstall works correctly", func(t *testing.T) {
// Create fresh home
freshHome := filepath.Join(tempDir, "home-reinstall")
require.NoError(t, os.MkdirAll(freshHome, 0755))
commandsDir := filepath.Join(freshHome, ".claude", "commands")
// Install
cmd := exec.Command(binaryPath, "install")
cmd.Env = append(os.Environ(),
"HOME="+freshHome,
"GOCOVERDIR=tmp/coverage",
)
_, err := cmd.CombinedOutput()
require.NoError(t, err)
// Verify installed
crReviewPath := filepath.Join(commandsDir, "cr-review.md")
_, err = os.Stat(crReviewPath)
require.NoError(t, err, "Should be installed")
// Uninstall
cmd = exec.Command(binaryPath, "uninstall")
cmd.Env = append(os.Environ(),
"HOME="+freshHome,
"GOCOVERDIR=tmp/coverage",
)
_, err = cmd.CombinedOutput()
require.NoError(t, err)
// Verify uninstalled
_, err = os.Stat(crReviewPath)
assert.True(t, os.IsNotExist(err), "Should be uninstalled")
// Reinstall
cmd = exec.Command(binaryPath, "install")
cmd.Env = append(os.Environ(),
"HOME="+freshHome,
"GOCOVERDIR=tmp/coverage",
)
output, err := cmd.CombinedOutput()
require.NoError(t, err)
assert.Contains(t, string(output), "Successfully installed")
// Verify reinstalled
_, err = os.Stat(crReviewPath)
assert.NoError(t, err, "Should be reinstalled")
// Verify content is correct
content, err := os.ReadFile(crReviewPath)
require.NoError(t, err)
assert.Contains(t, string(content), "claude-review")
})
}