perf(tui): cache Detect+Plan results in wizard renderPlan

This commit is contained in:
MDietermann
2026-04-14 08:18:38 +02:00
parent 05dc8aa7f5
commit 83bb622232
2 changed files with 125 additions and 28 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 := renderPlan(context.Background(), reg, []string{"hello"})
out := renderPlanCached(context.Background(), reg, []string{"hello"}, newPlanCache())
if !strings.Contains(out, "hello") {
t.Fatalf("output missing hello: %q", out)
}
}
func TestRenderPlanEmpty(t *testing.T) {
if got := renderPlan(context.Background(), module.NewRegistry(), nil); got != "No modules selected." {
if got := renderPlanCached(context.Background(), module.NewRegistry(), nil, newPlanCache()); got != "No modules selected." {
t.Fatalf("got %q", got)
}
}
@@ -63,6 +63,65 @@ func TestParseIntClamp(t *testing.T) {
}
}
func TestRenderPlanCachesDetectAndPlan(t *testing.T) {
var detects, plans int
m := countingModule{
meta: module.Meta{ID: "count", Name: "Count"},
onDetect: func() { detects++ },
onPlan: func() { plans++ },
}
reg := module.NewRegistry()
if err := reg.Register(m); err != nil {
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)
if detects != 1 || plans != 1 {
t.Fatalf("want 1 detect and 1 plan, got detects=%d plans=%d", detects, plans)
}
}
func TestRenderPlanInvalidatesOnSelectionChange(t *testing.T) {
var detects int
m := countingModule{
meta: module.Meta{ID: "count", Name: "Count"},
onDetect: func() { detects++ },
}
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)
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()
}
func (c countingModule) Meta() module.Meta { return c.meta }
func (c countingModule) Detect(context.Context) (module.State, error) {
if c.onDetect != nil {
c.onDetect()
}
return nil, nil
}
func (c countingModule) Plan(context.Context, module.State) (module.Plan, error) {
if c.onPlan != nil {
c.onPlan()
}
return module.Plan{}, nil
}
func (c countingModule) Apply(context.Context, module.Plan, module.Reporter) error { return nil }
func (c countingModule) Verify(context.Context) error { return nil }
func TestRunCancelledContextReturnsNoConfirmation(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()