85 lines
3.3 KiB
Go
85 lines
3.3 KiB
Go
|
|
package app
|
||
|
|
|
||
|
|
import (
|
||
|
|
"bytes"
|
||
|
|
"context"
|
||
|
|
"encoding/json"
|
||
|
|
"errors"
|
||
|
|
"strings"
|
||
|
|
"testing"
|
||
|
|
|
||
|
|
"gitea.maximumdirect.net/eric/narratio/internal/adapters/audita"
|
||
|
|
"gitea.maximumdirect.net/eric/narratio/internal/adapters/storage"
|
||
|
|
"gitea.maximumdirect.net/eric/narratio/internal/config"
|
||
|
|
"go.maximumdirect.net/commandkit"
|
||
|
|
configkit "go.maximumdirect.net/configkit"
|
||
|
|
"gopkg.in/yaml.v3"
|
||
|
|
)
|
||
|
|
|
||
|
|
func TestPreflightCommandReportsNativeFormats(t *testing.T) {
|
||
|
|
pipeline, campaign, session := writeValidConfigFiles(t, t.TempDir())
|
||
|
|
provider := &audita.FakeRunner{}
|
||
|
|
deps := fakeCommandDependencies(&storage.FakeBackend{}, nil)
|
||
|
|
deps.newAudita = func(*config.Config) (audita.Runner, error) { return provider, nil }
|
||
|
|
app, err := newApplication(deps, executePlan)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
for _, format := range []string{"json", "yaml", "text", "auto"} {
|
||
|
|
t.Run(format, func(t *testing.T) {
|
||
|
|
var out, diagnostics bytes.Buffer
|
||
|
|
args := []string{"preflight", "2026-05-03", "--config", pipeline, "--campaign-file", campaign, "--session-file", session}
|
||
|
|
if format != "auto" {
|
||
|
|
args = append(args, "--format", format)
|
||
|
|
}
|
||
|
|
outcome := app.Execute(context.Background(), args, commandkit.Streams{Out: &out, Err: &diagnostics})
|
||
|
|
if outcome.Status != 0 {
|
||
|
|
t.Fatalf("outcome=%+v diagnostics=%s", outcome, diagnostics.String())
|
||
|
|
}
|
||
|
|
var report map[string]any
|
||
|
|
if format == "json" {
|
||
|
|
err = json.Unmarshal(out.Bytes(), &report)
|
||
|
|
} else {
|
||
|
|
err = yaml.Unmarshal(out.Bytes(), &report)
|
||
|
|
}
|
||
|
|
if err != nil || report["ready"] != true || report["session_id"] != "2026-05-03" || report["checks"] == nil {
|
||
|
|
t.Fatalf("report=%+v output=%s error=%v", report, out.String(), err)
|
||
|
|
}
|
||
|
|
})
|
||
|
|
}
|
||
|
|
provider.PreflightErr = errors.New("synthetic provider failure")
|
||
|
|
var out, diagnostics bytes.Buffer
|
||
|
|
outcome := app.Execute(context.Background(), []string{"preflight", "2026-05-03", "--config", pipeline, "--campaign-file", campaign, "--session-file", session, "--quiet", "--format", "json"}, commandkit.Streams{Out: &out, Err: &diagnostics})
|
||
|
|
if outcome.Status != 1 || !errors.Is(outcome.Cause, provider.PreflightErr) || !strings.Contains(diagnostics.String(), "synthetic provider failure") {
|
||
|
|
t.Fatalf("outcome=%+v report=%s diagnostics=%s", outcome, out.String(), diagnostics.String())
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestPreflightHelpDoesNotAcquireConfiguration(t *testing.T) {
|
||
|
|
app, err := newApplication(commandDependencies{
|
||
|
|
loadPipelineSource: func(configkit.Source, config.PipelineLoadOptions) (*config.PipelineConfig, error) {
|
||
|
|
t.Fatal("help acquired configuration")
|
||
|
|
return nil, nil
|
||
|
|
},
|
||
|
|
}, executePlan)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
for _, args := range [][]string{{"--help"}, {"preflight", "--help"}, {"session", "--help"}} {
|
||
|
|
var out, diagnostics bytes.Buffer
|
||
|
|
result := app.Execute(context.Background(), args, commandkit.Streams{Out: &out, Err: &diagnostics})
|
||
|
|
if result.Status != 0 {
|
||
|
|
t.Fatalf("args=%v outcome=%+v diagnostics=%s", args, result, diagnostics.String())
|
||
|
|
}
|
||
|
|
if len(args) == 1 && !strings.Contains(out.String(), "preflight") {
|
||
|
|
t.Fatalf("root help=%s", out.String())
|
||
|
|
}
|
||
|
|
if args[0] == "session" && strings.Contains(out.String(), "validate") {
|
||
|
|
t.Fatalf("session help=%s", out.String())
|
||
|
|
}
|
||
|
|
if args[0] == "preflight" && (!strings.Contains(out.String(), "writes state") || !strings.Contains(out.String(), "providers")) {
|
||
|
|
t.Fatalf("preflight help=%s", out.String())
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|