package thin import ( "context" "errors" "os" "path/filepath" "testing" "time" "amadan.net/oficina/equipo/internal/runner/workspace" "amadan.net/oficina/equipo/internal/wire" ) // refusingForge is the hub refusing a burst of fetches: the first refuse // remotes fail, then it serves the repository at url. type refusingForge struct { Forge url string refuse, calls int } func (f *refusingForge) Remote(context.Context, string, Use) (workspace.Remote, error) { f.calls++ if f.calls <= f.refuse { return workspace.Remote{}, errors.New("hub: too many requests") } return workspace.Remote{URL: f.url}, nil } // Staging, 2026-11-07 19:11: a fix round's Checks step started a second // after Engineering pushed its new candidate and stopped on attempt 1 // with clone_failed ("something went wrong"). The runner fetched the // project once and gave up when the hub refused that fetch, which it did // while both runners re-indexed after an upgrade. A Checks step // dispatched straight after a push must still find and clone the // candidate: it fetches again, with backoff, and checks out that exact // commit. func TestChecksCloneFreshCandidateThroughRefusal(t *testing.T) { old := fetchBackoff fetchBackoff = 10 * time.Millisecond t.Cleanup(func() { fetchBackoff = old }) root := t.TempDir() work := filepath.Join(root, "work") gitT(t, root, "init", "-q", "-b", "main", work) if err := os.WriteFile(filepath.Join(work, "README.md"), []byte("Woodstar\n"), 0o644); err != nil { t.Fatal(err) } gitT(t, work, "add", "-A") gitT(t, work, "commit", "-qm", "base") base := gitT(t, work, "rev-parse", "HEAD") gitT(t, work, "checkout", "-q", "-b", "equipo/tk1-swipe") if err := os.MkdirAll(filepath.Join(work, "ios"), 0o755); err != nil { t.Fatal(err) } if err := os.WriteFile(filepath.Join(work, "ios", "AccountNavigationHost.swift"), []byte("// the fix\n"), 0o644); err != nil { t.Fatal(err) } gitT(t, work, "add", "-A") gitT(t, work, "commit", "-qm", "fix round") cand := gitT(t, work, "rev-parse", "HEAD") hub := filepath.Join(root, "hub.git") gitT(t, root, "clone", "-q", "--bare", work, hub) f := &refusingForge{url: hub, refuse: 2} ws := filepath.Join(root, "ws") e := &Engineer{Clones: &Clones{Root: ws, Git: "/usr/bin/git", Forge: f}, Owners: []string{"p@x"}} e.O.Runner.GitPath, e.O.Runner.WorkspaceRoot, e.O.Runner.StateDir = "/usr/bin/git", ws, ws r := stepRow{DispatchCommand: wire.DispatchCommand{Person: "p@x", TaskID: "tk1", StepID: "s5", Attempt: 1, Specialist: wire.SpecialistChecks, Repo: "amadan.net/oficina/sample", Branch: "equipo/tk1-swipe", BaseSHA: base, CandidateSHA: cand}} tree, reason, err := e.judgeTree(context.Background(), r) if reason != "" || err != nil { t.Fatalf("checks could not clone the fresh candidate: %q %v (fetches tried: %d)", reason, err, f.calls) } t.Cleanup(func() { os.RemoveAll(tree.Dir); os.RemoveAll(tree.GitDir) }) if f.calls != 3 { t.Fatalf("fetches: %d, want two refused and one that worked", f.calls) } if raw, err := os.ReadFile(filepath.Join(tree.Dir, "ios", "AccountNavigationHost.swift")); err != nil || string(raw) != "// the fix\n" { t.Fatalf("the tree is not the candidate: %q %v", raw, err) } if head := gitT(t, tree.Dir, "--git-dir="+tree.GitDir, "rev-parse", "HEAD"); head != cand { t.Fatalf("checked out %s, want the candidate %s", head, cand) } }