2026-11-10 17:33:30 +00:00
|
|
|
package preflight
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2026-11-16 14:18:38 +00:00
|
|
|
"errors"
|
2026-11-11 04:20:23 +00:00
|
|
|
"fmt"
|
2026-11-10 17:33:30 +00:00
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
2026-11-16 14:18:38 +00:00
|
|
|
"slices"
|
2026-11-10 17:33:30 +00:00
|
|
|
"strings"
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"gitea.maximumdirect.net/eric/audita/v3/internal/core/config"
|
|
|
|
|
"gitea.maximumdirect.net/eric/audita/v3/internal/core/diagnostics"
|
|
|
|
|
"gitea.maximumdirect.net/eric/audita/v3/internal/core/schema"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestLaterEmptyGlossaryFailsBeforeOutputPreparation(t *testing.T) {
|
|
|
|
|
profile, id := "selected", "spells"
|
|
|
|
|
cfg := &config.Config{Pipeline: config.Pipeline{Stages: []config.Stage{
|
|
|
|
|
{Module: "grammar", LLM: config.LLM{ProposalProfile: &profile, ValidationProfile: &profile}},
|
|
|
|
|
{Module: "glossary", ID: &id, Glossaries: []string{"empty"}, LLM: config.LLM{ProposalProfile: &profile, ValidationProfile: &profile}},
|
|
|
|
|
}}}
|
|
|
|
|
output := filepath.Join(t.TempDir(), "transcript.json")
|
|
|
|
|
calls := 0
|
|
|
|
|
_, err := Prepare(context.Background(), Input{Config: cfg, Transcript: &schema.SourceTranscript{}, Output: diagnostics.OutputSelection{TranscriptPath: output}, InspectProfile: func(context.Context, string) error { calls++; return nil }})
|
|
|
|
|
if err == nil || !strings.Contains(err.Error(), "stage 2") || !strings.Contains(err.Error(), "spells") || !strings.Contains(err.Error(), "empty") {
|
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if calls != 0 {
|
|
|
|
|
t.Fatal("inspected profiles before checking all glossary selections")
|
|
|
|
|
}
|
|
|
|
|
if _, err := os.Stat(output); !os.IsNotExist(err) {
|
|
|
|
|
t.Fatalf("output exists: %v", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestEmptyPipelineNeedsNoProfile(t *testing.T) {
|
2026-11-16 14:18:38 +00:00
|
|
|
if _, err := ProcessingPlan(context.Background(), &config.Config{}, nil); err != nil {
|
2026-11-10 17:33:30 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-11-11 04:20:23 +00:00
|
|
|
|
|
|
|
|
func TestCorrectionRepairReadinessDependsOnBudget(t *testing.T) {
|
|
|
|
|
proposal, repair := "proposal", "missing"
|
|
|
|
|
cfg := &config.Config{Pipeline: config.Pipeline{Stages: []config.Stage{{Module: "grammar", LLM: config.LLM{ProposalProfile: &proposal, ValidationProfile: &proposal, CorrectionRepairProfile: &repair}}}}}
|
|
|
|
|
inspect := func(_ context.Context, id string) error {
|
|
|
|
|
if id == repair {
|
|
|
|
|
return fmt.Errorf("unknown profile")
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2026-11-16 14:18:38 +00:00
|
|
|
if _, err := ProcessingPlan(context.Background(), cfg, inspect); err != nil {
|
2026-11-11 04:20:23 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
cfg.Pipeline.Stages[0].LLM.CorrectionRepairAttempts = 1
|
|
|
|
|
output := filepath.Join(t.TempDir(), "transcript.json")
|
|
|
|
|
_, err := Prepare(context.Background(), Input{Config: cfg, Transcript: &schema.SourceTranscript{}, Output: diagnostics.OutputSelection{TranscriptPath: output}, InspectProfile: inspect})
|
|
|
|
|
if err == nil || !strings.Contains(err.Error(), "correction repair") {
|
|
|
|
|
t.Fatalf("readiness error: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if _, err := os.Stat(output); !os.IsNotExist(err) {
|
|
|
|
|
t.Fatalf("output exists: %v", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-11-16 14:18:38 +00:00
|
|
|
|
|
|
|
|
func TestProcessingPlanReusesOnlyMatchingGlossaryViews(t *testing.T) {
|
|
|
|
|
profile := "selected"
|
|
|
|
|
item := config.Stage{Module: "grammar", Glossaries: []string{"a", "b"}, ProtectedTerms: []string{"a"},
|
|
|
|
|
LLM: config.LLM{ProposalProfile: &profile, ValidationProfile: &profile}}
|
|
|
|
|
reversed := item
|
|
|
|
|
reversed.Glossaries = []string{"b", "a"}
|
|
|
|
|
exempt := item
|
|
|
|
|
exempt.Module = "glossary"
|
|
|
|
|
cfg := &config.Config{
|
|
|
|
|
GlossaryCollections: map[string]config.GlossaryCollection{
|
|
|
|
|
"a": {Entries: []config.GlossaryEntry{{ID: "a", Name: "A"}}},
|
|
|
|
|
"b": {Entries: []config.GlossaryEntry{{ID: "b", Name: "B"}}},
|
|
|
|
|
},
|
|
|
|
|
Pipeline: config.Pipeline{Stages: []config.Stage{item, item, reversed, exempt}},
|
|
|
|
|
}
|
|
|
|
|
var inspected []string
|
|
|
|
|
plan, err := ProcessingPlan(context.Background(), cfg, func(_ context.Context, id string) error {
|
|
|
|
|
inspected = append(inspected, id)
|
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
if err != nil || len(plan) != 4 || !slices.Equal(inspected, []string{profile}) {
|
|
|
|
|
t.Fatalf("plan=%+v inspections=%v error=%v", plan, inspected, err)
|
|
|
|
|
}
|
2026-11-17 04:26:44 +00:00
|
|
|
if &plan[0].Glossary[0] != &plan[1].Glossary[0] || &plan[0].ProtectedTerms[0] != &plan[1].ProtectedTerms[0] ||
|
|
|
|
|
plan[0].GlossaryView != plan[1].GlossaryView || plan[0].GlossaryVariables != plan[1].GlossaryVariables ||
|
2026-11-16 14:18:38 +00:00
|
|
|
plan[0].GlossaryView == plan[2].GlossaryView || plan[0].GlossaryView == plan[3].GlossaryView ||
|
|
|
|
|
plan[2].Glossary[0].ID != "b" || plan[3].GlossaryVariables.ProtectionJSON != "[]" {
|
|
|
|
|
t.Fatalf("incorrect glossary reuse or exemptions: %+v", plan)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestProcessingPlanPreservesFirstInspectionFailureAndCause(t *testing.T) {
|
|
|
|
|
proposal, validation := "proposal", "bad"
|
|
|
|
|
cfg := &config.Config{Pipeline: config.Pipeline{Stages: []config.Stage{
|
|
|
|
|
{Module: "grammar", LLM: config.LLM{ProposalProfile: &proposal, ValidationProfile: &validation}},
|
|
|
|
|
{Module: "grammar"},
|
|
|
|
|
}}}
|
|
|
|
|
runtimeErr := errors.New("inspection failed")
|
|
|
|
|
var checked []string
|
|
|
|
|
_, err := ProcessingPlan(context.Background(), cfg, func(_ context.Context, id string) error {
|
|
|
|
|
checked = append(checked, id)
|
|
|
|
|
if id == validation {
|
|
|
|
|
return runtimeErr
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
var selection *SelectionError
|
|
|
|
|
if !errors.Is(err, runtimeErr) || errors.As(err, &selection) || !strings.Contains(err.Error(), "stage 1 (grammar) validation") ||
|
|
|
|
|
!slices.Equal(checked, []string{proposal, validation}) {
|
|
|
|
|
t.Fatalf("inspected %v, error %v", checked, err)
|
|
|
|
|
}
|
|
|
|
|
}
|