package app import ( "bytes" "context" "fmt" "os" "path/filepath" "strings" "testing" "gitea.maximumdirect.net/eric/narratio/internal/artifacts" "gitea.maximumdirect.net/eric/narratio/internal/manifest" ) const ( sharedProviderName = "NARRATIO_SHARED_PROVIDER_KEY" auditaOnlyName = "NARRATIO_AUDITA_ONLY_KEY" sharedProviderKey = "synthetic-shared-provider-value" auditaOnlyKey = "synthetic-audita-only-value" ) func TestCommandsShareProviderCredentialAcrossAdapters(t *testing.T) { configDir := t.TempDir() workspace := t.TempDir() secretsDir := filepath.Join(configDir, "secrets") for name, value := range map[string]string{ sharedProviderName: sharedProviderKey, auditaOnlyName: auditaOnlyKey, } { restoreEnvAfterTest(t, name) if err := os.Unsetenv(name); err != nil { t.Fatal(err) } mustWriteSecretFile(t, filepath.Join(secretsDir, name), value+"\n") } fixture := writeProviderProcessWrapper(t) seriatim := writeSeriatimAppTestWrapper(t) configPath := filepath.Join(configDir, "pipeline.yml") campaignPath := writeAppTestCampaignConfig(t, configDir) sessionPath := filepath.Join(configDir, "session.yml") notariusConfig := filepath.Join(configDir, "notarius.yml") mustWriteTestFile(t, notariusConfig, "pipelines: {}\n") mustWriteTestFile(t, configPath, `workspace: root: `+workspace+` storage: backend: local whisperx: transcribe_url: https://example.com/transcribe seriatim: binary: `+seriatim+` audita: binary: `+fixture+` credentials: [`+sharedProviderName+`, `+auditaOnlyName+`] scriptorium: binary: `+fixture+` credentials: [`+sharedProviderName+`] render_debug: true artifacts: recap: enabled: true prompt_id: recap output_path: artifacts/recap.md inputs: transcript: source: narratio.transcript.final_trimmed notarius: enabled: true binary: `+fixture+` config_path: `+notariusConfig+` pipeline_id: dnd-session credentials: [`+sharedProviderName+`] outputs: npc_registry: lane_id: npc-registry notification: mode: noop `) mustWriteTestFile(t, sessionPath, `session_id: 2026-05-03 campaign: sample-campaign inputs: audio_dir: ./audio speakers_file: ./speakers.yml glossary_file: ./glossary.yml `) configureTestSecrets(t, configPath, secretsDir) var stdout, stderr bytes.Buffer if code := executeForTest([]string{"config", "show", "--config", configPath}, &stdout, &stderr); code != 0 { t.Fatalf("config show exit = %d; stderr=%q", code, stderr.String()) } if !strings.Contains(stdout.String(), sharedProviderName) || !strings.Contains(stdout.String(), auditaOnlyName) { t.Fatalf("config show omitted reference names: %q", stdout.String()) } assertNoProviderValues(t, stdout.Bytes(), stderr.Bytes()) workRoot := filepath.Join(workspace, "work", "sample-campaign", "2026-05-03") manifestPath := filepath.Join(workRoot, "manifest.json") basePath := filepath.Join(workRoot, "transcripts", "base.json") mustWriteTestFile(t, basePath, `{"schema":"seriatim-intermediate","segments":[]}`) seedCurrentBuiltInEvidence(t, manifestPath, "2026-05-03", "sample-campaign", "merge", artifacts.TranscriptOutputKindBase, artifacts.ArtifactTranscriptBase, basePath) mustWriteTestFile(t, filepath.Join(workRoot, "inputs", "glossary.yml"), "[]\n") seedInvocationPrerequisites(t, workspace, "polish") args := []string{"--config", configPath, "--campaign-file", campaignPath, "--session-file", sessionPath} stdout.Reset() stderr.Reset() if code := executeForTest(append([]string{"run", "2026-05-03", "--stage", "polish", "--force"}, args...), &stdout, &stderr); code != 0 { t.Fatalf("polish exit = %d; stderr=%q", code, stderr.String()) } assertNoProviderValues(t, stdout.Bytes(), stderr.Bytes()) m, err := (&manifest.LocalStore{}).Load(context.Background(), manifestPath) if err != nil { t.Fatal(err) } if polish := m.Stages["polish"]; polish == nil || len(polish.Outputs) != 1 || len(polish.Logs) != 5 { t.Fatalf("polish did not retain canonical transcript and ordinary reports: %#v", polish) } for _, name := range []string{"errors.yml", "recoveries.yml", "report.md"} { found := false for _, path := range m.Stages["polish"].Logs { found = found || filepath.Base(path) == name } if !found { t.Fatalf("missing polish report %q", name) } } trimmedPath := filepath.Join(workRoot, "transcripts", "final.trimmed.json") mustWriteTestFile(t, trimmedPath, `{"segments":[]}`) setCurrentBuiltInEvidence(t, m, "trim", artifacts.TranscriptOutputKindFinalTrimmed, artifacts.ArtifactTranscriptFinalTrimmed, trimmedPath) if err := (&manifest.LocalStore{}).Save(context.Background(), manifestPath, m); err != nil { t.Fatal(err) } stdout.Reset() stderr.Reset() seedInvocationPrerequisites(t, workspace, "extract") if code := executeForTest(append([]string{"run", "2026-05-03", "--stage", "extract", "--force"}, args...), &stdout, &stderr); code != 0 { t.Fatalf("extract exit = %d; stderr=%q", code, stderr.String()) } assertNoProviderValues(t, stdout.Bytes(), stderr.Bytes()) if !runCountsMatch(t, stdout.Bytes(), 1, 0, "extract") { t.Fatalf("extract did not execute: %q", stdout.String()) } redactedLogs := 0 stdout.Reset() stderr.Reset() if code := executeForTest(append([]string{"run", "2026-05-03", "--stage", "analyze", "--force"}, args...), &stdout, &stderr); code != 0 { t.Fatalf("analyze exit = %d; stderr=%q", code, stderr.String()) } assertNoProviderValues(t, stdout.Bytes(), stderr.Bytes()) if err := filepath.WalkDir(workRoot, func(path string, entry os.DirEntry, walkErr error) error { if walkErr != nil || entry.IsDir() || entry.Type()&os.ModeSymlink != 0 { return walkErr } data, err := os.ReadFile(path) if err == nil { assertNoProviderValues(t, data) if strings.HasSuffix(path, ".stderr.log") && bytes.Contains(data, []byte("")) { redactedLogs++ } } return err }); err != nil { t.Fatal(err) } if redactedLogs < 4 { t.Fatalf("redacted subprocess logs = %d, want Audita, Notarius and Scriptorium run/render", redactedLogs) } } func assertNoProviderValues(t *testing.T, content ...[]byte) { t.Helper() for _, data := range content { if bytes.Contains(data, []byte(sharedProviderKey)) || bytes.Contains(data, []byte(auditaOnlyKey)) { t.Fatal("credential value appeared in inspection or workspace output") } } } func writeProviderProcessWrapper(t *testing.T) string { t.Helper() exe, err := os.Executable() if err != nil { t.Fatal(err) } path := filepath.Join(t.TempDir(), "provider-process.sh") if err := os.WriteFile(path, []byte("#!/bin/sh\nexec \""+exe+"\" -test.run=TestProviderCredentialProcessFixture -- \"$@\"\n"), 0o755); err != nil { t.Fatal(err) } return path } func TestProviderCredentialProcessFixture(t *testing.T) { if !appHelperInvocation() { return } start := -1 for i, arg := range os.Args { if arg == "--" { start = i + 1 break } } if start < 0 || start >= len(os.Args) { os.Exit(2) } args := os.Args[start:] if os.Getenv(sharedProviderName) != sharedProviderKey { _, _ = os.Stderr.WriteString("shared credential not forwarded\n") os.Exit(2) } if args[0] == "promptkit" || (args[0] == "run" && appSeriatimFlagValue(args, "--output-dir") == "") { if _, present := os.LookupEnv(auditaOnlyName); present { os.Exit(2) } if appSeriatimFlagValue(args, "--api-key-env") != "" { os.Exit(2) } if len(args) >= 4 && strings.Join(args[:3], " ") == "promptkit requirements show" { _, _ = fmt.Fprintf(os.Stdout, `{"prompt_id":%q,"profile_id":"","complete":true,"inputs":[],"variables":[],"issues":[]}`, args[3]) } else if len(args) >= 4 && strings.Join(args[:3], " ") == "promptkit show prepared" { _, _ = os.Stdout.WriteString(`{"prepared":true}`) } else if args[0] == "run" { _, _ = os.Stdout.WriteString("generated recap\n") } else { os.Exit(2) } _, _ = os.Stderr.WriteString(sharedProviderKey + "\n") os.Exit(0) } if args[0] == "pipeline" { _, _ = os.Stdout.WriteString(`{"pipeline":"dnd-session","input":{"module":"synthetic","execution_class":"deterministic"},"chunk":{"module":"synthetic","execution_class":"deterministic"},"output":{"module":"synthetic","execution_class":"deterministic"},"steps":[{"id":"step","lanes":[{"id":"npc-registry","artifact_kind":"synthetic","extract":{"module":"synthetic","execution_class":"deterministic"},"merge":{"module":"synthetic","execution_class":"deterministic"},"normalize":{"module":"synthetic","execution_class":"deterministic"}}]}]}`) os.Exit(0) } if args[0] == "preflight" && appSeriatimFlagValue(args, "--input-file") != "" { _, _ = os.Stdout.WriteString(`{"pipeline":"dnd-session","ready":true,"checks":[]}`) os.Exit(0) } switch args[0] { case "process", "preflight": if os.Getenv(auditaOnlyName) != auditaOnlyKey { _, _ = os.Stderr.WriteString("Audita-only credential not forwarded\n") os.Exit(2) } if args[0] == "preflight" { os.Exit(0) } output := appSeriatimFlagValue(args, "--output") if output == "" { os.Exit(2) } if err := os.MkdirAll(filepath.Dir(output), 0o755); err != nil { os.Exit(2) } if err := os.WriteFile(output, []byte(`{"schema":"audita.processed.v1","segments":[]}`), 0o644); err != nil { os.Exit(2) } reportDir := appSeriatimFlagValue(args, "--report-dir") if err := os.MkdirAll(reportDir, 0o755); err != nil { os.Exit(2) } for _, name := range []string{"errors.yml", "recoveries.yml", "report.md"} { if err := os.WriteFile(filepath.Join(reportDir, name), []byte("status: completed\n"), 0o644); err != nil { os.Exit(2) } } case "run": if _, present := os.LookupEnv(auditaOnlyName); present { _, _ = os.Stderr.WriteString("Audita-only credential reached Notarius\n") os.Exit(2) } root := appSeriatimFlagValue(args, "--output-dir") bundle := filepath.Join(root, "fixture-run") if err := os.MkdirAll(filepath.Join(bundle, "lanes"), 0o755); err != nil { os.Exit(2) } if err := os.WriteFile(filepath.Join(bundle, "lanes", "npc-registry.json"), []byte(`{"records":[]}`), 0o644); err != nil { os.Exit(2) } _, _ = fmt.Fprintf(os.Stdout, `{"pipeline":"dnd-session","artifact_count":1,"output_directory":%q}`, bundle) default: os.Exit(2) } _, _ = os.Stderr.WriteString(sharedProviderKey + "\n") os.Exit(0) }