package locationregistry import ( "context" "encoding/json" "strings" "testing" "gitea.maximumdirect.net/eric/notarius/assets" "go.maximumdirect.net/configkit" "go.maximumdirect.net/promptkit" "gitea.maximumdirect.net/eric/notarius/internal/core/source" "gitea.maximumdirect.net/eric/notarius/internal/framework/contracts" "gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline" "gitea.maximumdirect.net/eric/notarius/internal/modules/dnd" ) type recordingLocationNormalizerClient struct { response string responses []string err error errors []error requests []promptkit.GenerateRequest onGenerate func(context.Context, promptkit.GenerateRequest) } func (c *recordingLocationNormalizerClient) Generate(ctx context.Context, request promptkit.GenerateRequest) (*promptkit.GenerateResponse, error) { index := len(c.requests) c.requests = append(c.requests, request) if c.onGenerate != nil { c.onGenerate(ctx, request) } if c.err != nil { return nil, c.err } if index < len(c.errors) && c.errors[index] != nil { return nil, c.errors[index] } response := c.response if index < len(c.responses) { response = c.responses[index] } if response == "" { response = `{"duplicate_groups":[]}` } return &promptkit.GenerateResponse{Content: response}, nil } type normalizationTestInput struct { Source *source.SourceDocument LaneID string MergeOutput contracts.MergeArtifact[dnd.LocationRegistry] SourceInput contracts.LLMInputMaterial SessionID string References contracts.ReferenceSet LLMProfile string Metadata map[string]any Execution pipeline.ExecutionContext[dnd.LocationRegistry] } type normalizerFixture struct { *Normalizer engine *promptkit.Engine } func newNormalizer(t *testing.T, client promptkit.LLMClient) *normalizerFixture { t.Helper() type settings struct { PromptKit promptkit.Settings `yaml:"promptkit"` } definition := promptkit.Configure(configkit.Definition[settings]{}, func(value *settings) *promptkit.Settings { return &value.PromptKit }) filesystem := configkit.NewFilesystem("Notarius assets", assets.FS()) result, err := configkit.LoadLayered(configkit.Input{Source: configkit.Bytes("normalizer fixture", []byte(`promptkit: profiles: - id: location-normalize-test endpoint: https://example.invalid/v1 model: normalization-test-model api_key_required: false `))}, configkit.LayeredOptions[settings]{Definition: definition, Underlays: []configkit.Underlay{ promptkit.BundledOpenRouterUnderlay(), {Inputs: []configkit.Input{{Source: filesystem.File("notarius.yml"), Imports: true, Filesystem: &configkit.FilesystemContext{FS: &filesystem, Root: "."}}}}, }}) if err != nil { t.Fatal(err) } engine, err := promptkit.NewFromConfig(result, promptkit.RuntimeConfig{}, promptkit.WithLLMClient(client)) if err != nil { t.Fatal(err) } normalizer, err := New(Options{}) if err != nil { t.Fatal(err) } return &normalizerFixture{Normalizer: normalizer, engine: engine} } func runNormalization(fixture *normalizerFixture, ctx context.Context, req normalizationTestInput) (pipeline.OperationResult[dnd.LocationRegistry], error) { op := req.Execution execution := pipeline.ExecutionContext[dnd.LocationRegistry]{Engine: fixture.engine, Controls: op.Controls, Assess: op.Assess, Execution: op.Execution, ReuseEligible: op.ReuseEligible, Cache: op.Cache} input := contracts.NormalizeInput[dnd.LocationRegistry]{Source: req.Source, LaneID: req.LaneID, MergeOutput: req.MergeOutput, SourceInput: req.SourceInput, SessionID: req.SessionID, References: req.References, LLMProfile: req.LLMProfile, Metadata: req.Metadata} return pipeline.ExecuteOperation(ctx, execution, func(ctx context.Context) (contracts.PreparedOperation[dnd.LocationRegistry], error) { return fixture.Normalizer.PrepareNormalize(ctx, input) }) } func renderedInput(t *testing.T, request promptkit.GenerateRequest, name string) []byte { t.Helper() key := "candidate_number_range" if name == "transcript" { key = "windows" } for _, message := range request.Prompt.Messages { start := strings.Index(message.Content, "{") if start < 0 { continue } content := []byte(message.Content[start:]) var fields map[string]json.RawMessage if json.Unmarshal(content, &fields) == nil && fields[key] != nil { return content } } t.Fatalf("missing rendered %s input", name) return nil } func normalizeRequest(value dnd.LocationRegistry) normalizationTestInput { return normalizationTestInput{ Source: &source.SourceDocument{}, LLMProfile: "location-normalize-test", Execution: pipeline.ExecutionContext[dnd.LocationRegistry]{Controls: contracts.ProducerControls{Mode: contracts.ModeStrict, Cache: contracts.CacheBypass}, ReuseEligible: true, Assess: func(context.Context, dnd.LocationRegistry) (contracts.CandidateAssessment, error) { return contracts.CandidateAssessment{}, nil }}, MergeOutput: contracts.MergeArtifact[dnd.LocationRegistry]{Value: value}, } } func normalizeRequestWithSource(value dnd.LocationRegistry, doc *source.SourceDocument) normalizationTestInput { request := normalizeRequest(value) request.Source = doc return request } func semanticDocument() *source.SourceDocument { return &source.SourceDocument{ID: "location-session", Units: []source.SourceUnit{{ID: 10, Kind: "speech", Text: "The old mill is the Greencloak's refuge."}, {ID: 20, Kind: "speech", Text: "The mill stands on the northern road."}, {ID: 30, Kind: "speech", Text: "The tavern is beside the mill."}, {ID: 40, Kind: "speech", Text: "The mill's cellar is flooded."}}} } func hasDiagnostic(diagnostics []contracts.ProducerDiagnostic, reason string, disposition contracts.DiagnosticDisposition) bool { for _, diagnostic := range diagnostics { if diagnostic.ReasonCode == reason && diagnostic.Disposition == disposition { return true } } return false }