| 1 | package runner |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "slices" |
| 7 | "testing" |
| 8 | |
| 9 | "amadan.net/oficina/equipo/internal/equipo/team" |
| 10 | "amadan.net/oficina/equipo/internal/runner/provider" |
| 11 | "amadan.net/oficina/equipo/internal/runnerproto" |
| 12 | ) |
| 13 | |
| 14 | type outcomeAPI struct { |
| 15 | supervisorAPI |
| 16 | result *team.AttemptResult |
| 17 | } |
| 18 | |
| 19 | func (a *outcomeAPI) Call(ctx context.Context, op string, q runnerproto.RunnerRequest, out any) error { |
| 20 | if op == "complete" { |
| 21 | a.result = q.Result |
| 22 | } |
| 23 | return a.supervisorAPI.Call(ctx, op, q, out) |
| 24 | } |
| 25 | func TestDeterministicAttemptErrorsFailWithReason(t *testing.T) { |
| 26 | cases := []struct { |
| 27 | err error |
| 28 | reason string |
| 29 | }{ |
| 30 | {context.Canceled, "runner.cancelled"}, {context.DeadlineExceeded, "runner.timeout"}, |
| 31 | {provider.ErrAccountMaintenance, "runner.unavailable"}, {provider.ErrLoginRequired, "account.sign_in_again"}, |
| 32 | {provider.ErrIdentity, "runner.identity"}, {ErrPromptTooLarge, "runner.prompt_too_large"}, |
| 33 | {team.ErrEvidence, "runner.evidence"}, {team.ErrQuota, "runner.quota"}, |
| 34 | {team.ErrBudget, "runner.budget"}, {team.ErrReplayUnsafe, "runner.replay"}, |
| 35 | {provider.ErrContract, "runner.contract"}, {provider.ErrUncertain, "runner.unavailable"}, |
| 36 | {provider.ErrClosed, "runner.unavailable"}, |
| 37 | } |
| 38 | for _, key := range []string{"form.invalid", "runner.replay", "runner.budget", "runner.quota", "runner.evidence"} { |
| 39 | cases = append(cases, struct { |
| 40 | err error |
| 41 | reason string |
| 42 | }{&RefusalError{Reason: key, Status: 422}, key}) |
| 43 | } |
| 44 | for _, tc := range cases { |
| 45 | t.Run(tc.err.Error(), func(t *testing.T) { |
| 46 | for _, cancelled := range []bool{false, true} { |
| 47 | s, r, _, order := cleanupFixture(t) |
| 48 | api := &outcomeAPI{supervisorAPI: supervisorAPI{order: order}} |
| 49 | s.api = api |
| 50 | err := tc.err |
| 51 | if cancelled { |
| 52 | r.ServiceInterrupted = tc.err != context.Canceled && tc.err != context.DeadlineExceeded |
| 53 | err = errors.Join(err, context.Canceled) |
| 54 | } |
| 55 | _ = s.finishAttempt(context.Background(), r, err) |
| 56 | if slices.Contains(*order, "release") || api.result == nil || api.result.Outcome != team.OutcomeFailure || api.result.Detail != tc.reason || r.FailureReason != tc.reason { |
| 57 | t.Fatalf("task error released or reason lost: order=%v result=%+v reason=%s", *order, api.result, r.FailureReason) |
| 58 | } |
| 59 | } |
| 60 | }) |
| 61 | } |
| 62 | } |
| 63 | func TestProviderClosedBeforeDispatchReleases(t *testing.T) { |
| 64 | s, r, _, order := cleanupFixture(t) |
| 65 | r.Phase = "workspace" |
| 66 | _ = s.finishAttempt(context.Background(), r, provider.ErrClosed) |
| 67 | if !slices.Contains(*order, "release") || slices.Contains(*order, "complete") { |
| 68 | t.Fatal("pre-dispatch provider outage failed job", *order) |
| 69 | } |
| 70 | } |
| 71 | |