Prepare shared reconciliation operations before evaluation migration
This commit is contained in:
78
internal/framework/semanticreconcile/operation_test.go
Normal file
78
internal/framework/semanticreconcile/operation_test.go
Normal file
@@ -0,0 +1,78 @@
|
||||
package semanticreconcile
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline"
|
||||
"go.maximumdirect.net/promptkit"
|
||||
)
|
||||
|
||||
func TestPrepareOperationSelectsModelOrDeterministicWork(t *testing.T) {
|
||||
doc := &source.SourceDocument{ID: "document", Units: []source.SourceUnit{{ID: 1, Text: "Alpha"}, {ID: 2, Text: "Beta"}}}
|
||||
records := []string{"Alpha", "Beta"}
|
||||
input := OperationInput[[]string, string]{Source: doc, Original: []string{"original"}, Deterministic: []string{"preprocessed"}, Records: records, Limits: DefaultLimits(), PromptID: "initial", CorrectionID: "correct", ProfileID: "profile", SessionID: "session",
|
||||
Project: func(records []string) ([]Candidate, error) {
|
||||
return []Candidate{{Label: records[0], SourceRefs: []source.SourceRef{{SourceID: doc.ID, StartUnitID: 1, EndUnitID: 1}}}, {Label: records[1], SourceRefs: []source.SourceRef{{SourceID: doc.ID, StartUnitID: 2, EndUnitID: 2}}}}, nil
|
||||
},
|
||||
Interpret: func(context.Context, Preparation, *promptkit.RunResult) (contracts.Evaluation[[]string], error) {
|
||||
return nil, nil
|
||||
},
|
||||
Fallback: func(_ context.Context, original []string, _ contracts.Evaluation[[]string]) ([]string, error) {
|
||||
return original, nil
|
||||
},
|
||||
}
|
||||
operation, err := PrepareOperation(input)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
native, ok := operation.(contracts.NativeOperation[[]string])
|
||||
if !ok || native.Initial.PromptID != "initial" || native.Initial.ProfileID != "profile" || native.Initial.SessionID != "session" {
|
||||
t.Fatalf("native operation = %#v", operation)
|
||||
}
|
||||
correction, err := native.Correction(context.Background(), []string{"repair"})
|
||||
if err != nil || correction.PromptID != "correct" {
|
||||
t.Fatalf("correction = %#v, error = %v", correction, err)
|
||||
}
|
||||
|
||||
input.Records = records[:1]
|
||||
input.Project = func([]string) ([]Candidate, error) {
|
||||
t.Fatal("single record should not require projection")
|
||||
return nil, nil
|
||||
}
|
||||
operation, err = PrepareOperation(input)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
assessments := 0
|
||||
result, err := pipeline.ExecuteOperation(context.Background(), pipeline.ExecutionContext[[]string]{Controls: contracts.ProducerControls{Mode: contracts.ModePermissive}, Assess: func(context.Context, []string) (contracts.CandidateAssessment, error) {
|
||||
assessments++
|
||||
return contracts.CandidateAssessment{Validators: []contracts.ValidatorAssessment{{ValidatorName: "rule", Decision: contracts.AssessmentRejected}}}, nil
|
||||
}}, func(context.Context) (contracts.PreparedOperation[[]string], error) { return operation, nil })
|
||||
if err != nil || assessments != 1 || len(result.Value) != 1 || result.Value[0] != "original" || result.Outcome.Disposition != contracts.ProducerFallback {
|
||||
t.Fatalf("result = %#v, assessments = %d, error = %v", result, assessments, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrepareOperationPropagatesProjectionErrorsAndLimits(t *testing.T) {
|
||||
bad := errors.New("projection failed")
|
||||
input := OperationInput[int, int]{Records: []int{1, 2}, Limits: DefaultLimits(), Project: func([]int) ([]Candidate, error) { return nil, bad }}
|
||||
if _, err := PrepareOperation(input); !errors.Is(err, bad) {
|
||||
t.Fatalf("projection error = %v", err)
|
||||
}
|
||||
input.Source = &source.SourceDocument{ID: "doc", Units: []source.SourceUnit{{ID: 1}, {ID: 2}}}
|
||||
input.Project = func([]int) ([]Candidate, error) {
|
||||
return []Candidate{{SourceRefs: []source.SourceRef{{SourceID: "doc", StartUnitID: 1, EndUnitID: 1}}}, {SourceRefs: []source.SourceRef{{SourceID: "doc", StartUnitID: 2, EndUnitID: 2}}}}, nil
|
||||
}
|
||||
input.Limits.MaximumCandidates = 1
|
||||
operation, err := PrepareOperation(input)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, ok := operation.(contracts.PreparationLimit[int]); !ok {
|
||||
t.Fatalf("operation = %T, want preparation limit", operation)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user