Files
audita/internal/validators/registry_test.go
Eric Rakestraw da5b8913d1
All checks were successful
ci/woodpecker/tag/release Pipeline was successful
Migrate Audita to the v4 module path
2026-11-17 19:57:22 +00:00

172 lines
5.6 KiB
Go

package validators
import (
"context"
frameworkvalidators "gitea.maximumdirect.net/eric/audita/v4/internal/framework/validators"
"testing"
"gitea.maximumdirect.net/eric/audita/v4/internal/core/glossary"
"gitea.maximumdirect.net/eric/audita/v4/internal/core/modulecatalog"
"gitea.maximumdirect.net/eric/audita/v4/internal/core/schema"
"gitea.maximumdirect.net/eric/audita/v4/internal/framework/proposals"
validatormetadata "gitea.maximumdirect.net/eric/audita/v4/internal/validators/metadata"
)
func TestRegisteredValidatorsUseDocumentedRoles(t *testing.T) {
registry := NewBuiltInRegistry()
for _, tc := range []struct {
key string
role validatormetadata.ExecutionClass
}{
{KeyProposalShape, validatormetadata.ExecutionClassDeterministic},
{KeyOriginalTextPresence, validatormetadata.ExecutionClassDeterministic},
{KeyNonEmptyResultingSegment, validatormetadata.ExecutionClassDeterministic},
{KeyNoEffect, validatormetadata.ExecutionClassDeterministic},
{KeyProtectedTerms, validatormetadata.ExecutionClassDeterministic},
{KeySpokenFormPlausibility, validatormetadata.ExecutionClassLLMBacked},
{KeyMeaningReversalReview, validatormetadata.ExecutionClassLLMBacked},
{KeyEditorialReview, validatormetadata.ExecutionClassLLMBacked},
} {
t.Run(tc.key, func(t *testing.T) {
v, err := registry.MustBuild(tc.key)
if err != nil {
t.Fatal(err)
}
if v.Name() != tc.key || validatormetadata.ClassOf(v) != tc.role {
t.Fatalf("registered name=%q role=%q, want %q and %q", v.Name(), validatormetadata.ClassOf(v), tc.key, tc.role)
}
})
}
}
func TestRegistryProtectedTermsUsesNonGlossaryStageBehavior(t *testing.T) {
r := NewBuiltInRegistry()
v, err := r.MustBuild(KeyProtectedTerms)
if err != nil {
t.Fatalf("must build protected_terms: %v", err)
}
req := frameworkvalidators.Request{
ModuleKey: "glossary",
Glossary: []glossary.Entry{{Collection: "party", ID: "jesters", Name: "Jesters", Category: "faction", Description: "Protected"}},
CandidateProposal: []proposals.EnrichedCorrectionProposal{
{
CorrectionProposal: proposals.CorrectionProposal{
TargetSegmentID: 1,
OriginalText: "Jesters",
CorrectedText: "Gestures",
},
ProposalMetadata: proposals.ProposalMetadata{
ProposalIndex: 0,
ModuleKey: "glossary",
},
},
},
}
result, err := v.Validate(context.Background(), req)
if err != nil {
t.Fatalf("validate protected_terms: %v", err)
}
if len(result.Decisions) != 1 {
t.Fatalf("expected one decision, got %+v", result.Decisions)
}
if !result.Decisions[0].Approved {
t.Fatalf("expected glossary module proposal to be approved by non-glossary-stage protected_terms validator, got %+v", result.Decisions[0])
}
}
func TestResolveBuiltInChainUsesGlossaryStageProtectedTermsBehavior(t *testing.T) {
chain, err := ResolveBuiltInChain("glossary", NewBuiltInRegistry())
if err != nil {
t.Fatalf("resolve glossary chain: %v", err)
}
var v frameworkvalidators.Validator
for _, candidate := range chain {
if candidate.Name() == KeyProtectedTerms {
v = candidate
break
}
}
if v == nil {
t.Fatalf("expected protected_terms validator in glossary chain")
}
req := frameworkvalidators.Request{
WorkingTranscript: &schema.Transcript{Segments: []schema.Segment{{ID: 1, Text: "Jesters"}}},
ReplacementPolicy: proposals.ReplacementPolicyReplaceAll,
ModuleKey: "glossary",
ProtectedTerms: glossary.Vocabulary([]glossary.Entry{{Collection: "party", ID: "jesters", Name: "Jesters"}}, nil),
Glossary: []glossary.Entry{{Collection: "party", ID: "jesters", Name: "Jesters", Category: "faction", Description: "Protected"}},
CandidateProposal: []proposals.EnrichedCorrectionProposal{
{
CorrectionProposal: proposals.CorrectionProposal{
TargetSegmentID: 1,
OriginalText: "Jesters",
CorrectedText: "Gestures",
},
ProposalMetadata: proposals.ProposalMetadata{
ProposalIndex: 0,
ModuleKey: "glossary",
},
},
},
}
result, err := v.Validate(context.Background(), req)
if err != nil {
t.Fatalf("validate protected_terms: %v", err)
}
if len(result.Decisions) != 1 {
t.Fatalf("expected one decision, got %+v", result.Decisions)
}
if result.Decisions[0].Approved {
t.Fatalf("expected glossary-stage protected_terms rejection, got %+v", result.Decisions[0])
}
}
func TestBuiltInRegistryUnknownKeyFails(t *testing.T) {
r := NewBuiltInRegistry()
if _, err := r.MustBuild("missing"); err == nil {
t.Fatalf("expected unknown validator key error")
}
}
func TestBuiltInChainKeysResolveForProductionModules(t *testing.T) {
for _, moduleKey := range modulecatalog.SupportedKeys() {
keys, err := BuiltInChainKeys(moduleKey)
if err != nil {
t.Fatalf("resolve keys for %q: %v", moduleKey, err)
}
if len(keys) == 0 {
t.Fatalf("expected non-empty chain for %q", moduleKey)
}
}
}
func TestResolveBuiltInChainUsesRegisteredKeys(t *testing.T) {
r := NewBuiltInRegistry()
for _, moduleKey := range modulecatalog.SupportedKeys() {
chain, err := ResolveBuiltInChain(moduleKey, r)
if err != nil {
t.Fatalf("resolve chain for %q: %v", moduleKey, err)
}
if len(chain) == 0 {
t.Fatalf("expected non-empty chain for %q", moduleKey)
}
for _, v := range chain {
if v == nil {
t.Fatalf("nil validator in %q chain", moduleKey)
}
}
}
}
func TestBuiltInChainUnknownModuleFails(t *testing.T) {
if _, err := BuiltInChainKeys("unknown"); err == nil {
t.Fatalf("expected unknown module chain failure")
}
if _, err := ResolveBuiltInChain("unknown", NewBuiltInRegistry()); err == nil {
t.Fatalf("expected unknown module chain failure")
}
}