| 1 | package thin |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "testing" |
| 9 | "time" |
| 10 | |
| 11 | "amadan.net/oficina/equipo/internal/runner/workspace" |
| 12 | "amadan.net/oficina/equipo/internal/wire" |
| 13 | ) |
| 14 | |
| 15 | // refusingForge is the hub refusing a burst of fetches: the first refuse |
| 16 | // remotes fail, then it serves the repository at url. |
| 17 | type refusingForge struct { |
| 18 | Forge |
| 19 | url string |
| 20 | refuse, calls int |
| 21 | } |
| 22 | |
| 23 | func (f *refusingForge) Remote(context.Context, string, Use) (workspace.Remote, error) { |
| 24 | f.calls++ |
| 25 | if f.calls <= f.refuse { |
| 26 | return workspace.Remote{}, errors.New("hub: too many requests") |
| 27 | } |
| 28 | return workspace.Remote{URL: f.url}, nil |
| 29 | } |
| 30 | |
| 31 | // Staging, 2026-11-07 19:11: a fix round's Checks step started a second |
| 32 | // after Engineering pushed its new candidate and stopped on attempt 1 |
| 33 | // with clone_failed ("something went wrong"). The runner fetched the |
| 34 | // project once and gave up when the hub refused that fetch, which it did |
| 35 | // while both runners re-indexed after an upgrade. A Checks step |
| 36 | // dispatched straight after a push must still find and clone the |
| 37 | // candidate: it fetches again, with backoff, and checks out that exact |
| 38 | // commit. |
| 39 | func TestChecksCloneFreshCandidateThroughRefusal(t *testing.T) { |
| 40 | old := fetchBackoff |
| 41 | fetchBackoff = 10 * time.Millisecond |
| 42 | t.Cleanup(func() { fetchBackoff = old }) |
| 43 | root := t.TempDir() |
| 44 | work := filepath.Join(root, "work") |
| 45 | gitT(t, root, "init", "-q", "-b", "main", work) |
| 46 | if err := os.WriteFile(filepath.Join(work, "README.md"), []byte("Woodstar\n"), 0o644); err != nil { |
| 47 | t.Fatal(err) |
| 48 | } |
| 49 | gitT(t, work, "add", "-A") |
| 50 | gitT(t, work, "commit", "-qm", "base") |
| 51 | base := gitT(t, work, "rev-parse", "HEAD") |
| 52 | gitT(t, work, "checkout", "-q", "-b", "equipo/tk1-swipe") |
| 53 | if err := os.MkdirAll(filepath.Join(work, "ios"), 0o755); err != nil { |
| 54 | t.Fatal(err) |
| 55 | } |
| 56 | if err := os.WriteFile(filepath.Join(work, "ios", "AccountNavigationHost.swift"), []byte("// the fix\n"), 0o644); err != nil { |
| 57 | t.Fatal(err) |
| 58 | } |
| 59 | gitT(t, work, "add", "-A") |
| 60 | gitT(t, work, "commit", "-qm", "fix round") |
| 61 | cand := gitT(t, work, "rev-parse", "HEAD") |
| 62 | hub := filepath.Join(root, "hub.git") |
| 63 | gitT(t, root, "clone", "-q", "--bare", work, hub) |
| 64 | |
| 65 | f := &refusingForge{url: hub, refuse: 2} |
| 66 | ws := filepath.Join(root, "ws") |
| 67 | e := &Engineer{Clones: &Clones{Root: ws, Git: "/usr/bin/git", Forge: f}, Owners: []string{"p@x"}} |
| 68 | e.O.Runner.GitPath, e.O.Runner.WorkspaceRoot, e.O.Runner.StateDir = "/usr/bin/git", ws, ws |
| 69 | 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}} |
| 70 | tree, reason, err := e.judgeTree(context.Background(), r) |
| 71 | if reason != "" || err != nil { |
| 72 | t.Fatalf("checks could not clone the fresh candidate: %q %v (fetches tried: %d)", reason, err, f.calls) |
| 73 | } |
| 74 | t.Cleanup(func() { os.RemoveAll(tree.Dir); os.RemoveAll(tree.GitDir) }) |
| 75 | if f.calls != 3 { |
| 76 | t.Fatalf("fetches: %d, want two refused and one that worked", f.calls) |
| 77 | } |
| 78 | if raw, err := os.ReadFile(filepath.Join(tree.Dir, "ios", "AccountNavigationHost.swift")); err != nil || string(raw) != "// the fix\n" { |
| 79 | t.Fatalf("the tree is not the candidate: %q %v", raw, err) |
| 80 | } |
| 81 | if head := gitT(t, tree.Dir, "--git-dir="+tree.GitDir, "rev-parse", "HEAD"); head != cand { |
| 82 | t.Fatalf("checked out %s, want the candidate %s", head, cand) |
| 83 | } |
| 84 | } |
| 85 | |