package spoken_word import ( "context" "errors" "os" "path/filepath" "strings" "testing" ck "gitea.maximumdirect.net/eric/ConfigKit" "gitea.maximumdirect.net/eric/audita/internal/core/config" "gitea.maximumdirect.net/eric/audita/internal/core/schema" "gitea.maximumdirect.net/eric/audita/internal/framework/contracts" "gitea.maximumdirect.net/eric/audita/internal/framework/proposal_generation" "gitea.maximumdirect.net/eric/audita/internal/framework/proposals" "gitea.maximumdirect.net/eric/audita/internal/framework/validators" "gitea.maximumdirect.net/eric/audita/internal/prompts" ) type fakeLLMClient struct { responses []proposal_generation.StructuredCorrectionSet err error calls []contracts.StructuredCompletionRequest } func (f *fakeLLMClient) CompleteStructured(ctx context.Context, req contracts.StructuredCompletionRequest, out any) (contracts.StructuredCompletionResponse, error) { _ = ctx f.calls = append(f.calls, req) if f.err != nil { return contracts.StructuredCompletionResponse{}, f.err } target, ok := out.(*proposal_generation.StructuredCorrectionSet) if !ok { return contracts.StructuredCompletionResponse{}, errors.New("unexpected output type") } if len(f.responses) == 0 { return contracts.StructuredCompletionResponse{}, errors.New("unexpected call") } *target = f.responses[0] f.responses = f.responses[1:] return contracts.StructuredCompletionResponse{}, nil } func tinyTranscript() *schema.Transcript { return &schema.Transcript{Segments: []schema.Segment{ {ID: 1, Speaker: "A", Start: 0, End: 1, Text: "I I think, you know, we should go.", Categories: []string{"session"}}, }} } func tinyGlossary() *schema.Glossary { return &schema.Glossary{Entries: []schema.GlossaryEntry{ {Name: "Jesters", Aliases: []string{"Jester"}, Category: "faction", Summary: "Guild members", Plural: "Jesters"}, }} } func TestSpokenWordModuleReplacementPolicy(t *testing.T) { m, err := New() if err != nil { t.Fatalf("New error: %v", err) } if m.ReplacementPolicy() != proposals.ReplacementPolicyRequireUnique { t.Fatalf("unexpected replacement policy: %q", m.ReplacementPolicy()) } } func TestSpokenWordModuleValidatorChain(t *testing.T) { m, err := New() if err != nil { t.Fatalf("New error: %v", err) } got := make([]string, 0, len(m.Validators())) for _, v := range m.Validators() { got = append(got, v.Name()) } want := []string{ "proposal_shape", "no_effect", "original_text_presence", "confidence_threshold", "protected_terms", "non_empty_resulting_segment", "editorial_review", "meaning_reversal_review", } if strings.Join(got, ",") != strings.Join(want, ",") { t.Fatalf("unexpected validator chain\n got: %v\nwant: %v", got, want) } } func TestSpokenWordModuleProposeMapsCorrectionsAndWritesDiagnostics(t *testing.T) { secret := "spoken-word-secret" client := &fakeLLMClient{ responses: []proposal_generation.StructuredCorrectionSet{ { Corrections: []proposal_generation.StructuredCorrectionProposal{ {SegmentReference: 1, OriginalText: "I I think", CorrectedText: "I think", Confidence: 0.94}, }, }, }, } t.Setenv("MODULE_SECRET", secret) loaded, loadErr := config.Load(ck.Bytes("config.yml", []byte("version: 2\nbackends:\n offline:\n endpoint: https://offline.invalid/v1\n credential: MODULE_SECRET\nllm:\n proposal_profile: base\n")), "") if loadErr != nil { t.Fatal(loadErr) } cfg := loaded.Config m, err := New() if err != nil { t.Fatalf("New error: %v", err) } diagDir := t.TempDir() out, err := m.Propose(context.Background(), contracts.ProposalRequest{ ExecutionContext: contracts.ExecutionContext{ Config: &cfg, WorkingTranscript: tinyTranscript(), Glossary: tinyGlossary(), DiagnosticsDir: diagDir, }, RunSpec: contracts.ModuleRunSpec{ ModuleKey: "spoken_word", InstanceName: "spoken_word", ReplacementPolicy: proposals.ReplacementPolicyRequireUnique, }, LLMClient: client, }) if err != nil { t.Fatalf("Propose error: %v", err) } if len(client.calls) != 1 || client.calls[0].PromptID != prompts.PromptIDModuleSpokenWordProposal { t.Fatalf("expected one spoken_word:proposal call, got %+v", client.calls) } call := client.calls[0] if call.Schema != "correction_set" || call.Context.Role != "proposal" || !strings.Contains(call.Variables["SectionJSON"], "section_index") || !strings.Contains(call.Variables["GlossaryJSON"], "Jesters") { t.Fatalf("proposal mapping incomplete: %+v", call) } if len(out.Proposals) != 1 || out.Proposals[0].CorrectedText != "I think" { t.Fatalf("unexpected proposals: %+v", out) } diagFiles, globErr := filepath.Glob(filepath.Join(diagDir, "spoken_word", "*proposal*response-payload.json")) if globErr != nil { t.Fatalf("glob error: %v", globErr) } if len(diagFiles) == 0 { t.Fatalf("expected diagnostics response payload under %s", filepath.Join(diagDir, "spoken_word")) } for _, f := range diagFiles { raw, readErr := os.ReadFile(f) if readErr != nil { t.Fatalf("read diag %q: %v", f, readErr) } if strings.Contains(string(raw), secret) { t.Fatalf("secret leaked in diagnostics: %s", string(raw)) } } } func TestSpokenWordConfidenceThresholdIsUsed(t *testing.T) { module, err := New() if err != nil { t.Fatalf("New error: %v", err) } var thresholdValidator contracts.Validator for _, v := range module.Validators() { if v.Name() == "confidence_threshold" { thresholdValidator = v break } } if thresholdValidator == nil { t.Fatal("expected confidence_threshold validator") } cfg := config.Config{} cfg.Thresholds.SpokenWord = 0.95 result, err := thresholdValidator.Validate(context.Background(), validators.Request{ Config: &cfg, ModuleKey: "spoken_word", CandidateProposal: []proposals.EnrichedCorrectionProposal{ { CorrectionProposal: proposals.CorrectionProposal{ TargetSegmentID: 1, OriginalText: "I I think", CorrectedText: "I think", Confidence: 0.94, }, ProposalMetadata: proposals.ProposalMetadata{ProposalIndex: 0, ModuleKey: "spoken_word", ModuleInstance: "spoken_word"}, }, }, }) if err != nil { t.Fatalf("validate error: %v", err) } if len(result.Decisions) != 1 { t.Fatalf("expected one decision, got %+v", result.Decisions) } if result.Decisions[0].Approved { t.Fatalf("expected rejection under spoken_word threshold, got %+v", result.Decisions[0]) } if result.Decisions[0].ReasonCode != validators.ReasonLowConfidence { t.Fatalf("expected low confidence reason, got %+v", result.Decisions[0]) } } func TestSpokenWordProtectedGlossaryTermBehaviorRejectsUnsafeCorrection(t *testing.T) { module, err := New() if err != nil { t.Fatalf("New error: %v", err) } var protectedValidator contracts.Validator for _, v := range module.Validators() { if v.Name() == "protected_terms" { protectedValidator = v break } } if protectedValidator == nil { t.Fatal("expected protected_terms validator") } working := &schema.Transcript{Segments: []schema.Segment{ {ID: 1, Speaker: "A", Start: 0, End: 1, Text: "Jesters are here."}, }} glossary := &schema.Glossary{Entries: []schema.GlossaryEntry{ {Name: "Jesters", Category: "faction", Summary: "Faction"}, }} result, err := protectedValidator.Validate(context.Background(), validators.Request{ WorkingTranscript: working, Glossary: glossary, ModuleKey: "spoken_word", CandidateProposal: []proposals.EnrichedCorrectionProposal{ { CorrectionProposal: proposals.CorrectionProposal{ TargetSegmentID: 1, OriginalText: "Jesters", CorrectedText: "gestures", Confidence: 0.99, }, ProposalMetadata: proposals.ProposalMetadata{ProposalIndex: 0, ModuleKey: "spoken_word", ModuleInstance: "spoken_word"}, }, }, }) if err != nil { t.Fatalf("validate error: %v", err) } if len(result.Decisions) != 1 { t.Fatalf("expected one decision, got %+v", result.Decisions) } if result.Decisions[0].Approved { t.Fatalf("expected protected-term rejection, got %+v", result.Decisions[0]) } if result.Decisions[0].ReasonCode != validators.ReasonProtectedGlossaryTerm { t.Fatalf("expected protected glossary-term reason, got %+v", result.Decisions[0]) } }