Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
fdb112d
🌱 refactor: split src/pkg/dashboard/api_repo_item_decision_test.go in…
hivecommons-hive[bot] Oct 30, 2026
123fa43
Merge branch 'v5' into scanner/fix-15702
hivecommons-hive[bot] Oct 30, 2026
136b27a
Merge branch 'v5' into scanner/fix-15702
hivecommons-hive[bot] Oct 30, 2026
2922d97
Merge branch 'v5' into scanner/fix-15702
hivecommons-hive[bot] Oct 30, 2026
120a274
Merge branch 'v5' into scanner/fix-15702
hivecommons-hive[bot] Oct 30, 2026
3f94016
Merge branch 'v5' into scanner/fix-15702
hivecommons-hive[bot] Oct 30, 2026
919670b
Merge branch 'v5' into scanner/fix-15702
hivecommons-hive[bot] Oct 30, 2026
231e762
Merge branch 'v5' into scanner/fix-15702
hivecommons-hive[bot] Oct 30, 2026
bb12546
Merge branch 'v5' into scanner/fix-15702
hivecommons-hive[bot] Oct 30, 2026
dc21fe0
Merge branch 'v5' into scanner/fix-15702
hivecommons-hive[bot] Oct 30, 2026
3271500
Merge branch 'v5' into scanner/fix-15702
hivecommons-hive[bot] Oct 30, 2026
267dc4e
Merge branch 'v5' into scanner/fix-15702
hivecommons-hive[bot] Oct 30, 2026
f0d254b
Merge branch 'v5' into scanner/fix-15702
hivecommons-hive[bot] Oct 30, 2026
74d30c5
Merge branch 'v5' into scanner/fix-15702
hivecommons-hive[bot] Oct 30, 2026
aa40359
Merge branch 'v5' into scanner/fix-15702
hivecommons-hive[bot] Oct 30, 2026
8fb2197
Merge branch 'v5' into scanner/fix-15702
hivecommons-hive[bot] Oct 30, 2026
1b40bc4
Merge branch 'v5' into scanner/fix-15702
hivecommons-hive[bot] Oct 30, 2026
b749748
Merge branch 'v5' into scanner/fix-15702
hivecommons-hive[bot] Oct 30, 2026
998e825
Merge branch 'v5' into scanner/fix-15702
hivecommons-hive[bot] Oct 30, 2026
e6396ba
Merge branch 'v5' into scanner/fix-15702
hivecommons-hive[bot] Oct 30, 2026
2f92734
Merge branch 'v5' into scanner/fix-15702
hivecommons-hive[bot] Oct 30, 2026
29bdccc
Merge branch 'v5' into scanner/fix-15702
hivecommons-hive[bot] Oct 30, 2026
74e9eae
Merge branch 'v5' into scanner/fix-15702
hivecommons-hive[bot] Oct 30, 2026
1fbb865
Merge branch 'v5' into scanner/fix-15702
hivecommons-hive[bot] Oct 30, 2026
afe4fc0
Merge branch 'v5' into scanner/fix-15702
hivecommons-hive[bot] Oct 30, 2026
a4eb40d
Merge branch 'v5' into scanner/fix-15702
hivecommons-hive[bot] Oct 30, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/changed-15702-decision-test-split.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- refactor: split `src/pkg/dashboard/api_repo_item_decision_test.go` into 4 files (`api_repo_item_decision_test.go`, `_approve_test.go`, `_close_test.go`, `_merge_test.go`) to cut rebase churn on a merge hotspot; no behaviour change ([#15702](https://github.com/hivecommons/hive/issues/15702)).
176 changes: 176 additions & 0 deletions src/pkg/dashboard/api_repo_item_decision_approve_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
package dashboard

import (
"encoding/json"
"net/http"
"strings"
"testing"
"time"

"github.com/hivecommons/hive/pkg/github"
"github.com/hivecommons/hive/pkg/stagelabels"
)

func TestRepoItemDecisionApproveUnparksAndAudits(t *testing.T) {
mock := &repoItemDecisionMock{issueLabels: []string{"bug", "stage/045-needs-decision"}}
srv := newRepoItemDecisionServer(t, mock)

w := postRepoItemDecision(t, srv, 7, "writer", `{"decision":"approve"}`)
if w.Code != http.StatusOK {
t.Fatalf("approve: code=%d body=%s", w.Code, w.Body.String())
}
var resp repoItemDecisionResponse
_ = json.Unmarshal(w.Body.Bytes(), &resp)
if !resp.OK || resp.Status != "unparked" || resp.Label != github.HumanAckLabel {
t.Fatalf("response = %+v", resp)
}
// approved-direction is applied and the stage engine re-derives the
// stage off the park; nothing else may be written.
if len(mock.added) == 0 || mock.added[0] != github.HumanAckLabel {
t.Fatalf("added = %v, want %s first", mock.added, github.HumanAckLabel)
}
for _, l := range mock.added[1:] {
if !strings.HasPrefix(l, "stage/") {
t.Fatalf("unexpected label written: %q (all: %v)", l, mock.added)
}
}
if len(mock.edits) != 0 {
t.Fatalf("approve must not close anything, got edits %v", mock.edits)
}
if len(mock.comments) != 1 || !strings.Contains(mock.comments[0], "Un-parked by @writer") {
t.Fatalf("comments = %v", mock.comments)
}
if !strings.Contains(strings.Join(decisionAuditActions(srv), ","), auditRepoItemDecisionApprove) {
t.Fatalf("audit missing %s: %v", auditRepoItemDecisionApprove, decisionAuditActions(srv))
}
}

func TestRepoItemDecisionDirectionRequiresTextAndParkedIssue(t *testing.T) {
mock := &repoItemDecisionMock{issueLabels: []string{"bug", "stage/045-needs-decision"}}
srv := newRepoItemDecisionServer(t, mock)

if w := postRepoItemDecision(t, srv, 7, "writer", `{"decision":"direction"}`); w.Code != http.StatusBadRequest {
t.Fatalf("direction without text: code=%d body=%s", w.Code, w.Body.String())
}
w := postRepoItemDecision(t, srv, 7, "writer", `{"decision":"direction","comment":"ship option B"}`)
if w.Code != http.StatusOK {
t.Fatalf("direction: code=%d body=%s", w.Code, w.Body.String())
}
if len(mock.comments) != 1 || !strings.Contains(mock.comments[0], "Decision: ship option B") {
t.Fatalf("comments = %v", mock.comments)
}

mock.issueLabels = []string{"bug"}
if w := postRepoItemDecision(t, srv, 7, "writer", `{"decision":"approve"}`); w.Code != http.StatusConflict {
t.Fatalf("approve on un-parked issue: code=%d want 409 body=%s", w.Code, w.Body.String())
}
// A PR sent as an issue is told to resend as type "pr" rather than
// being run through the issue un-park.
if w := postRepoItemDecision(t, srv, 8, "writer", `{"decision":"approve"}`); w.Code != http.StatusBadRequest {
t.Fatalf("approve on PR typed as issue: code=%d want 400 body=%s", w.Code, w.Body.String())
}
}

func TestRepoItemDecisionApprovePRPostsLgtmAndApproveComments(t *testing.T) {
mock := &repoItemDecisionMock{}
srv := newRepoItemDecisionServer(t, mock)

w := postRepoItemDecision(t, srv, 8, "writer", `{"decision":"approve","type":"pr"}`)
if w.Code != http.StatusOK {
t.Fatalf("approve PR: code=%d body=%s", w.Code, w.Body.String())
}
var resp repoItemDecisionResponse
_ = json.Unmarshal(w.Body.Bytes(), &resp)
if resp.Status != "approval-commented" {
t.Fatalf("status = %q", resp.Status)
}
if len(mock.comments) != 2 || !strings.HasPrefix(mock.comments[0], "/lgtm\n") || !strings.HasPrefix(mock.comments[1], "/approve\n") {
t.Fatalf("comments = %q", mock.comments)
}
if !strings.Contains(mock.comments[0], "@writer") {
t.Fatalf("command comment must name the operator: %q", mock.comments[0])
}
if len(mock.added)+len(mock.edits) != 0 {
t.Fatalf("approval must not label or edit the PR (no merge): added=%v edits=%v", mock.added, mock.edits)
}
if actions := decisionAuditActions(srv); len(actions) == 0 || actions[0] != auditRepoItemDecisionApprove {
t.Fatalf("audit = %v", actions)
}
}

// TestRepoItemDecisionApprovePRReleasesThePark: the owner's Approve on a
// parked PR is the human review it waited for — the approval is recorded
// against the head (the lanes and the stage engine read it as the answer to
// requires_human / decision-requested), `needs-human` and the sentinel label
// come off, and the tracker cache is dropped so the next read re-derives the
// stage (hivecommons/hive#14821, #14822, crux#30 stayed at 085 with only the
// command comments written).
func TestRepoItemDecisionApprovePRReleasesThePark(t *testing.T) {
mock := &repoItemDecisionMock{prForeign: true, prLabels: []string{"stage/085-needs-decision", "needs-human", "sentinel-alert", "role/trusted"}}
srv := newRepoItemDecisionServer(t, mock)
// Own store: with no stage record there is no head admission to
// report (hivecommons/hive#15136), whatever other tests left in the
// process-wide store.
srv.stageStore = stagelabels.NewStore("", time.Now)
srv.stageTrackerCacheMu.Lock()
gen := srv.stageTrackerGen
srv.stageTrackerCacheMu.Unlock()

w := postRepoItemDecision(t, srv, 8, "writer", `{"decision":"approve","type":"pr"}`)
if w.Code != http.StatusOK {
t.Fatalf("approve PR: code=%d body=%s", w.Code, w.Body.String())
}
var resp repoItemDecisionResponse
_ = json.Unmarshal(w.Body.Bytes(), &resp)
if resp.Status != "approval-commented" || strings.Join(resp.Released, ",") != "approval-recorded,needs-human,sentinel-label" {
t.Fatalf("response = %+v", resp)
}
req, ok := repoItemDecisionChangeRequests().For("testorg/testrepo", 8, "feedfacefeedfacefeedfacefeedfacefeedface")
if !ok || !req.IsApproval() || req.Actor != "writer" {
t.Fatalf("approval must stand against the head: %+v ok=%v", req, ok)
}
if strings.Join(mock.removed, ",") != "needs-human,085-needs-decision,sentinel-alert" {
t.Fatalf("removed = %v", mock.removed)
}
if len(mock.comments) != 2 || len(mock.added)+len(mock.edits) != 0 {
t.Fatalf("approval still posts the two commands and nothing else: comments=%d added=%v edits=%v", len(mock.comments), mock.added, mock.edits)
}
srv.stageTrackerCacheMu.Lock()
stale, after := srv.stageTrackerCache.stale, srv.stageTrackerGen
srv.stageTrackerCacheMu.Unlock()
if !stale || after == gen {
t.Fatalf("tracker cache must be invalidated: stale=%v gen %d -> %d", stale, gen, after)
}
detail := srv.GetAudit().Recent(1)[0].Detail
if !strings.Contains(detail, "released=approval-recorded,needs-human,sentinel-label") {
t.Fatalf("audit detail = %q", detail)
}
}

func TestRepoItemDecisionDirectionPRRecordsChangeRequest(t *testing.T) {
mock := &repoItemDecisionMock{}
srv := newRepoItemDecisionServer(t, mock)

w := postRepoItemDecision(t, srv, 8, "writer", `{"decision":"direction","type":"pr","comment":"drop the retry loop"}`)
if w.Code != http.StatusOK {
t.Fatalf("direction PR: code=%d body=%s", w.Code, w.Body.String())
}
req, ok := repoItemDecisionChangeRequests().For("testorg/testrepo", 8, "feedfacefeedfacefeedfacefeedfacefeedface")
if !ok || req.Body != "drop the retry loop" || req.Actor != "writer" {
t.Fatalf("stored request = %+v ok=%v", req, ok)
}
if len(mock.comments) != 1 || !strings.Contains(mock.comments[0], "Decision recorded by @writer") {
t.Fatalf("comments = %q", mock.comments)
}
if actions := decisionAuditActions(srv); len(actions) == 0 || actions[0] != auditRepoItemDecisionDirection {
t.Fatalf("audit = %v", actions)
}

mock.prForeign = true
if w := postRepoItemDecision(t, srv, 8, "writer", `{"decision":"direction","type":"pr","comment":"x"}`); w.Code != http.StatusConflict {
t.Fatalf("foreign PR direction: code=%d want 409 body=%s", w.Code, w.Body.String())
}
if w := postRepoItemDecision(t, srv, 8, "writer", `{"decision":"direction","type":"pr"}`); w.Code != http.StatusBadRequest {
t.Fatalf("direction without text: code=%d want 400", w.Code)
}
}
169 changes: 169 additions & 0 deletions src/pkg/dashboard/api_repo_item_decision_close_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
package dashboard

import (
"encoding/json"
"net/http"
"strings"
"testing"

"github.com/hivecommons/hive/pkg/config"
"github.com/hivecommons/hive/pkg/github"
)

func TestRepoItemDecisionCloseWontFixCommentsAndCloses(t *testing.T) {
mock := &repoItemDecisionMock{issueLabels: []string{"bug"}}
srv := newRepoItemDecisionServer(t, mock)

w := postRepoItemDecision(t, srv, 7, "writer", `{"decision":"close-wontfix","comment":"out of scope for v5"}`)
if w.Code != http.StatusOK {
t.Fatalf("close: code=%d body=%s", w.Code, w.Body.String())
}
if len(mock.comments) != 1 || !strings.Contains(mock.comments[0], "won't fix by @writer") || !strings.Contains(mock.comments[0], "out of scope for v5") {
t.Fatalf("comments = %v", mock.comments)
}
if len(mock.edits) != 1 || mock.edits[0]["state"] != "closed" || mock.edits[0]["state_reason"] != issueStateReasonNotPlanned {
t.Fatalf("edits = %v", mock.edits)
}
if !strings.Contains(strings.Join(decisionAuditActions(srv), ","), auditRepoItemDecisionClose) {
t.Fatalf("audit missing %s: %v", auditRepoItemDecisionClose, decisionAuditActions(srv))
}
}

// close-read (hivecommons/hive#15381): an advisory issue is closed as
// completed through the same comment + close path; a PR is refused.
func TestRepoItemDecisionCloseReadClosesAsCompleted(t *testing.T) {
mock := &repoItemDecisionMock{issueLabels: []string{"triage/advisory"}}
srv := newRepoItemDecisionServer(t, mock)

w := postRepoItemDecision(t, srv, 7, "writer", `{"decision":"close-read"}`)
if w.Code != http.StatusOK {
t.Fatalf("close-read: code=%d body=%s", w.Code, w.Body.String())
}
if len(mock.comments) != 1 || !strings.Contains(mock.comments[0], "Closed as read (advisory acknowledged) by @writer") {
t.Fatalf("comments = %v", mock.comments)
}
if len(mock.edits) != 1 || mock.edits[0]["state"] != "closed" || mock.edits[0]["state_reason"] != issueStateReasonCompleted {
t.Fatalf("edits = %v", mock.edits)
}
var resp repoItemDecisionResponse
_ = json.Unmarshal(w.Body.Bytes(), &resp)
if resp.Status != "closed" || resp.Result == nil || resp.Result.Outcome != "Closed as read (advisory acknowledged)." {
t.Fatalf("resp = %+v", resp)
}
if w := postRepoItemDecision(t, srv, 8, "writer", `{"decision":"close-read","type":"pr"}`); w.Code != http.StatusBadRequest {
t.Fatalf("close-read on a PR: code=%d want 400 body=%s", w.Code, w.Body.String())
}
}

func TestRepoItemDecisionCloseSupersededPRUsesSweepPath(t *testing.T) {
mock := &repoItemDecisionMock{}
srv := newRepoItemDecisionServer(t, mock)

w := postRepoItemDecision(t, srv, 8, "writer", `{"decision":"close-superseded","type":"pr"}`)
if w.Code != http.StatusOK {
t.Fatalf("close superseded: code=%d body=%s", w.Code, w.Body.String())
}
var resp repoItemDecisionResponse
_ = json.Unmarshal(w.Body.Bytes(), &resp)
if resp.Label != github.SupersededLabel {
t.Fatalf("label = %q, want %s", resp.Label, github.SupersededLabel)
}
if strings.Join(mock.added, ",") != github.SupersededLabel {
t.Fatalf("added = %v", mock.added)
}
if len(mock.edits) != 1 || mock.edits[0]["state"] != "closed" {
t.Fatalf("edits = %v", mock.edits)
}
}

func TestRepoItemDecisionCloseSupersededByNamesTheWinner(t *testing.T) {
mock := &repoItemDecisionMock{}
srv := newRepoItemDecisionServer(t, mock)

w := postRepoItemDecision(t, srv, 8, "writer", `{"decision":"close-superseded","type":"pr","superseded_by":"#458"}`)
if w.Code != http.StatusOK {
t.Fatalf("close superseded by: code=%d body=%s", w.Code, w.Body.String())
}
if len(mock.comments) != 1 || !strings.Contains(mock.comments[0], "superseded by testorg/testrepo#458 by @writer") {
t.Fatalf("comments = %q", mock.comments)
}
if w := postRepoItemDecision(t, srv, 8, "writer", `{"decision":"close-superseded","type":"pr","superseded_by":"#8"}`); w.Code != http.StatusBadRequest {
t.Fatalf("self supersession: code=%d want 400", w.Code)
}
if w := postRepoItemDecision(t, srv, 8, "writer", `{"decision":"close-superseded","type":"pr","superseded_by":"nope"}`); w.Code != http.StatusBadRequest {
t.Fatalf("garbage ref: code=%d want 400", w.Code)
}
}

func TestParseRepoItemSupersededBy(t *testing.T) {
cases := map[string]string{
"": "",
"458": "o/r#458",
" #458 ": "o/r#458",
"acme/console#12": "acme/console#12",
"https://github.com/acme/console/pull/12": "acme/console#12",
"https://github.com/acme/console/issues/12/": "acme/console#12",
"https://ghe.example.com/acme/console/pull/3": "acme/console#3",
}
for in, want := range cases {
got, err := parseRepoItemSupersededBy(in, "o/r")
if err != nil || got != want {
t.Errorf("parse(%q) = %q, %v; want %q", in, got, err, want)
}
}
for _, bad := range []string{"x", "#0", "r#4", "https://github.com/acme/console", "a/b/c#4"} {
if _, err := parseRepoItemSupersededBy(bad, "o/r"); err == nil {
t.Errorf("parse(%q) must fail", bad)
}
}
}

// The real hivecommons/hive#15381 shape (a dedupe-checklist advisory: the
// triage/advisory label, a "Decision required: no" body, bot author) closes
// as completed with the acknowledgement comment; on a paused repo the same
// request is a 200 refused result whose Outcome says why, and nothing is
// written (hivecommons/hive#15436).
func TestRepoItemDecisionCloseReadAdvisoryFixtureAndPausedRefusal(t *testing.T) {
mock := &repoItemDecisionMock{
issueLabels: []string{"help wanted", "agent/dedupe", "triage/advisory"},
issueTitle: "triage: duplicate and overlapping open issues/PRs to consolidate",
issueBody: "<!-- hive:dedupe-checklist repo=hivecommons/hive -->\nDecision required: no\n\n## Summary\n\nTriage pass over the 28 open issues and 12 open PRs. Found 0 near-duplicate pairs.",
}
srv := newRepoItemDecisionServer(t, mock)

w := postRepoItemDecision(t, srv, 7, "writer", `{"decision":"close-read","type":"issue"}`)
if w.Code != http.StatusOK {
t.Fatalf("close-read: code=%d body=%s", w.Code, w.Body.String())
}
var resp repoItemDecisionResponse
_ = json.Unmarshal(w.Body.Bytes(), &resp)
if resp.Status != "closed" || resp.Result == nil || resp.Result.Action != rowActionClosed {
t.Fatalf("resp = %+v", resp)
}
if len(mock.comments) != 1 || !strings.Contains(mock.comments[0], "Closed as read (advisory acknowledged) by @writer") {
t.Fatalf("comments = %v", mock.comments)
}
if len(mock.edits) != 1 || mock.edits[0]["state"] != "closed" || mock.edits[0]["state_reason"] != issueStateReasonCompleted {
t.Fatalf("edits = %v", mock.edits)
}

// Paused repo: refused with a reason, nothing posted.
mock.comments, mock.edits = nil, nil
srv.deps.Config.Project.PausedRepos = []config.RepoPause{{Repo: "testrepo", By: "clubanderson", Reason: "release freeze"}}
t.Cleanup(func() { srv.deps.Config.Project.PausedRepos = nil })
w = postRepoItemDecision(t, srv, 7, "writer", `{"decision":"close-read","type":"issue"}`)
if w.Code != http.StatusOK {
t.Fatalf("paused close-read: code=%d body=%s", w.Code, w.Body.String())
}
resp = repoItemDecisionResponse{}
_ = json.Unmarshal(w.Body.Bytes(), &resp)
if resp.Status != rowActionRefused || resp.Result == nil || resp.Result.Action != rowActionRefused {
t.Fatalf("paused resp = %+v", resp)
}
if !strings.HasPrefix(resp.Result.Outcome, "Close refused: repo paused") || !strings.Contains(resp.Result.Outcome, "release freeze") {
t.Fatalf("outcome = %q, want the pause and its reason", resp.Result.Outcome)
}
if len(mock.comments)+len(mock.edits) != 0 {
t.Fatalf("a refused close must write nothing: comments=%v edits=%v", mock.comments, mock.edits)
}
}
Loading
Loading