|
| 1 | +package cmd |
| 2 | + |
| 3 | +// This file is an internal test — the only one in the package — because what it |
| 4 | +// checks is how the prune prompt is *rendered*, and a form is not something the |
| 5 | +// App seam can hand back. Everything else about prune is behaviour, and is tested |
| 6 | +// through the command in prune_test.go. |
| 7 | + |
| 8 | +import ( |
| 9 | + "fmt" |
| 10 | + "strings" |
| 11 | + "testing" |
| 12 | + |
| 13 | + tea "charm.land/bubbletea/v2" |
| 14 | + |
| 15 | + "github.com/specsnl/labelsync/internal/plan" |
| 16 | +) |
| 17 | + |
| 18 | +// candidatesFor is n removal candidates over n repositories, all carrying the same |
| 19 | +// label name — the shape the bug showed up in: one line per repository, and every |
| 20 | +// line reading almost identically, so a viewport of one row looks like a prompt |
| 21 | +// with a single entry rather than a list that has been cut off. |
| 22 | +func candidatesFor(n int) []plan.Candidate { |
| 23 | + out := make([]plan.Candidate, 0, n) |
| 24 | + for i := range n { |
| 25 | + out = append(out, plan.Candidate{Repo: fmt.Sprintf("specsnl/php%02d", 83+i), Name: "dependencies"}) |
| 26 | + } |
| 27 | + |
| 28 | + return out |
| 29 | +} |
| 30 | + |
| 31 | +// render draws the form once at a fixed terminal size. The window size is what |
| 32 | +// decides whether the list has to scroll, so it is the input under test, not an |
| 33 | +// incidental detail of the harness. |
| 34 | +func render(t *testing.T, candidates []plan.Candidate, width, height int) string { |
| 35 | + t.Helper() |
| 36 | + |
| 37 | + var selected []plan.Candidate |
| 38 | + |
| 39 | + model, _ := pruneForm(candidates, &selected).Update(tea.WindowSizeMsg{Width: width, Height: height}) |
| 40 | + |
| 41 | + return fmt.Sprint(model.View()) |
| 42 | +} |
| 43 | + |
| 44 | +// TestPruneFormShowsEveryCandidate is the regression: with room on screen, every |
| 45 | +// candidate is on screen. The prompt used to render a one-row viewport regardless |
| 46 | +// of how much space there was, because the field subtracted its own title and |
| 47 | +// description from a height derived from the option lines alone. |
| 48 | +func TestPruneFormShowsEveryCandidate(t *testing.T) { |
| 49 | + t.Parallel() |
| 50 | + |
| 51 | + for _, count := range []int{1, 3, 20} { |
| 52 | + t.Run(fmt.Sprintf("%d candidates", count), func(t *testing.T) { |
| 53 | + t.Parallel() |
| 54 | + |
| 55 | + candidates := candidatesFor(count) |
| 56 | + view := render(t, candidates, 120, 60) |
| 57 | + |
| 58 | + for _, candidate := range candidates { |
| 59 | + if !strings.Contains(view, candidate.Repo) { |
| 60 | + t.Errorf("%s is missing from the prompt:\n%s", candidate.Repo, view) |
| 61 | + } |
| 62 | + } |
| 63 | + }) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +// TestPruneFormFitsAShortTerminal is the other half of the same setting: the list |
| 68 | +// is allowed to scroll when it genuinely cannot fit, and stays within a line of |
| 69 | +// the window when it does. |
| 70 | +// |
| 71 | +// Within a line, and not exactly within the window, because huh sizes the group |
| 72 | +// from the header and the help footer and does not count the blank line it puts |
| 73 | +// between them — so a form that has to scroll renders one line taller than the |
| 74 | +// height it was given. That is huh's arithmetic, not a height this package |
| 75 | +// chooses, and one line of overdraw on a terminal too short for the prompt is not |
| 76 | +// worth computing the window size here to work around. |
| 77 | +func TestPruneFormFitsAShortTerminal(t *testing.T) { |
| 78 | + t.Parallel() |
| 79 | + |
| 80 | + const height = 12 |
| 81 | + |
| 82 | + view := render(t, candidatesFor(40), 120, height) |
| 83 | + lines := len(strings.Split(strings.TrimRight(view, "\n"), "\n")) |
| 84 | + |
| 85 | + if lines > height+1 { |
| 86 | + t.Errorf("prompt is %d lines tall in a %d-line terminal:\n%s", lines, height, view) |
| 87 | + } |
| 88 | + |
| 89 | + if !strings.Contains(view, "specsnl/php83") { |
| 90 | + t.Errorf("the first candidate is not visible:\n%s", view) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +// TestPruneFormSelectsNothingByDefault guards the pre-selection decision from the |
| 95 | +// build-up of options the form is configured with: enter on an untouched prompt |
| 96 | +// deletes nothing. |
| 97 | +func TestPruneFormSelectsNothingByDefault(t *testing.T) { |
| 98 | + t.Parallel() |
| 99 | + |
| 100 | + view := render(t, candidatesFor(3), 120, 60) |
| 101 | + |
| 102 | + if strings.Contains(view, "✓") { |
| 103 | + t.Errorf("a candidate arrives pre-selected:\n%s", view) |
| 104 | + } |
| 105 | +} |
0 commit comments