|
| 1 | +package core |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +func setupTestManager(t *testing.T) *Manager { |
| 10 | + tmpDir := t.TempDir() |
| 11 | + os.Setenv("ZUT_CONFIG_DIR", tmpDir) |
| 12 | + t.Cleanup(func() { |
| 13 | + os.Unsetenv("ZUT_CONFIG_DIR") |
| 14 | + }) |
| 15 | + |
| 16 | + manager, err := NewManager() |
| 17 | + if err != nil { |
| 18 | + t.Fatalf("NewManager() failed: %v", err) |
| 19 | + } |
| 20 | + |
| 21 | + return manager |
| 22 | +} |
| 23 | + |
| 24 | +func TestAddCommand(t *testing.T) { |
| 25 | + manager := setupTestManager(t) |
| 26 | + |
| 27 | + err := manager.AddCommand("test-alias", "echo test", "Test tag") |
| 28 | + if err != nil { |
| 29 | + t.Fatalf("AddCommand() failed: %v", err) |
| 30 | + } |
| 31 | + |
| 32 | + // Verify command was added |
| 33 | + entry, err := manager.GetCommand("test-alias") |
| 34 | + if err != nil { |
| 35 | + t.Fatalf("GetCommand() failed: %v", err) |
| 36 | + } |
| 37 | + |
| 38 | + if entry.Alias != "test-alias" { |
| 39 | + t.Errorf("Expected alias 'test-alias', got '%s'", entry.Alias) |
| 40 | + } |
| 41 | + if entry.Command != "echo test" { |
| 42 | + t.Errorf("Expected command 'echo test', got '%s'", entry.Command) |
| 43 | + } |
| 44 | + if entry.Tag != "Test tag" { |
| 45 | + t.Errorf("Expected tag 'Test tag', got '%s'", entry.Tag) |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +func TestAddDuplicateAlias(t *testing.T) { |
| 50 | + manager := setupTestManager(t) |
| 51 | + |
| 52 | + // Add first command |
| 53 | + err := manager.AddCommand("test-alias", "echo test", "Test tag") |
| 54 | + if err != nil { |
| 55 | + t.Fatalf("AddCommand() failed: %v", err) |
| 56 | + } |
| 57 | + |
| 58 | + // Try to add duplicate |
| 59 | + err = manager.AddCommand("test-alias", "echo duplicate", "Duplicate") |
| 60 | + if err == nil { |
| 61 | + t.Fatal("Expected error for duplicate alias, got nil") |
| 62 | + } |
| 63 | + |
| 64 | + if !strings.Contains(err.Error(), "already exists") { |
| 65 | + t.Errorf("Expected 'already exists' error, got: %v", err) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +func TestDeleteCommand(t *testing.T) { |
| 70 | + manager := setupTestManager(t) |
| 71 | + |
| 72 | + // Add command |
| 73 | + err := manager.AddCommand("test-alias", "echo test", "Test tag") |
| 74 | + if err != nil { |
| 75 | + t.Fatalf("AddCommand() failed: %v", err) |
| 76 | + } |
| 77 | + |
| 78 | + // Delete it |
| 79 | + err = manager.DeleteCommand("test-alias") |
| 80 | + if err != nil { |
| 81 | + t.Fatalf("DeleteCommand() failed: %v", err) |
| 82 | + } |
| 83 | + |
| 84 | + // Verify it's gone |
| 85 | + _, err = manager.GetCommand("test-alias") |
| 86 | + if err == nil { |
| 87 | + t.Fatal("Expected error for non-existent command, got nil") |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +func TestDeleteNonExistentCommand(t *testing.T) { |
| 92 | + manager := setupTestManager(t) |
| 93 | + |
| 94 | + err := manager.DeleteCommand("non-existent") |
| 95 | + if err == nil { |
| 96 | + t.Fatal("Expected error for non-existent command, got nil") |
| 97 | + } |
| 98 | + |
| 99 | + if !strings.Contains(err.Error(), "not found") { |
| 100 | + t.Errorf("Expected 'not found' error, got: %v", err) |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +func TestEditCommand(t *testing.T) { |
| 105 | + manager := setupTestManager(t) |
| 106 | + |
| 107 | + // Add command |
| 108 | + err := manager.AddCommand("test-alias", "echo test", "Test tag") |
| 109 | + if err != nil { |
| 110 | + t.Fatalf("AddCommand() failed: %v", err) |
| 111 | + } |
| 112 | + |
| 113 | + // Edit command |
| 114 | + err = manager.EditCommand("test-alias", "echo updated", "Updated tag") |
| 115 | + if err != nil { |
| 116 | + t.Fatalf("EditCommand() failed: %v", err) |
| 117 | + } |
| 118 | + |
| 119 | + // Verify changes |
| 120 | + entry, err := manager.GetCommand("test-alias") |
| 121 | + if err != nil { |
| 122 | + t.Fatalf("GetCommand() failed: %v", err) |
| 123 | + } |
| 124 | + |
| 125 | + if entry.Command != "echo updated" { |
| 126 | + t.Errorf("Expected command 'echo updated', got '%s'", entry.Command) |
| 127 | + } |
| 128 | + if entry.Tag != "Updated tag" { |
| 129 | + t.Errorf("Expected tag 'Updated tag', got '%s'", entry.Tag) |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +func TestEditCommandPartial(t *testing.T) { |
| 134 | + manager := setupTestManager(t) |
| 135 | + |
| 136 | + // Add command |
| 137 | + err := manager.AddCommand("test-alias", "echo test", "Test tag") |
| 138 | + if err != nil { |
| 139 | + t.Fatalf("AddCommand() failed: %v", err) |
| 140 | + } |
| 141 | + |
| 142 | + // Edit only command |
| 143 | + err = manager.EditCommand("test-alias", "echo updated", "") |
| 144 | + if err != nil { |
| 145 | + t.Fatalf("EditCommand() failed: %v", err) |
| 146 | + } |
| 147 | + |
| 148 | + // Verify only command changed |
| 149 | + entry, err := manager.GetCommand("test-alias") |
| 150 | + if err != nil { |
| 151 | + t.Fatalf("GetCommand() failed: %v", err) |
| 152 | + } |
| 153 | + |
| 154 | + if entry.Command != "echo updated" { |
| 155 | + t.Errorf("Expected command 'echo updated', got '%s'", entry.Command) |
| 156 | + } |
| 157 | + if entry.Tag != "Test tag" { |
| 158 | + t.Errorf("Expected tag 'Test tag' (unchanged), got '%s'", entry.Tag) |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +func TestEditNonExistentCommand(t *testing.T) { |
| 163 | + manager := setupTestManager(t) |
| 164 | + |
| 165 | + err := manager.EditCommand("non-existent", "echo test", "Test") |
| 166 | + if err == nil { |
| 167 | + t.Fatal("Expected error for non-existent command, got nil") |
| 168 | + } |
| 169 | + |
| 170 | + if !strings.Contains(err.Error(), "not found") { |
| 171 | + t.Errorf("Expected 'not found' error, got: %v", err) |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +func TestListCommands(t *testing.T) { |
| 176 | + manager := setupTestManager(t) |
| 177 | + |
| 178 | + // Add multiple commands |
| 179 | + commands := []struct { |
| 180 | + alias string |
| 181 | + command string |
| 182 | + tag string |
| 183 | + }{ |
| 184 | + {"cmd1", "echo 1", "Tag 1"}, |
| 185 | + {"cmd2", "echo 2", "Tag 2"}, |
| 186 | + {"cmd3", "echo 3", "Tag 3"}, |
| 187 | + } |
| 188 | + |
| 189 | + for _, cmd := range commands { |
| 190 | + err := manager.AddCommand(cmd.alias, cmd.command, cmd.tag) |
| 191 | + if err != nil { |
| 192 | + t.Fatalf("AddCommand() failed: %v", err) |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + // List all |
| 197 | + entries := manager.ListCommands() |
| 198 | + if len(entries) != len(commands) { |
| 199 | + t.Errorf("Expected %d entries, got %d", len(commands), len(entries)) |
| 200 | + } |
| 201 | +} |
| 202 | + |
| 203 | +func TestSearchCommands(t *testing.T) { |
| 204 | + manager := setupTestManager(t) |
| 205 | + |
| 206 | + // Add test commands |
| 207 | + testData := []struct { |
| 208 | + alias string |
| 209 | + command string |
| 210 | + tag string |
| 211 | + }{ |
| 212 | + {"git-push", "git push origin main", "Push to main"}, |
| 213 | + {"git-pull", "git pull origin main", "Pull from main"}, |
| 214 | + {"docker-ps", "docker ps -a", "List containers"}, |
| 215 | + {"docker-clean", "docker system prune", "Clean Docker"}, |
| 216 | + } |
| 217 | + |
| 218 | + for _, data := range testData { |
| 219 | + err := manager.AddCommand(data.alias, data.command, data.tag) |
| 220 | + if err != nil { |
| 221 | + t.Fatalf("AddCommand() failed: %v", err) |
| 222 | + } |
| 223 | + } |
| 224 | + |
| 225 | + // Test searches |
| 226 | + tests := []struct { |
| 227 | + search string |
| 228 | + expected int |
| 229 | + }{ |
| 230 | + {"git", 2}, |
| 231 | + {"docker", 2}, |
| 232 | + {"push", 1}, |
| 233 | + {"main", 2}, |
| 234 | + {"container", 1}, |
| 235 | + {"nonexistent", 0}, |
| 236 | + {"GIT", 2}, // Case insensitive |
| 237 | + } |
| 238 | + |
| 239 | + for _, tt := range tests { |
| 240 | + results := manager.SearchCommands(tt.search) |
| 241 | + if len(results) != tt.expected { |
| 242 | + t.Errorf("Search '%s': expected %d results, got %d", tt.search, tt.expected, len(results)) |
| 243 | + } |
| 244 | + } |
| 245 | +} |
| 246 | + |
| 247 | +func TestSearchCaseInsensitive(t *testing.T) { |
| 248 | + manager := setupTestManager(t) |
| 249 | + |
| 250 | + err := manager.AddCommand("TEST", "ECHO TEST", "TEST TAG") |
| 251 | + if err != nil { |
| 252 | + t.Fatalf("AddCommand() failed: %v", err) |
| 253 | + } |
| 254 | + |
| 255 | + // Search with different cases |
| 256 | + searches := []string{"test", "TEST", "TeSt", "echo", "ECHO", "tag"} |
| 257 | + for _, search := range searches { |
| 258 | + results := manager.SearchCommands(search) |
| 259 | + if len(results) != 1 { |
| 260 | + t.Errorf("Search '%s': expected 1 result, got %d", search, len(results)) |
| 261 | + } |
| 262 | + } |
| 263 | +} |
| 264 | + |
| 265 | +func TestGetNonExistentCommand(t *testing.T) { |
| 266 | + manager := setupTestManager(t) |
| 267 | + |
| 268 | + _, err := manager.GetCommand("non-existent") |
| 269 | + if err == nil { |
| 270 | + t.Fatal("Expected error for non-existent command, got nil") |
| 271 | + } |
| 272 | + |
| 273 | + if !strings.Contains(err.Error(), "not found") { |
| 274 | + t.Errorf("Expected 'not found' error, got: %v", err) |
| 275 | + } |
| 276 | +} |
0 commit comments