package pipeline import ( "context" "fmt" "testing" "gitea.maximumdirect.net/eric/notarius/internal/core/source" "gitea.maximumdirect.net/eric/notarius/internal/framework/contracts" ) func TestRunnerUsesProvidedSessionWithoutSourceFallback(t *testing.T) { for _, test := range []struct { name string session string want string }{ {name: "provided", session: " supplied-session ", want: "supplied-session"}, {name: "empty"}, } { t.Run(test.name, func(t *testing.T) { prepared := preparedConcurrentPipeline(t, 1) var session string calls := 0 installExtractOperation(prepared, 0, func(_ context.Context, request contracts.ExtractionInput) (erasedTypedResult, error) { calls++ session = request.SessionID return erasedTypedResult{Value: typedValueForLane(0, request.Chunk.Index)}, nil }) _, err := New().Run(context.Background(), RunInput{ Prepared: prepared, RawInput: []byte("input"), SessionID: test.session, Workers: 1, }) if err != nil { t.Fatal(err) } if calls != 1 || session != test.want { t.Fatalf("extract calls=%d session=%q, want one call and %q", calls, session, test.want) } }) } } func TestRunnerSharesWholeSourceIndexAcrossJobsAndSteps(t *testing.T) { prepared := preparedOrderedPipeline(t, 3, orderedLaneSpec{"first", "notes"}, orderedLaneSpec{"second", "notes"}) expected := 6 + len(prepared.chunkValidators.validators) for _, step := range prepared.Steps { lane := step.lanes[0] expected += 3*len(lane.extractValidators.validators) + len(lane.mergeValidators.validators) + len(lane.normalizeValidators.validators) } indexes := make(chan *source.DocumentIndex, expected) observe := func(doc *source.SourceDocument, index *source.DocumentIndex) error { if index == nil { return fmt.Errorf("missing source index") } for position, unit := range doc.Units { if got, ok := index.Position(unit.ID); !ok || got != position { return fmt.Errorf("source index position for %d = %d, %t, want %d", unit.ID, got, ok, position) } } select { case indexes <- index: default: return fmt.Errorf("unexpected extra index observation") } return nil } wrapValidators := func(chain *preparedValidatorChain) { for i := range chain.validators { validator := &chain.validators[i] switch validator.resolved.Target { case ValidatorTargetTyped: original := validator.typedValidate validator.typedValidate = func(ctx context.Context, impl any, target typedValidationTarget) (contracts.ValidationResult, error) { if err := observe(target.source, target.sourceIndex); err != nil { return contracts.ValidationResult{}, err } return original(ctx, impl, target) } case ValidatorTargetSerialized: original := validator.serialized validator.serialized = serializedValidationFunc(func(ctx context.Context, req contracts.SerializedValidationRequest) (contracts.ValidationResult, error) { if err := observe(req.Source, req.SourceIndex); err != nil { return contracts.ValidationResult{}, err } return original.Validate(ctx, req) }) case ValidatorTargetChunk: original := validator.chunk validator.chunk = chunkAssessmentValidator(func(ctx context.Context, req contracts.ChunkValidationRequest) (contracts.ValidationResult, error) { if err := observe(req.Source, req.SourceIndex); err != nil { return contracts.ValidationResult{}, err } return original.Validate(ctx, req) }) } } } wrapValidators(&prepared.chunkValidators) for step := range prepared.Steps { lane := &prepared.Steps[step].lanes[0] wrapValidators(&lane.extractValidators) wrapValidators(&lane.mergeValidators) wrapValidators(&lane.normalizeValidators) original := lane.typed.extract lane.typed.extract = func(ctx context.Context, impl any, request contracts.ExtractionInput, execution ExecutionContext[any]) (erasedTypedResult, error) { if err := observe(request.Source, request.SourceIndex); err != nil { return erasedTypedResult{}, err } return original(ctx, impl, request, execution) } } var previous *source.DocumentIndex for run := 0; run < 2; run++ { doc := typedTestDocumentWithUnits(3) if run == 1 { doc.Units[0], doc.Units[2] = doc.Units[2], doc.Units[0] doc.Digest, _ = source.DigestDocument(doc) } prepared.input.(*typedTestInput).doc = doc prepared.chunker = &typedTestChunker{key: "typed/chunk", plan: typedTestPlan(doc)} if _, err := New().Run(context.Background(), RunInput{Prepared: prepared, RawInput: []byte("input"), Workers: 3}); err != nil { t.Fatal(err) } if len(indexes) != expected { t.Fatalf("observed %d extraction/validation calls, want %d", len(indexes), expected) } first := <-indexes for i := 1; i < expected; i++ { if got := <-indexes; got != first { t.Fatal("jobs did not share the run source index") } } if first == previous { t.Fatal("separate runs shared a source index") } previous = first } }