Prepare scene extraction and deterministic normalization in core
This commit is contained in:
@@ -544,6 +544,10 @@ acceptance state, and no registry-specific concepts leak into ordinary core.
|
|||||||
|
|
||||||
## Stage 13 — Migrate Scene Extraction And Two Deterministic Normalizers
|
## Stage 13 — Migrate Scene Extraction And Two Deterministic Normalizers
|
||||||
|
|
||||||
|
**Status:** Complete. The focused suites and core pipeline tests pass, including
|
||||||
|
scene cache reuse against the current chunk and one-shot combat validation.
|
||||||
|
Other legacy callers remain deferred to the cutover.
|
||||||
|
|
||||||
**Goal:** Finish model extraction and cover non-registry normalization examples.
|
**Goal:** Finish model extraction and cover non-registry normalization examples.
|
||||||
|
|
||||||
**Inspect:** `dnd/extract/scenedescriptions`, `dnd/normalize/scenedescriptions`,
|
**Inspect:** `dnd/extract/scenedescriptions`, `dnd/normalize/scenedescriptions`,
|
||||||
|
|||||||
@@ -77,6 +77,9 @@ type NativeOperation[T any] struct {
|
|||||||
Correction CorrectionBuilder
|
Correction CorrectionBuilder
|
||||||
Fallback Fallback[T]
|
Fallback Fallback[T]
|
||||||
Diagnostics TerminalDiagnostics[T]
|
Diagnostics TerminalDiagnostics[T]
|
||||||
|
// Reuse checks and, when needed, adapts a decoded accepted value to this
|
||||||
|
// operation's current domain inputs. An error makes the cache entry unusable.
|
||||||
|
Reuse func(T) (T, error)
|
||||||
// Evidence is optional, lazy preparation evidence for this operation.
|
// Evidence is optional, lazy preparation evidence for this operation.
|
||||||
Evidence func() (any, error)
|
Evidence func() (any, error)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -100,6 +100,18 @@ func executeNative[T any](ctx context.Context, execution ExecutionContext[T], op
|
|||||||
var err error
|
var err error
|
||||||
var cacheKey string
|
var cacheKey string
|
||||||
cache := execution.Cache
|
cache := execution.Cache
|
||||||
|
if cache != nil && cache.Decode != nil && operation.Reuse != nil {
|
||||||
|
copy := *cache
|
||||||
|
decode := cache.Decode
|
||||||
|
copy.Decode = func(content []byte) (T, error) {
|
||||||
|
value, err := decode(content)
|
||||||
|
if err != nil {
|
||||||
|
return value, err
|
||||||
|
}
|
||||||
|
return operation.Reuse(value)
|
||||||
|
}
|
||||||
|
cache = ©
|
||||||
|
}
|
||||||
if execution.Execution.Capture != nil {
|
if execution.Execution.Capture != nil {
|
||||||
if err := execution.Execution.RecordEvidence("cache/selection.json", map[string]any{"mode": execution.Controls.Cache, "reuse_eligible": execution.ReuseEligible, "store_available": cache != nil && cache.Store != nil}); err != nil {
|
if err := execution.Execution.RecordEvidence("cache/selection.json", map[string]any{"mode": execution.Controls.Cache, "reuse_eligible": execution.ReuseEligible, "store_available": cache != nil && cache.Store != nil}); err != nil {
|
||||||
return empty, captureFailure(err, capture.Finish(nil, err, false, false))
|
return empty, captureFailure(err, capture.Finish(nil, err, false, false))
|
||||||
|
|||||||
@@ -49,44 +49,31 @@ var _ contracts.Extractor[dnd.SceneDescriptionList] = (*Extractor)(nil)
|
|||||||
|
|
||||||
type Options struct{}
|
type Options struct{}
|
||||||
|
|
||||||
type Extractor struct{ engine *promptkit.Engine }
|
type Extractor struct{}
|
||||||
|
|
||||||
func New(engine *promptkit.Engine, _ Options, references ...contracts.ReferenceSet) (*Extractor, error) {
|
func New(_ Options, references ...contracts.ReferenceSet) (*Extractor, error) {
|
||||||
if engine == nil {
|
|
||||||
return nil, extractorErrorf("native engine must not be nil")
|
|
||||||
}
|
|
||||||
if len(references) > 1 {
|
if len(references) > 1 {
|
||||||
return nil, extractorErrorf("at most one reference set may be supplied")
|
return nil, extractorErrorf("at most one reference set may be supplied")
|
||||||
}
|
}
|
||||||
return &Extractor{engine: engine}, nil
|
return &Extractor{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Extractor) Key() string { return Key }
|
func (e *Extractor) Key() string { return Key }
|
||||||
|
|
||||||
func (e *Extractor) ReferenceSlots() []contracts.ReferenceSlot { return referenceSlots() }
|
func (e *Extractor) ReferenceSlots() []contracts.ReferenceSlot { return referenceSlots() }
|
||||||
|
|
||||||
func (e *Extractor) Extract(ctx context.Context, req contracts.TypedExtractionRequest[dnd.SceneDescriptionList]) (contracts.TypedExtractionResult[dnd.SceneDescriptionList], error) {
|
func (e *Extractor) PrepareExtraction(ctx context.Context, req contracts.ExtractionInput) (contracts.PreparedOperation[dnd.SceneDescriptionList], error) {
|
||||||
if e == nil {
|
if e == nil {
|
||||||
return contracts.TypedExtractionResult[dnd.SceneDescriptionList]{}, extractorErrorf("extractor must not be nil")
|
return nil, extractorErrorf("extractor must not be nil")
|
||||||
}
|
}
|
||||||
if e.engine == nil {
|
sourceInput, err := shared.PrepareChunkInput(ctx, req)
|
||||||
return contracts.TypedExtractionResult[dnd.SceneDescriptionList]{}, extractorErrorf("native engine must not be nil")
|
|
||||||
}
|
|
||||||
sourceInput, err := shared.PrepareChunkExtraction(ctx, req)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return contracts.TypedExtractionResult[dnd.SceneDescriptionList]{}, extractorErrorf("%w", err)
|
return nil, extractorErrorf("%w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
chunk := source.Chunk{ID: req.Chunk.ID, Ref: req.Chunk.Ref}
|
chunk := source.Chunk{ID: req.Chunk.ID, Ref: req.Chunk.Ref}
|
||||||
index := source.NewDocumentIndex(req.Source)
|
index := source.NewDocumentIndex(req.Source)
|
||||||
if cache := req.Operation.Cache; cache != nil && cache.Decode != nil {
|
reuse := func(value dnd.SceneDescriptionList) (dnd.SceneDescriptionList, error) {
|
||||||
owned := *cache
|
|
||||||
decode := cache.Decode
|
|
||||||
owned.Decode = func(content []byte) (dnd.SceneDescriptionList, error) {
|
|
||||||
value, err := decode(content)
|
|
||||||
if err != nil {
|
|
||||||
return value, err
|
|
||||||
}
|
|
||||||
if len(value.Scenes) != 1 || value.Scenes[0].SourceRef != chunk.Ref {
|
if len(value.Scenes) != 1 || value.Scenes[0].SourceRef != chunk.Ref {
|
||||||
return dnd.SceneDescriptionList{}, fmt.Errorf("cached scene does not describe the current chunk range")
|
return dnd.SceneDescriptionList{}, fmt.Errorf("cached scene does not describe the current chunk range")
|
||||||
}
|
}
|
||||||
@@ -95,34 +82,29 @@ func (e *Extractor) Extract(ctx context.Context, req contracts.TypedExtractionRe
|
|||||||
value.Scenes[0].ID = chunk.ID
|
value.Scenes[0].ID = chunk.ID
|
||||||
return sceneFallback(&value, chunk, index)
|
return sceneFallback(&value, chunk, index)
|
||||||
}
|
}
|
||||||
req.Operation.Cache = &owned
|
|
||||||
}
|
|
||||||
materials := shared.PromptInputs(sourceInput, req.References)
|
materials := shared.PromptInputs(sourceInput, req.References)
|
||||||
inputs := make(map[string]promptkit.ArtifactRef, len(materials))
|
inputs := make(map[string]promptkit.ArtifactRef, len(materials))
|
||||||
for name, material := range materials {
|
for name, material := range materials {
|
||||||
inputs[name] = promptkit.InlineWithURI(material.OriginURI, string(material.Content))
|
inputs[name] = promptkit.InlineWithURI(material.OriginURI, string(material.Content))
|
||||||
}
|
}
|
||||||
initial := promptkit.RunRequest{PromptID: PromptID, ProfileID: req.LLMProfile, SessionID: req.SessionID, Inputs: inputs}
|
initial := promptkit.RunRequest{PromptID: PromptID, ProfileID: req.LLMProfile, SessionID: req.SessionID, Inputs: inputs}
|
||||||
value, outcome, err := pipeline.RunProducer(ctx, e.engine, req.Operation, initial,
|
return contracts.NativeOperation[dnd.SceneDescriptionList]{Initial: initial, Reuse: reuse,
|
||||||
func(_ context.Context, result *promptkit.RunResult) (*dnd.SceneDescriptionList, []string, error) {
|
Interpret: func(_ context.Context, result *promptkit.RunResult) (contracts.Evaluation[dnd.SceneDescriptionList], error) {
|
||||||
var response extractionResponse
|
var response extractionResponse
|
||||||
if err := json.Unmarshal([]byte(result.RawOutput), &response); err != nil {
|
if err := json.Unmarshal([]byte(result.RawOutput), &response); err != nil {
|
||||||
return nil, nil, extractorErrorf("decode native output: %w", err)
|
return nil, extractorErrorf("decode native output: %w", err)
|
||||||
}
|
}
|
||||||
value := mapResponse(response, &chunk)
|
value := mapResponse(response, &chunk)
|
||||||
return &value, nil, nil
|
return contracts.CandidateEvaluation[dnd.SceneDescriptionList]{Result: contracts.EvaluationView[dnd.SceneDescriptionList]{Candidate: &value}}, nil
|
||||||
},
|
},
|
||||||
func(_ context.Context, guidance []string) (promptkit.RunRequest, error) {
|
Correction: contracts.PromptCorrection(CorrectionPromptID, "guidance", func(guidance []string) promptkit.ArtifactRef { return promptkit.Inline(strings.Join(guidance, "\n\n")) }),
|
||||||
return promptkit.RunRequest{PromptID: CorrectionPromptID, ProfileID: req.LLMProfile, SessionID: req.SessionID,
|
Fallback: func(_ context.Context, evaluation contracts.Evaluation[dnd.SceneDescriptionList]) (dnd.SceneDescriptionList, error) {
|
||||||
Inputs: map[string]promptkit.ArtifactRef{"guidance": promptkit.Inline(strings.Join(guidance, "\n\n"))}}, nil
|
var final *dnd.SceneDescriptionList
|
||||||
},
|
if evaluation != nil {
|
||||||
func(_ context.Context, final *dnd.SceneDescriptionList) (dnd.SceneDescriptionList, error) {
|
final = evaluation.View().Candidate
|
||||||
return sceneFallback(final, chunk, index)
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return contracts.TypedExtractionResult[dnd.SceneDescriptionList]{}, extractorErrorf("produce scene description: %w", err)
|
|
||||||
}
|
}
|
||||||
return contracts.TypedExtractionResult[dnd.SceneDescriptionList]{Value: value, Outcome: outcome}, nil
|
return sceneFallback(final, chunk, index)
|
||||||
|
}}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func mapResponse(response extractionResponse, chunk *source.Chunk) dnd.SceneDescriptionList {
|
func mapResponse(response extractionResponse, chunk *source.Chunk) dnd.SceneDescriptionList {
|
||||||
@@ -154,7 +136,7 @@ func Register(registry *pipeline.ExtractorRegistry) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return New(request.Dependencies.Engine, options, request.References)
|
return New(options, request.References)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -85,15 +85,14 @@ func TestExtractReturnsSemanticallyInvalidResponseForDeterministicValidation(t *
|
|||||||
func TestExtractValidatesRequestsAndSurfacesProviderFailures(t *testing.T) {
|
func TestExtractValidatesRequestsAndSurfacesProviderFailures(t *testing.T) {
|
||||||
request := extractionRequest()
|
request := extractionRequest()
|
||||||
extractor := newExtractor(t, &fakeSceneDescriptionsLLMClient{response: extractionResponse{Kind: dnd.SceneKindMeta, Title: "Table talk", Summary: "The group discusses rules."}})
|
extractor := newExtractor(t, &fakeSceneDescriptionsLLMClient{response: extractionResponse{Kind: dnd.SceneKindMeta, Title: "Table talk", Summary: "The group discusses rules."}})
|
||||||
var nilExtractor *Extractor
|
var nilExtractor *extractionFixture
|
||||||
for _, test := range []struct {
|
for _, test := range []struct {
|
||||||
name string
|
name string
|
||||||
extractor *Extractor
|
extractor *extractionFixture
|
||||||
req contracts.TypedExtractionRequest[dnd.SceneDescriptionList]
|
req contracts.TypedExtractionRequest[dnd.SceneDescriptionList]
|
||||||
want string
|
want string
|
||||||
}{
|
}{
|
||||||
{name: "nil extractor", extractor: nilExtractor, req: request, want: "extractor"},
|
{name: "nil extractor", extractor: nilExtractor, req: request, want: "extractor"},
|
||||||
{name: "nil LLM client", extractor: &Extractor{}, req: request, want: "native engine"},
|
|
||||||
{name: "wrapped preflight failure", extractor: extractor, req: mismatchedSourceInputRequest(request), want: "must match chunk"},
|
{name: "wrapped preflight failure", extractor: extractor, req: mismatchedSourceInputRequest(request), want: "must match chunk"},
|
||||||
} {
|
} {
|
||||||
t.Run(test.name, func(t *testing.T) {
|
t.Run(test.name, func(t *testing.T) {
|
||||||
@@ -102,8 +101,9 @@ func TestExtractValidatesRequestsAndSurfacesProviderFailures(t *testing.T) {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
_, err := newExtractor(t, &fakeSceneDescriptionsLLMClient{err: errors.New("provider unavailable")}).Extract(context.Background(), request)
|
failure := errors.New("provider unavailable")
|
||||||
if err == nil || !strings.Contains(err.Error(), "dnd scene descriptions") || !strings.Contains(err.Error(), "provider unavailable") {
|
_, err := newExtractor(t, &fakeSceneDescriptionsLLMClient{err: failure}).Extract(context.Background(), request)
|
||||||
|
if !errors.Is(err, failure) {
|
||||||
t.Fatalf("provider Extract() error = %v, want contextual provider error", err)
|
t.Fatalf("provider Extract() error = %v, want contextual provider error", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ const combatScene = `{"kind":"combat","title":"Watchtower fight","summary":"Band
|
|||||||
const blankScene = `{"kind":"combat","title":"","summary":""}`
|
const blankScene = `{"kind":"combat","title":"","summary":""}`
|
||||||
const combatJudgment = `{"classification":"combat","explanation":"Bandits attack the party during the current scene."}`
|
const combatJudgment = `{"classification":"combat","explanation":"Bandits attack the party during the current scene."}`
|
||||||
|
|
||||||
func bindSceneAssessment(t *testing.T, extractor *Extractor, request *contracts.TypedExtractionRequest[dnd.SceneDescriptionList], withSemantic bool) *[]string {
|
func bindSceneAssessment(t *testing.T, extractor *extractionFixture, request *contracts.TypedExtractionRequest[dnd.SceneDescriptionList], withSemantic bool) *[]string {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
validators := []contracts.TypedValidator[dnd.SceneDescriptionList]{shape.New(shape.Options{}), sourcerefs.New(sourcerefs.Options{}), relatedness.New(relatedness.Options{})}
|
validators := []contracts.TypedValidator[dnd.SceneDescriptionList]{shape.New(shape.Options{}), sourcerefs.New(sourcerefs.Options{}), relatedness.New(relatedness.Options{})}
|
||||||
if withSemantic {
|
if withSemantic {
|
||||||
|
|||||||
@@ -10,11 +10,8 @@ import (
|
|||||||
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd"
|
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestNewRequiresLLMClientAndRejectsAmbiguousReferences(t *testing.T) {
|
func TestNewRejectsAmbiguousReferences(t *testing.T) {
|
||||||
if _, err := New(nil, Options{}); err == nil || !strings.Contains(err.Error(), "native engine") {
|
if _, err := New(Options{}, contracts.ReferenceSet{}, contracts.ReferenceSet{}); err == nil || !strings.Contains(err.Error(), "at most one reference set") {
|
||||||
t.Fatalf("New(nil) error = %v, want dependency error", err)
|
|
||||||
}
|
|
||||||
if _, err := New(newExtractor(t, &fakeSceneDescriptionsLLMClient{}).engine, Options{}, contracts.ReferenceSet{}, contracts.ReferenceSet{}); err == nil || !strings.Contains(err.Error(), "at most one reference set") {
|
|
||||||
t.Fatalf("New() error = %v, want reference-set error", err)
|
t.Fatalf("New() error = %v, want reference-set error", err)
|
||||||
}
|
}
|
||||||
if got := newExtractor(t, &fakeSceneDescriptionsLLMClient{}).Key(); got != Key {
|
if got := newExtractor(t, &fakeSceneDescriptionsLLMClient{}).Key(); got != Key {
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import (
|
|||||||
|
|
||||||
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
|
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
|
||||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
||||||
|
"gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline"
|
||||||
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd"
|
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -59,7 +60,12 @@ func mismatchedSourceInputRequest(req contracts.TypedExtractionRequest[dnd.Scene
|
|||||||
return req
|
return req
|
||||||
}
|
}
|
||||||
|
|
||||||
func newExtractor(t *testing.T, client promptkit.LLMClient, references ...contracts.ReferenceSet) *Extractor {
|
type extractionFixture struct {
|
||||||
|
*Extractor
|
||||||
|
engine *promptkit.Engine
|
||||||
|
}
|
||||||
|
|
||||||
|
func newExtractor(t *testing.T, client promptkit.LLMClient, references ...contracts.ReferenceSet) *extractionFixture {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
type settings struct {
|
type settings struct {
|
||||||
PromptKit promptkit.Settings `yaml:"promptkit"`
|
PromptKit promptkit.Settings `yaml:"promptkit"`
|
||||||
@@ -87,11 +93,25 @@ func newExtractor(t *testing.T, client promptkit.LLMClient, references ...contra
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
extractor, err := New(engine, Options{}, references...)
|
extractor, err := New(Options{}, references...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
return extractor
|
return &extractionFixture{Extractor: extractor, engine: engine}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (fixture *extractionFixture) Extract(ctx context.Context, req contracts.TypedExtractionRequest[dnd.SceneDescriptionList]) (pipeline.OperationResult[dnd.SceneDescriptionList], error) {
|
||||||
|
var extractor *Extractor
|
||||||
|
var engine *promptkit.Engine
|
||||||
|
if fixture != nil {
|
||||||
|
extractor, engine = fixture.Extractor, fixture.engine
|
||||||
|
}
|
||||||
|
op := req.Operation
|
||||||
|
execution := pipeline.ExecutionContext[dnd.SceneDescriptionList]{Engine: engine, Controls: op.Controls, Assess: op.Assess, Execution: op.Execution, ReuseEligible: op.ReuseEligible, Cache: op.Cache}
|
||||||
|
input := contracts.ExtractionInput{Source: req.Source, Chunk: req.Chunk, SourceInput: req.SourceInput, SessionID: req.SessionID, References: req.References, LLMProfile: req.LLMProfile, Metadata: req.Metadata, AmbientContext: req.AmbientContext}
|
||||||
|
return pipeline.ExecuteOperation(ctx, execution, func(ctx context.Context) (contracts.PreparedOperation[dnd.SceneDescriptionList], error) {
|
||||||
|
return extractor.PrepareExtraction(ctx, input)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func renderedInput(t *testing.T, request promptkit.GenerateRequest, name string) []byte {
|
func renderedInput(t *testing.T, request promptkit.GenerateRequest, name string) []byte {
|
||||||
|
|||||||
@@ -37,25 +37,25 @@ func (n *Normalizer) Key() string { return Key }
|
|||||||
|
|
||||||
func (n *Normalizer) ReferenceSlots() []contracts.ReferenceSlot { return nil }
|
func (n *Normalizer) ReferenceSlots() []contracts.ReferenceSlot { return nil }
|
||||||
|
|
||||||
func (n *Normalizer) Normalize(ctx context.Context, req contracts.TypedNormalizeRequest[dnd.SceneDescriptionList]) (contracts.TypedNormalizeResult[dnd.SceneDescriptionList], error) {
|
func (n *Normalizer) PrepareNormalize(ctx context.Context, req contracts.NormalizeInput[dnd.SceneDescriptionList]) (contracts.PreparedOperation[dnd.SceneDescriptionList], error) {
|
||||||
if n == nil {
|
if n == nil {
|
||||||
return contracts.TypedNormalizeResult[dnd.SceneDescriptionList]{}, normalizerErrorf("normalizer must not be nil")
|
return nil, normalizerErrorf("normalizer must not be nil")
|
||||||
}
|
}
|
||||||
if ctx == nil {
|
if ctx == nil {
|
||||||
return contracts.TypedNormalizeResult[dnd.SceneDescriptionList]{}, normalizerErrorf("context must not be nil")
|
return nil, normalizerErrorf("context must not be nil")
|
||||||
}
|
}
|
||||||
if err := ctx.Err(); err != nil {
|
if err := ctx.Err(); err != nil {
|
||||||
return contracts.TypedNormalizeResult[dnd.SceneDescriptionList]{}, normalizerErrorf("context error before normalize: %w", err)
|
return nil, normalizerErrorf("context error before normalize: %w", err)
|
||||||
}
|
}
|
||||||
value, findings, err := normalizeList(req.MergeOutput.Value, req.Source)
|
value, findings, err := normalizeList(req.MergeOutput.Value, req.Source)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return contracts.TypedNormalizeResult[dnd.SceneDescriptionList]{}, normalizerErrorf("normalize scenes: %w", err)
|
return nil, normalizerErrorf("normalize scenes: %w", err)
|
||||||
}
|
}
|
||||||
diagnosticGroups, err := diagnostics.NormalizationDiagnostics(findings)
|
diagnosticGroups, err := diagnostics.NormalizationDiagnostics(findings)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return contracts.TypedNormalizeResult[dnd.SceneDescriptionList]{}, normalizerErrorf("collect diagnostics: %w", err)
|
return nil, normalizerErrorf("collect diagnostics: %w", err)
|
||||||
}
|
}
|
||||||
return contracts.TypedNormalizeResult[dnd.SceneDescriptionList]{Value: value, Diagnostics: diagnosticGroups}, nil
|
return contracts.DeterministicOperation[dnd.SceneDescriptionList]{Evaluation: contracts.CandidateEvaluation[dnd.SceneDescriptionList]{Result: contracts.EvaluationView[dnd.SceneDescriptionList]{Candidate: &value, Diagnostics: diagnosticGroups}}}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type normalizedScene struct {
|
type normalizedScene struct {
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ func TestNormalizeTrimsOrdersDeduplicatesAndOwnsOutput(t *testing.T) {
|
|||||||
scene("later", 3, 3, dnd.SceneKindNarrative, " Return ", " The party returns. "),
|
scene("later", 3, 3, dnd.SceneKindNarrative, " Return ", " The party returns. "),
|
||||||
}}
|
}}
|
||||||
before := cloneList(input)
|
before := cloneList(input)
|
||||||
result, err := New(Options{}).Normalize(context.Background(), normalizeRequest(input, doc))
|
result, err := runNormalize(New(Options{}), context.Background(), normalizeRequest(input, doc))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Normalize() error = %v, want nil", err)
|
t.Fatalf("Normalize() error = %v, want nil", err)
|
||||||
}
|
}
|
||||||
@@ -44,7 +44,7 @@ func TestNormalizeTrimsOrdersDeduplicatesAndOwnsOutput(t *testing.T) {
|
|||||||
if input.Scenes[0].Title == "changed" {
|
if input.Scenes[0].Title == "changed" {
|
||||||
t.Fatal("normalized output aliases input storage")
|
t.Fatal("normalized output aliases input storage")
|
||||||
}
|
}
|
||||||
second, err := New(Options{}).Normalize(context.Background(), normalizeRequest(dnd.SceneDescriptionList{Scenes: want}, doc))
|
second, err := runNormalize(New(Options{}), context.Background(), normalizeRequest(dnd.SceneDescriptionList{Scenes: want}, doc))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("second Normalize() error = %v", err)
|
t.Fatalf("second Normalize() error = %v", err)
|
||||||
}
|
}
|
||||||
@@ -62,7 +62,7 @@ func TestNormalizeLimitsCombinedSceneMutationFindings(t *testing.T) {
|
|||||||
doc.Units[index] = source.SourceUnit{ID: index + 1}
|
doc.Units[index] = source.SourceUnit{ID: index + 1}
|
||||||
input.Scenes[index] = scene(fmt.Sprintf("scene-%02d", unitID), unitID, unitID, dnd.SceneKindNarrative, " Arrival ", " The party arrives. ")
|
input.Scenes[index] = scene(fmt.Sprintf("scene-%02d", unitID), unitID, unitID, dnd.SceneKindNarrative, " Arrival ", " The party arrives. ")
|
||||||
}
|
}
|
||||||
result, err := New(Options{}).Normalize(context.Background(), normalizeRequest(input, doc))
|
result, err := runNormalize(New(Options{}), context.Background(), normalizeRequest(input, doc))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Normalize() error = %v", err)
|
t.Fatalf("Normalize() error = %v", err)
|
||||||
}
|
}
|
||||||
@@ -90,7 +90,7 @@ func TestNormalizeRejectsInvalidCandidatesAndConflicts(t *testing.T) {
|
|||||||
{name: "same range conflict", input: dnd.SceneDescriptionList{Scenes: []dnd.SceneDescription{valid, scene("two", 1, 1, dnd.SceneKindCombat, "Ambush", "Bandits strike.")}}},
|
{name: "same range conflict", input: dnd.SceneDescriptionList{Scenes: []dnd.SceneDescription{valid, scene("two", 1, 1, dnd.SceneKindCombat, "Ambush", "Bandits strike.")}}},
|
||||||
} {
|
} {
|
||||||
t.Run(test.name, func(t *testing.T) {
|
t.Run(test.name, func(t *testing.T) {
|
||||||
if _, err := New(Options{}).Normalize(context.Background(), normalizeRequest(test.input, doc)); err == nil {
|
if _, err := runNormalize(New(Options{}), context.Background(), normalizeRequest(test.input, doc)); err == nil {
|
||||||
t.Fatal("Normalize() error = nil, want rejection")
|
t.Fatal("Normalize() error = nil, want rejection")
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -106,7 +106,7 @@ func TestNormalizeUsesDocumentOrderRatherThanNumericUnitID(t *testing.T) {
|
|||||||
scene("later", 10, 10, dnd.SceneKindNarrative, "Departure", "The party departs."),
|
scene("later", 10, 10, dnd.SceneKindNarrative, "Departure", "The party departs."),
|
||||||
scene("first", 20, 20, dnd.SceneKindNarrative, "Arrival", "The party arrives."),
|
scene("first", 20, 20, dnd.SceneKindNarrative, "Arrival", "The party arrives."),
|
||||||
}}
|
}}
|
||||||
result, err := New(Options{}).Normalize(context.Background(), normalizeRequest(input, doc))
|
result, err := runNormalize(New(Options{}), context.Background(), normalizeRequest(input, doc))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Normalize() error = %v", err)
|
t.Fatalf("Normalize() error = %v", err)
|
||||||
}
|
}
|
||||||
@@ -133,16 +133,24 @@ func TestNormalizerContractAndCancellation(t *testing.T) {
|
|||||||
normalizer := New(Options{})
|
normalizer := New(Options{})
|
||||||
canceled, cancel := context.WithCancel(context.Background())
|
canceled, cancel := context.WithCancel(context.Background())
|
||||||
cancel()
|
cancel()
|
||||||
if _, err := normalizer.Normalize(canceled, normalizeRequest(dnd.SceneDescriptionList{}, testDocument())); err == nil || !strings.Contains(err.Error(), "context") {
|
if _, err := runNormalize(normalizer, canceled, normalizeRequest(dnd.SceneDescriptionList{}, testDocument())); err == nil || !strings.Contains(err.Error(), "context") {
|
||||||
t.Fatalf("canceled Normalize() error = %v", err)
|
t.Fatalf("canceled Normalize() error = %v", err)
|
||||||
}
|
}
|
||||||
if _, err := normalizer.Normalize(context.Background(), normalizeRequest(dnd.SceneDescriptionList{Scenes: []dnd.SceneDescription{scene("one", 1, 1, dnd.SceneKindNarrative, "Arrival", "The party arrives.")}}, nil)); err == nil || !strings.Contains(err.Error(), "source document") {
|
if _, err := runNormalize(normalizer, context.Background(), normalizeRequest(dnd.SceneDescriptionList{Scenes: []dnd.SceneDescription{scene("one", 1, 1, dnd.SceneKindNarrative, "Arrival", "The party arrives.")}}, nil)); err == nil || !strings.Contains(err.Error(), "source document") {
|
||||||
t.Fatalf("nil source Normalize() error = %v", err)
|
t.Fatalf("nil source Normalize() error = %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func normalizeRequest(value dnd.SceneDescriptionList, doc *source.SourceDocument) contracts.TypedNormalizeRequest[dnd.SceneDescriptionList] {
|
func runNormalize(normalizer *Normalizer, ctx context.Context, req contracts.NormalizeInput[dnd.SceneDescriptionList]) (pipeline.OperationResult[dnd.SceneDescriptionList], error) {
|
||||||
return contracts.TypedNormalizeRequest[dnd.SceneDescriptionList]{Source: doc, MergeOutput: contracts.MergeArtifact[dnd.SceneDescriptionList]{Value: value}}
|
return pipeline.ExecuteOperation(ctx, pipeline.ExecutionContext[dnd.SceneDescriptionList]{Assess: func(context.Context, dnd.SceneDescriptionList) (contracts.CandidateAssessment, error) {
|
||||||
|
return contracts.CandidateAssessment{}, nil
|
||||||
|
}}, func(ctx context.Context) (contracts.PreparedOperation[dnd.SceneDescriptionList], error) {
|
||||||
|
return normalizer.PrepareNormalize(ctx, req)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func normalizeRequest(value dnd.SceneDescriptionList, doc *source.SourceDocument) contracts.NormalizeInput[dnd.SceneDescriptionList] {
|
||||||
|
return contracts.NormalizeInput[dnd.SceneDescriptionList]{Source: doc, MergeOutput: contracts.MergeArtifact[dnd.SceneDescriptionList]{Value: value}}
|
||||||
}
|
}
|
||||||
|
|
||||||
func testDocument() *source.SourceDocument {
|
func testDocument() *source.SourceDocument {
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ func TestNormalizeAcceptedFixtures(t *testing.T) {
|
|||||||
|
|
||||||
for _, fixture := range fixtures.Cases {
|
for _, fixture := range fixtures.Cases {
|
||||||
t.Run(fixture.Name, func(t *testing.T) {
|
t.Run(fixture.Name, func(t *testing.T) {
|
||||||
result, err := newNormalizer(t).Normalize(context.Background(), normalizeRequestWithSource(fixture.Input, sourceDocument(2)))
|
result, err := runNormalize(newNormalizer(t), context.Background(), normalizeRequestWithSource(fixture.Input, sourceDocument(2)))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Normalize() error = %v, want nil", err)
|
t.Fatalf("Normalize() error = %v, want nil", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -63,15 +63,15 @@ func (n *Normalizer) ReferenceSlots() []contracts.ReferenceSlot {
|
|||||||
return referenceSlots()
|
return referenceSlots()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *Normalizer) Normalize(ctx context.Context, req contracts.TypedNormalizeRequest[dnd.SpellList]) (contracts.TypedNormalizeResult[dnd.SpellList], error) {
|
func (n *Normalizer) PrepareNormalize(ctx context.Context, req contracts.NormalizeInput[dnd.SpellList]) (contracts.PreparedOperation[dnd.SpellList], error) {
|
||||||
if n == nil {
|
if n == nil {
|
||||||
return contracts.TypedNormalizeResult[dnd.SpellList]{}, normalizerErrorf("normalizer must not be nil")
|
return nil, normalizerErrorf("normalizer must not be nil")
|
||||||
}
|
}
|
||||||
if ctx == nil {
|
if ctx == nil {
|
||||||
return contracts.TypedNormalizeResult[dnd.SpellList]{}, normalizerErrorf("context must not be nil")
|
return nil, normalizerErrorf("context must not be nil")
|
||||||
}
|
}
|
||||||
if err := ctx.Err(); err != nil {
|
if err := ctx.Err(); err != nil {
|
||||||
return contracts.TypedNormalizeResult[dnd.SpellList]{}, normalizerErrorf("context error before normalize: %w", err)
|
return nil, normalizerErrorf("context error before normalize: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
index := source.NewDocumentIndex(req.Source)
|
index := source.NewDocumentIndex(req.Source)
|
||||||
@@ -81,9 +81,9 @@ func (n *Normalizer) Normalize(ctx context.Context, req contracts.TypedNormalize
|
|||||||
findings = append(findings, duplicateFindings...)
|
findings = append(findings, duplicateFindings...)
|
||||||
diagnosticGroups, err := diagnostics.NormalizationDiagnostics(findings, ReasonCodeSpellNameUnresolved)
|
diagnosticGroups, err := diagnostics.NormalizationDiagnostics(findings, ReasonCodeSpellNameUnresolved)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return contracts.TypedNormalizeResult[dnd.SpellList]{}, normalizerErrorf("collect diagnostics: %w", err)
|
return nil, normalizerErrorf("collect diagnostics: %w", err)
|
||||||
}
|
}
|
||||||
return contracts.TypedNormalizeResult[dnd.SpellList]{Value: value, Diagnostics: diagnosticGroups}, nil
|
return contracts.DeterministicOperation[dnd.SpellList]{Evaluation: contracts.CandidateEvaluation[dnd.SpellList]{Result: contracts.EvaluationView[dnd.SpellList]{Candidate: &value, Diagnostics: diagnosticGroups}}}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func normalizeSpellList(input dnd.SpellList, catalog spellcatalog.EffectiveCatalog, order shared.SourceRefOrder) (dnd.SpellList, []diagnostics.Finding) {
|
func normalizeSpellList(input dnd.SpellList, catalog spellcatalog.EffectiveCatalog, order shared.SourceRefOrder) (dnd.SpellList, []diagnostics.Finding) {
|
||||||
|
|||||||
@@ -109,7 +109,7 @@ func TestNormalizeCanonicalizesNamesAndReportsUnresolvedNames(t *testing.T) {
|
|||||||
{Spell: "Mystery\nSpell\tName"},
|
{Spell: "Mystery\nSpell\tName"},
|
||||||
{Spell: "Cure Wounds"},
|
{Spell: "Cure Wounds"},
|
||||||
}}
|
}}
|
||||||
result, err := normalizer.Normalize(context.Background(), normalizeRequest(input))
|
result, err := runNormalize(normalizer, context.Background(), normalizeRequest(input))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Normalize() error = %v, want nil", err)
|
t.Fatalf("Normalize() error = %v, want nil", err)
|
||||||
}
|
}
|
||||||
@@ -131,7 +131,7 @@ func TestNormalizeLimitsFindingsWithoutChangingSpellValues(t *testing.T) {
|
|||||||
for index := range input.SpellCasts {
|
for index := range input.SpellCasts {
|
||||||
input.SpellCasts[index].Spell = fmt.Sprintf("Unknown Spell %d", index)
|
input.SpellCasts[index].Spell = fmt.Sprintf("Unknown Spell %d", index)
|
||||||
}
|
}
|
||||||
result, err := newNormalizer(t).Normalize(context.Background(), normalizeRequest(input))
|
result, err := runNormalize(newNormalizer(t), context.Background(), normalizeRequest(input))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Normalize() error = %v", err)
|
t.Fatalf("Normalize() error = %v", err)
|
||||||
}
|
}
|
||||||
@@ -150,7 +150,7 @@ func TestNormalizeBoundsUnicodeNamesAndQuotesCanonicalReplacement(t *testing.T)
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("New() error = %v, want nil", err)
|
t.Fatalf("New() error = %v, want nil", err)
|
||||||
}
|
}
|
||||||
result, err := normalizer.Normalize(context.Background(), normalizeRequest(dnd.SpellList{SpellCasts: []dnd.SpellCast{{Spell: "long alias"}}}))
|
result, err := runNormalize(normalizer, context.Background(), normalizeRequest(dnd.SpellList{SpellCasts: []dnd.SpellCast{{Spell: "long alias"}}}))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Normalize() error = %v, want nil", err)
|
t.Fatalf("Normalize() error = %v, want nil", err)
|
||||||
}
|
}
|
||||||
@@ -174,7 +174,7 @@ func TestNormalizeSortsAndDeduplicatesExactSourceReferences(t *testing.T) {
|
|||||||
{SourceID: "source-a", StartUnitID: 1, EndUnitID: 4},
|
{SourceID: "source-a", StartUnitID: 1, EndUnitID: 4},
|
||||||
}
|
}
|
||||||
input := dnd.SpellList{SpellCasts: []dnd.SpellCast{{Spell: "Cure Wounds", SourceRefs: inputRefs}}}
|
input := dnd.SpellList{SpellCasts: []dnd.SpellCast{{Spell: "Cure Wounds", SourceRefs: inputRefs}}}
|
||||||
result, err := normalizer.Normalize(context.Background(), normalizeRequest(input))
|
result, err := runNormalize(normalizer, context.Background(), normalizeRequest(input))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Normalize() error = %v, want nil", err)
|
t.Fatalf("Normalize() error = %v, want nil", err)
|
||||||
}
|
}
|
||||||
@@ -202,7 +202,7 @@ func TestNormalizeReportsDuplicateRemovalWithoutOrderChange(t *testing.T) {
|
|||||||
SourceRefs: []source.SourceRef{ref1, ref1, ref2},
|
SourceRefs: []source.SourceRef{ref1, ref1, ref2},
|
||||||
}}}
|
}}}
|
||||||
|
|
||||||
result, err := newNormalizer(t).Normalize(context.Background(), normalizeRequest(input))
|
result, err := runNormalize(newNormalizer(t), context.Background(), normalizeRequest(input))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Normalize() error = %v, want nil", err)
|
t.Fatalf("Normalize() error = %v, want nil", err)
|
||||||
}
|
}
|
||||||
@@ -226,7 +226,7 @@ func TestNormalizeOrdersReferencesBySourceDocumentPosition(t *testing.T) {
|
|||||||
{SourceID: doc.ID, StartUnitID: 999, EndUnitID: 999},
|
{SourceID: doc.ID, StartUnitID: 999, EndUnitID: 999},
|
||||||
},
|
},
|
||||||
}}}
|
}}}
|
||||||
result, err := newNormalizer(t).Normalize(context.Background(), normalizeRequestWithSource(input, doc))
|
result, err := runNormalize(newNormalizer(t), context.Background(), normalizeRequestWithSource(input, doc))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Normalize() error = %v, want nil", err)
|
t.Fatalf("Normalize() error = %v, want nil", err)
|
||||||
}
|
}
|
||||||
@@ -250,7 +250,7 @@ func TestNormalizePreservesNilEmptyAndAdjacentOrOverlappingReferences(t *testing
|
|||||||
before := input
|
before := input
|
||||||
before.SpellCasts = append([]dnd.SpellCast(nil), input.SpellCasts...)
|
before.SpellCasts = append([]dnd.SpellCast(nil), input.SpellCasts...)
|
||||||
before.SpellCasts[2].SourceRefs = append([]source.SourceRef(nil), input.SpellCasts[2].SourceRefs...)
|
before.SpellCasts[2].SourceRefs = append([]source.SourceRef(nil), input.SpellCasts[2].SourceRefs...)
|
||||||
result, err := normalizer.Normalize(context.Background(), normalizeRequest(input))
|
result, err := runNormalize(normalizer, context.Background(), normalizeRequest(input))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Normalize() error = %v, want nil", err)
|
t.Fatalf("Normalize() error = %v, want nil", err)
|
||||||
}
|
}
|
||||||
@@ -272,16 +272,16 @@ func TestNormalizePreservesNilEmptyAndAdjacentOrOverlappingReferences(t *testing
|
|||||||
func TestNormalizeHandlesNilAndCanceledCalls(t *testing.T) {
|
func TestNormalizeHandlesNilAndCanceledCalls(t *testing.T) {
|
||||||
request := normalizeRequest(dnd.SpellList{})
|
request := normalizeRequest(dnd.SpellList{})
|
||||||
var normalizer *Normalizer
|
var normalizer *Normalizer
|
||||||
if _, err := normalizer.Normalize(context.Background(), request); err == nil || !strings.Contains(err.Error(), "normalizer must not be nil") {
|
if _, err := runNormalize(normalizer, context.Background(), request); err == nil || !strings.Contains(err.Error(), "normalizer must not be nil") {
|
||||||
t.Fatalf("nil receiver error = %v, want nil receiver error", err)
|
t.Fatalf("nil receiver error = %v, want nil receiver error", err)
|
||||||
}
|
}
|
||||||
normalizer = newNormalizer(t)
|
normalizer = newNormalizer(t)
|
||||||
if _, err := normalizer.Normalize(nil, request); err == nil || !strings.Contains(err.Error(), "context must not be nil") {
|
if _, err := runNormalize(normalizer, nil, request); err == nil || !strings.Contains(err.Error(), "context must not be nil") {
|
||||||
t.Fatalf("nil context error = %v, want nil context error", err)
|
t.Fatalf("nil context error = %v, want nil context error", err)
|
||||||
}
|
}
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
cancel()
|
cancel()
|
||||||
if _, err := normalizer.Normalize(ctx, request); err == nil || !strings.Contains(err.Error(), "context error before normalize") {
|
if _, err := runNormalize(normalizer, ctx, request); err == nil || !strings.Contains(err.Error(), "context canceled") {
|
||||||
t.Fatalf("canceled context error = %v, want canceled context error", err)
|
t.Fatalf("canceled context error = %v, want canceled context error", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -300,7 +300,7 @@ func TestNormalizeCollapsesDuplicateGroupsAfterCanonicalization(t *testing.T) {
|
|||||||
{Caster: " kyle ", Spell: "Cure Wounds", SourceRefs: []source.SourceRef{firstEvidence}},
|
{Caster: " kyle ", Spell: "Cure Wounds", SourceRefs: []source.SourceRef{firstEvidence}},
|
||||||
}}
|
}}
|
||||||
|
|
||||||
result, err := normalizer.Normalize(context.Background(), normalizeRequestWithSource(input, doc))
|
result, err := runNormalize(normalizer, context.Background(), normalizeRequestWithSource(input, doc))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Normalize() error = %v, want nil", err)
|
t.Fatalf("Normalize() error = %v, want nil", err)
|
||||||
}
|
}
|
||||||
@@ -343,7 +343,7 @@ func TestNormalizeKeepsDistinctAndIneligibleCastsSeparate(t *testing.T) {
|
|||||||
base := dnd.SpellCast{Spell: "Cure Wounds", Caster: "Aria", SourceRefs: []source.SourceRef{ref}}
|
base := dnd.SpellCast{Spell: "Cure Wounds", Caster: "Aria", SourceRefs: []source.SourceRef{ref}}
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
t.Run(test.name, func(t *testing.T) {
|
t.Run(test.name, func(t *testing.T) {
|
||||||
result, err := newNormalizer(t).Normalize(context.Background(), normalizeRequestWithSource(dnd.SpellList{SpellCasts: []dnd.SpellCast{base, test.variant}}, doc))
|
result, err := runNormalize(newNormalizer(t), context.Background(), normalizeRequestWithSource(dnd.SpellList{SpellCasts: []dnd.SpellCast{base, test.variant}}, doc))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Normalize() error = %v, want nil", err)
|
t.Fatalf("Normalize() error = %v, want nil", err)
|
||||||
}
|
}
|
||||||
@@ -365,7 +365,7 @@ func TestNormalizeDoesNotCollapseAdjacentOrOverlappingEvidence(t *testing.T) {
|
|||||||
{Spell: "Cure Wounds", Caster: "Aria", SourceRefs: []source.SourceRef{{SourceID: "source", StartUnitID: 1, EndUnitID: 3}}},
|
{Spell: "Cure Wounds", Caster: "Aria", SourceRefs: []source.SourceRef{{SourceID: "source", StartUnitID: 1, EndUnitID: 3}}},
|
||||||
{Spell: "Cure Wounds", Caster: "aria", SourceRefs: []source.SourceRef{{SourceID: "source", StartUnitID: 2, EndUnitID: 4}}},
|
{Spell: "Cure Wounds", Caster: "aria", SourceRefs: []source.SourceRef{{SourceID: "source", StartUnitID: 2, EndUnitID: 4}}},
|
||||||
}}
|
}}
|
||||||
result, err := newNormalizer(t).Normalize(context.Background(), normalizeRequestWithSource(input, doc))
|
result, err := runNormalize(newNormalizer(t), context.Background(), normalizeRequestWithSource(input, doc))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Normalize() error = %v, want nil", err)
|
t.Fatalf("Normalize() error = %v, want nil", err)
|
||||||
}
|
}
|
||||||
@@ -384,7 +384,7 @@ func TestNormalizeBoundsDuplicateFindingIndices(t *testing.T) {
|
|||||||
for index := range casts {
|
for index := range casts {
|
||||||
casts[index] = dnd.SpellCast{Spell: "Cure Wounds", Caster: "Aria", SourceRefs: []source.SourceRef{ref}}
|
casts[index] = dnd.SpellCast{Spell: "Cure Wounds", Caster: "Aria", SourceRefs: []source.SourceRef{ref}}
|
||||||
}
|
}
|
||||||
result, err := newNormalizer(t).Normalize(context.Background(), normalizeRequestWithSource(dnd.SpellList{SpellCasts: casts}, doc))
|
result, err := runNormalize(newNormalizer(t), context.Background(), normalizeRequestWithSource(dnd.SpellList{SpellCasts: casts}, doc))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Normalize() error = %v, want nil", err)
|
t.Fatalf("Normalize() error = %v, want nil", err)
|
||||||
}
|
}
|
||||||
@@ -406,11 +406,11 @@ func TestNormalizeIsIdempotentForAlreadyNormalizedInput(t *testing.T) {
|
|||||||
SourceRefs: []source.SourceRef{{SourceID: "source", StartUnitID: 1, EndUnitID: 2}},
|
SourceRefs: []source.SourceRef{{SourceID: "source", StartUnitID: 1, EndUnitID: 2}},
|
||||||
}}}
|
}}}
|
||||||
normalizer := newNormalizer(t)
|
normalizer := newNormalizer(t)
|
||||||
first, err := normalizer.Normalize(context.Background(), normalizeRequestWithSource(input, doc))
|
first, err := runNormalize(normalizer, context.Background(), normalizeRequestWithSource(input, doc))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("first Normalize() error = %v, want nil", err)
|
t.Fatalf("first Normalize() error = %v, want nil", err)
|
||||||
}
|
}
|
||||||
second, err := normalizer.Normalize(context.Background(), normalizeRequestWithSource(first.Value, doc))
|
second, err := runNormalize(normalizer, context.Background(), normalizeRequestWithSource(first.Value, doc))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("second Normalize() error = %v, want nil", err)
|
t.Fatalf("second Normalize() error = %v, want nil", err)
|
||||||
}
|
}
|
||||||
@@ -426,7 +426,7 @@ func TestNormalizeDuplicateOutputDoesNotShareInputSlices(t *testing.T) {
|
|||||||
{Spell: "Cure Wounds", Caster: "Aria", SourceRefs: []source.SourceRef{ref}},
|
{Spell: "Cure Wounds", Caster: "Aria", SourceRefs: []source.SourceRef{ref}},
|
||||||
{Spell: "Cure Wounds", Caster: "aria", SourceRefs: []source.SourceRef{ref}},
|
{Spell: "Cure Wounds", Caster: "aria", SourceRefs: []source.SourceRef{ref}},
|
||||||
}}
|
}}
|
||||||
result, err := newNormalizer(t).Normalize(context.Background(), normalizeRequestWithSource(input, doc))
|
result, err := runNormalize(newNormalizer(t), context.Background(), normalizeRequestWithSource(input, doc))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Normalize() error = %v, want nil", err)
|
t.Fatalf("Normalize() error = %v, want nil", err)
|
||||||
}
|
}
|
||||||
@@ -445,8 +445,16 @@ func newNormalizer(t *testing.T) *Normalizer {
|
|||||||
return normalizer
|
return normalizer
|
||||||
}
|
}
|
||||||
|
|
||||||
func normalizeRequest(value dnd.SpellList) contracts.TypedNormalizeRequest[dnd.SpellList] {
|
func runNormalize(normalizer *Normalizer, ctx context.Context, req contracts.NormalizeInput[dnd.SpellList]) (pipeline.OperationResult[dnd.SpellList], error) {
|
||||||
return contracts.TypedNormalizeRequest[dnd.SpellList]{
|
return pipeline.ExecuteOperation(ctx, pipeline.ExecutionContext[dnd.SpellList]{Assess: func(context.Context, dnd.SpellList) (contracts.CandidateAssessment, error) {
|
||||||
|
return contracts.CandidateAssessment{}, nil
|
||||||
|
}}, func(ctx context.Context) (contracts.PreparedOperation[dnd.SpellList], error) {
|
||||||
|
return normalizer.PrepareNormalize(ctx, req)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func normalizeRequest(value dnd.SpellList) contracts.NormalizeInput[dnd.SpellList] {
|
||||||
|
return contracts.NormalizeInput[dnd.SpellList]{
|
||||||
LaneID: "spells",
|
LaneID: "spells",
|
||||||
MergeOutput: contracts.MergeArtifact[dnd.SpellList]{
|
MergeOutput: contracts.MergeArtifact[dnd.SpellList]{
|
||||||
LaneID: "spells",
|
LaneID: "spells",
|
||||||
@@ -457,7 +465,7 @@ func normalizeRequest(value dnd.SpellList) contracts.TypedNormalizeRequest[dnd.S
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func normalizeRequestWithSource(value dnd.SpellList, doc *source.SourceDocument) contracts.TypedNormalizeRequest[dnd.SpellList] {
|
func normalizeRequestWithSource(value dnd.SpellList, doc *source.SourceDocument) contracts.NormalizeInput[dnd.SpellList] {
|
||||||
request := normalizeRequest(value)
|
request := normalizeRequest(value)
|
||||||
request.Source = doc
|
request.Source = doc
|
||||||
return request
|
return request
|
||||||
|
|||||||
Reference in New Issue
Block a user