package audita import ( "context" "encoding/json" "errors" "os" "path/filepath" "reflect" "strings" "testing" "gitea.maximumdirect.net/eric/narratio/internal/adapters/subprocess" configkit "go.maximumdirect.net/configkit" ) func TestPreflightUsesNativeProcessingSettingsAndCleansTemporaryInputs(t *testing.T) { key := auditaTestSecret(t, "NARRATIO_PREFLIGHT_TEST_KEY", "synthetic-secret") runner := auditaTestRunner(t, SubprocessRunnerConfig{Credentials: []configkit.Secret{key}, Debug: true, ConfigPath: "config.yml", Overlay: "testing", ProposalProfile: "proposal", ValidationProfile: "validation", OutputSchema: "audita-v1"}) req := auditaTestRequest(t) req.ReportDir = filepath.Join(t.TempDir(), "reports") req.WorkDir = filepath.Join(t.TempDir(), "debug") originalStdout := req.StdoutLogPath if err := os.WriteFile(originalStdout, []byte("prior"), 0600); err != nil { t.Fatal(err) } var captured subprocess.RunRequest var tempDir string runner.run = func(_ context.Context, r subprocess.RunRequest) (subprocess.RunResult, error) { captured = r tempDir = filepath.Dir(r.StdoutLogPath) data, err := os.ReadFile(filepath.Join(tempDir, "transcript.json")) if err != nil { t.Fatal(err) } var transcript struct { Segments []map[string]any `json:"segments"` } if err := json.Unmarshal(data, &transcript); err != nil || len(transcript.Segments) < 2 { t.Fatalf("fixture: %s %v", data, err) } if r.StdoutLogPath == originalStdout || r.StderrLogPath == req.StderrLogPath { t.Fatal("overwrote processing diagnostics") } if r.EnvOverrides["NARRATIO_PREFLIGHT_TEST_KEY"] != "synthetic-secret" || !reflect.DeepEqual(r.SensitiveEnvNames, []string{"NARRATIO_PREFLIGHT_TEST_KEY"}) { t.Fatal("credential forwarding lost") } return subprocess.RunResult{}, nil } result, err := runner.Preflight(context.Background(), req) if err != nil || result.ExitCode != 0 { t.Fatalf("preflight: %+v %v", result, err) } args := runner.buildArgs("process", req) if captured.Args[0] != "preflight" || !reflect.DeepEqual(captured.Args[2:], args[2:]) { t.Fatalf("settings differ: %v vs %v", captured.Args, args) } if strings.Contains(strings.Join(captured.Args, " "), "--smoke=false") { t.Fatal("smoke disabled") } if _, err := os.Stat(tempDir); !os.IsNotExist(err) { t.Fatalf("temporary directory remains: %v", err) } prior, err := os.ReadFile(originalStdout) if err != nil || string(prior) != "prior" { t.Fatalf("prior diagnostics changed: %q %v", prior, err) } } func TestPreflightPreservesFailureAndRedactsDiagnostics(t *testing.T) { const name = "NARRATIO_PREFLIGHT_SECRET" secret := auditaTestSecret(t, name, "synthetic-private-value") binary := filepath.Join(t.TempDir(), "helper") writeAuditaTestFile(t, binary, "#!/bin/sh\nprintf 'provider rejected %s\\n' \"$"+name+"\" >&2\nexit 2\n") if err := os.Chmod(binary, 0700); err != nil { t.Fatal(err) } runner := auditaTestRunner(t, SubprocessRunnerConfig{Binary: binary, Credentials: []configkit.Secret{secret}}) result, err := runner.Preflight(context.Background(), auditaTestRequest(t)) if err == nil || result.ExitCode != 2 || !strings.Contains(err.Error(), "provider rejected") || strings.Contains(err.Error(), "synthetic-private-value") { t.Fatalf("failure: %+v %v", result, err) } } func TestPreflightCancellationPreservesCause(t *testing.T) { runner := auditaTestRunner(t, SubprocessRunnerConfig{}) runner.run = func(ctx context.Context, _ subprocess.RunRequest) (subprocess.RunResult, error) { return subprocess.RunResult{Canceled: true}, ctx.Err() } ctx, cancel := context.WithCancel(context.Background()) cancel() _, err := runner.Preflight(ctx, auditaTestRequest(t)) if !errors.Is(err, context.Canceled) { t.Fatalf("cancellation: %v", err) } } func TestPreflightCleansTemporaryFilesOnFailure(t *testing.T) { runner := auditaTestRunner(t, SubprocessRunnerConfig{}) failure := errors.New("synthetic provider failure") var tempDir string runner.run = func(_ context.Context, r subprocess.RunRequest) (subprocess.RunResult, error) { tempDir = filepath.Dir(r.StderrLogPath) return subprocess.RunResult{ExitCode: 1}, failure } _, err := runner.Preflight(context.Background(), auditaTestRequest(t)) if !errors.Is(err, failure) { t.Fatalf("cause lost: %v", err) } if _, err := os.Stat(tempDir); !os.IsNotExist(err) { t.Fatalf("temporary directory remains: %v", err) } }