195 lines
6.9 KiB
Go
195 lines
6.9 KiB
Go
package app
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"gitea.maximumdirect.net/eric/narratio/internal/config"
|
|
)
|
|
|
|
func TestExecuteConfigUsesInvocationPipelineLoader(t *testing.T) {
|
|
dir := t.TempDir()
|
|
pipelinePath := filepath.Join(dir, "pipeline.yml")
|
|
mustWriteTestFile(t, pipelinePath, "workspace:\n root: "+filepath.Join(dir, "workspace")+"\nwhisperx:\n transcribe_url: https://transcription.example.com/transcribe\n")
|
|
|
|
for _, subcommand := range []string{"validate", "show", "sources"} {
|
|
t.Run(subcommand, func(t *testing.T) {
|
|
calls := 0
|
|
deps := commandDependencies{
|
|
loadPipelineConfig: func(path string, opts config.PipelineLoadOptions) (*config.PipelineConfig, error) {
|
|
calls++
|
|
return config.LoadPipelineWithOptions(path, opts)
|
|
},
|
|
}
|
|
var stdout, stderr bytes.Buffer
|
|
code := executeWithDependencies(
|
|
[]string{"config", subcommand, "--config", pipelinePath},
|
|
&stdout,
|
|
&stderr,
|
|
deps,
|
|
executePlan,
|
|
)
|
|
if code != 0 {
|
|
t.Fatalf("exit code = %d, want 0; stdout=%q stderr=%q", code, stdout.String(), stderr.String())
|
|
}
|
|
if calls != 1 {
|
|
t.Fatalf("pipeline loader calls = %d, want 1", calls)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func writeInspectionOverlays(t *testing.T, pipelinePath, production, testing string) {
|
|
t.Helper()
|
|
overlaysDir := filepath.Join(filepath.Dir(pipelinePath), "overlays")
|
|
mustWriteTestFile(t, filepath.Join(overlaysDir, "production.yml"), production)
|
|
mustWriteTestFile(t, filepath.Join(overlaysDir, "testing.yml"), testing)
|
|
}
|
|
|
|
func writeInspectionFamilyConfig(t *testing.T) (string, string) {
|
|
t.Helper()
|
|
dir := t.TempDir()
|
|
campaignRoot := filepath.Join(dir, "campaigns")
|
|
campaignDir := filepath.Join(campaignRoot, "sample-campaign")
|
|
pipelinePath := filepath.Join(dir, "pipeline.yml")
|
|
campaignPath := filepath.Join(campaignDir, "campaign.yml")
|
|
mustWriteTestFile(t, pipelinePath, `workspace:
|
|
root: `+filepath.Join(dir, "workspace")+`
|
|
campaigns:
|
|
root: `+campaignRoot+`
|
|
whisperx:
|
|
transcribe_url: https://transcription.example.com/transcribe
|
|
scriptorium:
|
|
artifact_families:
|
|
character_meta:
|
|
enabled: false
|
|
for_each: party.characters
|
|
output_path_pattern: artifacts/characters/{character_id}/meta.md
|
|
character_note:
|
|
enabled: false
|
|
for_each: party.characters
|
|
output_path_pattern: artifacts/characters/{character_id}/note.md
|
|
member_dependencies: [character_meta]
|
|
inputs:
|
|
prior: {source: narratio.member_artifact.character_meta, required: true}
|
|
member_vars:
|
|
character_name: character.name
|
|
publish:
|
|
enabled: true
|
|
required: true
|
|
publish:
|
|
enabled: true
|
|
upload_run: false
|
|
`)
|
|
mustWriteTestFile(t, campaignPath, `campaign_id: sample-campaign
|
|
inputs:
|
|
speakers_file: ./speakers.yml
|
|
autocorrect_file: ./autocorrect.yml
|
|
glossary_file: ./glossary.yml
|
|
party_file: ./party.yml
|
|
`)
|
|
mustWriteTestFile(t, filepath.Join(campaignDir, "party.yml"), `schema_version: narratio.party.v1
|
|
characters:
|
|
arannis:
|
|
player: {name: Eric}
|
|
character: {name: Arannis, classes: [{name: wizard}]}
|
|
`)
|
|
return pipelinePath, campaignPath
|
|
}
|
|
|
|
func TestNativeBundleInspectionAndExecutionAgree(t *testing.T) {
|
|
examples, err := filepath.Abs(filepath.Join("..", "..", "examples"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
bundle := t.TempDir()
|
|
if err := os.CopyFS(bundle, os.DirFS(examples)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
root := filepath.Join(bundle, "production-testing")
|
|
pipeline := filepath.Join(root, "narratio.yml")
|
|
if err := os.Rename(filepath.Join(root, "pipeline.yml"), pipeline); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
f, err := os.OpenFile(pipeline, os.O_APPEND|os.O_WRONLY, 0)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := f.WriteString("\naudita:\n credentials: [NARRATIO_BUNDLE_TEST_KEY]\n"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := f.Close(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
const secretName = "NARRATIO_BUNDLE_TEST_KEY"
|
|
const secretValue = "synthetic-bundle-secret"
|
|
restoreEnvAfterTest(t, secretName)
|
|
if err := os.Unsetenv(secretName); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
secrets := filepath.Join(bundle, "secrets")
|
|
mustWriteSecretFile(t, filepath.Join(secrets, secretName), secretValue)
|
|
t.Chdir(root)
|
|
campaign := filepath.Join(bundle, "campaigns", "sample-campaign", "campaign.yml")
|
|
common := []string{"--campaign-file", campaign, "--secrets-dir", secrets}
|
|
for _, operation := range []string{"validate", "show", "sources"} {
|
|
args := []string{"config", operation, "--secrets-dir", secrets}
|
|
out := nativeInspectionForTest(t, args)
|
|
if strings.Contains(out, secretValue) {
|
|
t.Fatalf("%s exposed credential value", operation)
|
|
}
|
|
if operation == "show" && !strings.Contains(out, "artifact_families") {
|
|
t.Fatal("native show omitted authored families")
|
|
}
|
|
}
|
|
var catalog bytes.Buffer
|
|
if err := ConfigArtifacts(context.Background(), common, &catalog); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !strings.Contains(catalog.String(), "character_meta_arannis") {
|
|
t.Fatal("domain catalog omitted concrete family member")
|
|
}
|
|
diff := nativeInspectionForTest(t, []string{"config", "diff", "--details", "--against-overlay", "testing", "--secrets-dir", secrets, "--format", "json"})
|
|
if !strings.Contains(diff, "/audita/proposal_profile") || strings.Contains(diff, secretValue) {
|
|
t.Fatalf("unexpected diff: %s", diff)
|
|
}
|
|
workspace := filepath.Join(root, "conf.d", "workspace")
|
|
if _, err := os.Stat(workspace); !os.IsNotExist(err) {
|
|
t.Fatalf("inspection created workspace: %v", err)
|
|
}
|
|
var execution *config.Config
|
|
executor := func(_ context.Context, cfg *config.Config, _ BoundedPlan, _ RunOptions) (*RunSummary, error) {
|
|
execution = cfg
|
|
return &RunSummary{SessionID: "2026-05-03"}, nil
|
|
}
|
|
runArgs := append([]string{"2026-05-03", "--session", filepath.Join(bundle, "session.local-audio.yml"), "--overlay", "testing"}, common...)
|
|
var plan bytes.Buffer
|
|
planArgs := append(append([]string{}, runArgs...), "--from", "prepare", "--through", "prepare")
|
|
if err := Plan(context.Background(), planArgs, &plan); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !strings.Contains(plan.String(), "prepare: run") {
|
|
t.Fatalf("unexpected plan: %s", plan.String())
|
|
}
|
|
if _, err := os.Stat(workspace); !os.IsNotExist(err) {
|
|
t.Fatalf("plan created workspace: %v", err)
|
|
}
|
|
if err := runWithPipelineExecutor(context.Background(), runArgs, io.Discard, executor); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if execution == nil || execution.Session.Inputs.AudioDir != filepath.Join(bundle, "audio") || execution.Campaign.Inputs.PartyFile != filepath.Join(bundle, "campaigns", "sample-campaign", "party.yml") {
|
|
t.Fatalf("execution did not resolve imported session and campaign resources: %+v", execution)
|
|
}
|
|
if execution.Pipeline.Audita.ProposalProfile != "narratio-testing-model-placeholder" {
|
|
t.Fatalf("execution did not use the inspected overlay: %q", execution.Pipeline.Audita.ProposalProfile)
|
|
}
|
|
if _, ok := execution.ArtifactCatalog.Artifacts["character_meta_arannis"]; !ok {
|
|
t.Fatal("execution did not expand the inspected party family")
|
|
}
|
|
}
|