|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "agentmail/internal/daemon" |
| 10 | +) |
| 11 | + |
| 12 | +// ============================================================================= |
| 13 | +// T003-T004: Tests for MailmanStop - successful stop file creation |
| 14 | +// ============================================================================= |
| 15 | + |
| 16 | +func TestMailmanStop_CreatesStopFile(t *testing.T) { |
| 17 | + // Create temp directory for test |
| 18 | + tmpDir, err := os.MkdirTemp("", "agentmail-stop-test-*") |
| 19 | + if err != nil { |
| 20 | + t.Fatalf("Failed to create temp dir: %v", err) |
| 21 | + } |
| 22 | + defer os.RemoveAll(tmpDir) |
| 23 | + |
| 24 | + // Create .agentmail directory (simulating existing daemon setup) |
| 25 | + agentmailDir := filepath.Join(tmpDir, ".agentmail") |
| 26 | + if err := os.MkdirAll(agentmailDir, 0755); err != nil { |
| 27 | + t.Fatalf("Failed to create .agentmail dir: %v", err) |
| 28 | + } |
| 29 | + |
| 30 | + var stdout, stderr bytes.Buffer |
| 31 | + |
| 32 | + // Run stop command |
| 33 | + exitCode := MailmanStop(&stdout, &stderr, MailmanStopOptions{ |
| 34 | + RepoRoot: tmpDir, |
| 35 | + }) |
| 36 | + |
| 37 | + // Verify exit code 0 |
| 38 | + if exitCode != 0 { |
| 39 | + t.Errorf("Expected exit code 0, got %d. stderr: %s", exitCode, stderr.String()) |
| 40 | + } |
| 41 | + |
| 42 | + // Verify stop file was created |
| 43 | + stopPath := daemon.StopFilePath(tmpDir) |
| 44 | + if _, err := os.Stat(stopPath); os.IsNotExist(err) { |
| 45 | + t.Error("Stop file was not created") |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +func TestMailmanStop_OutputsSuccessMessage(t *testing.T) { |
| 50 | + // Create temp directory for test |
| 51 | + tmpDir, err := os.MkdirTemp("", "agentmail-stop-test-*") |
| 52 | + if err != nil { |
| 53 | + t.Fatalf("Failed to create temp dir: %v", err) |
| 54 | + } |
| 55 | + defer os.RemoveAll(tmpDir) |
| 56 | + |
| 57 | + // Create .agentmail directory |
| 58 | + agentmailDir := filepath.Join(tmpDir, ".agentmail") |
| 59 | + if err := os.MkdirAll(agentmailDir, 0755); err != nil { |
| 60 | + t.Fatalf("Failed to create .agentmail dir: %v", err) |
| 61 | + } |
| 62 | + |
| 63 | + var stdout, stderr bytes.Buffer |
| 64 | + |
| 65 | + // Run stop command |
| 66 | + exitCode := MailmanStop(&stdout, &stderr, MailmanStopOptions{ |
| 67 | + RepoRoot: tmpDir, |
| 68 | + }) |
| 69 | + |
| 70 | + // Verify exit code 0 |
| 71 | + if exitCode != 0 { |
| 72 | + t.Errorf("Expected exit code 0, got %d", exitCode) |
| 73 | + } |
| 74 | + |
| 75 | + // Verify success message |
| 76 | + expectedMsg := "Stop signal sent\n" |
| 77 | + if stdout.String() != expectedMsg { |
| 78 | + t.Errorf("Expected stdout %q, got %q", expectedMsg, stdout.String()) |
| 79 | + } |
| 80 | + |
| 81 | + // Verify no stderr output |
| 82 | + if stderr.String() != "" { |
| 83 | + t.Errorf("Expected empty stderr, got %q", stderr.String()) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +// ============================================================================= |
| 88 | +// T015-T016: Tests for MailmanStop - stop already pending |
| 89 | +// ============================================================================= |
| 90 | + |
| 91 | +func TestMailmanStop_StopAlreadyPending_ReturnsError(t *testing.T) { |
| 92 | + // Create temp directory for test |
| 93 | + tmpDir, err := os.MkdirTemp("", "agentmail-stop-test-*") |
| 94 | + if err != nil { |
| 95 | + t.Fatalf("Failed to create temp dir: %v", err) |
| 96 | + } |
| 97 | + defer os.RemoveAll(tmpDir) |
| 98 | + |
| 99 | + // Create .agentmail directory |
| 100 | + agentmailDir := filepath.Join(tmpDir, ".agentmail") |
| 101 | + if err := os.MkdirAll(agentmailDir, 0755); err != nil { |
| 102 | + t.Fatalf("Failed to create .agentmail dir: %v", err) |
| 103 | + } |
| 104 | + |
| 105 | + // Pre-create the stop file to simulate pending stop |
| 106 | + stopPath := daemon.StopFilePath(tmpDir) |
| 107 | + if err := os.WriteFile(stopPath, []byte{}, 0600); err != nil { |
| 108 | + t.Fatalf("Failed to create stop file: %v", err) |
| 109 | + } |
| 110 | + |
| 111 | + var stdout, stderr bytes.Buffer |
| 112 | + |
| 113 | + // Run stop command |
| 114 | + exitCode := MailmanStop(&stdout, &stderr, MailmanStopOptions{ |
| 115 | + RepoRoot: tmpDir, |
| 116 | + }) |
| 117 | + |
| 118 | + // Verify exit code 1 |
| 119 | + if exitCode != 1 { |
| 120 | + t.Errorf("Expected exit code 1, got %d", exitCode) |
| 121 | + } |
| 122 | + |
| 123 | + // Verify error message |
| 124 | + expectedMsg := "Stop already pending\n" |
| 125 | + if stderr.String() != expectedMsg { |
| 126 | + t.Errorf("Expected stderr %q, got %q", expectedMsg, stderr.String()) |
| 127 | + } |
| 128 | + |
| 129 | + // Verify no stdout output |
| 130 | + if stdout.String() != "" { |
| 131 | + t.Errorf("Expected empty stdout, got %q", stdout.String()) |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +func TestMailmanStop_FilesystemError_ReturnsError(t *testing.T) { |
| 136 | + // Create temp directory for test |
| 137 | + tmpDir, err := os.MkdirTemp("", "agentmail-stop-test-*") |
| 138 | + if err != nil { |
| 139 | + t.Fatalf("Failed to create temp dir: %v", err) |
| 140 | + } |
| 141 | + defer os.RemoveAll(tmpDir) |
| 142 | + |
| 143 | + // Do NOT create .agentmail directory - this should cause a filesystem error |
| 144 | + // when trying to create the stop file |
| 145 | + |
| 146 | + var stdout, stderr bytes.Buffer |
| 147 | + |
| 148 | + // Run stop command |
| 149 | + exitCode := MailmanStop(&stdout, &stderr, MailmanStopOptions{ |
| 150 | + RepoRoot: tmpDir, |
| 151 | + }) |
| 152 | + |
| 153 | + // Verify exit code 1 |
| 154 | + if exitCode != 1 { |
| 155 | + t.Errorf("Expected exit code 1, got %d", exitCode) |
| 156 | + } |
| 157 | + |
| 158 | + // Verify error message contains expected prefix |
| 159 | + expectedPrefix := "Failed to send stop signal:" |
| 160 | + if len(stderr.String()) < len(expectedPrefix) || stderr.String()[:len(expectedPrefix)] != expectedPrefix { |
| 161 | + t.Errorf("Expected stderr to start with %q, got %q", expectedPrefix, stderr.String()) |
| 162 | + } |
| 163 | + |
| 164 | + // Verify no stdout output |
| 165 | + if stdout.String() != "" { |
| 166 | + t.Errorf("Expected empty stdout, got %q", stdout.String()) |
| 167 | + } |
| 168 | +} |
0 commit comments