gunk-dev/the-valley

dashboard_wake_test.go

source28abc5737165main's published tip history
3.4 KiB
1package cmd
2
3import (
4 "context"
5 "testing"
6
7 tea "github.com/charmbracelet/bubbletea"
8 "the-valley/flow/internal/config"
9 gh "the-valley/flow/internal/github"
10 "the-valley/flow/internal/integration"
11 "the-valley/flow/internal/run"
12)
13
14// countingChecker counts the integration checks run.
15type countingChecker struct{ n int }
16
17func (c *countingChecker) Check(context.Context, []*run.State) integration.Report {
18 c.n++
19 return integration.Report{}
20}
21
22// TestValleyWakesCoalesce drives the dashboard's update loop with wakes,
23// timer ticks and check results: wakes during a check ask for one check
24// after it, not one each, and a tick armed before a woken check is dropped,
25// so the checks never overlap and the timer never doubles.
26func TestValleyWakesCoalesce(t *testing.T) {
27 checker := &countingChecker{}
28 m := newDashboardModel(run.NewGitDirStore(t.TempDir()), config.Config{}, gh.NewGHCLIClient(""))
29 m.checker, m.restackOn = checker, false
30 send := func(msg tea.Msg) tea.Cmd {
31 t.Helper()
32 next, cmd := m.Update(msg)
33 m = next.(dashboardModel)
34 return cmd
35 }
36 // finish runs a started check and delivers its result.
37 finish := func(cmd tea.Cmd) tea.Cmd {
38 t.Helper()
39 msg, ok := cmd().(integrationMsg)
40 if !ok {
41 t.Fatalf("the check returned %T", msg)
42 }
43 return send(msg)
44 }
45
46 send(statesLoadedMsg{}) // starts the loop's first check, left unrun
47 if !m.checking {
48 t.Fatal("the first load should start a check")
49 }
50 // A burst of records while the first check runs: one wake held for
51 // wakeSettle, then one more check asked for.
52 send(valleyWakeMsg{})
53 send(valleyWakeMsg{})
54 if !m.wakeHeld {
55 t.Fatal("a wake should wait for the rest of its burst")
56 }
57 send(valleyWakeCheckMsg{})
58 send(valleyWakeCheckMsg{})
59 again := send(integrationMsg{})
60 if checker.n != 0 || again == nil || !m.checking || m.checkAgain {
61 t.Fatalf("the check's end should start the one check the wakes asked for (checking=%v again=%v)", m.checking, m.checkAgain)
62 }
63 if tick := finish(again); tick == nil || m.checking {
64 t.Fatal("a check with no wake pending should arm the timer")
65 }
66 stale := m.checkSeq
67
68 // A wake while idle checks at once; the tick armed before it is
69 // dropped, and the one the woken check arms counts.
70 send(valleyWakeMsg{})
71 woken := send(valleyWakeCheckMsg{})
72 if woken == nil || !m.checking {
73 t.Fatal("a wake while no check runs should start one")
74 }
75 if cmd := send(integrationTickMsg{seq: stale}); cmd != nil {
76 t.Fatal("the timer should not start a check beside a woken one")
77 }
78 finish(woken)
79 if cmd := send(integrationTickMsg{seq: stale}); cmd != nil {
80 t.Fatal("a tick armed before a woken check should be dropped")
81 }
82 tick := send(integrationTickMsg{seq: m.checkSeq})
83 if tick == nil || !m.checking {
84 t.Fatal("the latest tick should start a check")
85 }
86 finish(tick)
87 if checker.n != 3 {
88 t.Errorf("ran %d checks, want 3: the woken two and the tick's", checker.n)
89 }
90}
91
92// TestRemoteHost: projects on one valley host share a follow, whatever
93// form their origins take.
94func TestRemoteHost(t *testing.T) {
95 for remote, want := range map[string]string{
96 "git@valley-host:widget.git": "valley-host",
97 "valley-host:site.git": "valley-host",
98 "ssh://git@valley-host:2222/app.git": "valley-host",
99 "https://valley.example/the-valley": "valley.example",
100 "/tmp/sandbox/origin": "/tmp/sandbox/origin",
101 } {
102 if got := remoteHost(remote); got != want {
103 t.Errorf("remoteHost(%q) = %q, want %q", remote, got, want)
104 }
105 }
106}