Files
narratio/internal/app/execution_command_test.go

104 lines
4.0 KiB
Go

package app
import (
"bytes"
"context"
"encoding/json"
"reflect"
"testing"
"gitea.maximumdirect.net/eric/narratio/internal/config"
"go.maximumdirect.net/commandkit"
configkit "go.maximumdirect.net/configkit"
)
func TestExecutionCommandsUseTypedBoundedPlans(t *testing.T) {
pipeline, campaign, session := writeValidConfigFiles(t, t.TempDir())
full, err := BuildBoundedPlan("", "")
if err != nil {
t.Fatal(err)
}
for _, test := range []struct {
name string
args, want []string
force bool
}{
{"full", []string{"run", "2026-05-03"}, full.Names(), false},
{"single", []string{"run", "2026-05-03", "--stage", "analyze"}, []string{"analyze"}, false},
{"forced single", []string{"run", "2026-05-03", "--stage", "publish", "--force"}, []string{"publish"}, true},
{"range", []string{"run", "2026-05-03", "--from", "extract", "--through", "analyze"}, []string{"extract", "analyze"}, false},
{"regenerate", []string{"artifacts", "regenerate", "2026-05-03"}, []string{"extract", "analyze"}, true},
} {
t.Run(test.name, func(t *testing.T) {
calls := 0
executor := func(_ context.Context, cfg *config.Config, plan BoundedPlan, options RunOptions) (*RunSummary, error) {
calls++
if !reflect.DeepEqual(plan.Names(), test.want) || options.Force != test.force {
t.Fatalf("plan=%v force=%v", plan.Names(), options.Force)
}
if cfg.Session.SessionID != "2026-05-03" || !options.EffectiveArtifacts.Resolved() {
t.Fatal("missing resolved execution inputs")
}
return &RunSummary{SessionID: cfg.Session.SessionID, RunID: "synthetic-run", Executed: plan.Names()}, nil
}
app, err := newApplication(commandDependencies{}, executor)
if err != nil {
t.Fatal(err)
}
args := append(append([]string{}, test.args...), "--config", pipeline, "--campaign-file", campaign, "--session-file", session, "--format", "json")
var out, diagnostic bytes.Buffer
outcome := app.Execute(context.Background(), args, commandkit.Streams{Out: &out, Err: &diagnostic})
if outcome.Status != 0 || calls != 1 {
t.Fatalf("status=%d calls=%d diagnostics=%s", outcome.Status, calls, diagnostic.String())
}
var report runReport
if err := json.Unmarshal(out.Bytes(), &report); err != nil {
t.Fatal(err)
}
if !report.Complete || report.Campaign != "sample-campaign" || !reflect.DeepEqual(report.Executed, test.want) {
t.Fatalf("report=%#v", report)
}
})
}
}
func TestExecutionCommandsRejectInvalidSelectionBeforeBootstrap(t *testing.T) {
app, err := newApplication(commandDependencies{
loadSecretsDir: func(string) (*configkit.SecretsReport, error) {
t.Fatal("invalid input bootstrapped secrets")
return nil, nil
},
loadPipelineConfig: func(string, config.PipelineLoadOptions) (*config.PipelineConfig, error) {
t.Fatal("invalid input loaded config")
return nil, nil
},
}, func(context.Context, *config.Config, BoundedPlan, RunOptions) (*RunSummary, error) {
t.Fatal("invalid input executed pipeline")
return nil, nil
})
if err != nil {
t.Fatal(err)
}
for _, args := range [][]string{
{"run"}, {"run", "session", "--stage", "analyze", "--from", "analyze"},
{"run", "session", "--stage", "analyze", "--through", "analyze"},
{"run", "session", "--from", "publish", "--through", "prepare"},
{"run", "session", "--stage", "missing"},
{"run", "session", "--stage", "prepare", "--artifacts", "recap"},
{"artifacts", "regenerate", "session", "--force=false"},
{"artifacts", "regenerate", "session", "--through", "publish"},
{"run", "session", "--session-id", "session"},
{"run", "session", "--set", `/whisperx/language="fr"`},
{"run-stage", "analyze", "session"},
{"analyze", "session"},
{"publish", "session"},
{"regenerate-artifacts", "session"},
} {
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())
}
}
}