package thin import ( "amadan.net/oficina/equipo/internal/runner/workspace" "amadan.net/oficina/equipo/internal/wire" "context" "errors" "path/filepath" "reflect" "strings" "testing" ) type proposalSpy struct { Forge calls []Proposal failure error at func(Proposal) } func (f *proposalSpy) Propose(ctx context.Context, p Proposal) (ProposalRef, error) { f.calls = append(f.calls, p) if f.at != nil { f.at(p) } if f.failure != nil { return ProposalRef{}, f.failure } return f.Forge.Propose(ctx, p) } func TestEngineeringProposesAfterPush(t *testing.T) { e, f, r, _ := hubRig(t) r.Specialist, r.StepID, r.Branch = wire.SpecialistEngineering, "s2", "equipo/tk1-another-fix" base := gitOut(t, f.dir, "rev-parse", "main") gitDir := e.tree(r, base).GitDir gitOut(t, filepath.Dir(f.dir), "clone", "-q", "--bare", f.dir, gitDir) tree := gitOut(t, filepath.Dir(f.dir), "--git-dir="+gitDir, "rev-parse", base+"^{tree}") r.Base, r.Candidate = base, gitOut(t, filepath.Dir(f.dir), "--git-dir="+gitDir, "commit-tree", tree, "-p", base, "-m", "another fix") spy := &proposalSpy{Forge: f, at: func(p Proposal) { if p.Repo != r.Repo || p.Branch != r.Branch || gitOut(t, f.dir, "rev-parse", p.Branch) != r.Candidate { t.Fatal("proposal before push or wrong branch") } }} authority := &useSpy{Forge: spy} e.Forge = authority rep := e.publish(context.Background(), r, wire.StepReport{}) if !reflect.DeepEqual(authority.uses, []Use{Push}) { t.Fatalf("publish authority %v", authority.uses) } if !rep.Pushed || len(spy.calls) != 1 { t.Fatalf("report %+v proposals %+v", rep, spy.calls) } } func TestRelayProposesAfterPush(t *testing.T) { r := newRelayRig(t) spy := &proposalSpy{Forge: r.f, at: func(p Proposal) { if p.Repo != r.header().Repo || p.Branch != relayBranch || r.head(t) != r.commit { t.Fatal("proposal before push or wrong branch") } }} authority := &useSpy{Forge: spy} r.e.Forge = authority r.receive(t, r.pack, r.header()) r.publish(t, "pb_1") r.relay.Once(context.Background()) if !reflect.DeepEqual(authority.uses, []Use{Push}) { t.Fatalf("relay authority %v", authority.uses) } if len(spy.calls) != 1 { t.Fatalf("proposals %+v", spy.calls) } } // Repository initialization is also a write, even before its first task. type useSpy struct { Forge uses []Use } func (f *useSpy) Remote(ctx context.Context, repo string, use Use) (workspace.Remote, error) { f.uses = append(f.uses, use) return f.Forge.Remote(ctx, repo, use) } func TestStartDefaultRequestsPushAuthority(t *testing.T) { f, c := startRig(t) const repo = "amadan.net/oficina/site" if err := f.Create(context.Background(), repo); err != nil { t.Fatal(err) } spy := &useSpy{Forge: f} c.Forge = spy if err := c.StartDefault(context.Background(), repo); err != nil { t.Fatal(err) } if len(spy.uses) != 1 || spy.uses[0] != Push { t.Fatalf("initialization authority %v", spy.uses) } } func TestEngineeringProposalFailureKeepsPush(t *testing.T) { e, f, r, _ := hubRig(t) r.Specialist, r.StepID, r.Branch = wire.SpecialistEngineering, "s2", "equipo/tk1-another-fix" base := gitOut(t, f.dir, "rev-parse", "main") gitDir := e.tree(r, base).GitDir gitOut(t, filepath.Dir(f.dir), "clone", "-q", "--bare", f.dir, gitDir) tree := gitOut(t, filepath.Dir(f.dir), "--git-dir="+gitDir, "rev-parse", base+"^{tree}") r.Base, r.Candidate = base, gitOut(t, filepath.Dir(f.dir), "--git-dir="+gitDir, "commit-tree", tree, "-p", base, "-m", "another fix") spy := &proposalSpy{Forge: f, failure: errors.New("private raw forge diagnostic"), at: func(p Proposal) { if p.Repo != r.Repo || p.Branch != r.Branch || gitOut(t, f.dir, "rev-parse", p.Branch) != r.Candidate { t.Fatal("proposal before push or wrong branch") } }} authority := &useSpy{Forge: spy} e.Forge = authority rep := e.publish(context.Background(), r, wire.StepReport{}) if !reflect.DeepEqual(authority.uses, []Use{Push}) { t.Fatalf("publish authority %v", authority.uses) } if !rep.Pushed || rep.State != wire.StepDone || rep.Reason != "proposal_failed" || len(spy.calls) != 1 || strings.Contains(rep.LogTail, "private raw") { t.Fatalf("report %+v proposals %+v", rep, spy.calls) } } func TestRelayProposalFailureKeepsPush(t *testing.T) { r := newRelayRig(t) spy := &proposalSpy{Forge: r.f, failure: errors.New("private raw forge diagnostic"), at: func(p Proposal) { if p.Repo != r.header().Repo || p.Branch != relayBranch || r.head(t) != r.commit { t.Fatal("proposal before push or wrong branch") } }} authority := &useSpy{Forge: spy} r.e.Forge = authority r.receive(t, r.pack, r.header()) r.publish(t, "pb_1") r.relay.Once(context.Background()) if !reflect.DeepEqual(authority.uses, []Use{Push}) { t.Fatalf("relay authority %v", authority.uses) } r.relay.Once(context.Background()) reps := r.reports(t) if len(reps) != 1 || !reps[0].Pushed || reps[0].Reason != "proposal_failed" || r.pushCount() != 1 { t.Fatalf("publication: %+v pushes %d", reps, r.pushCount()) } if len(spy.calls) != 1 { t.Fatalf("proposals %+v", spy.calls) } }