Files
archie/internal/taskrun/taskrun_test.go
T
sam 1f3f54bedd test(archied): pin the wiring these fixes depend on and drop text coupling
Two fresh-context reviews of the preceding five fixes found that the tests were
mostly sound but that three things were not: one harness was coupled to shell
source text, one assertion was coupled to a tool count, and -- worse -- the two
changes carrying the most weight were pinned by nothing at all.

Composition order. review_pr worked only because Run builds the worktree manager
before the gateways; no test observed it. Reverting the order left every test
green while the daemon silently stopped offering review_pr, because the producer
guard then returns nil instead of advertising a broken tool. Pin the order by
source position, matching the existing
TestSetupBackendsRegistersContainerCleanupAfterNATS, since no runtime seam can
see it. methodBody now resolves package-level funcs as well as methods.

Composition wiring. The daemon fix's whole point is that resolved routing
bindings reach the worker, yet nothing verified that buildDaemon puts them on the
daemon: with b.kindWorkflows replaced by nil, only the new test fails and both
the daemon and worker packages stay green. That is the "tested in isolation,
never verified wired" shape this work exists to remove, so pin the hop.

The wire contract itself. taskrun.Request's json tags are the interface between
two binaries, and a same-type round trip cannot see a tag rename. Assert the
emitted keys literally; renaming one now fails with the key list.

Brittleness. selfHostConfig no longer string-matches install.sh's heredoc opener
or assumes the delimiter is EOF, and no longer hand-substitutes a copy of
forge_block's output -- it discovers heredocs by shape and selects the one
declaring [nats], a table the assertions never touch. Verified against four
behaviour-preserving refactors that previously failed: renaming the delimiter,
reordering the redirect, factoring the path into a variable, and <<-. The two
[services.gateway] source greps are gone: defaults.go already supplies that
target, so there was no behaviour to pin. review_pr is asserted present by name
rather than by entry count, so a sibling review tool cannot turn it red.

Also carry the published request's decode error instead of discarding it, so a
payload-shape break reports as itself rather than as an empty routing binding.

Every new test was verified non-vacuous by reintroducing the defect.
2026-09-15 02:18:30 +10:00

131 lines
4.1 KiB
Go

package taskrun
import (
"encoding/json"
"testing"
"github.com/samcharles93/archie-core/internal/agentexec"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/domain/workflow"
"github.com/samcharles93/archie-core/internal/domain/workintake"
)
func TestRequestJSONRoundTrip(t *testing.T) {
req := Request{
Task: &workflow.Task{
ID: 1,
Owner: "acme",
Repo: "widget",
IssueNumber: 42,
Title: "feat: thing",
Status: workflow.StatusRunning,
},
Repo: config.Repo{Owner: "acme", Name: "widget", Base: "main"},
Cfg: config.Config{DiffCapLines: 500}.ForTask(),
Providers: map[string]agentexec.Provider{
"anthropic": {Class: "anthropic", APIKeyEnv: "ANTHROPIC_API_KEY"},
},
WorktreeGrant: "grant",
}
data, err := json.Marshal(req)
if err != nil {
t.Fatalf("marshal: %v", err)
}
var got Request
if err := json.Unmarshal(data, &got); err != nil {
t.Fatalf("unmarshal: %v", err)
}
if got.Task == nil || got.Task.ID != 1 || got.Task.Owner != "acme" || got.Task.IssueNumber != 42 {
t.Fatalf("Task did not round-trip: %+v", got.Task)
}
if got.Repo.FullName() != "acme/widget" || got.Repo.BaseBranch() != "main" {
t.Fatalf("Repo did not round-trip: %+v", got.Repo)
}
if got.Cfg.DiffCapLines != 500 {
t.Fatalf("Cfg did not round-trip: %+v", got.Cfg)
}
if got.Providers["anthropic"].APIKeyEnv != "ANTHROPIC_API_KEY" {
t.Fatalf("Providers did not round-trip: %+v", got.Providers)
}
if got.WorktreeGrant != "grant" {
t.Fatalf("WorktreeGrant did not round-trip: %q", got.WorktreeGrant)
}
}
func TestRequestValidateRequiresPositiveTaskID(t *testing.T) {
for _, test := range []struct {
name string
request Request
wantErr bool
}{
{name: "missing task", wantErr: true},
{name: "zero ID", request: Request{Task: &workflow.Task{}}, wantErr: true},
{name: "negative ID", request: Request{Task: &workflow.Task{ID: -1}}, wantErr: true},
{name: "missing worktree grant", request: Request{Task: &workflow.Task{ID: 1}}, wantErr: true},
{name: "valid request", request: Request{Task: &workflow.Task{ID: 1}, WorktreeGrant: "grant"}},
} {
t.Run(test.name, func(t *testing.T) {
err := test.request.Validate()
if (err != nil) != test.wantErr {
t.Fatalf("Validate() error = %v, wantErr %v", err, test.wantErr)
}
})
}
}
func TestResponseJSONRoundTrip(t *testing.T) {
resp := Response{
Task: &workflow.Task{ID: 1, Status: workflow.StatusPROpen},
Status: "passed",
}
data, err := json.Marshal(resp)
if err != nil {
t.Fatalf("marshal: %v", err)
}
var got Response
if err := json.Unmarshal(data, &got); err != nil {
t.Fatalf("unmarshal: %v", err)
}
if got.Task == nil || got.Task.Status != workflow.StatusPROpen || got.Status != "passed" {
t.Fatalf("Response did not round-trip: %+v", got)
}
}
// TestRequestRoutingBindingsWireKeys pins the wire contract for the routing
// bindings. The daemon and the archie-agent worker are separate binaries, so the
// JSON keys on Request are the actual interface between them: a renamed tag
// would compile, pass every round-trip test that unmarshals into the same Go
// type, and silently fall back to built-in routing against a version-skewed
// peer. Assert the emitted keys literally.
func TestRequestRoutingBindingsWireKeys(t *testing.T) {
data, err := json.Marshal(Request{
Task: &workflow.Task{ID: 1, Owner: "acme", Repo: "widget"},
WorktreeGrant: "grant",
KindWorkflows: workflow.KindWorkflows{workintake.KindBug: "custom-bug"},
LabelWorkflows: workflow.LabelWorkflows{"security": "security-review"},
})
if err != nil {
t.Fatal(err)
}
var doc map[string]json.RawMessage
if err := json.Unmarshal(data, &doc); err != nil {
t.Fatal(err)
}
for _, key := range []string{"kind_workflows", "label_workflows"} {
raw, ok := doc[key]
if !ok {
got := make([]string, 0, len(doc))
for k := range doc {
got = append(got, k)
}
t.Fatalf("marshalled Request has no %q key; the cross-process routing contract changed. Present keys: %v", key, got)
}
if string(raw) == "{}" || string(raw) == "null" {
t.Fatalf("%q = %s, want the bindings object", key, raw)
}
}
}