feat(tui): wizard uses PlanForce preview when Force is set

This commit is contained in:
MDietermann
2026-04-14 22:24:49 +02:00
parent 48d50efe19
commit ee3bb1d67c
3 changed files with 117 additions and 39 deletions

View File

@@ -31,14 +31,14 @@ func TestSyncSelected(t *testing.T) {
func TestRenderPlanWithHello(t *testing.T) {
reg := module.NewRegistry()
_ = reg.Register(hello.Module{Path: t.TempDir() + "/marker"})
out := renderPlanCached(context.Background(), reg, []string{"hello"}, newPlanCache())
out := renderPlanCached(context.Background(), reg, []string{"hello"}, newPlanCache(), false)
if !strings.Contains(out, "hello") {
t.Fatalf("output missing hello: %q", out)
}
}
func TestRenderPlanEmpty(t *testing.T) {
if got := renderPlanCached(context.Background(), module.NewRegistry(), nil, newPlanCache()); got != "No modules selected." {
if got := renderPlanCached(context.Background(), module.NewRegistry(), nil, newPlanCache(), false); got != "No modules selected." {
t.Fatalf("got %q", got)
}
}
@@ -75,9 +75,9 @@ func TestRenderPlanCachesDetectAndPlan(t *testing.T) {
t.Fatal(err)
}
cache := newPlanCache()
_ = renderPlanCached(context.Background(), reg, []string{"count"}, cache)
_ = renderPlanCached(context.Background(), reg, []string{"count"}, cache)
_ = renderPlanCached(context.Background(), reg, []string{"count"}, cache)
_ = renderPlanCached(context.Background(), reg, []string{"count"}, cache, false)
_ = renderPlanCached(context.Background(), reg, []string{"count"}, cache, false)
_ = renderPlanCached(context.Background(), reg, []string{"count"}, cache, false)
if detects != 1 || plans != 1 {
t.Fatalf("want 1 detect and 1 plan, got detects=%d plans=%d", detects, plans)
}
@@ -92,18 +92,20 @@ func TestRenderPlanInvalidatesOnSelectionChange(t *testing.T) {
reg := module.NewRegistry()
_ = reg.Register(m)
cache := newPlanCache()
_ = renderPlanCached(context.Background(), reg, []string{"count"}, cache)
_ = renderPlanCached(context.Background(), reg, []string{}, cache)
_ = renderPlanCached(context.Background(), reg, []string{"count"}, cache)
_ = renderPlanCached(context.Background(), reg, []string{"count"}, cache, false)
_ = renderPlanCached(context.Background(), reg, []string{}, cache, false)
_ = renderPlanCached(context.Background(), reg, []string{"count"}, cache, false)
if detects != 2 {
t.Fatalf("deselect+reselect should re-detect; detects=%d (want 2)", detects)
}
}
type countingModule struct {
meta module.Meta
onDetect func()
onPlan func()
meta module.Meta
onDetect func()
onPlan func()
onPlanForce func()
planForceOut module.Plan
}
func (c countingModule) Meta() module.Meta { return c.meta }
@@ -120,14 +122,62 @@ func (c countingModule) Plan(context.Context, module.State) (module.Plan, error)
return module.Plan{}, nil
}
func (c countingModule) PlanForce(context.Context) (module.Plan, error) {
if c.onPlan != nil {
c.onPlan()
if c.onPlanForce != nil {
c.onPlanForce()
}
return module.Plan{}, nil
return c.planForceOut, nil
}
func (c countingModule) Apply(context.Context, module.Plan, module.Reporter) error { return nil }
func (c countingModule) Verify(context.Context) error { return nil }
func TestWizardPreviewUsesPlanForce(t *testing.T) {
var planForceCalls int
m := countingModule{
meta: module.Meta{ID: "count", Name: "Count"},
onDetect: func() {
t.Fatal("Detect must not be called in force mode")
},
onPlan: func() {
t.Fatal("Plan must not be called in force mode")
},
onPlanForce: func() { planForceCalls++ },
planForceOut: module.Plan{
Actions: []module.Action{{Description: "forced-reapply"}},
},
}
reg := module.NewRegistry()
if err := reg.Register(m); err != nil {
t.Fatal(err)
}
out := renderPlanCached(context.Background(), reg, []string{"count"}, newPlanCache(), true)
if planForceCalls != 1 {
t.Fatalf("want 1 PlanForce call, got %d", planForceCalls)
}
if !strings.Contains(out, "forced-reapply") {
t.Fatalf("output missing PlanForce action: %q", out)
}
}
func TestWizardPreviewCacheSeparatesForceAndNonForce(t *testing.T) {
var detects, planForceCalls int
m := countingModule{
meta: module.Meta{ID: "count", Name: "Count"},
onDetect: func() { detects++ },
onPlanForce: func() { planForceCalls++ },
}
reg := module.NewRegistry()
_ = reg.Register(m)
cache := newPlanCache()
_ = renderPlanCached(context.Background(), reg, []string{"count"}, cache, false)
_ = renderPlanCached(context.Background(), reg, []string{"count"}, cache, true)
if detects != 1 {
t.Fatalf("want 1 Detect (non-force path), got %d", detects)
}
if planForceCalls != 1 {
t.Fatalf("want 1 PlanForce (force path), got %d", planForceCalls)
}
}
func TestRunCancelledContextReturnsNoConfirmation(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()