Files
archie/internal/forgerpc/forgerpc_test.go
T
sam 170fe0250b fix: close the duplicate-work loop and the dead paths around it
Applies the four-reviewer pass over the forge/dashboard-actions work.
The through-line is that several capabilities were wired but inert, and
one of them could cost real work.

DUPLICATE WORK. "Closes #N" was dropped from PR bodies in favour of
LinkBranch, which is sidebar linkage on Gitea and a no-op stub on
GitHub -- neither closes anything, and nothing else did either. So a
merged task left its issue open, labelled and assigned; the next poll
re-enqueued it, and once the dashboard's Clear removed the task row that
became a second implementation and a second PR for work already merged.
reconcilePRs now closes the issue on merge, and a rejected task closes
its issue too. A PR closed without merging deliberately leaves the issue
open: that usually means "try again".

MAX RETRIES. Configured, defaulted and reported by /api/config, and
enforced nowhere -- markDead was unreachable and EffectiveMaxRetries had
no callers, so the dashboard's retry incremented retry_count forever.
The retry action now retires a task to dead at the cap.

DEAD ON ARRIVAL. Tasks() never selected plan or source, so the
"Decision required" panel could not render and chat tasks -- whose issue
number is synthetic -- linked to forge issues that do not exist.

COSMETIC FAILURE, FATAL RESULT. OpenPR failed the whole stage when
LinkBranch errored, so the pull request was never opened and the task
parked; a retry re-links the same branch, which Gitea answers with a
conflict, so every retry parked again. It is best-effort now.

INVISIBLE ACTIONS. Approve/retry/reject wrote store rows and nothing
else, so the timeline and activity stream never showed that a human had
intervened. They emit events now.

Also: store write failures answer 500 rather than 409; the dashboard
surfaces the server's actual reason instead of "409 Conflict"; action
buttons stop propagation (acting on a task no longer also expands it),
disable in flight, keep per-task errors instead of one shared string,
and confirm before rejecting.

Removed: the maybeRetryParked shim and markDead with their now-vacuous
tests, WaitingTasks (no consumers; its tests were rewritten to assert
the same routing through TaskByID), write-only Daemon.Runtime, and
dir/dirs response bloat from /api/skills. skillSource checked workDir
before sharedDir, mislabelling every shared skill when the two are equal.
DefaultRoots now dedupes roots differing only by a trailing separator.

The air build rule claiming to keep ui/dist current never ran -- it
required one file to satisfy include_dir and include_file together, and
ui/ is excluded from the walk. Deleted with the comment that promised it.
task test:ui joins task check and CI; it ran nowhere before, so the
dashboard suite could rot silently.

Docs corrected where they described behaviour that no longer exists:
parks no longer comment on the issue, state labels no longer drive
retries, and forgerpc's doc claimed four proxied methods while listing
three of five.
2026-08-05 03:54:25 +10:00

242 lines
7.0 KiB
Go

package forgerpc
import (
"context"
"errors"
"log/slog"
"testing"
"time"
"github.com/nats-io/nats-server/v2/server"
natssrv "github.com/nats-io/nats-server/v2/test"
"github.com/nats-io/nats.go"
"github.com/samcharles93/archie-core/internal/forge"
)
func startEmbedded(t *testing.T) *server.Server {
t.Helper()
srv := natssrv.RunRandClientPortServer()
t.Cleanup(srv.Shutdown)
return srv
}
func connect(t *testing.T, url string) *nats.Conn {
t.Helper()
nc, err := nats.Connect(url)
if err != nil {
t.Fatalf("nats connect: %v", err)
}
t.Cleanup(nc.Close)
return nc
}
// fakeForge implements forge.Forge, recording calls to the four methods
// workflow stages invoke mid-run. Every other method panics if called --
// this RPC layer must never proxy them.
type fakeForge struct {
comments []commentCall
closes []closeCall
prs []prCall
stateLabels []stateLabelCall
branches []branchCall
commentErr error
closeErr error
prErr error
}
type commentCall struct {
owner, repo string
number int
body string
}
type closeCall struct {
owner, repo string
number int
comment string
}
type prCall struct {
owner, repo, title, head, base, body string
}
type stateLabelCall struct {
owner, repo string
number int
label string
knownLabels []string
}
type branchCall struct {
owner, repo, branch string
number int
}
func (f *fakeForge) Comment(_ context.Context, owner, repo string, number int, body string) (int64, error) {
f.comments = append(f.comments, commentCall{owner, repo, number, body})
if f.commentErr != nil {
return 0, f.commentErr
}
return 99, nil
}
func (f *fakeForge) CloseIssue(_ context.Context, owner, repo string, number int, comment string) error {
f.closes = append(f.closes, closeCall{owner, repo, number, comment})
return f.closeErr
}
func (f *fakeForge) CreatePR(_ context.Context, owner, repo, title, head, base, body string) (int, error) {
f.prs = append(f.prs, prCall{owner, repo, title, head, base, body})
if f.prErr != nil {
return 0, f.prErr
}
return 7, nil
}
func (f *fakeForge) SetStateLabel(_ context.Context, owner, repo string, number int, label string, knownLabels []string) {
f.stateLabels = append(f.stateLabels, stateLabelCall{owner, repo, number, label, knownLabels})
}
func (f *fakeForge) LinkBranch(_ context.Context, owner, repo string, number int, branch string) error {
f.branches = append(f.branches, branchCall{owner: owner, repo: repo, number: number, branch: branch})
return nil
}
func (f *fakeForge) AcceptInvitations(context.Context) error { panic("unexpected call") }
func (f *fakeForge) AssignedIssues(context.Context, string, string, string) ([]forge.Issue, error) {
panic("unexpected call")
}
func (f *fakeForge) IssuesWithLabel(context.Context, string, string, string) ([]forge.Issue, error) {
panic("unexpected call")
}
func (f *fakeForge) RepliesAfter(context.Context, string, string, int, int64, string) ([]forge.Reply, error) {
panic("unexpected call")
}
func (f *fakeForge) PRState(context.Context, string, string, int) (string, error) {
panic("unexpected call")
}
func (f *fakeForge) CreateIssue(context.Context, string, string, string, string, []string) (int, error) {
panic("unexpected call")
}
func (f *fakeForge) React(context.Context, string, string, int, string) error {
panic("unexpected call")
}
func (f *fakeForge) VerifyPush(context.Context, string, string) error { panic("unexpected call") }
func newTestServer(t *testing.T) (*fakeForge, *Client) {
t.Helper()
fg := &fakeForge{}
srv := startEmbedded(t)
url := srv.ClientURL()
serverConn := connect(t, url)
rpcServer := &Server{Forge: fg, Log: slog.New(slog.DiscardHandler)}
unsub, err := rpcServer.Register(serverConn)
if err != nil {
t.Fatalf("register: %v", err)
}
t.Cleanup(unsub)
clientConn := connect(t, url)
client := &Client{Conn: clientConn, Timeout: 2 * time.Second}
return fg, client
}
func TestClientCommentPersistsViaServer(t *testing.T) {
fg, client := newTestServer(t)
id, err := client.Comment(context.Background(), "acme", "widget", 1, "hello")
if err != nil {
t.Fatalf("Comment: %v", err)
}
if id != 99 {
t.Fatalf("expected comment id 99, got %d", id)
}
if len(fg.comments) != 1 || fg.comments[0] != (commentCall{"acme", "widget", 1, "hello"}) {
t.Fatalf("comment did not reach server, got %+v", fg.comments)
}
}
func TestClientCloseIssuePersistsViaServer(t *testing.T) {
fg, client := newTestServer(t)
if err := client.CloseIssue(context.Background(), "acme", "widget", 2, "done"); err != nil {
t.Fatalf("CloseIssue: %v", err)
}
if len(fg.closes) != 1 || fg.closes[0] != (closeCall{"acme", "widget", 2, "done"}) {
t.Fatalf("close did not reach server, got %+v", fg.closes)
}
}
func TestClientCreatePRPersistsViaServer(t *testing.T) {
fg, client := newTestServer(t)
num, err := client.CreatePR(context.Background(), "acme", "widget", "title", "head", "main", "body")
if err != nil {
t.Fatalf("CreatePR: %v", err)
}
if num != 7 {
t.Fatalf("expected PR number 7, got %d", num)
}
if len(fg.prs) != 1 || fg.prs[0] != (prCall{"acme", "widget", "title", "head", "main", "body"}) {
t.Fatalf("PR did not reach server, got %+v", fg.prs)
}
}
func TestClientLinkBranchPersistsViaServer(t *testing.T) {
f, client := newTestServer(t)
if err := client.LinkBranch(context.Background(), "acme", "widget", 4, "fix/4-bug"); err != nil {
t.Fatalf("LinkBranch: %v", err)
}
if len(f.branches) != 1 || f.branches[0] != (branchCall{owner: "acme", repo: "widget", number: 4, branch: "fix/4-bug"}) {
t.Fatalf("branch link did not reach server, got %+v", f.branches)
}
}
func TestClientSetStateLabelPersistsViaServer(t *testing.T) {
fg, client := newTestServer(t)
client.SetStateLabel(context.Background(), "acme", "widget", 3, "archie:working", []string{"archie:queued", "archie:working"})
if len(fg.stateLabels) != 1 {
t.Fatalf("expected 1 state label call, got %d", len(fg.stateLabels))
}
got := fg.stateLabels[0]
if got.owner != "acme" || got.repo != "widget" || got.number != 3 || got.label != "archie:working" {
t.Fatalf("state label call mismatch: %+v", got)
}
}
func TestClientPropagatesServerError(t *testing.T) {
fg := &fakeForge{commentErr: errors.New("forge unavailable")}
srv := startEmbedded(t)
url := srv.ClientURL()
rpcServer := &Server{Forge: fg, Log: slog.New(slog.DiscardHandler)}
unsub, err := rpcServer.Register(connect(t, url))
if err != nil {
t.Fatalf("register: %v", err)
}
t.Cleanup(unsub)
client := &Client{Conn: connect(t, url), Timeout: 2 * time.Second}
_, err = client.Comment(context.Background(), "acme", "widget", 1, "hello")
if err == nil {
t.Fatal("expected Comment to propagate the server-side error")
}
}
func TestClientCommentTimesOutWithNoResponder(t *testing.T) {
srv := startEmbedded(t)
client := &Client{Conn: connect(t, srv.ClientURL()), Timeout: 100 * time.Millisecond}
_, err := client.Comment(context.Background(), "acme", "widget", 1, "hello")
if err == nil {
t.Fatal("expected Comment to time out with no server registered")
}
}