2026-08-03 19:05:49 +00:00
|
|
|
package pipeline
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2026-11-26 01:34:51 +00:00
|
|
|
"fmt"
|
2026-08-03 19:05:49 +00:00
|
|
|
"testing"
|
2026-11-22 04:28:48 +00:00
|
|
|
|
2026-11-26 01:34:51 +00:00
|
|
|
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
|
2026-11-22 04:28:48 +00:00
|
|
|
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
2026-08-03 19:05:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestRunnerUsesProvidedSessionWithoutSourceFallback(t *testing.T) {
|
|
|
|
|
for _, test := range []struct {
|
|
|
|
|
name string
|
|
|
|
|
session string
|
|
|
|
|
want string
|
|
|
|
|
}{
|
2026-11-22 04:28:48 +00:00
|
|
|
{name: "provided", session: " supplied-session ", want: "supplied-session"},
|
|
|
|
|
{name: "empty"},
|
2026-08-03 19:05:49 +00:00
|
|
|
} {
|
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
2026-11-22 04:28:48 +00:00
|
|
|
prepared := preparedConcurrentPipeline(t, 1)
|
|
|
|
|
var session string
|
|
|
|
|
calls := 0
|
2026-11-25 15:24:50 +00:00
|
|
|
installExtractOperation(prepared, 0, func(_ context.Context, request contracts.ExtractionInput) (erasedTypedResult, error) {
|
2026-11-22 04:28:48 +00:00
|
|
|
calls++
|
|
|
|
|
session = request.SessionID
|
|
|
|
|
return erasedTypedResult{Value: typedValueForLane(0, request.Chunk.Index)}, nil
|
|
|
|
|
})
|
|
|
|
|
_, err := New().Run(context.Background(), RunInput{
|
2026-08-03 19:05:49 +00:00
|
|
|
Prepared: prepared,
|
|
|
|
|
RawInput: []byte("input"),
|
|
|
|
|
SessionID: test.session,
|
2026-11-22 04:28:48 +00:00
|
|
|
Workers: 1,
|
2026-08-03 19:05:49 +00:00
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2026-11-22 04:28:48 +00:00
|
|
|
if calls != 1 || session != test.want {
|
|
|
|
|
t.Fatalf("extract calls=%d session=%q, want one call and %q", calls, session, test.want)
|
2026-08-03 19:05:49 +00:00
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-11-26 01:34:51 +00:00
|
|
|
|
|
|
|
|
func TestRunnerSharesWholeSourceIndexAcrossJobsAndSteps(t *testing.T) {
|
|
|
|
|
prepared := preparedOrderedPipeline(t, 3, orderedLaneSpec{"first", "notes"}, orderedLaneSpec{"second", "notes"})
|
2026-11-26 05:12:19 +00:00
|
|
|
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)
|
2026-11-26 01:34:51 +00:00
|
|
|
for step := range prepared.Steps {
|
|
|
|
|
lane := &prepared.Steps[step].lanes[0]
|
2026-11-26 05:12:19 +00:00
|
|
|
wrapValidators(&lane.extractValidators)
|
|
|
|
|
wrapValidators(&lane.mergeValidators)
|
|
|
|
|
wrapValidators(&lane.normalizeValidators)
|
2026-11-26 01:34:51 +00:00
|
|
|
original := lane.typed.extract
|
|
|
|
|
lane.typed.extract = func(ctx context.Context, impl any, request contracts.ExtractionInput, execution ExecutionContext[any]) (erasedTypedResult, error) {
|
2026-11-26 05:12:19 +00:00
|
|
|
if err := observe(request.Source, request.SourceIndex); err != nil {
|
|
|
|
|
return erasedTypedResult{}, err
|
2026-11-26 01:34:51 +00:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
|
}
|
2026-11-26 05:12:19 +00:00
|
|
|
if len(indexes) != expected {
|
|
|
|
|
t.Fatalf("observed %d extraction/validation calls, want %d", len(indexes), expected)
|
2026-11-26 01:34:51 +00:00
|
|
|
}
|
|
|
|
|
first := <-indexes
|
2026-11-26 05:12:19 +00:00
|
|
|
for i := 1; i < expected; i++ {
|
2026-11-26 01:34:51 +00:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
}
|