| 1 | package projectsync |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "os" |
| 7 | "os/exec" |
| 8 | "path/filepath" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | |
| 12 | "the-valley/flow/internal/git" |
| 13 | "the-valley/flow/internal/project" |
| 14 | ) |
| 15 | |
| 16 | // runGit runs a git command from dir and fails the test on error. |
| 17 | func runGit(t *testing.T, dir string, args ...string) string { |
| 18 | t.Helper() |
| 19 | cmd := exec.Command("git", append([]string{"-C", dir}, args...)...) |
| 20 | out, err := cmd.CombinedOutput() |
| 21 | if err != nil { |
| 22 | t.Fatalf("git %v in %s: %v\n%s", args, dir, err, out) |
| 23 | } |
| 24 | return strings.TrimSpace(string(out)) |
| 25 | } |
| 26 | |
| 27 | // makeUpstream creates a bare origin repo with one initial commit and returns |
| 28 | // its path. |
| 29 | func makeUpstream(t *testing.T) string { |
| 30 | t.Helper() |
| 31 | seed := t.TempDir() |
| 32 | runGit(t, ".", "init", "--initial-branch=main", seed) |
| 33 | runGit(t, seed, "config", "user.email", "test@test.com") |
| 34 | runGit(t, seed, "config", "user.name", "Test") |
| 35 | if err := os.WriteFile(filepath.Join(seed, "README.md"), []byte("# seed\n"), 0o644); err != nil { |
| 36 | t.Fatal(err) |
| 37 | } |
| 38 | runGit(t, seed, "add", "README.md") |
| 39 | runGit(t, seed, "commit", "-m", "initial") |
| 40 | |
| 41 | bare := filepath.Join(t.TempDir(), "origin.git") |
| 42 | if out, err := exec.Command("git", "clone", "--bare", seed, bare).CombinedOutput(); err != nil { |
| 43 | t.Fatalf("bare clone: %v\n%s", err, out) |
| 44 | } |
| 45 | return bare |
| 46 | } |
| 47 | |
| 48 | // cloneFrom clones bare into a fresh directory and configures git user, returning the path. |
| 49 | func cloneFrom(t *testing.T, bare string) string { |
| 50 | t.Helper() |
| 51 | dir := filepath.Join(t.TempDir(), "clone") |
| 52 | if out, err := exec.Command("git", "clone", bare, dir).CombinedOutput(); err != nil { |
| 53 | t.Fatalf("clone: %v\n%s", err, out) |
| 54 | } |
| 55 | runGit(t, dir, "config", "user.email", "test@test.com") |
| 56 | runGit(t, dir, "config", "user.name", "Test") |
| 57 | return dir |
| 58 | } |
| 59 | |
| 60 | // advanceOrigin pushes one new commit to the bare origin repo by doing the |
| 61 | // commit in a temporary staging clone. |
| 62 | func advanceOrigin(t *testing.T, bare string) { |
| 63 | t.Helper() |
| 64 | staging := cloneFrom(t, bare) |
| 65 | fname := filepath.Join(staging, "advance.txt") |
| 66 | if err := os.WriteFile(fname, []byte("advance"), 0o644); err != nil { |
| 67 | t.Fatal(err) |
| 68 | } |
| 69 | runGit(t, staging, "add", "advance.txt") |
| 70 | runGit(t, staging, "commit", "-m", "advance origin") |
| 71 | runGit(t, staging, "push", "origin", "main") |
| 72 | } |
| 73 | |
| 74 | func makeRegistry(t *testing.T, projects map[string]string) *project.Registry { |
| 75 | t.Helper() |
| 76 | path := filepath.Join(t.TempDir(), "projects.json") |
| 77 | reg := &project.Registry{ |
| 78 | ProjectsDir: t.TempDir(), |
| 79 | Projects: projects, |
| 80 | } |
| 81 | if err := reg.SaveTo(path); err != nil { |
| 82 | t.Fatal(err) |
| 83 | } |
| 84 | loaded, err := project.LoadFrom(path) |
| 85 | if err != nil { |
| 86 | t.Fatal(err) |
| 87 | } |
| 88 | return loaded |
| 89 | } |
| 90 | |
| 91 | func findResult(results []SyncResult, name string) *SyncResult { |
| 92 | for i := range results { |
| 93 | if results[i].Name == name { |
| 94 | return &results[i] |
| 95 | } |
| 96 | } |
| 97 | return nil |
| 98 | } |
| 99 | |
| 100 | func TestSync_CleanAndBehindGetsPulled(t *testing.T) { |
| 101 | bare := makeUpstream(t) |
| 102 | clone := cloneFrom(t, bare) |
| 103 | advanceOrigin(t, bare) |
| 104 | |
| 105 | reg := makeRegistry(t, map[string]string{"demo": clone}) |
| 106 | results := Sync(context.Background(), reg, git.NewExecClient()) |
| 107 | |
| 108 | r := findResult(results, "demo") |
| 109 | if r == nil { |
| 110 | t.Fatalf("no result for demo; got %+v", results) |
| 111 | } |
| 112 | if r.Status != StatusPulled { |
| 113 | t.Errorf("Status = %q, want %q (detail=%q)", r.Status, StatusPulled, r.Detail) |
| 114 | } |
| 115 | if r.Branch != "main" { |
| 116 | t.Errorf("Branch = %q, want main", r.Branch) |
| 117 | } |
| 118 | if _, err := os.Stat(filepath.Join(clone, "advance.txt")); err != nil { |
| 119 | t.Errorf("advance.txt should exist after fast-forward: %v", err) |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | func TestSync_AlreadyUpToDate(t *testing.T) { |
| 124 | bare := makeUpstream(t) |
| 125 | clone := cloneFrom(t, bare) |
| 126 | |
| 127 | reg := makeRegistry(t, map[string]string{"demo": clone}) |
| 128 | results := Sync(context.Background(), reg, git.NewExecClient()) |
| 129 | r := findResult(results, "demo") |
| 130 | if r == nil || r.Status != StatusUpToDate { |
| 131 | t.Errorf("expected up-to-date, got %+v", r) |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | func TestSync_DirtyTreeIsSkipped(t *testing.T) { |
| 136 | bare := makeUpstream(t) |
| 137 | clone := cloneFrom(t, bare) |
| 138 | advanceOrigin(t, bare) |
| 139 | |
| 140 | // Dirty the working tree โ untracked file is enough |
| 141 | if err := os.WriteFile(filepath.Join(clone, "local.txt"), []byte("wip"), 0o644); err != nil { |
| 142 | t.Fatal(err) |
| 143 | } |
| 144 | |
| 145 | reg := makeRegistry(t, map[string]string{"demo": clone}) |
| 146 | results := Sync(context.Background(), reg, git.NewExecClient()) |
| 147 | r := findResult(results, "demo") |
| 148 | if r == nil || r.Status != StatusSkipped { |
| 149 | t.Fatalf("expected skipped, got %+v", r) |
| 150 | } |
| 151 | if !strings.Contains(r.Detail, "dirty") { |
| 152 | t.Errorf("Detail should mention dirty, got %q", r.Detail) |
| 153 | } |
| 154 | // Must NOT have fast-forwarded |
| 155 | if _, err := os.Stat(filepath.Join(clone, "advance.txt")); !os.IsNotExist(err) { |
| 156 | t.Error("sync should not touch a dirty clone") |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | func TestSync_DivergedIsSkipped(t *testing.T) { |
| 161 | bare := makeUpstream(t) |
| 162 | clone := cloneFrom(t, bare) |
| 163 | advanceOrigin(t, bare) |
| 164 | |
| 165 | // Commit a different change locally so we diverge from origin/main. |
| 166 | if err := os.WriteFile(filepath.Join(clone, "local.txt"), []byte("local"), 0o644); err != nil { |
| 167 | t.Fatal(err) |
| 168 | } |
| 169 | runGit(t, clone, "add", "local.txt") |
| 170 | runGit(t, clone, "commit", "-m", "local change") |
| 171 | |
| 172 | reg := makeRegistry(t, map[string]string{"demo": clone}) |
| 173 | results := Sync(context.Background(), reg, git.NewExecClient()) |
| 174 | r := findResult(results, "demo") |
| 175 | if r == nil { |
| 176 | t.Fatalf("no result") |
| 177 | } |
| 178 | if r.Status != StatusSkipped { |
| 179 | t.Errorf("Status = %q, want skipped (detail=%q)", r.Status, r.Detail) |
| 180 | } |
| 181 | if !strings.Contains(r.Detail, "fast-forward") { |
| 182 | t.Errorf("Detail should mention fast-forward, got %q", r.Detail) |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | func TestSync_NoUpstreamIsSkipped(t *testing.T) { |
| 187 | bare := makeUpstream(t) |
| 188 | clone := cloneFrom(t, bare) |
| 189 | |
| 190 | // Check out a new branch with no upstream |
| 191 | runGit(t, clone, "checkout", "-b", "feature") |
| 192 | |
| 193 | reg := makeRegistry(t, map[string]string{"demo": clone}) |
| 194 | results := Sync(context.Background(), reg, git.NewExecClient()) |
| 195 | r := findResult(results, "demo") |
| 196 | if r == nil || r.Status != StatusSkipped { |
| 197 | t.Fatalf("expected skipped, got %+v", r) |
| 198 | } |
| 199 | if !strings.Contains(r.Detail, "upstream") { |
| 200 | t.Errorf("Detail should mention upstream, got %q", r.Detail) |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | func TestSync_DetachedHeadIsSkipped(t *testing.T) { |
| 205 | bare := makeUpstream(t) |
| 206 | clone := cloneFrom(t, bare) |
| 207 | runGit(t, clone, "checkout", "--detach") |
| 208 | |
| 209 | reg := makeRegistry(t, map[string]string{"demo": clone}) |
| 210 | results := Sync(context.Background(), reg, git.NewExecClient()) |
| 211 | r := findResult(results, "demo") |
| 212 | if r == nil || r.Status != StatusSkipped { |
| 213 | t.Fatalf("expected skipped, got %+v", r) |
| 214 | } |
| 215 | if !strings.Contains(r.Detail, "detached") { |
| 216 | t.Errorf("Detail should mention detached, got %q", r.Detail) |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | func TestSync_FetchErrorReported(t *testing.T) { |
| 221 | // Point the clone at a non-existent origin so fetch fails. |
| 222 | bare := makeUpstream(t) |
| 223 | clone := cloneFrom(t, bare) |
| 224 | bad := filepath.Join(t.TempDir(), "nonexistent.git") |
| 225 | runGit(t, clone, "remote", "set-url", "origin", bad) |
| 226 | |
| 227 | reg := makeRegistry(t, map[string]string{"demo": clone}) |
| 228 | results := Sync(context.Background(), reg, git.NewExecClient()) |
| 229 | r := findResult(results, "demo") |
| 230 | if r == nil || r.Status != StatusError { |
| 231 | t.Fatalf("expected error, got %+v", r) |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | func TestSync_ExcludePathSkipsMatchingProject(t *testing.T) { |
| 236 | bare := makeUpstream(t) |
| 237 | a := cloneFrom(t, bare) |
| 238 | b := cloneFrom(t, bare) |
| 239 | advanceOrigin(t, bare) |
| 240 | |
| 241 | // Exclude `a` โ it should not appear in results at all, and must not be |
| 242 | // fast-forwarded. `b` should still be synced normally. |
| 243 | reg := makeRegistry(t, map[string]string{"alpha": a, "bravo": b}) |
| 244 | results := Sync(context.Background(), reg, git.NewExecClient(), a) |
| 245 | |
| 246 | if findResult(results, "alpha") != nil { |
| 247 | t.Errorf("alpha should have been excluded, got %+v", results) |
| 248 | } |
| 249 | if r := findResult(results, "bravo"); r == nil || r.Status != StatusPulled { |
| 250 | t.Errorf("bravo should be pulled, got %+v", r) |
| 251 | } |
| 252 | if _, err := os.Stat(filepath.Join(a, "advance.txt")); !os.IsNotExist(err) { |
| 253 | t.Errorf("excluded clone %q must not be fast-forwarded", a) |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | func TestSync_BoundedConcurrency(t *testing.T) { |
| 258 | // Register more than MaxConcurrency projects. All should complete despite |
| 259 | // the cap. This is a smoke test โ the main guarantee we care about is that |
| 260 | // bounded concurrency doesn't drop any project from the results. |
| 261 | bare := makeUpstream(t) |
| 262 | projects := make(map[string]string, MaxConcurrency+2) |
| 263 | for i := 0; i < MaxConcurrency+2; i++ { |
| 264 | projects[fmt.Sprintf("p%02d", i)] = cloneFrom(t, bare) |
| 265 | } |
| 266 | reg := makeRegistry(t, projects) |
| 267 | results := Sync(context.Background(), reg, git.NewExecClient()) |
| 268 | if len(results) != len(projects) { |
| 269 | t.Fatalf("got %d results, want %d", len(results), len(projects)) |
| 270 | } |
| 271 | for _, r := range results { |
| 272 | if r.Status != StatusUpToDate { |
| 273 | t.Errorf("%s: want up-to-date, got %q (%s)", r.Name, r.Status, r.Detail) |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | func TestSync_SortsByName(t *testing.T) { |
| 279 | bare := makeUpstream(t) |
| 280 | a := cloneFrom(t, bare) |
| 281 | b := cloneFrom(t, bare) |
| 282 | c := cloneFrom(t, bare) |
| 283 | |
| 284 | reg := makeRegistry(t, map[string]string{ |
| 285 | "zebra": a, |
| 286 | "alpha": b, |
| 287 | "mango": c, |
| 288 | }) |
| 289 | results := Sync(context.Background(), reg, git.NewExecClient()) |
| 290 | if len(results) != 3 { |
| 291 | t.Fatalf("got %d results, want 3", len(results)) |
| 292 | } |
| 293 | want := []string{"alpha", "mango", "zebra"} |
| 294 | for i, w := range want { |
| 295 | if results[i].Name != w { |
| 296 | t.Errorf("results[%d].Name = %q, want %q", i, results[i].Name, w) |
| 297 | } |
| 298 | } |
| 299 | } |