package preflight import ( "context" "errors" "testing" "gitea.maximumdirect.net/eric/notarius/internal/framework/contracts" "gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline" "go.maximumdirect.net/configkit" "go.maximumdirect.net/promptkit" ) func TestDiscoverTargetsUsesInitialPromptAndDeduplicates(t *testing.T) { engine, err := promptkit.NewEngine(promptkit.Config{Sources: promptkit.ConfigurationSources{Underlays: []configkit.Underlay{{Inputs: []configkit.Input{promptkit.SettingsInput(configkit.Bytes("targets", []byte(`backends: - id: shared endpoint: https://example.invalid profiles: - id: primary backend: shared - id: secondary backend: shared - id: endpoint-only endpoint: https://example.invalid - id: unused backend: shared prompts: - id: initial default_profile: primary messages: [{role: user, content: 'Initial'}] output: {format: text, validation_mode: none} - id: correction default_profile: unused messages: [{role: user, content: 'Correct'}] output: {format: text, validation_mode: none} - id: validator default_profile: secondary messages: [{role: user, content: 'Assess'}] output: {format: text, validation_mode: none} `)))}}}}}, promptkit.WithBundledOpenRouter(false), promptkit.WithLLMClient(noGeneration{t})) if err != nil { t.Fatal(err) } chunkers := pipeline.NewChunkerRegistry() err = chunkers.RegisterWithSpec(pipeline.ModuleSpec{Key: "chunk", Stage: pipeline.StageChunk, ExecutionClass: contracts.ExecutionClassLLMBacked, Prompts: []string{"initial", "correction"}, InitialPrompts: []string{"initial"}}, func() (contracts.Chunker, error) { t.Fatal("constructed chunker"); return nil, nil }) if err != nil { t.Fatal(err) } catalog := pipeline.ModuleCatalog{Chunkers: chunkers} resolved := pipeline.ResolvedPipeline{Chunk: pipeline.Binding("chunk"), ChunkExecutionClass: contracts.ExecutionClassLLMBacked, ValidatorChains: []pipeline.ResolvedValidatorChain{{Stage: pipeline.StageChunk, ModuleKey: "chunk", Validators: []pipeline.ResolvedValidator{{Binding: pipeline.Binding("validator"), ExecutionClass: contracts.ExecutionClassLLMBacked, Prompts: []string{"validator"}}}}}} targets, err := DiscoverTargets(context.Background(), resolved, catalog, engine) if err != nil { t.Fatal(err) } if len(targets.Profiles) != 2 || targets.Profiles[0].ID != "primary" || targets.Profiles[1].ID != "secondary" || len(targets.Access) != 1 || targets.Access[0].BackendID != "shared" || len(targets.Access[0].Bindings) != 2 { t.Fatalf("targets = %+v", targets) } resolved.ValidatorChains[0].Validators[0].Binding.LLMProfile = "primary" targets, err = DiscoverTargets(context.Background(), resolved, catalog, engine) if err != nil || len(targets.Profiles) != 1 || len(targets.Profiles[0].Bindings) != 2 { t.Fatalf("shared profile = %+v, %v", targets, err) } resolved.ValidatorChains[0].Validators[0].Binding.LLMProfile = "" resolved.Chunk.LLMProfile = "endpoint-only" targets, err = DiscoverTargets(context.Background(), resolved, catalog, engine) if err != nil || len(targets.Access) != 2 || targets.Access[0].BackendID != "" || targets.Access[0].ProfileID != "endpoint-only" { t.Fatalf("endpoint target = %+v, %v", targets, err) } resolved.Chunk.LLMProfile = "missing" if _, err := DiscoverTargets(context.Background(), resolved, catalog, engine); !errors.Is(err, promptkit.ErrProfileNotFound) { t.Fatalf("missing profile cause = %v", err) } resolved.Chunk.LLMProfile = "" resolved.Chunk.Module = "dynamic" if _, err := DiscoverTargets(context.Background(), resolved, catalog, engine); err == nil { t.Fatal("unresolved target was silently omitted") } }