Migrate framework tests to native producer and artifact contracts
This commit is contained in:
@@ -2,10 +2,8 @@ package pipeline
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
@@ -93,26 +91,6 @@ func matchingNotes(values []codecNotes, value string) int {
|
||||
return count
|
||||
}
|
||||
|
||||
type candidateCheckpointRecorder struct {
|
||||
CheckpointRecorder
|
||||
mergeSucceeded int
|
||||
normalizeSucceeded int
|
||||
mergeOutput CheckpointArtifact
|
||||
normalizeOutput CheckpointArtifact
|
||||
}
|
||||
|
||||
func (r *candidateCheckpointRecorder) MergeSucceeded(_ string, _ string, _ []CheckpointFingerprint, output CheckpointArtifact) error {
|
||||
r.mergeSucceeded++
|
||||
r.mergeOutput = cloneCheckpointArtifact(output)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *candidateCheckpointRecorder) NormalizeSucceeded(_ string, _ string, _ []CheckpointFingerprint, output CheckpointArtifact) error {
|
||||
r.normalizeSucceeded++
|
||||
r.normalizeOutput = cloneCheckpointArtifact(output)
|
||||
return nil
|
||||
}
|
||||
|
||||
func installObservedNotesCodec(t *testing.T, prepared *PreparedPipeline, codec *observedNotesCodec) {
|
||||
t.Helper()
|
||||
registry := NewArtifactCodecRegistry()
|
||||
@@ -130,7 +108,7 @@ func configureCandidateOperation(prepared *PreparedPipeline, target ModuleStage,
|
||||
lane := &prepared.Steps[0].lanes[0]
|
||||
switch target {
|
||||
case StageExtract:
|
||||
lane.typed.extract = func(context.Context, any, contracts.TypedExtractionRequest) (erasedTypedResult, error) {
|
||||
lane.typed.extract = func(context.Context, any, contracts.TypedExtractionRequest[any]) (erasedTypedResult, error) {
|
||||
return erasedTypedResult{Value: value}, nil
|
||||
}
|
||||
lane.typed.merge = func(context.Context, any, contracts.TypedMergeRequest[any]) (erasedTypedResult, error) {
|
||||
@@ -176,12 +154,7 @@ func setCandidateValidator(prepared *PreparedPipeline, target ModuleStage, appro
|
||||
func TestRunnerIsolatesTypedValidatorCandidates(t *testing.T) {
|
||||
for _, target := range []ModuleStage{StageExtract, StageMerge, StageNormalize} {
|
||||
t.Run(string(target), func(t *testing.T) {
|
||||
prepared := preparedAttemptDebugPipeline(t)
|
||||
if target == StageMerge {
|
||||
prepared.Steps[0].lanes[0].resolved.MergeValidationPolicy.SemanticRejection = SemanticRejectionRejectOutput
|
||||
} else {
|
||||
prepared.Steps[0].lanes[0].resolved.NormalizeValidationPolicy.SemanticRejection = SemanticRejectionRejectOutput
|
||||
}
|
||||
prepared := preparedConcurrentPipeline(t, 1)
|
||||
prepared.Steps[0].lanes[0].extractValidators = preparedValidatorChain{}
|
||||
prepared.Steps[0].lanes[0].mergeValidators = preparedValidatorChain{}
|
||||
prepared.Steps[0].lanes[0].normalizeValidators = preparedValidatorChain{}
|
||||
@@ -233,8 +206,7 @@ func TestRunnerIsolatesTypedValidatorCandidates(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
debug := newCapturedDebugRecorder()
|
||||
output, err := New().Run(context.Background(), RunInput{Prepared: prepared, RawInput: []byte("input"), Debug: debug})
|
||||
output, err := New().Run(context.Background(), RunInput{Prepared: prepared, RawInput: []byte("input")})
|
||||
if err != nil {
|
||||
t.Fatalf("Run() error = %v, want nil", err)
|
||||
}
|
||||
@@ -251,23 +223,10 @@ func TestRunnerIsolatesTypedValidatorCandidates(t *testing.T) {
|
||||
}
|
||||
case StageNormalize:
|
||||
var normalized codecNotes
|
||||
if err := json.Unmarshal(output.NormalizeOutputs[0].Artifact.Content, &normalized); err != nil || !reflect.DeepEqual(normalized, candidate) {
|
||||
if err := json.Unmarshal(output.normalizeOutputs[0].Artifact.Content, &normalized); err != nil || !reflect.DeepEqual(normalized, candidate) {
|
||||
t.Fatalf("normalized output = %#v, %v; want %#v", normalized, err, candidate)
|
||||
}
|
||||
}
|
||||
attemptPath := fmt.Sprintf("%s/notes/attempt-01.json", target)
|
||||
if target == StageExtract {
|
||||
attemptPath = "extract/notes/chunk-000001/attempt-01.json"
|
||||
}
|
||||
payload := debug.envelope(t, attemptPath).Payload.(map[string]any)
|
||||
content, err := base64.StdEncoding.DecodeString(payload["output"].(map[string]any)["content"].(map[string]any)["content_base64"].(string))
|
||||
var debugValue codecNotes
|
||||
if err == nil {
|
||||
err = json.Unmarshal(content, &debugValue)
|
||||
}
|
||||
if err != nil || !reflect.DeepEqual(debugValue, candidate) {
|
||||
t.Fatalf("debug candidate = %#v, %v; want %#v", debugValue, err, candidate)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -284,7 +243,7 @@ func setCandidateValidators(prepared *PreparedPipeline, target ModuleStage, vali
|
||||
}
|
||||
|
||||
func TestRunnerReportsCandidateDecodeFailure(t *testing.T) {
|
||||
prepared := preparedAttemptDebugPipeline(t)
|
||||
prepared := preparedConcurrentPipeline(t, 1)
|
||||
codec := &observedNotesCodec{candidateDecodeError: "decode-failure"}
|
||||
installObservedNotesCodec(t, prepared, codec)
|
||||
configureCandidateOperation(prepared, StageMerge, codecNotes{Items: []string{"decode-failure"}})
|
||||
@@ -298,131 +257,74 @@ func TestRunnerReportsCandidateDecodeFailure(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestRunnerRejectsCandidatesBeforeFinalEncoding(t *testing.T) {
|
||||
for _, target := range []ModuleStage{StageMerge, StageNormalize} {
|
||||
for _, target := range []ModuleStage{StageExtract, StageMerge, StageNormalize} {
|
||||
t.Run(string(target), func(t *testing.T) {
|
||||
prepared := preparedAttemptDebugPipeline(t)
|
||||
if target == StageMerge {
|
||||
prepared.Steps[0].lanes[0].resolved.MergeValidationPolicy.SemanticRejection = SemanticRejectionRejectOutput
|
||||
} else {
|
||||
prepared.Steps[0].lanes[0].resolved.NormalizeValidationPolicy.SemanticRejection = SemanticRejectionRejectOutput
|
||||
}
|
||||
prepared := preparedConcurrentPipeline(t, 1)
|
||||
codec := &observedNotesCodec{}
|
||||
installObservedNotesCodec(t, prepared, codec)
|
||||
configureCandidateOperation(prepared, target, codecNotes{Items: []string{"invalid"}})
|
||||
setCandidateValidator(prepared, target, false)
|
||||
recorder := &candidateCheckpointRecorder{CheckpointRecorder: NoopCheckpointRecorder()}
|
||||
|
||||
output, err := New().Run(context.Background(), RunInput{Prepared: prepared, RawInput: []byte("input"), Checkpoints: recorder})
|
||||
if err != nil {
|
||||
t.Fatalf("Run() error = %v, want validator rejection", err)
|
||||
}
|
||||
if len(output.Rejected) != 1 || output.Rejected[0].Stage != string(target) || output.Rejected[0].ReasonCode != "candidate_rejected" {
|
||||
t.Fatalf("rejected outputs = %#v, want %s validator rejection", output.Rejected, target)
|
||||
output, err := New().Run(context.Background(), RunInput{Prepared: prepared, RawInput: []byte("input")})
|
||||
if err == nil || !strings.Contains(err.Error(), "candidate-check") || len(output.OutputFiles) != 0 {
|
||||
t.Fatalf("result=%#v error=%v, want rejection without publication", output, err)
|
||||
}
|
||||
if codec.candidateCalls("invalid") != 1 || codec.finalCalls("invalid") != 0 {
|
||||
t.Fatalf("invalid candidate calls = candidate %d, final %d; want 1, 0", codec.candidateCalls("invalid"), codec.finalCalls("invalid"))
|
||||
}
|
||||
if target == StageMerge && recorder.mergeSucceeded != 0 {
|
||||
t.Fatalf("merge checkpoints = %d, want none", recorder.mergeSucceeded)
|
||||
}
|
||||
if target == StageNormalize && recorder.normalizeSucceeded != 0 {
|
||||
t.Fatalf("normalize checkpoints = %d, want none", recorder.normalizeSucceeded)
|
||||
t.Fatalf("rejected candidate calls = candidate %d, final %d; want 1, 0", codec.candidateCalls("invalid"), codec.finalCalls("invalid"))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunnerFinalEncodesAcceptedCandidatesOnce(t *testing.T) {
|
||||
for _, target := range []ModuleStage{StageMerge, StageNormalize} {
|
||||
for _, target := range []ModuleStage{StageExtract, StageMerge, StageNormalize} {
|
||||
t.Run(string(target), func(t *testing.T) {
|
||||
prepared := preparedAttemptDebugPipeline(t)
|
||||
prepared := preparedConcurrentPipeline(t, 1)
|
||||
codec := &observedNotesCodec{}
|
||||
installObservedNotesCodec(t, prepared, codec)
|
||||
value := "accepted-" + string(target)
|
||||
configureCandidateOperation(prepared, target, codecNotes{Items: []string{value}})
|
||||
setCandidateValidator(prepared, target, true)
|
||||
recorder := &candidateCheckpointRecorder{CheckpointRecorder: NoopCheckpointRecorder()}
|
||||
loader := &extractResultLoader{CheckpointLoader: NoopCheckpointLoader(), decision: CheckpointDecision{Reason: "not found"}}
|
||||
|
||||
output, err := New().Run(context.Background(), RunInput{Prepared: prepared, RawInput: []byte("input"), Checkpoints: recorder, Checkpoint: loader})
|
||||
_, err := New().Run(context.Background(), RunInput{Prepared: prepared, RawInput: []byte("input")})
|
||||
if err != nil {
|
||||
t.Fatalf("Run() error = %v, want nil", err)
|
||||
t.Fatal(err)
|
||||
}
|
||||
if codec.candidateCalls(value) != 1 || codec.finalCalls(value) != 1 {
|
||||
t.Fatalf("accepted candidate calls = candidate %d, final %d; want 1, 1", codec.candidateCalls(value), codec.finalCalls(value))
|
||||
}
|
||||
if target == StageMerge && recorder.mergeSucceeded != 1 {
|
||||
t.Fatalf("merge checkpoints = %d, want one", recorder.mergeSucceeded)
|
||||
}
|
||||
if target == StageNormalize && recorder.normalizeSucceeded != 1 {
|
||||
t.Fatalf("normalize checkpoints = %d, want one", recorder.normalizeSucceeded)
|
||||
}
|
||||
checkpoint := recorder.mergeOutput
|
||||
if target == StageNormalize {
|
||||
checkpoint = recorder.normalizeOutput
|
||||
}
|
||||
var stored codecNotes
|
||||
if err := json.Unmarshal(checkpoint.Artifact.Content, &stored); err != nil || !reflect.DeepEqual(stored, codecNotes{Items: []string{value}}) {
|
||||
t.Fatalf("checkpoint content = %s, %v; want accepted value %q", checkpoint.Artifact.Content, err, value)
|
||||
}
|
||||
gotStages := make([]string, len(output.CheckpointEvents))
|
||||
for i, event := range output.CheckpointEvents {
|
||||
gotStages[i] = event.Stage
|
||||
}
|
||||
wantStages := []string{"source", string(StageExtract), string(StageMerge), string(StageNormalize)}
|
||||
if !reflect.DeepEqual(gotStages, wantStages) {
|
||||
t.Fatalf("checkpoint event stages = %#v, want %#v", gotStages, wantStages)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunnerRecordsCandidateAndFinalEncodingFailures(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
target ModuleStage
|
||||
value string
|
||||
candidate bool
|
||||
wantError string
|
||||
}{
|
||||
{name: "merge candidate", target: StageMerge, value: "merge-candidate-failure", candidate: true, wantError: "serialize merge candidate"},
|
||||
{name: "normalize candidate", target: StageNormalize, value: "normalize-candidate-failure", candidate: true, wantError: "serialize normalize candidate"},
|
||||
{name: "merge final", target: StageMerge, value: "merge-final-failure", wantError: "serialize accepted merge output"},
|
||||
{name: "normalize final", target: StageNormalize, value: "normalize-final-failure", wantError: "serialize accepted normalize output"},
|
||||
}
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
prepared := preparedAttemptDebugPipeline(t)
|
||||
codec := &observedNotesCodec{}
|
||||
if tc.candidate {
|
||||
codec.candidateError = tc.value
|
||||
} else {
|
||||
codec.finalError = tc.value
|
||||
func TestRunnerReportsCandidateAndFinalEncodingFailures(t *testing.T) {
|
||||
for _, target := range []ModuleStage{StageExtract, StageMerge, StageNormalize} {
|
||||
for _, candidateFailure := range []bool{true, false} {
|
||||
name := string(target) + "/final"
|
||||
if candidateFailure {
|
||||
name = string(target) + "/candidate"
|
||||
}
|
||||
installObservedNotesCodec(t, prepared, codec)
|
||||
configureCandidateOperation(prepared, tc.target, codecNotes{Items: []string{tc.value}})
|
||||
setCandidateValidator(prepared, tc.target, true)
|
||||
recorder := &candidateCheckpointRecorder{CheckpointRecorder: NoopCheckpointRecorder()}
|
||||
debug := newCapturedDebugRecorder()
|
||||
|
||||
_, err := New().Run(context.Background(), RunInput{Prepared: prepared, RawInput: []byte("input"), Checkpoints: recorder, Debug: debug})
|
||||
if err == nil || !strings.Contains(err.Error(), tc.wantError) {
|
||||
t.Fatalf("Run() error = %v, want %q", err, tc.wantError)
|
||||
}
|
||||
attemptPath := fmt.Sprintf("%s/notes/attempt-01.json", tc.target)
|
||||
envelope := debug.envelope(t, attemptPath)
|
||||
if !strings.Contains(envelope.Error, tc.wantError) {
|
||||
t.Fatalf("attempt error = %q, want %q", envelope.Error, tc.wantError)
|
||||
}
|
||||
if tc.candidate && codec.finalCalls(tc.value) != 0 {
|
||||
t.Fatalf("final encode calls = %d, want none after candidate failure", codec.finalCalls(tc.value))
|
||||
}
|
||||
if tc.target == StageMerge && recorder.mergeSucceeded != 0 {
|
||||
t.Fatalf("merge checkpoints = %d, want none", recorder.mergeSucceeded)
|
||||
}
|
||||
if tc.target == StageNormalize && recorder.normalizeSucceeded != 0 {
|
||||
t.Fatalf("normalize checkpoints = %d, want none", recorder.normalizeSucceeded)
|
||||
}
|
||||
})
|
||||
t.Run(name, func(t *testing.T) {
|
||||
prepared := preparedConcurrentPipeline(t, 1)
|
||||
codec := &observedNotesCodec{}
|
||||
value := "encoding-failure"
|
||||
wantOperation := "encode"
|
||||
if candidateFailure {
|
||||
codec.candidateError = value
|
||||
wantOperation = "encode candidate"
|
||||
} else {
|
||||
codec.finalError = value
|
||||
}
|
||||
installObservedNotesCodec(t, prepared, codec)
|
||||
configureCandidateOperation(prepared, target, codecNotes{Items: []string{value}})
|
||||
setCandidateValidator(prepared, target, true)
|
||||
output, err := New().Run(context.Background(), RunInput{Prepared: prepared, RawInput: []byte("input")})
|
||||
var codecErr *ArtifactCodecOperationError
|
||||
if !errors.As(err, &codecErr) || codecErr.Operation != wantOperation || codecErr.Kind != "test/notes" || len(output.OutputFiles) != 0 {
|
||||
t.Fatalf("output=%#v error=%v codec error=%#v, want scoped %s failure without publication", output, err, codecErr, wantOperation)
|
||||
}
|
||||
if candidateFailure && codec.finalCalls(value) != 0 {
|
||||
t.Fatal("candidate failure reached final encoding")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user