gunk-dev/the-valley

change_events_test.go

source60ad80d5a5a5main's published tip history
6.1 KiB
1//go:build e2e
2
3package e2e
4
5import (
6 "fmt"
7 "os"
8 "path/filepath"
9 "slices"
10 "strings"
11 "testing"
12 "time"
13)
14
15// TestDashboardReportsChangeTransitions runs the real klaus against a fake
16// valley. klaus status reads where a valley change stands and reports
17// nothing. The dashboard's first check on a session records where the
18// change stands, approved, without reporting it; once the change lands, the
19// next dashboard reports change:integrated, under the change's handle, and
20// no later start reports it again. klaus notifications lists the landing.
21func TestDashboardReportsChangeTransitions(t *testing.T) {
22 t.Parallel()
23 h := NewHarness(t)
24 h.installValleyStub()
25 sha := h.pushBranch("agent/20261030-0900-0001", "health.txt")
26 st := h.valleyRun("20261030-0900-0001", "prompt one", "nxvp", "Add a health check", 1, sha, time.Now().Add(-5*time.Minute))
27 v1 := version(1, sha, "Add a health check", "not-landed")
28 v1["request"] = map[string]any{"ref": "refs/valley/requests/nxvp", "commit": strings.Repeat("a", 40)}
29 listing := func(v map[string]any) {
30 h.writeValleyChanges(map[string]any{"schema": "valley-changes/v1", "host": "valley-host",
31 "repos": []any{map[string]any{"repo": "widget", "url": "git@valley-host:widget.git", "error": nil,
32 "changes": []any{change(st, "ready", 1, v)}, "branches": []any{}}}})
33 }
34 listing(v1)
35 sessionDir := filepath.Join(h.Home, ".klaus", "sessions", h.SessionID)
36 ledger := filepath.Join(sessionDir, "change-transitions.json")
37 // klaus session creates the events file when it starts a session.
38 if err := os.WriteFile(filepath.Join(sessionDir, "events.jsonl"), nil, 0o644); err != nil {
39 t.Fatal(err)
40 }
41 changeEvents := func() []string {
42 var out []string
43 for _, e := range h.runEvents("nxvp") {
44 out = append(out, e.Type)
45 }
46 return out
47 }
48
49 if res := h.RunKlaus("status"); res.ExitCode != 0 {
50 t.Fatalf("status exited %d; stderr: %s", res.ExitCode, res.Stderr)
51 }
52 if got := changeEvents(); len(got) != 0 {
53 t.Errorf("klaus status reported %q; it only reads", got)
54 }
55 if _, err := os.Stat(ledger); err == nil {
56 t.Error("klaus status wrote the change ledger; it only reads")
57 }
58
59 _, screen := h.startDashboard(170)
60 screen("APPROVED")
61 if got := changeEvents(); len(got) != 0 {
62 t.Errorf("the first dashboard start reported %q; it should record the change as it stands", got)
63 }
64 if _, err := os.Stat(ledger); err != nil {
65 t.Errorf("the first check should record where the change stands: %v", err)
66 }
67 h.tmux("kill-session", "-t", "dash")
68
69 h.git(h.RepoDir, "push", "-q", "origin", sha+":refs/heads/main")
70 landed := version(1, sha, "Add a health check", "landed")
71 landed["request"], landed["landed_commit"] = v1["request"], sha
72 listing(landed)
73 for i := 0; i < 2; i++ {
74 _, screen = h.startDashboard(170)
75 screen("INTEGRATED")
76 h.tmux("kill-session", "-t", "dash")
77 if got := changeEvents(); len(got) != 1 || got[0] != "change:integrated" {
78 t.Fatalf("dashboard start %d: change events %q, want change:integrated once", i+2, got)
79 }
80 }
81 if e := h.runEvents("nxvp")[0]; e.Data["landed_commit"] != sha || e.Data["repo"] != "widget" || e.Data["title"] != "Add a health check" {
82 t.Errorf("change:integrated carries %v", e.Data)
83 }
84
85 res := h.RunKlaus("notifications", "--all")
86 if res.ExitCode != 0 {
87 t.Fatalf("notifications exited %d; stderr: %s", res.ExitCode, res.Stderr)
88 }
89 if !strings.Contains(res.Stdout, "1 change(s) integrated: widget nxvp v1\n") {
90 t.Errorf("notifications should list the landing:\n%s", res.Stdout)
91 }
92}
93
94// TestDashboardIgnoresAnOlderListing runs the real dashboard against a fake
95// valley that serves a listing, then one read before it. The first check
96// records the restacker's version 2. The older listing shows version 1
97// approved and gone stale, which is old news: the dashboard shows it, and
98// reports nothing. A newer listing's approval and rejection of version 2
99// are reported.
100func TestDashboardIgnoresAnOlderListing(t *testing.T) {
101 t.Parallel()
102 h := NewHarness(t)
103 h.installValleyStub()
104 sha := h.pushBranch("agent/20261030-0900-0001", "health.txt")
105 st := h.valleyRun("20261030-0900-0001", "prompt one", "nxvp", "Add a health check", 1, sha, time.Now().Add(-5*time.Minute))
106 request := map[string]any{"ref": "refs/valley/requests/nxvp", "commit": strings.Repeat("a", 40)}
107 v1 := version(1, sha, "Add a health check", "not-landed")
108 v2 := version(2, strings.Repeat("b", 40), "Add a health check", "not-landed")
109 v2["uploader"] = "restacker"
110 listing := func(active int, vs ...map[string]any) {
111 h.writeValleyChanges(map[string]any{"schema": "valley-changes/v1", "host": "valley-host",
112 "repos": []any{map[string]any{"repo": "widget", "url": "git@valley-host:widget.git", "error": nil,
113 "changes": []any{change(st, "ready", active, vs...)}, "branches": []any{}}}})
114 }
115 decided := func(v map[string]any, decision string) map[string]any {
116 d := make(map[string]any, len(v))
117 for k, x := range v {
118 d[k] = x
119 }
120 d["request"], d["outcome"] = request, map[string]any{"decision": decision, "head": v["head"], "tip": sha}
121 return d
122 }
123 sessionDir := filepath.Join(h.Home, ".klaus", "sessions", h.SessionID)
124 // klaus session creates the events file when it starts a session.
125 if err := os.WriteFile(filepath.Join(sessionDir, "events.jsonl"), nil, 0o644); err != nil {
126 t.Fatal(err)
127 }
128 changeEvents := func() []string {
129 var out []string
130 for _, e := range h.runEvents("nxvp") {
131 out = append(out, fmt.Sprintf("%s v%v", e.Type, e.Data["version"]))
132 }
133 return out
134 }
135
136 listing(2, v1, v2)
137 _, screen := h.startDashboard(170)
138 screen("READY v2@bbbbbbb")
139 h.tmux("kill-session", "-t", "dash")
140
141 listing(1, decided(v1, "stale"))
142 _, screen = h.startDashboard(170)
143 screen("STALE v1@" + sha[:7])
144 h.tmux("kill-session", "-t", "dash")
145 if got := changeEvents(); len(got) != 0 {
146 t.Errorf("an older listing reported %q; version 1 was superseded", got)
147 }
148
149 listing(2, decided(v1, "stale"), decided(v2, "rejected"))
150 _, screen = h.startDashboard(170)
151 screen("REJECTED v2@bbbbbbb")
152 h.tmux("kill-session", "-t", "dash")
153 if got, want := changeEvents(), []string{"change:approved v2", "change:rejected v2"}; !slices.Equal(got, want) {
154 t.Errorf("change events %q, want %q", got, want)
155 }
156}