Files
audita/internal/framework/contracts/contracts_test.go

145 lines
5.0 KiB
Go
Raw Normal View History

package contracts
import (
"context"
"encoding/json"
"reflect"
"strings"
"testing"
2026-11-17 19:57:22 +00:00
"gitea.maximumdirect.net/eric/audita/v4/internal/core/chunking"
"gitea.maximumdirect.net/eric/audita/v4/internal/core/config"
"gitea.maximumdirect.net/eric/audita/v4/internal/framework/proposals"
"gitea.maximumdirect.net/eric/audita/v4/internal/framework/validators"
)
type fakeLLMClient struct{}
func (f *fakeLLMClient) CompleteStructured(ctx context.Context, req StructuredCompletionRequest, out any) (StructuredCompletionResponse, error) {
_ = ctx
_ = req
if out != nil {
switch target := out.(type) {
case *map[string]any:
*target = map[string]any{"ok": true}
}
}
return StructuredCompletionResponse{Content: json.RawMessage(`{"ok":true}`)}, nil
}
type fakeValidator struct{}
func (f *fakeValidator) Name() string { return "fake-validator" }
func (f *fakeValidator) Validate(ctx context.Context, req validators.Request) (validators.Result, error) {
_ = ctx
decisions := make([]validators.Decision, len(req.CandidateProposal))
for i, proposal := range req.CandidateProposal {
decisions[i] = validators.Decision{ProposalIndex: proposal.ProposalIndex, Approved: true, ReasonCode: validators.ReasonApproved, Message: "approved"}
}
return validators.Result{ValidatorName: f.Name(), Decisions: decisions}, nil
}
type fakeModule struct{}
func (f *fakeModule) Key() string { return "fake" }
func (f *fakeModule) ReplacementPolicy() proposals.ReplacementPolicy {
return proposals.ReplacementPolicyRequireUnique
}
func (f *fakeModule) Validators() []validators.Validator {
return []validators.Validator{&fakeValidator{}}
}
func (f *fakeModule) Propose(ctx context.Context, req ProposalRequest) (ProposalResult, error) {
_ = ctx
_ = req
return ProposalResult{
Proposals: []proposals.CorrectionProposal{
{TargetSegmentID: 1, OriginalText: "a", CorrectedText: "b"},
},
}, nil
}
func TestInterfaceContractsCompileWithFakes(t *testing.T) {
var _ StructuredLLMClient = (*fakeLLMClient)(nil)
var _ validators.Validator = (*fakeValidator)(nil)
var _ TranscriptModule = (*fakeModule)(nil)
module := &fakeModule{}
if got := module.Key(); got != "fake" {
t.Fatalf("unexpected module key: %q", got)
}
proposalResult, err := module.Propose(context.Background(), ProposalRequest{})
if err != nil {
t.Fatalf("unexpected propose error: %v", err)
}
if len(proposalResult.Proposals) != 1 {
t.Fatalf("expected one proposal, got %d", len(proposalResult.Proposals))
}
}
func TestResolveModuleRunSpecs(t *testing.T) {
id := "named"
cfg := &config.Config{Pipeline: config.Pipeline{Stages: []config.Stage{{Module: "glossary", ID: &id}, {Module: "grammar"}, {Module: "glossary"}}}}
specs := ResolveModuleRunSpecs(cfg)
if len(specs) != 3 || specs[0].Index != 0 || specs[0].ID == nil || *specs[0].ID != id || specs[0].InstanceName != "glossary_1" || specs[1].InstanceName != "grammar_2" || specs[2].InstanceName != "glossary_3" {
t.Fatalf("unexpected specs: %+v", specs)
}
if len(ResolveModuleRunSpecs(nil)) != 0 {
t.Fatal("nil configuration has run specs")
}
}
2026-11-26 18:14:27 +00:00
func TestResolveGlossaryInstructions(t *testing.T) {
cfg := &config.Config{
GlossaryCollections: map[string]config.GlossaryCollection{
"one": {Instruction: "First {{literal}}"}, "two": {Instruction: "Second"}, "protected": {Instruction: "Protection only"}, "empty": {},
2026-11-26 18:14:27 +00:00
},
Pipeline: config.Pipeline{Stages: []config.Stage{
{Module: "grammar", Glossaries: []string{"two", "empty", "one"}, ProtectedTerms: []string{"protected"}},
{Module: "grammar", Glossaries: []string{"two", "empty", "one"}, ProtectedTerms: []string{"protected"}},
2026-11-26 18:14:27 +00:00
{Module: "grammar", Glossaries: []string{"one"}, ProtectedTerms: []string{"protected"}},
}},
}
specs := ResolveModuleRunSpecs(cfg)
want := []GlossaryInstruction{{Collection: "two", Text: "Second"}, {Collection: "empty"}, {Collection: "one", Text: "First {{literal}}"}}
2026-11-26 18:14:27 +00:00
for _, spec := range specs[:2] {
if !reflect.DeepEqual(spec.GlossaryInstructions, want) {
t.Fatalf("instructions = %+v, want %+v", spec.GlossaryInstructions, want)
}
}
if !reflect.DeepEqual(specs[2].GlossaryInstructions, want[2:]) {
2026-11-26 18:14:27 +00:00
t.Fatalf("specific selection = %+v", specs[2].GlossaryInstructions)
}
if err := specs[0].PrepareGlossary(); err != nil {
t.Fatal(err)
}
block := specs[0].GlossaryVariables.InstructionsBlock
if !strings.Contains(block, "two") || !strings.Contains(block, "one") || !strings.Contains(block, "First {{literal}}") || strings.Index(block, "Second") > strings.Index(block, "First") || strings.Contains(block, "empty") || strings.Contains(block, "Protection only") {
t.Fatalf("instruction block = %q", block)
}
2026-11-26 18:14:27 +00:00
}
func TestSectionMetadataFromSection(t *testing.T) {
section := chunking.Section{
Index: 3,
StartSegmentID: 10,
EndSegmentID: 14,
EstimatedTokens: 123,
}
got := SectionMetadataFromSection(section)
want := SectionMetadata{
Index: 3,
StartSegmentID: 10,
EndSegmentID: 14,
EstimatedTokens: 123,
}
if !reflect.DeepEqual(got, want) {
t.Fatalf("unexpected section metadata: got=%+v want=%+v", got, want)
}
}