package pipeline import ( "context" "errors" "fmt" "testing" "gitea.maximumdirect.net/eric/notarius/internal/framework/contracts" "go.maximumdirect.net/promptkit" ) func TestRunErrorSelectionPreservesOperationalDeadlines(t *testing.T) { for _, first := range []error{context.DeadlineExceeded, fmt.Errorf("%w: %w", promptkit.ErrOperationTimeout, context.DeadlineExceeded)} { later := errors.New("later failure") got := selectRunError(context.Background(), []orderedRunError{{lane: 1, err: later}, {lane: 0, err: first}, {lane: -1, err: context.Canceled}}) if got != first { t.Fatalf("selected %v, want %v", got, first) } } ctx, cancel := context.WithCancel(context.Background()) cancel() if got := selectRunError(ctx, []orderedRunError{{err: errors.New("operation")}}); got != context.Canceled { t.Fatal(got) } } func TestRunErrorSelectionRetainsOnlyCaptureBearingJobsOnCancellation(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) cancel() firstCause, extraCause, laterCause := errors.New("first capture"), errors.New("extra capture"), errors.New("later capture") first := &contracts.CaptureError{Cause: firstCause} later := &contracts.CaptureError{Cause: laterCause} ordinary := errors.New("ordinary sibling failure") got := selectRunError(ctx, []orderedRunError{ {stage: 2, lane: 0, err: later}, {stage: 0, lane: -1, err: ordinary}, {stage: 0, lane: 0, err: errors.Join(first, &contracts.CaptureError{Cause: extraCause})}, {stage: 0, lane: -2, err: context.Canceled}, }) for _, cause := range []error{context.Canceled, firstCause, extraCause, laterCause} { if !errors.Is(got, cause) { t.Fatalf("selected error %v lost cause %v", got, cause) } } var captured *contracts.CaptureError if !errors.As(got, &captured) || captured != first || errors.Is(got, ordinary) { t.Fatalf("selected error %v changed canonical capture order or retained ordinary siblings", got) } }