watchpr: emit the baseline under --json

The baseline is the only notice that a watch started against an already
conflicted PR, and it was missing from the mode automation uses. It goes to
stderr as a JSON record, leaving stdout a single result record.
This commit is contained in:
2026-09-26 21:14:20 +10:00
parent 27e48ac45e
commit 15c527ee56
2 changed files with 79 additions and 9 deletions
+44
View File
@@ -1,6 +1,8 @@
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
@@ -8,6 +10,7 @@ import (
"net/http/httptest"
"strings"
"testing"
"time"
"git.unkin.net/unkin/agent-tools/internal/agent"
)
@@ -212,3 +215,44 @@ func TestBaselineLineNamesSuppressedConditions(t *testing.T) {
t.Errorf("baselineLine = %q, want no suppression note for a clean baseline", got)
}
}
// --json is the mode automation uses, and automation is exactly who needs to be
// told the watch started against an already-conflicted PR -- the one case that
// will never produce an alert. It must therefore be emitted in JSON mode too,
// on stderr, where it cannot corrupt the result record on stdout.
func TestBaselineIsEmittedInJSONMode(t *testing.T) {
st := agent.PRState{
Ref: agent.PRRef{Owner: "unkin", Repo: "repo", Number: 7},
State: "open",
Mergeable: agent.MergeNo,
CIStatus: "failure",
HeadSHA: "cafebabecafebabe",
}
var buf bytes.Buffer
emitBaselines(&buf, []agent.PRState{st}, 30*time.Second, true)
var got baselineRecord
if err := json.Unmarshal(buf.Bytes(), &got); err != nil {
t.Fatalf("baseline is not a JSON record (%v); got %q", err, buf.String())
}
if !got.Baseline {
t.Error("record does not mark itself as the baseline")
}
if got.State.Ref != st.Ref || got.State.Mergeable != agent.MergeNo {
t.Errorf("record state = %+v, want the conflicted snapshot the watch started from", got.State)
}
for _, want := range []string{"already non-mergeable", "CI already failure"} {
if !strings.Contains(got.Suppressed, want) {
t.Errorf("suppressed = %q, want it to mention %q", got.Suppressed, want)
}
}
buf.Reset()
clean := st
clean.Mergeable = agent.MergeYes
clean.CIStatus = "success"
emitBaselines(&buf, []agent.PRState{clean}, 30*time.Second, true)
if strings.Contains(buf.String(), "suppressed") {
t.Errorf("clean baseline = %q, want no suppression field", buf.String())
}
}