gunk-dev/the-valley

review_check_test.go

source28abc5737165main's published tip history
13.2 KiB
1package cmd
2
3import (
4 "encoding/json"
5 "testing"
6 "time"
7
8 "the-valley/flow/internal/config"
9 "the-valley/flow/internal/github"
10
11 "the-valley/flow/internal/pipeline"
12)
13
14// activityFixture builds the review activity hasUnaddressedTrustedComments
15// reads from REST-shaped JSON: reviews, their inline comments (by review
16// ID), conversation comments and commits. The PR author is "maintainer", who
17// is not trusted by the default config, which trusts gemini-code-assist[bot].
18func activityFixture(t *testing.T, reviews, comments, commits string) *github.PRActivity {
19 t.Helper()
20 return activityFixtureWithConversation(t, reviews, comments, `[]`, commits)
21}
22
23// activityFixtureWithConversation is activityFixture plus conversation comments.
24func activityFixtureWithConversation(t *testing.T, reviews, comments, conversation, commits string) *github.PRActivity {
25 t.Helper()
26 return activityFixtureWithAuthor(t, "maintainer", reviews, comments, conversation, commits)
27}
28
29// activityFixtureWithAuthor is activityFixtureWithConversation with the given
30// PR author, who is also the operator gh runs as.
31func activityFixtureWithAuthor(t *testing.T, author, reviews, comments, conversation, commits string) *github.PRActivity {
32 t.Helper()
33 type user struct {
34 Login string `json:"login"`
35 }
36 var rs []struct {
37 ID int64 `json:"id"`
38 User user `json:"user"`
39 State string `json:"state"`
40 SubmittedAt string `json:"submitted_at"`
41 Body string `json:"body"`
42 }
43 var cs []struct {
44 ReviewID int64 `json:"pull_request_review_id"`
45 Body string `json:"body"`
46 }
47 var conv []struct {
48 User user `json:"user"`
49 Body string `json:"body"`
50 CreatedAt string `json:"created_at"`
51 UpdatedAt string `json:"updated_at"`
52 }
53 var commitList []struct {
54 Commit struct {
55 Committer struct {
56 Date string `json:"date"`
57 } `json:"committer"`
58 } `json:"commit"`
59 }
60 for _, j := range []struct {
61 in string
62 out any
63 }{{reviews, &rs}, {comments, &cs}, {conversation, &conv}, {commits, &commitList}} {
64 if err := json.Unmarshal([]byte(j.in), j.out); err != nil {
65 t.Fatalf("fixture %s: %v", j.in, err)
66 }
67 }
68 parse := func(s string) time.Time {
69 ts, _ := time.Parse(time.RFC3339, s)
70 return ts
71 }
72 a := &github.PRActivity{Operator: author, Author: author}
73 for _, r := range rs {
74 rv := github.Review{ID: r.ID, Author: r.User.Login, State: r.State, Body: r.Body, SubmittedAt: parse(r.SubmittedAt)}
75 for _, c := range cs {
76 if c.ReviewID == r.ID {
77 rv.Comments = append(rv.Comments, c.Body)
78 }
79 }
80 a.Reviews = append(a.Reviews, rv)
81 }
82 for _, c := range conv {
83 at := c.UpdatedAt
84 if at == "" {
85 at = c.CreatedAt
86 }
87 a.Comments = append(a.Comments, github.IssueComment{Author: c.User.Login, Body: c.Body, UpdatedAt: parse(at)})
88 }
89 for _, c := range commitList {
90 if ts := parse(c.Commit.Committer.Date); ts.After(a.LastCommitAt) {
91 a.LastCommitAt = ts
92 }
93 }
94 return a
95}
96
97// unaddressed asks hasUnaddressedTrustedComments about a fixture with the
98// default trusted reviewers.
99func unaddressed(a *github.PRActivity) bool {
100 return hasUnaddressedTrustedComments(a, config.Defaults().TrustedReviewers)
101}
102
103// Production repro (2026-07-06): gemini-code-assist left a COMMENTED review
104// consisting of only a body โ€” zero inline comments โ€” on a docs PR. A fix
105// agent has nothing to address and pushes no commit, so the commit-time
106// watermark never advances; treating the review as "unaddressed" redispatched
107// fix agents forever. Body-only reviews must not count.
108func TestHasUnaddressedTrustedComments_BodyOnlyReview(t *testing.T) {
109 a := activityFixture(t,
110 `[{"id": 100, "user": {"login": "gemini-code-assist[bot]"}, "state": "COMMENTED", "submitted_at": "2026-07-06T10:00:00Z"}]`,
111 `[]`,
112 `[{"commit": {"committer": {"date": "2026-07-06T09:00:00Z"}}}]`,
113 )
114 if unaddressed(a) {
115 t.Error("body-only trusted COMMENTED review must not count as unaddressed")
116 }
117}
118
119func TestHasUnaddressedTrustedComments_InlineComments(t *testing.T) {
120 a := activityFixture(t,
121 `[{"id": 100, "user": {"login": "gemini-code-assist[bot]"}, "state": "COMMENTED", "submitted_at": "2026-07-06T10:00:00Z"}]`,
122 `[{"pull_request_review_id": 100}]`,
123 `[{"commit": {"committer": {"date": "2026-07-06T09:00:00Z"}}}]`,
124 )
125 if !unaddressed(a) {
126 t.Error("trusted review with inline comments and no newer commit must count as unaddressed")
127 }
128}
129
130func TestHasUnaddressedTrustedComments_AddressedByNewerCommit(t *testing.T) {
131 a := activityFixture(t,
132 `[{"id": 100, "user": {"login": "gemini-code-assist[bot]"}, "state": "COMMENTED", "submitted_at": "2026-07-06T10:00:00Z"}]`,
133 `[{"pull_request_review_id": 100}]`,
134 `[{"commit": {"committer": {"date": "2026-07-06T11:00:00Z"}}}]`,
135 )
136 if unaddressed(a) {
137 t.Error("review followed by a newer commit must count as addressed")
138 }
139}
140
141// A rebase preserves the author date; only the committer date reflects when
142// the push actually happened. Here the addressing commit was authored before
143// the review but committed after it โ€” going by author date would falsely
144// mark the review unaddressed and redispatch.
145func TestHasUnaddressedTrustedComments_UsesCommitterDateNotAuthorDate(t *testing.T) {
146 a := activityFixture(t,
147 `[{"id": 100, "user": {"login": "gemini-code-assist[bot]"}, "state": "COMMENTED", "submitted_at": "2026-07-06T10:00:00Z"}]`,
148 `[{"pull_request_review_id": 100}]`,
149 `[{"commit": {"author": {"date": "2026-07-06T08:00:00Z"}, "committer": {"date": "2026-07-06T11:00:00Z"}}}]`,
150 )
151 if unaddressed(a) {
152 t.Error("rebased commit newer than review (by committer date) must count as addressed")
153 }
154}
155
156func TestHasUnaddressedTrustedComments_UntrustedReviewer(t *testing.T) {
157 a := activityFixture(t,
158 `[{"id": 100, "user": {"login": "drive-by-user"}, "state": "COMMENTED", "submitted_at": "2026-07-06T10:00:00Z"}]`,
159 `[{"pull_request_review_id": 100}]`,
160 `[{"commit": {"committer": {"date": "2026-07-06T09:00:00Z"}}}]`,
161 )
162 if unaddressed(a) {
163 t.Error("untrusted reviewer's comments must not trigger dispatch")
164 }
165}
166
167// Production repro (2026-09-14): the operator, a trusted reviewer, left review
168// feedback as PR conversation comments (issues API) rather than inline review
169// comments, and nothing was dispatched.
170func TestHasUnaddressedTrustedComments_ConversationComment(t *testing.T) {
171 a := activityFixtureWithConversation(t,
172 `[]`,
173 `[]`,
174 `[{"user": {"login": "gemini-code-assist[bot]"}, "body": "please rename this", "created_at": "2026-09-14T10:00:00Z"}]`,
175 `[{"commit": {"committer": {"date": "2026-09-14T09:00:00Z"}}}]`,
176 )
177 if !unaddressed(a) {
178 t.Error("trusted conversation comment with no newer commit must count as unaddressed")
179 }
180}
181
182func TestHasUnaddressedTrustedComments_ConversationCommentAddressedByNewerCommit(t *testing.T) {
183 a := activityFixtureWithConversation(t,
184 `[]`,
185 `[]`,
186 `[{"user": {"login": "gemini-code-assist[bot]"}, "body": "please rename this", "created_at": "2026-09-14T10:00:00Z"}]`,
187 `[{"commit": {"committer": {"date": "2026-09-14T11:00:00Z"}}}]`,
188 )
189 if unaddressed(a) {
190 t.Error("conversation comment followed by a newer commit must count as addressed")
191 }
192}
193
194func TestHasUnaddressedTrustedComments_UntrustedConversationComment(t *testing.T) {
195 a := activityFixtureWithConversation(t,
196 `[]`,
197 `[]`,
198 `[{"user": {"login": "drive-by-user"}, "body": "please rename this", "created_at": "2026-09-14T10:00:00Z"}]`,
199 `[{"commit": {"committer": {"date": "2026-09-14T09:00:00Z"}}}]`,
200 )
201 if unaddressed(a) {
202 t.Error("untrusted user's conversation comment must not trigger dispatch")
203 }
204}
205
206// Fix agents post as the operator, so their replies carry a trusted login and
207// land after the push. Counting them would redispatch a fix agent on every
208// reply it writes.
209func TestHasUnaddressedTrustedComments_IgnoresAgentReplies(t *testing.T) {
210 reply := `"done, renamed ` + pipeline.AgentReplyMarker + `"`
211 a := activityFixtureWithConversation(t,
212 `[{"id": 200, "user": {"login": "gemini-code-assist[bot]"}, "state": "COMMENTED", "submitted_at": "2026-09-14T12:00:00Z"}]`,
213 `[{"pull_request_review_id": 200, "body": `+reply+`}]`,
214 `[{"user": {"login": "gemini-code-assist[bot]"}, "body": "please rename this", "created_at": "2026-09-14T10:00:00Z"},
215 {"user": {"login": "gemini-code-assist[bot]"}, "body": `+reply+`, "created_at": "2026-09-14T12:00:00Z"}]`,
216 `[{"commit": {"committer": {"date": "2026-09-14T11:00:00Z"}}}]`,
217 )
218 if unaddressed(a) {
219 t.Error("agent replies (inline or conversation) newer than the push must not count as unaddressed")
220 }
221}
222
223// The operator and fix agents post through the PR author's account, so a
224// plain author conversation comment is not feedback, even when the author is
225// a trusted reviewer.
226func TestHasUnaddressedTrustedComments_AuthorConversationCommentIgnored(t *testing.T) {
227 a := activityFixtureWithAuthor(t, "gemini-code-assist[bot]",
228 `[]`,
229 `[]`,
230 `[{"user": {"login": "gemini-code-assist[bot]"}, "body": "CI is green, merging once approved", "created_at": "2026-09-14T10:00:00Z", "updated_at": "2026-09-14T10:00:00Z"}]`,
231 `[{"commit": {"committer": {"date": "2026-09-14T09:00:00Z"}}}]`,
232 )
233 if unaddressed(a) {
234 t.Error("PR author's conversation comment without opt-in must not trigger dispatch")
235 }
236}
237
238func TestHasUnaddressedTrustedComments_AuthorConversationCommentOptedInWithFixCommand(t *testing.T) {
239 a := activityFixtureWithAuthor(t, "gemini-code-assist[bot]",
240 `[]`,
241 `[]`,
242 `[{"user": {"login": "gemini-code-assist[bot]"}, "body": "`+pipeline.FixCommand+`\nplease rename this", "created_at": "2026-09-14T10:00:00Z", "updated_at": "2026-09-14T10:00:00Z"}]`,
243 `[{"commit": {"committer": {"date": "2026-09-14T09:00:00Z"}}}]`,
244 )
245 if !unaddressed(a) {
246 t.Error("PR author's conversation comment opening with /klaus fix must count as unaddressed")
247 }
248}
249
250func TestHasUnaddressedTrustedComments_AuthorConversationCommentOptedInWithMarker(t *testing.T) {
251 a := activityFixtureWithAuthor(t, "gemini-code-assist[bot]",
252 `[]`,
253 `[]`,
254 `[{"user": {"login": "gemini-code-assist[bot]"}, "body": "please rename this\n`+pipeline.ActionableMarker+`", "created_at": "2026-09-14T10:00:00Z", "updated_at": "2026-09-14T10:00:00Z"}]`,
255 `[{"commit": {"committer": {"date": "2026-09-14T09:00:00Z"}}}]`,
256 )
257 if !unaddressed(a) {
258 t.Error("PR author's conversation comment carrying the actionable marker must count as unaddressed")
259 }
260}
261
262// A fix agent's reply that forgets its marker lands after the push as an
263// author comment. Even quoting the opted-in original must not re-arm it.
264func TestHasUnaddressedTrustedComments_AgentReplyMissingMarkerIgnored(t *testing.T) {
265 a := activityFixtureWithAuthor(t, "gemini-code-assist[bot]",
266 `[]`,
267 `[]`,
268 `[{"user": {"login": "gemini-code-assist[bot]"}, "body": "`+pipeline.FixCommand+` please rename this", "created_at": "2026-09-14T10:00:00Z", "updated_at": "2026-09-14T10:00:00Z"},
269 {"user": {"login": "gemini-code-assist[bot]"}, "body": "> `+pipeline.FixCommand+` please rename this\n> `+pipeline.ActionableMarker+`\n\nDone, renamed.", "created_at": "2026-09-14T12:00:00Z", "updated_at": "2026-09-14T12:00:00Z"}]`,
270 `[{"commit": {"committer": {"date": "2026-09-14T11:00:00Z"}}}]`,
271 )
272 if unaddressed(a) {
273 t.Error("agent reply without its marker must not trigger dispatch: it is an author comment without opt-in")
274 }
275}
276
277func TestHasUnaddressedTrustedComments_NonAuthorTrustedConversationCommentNeedsNoOptIn(t *testing.T) {
278 a := activityFixtureWithAuthor(t, "maintainer",
279 `[]`,
280 `[]`,
281 `[{"user": {"login": "gemini-code-assist[bot]"}, "body": "please rename this", "created_at": "2026-09-14T10:00:00Z", "updated_at": "2026-09-14T10:00:00Z"}]`,
282 `[{"commit": {"committer": {"date": "2026-09-14T09:00:00Z"}}}]`,
283 )
284 if !unaddressed(a) {
285 t.Error("trusted non-author conversation comment must count as unaddressed without opt-in")
286 }
287}
288
289// A reviewer edits a comment created before the latest push to add new
290// feedback: updated_at, not created_at, must be compared to the push.
291func TestHasUnaddressedTrustedComments_EditedConversationCommentAfterPush(t *testing.T) {
292 a := activityFixtureWithConversation(t,
293 `[]`,
294 `[]`,
295 `[{"user": {"login": "gemini-code-assist[bot]"}, "body": "please rename this\n\nEdit: the test helper too", "created_at": "2026-09-14T08:00:00Z", "updated_at": "2026-09-14T10:00:00Z"}]`,
296 `[{"commit": {"committer": {"date": "2026-09-14T09:00:00Z"}}}]`,
297 )
298 if !unaddressed(a) {
299 t.Error("conversation comment edited after the latest push must count as unaddressed")
300 }
301}
302
303// `klaus review --post` posts through the operator's gh account, which need not be in trusted_reviewers.
304func TestHasUnaddressedTrustedComments_OperatorCrossReview(t *testing.T) {
305 for _, tt := range []struct {
306 login string
307 want bool
308 }{
309 {login: "maintainer", want: true}, // the authenticated gh user
310 {login: "drive-by-user", want: false}, // copied marker grants nothing
311 } {
312 t.Run(tt.login, func(t *testing.T) {
313 body := `"**Cross-model review** by codex\n\n` + pipeline.CrossReviewMarker + ` backend=codex model= sha=abc -->"`
314 a := activityFixtureWithAuthor(t, "maintainer",
315 `[{"id": 300, "user": {"login": "`+tt.login+`"}, "state": "COMMENTED", "submitted_at": "2026-09-18T10:00:00Z", "body": `+body+`}]`,
316 `[{"pull_request_review_id": 300, "body": "nil deref"}]`,
317 `[]`,
318 `[{"commit": {"committer": {"date": "2026-09-18T09:00:00Z"}}}]`,
319 )
320 if got := unaddressed(a); got != tt.want {
321 t.Errorf("cross-review by %s: unaddressed = %v, want %v", tt.login, got, tt.want)
322 }
323 })
324 }
325}