package app import ( "bytes" "context" "encoding/json" "errors" "os" "path/filepath" "reflect" "testing" "gitea.maximumdirect.net/eric/narratio/internal/config" "go.maximumdirect.net/commandkit" ) func catalogReportForTest(t *testing.T, args []string) *artifactCatalogReport { t.Helper() output := nativeInspectionForTest(t, append([]string{"artifacts", "list", "--format", "json"}, args...)) var report artifactCatalogReport if err := json.Unmarshal([]byte(output), &report); err != nil { t.Fatal(err) } return &report } func TestNativeCatalogValidationErrorsAreUsage(t *testing.T) { pipeline, campaign, session := writeValidConfigFiles(t, t.TempDir()) data, err := os.ReadFile(pipeline) if err != nil { t.Fatal(err) } mustWriteTestFile(t, pipeline, string(data)+`scriptorium: artifacts: result: enabled: false output_path: artifacts/result.md depends_on: [missing] `) for _, args := range [][]string{ {"artifacts", "list", "--config", pipeline}, {"run", "2026-05-03", "--stage", "analyze", "--config", pipeline, "--campaign-file", campaign, "--session-file", session}, } { var out, diagnostic bytes.Buffer outcome := Execute(context.Background(), args, commandkit.Streams{Out: &out, Err: &diagnostic}) var input *config.ArtifactCatalogInputError if outcome.Status != 2 || !errors.As(outcome.Cause, &input) { t.Fatalf("%v: status=%d cause=%v diagnostics=%s", args, outcome.Status, outcome.Cause, &diagnostic) } } } func TestArtifactCatalogCommandWithoutCampaignPreservesDependencies(t *testing.T) { dir := t.TempDir() pipeline := filepath.Join(dir, "pipeline.yml") mustWriteTestFile(t, pipeline, `workspace: root: `+filepath.Join(dir, "workspace")+` whisperx: {transcribe_url: https://transcription.example.test/transcribe} scriptorium: artifacts: zeta: {enabled: false, output_path: artifacts/zeta.md} alpha: enabled: false output_path: artifacts/alpha.md inputs: upstream: {source: narratio.artifact.zeta, required: true} `) report := catalogReportForTest(t, []string{"--config", pipeline}) found := map[string]artifactDefinitionReport{} for _, entry := range report.Artifacts { found[entry.Source] = entry } alpha := found["narratio.artifact.alpha"] if alpha.Producer != "analyze" || alpha.Enabled == nil || *alpha.Enabled || !reflect.DeepEqual(alpha.Dependencies, []string{"zeta"}) || !reflect.DeepEqual(alpha.DefinitionSources, []string{pipeline}) { t.Fatalf("alpha=%#v", alpha) } if found["narratio.transcript.base"].Producer != "merge" { t.Fatal("catalog omitted built-in producer") } if _, err := os.Stat(filepath.Join(dir, "workspace")); !os.IsNotExist(err) { t.Fatalf("listing created workspace: %v", err) } } func TestArtifactCatalogCommandUsesExplicitRosterAndNativeConfiguration(t *testing.T) { pipeline, _ := writeInspectionFamilyConfig(t) overlay := filepath.Join(filepath.Dir(pipeline), "overlays", "testing.yml") mustWriteTestFile(t, overlay, "scriptorium:\n artifact_families:\n character_note:\n prompt_id: dnd.character_note.testing\n") roster := filepath.Join(t.TempDir(), "selected.yml") mustWriteTestFile(t, roster, "schema_version: narratio.party.v1\ncharacters:\n synthetic_character:\n player: {name: Synthetic Player}\n character: {name: Synthetic Character, classes: [{name: ranger}]}\n") selected := "testing" loaded, err := config.LoadPipelineWithOptions(pipeline, config.PipelineLoadOptions{Overlay: &selected}) if err != nil { t.Fatal(err) } var before, after bytes.Buffer if err := loaded.ConfigKitResult().WriteEffective(&before); err != nil { t.Fatal(err) } app, err := newApplication(commandDependencies{loadPipelineConfig: func(string, config.PipelineLoadOptions) (*config.PipelineConfig, error) { return loaded, nil }}, executePlan) if err != nil { t.Fatal(err) } var out, diagnostic bytes.Buffer outcome := app.Execute(context.Background(), []string{"artifacts", "list", "--config", pipeline, "--overlay", selected, "--party-file", roster, "--format", "json"}, commandkit.Streams{Out: &out, Err: &diagnostic}) if outcome.Status != 0 { t.Fatalf("status=%d diagnostics=%s", outcome.Status, diagnostic.String()) } var report artifactCatalogReport if err := json.Unmarshal(out.Bytes(), &report); err != nil { t.Fatal(err) } found := false for _, entry := range report.Artifacts { if entry.Family == "character_note" { if entry.CharacterID != "synthetic_character" || entry.PartySource != roster || entry.Key != "character_note_synthetic_character" { t.Fatalf("member=%#v", entry) } if !reflect.DeepEqual(entry.DefinitionSources, []string{overlay, pipeline}) { t.Fatalf("definition sources=%v", entry.DefinitionSources) } found = true } } if !found { t.Fatal("catalog omitted selected family member") } if err := loaded.ConfigKitResult().WriteEffective(&after); err != nil { t.Fatal(err) } if !bytes.Equal(before.Bytes(), after.Bytes()) { t.Fatal("catalog derivation changed native configuration") } }