package runner import ( "context" "errors" "io" "net/http" "slices" "strings" "testing" "time" "amadan.net/oficina/equipo/internal/equipo/team" "amadan.net/oficina/equipo/internal/runnerproto" ) func TestInfrastructureFailureReleasesAttempt(t *testing.T) { for _, cause := range []error{context.Canceled, context.DeadlineExceeded, ErrTransient, team.ErrEvidence} { t.Run(cause.Error(), func(t *testing.T) { s, r, _, order := cleanupFixture(t) r.ServiceInterrupted = errors.Is(cause, context.Canceled) || errors.Is(cause, context.DeadlineExceeded) _ = s.finishAttempt(context.Background(), r, cause) if errors.Is(cause, team.ErrEvidence) { if !slices.Contains(*order, "complete") { t.Fatal("task outcome not failed", *order) } } else if slices.Contains(*order, "complete") || !slices.Contains(*order, "release") { t.Fatal("infrastructure permanently failed task", *order) } }) } } func TestReportPostsRetryExactRequest(t *testing.T) { for _, op := range []string{"evidence", "artifacts", "freeze", "complete", "questions", "prepare", "publication_ready", "publication_authorize"} { t.Run(op, func(t *testing.T) { calls := 0 var first string c := &HTTPInstance{origin: "https://equipo.example", bearer: runnerproto.RunnerCredential{Token: "test", ExpiresAt: time.Now().Add(time.Hour)}, client: &http.Client{Transport: transportFunc(func(r *http.Request) (*http.Response, error) { raw, _ := io.ReadAll(r.Body) calls++ if calls == 1 { first = string(raw) return nil, io.ErrUnexpectedEOF } if string(raw) != first { t.Error("retry changed request") } return &http.Response{StatusCode: 200, Body: io.NopCloser(strings.NewReader(`{"version":2,"result":null}`))}, nil })}} ctx, cancel := context.WithTimeout(context.Background(), time.Second) defer cancel() if err := c.Call(ctx, op, runnerproto.RunnerRequest{Version: 2, RequestID: "exact"}, nil); err != nil || calls != 2 { t.Fatal("post was not retried", calls, err) } }) } }