gunk-dev/the-valley

dashboard_restart_test.go

source672eff5ffaecmain's published tip history
7.3 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// link points the profile's klaus at target, as a profile install does.
49func (f *profileFixture) link(target string) {
50 f.t.Helper()
51 _ = os.Remove(filepath.Join(f.profile, "klaus"))
52 if err := os.Symlink(target, filepath.Join(f.profile, "klaus")); err != nil {
53 f.t.Fatal(err)
54 }
55}
56
57func (f *profileFixture) check(running string) *binCheck {
58 return &binCheck{running: running, path: func() string { return f.path }, failed: map[string]bool{}}
59}
60
61// TestBinCheckFindsChangedKlaus drives the check with an injected PATH and
62// running executable.
63func TestBinCheckFindsChangedKlaus(t *testing.T) {
64 f := newProfileFixture(t)
65 oldStore := f.build("old", "0.4.1-1111111")
66 newStore := f.build("new", "0.4.1-2222222")
67 b := f.check(oldStore)
68
69 // Nothing in the profile yet: PATH finds the system klaus, which is not
70 // what runs.
71 if got, ok := b.candidate(); !ok || got != f.system {
72 t.Errorf("empty profile: got %q, %v; want the system klaus", got, ok)
73 }
74 f.link(oldStore)
75 if got, ok := b.candidate(); ok {
76 t.Errorf("profile links the running klaus: got %q, want no change", got)
77 }
78 f.link(newStore)
79 if got, ok := b.candidate(); !ok || got != newStore {
80 t.Errorf("profile links a new klaus: got %q, %v; want %q", got, ok, newStore)
81 }
82 b.failed[newStore] = true
83 if got, ok := b.candidate(); ok {
84 t.Errorf("exec of %s failed before: got %q, want it left alone", newStore, got)
85 }
86}
87
88// pendingRestart returns a dashboard that found newStore on PATH just after
89// a key press, so its restart waits.
90func pendingRestart(t *testing.T, b *binCheck, newStore, version string) dashboardModel {
91 t.Helper()
92 m := dashboardModel{bin: b}
93 next, _ := m.update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("j")})
94 m = next.(dashboardModel)
95 next, cmd := m.update(binUpdateMsg{path: newStore, version: version})
96 m = next.(dashboardModel)
97 if m.binNotice != "klaus updated to "+version+"; restarting" {
98 t.Errorf("notice = %q", m.binNotice)
99 }
100 if isQuit(cmd) {
101 t.Fatal("restarted at once, without showing the notice")
102 }
103 next, cmd = m.update(restartWaitMsg{})
104 m = next.(dashboardModel)
105 if isQuit(cmd) || m.restarting {
106 t.Fatal("restarted a moment after a key press")
107 }
108 return m
109}
110
111// quietRestart lets the keyboard go quiet and runs the restart's last check.
112func quietRestart(t *testing.T, m dashboardModel) (dashboardModel, bool) {
113 t.Helper()
114 m.lastKeyAt = time.Now().Add(-restartQuiet)
115 next, cmd := m.update(restartWaitMsg{})
116 m = next.(dashboardModel)
117 if cmd == nil {
118 t.Fatal("a quiet keyboard did not re-check PATH")
119 }
120 msg := cmd()
121 if _, ok := msg.(restartTargetMsg); !ok {
122 t.Fatalf("a quiet keyboard sent %T, want the re-check of PATH", msg)
123 }
124 next, cmd = m.update(msg)
125 return next.(dashboardModel), isQuit(cmd)
126}
127
128// TestDashboardRestartWaitsForQuietKeyboard: a new klaus restarts the
129// dashboard only once no key has been pressed for restartQuiet.
130func TestDashboardRestartWaitsForQuietKeyboard(t *testing.T) {
131 f := newProfileFixture(t)
132 running, newStore := f.build("old", "0.4.1-1111111"), f.build("new", "0.4.1-2222222")
133 f.link(newStore)
134 m := pendingRestart(t, f.check(running), newStore, "0.4.1-2222222")
135
136 m, quit := quietRestart(t, m)
137 if !quit || !m.restarting || m.restart == nil || m.restart.path != newStore {
138 t.Fatalf("quiet keyboard: quit=%v restarting=%v restart=%+v; want a restart onto %s", quit, m.restarting, m.restart, newStore)
139 }
140}
141
142// TestDashboardRestartRechecksPath: the profile changes while the restart
143// waits for the keyboard. The dashboard execs what PATH names at the end,
144// or drops the restart when that is the running klaus again.
145func TestDashboardRestartRechecksPath(t *testing.T) {
146 t.Run("a newer klaus", func(t *testing.T) {
147 f := newProfileFixture(t)
148 running, first, second := f.build("old", "0.4.1-1111111"), f.build("first", "0.4.1-2222222"), f.build("second", "0.4.1-3333333")
149 f.link(first)
150 m := pendingRestart(t, f.check(running), first, "0.4.1-2222222")
151
152 f.link(second)
153 m, quit := quietRestart(t, m)
154 if !quit || !m.restarting || m.restart == nil || m.restart.path != second || m.restart.version != "0.4.1-3333333" {
155 t.Fatalf("quit=%v restarting=%v restart=%+v; want a restart onto %s, 0.4.1-3333333", quit, m.restarting, m.restart, second)
156 }
157 if m.binNotice != "klaus updated to 0.4.1-3333333; restarting" {
158 t.Errorf("notice = %q, want the version restarted onto", m.binNotice)
159 }
160 })
161 t.Run("rolled back to the running klaus", func(t *testing.T) {
162 f := newProfileFixture(t)
163 running, newStore := f.build("old", "0.4.1-1111111"), f.build("new", "0.4.1-2222222")
164 f.link(newStore)
165 m := pendingRestart(t, f.check(running), newStore, "0.4.1-2222222")
166
167 f.link(running)
168 m, quit := quietRestart(t, m)
169 if quit || m.restarting || m.restart != nil {
170 t.Fatalf("quit=%v restarting=%v restart=%+v; want the restart dropped", quit, m.restarting, m.restart)
171 }
172 if m.binNotice != "" {
173 t.Errorf("notice = %q, want it cleared", m.binNotice)
174 }
175 })
176}
177
178// TestDashboardRestartPathChangesDuringProbe: a rollback while --version
179// runs on the new klaus means the dashboard execs nothing; the pending
180// restart is dropped, and a periodic check reports nothing.
181func TestDashboardRestartPathChangesDuringProbe(t *testing.T) {
182 f := newProfileFixture(t)
183 running, newStore := f.build("old", "0.4.1-1111111"), f.build("new", "0.4.1-2222222")
184 b := f.check(running)
185 rollBackDuringProbe := func(path string) (string, error) {
186 f.link(running)
187 return "0.4.1-2222222", nil
188 }
189
190 f.link(newStore)
191 b.version = rollBackDuringProbe
192 if msg := checkBinCmd(b)(); msg != nil {
193 t.Errorf("periodic check reported %+v; PATH named the running klaus again after the probe", msg)
194 }
195
196 f.link(newStore)
197 b.version = nil
198 m := pendingRestart(t, b, newStore, "0.4.1-2222222")
199 b.version = rollBackDuringProbe
200 m, quit := quietRestart(t, m)
201 if quit || m.restarting || m.restart != nil || m.binNotice != "" {
202 t.Fatalf("quit=%v restarting=%v restart=%+v notice=%q; want the restart dropped", quit, m.restarting, m.restart, m.binNotice)
203 }
204}
205
206func isQuit(cmd tea.Cmd) bool {
207 if cmd == nil {
208 return false
209 }
210 _, ok := cmd().(tea.QuitMsg)
211 return ok
212}