package app import ( "bytes" "os" "path/filepath" "strings" "testing" "gitea.maximumdirect.net/eric/narratio/internal/artifacts" ) func TestExecuteCleanSessionPreservesHistoricalAudioStorage(t *testing.T) { root := t.TempDir() pipeline, campaign, session := writeValidConfigFiles(t, root) work := artifacts.SessionWorkDirForCampaign(root, "sample-campaign", "2026-05-03") historical := filepath.Join(root, "spool", "sample-campaign", "2026-05-03", "audio.flac") cache := filepath.Join(root, "cache", "old.flac") mustWriteTestFile(t, filepath.Join(work, "manifest.json"), "{}") mustWriteTestFile(t, historical, "recording") mustWriteTestFile(t, cache, "cached") var stdout, stderr bytes.Buffer if code := executeForTest([]string{"session", "clean", "2026-05-03", "--config", pipeline, "--campaign-file", campaign, "--session-file", session}, &stdout, &stderr); code != 0 { t.Fatalf("exit=%d stderr=%s", code, stderr.String()) } cleanAssertMissing(t, work) cleanAssertExists(t, historical) cleanAssertExists(t, cache) } func TestExecuteCleanSessionDryRunDeletesNothing(t *testing.T) { workspaceRoot := t.TempDir() pipelinePath, campaignPath, sessionPath := writeValidConfigFiles(t, workspaceRoot) workDir := artifacts.SessionWorkDirForCampaign(workspaceRoot, "sample-campaign", "2026-05-03") spoolDir := filepath.Join(workspaceRoot, "spool", "sample-campaign", "2026-05-03") mustWriteTestFile(t, filepath.Join(workDir, "manifest.json"), "{}") mustWriteTestFile(t, filepath.Join(spoolDir, "run-1", "audio", "alice.flac"), "audio") var stdout bytes.Buffer var stderr bytes.Buffer code := executeForTest([]string{"session", "clean", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session-file", sessionPath, "--dry-run"}, &stdout, &stderr) if code != 0 { t.Fatalf("exit code = %d, want 0; stderr=%q", code, stderr.String()) } cleanAssertExists(t, workDir) cleanAssertExists(t, spoolDir) if !strings.Contains(stdout.String(), "status: would_delete") { t.Fatalf("stdout = %q, want dry-run delete plan", stdout.String()) } } func TestExecuteCleanMissingSessionPathsSucceeds(t *testing.T) { workspaceRoot := t.TempDir() pipelinePath, campaignPath, sessionPath := writeValidConfigFiles(t, workspaceRoot) var stdout bytes.Buffer var stderr bytes.Buffer code := executeForTest([]string{"session", "clean", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--session-file", sessionPath}, &stdout, &stderr) if code != 0 { t.Fatalf("exit code = %d, want 0; stderr=%q", code, stderr.String()) } if !strings.Contains(stdout.String(), "status: missing") { t.Fatalf("stdout = %q, want missing path output", stdout.String()) } } func TestExecuteCleanAllPreservesHistoricalAudioStorage(t *testing.T) { workspaceRoot := t.TempDir() pipelinePath, _, _ := writeValidConfigFiles(t, workspaceRoot) workRoot := filepath.Join(workspaceRoot, "work") spoolRoot := filepath.Join(workspaceRoot, "spool") cachePath := filepath.Join(workspaceRoot, "cache", "keep.txt") mustWriteTestFile(t, filepath.Join(workRoot, "sample-campaign", "2026-05-03", "manifest.json"), "{}") mustWriteTestFile(t, filepath.Join(spoolRoot, "sample-campaign", "2026-05-03", "run-1", "audio", "alice.flac"), "audio") mustWriteTestFile(t, cachePath, "cache") var stdout bytes.Buffer var stderr bytes.Buffer code := executeForTest([]string{"session", "clean", "--config", pipelinePath, "--all"}, &stdout, &stderr) if code != 0 { t.Fatalf("exit code = %d, want 0; stderr=%q", code, stderr.String()) } cleanAssertMissing(t, workRoot) cleanAssertExists(t, spoolRoot) cleanAssertExists(t, filepath.Join(spoolRoot, "sample-campaign")) cleanAssertExists(t, cachePath) } func TestExecuteCleanAllRejectsSessionScopedFlags(t *testing.T) { workspaceRoot := t.TempDir() pipelinePath, campaignPath, _ := writeValidConfigFiles(t, workspaceRoot) var stdout bytes.Buffer var stderr bytes.Buffer code := executeForTest([]string{"session", "clean", "2026-05-03", "--config", pipelinePath, "--campaign-file", campaignPath, "--all"}, &stdout, &stderr) if code == 0 { t.Fatal("exit code = 0, want non-zero") } if !strings.Contains(stderr.String(), "--all cannot be combined") { t.Fatalf("stderr = %q, want --all conflict", stderr.String()) } } func TestCleanRequiresSessionID(t *testing.T) { var stdout bytes.Buffer var stderr bytes.Buffer code := executeForTest([]string{"session", "clean"}, &stdout, &stderr) if code == 0 { t.Fatal("exit code = 0, want non-zero") } if !strings.Contains(stderr.String(), "session identity is required unless --all is set") { t.Fatalf("stderr = %q, want missing session-id", stderr.String()) } } func TestCleanRejectsUnsafeTargets(t *testing.T) { root := t.TempDir() outside := t.TempDir() if _, err := cleanScopedTarget(root, filepath.Join(outside, "target"), "test.outside", true, false); err == nil { t.Fatal("outside target error = nil, want error") } if _, err := cleanScopedTarget(root, root, "test.root", true, false); err == nil { t.Fatal("root target error = nil, want error") } filePath := filepath.Join(root, "file.txt") mustWriteTestFile(t, filePath, "file") if _, err := cleanScopedTarget(root, filePath, "test.file", true, false); err == nil { t.Fatal("file target error = nil, want error") } symlinkPath := filepath.Join(root, "link") if err := os.Symlink(filepath.Join(root, "missing"), symlinkPath); err != nil { t.Fatalf("Symlink() error = %v", err) } if _, err := cleanScopedTarget(root, symlinkPath, "test.symlink", true, false); err == nil { t.Fatal("symlink target error = nil, want error") } } func TestClearIsNotCommandAlias(t *testing.T) { var stdout bytes.Buffer var stderr bytes.Buffer code := executeForTest([]string{"clear"}, &stdout, &stderr) if code == 0 { t.Fatal("exit code = 0, want non-zero") } if code != 2 { t.Fatalf("stderr = %q, want unknown clear command", stderr.String()) } } func cleanAssertExists(t *testing.T, path string) { t.Helper() if _, err := os.Stat(path); err != nil { t.Fatalf("expected %q to exist: %v", path, err) } } func cleanAssertMissing(t *testing.T, path string) { t.Helper() if _, err := os.Stat(path); !os.IsNotExist(err) { t.Fatalf("expected %q to be missing, stat err=%v", path, err) } }