package workflow import ( "context" "log/slog" "os/exec" "strings" "testing" "time" "github.com/samcharles93/archie-core/internal/config" "github.com/samcharles93/archie-core/internal/domain/agentrun" "github.com/samcharles93/archie-core/internal/domain/workflow/task" ) type countingRunner struct{ runs int } func (r *countingRunner) Run(context.Context, string, agentrun.Request, agentrun.Reporter) (agentrun.Result, error) { r.runs++ return agentrun.Result{Version: agentrun.ProtocolVersion, Status: "failed"}, nil } // scriptedLedger answers claims from a fixed list, then repeats the last one. type scriptedLedger struct { claims []BaselineClaim finished []string } func (l *scriptedLedger) ClaimBaselineFix(context.Context, string) (BaselineClaim, error) { claim := l.claims[0] if len(l.claims) > 1 { l.claims = l.claims[1:] } return claim, nil } func (l *scriptedLedger) FinishBaselineFix(_ context.Context, _, failure string) error { l.finished = append(l.finished, failure) return nil } func TestRedBaselineIsRepairedByOneTask(t *testing.T) { tests := []struct { name string claims []BaselineClaim wantRuns int wantFinished int wantInErr string }{ { name: "the task that leads repairs it", claims: []BaselineClaim{{Lead: true}}, wantRuns: 1, wantFinished: 1, wantInErr: "did not repair it", }, { name: "a task waits on the lead, then reports its failure", claims: []BaselineClaim{{Task: 7}, {Task: 7}, {Task: 7, Done: true, Failure: "builder status failed"}}, wantInErr: "task 7 could not repair it: builder status failed", }, { name: "a task reuses a repair made on another branch", claims: []BaselineClaim{{Task: 7, Done: true}}, wantInErr: "task 7 repaired it in a pull request", }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { dir := t.TempDir() for _, args := range [][]string{{"init", "-q"}, {"-c", "user.name=t", "-c", "user.email=t@t", "commit", "-q", "--allow-empty", "-m", "base"}} { if out, err := exec.CommandContext(t.Context(), "git", append([]string{"-C", dir}, args...)...).CombinedOutput(); err != nil { t.Fatalf("git %v: %v\n%s", args, err, out) } } agent := &countingRunner{} ledger := &scriptedLedger{claims: tt.claims} tc := &TaskContext{ Task: &task.Task{ID: 1, Attempt: 1, Source: task.SourceTest}, Repo: config.Repo{Owner: "acme", Name: "widgets"}, Cfg: config.Config{Models: map[string]string{"default": "m"}}, Dir: dir, Log: slog.Default(), Agent: agent, Baseline: ledger, baselinePoll: time.Millisecond, } err := repairBaselineOnce(context.Background(), tc, []string{"task", "check"}, []byte("FAIL")) if err == nil || !strings.Contains(err.Error(), tt.wantInErr) { t.Fatalf("err = %v, want it to contain %q", err, tt.wantInErr) } if agent.runs != tt.wantRuns { t.Errorf("fix agents run = %d, want %d", agent.runs, tt.wantRuns) } if len(ledger.finished) != tt.wantFinished { t.Errorf("claims finished = %d, want %d", len(ledger.finished), tt.wantFinished) } }) } } func TestTrimGateProgressKeepsTheFailure(t *testing.T) { in := "go: downloading github.com/a/b v1.0.0\ngo: downloading github.com/c/d v2.0.0\nprotoc-gen-go: executable file not found in $PATH\nFAIL\n" want := "protoc-gen-go: executable file not found in $PATH\nFAIL\n" if got := trimGateProgress(in); got != want { t.Fatalf("trimGateProgress = %q, want %q", got, want) } }