127 lines
3.6 KiB
Go
127 lines
3.6 KiB
Go
package contracts
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"reflect"
|
|
"testing"
|
|
|
|
"gitea.maximumdirect.net/eric/audita/internal/core/chunking"
|
|
"gitea.maximumdirect.net/eric/audita/internal/framework/proposals"
|
|
"gitea.maximumdirect.net/eric/audita/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 ValidationRequest) (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() []Validator {
|
|
return []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", Confidence: 0.9},
|
|
},
|
|
}, nil
|
|
}
|
|
|
|
func TestInterfaceContractsCompileWithFakes(t *testing.T) {
|
|
var _ StructuredLLMClient = (*fakeLLMClient)(nil)
|
|
var _ 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) {
|
|
specs, err := ResolveModuleRunSpecs([]string{"glossary", "homophones", "glossary", "spoken_word", "grammar"})
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
|
|
expected := []ModuleRunSpec{
|
|
{ModuleKey: "glossary", InstanceName: "glossary_1"},
|
|
{ModuleKey: "homophones", InstanceName: "homophones"},
|
|
{ModuleKey: "glossary", InstanceName: "glossary_2"},
|
|
{ModuleKey: "spoken_word", InstanceName: "spoken_word"},
|
|
{ModuleKey: "grammar", InstanceName: "grammar"},
|
|
}
|
|
|
|
if !reflect.DeepEqual(specs, expected) {
|
|
t.Fatalf("unexpected specs\nexpected: %+v\nactual: %+v", expected, specs)
|
|
}
|
|
}
|
|
|
|
func TestResolveModuleRunSpecsRejectsEmptyKey(t *testing.T) {
|
|
_, err := ResolveModuleRunSpecs([]string{"glossary", ""})
|
|
if err == nil {
|
|
t.Fatal("expected error for empty module key")
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|