Files
audita/internal/framework/preflight/plan_test.go

74 lines
3.8 KiB
Go
Raw Normal View History

package preflight
import (
"context"
"errors"
"slices"
"strings"
"testing"
"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/core/schema"
)
func TestBuildPlanResolvesProfilesAndChunks(t *testing.T) {
proposal, validation, repair, unused := "proposal", "validation", "repair", "unused"
cfg := &config.Config{
Chunking: config.Chunking{MaxSectionTokens: 100},
Pipeline: config.Pipeline{Stages: []config.Stage{
{Module: "grammar", LLM: config.LLM{ProposalProfile: &proposal, ValidationProfile: &validation, CorrectionRepairProfile: &unused}},
{Module: "grammar", LLM: config.LLM{ProposalProfile: &validation, ValidationProfile: &proposal, CorrectionRepairProfile: &repair, CorrectionRepairAttempts: 1}},
}},
}
source := &schema.SourceTranscript{Segments: []schema.SourceSegment{{Speaker: "A", Start: 0, End: 1, Text: "hello"}, {Speaker: "B", Start: 2, End: 3, Text: "world"}}}
var inspected []string
plan, err := BuildPlan(context.Background(), cfg, source, func(_ context.Context, id string) error { inspected = append(inspected, id); return nil })
if err != nil {
t.Fatal(err)
}
if !slices.Equal(plan.Profiles, []string{proposal, validation, repair}) || !slices.Equal(plan.Profiles, inspected) {
t.Fatalf("profiles %v, inspected %v", plan.Profiles, inspected)
}
if len(plan.Specs) != 2 || len(plan.Transcript.Segments) != 2 || plan.Summary.InputSegmentCount != 2 || len(plan.Sections) != 1 || plan.Sections[0].StartSegmentID != 1 || plan.Sections[0].EndSegmentID != 2 {
t.Fatalf("unexpected plan: %+v", plan)
}
count := 3
cfg.Chunking.TargetSections = &count
_, err = BuildPlan(context.Background(), cfg, source, func(context.Context, string) error { return nil })
var selection *SelectionError
var infeasible *chunking.InfeasibleTargetError
if !errors.As(err, &selection) || !errors.As(err, &infeasible) {
t.Fatalf("expected input selection failure, got %v", err)
}
}
func TestBuildPlanEmptyPipeline(t *testing.T) {
cfg := &config.Config{Chunking: config.Chunking{MaxSectionTokens: 100}}
plan, err := BuildPlan(context.Background(), cfg, &schema.SourceTranscript{Segments: []schema.SourceSegment{{Speaker: "A", Text: "hello"}}}, nil)
if err != nil || len(plan.Profiles) != 0 || len(plan.Specs) != 0 || len(plan.Sections) != 1 {
t.Fatalf("plan %+v, error %v", plan, err)
}
}
func TestBuildPlanRetainsNormalizedContentAndGuidance(t *testing.T) {
profile := "selected"
cfg := &config.Config{
Chunking: config.Chunking{MaxSectionTokens: 100},
Normalization: config.Normalization{MaxSegmentGap: 2, EllipsisGap: 2, MaxSegmentDuration: 10, MaxSegmentTokens: 100},
GlossaryCollections: map[string]config.GlossaryCollection{"terms": {Instruction: "Use the shared vocabulary", Entries: []config.GlossaryEntry{{ID: "one", Name: "Example"}}}},
Pipeline: config.Pipeline{Stages: []config.Stage{{Module: "glossary", Glossaries: []string{"terms"}, LLM: config.LLM{ProposalProfile: &profile, ValidationProfile: &profile}}}},
}
source := &schema.SourceTranscript{Segments: []schema.SourceSegment{{Speaker: "A", Start: 0, End: 1, Text: "hello"}, {Speaker: "A", Start: 1.1, End: 2, Text: "world"}}}
plan, err := BuildPlan(context.Background(), cfg, source, func(context.Context, string) error { return nil })
if err != nil {
t.Fatal(err)
}
if len(plan.Transcript.Segments) != 1 || plan.Transcript.Segments[0].Text != "hello world" || plan.Summary.MergesPerformed != 1 || len(plan.Sections) != 1 || plan.Sections[0].Segments[0].Text != "hello world" {
t.Fatalf("normalization or chunks: %+v", plan)
}
if !strings.Contains(plan.Specs[0].GlossaryVariables.InstructionsBlock, "Use the shared vocabulary") {
t.Fatalf("guidance missing: %+v", plan.Specs[0])
}
}