Route console diagnostics and stage progress through native reporting
This commit is contained in:
173
internal/app/command_reporting_test.go
Normal file
173
internal/app/command_reporting_test.go
Normal file
@@ -0,0 +1,173 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/config"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/manifest"
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/stage"
|
||||
"go.maximumdirect.net/commandkit"
|
||||
)
|
||||
|
||||
type diagnosticStage struct {
|
||||
result *stage.StageResult
|
||||
err error
|
||||
}
|
||||
|
||||
func (s diagnosticStage) Name() string { return "prepare" }
|
||||
func (s diagnosticStage) Run(_ context.Context, env *stage.Env, _ *manifest.Manifest) (*stage.StageResult, error) {
|
||||
env.Info("stage context")
|
||||
env.Warn("stage warning")
|
||||
env.Debug("stage diagnostic")
|
||||
return s.result, s.err
|
||||
}
|
||||
|
||||
func TestNativeConsoleVisibilityDoesNotChangeExecution(t *testing.T) {
|
||||
for _, test := range []struct {
|
||||
name string
|
||||
flags []string
|
||||
info, debug, progress, quiet bool
|
||||
}{
|
||||
{name: "ordinary"},
|
||||
{name: "verbose", flags: []string{"--verbose"}, info: true},
|
||||
{name: "progress", flags: []string{"--progress"}, progress: true},
|
||||
{name: "verbose progress", flags: []string{"--verbose", "--progress"}, info: true, progress: true},
|
||||
{name: "quiet", flags: []string{"--debug", "--progress", "--quiet"}, quiet: true},
|
||||
{name: "debug", flags: []string{"--debug"}, info: true, debug: true, progress: true},
|
||||
{name: "debug without progress", flags: []string{"--debug", "--progress=false"}, info: true, debug: true},
|
||||
{name: "cleared debug", flags: []string{"--debug", "--debug=false"}},
|
||||
{name: "last progress value", flags: []string{"--progress=false", "--progress=true"}, progress: true},
|
||||
{name: "last mode", flags: []string{"--quiet", "--verbose"}, info: true},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
pipeline, campaign, session := writeValidConfigFiles(t, t.TempDir())
|
||||
logPath := filepath.Join(t.TempDir(), "captured.log")
|
||||
const captured = "synthetic-private-downstream-output"
|
||||
if err := os.WriteFile(logPath, []byte(captured), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var saved *RunSummary
|
||||
app, err := newApplication(commandDependencies{}, func(ctx context.Context, cfg *config.Config, plan BoundedPlan, opts RunOptions) (*RunSummary, error) {
|
||||
if len(plan.Names()) != 1 || plan.Names()[0] != "prepare" || opts.Force {
|
||||
t.Fatal("console flags changed execution selection")
|
||||
}
|
||||
if cfg.Pipeline.Trim.Bounds.RenderDebug || (cfg.Pipeline.Scriptorium != nil && cfg.Pipeline.Scriptorium.RenderDebug) {
|
||||
t.Fatal("console debug changed downstream capture")
|
||||
}
|
||||
var err error
|
||||
saved, err = executeStages(ctx, cfg, []stage.Stage{diagnosticStage{result: &stage.StageResult{Logs: []string{logPath}}}}, opts)
|
||||
return saved, err
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
args := []string{"run", "2026-05-03", "--stage", "prepare", "--config", pipeline, "--campaign-file", campaign, "--session-file", session, "--format", "json"}
|
||||
args = append(args, test.flags...)
|
||||
var out, diagnostic bytes.Buffer
|
||||
outcome := app.Execute(context.Background(), args, commandkit.Streams{Out: &out, Err: &diagnostic})
|
||||
if outcome.Status != 0 || saved == nil || !saved.Complete {
|
||||
t.Fatalf("outcome=%#v diagnostics=%s", outcome, diagnostic.String())
|
||||
}
|
||||
text := diagnostic.String()
|
||||
for _, check := range []struct {
|
||||
fragment string
|
||||
want bool
|
||||
}{{"stage context", test.info}, {"selected pipeline configuration", test.info}, {"stage warning", test.info}, {"stage diagnostic", test.debug}, {"manifest saved", test.debug}, {"starting stage", test.progress}, {"stage succeeded", test.progress}} {
|
||||
if strings.Contains(text, check.fragment) != check.want {
|
||||
t.Fatalf("visibility for %q = %v, want %v; diagnostics=%s", check.fragment, !check.want, check.want, text)
|
||||
}
|
||||
}
|
||||
if strings.Contains(text+out.String(), captured) {
|
||||
t.Fatal("replayed captured downstream output")
|
||||
}
|
||||
if test.quiet {
|
||||
if out.Len() != 0 || diagnostic.Len() != 0 {
|
||||
t.Fatal("quiet produced successful console output")
|
||||
}
|
||||
return
|
||||
}
|
||||
var report runReport
|
||||
if err := json.Unmarshal(out.Bytes(), &report); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !report.Complete || len(report.StageResults) != 1 || report.StageResults[0].Outcome != "completed" {
|
||||
t.Fatalf("report=%#v", report)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestNativeConsoleReportsSelfSkipReuseAndSingleFailure(t *testing.T) {
|
||||
pipeline, campaign, session := writeValidConfigFiles(t, t.TempDir())
|
||||
cause := errors.New("synthetic stage failure")
|
||||
for _, test := range []struct {
|
||||
name string
|
||||
selected stage.Stage
|
||||
flags []string
|
||||
want string
|
||||
status int
|
||||
}{
|
||||
{name: "self skip", selected: diagnosticStage{result: &stage.StageResult{Disposition: stage.StageDispositionSkipped, SkipReason: "disabled"}}, flags: []string{"--progress"}, want: "stage skipped"},
|
||||
{name: "success", selected: diagnosticStage{}, flags: []string{"--progress"}, want: "stage succeeded"},
|
||||
{name: "reuse", selected: failingStage{name: "prepare", err: errors.New("must reuse")}, flags: []string{"--progress"}, want: "reusing stage"},
|
||||
{name: "failure", selected: diagnosticStage{err: cause}, flags: []string{"--force", "--progress"}, status: 1},
|
||||
{name: "quiet failure", selected: diagnosticStage{err: cause}, flags: []string{"--force", "--quiet"}, status: 1},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
app, err := newApplication(commandDependencies{}, func(ctx context.Context, cfg *config.Config, _ BoundedPlan, opts RunOptions) (*RunSummary, error) {
|
||||
return executeStages(ctx, cfg, []stage.Stage{test.selected}, opts)
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
args := []string{"run", "2026-05-03", "--stage", "prepare", "--config", pipeline, "--campaign-file", campaign, "--session-file", session, "--format", "json"}
|
||||
var out, diagnostic bytes.Buffer
|
||||
outcome := app.Execute(context.Background(), append(args, test.flags...), commandkit.Streams{Out: &out, Err: &diagnostic})
|
||||
if outcome.Status != test.status {
|
||||
t.Fatalf("outcome=%#v diagnostics=%s", outcome, diagnostic.String())
|
||||
}
|
||||
if test.status == 1 {
|
||||
if strings.Count(diagnostic.String(), cause.Error()) != 1 {
|
||||
t.Fatalf("failure presented repeatedly: %s", diagnostic.String())
|
||||
}
|
||||
return
|
||||
}
|
||||
if !strings.Contains(diagnostic.String(), test.want) {
|
||||
t.Fatalf("missing %q: %s", test.want, diagnostic.String())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestNativeConsoleDeliveryFailureDoesNotInterruptCheckpointing(t *testing.T) {
|
||||
pipeline, campaign, session := writeValidConfigFiles(t, t.TempDir())
|
||||
writeErr := errors.New("diagnostic destination unavailable")
|
||||
var saved *RunSummary
|
||||
app, err := newApplication(commandDependencies{}, func(ctx context.Context, cfg *config.Config, _ BoundedPlan, opts RunOptions) (*RunSummary, error) {
|
||||
var err error
|
||||
saved, err = executeStages(ctx, cfg, []stage.Stage{diagnosticStage{}}, opts)
|
||||
return saved, err
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var out bytes.Buffer
|
||||
outcome := app.Execute(context.Background(), []string{"run", "2026-05-03", "--stage", "prepare", "--config", pipeline, "--campaign-file", campaign, "--session-file", session, "--progress", "--format", "json"}, commandkit.Streams{Out: &out, Err: runReportWriteFunc(func([]byte) (int, error) { return 0, writeErr })})
|
||||
if outcome.Status != 1 || !errors.Is(outcome.Presentation.Diagnostics.WriteError, writeErr) || saved == nil || !saved.Complete {
|
||||
t.Fatalf("outcome=%#v summary=%#v", outcome, saved)
|
||||
}
|
||||
durable, err := (&manifest.LocalStore{}).Load(context.Background(), saved.ManifestPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if durable.Stages["prepare"].Status != manifest.StatusSucceeded {
|
||||
t.Fatal("diagnostic delivery failure interrupted domain work")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user