gunk-dev/the-valley

dashboard_restart_test.go

source4cbb5ec9c5dbmain's published tip history
12.6 KiB
1package cmd
2
3import (
4 "os"
5 "path/filepath"
6 "testing"
7 "time"
8
9 tea "github.com/charmbracelet/bubbletea"
10)
11
12// profileFixture is a PATH with a nix profile's bin ahead of the system
13// klaus. Its klaus is a symlink into a store of klaus builds, each a script
14// that answers --version.
15type profileFixture struct {
16 t *testing.T
17 root string
18 profile string // the profile's bin dir
19 system string // the system klaus
20 path string // the PATH: a relative entry, the profile, the system bin
21}
22
23func newProfileFixture(t *testing.T) *profileFixture {
24 f := &profileFixture{t: t, root: t.TempDir()}
25 f.profile = filepath.Join(f.root, "profile", "bin")
26 if err := os.MkdirAll(f.profile, 0o755); err != nil {
27 t.Fatal(err)
28 }
29 f.system = f.build("system", "0.4.0-0000000")
30 sep := string(filepath.ListSeparator)
31 f.path = "relative/bin" + sep + f.profile + sep + filepath.Dir(f.system)
32 return f
33}
34
35// build makes a klaus build in the store and returns its bin/klaus.
36func (f *profileFixture) build(name, version string) string {
37 f.t.Helper()
38 p := filepath.Join(f.root, "store", name, "bin", "klaus")
39 if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
40 f.t.Fatal(err)
41 }
42 if err := os.WriteFile(p, []byte("#!/bin/sh\necho 'klaus version "+version+"'\n"), 0o755); err != nil {
43 f.t.Fatal(err)
44 }
45 return p
46}
47
48// buildWithHelper makes a klaus build that also ships valley-flow, a
49// symlink to its klaus, as the package does, and returns its bin/klaus.
50func (f *profileFixture) buildWithHelper(name, version string) string {
51 f.t.Helper()
52 p := f.build(name, version)
53 f.shipHelper(p)
54 return p
55}
56
57// shipHelper adds valley-flow beside klaus, a symlink to it.
58func (f *profileFixture) shipHelper(klaus string) {
59 f.t.Helper()
60 if err := os.Symlink("klaus", filepath.Join(filepath.Dir(klaus), helperName)); err != nil {
61 f.t.Fatal(err)
62 }
63}
64
65// link points the profile's klaus at target, as a profile install does.
66func (f *profileFixture) link(target string) {
67 f.t.Helper()
68 f.linkAs("klaus", target)
69}
70
71// linkAs points the profile's program name at target; "" removes it.
72func (f *profileFixture) linkAs(name, target string) {
73 f.t.Helper()
74 _ = os.Remove(filepath.Join(f.profile, name))
75 if target == "" {
76 return
77 }
78 if err := os.Symlink(target, filepath.Join(f.profile, name)); err != nil {
79 f.t.Fatal(err)
80 }
81}
82
83// install links every program of the build whose bin/klaus is klaus into
84// the profile, and removes the profile's valley-flow when the build has
85// none, as a profile install of that build does.
86func (f *profileFixture) install(klaus string) {
87 f.t.Helper()
88 f.link(klaus)
89 helper := filepath.Join(filepath.Dir(klaus), helperName)
90 if _, err := os.Lstat(helper); err != nil {
91 helper = ""
92 }
93 f.linkAs(helperName, helper)
94}
95
96func (f *profileFixture) check(running string) *binCheck {
97 return f.checkAs("klaus", running)
98}
99
100// checkAs is the check of a dashboard started as program name, which notes
101// its home from PATH as it is now.
102func (f *profileFixture) checkAs(name, running string) *binCheck {
103 return startBinCheck(running, name, func() string { return f.path })
104}
105
106// TestBinCheckFindsChangedKlaus drives the check with an injected PATH and
107// running executable.
108func TestBinCheckFindsChangedKlaus(t *testing.T) {
109 f := newProfileFixture(t)
110 oldStore := f.build("old", "0.4.1-1111111")
111 newStore := f.build("new", "0.4.1-2222222")
112 b := f.check(oldStore)
113
114 // Nothing in the profile yet: PATH finds the system klaus, which is not
115 // what runs.
116 if got, ok := b.candidate(); !ok || got != f.system {
117 t.Errorf("empty profile: got %q, %v; want the system klaus", got, ok)
118 }
119 f.link(oldStore)
120 if got, ok := b.candidate(); ok {
121 t.Errorf("profile links the running klaus: got %q, want no change", got)
122 }
123 f.link(newStore)
124 if got, ok := b.candidate(); !ok || got != newStore {
125 t.Errorf("profile links a new klaus: got %q, %v; want %q", got, ok, newStore)
126 }
127 b.failed[newStore] = true
128 if got, ok := b.candidate(); ok {
129 t.Errorf("exec of %s failed before: got %q, want it left alone", newStore, got)
130 }
131}
132
133// TestBinCheckFollowsTheStartedName: a dashboard started as valley-flow
134// restarts onto valley-flow on PATH, and onto klaus from its home when the
135// home has no valley-flow any more, as after a rollback to a build without
136// one, even when a later directory has a valley-flow. One started as klaus
137// looks for klaus alone.
138func TestBinCheckFollowsTheStartedName(t *testing.T) {
139 f := newProfileFixture(t)
140 running := f.buildWithHelper("old", "0.4.1-1111111")
141 newStore := f.buildWithHelper("new", "0.4.1-2222222")
142 before := f.build("before", "0.4.0-3333333")
143 f.install(running)
144 asHelper, asKlaus := f.checkAs(helperName, running), f.checkAs("klaus", running)
145
146 for _, b := range []*binCheck{asHelper, asKlaus} {
147 if got, ok := b.candidate(); ok {
148 t.Errorf("started as %s, the profile holds the running build: got %q, want no change", b.name, got)
149 }
150 }
151 f.install(newStore)
152 for _, b := range []*binCheck{asHelper, asKlaus} {
153 if got, ok := b.candidate(); !ok || got != newStore {
154 t.Errorf("started as %s, the profile holds a new build: got %q, %v; want %q", b.name, got, ok, newStore)
155 }
156 }
157 // Each name finds its own program.
158 f.link(running)
159 if got, ok := asHelper.candidate(); !ok || got != newStore {
160 t.Errorf("started as valley-flow, PATH's valley-flow is new: got %q, %v; want %q", got, ok, newStore)
161 }
162 if got, ok := asKlaus.candidate(); ok {
163 t.Errorf("started as klaus, PATH's klaus is the running one: got %q, want no change", got)
164 }
165 f.install(before)
166 if err := os.Symlink(filepath.Join(filepath.Dir(newStore), helperName), filepath.Join(filepath.Dir(f.system), helperName)); err != nil {
167 t.Fatal(err)
168 }
169 for _, b := range []*binCheck{asHelper, asKlaus} {
170 if got, ok := b.candidate(); !ok || got != before {
171 t.Errorf("started as %s, the profile rolled back to a build without valley-flow: got %q, %v; want %q", b.name, got, ok, before)
172 }
173 }
174}
175
176// TestBinCheckBesideAnotherInstallation: with two installations on PATH,
177// a dashboard started as valley-flow restarts onto what valley flow runs,
178// and one started as klaus onto the first klaus, as before valley-flow.
179func TestBinCheckBesideAnotherInstallation(t *testing.T) {
180 sep := string(filepath.ListSeparator)
181 t.Run("an older klaus ahead", func(t *testing.T) {
182 // The system's klaus, from before valley-flow, comes first on PATH.
183 f := newProfileFixture(t)
184 f.path = filepath.Dir(f.system) + sep + f.profile
185 running, newStore := f.buildWithHelper("old", "0.4.1-1111111"), f.buildWithHelper("new", "0.4.1-2222222")
186 f.install(running)
187 b := f.checkAs(helperName, running)
188 if got, ok := b.candidate(); ok {
189 t.Errorf("valley flow runs the running build: got %q, want no change", got)
190 }
191 f.install(newStore)
192 if got, ok := b.candidate(); !ok || got != newStore {
193 t.Errorf("the profile holds a new build: got %q, %v; want %q", got, ok, newStore)
194 }
195 })
196 t.Run("a new build lands ahead", func(t *testing.T) {
197 // The dashboard runs the system's build; self-update fills the
198 // profile, which comes first on PATH.
199 f := newProfileFixture(t)
200 f.path = f.profile + sep + filepath.Dir(f.system)
201 f.shipHelper(f.system)
202 newStore := f.buildWithHelper("new", "0.4.1-2222222")
203 b := f.checkAs(helperName, f.system)
204 f.install(newStore)
205 if got, ok := b.candidate(); !ok || got != newStore {
206 t.Errorf("got %q, %v; want %q", got, ok, newStore)
207 }
208 })
209 t.Run("started as klaus", func(t *testing.T) {
210 // A valley-flow ahead goes unseen; a klaus ahead is taken, although
211 // the dashboard's klaus is further down.
212 f := newProfileFixture(t)
213 f.path = f.profile + sep + filepath.Dir(f.system)
214 newStore := f.buildWithHelper("new", "0.4.1-2222222")
215 b := f.checkAs("klaus", f.system)
216 f.linkAs(helperName, filepath.Join(filepath.Dir(newStore), helperName))
217 if got, ok := b.candidate(); ok {
218 t.Errorf("a valley-flow ahead: got %q, want no change", got)
219 }
220 f.link(newStore)
221 if got, ok := b.candidate(); !ok || got != newStore {
222 t.Errorf("a klaus ahead: got %q, %v; want %q", got, ok, newStore)
223 }
224 })
225}
226
227// pendingRestart returns a dashboard that found newStore on PATH just after
228// a key press, so its restart waits.
229func pendingRestart(t *testing.T, b *binCheck, newStore, version string) dashboardModel {
230 t.Helper()
231 m := dashboardModel{bin: b}
232 next, _ := m.update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("j")})
233 m = next.(dashboardModel)
234 next, cmd := m.update(binUpdateMsg{path: newStore, version: version})
235 m = next.(dashboardModel)
236 if m.binNotice != "flow updated to "+version+"; restarting" {
237 t.Errorf("notice = %q", m.binNotice)
238 }
239 if isQuit(cmd) {
240 t.Fatal("restarted at once, without showing the notice")
241 }
242 next, cmd = m.update(restartWaitMsg{})
243 m = next.(dashboardModel)
244 if isQuit(cmd) || m.restarting {
245 t.Fatal("restarted a moment after a key press")
246 }
247 return m
248}
249
250// quietRestart lets the keyboard go quiet and runs the restart's last check.
251func quietRestart(t *testing.T, m dashboardModel) (dashboardModel, bool) {
252 t.Helper()
253 m.lastKeyAt = time.Now().Add(-restartQuiet)
254 next, cmd := m.update(restartWaitMsg{})
255 m = next.(dashboardModel)
256 if cmd == nil {
257 t.Fatal("a quiet keyboard did not re-check PATH")
258 }
259 msg := cmd()
260 if _, ok := msg.(restartTargetMsg); !ok {
261 t.Fatalf("a quiet keyboard sent %T, want the re-check of PATH", msg)
262 }
263 next, cmd = m.update(msg)
264 return next.(dashboardModel), isQuit(cmd)
265}
266
267// TestDashboardRestartWaitsForQuietKeyboard: a new klaus restarts the
268// dashboard only once no key has been pressed for restartQuiet.
269func TestDashboardRestartWaitsForQuietKeyboard(t *testing.T) {
270 f := newProfileFixture(t)
271 running, newStore := f.build("old", "0.4.1-1111111"), f.build("new", "0.4.1-2222222")
272 f.link(newStore)
273 m := pendingRestart(t, f.check(running), newStore, "0.4.1-2222222")
274
275 m, quit := quietRestart(t, m)
276 if !quit || !m.restarting || m.restart == nil || m.restart.path != newStore {
277 t.Fatalf("quiet keyboard: quit=%v restarting=%v restart=%+v; want a restart onto %s", quit, m.restarting, m.restart, newStore)
278 }
279}
280
281// TestDashboardRestartRechecksPath: the profile changes while the restart
282// waits for the keyboard. The dashboard execs what PATH names at the end,
283// or drops the restart when that is the running klaus again.
284func TestDashboardRestartRechecksPath(t *testing.T) {
285 t.Run("a newer klaus", func(t *testing.T) {
286 f := newProfileFixture(t)
287 running, first, second := f.build("old", "0.4.1-1111111"), f.build("first", "0.4.1-2222222"), f.build("second", "0.4.1-3333333")
288 f.link(first)
289 m := pendingRestart(t, f.check(running), first, "0.4.1-2222222")
290
291 f.link(second)
292 m, quit := quietRestart(t, m)
293 if !quit || !m.restarting || m.restart == nil || m.restart.path != second || m.restart.version != "0.4.1-3333333" {
294 t.Fatalf("quit=%v restarting=%v restart=%+v; want a restart onto %s, 0.4.1-3333333", quit, m.restarting, m.restart, second)
295 }
296 if m.binNotice != "flow updated to 0.4.1-3333333; restarting" {
297 t.Errorf("notice = %q, want the version restarted onto", m.binNotice)
298 }
299 })
300 t.Run("rolled back to the running klaus", func(t *testing.T) {
301 f := newProfileFixture(t)
302 running, newStore := f.build("old", "0.4.1-1111111"), f.build("new", "0.4.1-2222222")
303 f.link(newStore)
304 m := pendingRestart(t, f.check(running), newStore, "0.4.1-2222222")
305
306 f.link(running)
307 m, quit := quietRestart(t, m)
308 if quit || m.restarting || m.restart != nil {
309 t.Fatalf("quit=%v restarting=%v restart=%+v; want the restart dropped", quit, m.restarting, m.restart)
310 }
311 if m.binNotice != "" {
312 t.Errorf("notice = %q, want it cleared", m.binNotice)
313 }
314 })
315}
316
317// TestDashboardRestartPathChangesDuringProbe: a rollback while --version
318// runs on the new klaus means the dashboard execs nothing; the pending
319// restart is dropped, and a periodic check reports nothing.
320func TestDashboardRestartPathChangesDuringProbe(t *testing.T) {
321 f := newProfileFixture(t)
322 running, newStore := f.build("old", "0.4.1-1111111"), f.build("new", "0.4.1-2222222")
323 b := f.check(running)
324 rollBackDuringProbe := func(path string) (string, error) {
325 f.link(running)
326 return "0.4.1-2222222", nil
327 }
328
329 f.link(newStore)
330 b.version = rollBackDuringProbe
331 if msg := checkBinCmd(b)(); msg != nil {
332 t.Errorf("periodic check reported %+v; PATH named the running klaus again after the probe", msg)
333 }
334
335 f.link(newStore)
336 b.version = nil
337 m := pendingRestart(t, b, newStore, "0.4.1-2222222")
338 b.version = rollBackDuringProbe
339 m, quit := quietRestart(t, m)
340 if quit || m.restarting || m.restart != nil || m.binNotice != "" {
341 t.Fatalf("quit=%v restarting=%v restart=%+v notice=%q; want the restart dropped", quit, m.restarting, m.restart, m.binNotice)
342 }
343}
344
345func isQuit(cmd tea.Cmd) bool {
346 if cmd == nil {
347 return false
348 }
349 _, ok := cmd().(tea.QuitMsg)
350 return ok
351}