|
| 1 | +package registry |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | +) |
| 7 | + |
| 8 | +// newTestRegistry creates a Registry with entries populated in-memory only (no disk I/O). |
| 9 | +func newTestRegistry(entries ...*Entry) *Registry { |
| 10 | + r := &Registry{ |
| 11 | + entries: make(map[string]*Entry), |
| 12 | + candidates: make(map[string]*CandidateEntry), |
| 13 | + indexRoot: make(map[string]string), |
| 14 | + indexName: make(map[string][]string), |
| 15 | + clock: time.Now, |
| 16 | + audit: noopAuditSink{}, |
| 17 | + } |
| 18 | + for _, e := range entries { |
| 19 | + r.addEntry(e) |
| 20 | + } |
| 21 | + return r |
| 22 | +} |
| 23 | + |
| 24 | +func TestFindParentWorkspace(t *testing.T) { |
| 25 | + r := newTestRegistry(&Entry{ |
| 26 | + ID: hashRoot("/home/user/projects/big-project"), |
| 27 | + Root: "/home/user/projects/big-project", |
| 28 | + Name: "big-project", |
| 29 | + }) |
| 30 | + |
| 31 | + tests := []struct { |
| 32 | + name string |
| 33 | + path string |
| 34 | + wantRoot string |
| 35 | + wantFound bool |
| 36 | + }{ |
| 37 | + { |
| 38 | + name: "child directory matches parent", |
| 39 | + path: "/home/user/projects/big-project/submodule/graph", |
| 40 | + wantRoot: "/home/user/projects/big-project", |
| 41 | + wantFound: true, |
| 42 | + }, |
| 43 | + { |
| 44 | + name: "deep nested child matches", |
| 45 | + path: "/home/user/projects/big-project/a/b/c/d", |
| 46 | + wantRoot: "/home/user/projects/big-project", |
| 47 | + wantFound: true, |
| 48 | + }, |
| 49 | + { |
| 50 | + name: "same path does not match (not strictly inside)", |
| 51 | + path: "/home/user/projects/big-project", |
| 52 | + wantRoot: "", |
| 53 | + wantFound: false, |
| 54 | + }, |
| 55 | + { |
| 56 | + name: "sibling directory does not match", |
| 57 | + path: "/home/user/projects/other-project", |
| 58 | + wantRoot: "", |
| 59 | + wantFound: false, |
| 60 | + }, |
| 61 | + { |
| 62 | + name: "parent directory does not match", |
| 63 | + path: "/home/user/projects", |
| 64 | + wantRoot: "", |
| 65 | + wantFound: false, |
| 66 | + }, |
| 67 | + { |
| 68 | + name: "similar prefix but different dir does not match", |
| 69 | + path: "/home/user/projects/big-project-v2/src", |
| 70 | + wantRoot: "", |
| 71 | + wantFound: false, |
| 72 | + }, |
| 73 | + { |
| 74 | + name: "empty path returns not found", |
| 75 | + path: "", |
| 76 | + wantRoot: "", |
| 77 | + wantFound: false, |
| 78 | + }, |
| 79 | + } |
| 80 | + |
| 81 | + for _, tt := range tests { |
| 82 | + t.Run(tt.name, func(t *testing.T) { |
| 83 | + root, found := r.FindParentWorkspace(tt.path) |
| 84 | + if found != tt.wantFound { |
| 85 | + t.Errorf("FindParentWorkspace(%q) found=%v, want %v", tt.path, found, tt.wantFound) |
| 86 | + } |
| 87 | + if root != tt.wantRoot { |
| 88 | + t.Errorf("FindParentWorkspace(%q) root=%q, want %q", tt.path, root, tt.wantRoot) |
| 89 | + } |
| 90 | + }) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +func TestFindParentWorkspacePicksDeepest(t *testing.T) { |
| 95 | + r := newTestRegistry( |
| 96 | + &Entry{ |
| 97 | + ID: hashRoot("/home/user/projects"), |
| 98 | + Root: "/home/user/projects", |
| 99 | + Name: "projects", |
| 100 | + }, |
| 101 | + &Entry{ |
| 102 | + ID: hashRoot("/home/user/projects/monorepo"), |
| 103 | + Root: "/home/user/projects/monorepo", |
| 104 | + Name: "monorepo", |
| 105 | + }, |
| 106 | + ) |
| 107 | + |
| 108 | + // A path inside monorepo should match monorepo (the deepest parent), not projects |
| 109 | + root, found := r.FindParentWorkspace("/home/user/projects/monorepo/packages/core") |
| 110 | + if !found { |
| 111 | + t.Fatal("expected to find parent workspace") |
| 112 | + } |
| 113 | + if root != "/home/user/projects/monorepo" { |
| 114 | + t.Fatalf("expected deepest parent /home/user/projects/monorepo, got %s", root) |
| 115 | + } |
| 116 | + |
| 117 | + // A path inside projects but outside monorepo should match projects |
| 118 | + root, found = r.FindParentWorkspace("/home/user/projects/other-app/src") |
| 119 | + if !found { |
| 120 | + t.Fatal("expected to find parent workspace") |
| 121 | + } |
| 122 | + if root != "/home/user/projects" { |
| 123 | + t.Fatalf("expected parent /home/user/projects, got %s", root) |
| 124 | + } |
| 125 | +} |
0 commit comments