Add native restore and session cleanup commands
This commit is contained in:
98
internal/app/clean_command_test.go
Normal file
98
internal/app/clean_command_test.go
Normal file
@@ -0,0 +1,98 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/artifacts"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/config"
|
||||
"go.maximumdirect.net/commandkit"
|
||||
configkit "go.maximumdirect.net/configkit"
|
||||
)
|
||||
|
||||
func TestCleanCommandReportsCompletedActionsBeforeLaterFailure(t *testing.T) {
|
||||
workspace := t.TempDir()
|
||||
pipeline, campaign, session := writeValidConfigFiles(t, workspace)
|
||||
work := artifacts.SessionWorkDirForCampaign(workspace, "sample-campaign", "2026-05-03")
|
||||
mustWriteTestFile(t, filepath.Join(work, "manifest.json"), "{}")
|
||||
spool := artifacts.SessionSpoolDir(filepath.Join(workspace, "spool"), "sample-campaign", "2026-05-03")
|
||||
if err := os.MkdirAll(filepath.Dir(spool), 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
outside := t.TempDir()
|
||||
sentinel := filepath.Join(outside, "retained")
|
||||
mustWriteTestFile(t, sentinel, "retained")
|
||||
if err := os.Symlink(outside, spool); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var out, diagnostic bytes.Buffer
|
||||
outcome := Execute(context.Background(), []string{"session", "clean", "2026-05-03", "--config", pipeline, "--campaign-file", campaign, "--session-file", session, "--format", "json"}, commandkit.Streams{Out: &out, Err: &diagnostic})
|
||||
if outcome.Status != 1 {
|
||||
t.Fatalf("status=%d diagnostics=%s", outcome.Status, diagnostic.String())
|
||||
}
|
||||
var report cleanCommandReport
|
||||
if err := json.Unmarshal(out.Bytes(), &report); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if report.Complete || len(report.Actions) != 2 || report.Actions[0].Status != "deleted" || report.Actions[1].Status != "failed" || report.Error == "" {
|
||||
t.Fatalf("partial report=%#v", report)
|
||||
}
|
||||
cleanAssertMissing(t, work)
|
||||
cleanAssertExists(t, sentinel)
|
||||
}
|
||||
|
||||
func TestCleanCommandChecksAllSelectionBeforeBootstrap(t *testing.T) {
|
||||
app, err := newApplication(commandDependencies{
|
||||
loadSecretsDir: func(string) (*configkit.SecretsReport, error) {
|
||||
t.Fatal("invalid cleanup bootstrapped secrets")
|
||||
return nil, nil
|
||||
},
|
||||
loadPipelineConfig: func(string, config.PipelineLoadOptions) (*config.PipelineConfig, error) {
|
||||
t.Fatal("invalid cleanup loaded configuration")
|
||||
return nil, nil
|
||||
},
|
||||
}, executePlan)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, args := range [][]string{
|
||||
{"session", "clean"},
|
||||
{"session", "clean", "session", "--all"},
|
||||
{"session", "clean", "--all", "--campaign", "campaign"},
|
||||
{"session", "clean", "--all", "--campaign-file", "campaign.yml"},
|
||||
{"session", "clean", "--all", "--session-file", "session.yml"},
|
||||
{"session", "clean", "--all", "--party-file", "party.yml"},
|
||||
{"session", "clean", "--all", "--previous-session-id", "previous"},
|
||||
{"clean"},
|
||||
} {
|
||||
var out, diagnostic bytes.Buffer
|
||||
outcome := app.Execute(context.Background(), args, commandkit.Streams{Out: &out, Err: &diagnostic})
|
||||
if outcome.Status != 2 {
|
||||
t.Fatalf("%v status=%d diagnostics=%s", args, outcome.Status, diagnostic.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCleanAllCommandNeedsOnlyPipelineConfiguration(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
pipeline := filepath.Join(root, "pipeline.yml")
|
||||
mustWriteTestFile(t, pipeline, "workspace: {root: ./workspace}\nspool: {root: ./spool}\nwhisperx: {transcribe_url: https://transcription.example.test/transcribe}\n")
|
||||
var out, diagnostic bytes.Buffer
|
||||
outcome := Execute(context.Background(), []string{"session", "clean", "--all", "--dry-run", "--config", pipeline, "--format", "json"}, commandkit.Streams{Out: &out, Err: &diagnostic})
|
||||
if outcome.Status != 0 {
|
||||
t.Fatalf("status=%d diagnostics=%s", outcome.Status, diagnostic.String())
|
||||
}
|
||||
var report cleanCommandReport
|
||||
if err := json.Unmarshal(out.Bytes(), &report); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !report.All || !report.DryRun || report.Complete || report.Campaign != "" || report.Cache != "preserved" {
|
||||
t.Fatalf("report=%#v", report)
|
||||
}
|
||||
cleanAssertMissing(t, filepath.Join(root, "workspace"))
|
||||
cleanAssertMissing(t, filepath.Join(root, "spool"))
|
||||
}
|
||||
Reference in New Issue
Block a user