2026-07-21 21:39:23 +00:00
|
|
|
package pipeline
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"encoding/json"
|
2026-08-09 01:30:10 +00:00
|
|
|
"fmt"
|
2026-07-21 21:39:23 +00:00
|
|
|
"reflect"
|
|
|
|
|
"strings"
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func handoffFixture(t *testing.T, value codecNotes) (RunInput, PreparedPipelineStep, contracts.SerializedOutput) {
|
|
|
|
|
t.Helper()
|
|
|
|
|
prepared := preparedOrderedPipeline(t, 1,
|
|
|
|
|
orderedLaneSpec{id: "notes", profile: "notes"},
|
|
|
|
|
orderedLaneSpec{id: "score", profile: "score"},
|
|
|
|
|
)
|
|
|
|
|
consumer := &prepared.Steps[1].lanes[0]
|
|
|
|
|
installGeneratedTarget := func(target *ResolvedReferenceTarget, stage ModuleStage, module string) {
|
|
|
|
|
*target = ResolvedReferenceTarget{
|
|
|
|
|
Stage: stage,
|
|
|
|
|
StepID: consumer.resolved.StepID,
|
|
|
|
|
LaneID: consumer.resolved.ID,
|
|
|
|
|
Module: module,
|
|
|
|
|
Bindings: []ReferenceBinding{{
|
|
|
|
|
Stage: stage,
|
|
|
|
|
LaneID: consumer.resolved.ID,
|
|
|
|
|
SlotName: "producer-output",
|
|
|
|
|
Artifact: &ArtifactReference{Step: "step-1", Lane: "notes"},
|
|
|
|
|
}},
|
|
|
|
|
ReferenceSet: contracts.ReferenceSet{Slots: map[string]contracts.ResolvedReferenceSlot{
|
|
|
|
|
"producer-output": {Slot: contracts.ReferenceSlot{
|
|
|
|
|
Name: "producer-output",
|
|
|
|
|
AcceptedArtifactKinds: []contracts.ArtifactKind{"test/notes"},
|
|
|
|
|
AcceptedMediaTypes: []string{"application/json"},
|
|
|
|
|
}},
|
|
|
|
|
}},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
installGeneratedTarget(&consumer.resolved.ExtractReferences, StageExtract, consumer.resolved.Extract.Module)
|
|
|
|
|
installGeneratedTarget(&consumer.resolved.MergeReferences, StageMerge, consumer.resolved.Merge.Module)
|
|
|
|
|
installGeneratedTarget(&consumer.resolved.NormalizeReferences, StageNormalize, consumer.resolved.Normalize.Module)
|
|
|
|
|
|
2026-11-22 04:28:48 +00:00
|
|
|
artifact, err := serializeArtifact(prepared.Steps[0].lanes[0].typed.codec, value, false)
|
2026-07-21 21:39:23 +00:00
|
|
|
if err != nil {
|
2026-11-22 04:28:48 +00:00
|
|
|
t.Fatalf("serializeArtifact() error = %v", err)
|
2026-07-21 21:39:23 +00:00
|
|
|
}
|
|
|
|
|
input := RunInput{Prepared: prepared}
|
|
|
|
|
input.pipeline = prepared.resolved
|
|
|
|
|
return input, prepared.Steps[1], contracts.SerializedOutput{
|
|
|
|
|
StepID: "step-1",
|
|
|
|
|
LaneID: "notes",
|
|
|
|
|
NormalizerKey: "typed/normalize",
|
|
|
|
|
SourceID: "source",
|
2026-11-22 04:28:48 +00:00
|
|
|
Artifact: contracts.CloneSerializedArtifact(artifact),
|
2026-07-21 21:39:23 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestBuildStepReferenceSetsCanonicalizesAndClonesFanout(t *testing.T) {
|
|
|
|
|
input, step, producerOutput := handoffFixture(t, codecNotes{Items: []string{"first"}})
|
2026-11-22 04:28:48 +00:00
|
|
|
sets, err := buildStepReferenceSets(input, step, []contracts.SerializedOutput{producerOutput})
|
2026-07-21 21:39:23 +00:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("buildStepReferenceSets() error = %v", err)
|
|
|
|
|
}
|
|
|
|
|
if got, want := len(sets), 3; got != want {
|
|
|
|
|
t.Fatalf("reference target set count = %d, want %d", got, want)
|
|
|
|
|
}
|
|
|
|
|
var first []byte
|
|
|
|
|
for key, set := range sets {
|
|
|
|
|
item := set.Slots["producer-output"].Items[0]
|
|
|
|
|
if item.Origin.Type != "generated" || item.Origin.URI != "" {
|
|
|
|
|
t.Fatalf("target %v origin = %#v, want generated origin without URI", key, item.Origin)
|
|
|
|
|
}
|
|
|
|
|
if item.Producer.StepID != "step-1" || item.Producer.LaneID != "notes" || item.Producer.ModuleKey != "typed/normalize" {
|
|
|
|
|
t.Fatalf("target %v producer = %#v, want producer provenance", key, item.Producer)
|
|
|
|
|
}
|
|
|
|
|
if first == nil {
|
|
|
|
|
first = item.Content
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if &first[0] == &item.Content[0] {
|
|
|
|
|
t.Fatalf("target %v shares generated content backing storage", key)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if got := len(step.lanes[0].resolved.ExtractReferences.ReferenceSet.Slots["producer-output"].Items); got != 0 {
|
|
|
|
|
t.Fatalf("prepared extract reference items = %d, want zero", got)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-09 01:30:10 +00:00
|
|
|
func TestBuildStepReferenceSetsIndexesManyOutputsForFanout(t *testing.T) {
|
|
|
|
|
input, step, producerOutput := handoffFixture(t, codecNotes{Items: []string{"first"}})
|
|
|
|
|
outputs := make([]contracts.SerializedOutput, 0, 65)
|
|
|
|
|
for index := range 64 {
|
|
|
|
|
outputs = append(outputs, contracts.SerializedOutput{StepID: fmt.Sprintf("unrelated-step-%d", index), LaneID: "unrelated-lane"})
|
|
|
|
|
}
|
|
|
|
|
outputs = append(outputs, producerOutput)
|
|
|
|
|
|
2026-11-22 04:28:48 +00:00
|
|
|
sets, err := buildStepReferenceSets(input, step, outputs)
|
2026-08-09 01:30:10 +00:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("buildStepReferenceSets() error = %v", err)
|
|
|
|
|
}
|
2026-11-22 04:28:48 +00:00
|
|
|
if len(sets) != 3 {
|
|
|
|
|
t.Fatalf("reference set count = %d, want 3", len(sets))
|
2026-08-09 01:30:10 +00:00
|
|
|
}
|
|
|
|
|
for target, set := range sets {
|
|
|
|
|
item := set.Slots["producer-output"].Items[0]
|
|
|
|
|
if item.Producer.StepID != "step-1" || item.Producer.LaneID != "notes" || string(item.Content) == "" {
|
|
|
|
|
t.Fatalf("target %v item = %#v, want canonical producer artifact", target, item)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-21 21:39:23 +00:00
|
|
|
func TestGeneratedReferenceAcceptsTypedEmptyCollection(t *testing.T) {
|
|
|
|
|
input, step, producerOutput := handoffFixture(t, codecNotes{})
|
2026-11-22 04:28:48 +00:00
|
|
|
sets, err := buildStepReferenceSets(input, step, []contracts.SerializedOutput{producerOutput})
|
2026-07-21 21:39:23 +00:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("buildStepReferenceSets() error = %v, want nil for accepted empty collection", err)
|
|
|
|
|
}
|
|
|
|
|
if got := len(sets[keyForReferenceTarget(step.lanes[0].resolved.ExtractReferences)].Slots["producer-output"].Items); got != 1 {
|
|
|
|
|
t.Fatalf("generated empty collection item count = %d, want one artifact", got)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestGeneratedReferenceRejectsInvalidProducerOutputsDeterministically(t *testing.T) {
|
|
|
|
|
tests := []struct {
|
|
|
|
|
name string
|
|
|
|
|
outputs func(contracts.SerializedOutput) []contracts.SerializedOutput
|
|
|
|
|
mutate func(*ResolvedReferenceTarget, *contracts.SerializedOutput)
|
|
|
|
|
want string
|
|
|
|
|
}{
|
|
|
|
|
{name: "missing", outputs: func(contracts.SerializedOutput) []contracts.SerializedOutput { return nil }, want: "no accepted normalized output"},
|
|
|
|
|
{name: "multiple", outputs: func(output contracts.SerializedOutput) []contracts.SerializedOutput {
|
|
|
|
|
return []contracts.SerializedOutput{output, output}
|
|
|
|
|
}, want: "exactly one is required"},
|
2026-08-09 01:30:10 +00:00
|
|
|
{name: "multiple after identifier normalization", outputs: func(output contracts.SerializedOutput) []contracts.SerializedOutput {
|
|
|
|
|
duplicate := contracts.CloneSerializedOutput(output)
|
|
|
|
|
duplicate.StepID = " step-1 "
|
|
|
|
|
duplicate.LaneID = " notes "
|
|
|
|
|
return []contracts.SerializedOutput{output, duplicate}
|
|
|
|
|
}, want: "producer has 2 accepted normalized outputs; exactly one is required"},
|
2026-07-21 21:39:23 +00:00
|
|
|
{name: "kind mismatch", outputs: func(output contracts.SerializedOutput) []contracts.SerializedOutput {
|
|
|
|
|
return []contracts.SerializedOutput{output}
|
|
|
|
|
}, mutate: func(target *ResolvedReferenceTarget, _ *contracts.SerializedOutput) {
|
|
|
|
|
target.ReferenceSet.Slots["producer-output"] = contracts.ResolvedReferenceSlot{Slot: contracts.ReferenceSlot{Name: "producer-output", AcceptedArtifactKinds: []contracts.ArtifactKind{"test/score"}}}
|
|
|
|
|
}, want: "does not accept artifact kind"},
|
|
|
|
|
{name: "schema mismatch", outputs: func(output contracts.SerializedOutput) []contracts.SerializedOutput {
|
|
|
|
|
return []contracts.SerializedOutput{output}
|
|
|
|
|
}, mutate: func(_ *ResolvedReferenceTarget, output *contracts.SerializedOutput) {
|
|
|
|
|
output.Artifact.Schema.ID = "wrong.schema"
|
|
|
|
|
}, want: "schema identity"},
|
|
|
|
|
{name: "media mismatch", outputs: func(output contracts.SerializedOutput) []contracts.SerializedOutput {
|
|
|
|
|
return []contracts.SerializedOutput{output}
|
|
|
|
|
}, mutate: func(_ *ResolvedReferenceTarget, output *contracts.SerializedOutput) {
|
|
|
|
|
output.Artifact.MediaType = "text/plain"
|
|
|
|
|
}, want: "media type"},
|
|
|
|
|
{name: "size mismatch", outputs: func(output contracts.SerializedOutput) []contracts.SerializedOutput {
|
|
|
|
|
return []contracts.SerializedOutput{output}
|
|
|
|
|
}, mutate: func(target *ResolvedReferenceTarget, _ *contracts.SerializedOutput) {
|
|
|
|
|
slot := target.ReferenceSet.Slots["producer-output"]
|
|
|
|
|
slot.Slot.MaxBytes = 1
|
|
|
|
|
target.ReferenceSet.Slots["producer-output"] = slot
|
|
|
|
|
}, want: "limit"},
|
|
|
|
|
}
|
|
|
|
|
for _, test := range tests {
|
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
|
input, step, output := handoffFixture(t, codecNotes{Items: []string{"first"}})
|
|
|
|
|
output = contracts.CloneSerializedOutput(output)
|
|
|
|
|
if test.mutate != nil {
|
|
|
|
|
target := &step.lanes[0].resolved.ExtractReferences
|
|
|
|
|
test.mutate(target, &output)
|
|
|
|
|
}
|
2026-11-22 04:28:48 +00:00
|
|
|
_, err := buildStepReferenceSets(input, step, test.outputs(output))
|
2026-07-21 21:39:23 +00:00
|
|
|
if err == nil || !strings.Contains(err.Error(), test.want) {
|
|
|
|
|
t.Fatalf("buildStepReferenceSets() error = %v, want %q", err, test.want)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-09 01:30:10 +00:00
|
|
|
func BenchmarkIndexGeneratedOutputs(b *testing.B) {
|
|
|
|
|
outputs := make([]contracts.SerializedOutput, 0, 1024)
|
|
|
|
|
for index := range 1024 {
|
|
|
|
|
outputs = append(outputs, contracts.SerializedOutput{StepID: fmt.Sprintf("step-%d", index), LaneID: fmt.Sprintf("lane-%d", index)})
|
|
|
|
|
}
|
|
|
|
|
b.ResetTimer()
|
|
|
|
|
for range b.N {
|
|
|
|
|
_ = indexGeneratedOutputs(outputs)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-21 21:39:23 +00:00
|
|
|
func TestRunnerHandsOffAcceptedNormalizedOutputBeforeConsumerLanes(t *testing.T) {
|
|
|
|
|
input, _, _ := handoffFixture(t, codecNotes{Items: []string{"first"}})
|
|
|
|
|
prepared := input.Prepared
|
2026-11-22 04:28:48 +00:00
|
|
|
received := make(map[ModuleStage]contracts.ReferenceSet)
|
|
|
|
|
prepared.Steps[0].lanes[0].typed.extract = func(context.Context, any, contracts.TypedExtractionRequest[any]) (erasedTypedResult, error) {
|
2026-07-21 21:39:23 +00:00
|
|
|
return erasedTypedResult{Value: codecNotes{Items: []string{"first"}}}, nil
|
|
|
|
|
}
|
2026-11-22 04:28:48 +00:00
|
|
|
prepared.Steps[1].lanes[0].typed.extract = func(_ context.Context, _ any, request contracts.TypedExtractionRequest[any]) (erasedTypedResult, error) {
|
|
|
|
|
received[StageExtract] = CloneReferenceSet(request.References)
|
2026-07-21 21:39:23 +00:00
|
|
|
return erasedTypedResult{Value: codecScore{Value: 3}}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
prepared.Steps[0].lanes[0].typed.normalize = func(context.Context, any, contracts.TypedNormalizeRequest[any]) (erasedTypedResult, error) {
|
2026-11-22 04:28:48 +00:00
|
|
|
return erasedTypedResult{Value: codecNotes{Items: []string{"first"}}}, nil
|
2026-07-21 21:39:23 +00:00
|
|
|
}
|
2026-11-22 04:28:48 +00:00
|
|
|
prepared.Steps[1].lanes[0].typed.merge = func(_ context.Context, _ any, request contracts.TypedMergeRequest[any]) (erasedTypedResult, error) {
|
|
|
|
|
received[StageMerge] = CloneReferenceSet(request.References)
|
2026-07-21 21:39:23 +00:00
|
|
|
return erasedTypedResult{Value: codecScore{Value: 3}}, nil
|
|
|
|
|
}
|
2026-11-22 04:28:48 +00:00
|
|
|
prepared.Steps[1].lanes[0].typed.normalize = func(_ context.Context, _ any, request contracts.TypedNormalizeRequest[any]) (erasedTypedResult, error) {
|
|
|
|
|
received[StageNormalize] = CloneReferenceSet(request.References)
|
|
|
|
|
return erasedTypedResult{Value: codecScore{Value: 3}}, nil
|
2026-07-21 21:39:23 +00:00
|
|
|
}
|
2026-11-22 04:28:48 +00:00
|
|
|
_, err := New().Run(context.Background(), input)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("Run() error = %v", err)
|
2026-07-21 21:39:23 +00:00
|
|
|
}
|
2026-11-22 04:28:48 +00:00
|
|
|
for _, target := range []ModuleStage{StageExtract, StageMerge, StageNormalize} {
|
|
|
|
|
set, found := received[target]
|
|
|
|
|
if !found || len(set.Slots["producer-output"].Items) != 1 {
|
|
|
|
|
t.Fatalf("%s reference set = %#v, want producer artifact", target, set)
|
2026-07-21 21:39:23 +00:00
|
|
|
}
|
2026-11-22 04:28:48 +00:00
|
|
|
item := set.Slots["producer-output"].Items[0]
|
|
|
|
|
if item.Origin.Type != "generated" || item.Producer.StepID != "step-1" || item.Producer.LaneID != "notes" {
|
|
|
|
|
t.Fatalf("%s reference = %#v, want generated producer reference", target, item)
|
|
|
|
|
}
|
|
|
|
|
var value codecNotes
|
|
|
|
|
if err := json.Unmarshal(item.Content, &value); err != nil || !reflect.DeepEqual(value, codecNotes{Items: []string{"first"}}) {
|
|
|
|
|
t.Fatalf("%s reference value = %#v, %v", target, value, err)
|
2026-07-21 21:39:23 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|