|
| 1 | +package tui_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + tea "github.com/charmbracelet/bubbletea" |
| 9 | + "github.com/charmbracelet/huh" |
| 10 | + "github.com/charmbracelet/x/ansi" |
| 11 | + "github.com/charmbracelet/x/exp/teatest" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | + |
| 14 | + "github.com/render-oss/cli/pkg/tui" |
| 15 | + "github.com/render-oss/cli/pkg/tui/testhelper" |
| 16 | +) |
| 17 | + |
| 18 | +// waitOpts is the standard polling/timeout config used by these tests, matching |
| 19 | +// pkg/tui/views/logview_test.go. |
| 20 | +var waitOpts = []teatest.WaitForOption{ |
| 21 | + teatest.WithCheckInterval(time.Millisecond * 10), |
| 22 | + teatest.WithDuration(time.Second * 3), |
| 23 | +} |
| 24 | + |
| 25 | +// formHarness wires up a FormWithAction inside a real StackModel and returns |
| 26 | +// the pieces a test needs. The action pushes a sentinel model so the stack has |
| 27 | +// a frame to pop when we send Esc. |
| 28 | +type formHarness struct { |
| 29 | + stack *tui.StackModel |
| 30 | + tm *teatest.TestModel |
| 31 | + fieldPtr *string |
| 32 | + sentinel string |
| 33 | +} |
| 34 | + |
| 35 | +// newFormHarness returns a harness whose form factory captures the |
| 36 | +// caller-provided field instance. Reusing the same field across rebuilds |
| 37 | +// matches the pattern in workflowcreate.go / jobcreate.go / taskrun.go, where |
| 38 | +// fields outlive the huh.Form they're wrapped in so user input persists across |
| 39 | +// re-entry. |
| 40 | +func newFormHarness(t *testing.T, field huh.Field, fieldPtr *string) *formHarness { |
| 41 | + t.Helper() |
| 42 | + stack := tui.NewStack() |
| 43 | + |
| 44 | + const sentinel = "SENTINEL_VIEW" |
| 45 | + sentinelModel := &testhelper.SimpleModel{Str: sentinel} |
| 46 | + |
| 47 | + action := tui.NewFormAction( |
| 48 | + func(string) tea.Cmd { |
| 49 | + return stack.Push(tui.ModelWithCmd{Model: sentinelModel, Breadcrumb: "Sentinel"}) |
| 50 | + }, |
| 51 | + tui.TypedCmd[string](func() tea.Msg { |
| 52 | + return tui.LoadDataMsg[string]{Data: "ok"} |
| 53 | + }), |
| 54 | + ) |
| 55 | + |
| 56 | + buildForm := func() *huh.Form { |
| 57 | + return huh.NewForm(huh.NewGroup(field)) |
| 58 | + } |
| 59 | + |
| 60 | + fwa := tui.NewFormWithAction(action, buildForm) |
| 61 | + stack.Push(tui.ModelWithCmd{Model: fwa, Breadcrumb: "Form"}) |
| 62 | + |
| 63 | + tm := teatest.NewTestModel(t, stack) |
| 64 | + tm.Send(tea.WindowSizeMsg{Width: 80, Height: 24}) |
| 65 | + |
| 66 | + return &formHarness{stack: stack, tm: tm, fieldPtr: fieldPtr, sentinel: sentinel} |
| 67 | +} |
| 68 | + |
| 69 | +func (h *formHarness) cleanup(t *testing.T) { |
| 70 | + t.Helper() |
| 71 | + h.tm.Quit() |
| 72 | + h.tm.WaitFinished(t, teatest.WithFinalTimeout(time.Second*3)) |
| 73 | +} |
| 74 | + |
| 75 | +// TestFormWithAction_RendersAfterReInit guards the regression where, after huh |
| 76 | +// transitions to StateCompleted on submit, navigating back to the form (e.g. |
| 77 | +// via Esc on a follow-up view) left the user on a blank screen because huh |
| 78 | +// exposes no API to reset f.quitting. |
| 79 | +// |
| 80 | +// Each teatest.WaitFor consumes the output stream as it reads, so the final |
| 81 | +// WaitFor sees only post-Esc output. If the form fails to re-render (the |
| 82 | +// original bug), "FieldHeading" never appears in that fresh window and the |
| 83 | +// WaitFor times out. |
| 84 | +func TestFormWithAction_RendersAfterReInit(t *testing.T) { |
| 85 | + var fieldValue string |
| 86 | + field := huh.NewInput().Title("FieldHeading").Value(&fieldValue) |
| 87 | + h := newFormHarness(t, field, &fieldValue) |
| 88 | + defer h.cleanup(t) |
| 89 | + |
| 90 | + // 1. Initial render. |
| 91 | + teatest.WaitFor(t, h.tm.Output(), func(b []byte) bool { |
| 92 | + return bytes.Contains(b, []byte("FieldHeading")) |
| 93 | + }, waitOpts...) |
| 94 | + |
| 95 | + // 2. Submit. The action pushes the sentinel onto the stack. |
| 96 | + h.tm.Send(tea.KeyMsg{Type: tea.KeyEnter}) |
| 97 | + teatest.WaitFor(t, h.tm.Output(), func(b []byte) bool { |
| 98 | + return bytes.Contains(b, []byte(h.sentinel)) |
| 99 | + }, waitOpts...) |
| 100 | + |
| 101 | + // 3. Esc pops the sentinel; StackModel re-Inits the form. With the factory |
| 102 | + // pattern, this rebuilds a fresh huh.Form. Without it, the reused form is |
| 103 | + // in StateCompleted and View() returns "", so "FieldHeading" never appears |
| 104 | + // in this fresh output window and the WaitFor times out. |
| 105 | + h.tm.Send(tea.KeyMsg{Type: tea.KeyEsc}) |
| 106 | + teatest.WaitFor(t, h.tm.Output(), func(b []byte) bool { |
| 107 | + return bytes.Contains(b, []byte("FieldHeading")) |
| 108 | + }, waitOpts...) |
| 109 | +} |
| 110 | + |
| 111 | +// TestFormWithAction_PreservesValuesOnReInit guards the design choice behind |
| 112 | +// reusing huh.Field instances across factory rebuilds in production callers |
| 113 | +// (workflowcreate.go, jobcreate.go, taskrun.go). Because the field's value |
| 114 | +// pointer survives a rebuild, anything the user typed before submit should |
| 115 | +// still be visible when they navigate back to the form. |
| 116 | +func TestFormWithAction_PreservesValuesOnReInit(t *testing.T) { |
| 117 | + var fieldValue string |
| 118 | + field := huh.NewInput().Title("FieldHeading").Value(&fieldValue) |
| 119 | + h := newFormHarness(t, field, &fieldValue) |
| 120 | + defer h.cleanup(t) |
| 121 | + |
| 122 | + teatest.WaitFor(t, h.tm.Output(), func(b []byte) bool { |
| 123 | + return bytes.Contains(b, []byte("FieldHeading")) |
| 124 | + }, waitOpts...) |
| 125 | + |
| 126 | + // Type a marker value that wouldn't appear anywhere else in the form |
| 127 | + // chrome, so we can unambiguously detect its survival across re-init. |
| 128 | + const typed = "rendywashere" |
| 129 | + h.tm.Send(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune(typed)}) |
| 130 | + teatest.WaitFor(t, h.tm.Output(), func(b []byte) bool { |
| 131 | + return bytes.Contains(b, []byte(typed)) |
| 132 | + }, waitOpts...) |
| 133 | + |
| 134 | + // Submit and navigate to the sentinel. |
| 135 | + h.tm.Send(tea.KeyMsg{Type: tea.KeyEnter}) |
| 136 | + teatest.WaitFor(t, h.tm.Output(), func(b []byte) bool { |
| 137 | + return bytes.Contains(b, []byte(h.sentinel)) |
| 138 | + }, waitOpts...) |
| 139 | + |
| 140 | + // Esc back to the form. The rebuilt huh.Form wraps the same field instance, |
| 141 | + // whose Value pointer still references our typed string. |
| 142 | + h.tm.Send(tea.KeyMsg{Type: tea.KeyEsc}) |
| 143 | + teatest.WaitFor(t, h.tm.Output(), func(b []byte) bool { |
| 144 | + return bytes.Contains(b, []byte(typed)) |
| 145 | + }, waitOpts...) |
| 146 | + |
| 147 | + require.Equal(t, typed, *h.fieldPtr, |
| 148 | + "field's bound value should still hold the typed string after re-init") |
| 149 | +} |
| 150 | + |
| 151 | +// TestFormWithAction_ClearsScreenOnSubmit guards the secondary fix that |
| 152 | +// prepends tea.ClearScreen to huh.Form.SubmitCmd, so the renderer wipes any |
| 153 | +// leftover form chrome (titles, labels) before the loading state takes over. |
| 154 | +// Without ClearScreen, leftover lines could persist briefly during the |
| 155 | +// form-to-loading transition. |
| 156 | +// |
| 157 | +// We verify by looking for the EraseEntireDisplay ANSI sequence emitted by the |
| 158 | +// bubble tea renderer when it processes a clearScreenMsg, which only happens |
| 159 | +// via tea.ClearScreen in this flow. Using the ansi package's constant (rather |
| 160 | +// than a hard-coded escape) means we follow the renderer's source of truth |
| 161 | +// across upstream version bumps. |
| 162 | +func TestFormWithAction_ClearsScreenOnSubmit(t *testing.T) { |
| 163 | + var fieldValue string |
| 164 | + field := huh.NewInput().Title("FieldHeading").Value(&fieldValue) |
| 165 | + h := newFormHarness(t, field, &fieldValue) |
| 166 | + defer h.cleanup(t) |
| 167 | + |
| 168 | + teatest.WaitFor(t, h.tm.Output(), func(b []byte) bool { |
| 169 | + return bytes.Contains(b, []byte("FieldHeading")) |
| 170 | + }, waitOpts...) |
| 171 | + |
| 172 | + h.tm.Send(tea.KeyMsg{Type: tea.KeyEnter}) |
| 173 | + |
| 174 | + teatest.WaitFor(t, h.tm.Output(), func(b []byte) bool { |
| 175 | + return bytes.Contains(b, []byte(ansi.EraseEntireDisplay)) |
| 176 | + }, waitOpts...) |
| 177 | +} |
0 commit comments