2026-05-13 18:36:46 +00:00
|
|
|
package validators
|
|
|
|
|
|
|
|
|
|
import (
|
2026-05-14 00:28:28 +00:00
|
|
|
"context"
|
2026-11-16 14:11:02 +00:00
|
|
|
frameworkvalidators "gitea.maximumdirect.net/eric/audita/v3/internal/framework/validators"
|
2026-05-13 18:36:46 +00:00
|
|
|
"testing"
|
2026-05-14 00:28:28 +00:00
|
|
|
|
2026-11-10 18:13:12 +00:00
|
|
|
"gitea.maximumdirect.net/eric/audita/v3/internal/core/glossary"
|
2026-10-11 01:40:38 +00:00
|
|
|
"gitea.maximumdirect.net/eric/audita/v3/internal/core/modulecatalog"
|
2026-11-16 04:09:25 +00:00
|
|
|
"gitea.maximumdirect.net/eric/audita/v3/internal/core/schema"
|
2026-10-11 01:40:38 +00:00
|
|
|
"gitea.maximumdirect.net/eric/audita/v3/internal/framework/proposals"
|
|
|
|
|
validatormetadata "gitea.maximumdirect.net/eric/audita/v3/internal/validators/metadata"
|
2026-05-13 18:36:46 +00:00
|
|
|
)
|
|
|
|
|
|
2026-11-16 15:19:59 +00:00
|
|
|
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},
|
2026-05-13 18:36:46 +00:00
|
|
|
} {
|
2026-11-16 15:19:59 +00:00
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
})
|
2026-05-23 18:02:53 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-14 00:28:28 +00:00
|
|
|
func TestRegistryProtectedTermsUsesNonGlossaryStageBehavior(t *testing.T) {
|
|
|
|
|
r := NewBuiltInRegistry()
|
|
|
|
|
v, err := r.MustBuild(KeyProtectedTerms)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("must build protected_terms: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-16 14:11:02 +00:00
|
|
|
req := frameworkvalidators.Request{
|
2026-05-14 00:28:28 +00:00
|
|
|
ModuleKey: "glossary",
|
2026-11-10 18:13:12 +00:00
|
|
|
Glossary: []glossary.Entry{{Collection: "party", ID: "jesters", Name: "Jesters", Category: "faction", Description: "Protected"}},
|
2026-05-14 00:28:28 +00:00
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-16 14:11:02 +00:00
|
|
|
var v frameworkvalidators.Validator
|
2026-05-14 00:28:28 +00:00
|
|
|
for _, candidate := range chain {
|
|
|
|
|
if candidate.Name() == KeyProtectedTerms {
|
|
|
|
|
v = candidate
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if v == nil {
|
|
|
|
|
t.Fatalf("expected protected_terms validator in glossary chain")
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-16 14:11:02 +00:00
|
|
|
req := frameworkvalidators.Request{
|
2026-11-16 04:09:25 +00:00
|
|
|
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"}},
|
2026-05-14 00:28:28 +00:00
|
|
|
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])
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-13 18:36:46 +00:00
|
|
|
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) {
|
2026-05-23 17:39:13 +00:00
|
|
|
for _, moduleKey := range modulecatalog.SupportedKeys() {
|
2026-05-13 18:36:46 +00:00
|
|
|
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()
|
2026-05-23 17:39:13 +00:00
|
|
|
for _, moduleKey := range modulecatalog.SupportedKeys() {
|
2026-05-13 18:36:46 +00:00
|
|
|
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")
|
|
|
|
|
}
|
|
|
|
|
}
|