| 1 | package project |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | "testing" |
| 7 | ) |
| 8 | |
| 9 | func TestLoadFromEmpty(t *testing.T) { |
| 10 | dir := t.TempDir() |
| 11 | path := filepath.Join(dir, "projects.json") |
| 12 | |
| 13 | reg, err := LoadFrom(path) |
| 14 | if err != nil { |
| 15 | t.Fatalf("LoadFrom non-existent file: %v", err) |
| 16 | } |
| 17 | if len(reg.Projects) != 0 { |
| 18 | t.Errorf("expected empty projects map, got %d entries", len(reg.Projects)) |
| 19 | } |
| 20 | if reg.ProjectsDir == "" { |
| 21 | t.Error("expected default ProjectsDir, got empty string") |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | func TestSaveAndLoad(t *testing.T) { |
| 26 | dir := t.TempDir() |
| 27 | path := filepath.Join(dir, "projects.json") |
| 28 | |
| 29 | reg := &Registry{ |
| 30 | ProjectsDir: "/home/test/projects", |
| 31 | Projects: map[string]string{"myrepo": "/home/test/projects/myrepo"}, |
| 32 | } |
| 33 | |
| 34 | if err := reg.SaveTo(path); err != nil { |
| 35 | t.Fatalf("SaveTo: %v", err) |
| 36 | } |
| 37 | |
| 38 | loaded, err := LoadFrom(path) |
| 39 | if err != nil { |
| 40 | t.Fatalf("LoadFrom: %v", err) |
| 41 | } |
| 42 | |
| 43 | if loaded.ProjectsDir != reg.ProjectsDir { |
| 44 | t.Errorf("ProjectsDir = %q, want %q", loaded.ProjectsDir, reg.ProjectsDir) |
| 45 | } |
| 46 | if len(loaded.Projects) != 1 { |
| 47 | t.Fatalf("expected 1 project, got %d", len(loaded.Projects)) |
| 48 | } |
| 49 | if loaded.Projects["myrepo"] != "/home/test/projects/myrepo" { |
| 50 | t.Errorf("myrepo path = %q, want %q", loaded.Projects["myrepo"], "/home/test/projects/myrepo") |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | func TestAddAndGet(t *testing.T) { |
| 55 | reg := &Registry{ |
| 56 | ProjectsDir: "/tmp/projects", |
| 57 | Projects: make(map[string]string), |
| 58 | } |
| 59 | |
| 60 | if err := reg.Add("foo", "/tmp/projects/foo"); err != nil { |
| 61 | t.Fatalf("Add: %v", err) |
| 62 | } |
| 63 | |
| 64 | path, ok := reg.Get("foo") |
| 65 | if !ok { |
| 66 | t.Fatal("Get returned false for existing project") |
| 67 | } |
| 68 | if path != "/tmp/projects/foo" { |
| 69 | t.Errorf("Get = %q, want %q", path, "/tmp/projects/foo") |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | func TestAddDuplicateReturnsError(t *testing.T) { |
| 74 | reg := &Registry{ |
| 75 | ProjectsDir: "/tmp/projects", |
| 76 | Projects: map[string]string{"foo": "/tmp/foo"}, |
| 77 | } |
| 78 | |
| 79 | err := reg.Add("foo", "/tmp/other/foo") |
| 80 | if err == nil { |
| 81 | t.Fatal("expected error for duplicate name, got nil") |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | func TestRemove(t *testing.T) { |
| 86 | reg := &Registry{ |
| 87 | ProjectsDir: "/tmp/projects", |
| 88 | Projects: map[string]string{"foo": "/tmp/foo"}, |
| 89 | } |
| 90 | |
| 91 | if err := reg.Remove("foo"); err != nil { |
| 92 | t.Fatalf("Remove: %v", err) |
| 93 | } |
| 94 | |
| 95 | _, ok := reg.Get("foo") |
| 96 | if ok { |
| 97 | t.Error("Get returned true after Remove") |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | func TestRemoveNonExistent(t *testing.T) { |
| 102 | reg := &Registry{ |
| 103 | ProjectsDir: "/tmp/projects", |
| 104 | Projects: make(map[string]string), |
| 105 | } |
| 106 | |
| 107 | err := reg.Remove("nope") |
| 108 | if err == nil { |
| 109 | t.Fatal("expected error for non-existent project, got nil") |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | func TestGetNonExistent(t *testing.T) { |
| 114 | reg := &Registry{ |
| 115 | ProjectsDir: "/tmp/projects", |
| 116 | Projects: make(map[string]string), |
| 117 | } |
| 118 | |
| 119 | _, ok := reg.Get("nope") |
| 120 | if ok { |
| 121 | t.Error("Get returned true for non-existent project") |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | func TestList(t *testing.T) { |
| 126 | reg := &Registry{ |
| 127 | ProjectsDir: "/tmp/projects", |
| 128 | Projects: map[string]string{ |
| 129 | "a": "/tmp/a", |
| 130 | "b": "/tmp/b", |
| 131 | }, |
| 132 | } |
| 133 | |
| 134 | list := reg.List() |
| 135 | if len(list) != 2 { |
| 136 | t.Fatalf("List returned %d entries, want 2", len(list)) |
| 137 | } |
| 138 | if list["a"] != "/tmp/a" { |
| 139 | t.Errorf("list[a] = %q, want %q", list["a"], "/tmp/a") |
| 140 | } |
| 141 | if list["b"] != "/tmp/b" { |
| 142 | t.Errorf("list[b] = %q, want %q", list["b"], "/tmp/b") |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | func TestSetProjectsDir(t *testing.T) { |
| 147 | reg := &Registry{ |
| 148 | ProjectsDir: "/old", |
| 149 | Projects: make(map[string]string), |
| 150 | } |
| 151 | |
| 152 | reg.SetProjectsDir("/new/path") |
| 153 | if reg.ProjectsDir != "/new/path" { |
| 154 | t.Errorf("ProjectsDir = %q, want %q", reg.ProjectsDir, "/new/path") |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | func TestExpandHome(t *testing.T) { |
| 159 | home, err := os.UserHomeDir() |
| 160 | if err != nil { |
| 161 | t.Skip("cannot determine home dir") |
| 162 | } |
| 163 | |
| 164 | tests := []struct { |
| 165 | name string |
| 166 | input string |
| 167 | want string |
| 168 | }{ |
| 169 | {"tilde only", "~", home}, |
| 170 | {"tilde slash", "~/foo/bar", filepath.Join(home, "foo/bar")}, |
| 171 | {"absolute", "/usr/bin", "/usr/bin"}, |
| 172 | {"relative", "foo/bar", "foo/bar"}, |
| 173 | } |
| 174 | |
| 175 | for _, tt := range tests { |
| 176 | t.Run(tt.name, func(t *testing.T) { |
| 177 | got, err := ExpandHome(tt.input) |
| 178 | if err != nil { |
| 179 | t.Fatalf("ExpandHome(%q): %v", tt.input, err) |
| 180 | } |
| 181 | if got != tt.want { |
| 182 | t.Errorf("ExpandHome(%q) = %q, want %q", tt.input, got, tt.want) |
| 183 | } |
| 184 | }) |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | func TestSaveContractsHomePaths(t *testing.T) { |
| 189 | home, err := os.UserHomeDir() |
| 190 | if err != nil { |
| 191 | t.Skip("cannot determine home dir") |
| 192 | } |
| 193 | |
| 194 | dir := t.TempDir() |
| 195 | path := filepath.Join(dir, "projects.json") |
| 196 | |
| 197 | reg := &Registry{ |
| 198 | ProjectsDir: filepath.Join(home, "projects"), |
| 199 | Projects: map[string]string{"foo": filepath.Join(home, "projects/foo")}, |
| 200 | } |
| 201 | |
| 202 | if err := reg.SaveTo(path); err != nil { |
| 203 | t.Fatalf("SaveTo: %v", err) |
| 204 | } |
| 205 | |
| 206 | // Read raw JSON to verify ~ is stored |
| 207 | data, err := os.ReadFile(path) |
| 208 | if err != nil { |
| 209 | t.Fatalf("ReadFile: %v", err) |
| 210 | } |
| 211 | |
| 212 | raw := string(data) |
| 213 | if filepath.Join(home, "projects") != "" { |
| 214 | // The file should contain ~/projects, not the expanded path |
| 215 | if !contains(raw, "~/projects") { |
| 216 | t.Errorf("saved file should contain ~/projects, got:\n%s", raw) |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | func TestRoundTripWithTildePaths(t *testing.T) { |
| 222 | dir := t.TempDir() |
| 223 | path := filepath.Join(dir, "projects.json") |
| 224 | |
| 225 | // Write a file with tilde paths |
| 226 | if err := os.WriteFile(path, []byte(`{ |
| 227 | "projects_dir": "~/hack", |
| 228 | "projects": {"klaus": "~/hack/klaus"} |
| 229 | }`), 0o644); err != nil { |
| 230 | t.Fatalf("WriteFile: %v", err) |
| 231 | } |
| 232 | |
| 233 | reg, err := LoadFrom(path) |
| 234 | if err != nil { |
| 235 | t.Fatalf("LoadFrom: %v", err) |
| 236 | } |
| 237 | |
| 238 | home, err := os.UserHomeDir() |
| 239 | if err != nil { |
| 240 | t.Skip("cannot determine home dir") |
| 241 | } |
| 242 | |
| 243 | // Get should expand ~ |
| 244 | p, ok := reg.Get("klaus") |
| 245 | if !ok { |
| 246 | t.Fatal("Get returned false") |
| 247 | } |
| 248 | if p != filepath.Join(home, "hack/klaus") { |
| 249 | t.Errorf("Get = %q, want %q", p, filepath.Join(home, "hack/klaus")) |
| 250 | } |
| 251 | |
| 252 | // ExpandedProjectsDir should expand ~ |
| 253 | pd, err := reg.ExpandedProjectsDir() |
| 254 | if err != nil { |
| 255 | t.Fatalf("ExpandedProjectsDir: %v", err) |
| 256 | } |
| 257 | if pd != filepath.Join(home, "hack") { |
| 258 | t.Errorf("ExpandedProjectsDir = %q, want %q", pd, filepath.Join(home, "hack")) |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | func TestDescribeAndDescription(t *testing.T) { |
| 263 | reg := &Registry{ |
| 264 | ProjectsDir: "/tmp/projects", |
| 265 | Projects: map[string]string{"foo": "/tmp/foo"}, |
| 266 | } |
| 267 | |
| 268 | if got := reg.Description("foo"); got != "" { |
| 269 | t.Errorf("Description before set = %q, want empty", got) |
| 270 | } |
| 271 | |
| 272 | if err := reg.Describe("foo", "the foo tool"); err != nil { |
| 273 | t.Fatalf("Describe: %v", err) |
| 274 | } |
| 275 | if got := reg.Description("foo"); got != "the foo tool" { |
| 276 | t.Errorf("Description = %q, want %q", got, "the foo tool") |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | func TestDescribeUnregisteredProjectErrors(t *testing.T) { |
| 281 | reg := &Registry{ |
| 282 | ProjectsDir: "/tmp/projects", |
| 283 | Projects: make(map[string]string), |
| 284 | } |
| 285 | |
| 286 | if err := reg.Describe("nope", "anything"); err == nil { |
| 287 | t.Fatal("expected error for unregistered project, got nil") |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | func TestDescribeNormalizesWhitespace(t *testing.T) { |
| 292 | reg := &Registry{ |
| 293 | ProjectsDir: "/tmp/projects", |
| 294 | Projects: map[string]string{"foo": "/tmp/foo"}, |
| 295 | } |
| 296 | |
| 297 | if err := reg.Describe("foo", " the\tfoo\n tool "); err != nil { |
| 298 | t.Fatalf("Describe: %v", err) |
| 299 | } |
| 300 | if got := reg.Description("foo"); got != "the foo tool" { |
| 301 | t.Errorf("Description = %q, want %q", got, "the foo tool") |
| 302 | } |
| 303 | |
| 304 | // Whitespace-only input should be treated as empty and clear the entry. |
| 305 | if err := reg.Describe("foo", " \t\n "); err != nil { |
| 306 | t.Fatalf("Describe whitespace-only: %v", err) |
| 307 | } |
| 308 | if _, ok := reg.Descriptions["foo"]; ok { |
| 309 | t.Error("expected whitespace-only description to clear entry") |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | func TestDescribeEmptyClearsEntry(t *testing.T) { |
| 314 | reg := &Registry{ |
| 315 | ProjectsDir: "/tmp/projects", |
| 316 | Projects: map[string]string{"foo": "/tmp/foo"}, |
| 317 | Descriptions: map[string]string{"foo": "old"}, |
| 318 | } |
| 319 | |
| 320 | if err := reg.Describe("foo", ""); err != nil { |
| 321 | t.Fatalf("Describe with empty: %v", err) |
| 322 | } |
| 323 | if _, ok := reg.Descriptions["foo"]; ok { |
| 324 | t.Error("expected description entry to be deleted on empty input") |
| 325 | } |
| 326 | if got := reg.Description("foo"); got != "" { |
| 327 | t.Errorf("Description after clear = %q, want empty", got) |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | func TestRemoveCleansUpDescription(t *testing.T) { |
| 332 | reg := &Registry{ |
| 333 | ProjectsDir: "/tmp/projects", |
| 334 | Projects: map[string]string{"foo": "/tmp/foo"}, |
| 335 | Descriptions: map[string]string{"foo": "the foo tool"}, |
| 336 | } |
| 337 | |
| 338 | if err := reg.Remove("foo"); err != nil { |
| 339 | t.Fatalf("Remove: %v", err) |
| 340 | } |
| 341 | if _, ok := reg.Descriptions["foo"]; ok { |
| 342 | t.Error("Remove should also delete the description entry") |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | func TestDescriptionsRoundTrip(t *testing.T) { |
| 347 | dir := t.TempDir() |
| 348 | path := filepath.Join(dir, "projects.json") |
| 349 | |
| 350 | reg := &Registry{ |
| 351 | ProjectsDir: "/tmp/projects", |
| 352 | Projects: map[string]string{"foo": "/tmp/foo", "bar": "/tmp/bar"}, |
| 353 | Descriptions: map[string]string{"foo": "foo tool"}, |
| 354 | } |
| 355 | |
| 356 | if err := reg.SaveTo(path); err != nil { |
| 357 | t.Fatalf("SaveTo: %v", err) |
| 358 | } |
| 359 | |
| 360 | loaded, err := LoadFrom(path) |
| 361 | if err != nil { |
| 362 | t.Fatalf("LoadFrom: %v", err) |
| 363 | } |
| 364 | |
| 365 | if got := loaded.Description("foo"); got != "foo tool" { |
| 366 | t.Errorf("Description(foo) after round-trip = %q, want %q", got, "foo tool") |
| 367 | } |
| 368 | if got := loaded.Description("bar"); got != "" { |
| 369 | t.Errorf("Description(bar) after round-trip = %q, want empty", got) |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | func TestLoadLegacyJSONWithoutDescriptionsKey(t *testing.T) { |
| 374 | // Mirror the literal shape of an existing projects.json from before the |
| 375 | // descriptions field was added — no `descriptions` key at all. |
| 376 | dir := t.TempDir() |
| 377 | path := filepath.Join(dir, "projects.json") |
| 378 | |
| 379 | if err := os.WriteFile(path, []byte(`{ |
| 380 | "projects_dir": "~/hack", |
| 381 | "projects": {"klaus": "~/hack/klaus"} |
| 382 | }`), 0o644); err != nil { |
| 383 | t.Fatalf("WriteFile: %v", err) |
| 384 | } |
| 385 | |
| 386 | reg, err := LoadFrom(path) |
| 387 | if err != nil { |
| 388 | t.Fatalf("LoadFrom legacy file: %v", err) |
| 389 | } |
| 390 | |
| 391 | if reg.Descriptions == nil { |
| 392 | t.Error("Descriptions should be initialized to non-nil empty map for legacy files") |
| 393 | } |
| 394 | if got := reg.Description("klaus"); got != "" { |
| 395 | t.Errorf("Description(klaus) = %q, want empty for legacy file", got) |
| 396 | } |
| 397 | |
| 398 | // Setting a description on a legacy-loaded registry should still work. |
| 399 | if err := reg.Describe("klaus", "self-hosting orchestrator"); err != nil { |
| 400 | t.Fatalf("Describe after legacy load: %v", err) |
| 401 | } |
| 402 | if got := reg.Description("klaus"); got != "self-hosting orchestrator" { |
| 403 | t.Errorf("Description after Describe = %q", got) |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | func TestSaveOmitsDescriptionsKeyWhenEmpty(t *testing.T) { |
| 408 | // When no descriptions are set, the saved JSON should not include a |
| 409 | // `descriptions` key (omitempty), keeping diffs clean for users who |
| 410 | // don't use this feature. |
| 411 | dir := t.TempDir() |
| 412 | path := filepath.Join(dir, "projects.json") |
| 413 | |
| 414 | reg := &Registry{ |
| 415 | ProjectsDir: "/tmp/projects", |
| 416 | Projects: map[string]string{"foo": "/tmp/foo"}, |
| 417 | } |
| 418 | if err := reg.SaveTo(path); err != nil { |
| 419 | t.Fatalf("SaveTo: %v", err) |
| 420 | } |
| 421 | |
| 422 | data, err := os.ReadFile(path) |
| 423 | if err != nil { |
| 424 | t.Fatalf("ReadFile: %v", err) |
| 425 | } |
| 426 | if contains(string(data), "descriptions") { |
| 427 | t.Errorf("saved file should not contain `descriptions` when none set:\n%s", data) |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | func contains(s, substr string) bool { |
| 432 | return len(s) >= len(substr) && searchString(s, substr) |
| 433 | } |
| 434 | |
| 435 | func searchString(s, sub string) bool { |
| 436 | for i := 0; i <= len(s)-len(sub); i++ { |
| 437 | if s[i:i+len(sub)] == sub { |
| 438 | return true |
| 439 | } |
| 440 | } |
| 441 | return false |
| 442 | } |