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.
This commit is contained in:
7 files changed
+217
-61
No files matched your search
+58
-49
@@ -3,6 +3,7 @@ package configtemplate
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
@@ -36,45 +37,59 @@ func TestExternalNATSProfileLoadsWithManagedWorkers(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// selfHostConfig reconstructs the config.toml the install.sh self-host branch
|
||||
// writes through its hand-rolled heredoc, with the shell expansions resolved,
|
||||
// so tests can parse exactly what a fresh self-host install would produce.
|
||||
func selfHostConfig(t *testing.T) string {
|
||||
// heredocRe matches any `cat <<DELIM` heredoc opener in install.sh, tolerating
|
||||
// <<- and a quoted delimiter. The redirect target is deliberately NOT matched:
|
||||
// the config writer may name it inline, put the redirect before the delimiter,
|
||||
// or factor the path into a variable, and none of those change what is written.
|
||||
var heredocRe = regexp.MustCompile("(?m)^[^\\n]*\\bcat\\b[^\\n]*?<<-?[ \\t]*['\"]?([A-Za-z_][A-Za-z0-9_]*)['\"]?[ \\t]*")
|
||||
|
||||
// configHeredocs returns the body of every install.sh heredoc that declares the
|
||||
// daemon config's [nats] table, which the systemd unit heredoc does not. The
|
||||
// selector is a table the assertions below never touch, so it stays structural
|
||||
// rather than becoming a search for the field under test.
|
||||
//
|
||||
// Command substitutions are dropped rather than expanded. Substituting a
|
||||
// hand-written forge block here would duplicate install.sh's own forge_block
|
||||
// output, so the test would assert against a copy that can silently diverge from
|
||||
// what the installer writes; the loader defaults what their absence omits. Plain
|
||||
// ${VAR} references are left in place: they are ordinary string contents once
|
||||
// parsed, and removing them would empty the [models] table for no benefit.
|
||||
func configHeredocs(t *testing.T) []string {
|
||||
t.Helper()
|
||||
source := readDeploymentFile(t, "install.sh")
|
||||
const marker = `cat <<EOF > "${ARCHIE_CONFIG_DIR}/config.toml"`
|
||||
start := strings.Index(source, marker)
|
||||
if start < 0 {
|
||||
t.Fatalf("install.sh is missing the self-host config heredoc marker %q", marker)
|
||||
}
|
||||
bodyStart := strings.IndexByte(source[start:], '\n')
|
||||
if bodyStart < 0 {
|
||||
t.Fatal("install.sh self-host config heredoc is malformed")
|
||||
}
|
||||
bodyStart += start + 1
|
||||
lines := strings.Split(source[bodyStart:], "\n")
|
||||
var body strings.Builder
|
||||
for _, line := range lines {
|
||||
if line == "EOF" {
|
||||
break
|
||||
var out []string
|
||||
for _, m := range heredocRe.FindAllStringSubmatchIndex(source, -1) {
|
||||
delim := source[m[2]:m[3]]
|
||||
rest := source[m[1]:]
|
||||
_, after, ok := strings.Cut(rest, "\n")
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
var body strings.Builder
|
||||
for line := range strings.SplitSeq(after, "\n") {
|
||||
if strings.TrimRight(strings.TrimLeft(line, "\t"), "\r") == delim {
|
||||
break
|
||||
}
|
||||
if strings.Contains(line, "$(") {
|
||||
continue // a command substitution install.sh resolves at run time
|
||||
}
|
||||
body.WriteString(line)
|
||||
body.WriteByte('\n')
|
||||
}
|
||||
if b := body.String(); strings.Contains(b, "\n[nats]") {
|
||||
out = append(out, b)
|
||||
}
|
||||
body.WriteString(line)
|
||||
body.WriteByte('\n')
|
||||
}
|
||||
out := body.String()
|
||||
// install.sh expands these two shell constructs inside the heredoc; pin
|
||||
// concrete values here so the reconstructed document parses. The forge
|
||||
// block is the GitHub output forge_block prints (install.sh's own function).
|
||||
out = strings.Replace(out, "$(forge_block)",
|
||||
"[forge]\ntype = \"github\"\nhost = \"https://github.com\"\ntoken = { engine = \"env\", key = \"ARCHIE_GITHUB_TOKEN\" }\n\n", 1)
|
||||
out = strings.ReplaceAll(out, "ollama/${OLLAMA_MODEL}", "ollama/llama3")
|
||||
if len(out) == 0 {
|
||||
t.Fatal("install.sh has no heredoc declaring the daemon config's [nats] table")
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func TestInstallerGeneratedConfigHasServiceTargets(t *testing.T) {
|
||||
// Non-self-host branch: install.sh replaces only the [forge] block in
|
||||
// config.example.toml, so every other section -- including the required
|
||||
// [services.*] targets -- must already be active there.
|
||||
// [services.state] target -- must already be active there.
|
||||
example, err := configuration.New(nil).File("config.example.toml")
|
||||
if err != nil {
|
||||
t.Fatalf("load config.example.toml: %v", err)
|
||||
@@ -82,28 +97,22 @@ func TestInstallerGeneratedConfigHasServiceTargets(t *testing.T) {
|
||||
if example.Config.Services.State.Target == "" {
|
||||
t.Error("config.example.toml leaves services.state.target empty; the install.sh awk branch would emit an unbootable config")
|
||||
}
|
||||
exampleSource := readDeploymentFile(t, "config.example.toml")
|
||||
if !strings.Contains(exampleSource, "\n[services.gateway]\n") {
|
||||
t.Error("config.example.toml does not declare an active [services.gateway] section")
|
||||
}
|
||||
|
||||
// Self-host branch: install.sh writes a hand-rolled heredoc. Reconstruct
|
||||
// it the way the installer would and require it to parse with the State
|
||||
// Self-host branch: install.sh writes the config through its own heredoc.
|
||||
// Reconstruct every such heredoc and require each to parse with the State
|
||||
// Store target the daemon's openStateStoreAdapter enforces.
|
||||
selfHost := selfHostConfig(t)
|
||||
path := filepath.Join(t.TempDir(), "config.toml")
|
||||
if err := os.WriteFile(path, []byte(selfHost), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
generated, err := configuration.New(nil).File(path)
|
||||
if err != nil {
|
||||
t.Fatalf("load reconstructed self-host install config: %v", err)
|
||||
}
|
||||
if generated.Config.Services.State.Target == "" {
|
||||
t.Error("install.sh self-host heredoc leaves services.state.target empty; generated config cannot boot")
|
||||
}
|
||||
if !strings.Contains(selfHost, "\n[services.gateway]\n") {
|
||||
t.Error("install.sh self-host heredoc does not declare an active [services.gateway] section")
|
||||
for _, body := range configHeredocs(t) {
|
||||
path := filepath.Join(t.TempDir(), "config.toml")
|
||||
if err := os.WriteFile(path, []byte(body), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
generated, err := configuration.New(nil).File(path)
|
||||
if err != nil {
|
||||
t.Fatalf("load reconstructed self-host install config: %v", err)
|
||||
}
|
||||
if generated.Config.Services.State.Target == "" {
|
||||
t.Error("install.sh config heredoc leaves services.state.target empty; generated config cannot boot")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -42,11 +42,14 @@ func TestSetupBackendsRegistersContainerCleanupAfterNATS(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// methodBody returns the body of the func or method named name. It matches on
|
||||
// name alone, so a package-level function (such as Run in main.go) resolves as
|
||||
// well as a method; names are unique per package.
|
||||
func methodBody(t *testing.T, file *ast.File, name string) *ast.BlockStmt {
|
||||
t.Helper()
|
||||
for _, decl := range file.Decls {
|
||||
fn, ok := decl.(*ast.FuncDecl)
|
||||
if ok && fn.Recv != nil && fn.Name.Name == name {
|
||||
if ok && fn.Name.Name == name {
|
||||
return fn.Body
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
package archied
|
||||
|
||||
import (
|
||||
"go/parser"
|
||||
"go/token"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestRunBuildsWorktreeManagerBeforeGateways pins the composition order that
|
||||
// review_pr depends on.
|
||||
//
|
||||
// boot.prReviewer captures b.trees, and it refuses to produce a reviewer when
|
||||
// b.trees is nil so that a process which can never run a review does not
|
||||
// advertise the tool. In the daemon that makes the order load-bearing: if the
|
||||
// gateway wiring runs first it asks for a reviewer before the worktree manager
|
||||
// exists, gets nil, and the daemon silently offers no review_pr at all. Nothing
|
||||
// at runtime observes the order, so it is asserted by source position, the same
|
||||
// way TestSetupBackendsRegistersContainerCleanupAfterNATS in
|
||||
// bootstrap_nats_test.go pins cleanup ordering for the same reason.
|
||||
func TestRunBuildsWorktreeManagerBeforeGateways(t *testing.T) {
|
||||
fileset := token.NewFileSet()
|
||||
file, err := parser.ParseFile(fileset, "main.go", nil, 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
run := methodBody(t, file, "Run")
|
||||
trees := methodCallPosition(run, "buildTreesAndIdentities")
|
||||
gateways := methodCallPosition(run, "setupGateways")
|
||||
if trees == token.NoPos || gateways == token.NoPos {
|
||||
t.Fatalf("Run call positions: buildTreesAndIdentities=%v setupGateways=%v, want both present", trees, gateways)
|
||||
}
|
||||
if gateways < trees {
|
||||
t.Fatal("Run wires the gateways before buildTreesAndIdentities; prReviewer captures b.trees, so review_pr would be dropped instead of advertised")
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
||||
"github.com/samcharles93/archie-core/internal/forge"
|
||||
"github.com/samcharles93/archie-core/internal/gateway"
|
||||
"github.com/samcharles93/archie-core/internal/tools"
|
||||
"github.com/samcharles93/archie-core/internal/worktree"
|
||||
)
|
||||
|
||||
@@ -218,12 +219,25 @@ func TestReviewPRToolRegisteredOnlyWithWorkingReviewer(t *testing.T) {
|
||||
log: logger,
|
||||
}
|
||||
|
||||
if entries := gateway.ReviewTools(b.prReviewer(), "archie"); len(entries) != 0 {
|
||||
t.Fatalf("ReviewTools with no worktree manager = %d entries, want 0", len(entries))
|
||||
if hasReviewPRTool(gateway.ReviewTools(b.prReviewer(), "archie")) {
|
||||
t.Fatal("review_pr advertised with no worktree manager: a tool that cannot run must not be offered")
|
||||
}
|
||||
|
||||
b.trees = &worktree.Manager{}
|
||||
if entries := gateway.ReviewTools(b.prReviewer(), "archie"); len(entries) != 1 {
|
||||
t.Fatalf("ReviewTools with a worktree manager = %d entries, want 1", len(entries))
|
||||
if !hasReviewPRTool(gateway.ReviewTools(b.prReviewer(), "archie")) {
|
||||
t.Fatal("review_pr not advertised with a worktree manager")
|
||||
}
|
||||
}
|
||||
|
||||
// hasReviewPRTool reports whether review_pr is offered. Presence is asserted by
|
||||
// name rather than by entry count: the contract is that this one capability is
|
||||
// offered only when it can run, so adding a sibling review tool must not turn
|
||||
// this red.
|
||||
func hasReviewPRTool(entries []tools.ToolEntry) bool {
|
||||
for _, e := range entries {
|
||||
if e.Name == "review_pr" {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package archied
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/config"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workintake"
|
||||
"github.com/samcharles93/archie-core/internal/store"
|
||||
"github.com/samcharles93/archie-core/internal/webui"
|
||||
)
|
||||
|
||||
// TestBuildDaemonCarriesWorkflowRoutingBindings pins the composition hop in the
|
||||
// cross-process routing fix: the resolved bindings loaded by loadWorkflowRouting
|
||||
// must survive buildDaemon onto the daemon that puts them into taskrun.Request.
|
||||
//
|
||||
// Without this, a nil here reverts the entire fix while every other test stays
|
||||
// green: the daemon test sets d.KindWorkflows itself, and the worker test drives
|
||||
// routeTask directly, so neither observes how the daemon obtained them. That is
|
||||
// the "tested in isolation, never verified wired" shape this repo has been bitten
|
||||
// by before.
|
||||
func TestBuildDaemonCarriesWorkflowRoutingBindings(t *testing.T) {
|
||||
storeA, err := store.Open(t.Context(), filepath.Join(t.TempDir(), "store-a.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _ = storeA.Close() })
|
||||
storeB := openSecondStore(t)
|
||||
|
||||
b := &boot{
|
||||
cfg: config.Config{},
|
||||
log: slog.New(slog.DiscardHandler),
|
||||
st: storeA,
|
||||
stateStore: storeB,
|
||||
web: &webui.Server{},
|
||||
kindWorkflows: workflow.KindWorkflows{workintake.KindBug: "custom-bug"},
|
||||
labelWorkflows: workflow.LabelWorkflows{"security": "security-review"},
|
||||
}
|
||||
b.buildDaemon()
|
||||
|
||||
if got := b.d.KindWorkflows[workintake.KindBug]; got != "custom-bug" {
|
||||
t.Fatalf("Daemon.KindWorkflows[bug] = %q, want %q; buildDaemon dropped the kind routing bindings, so the worker would route with built-in defaults", got, "custom-bug")
|
||||
}
|
||||
if got := b.d.LabelWorkflows["security"]; got != "security-review" {
|
||||
t.Fatalf("Daemon.LabelWorkflows[security] = %q, want %q; buildDaemon dropped the label routing bindings", got, "security-review")
|
||||
}
|
||||
}
|
||||
@@ -753,11 +753,18 @@ func TestRunViaAgentSendsRoutingBindings(t *testing.T) {
|
||||
t.Fatalf("claim: (%v, %v)", task, err)
|
||||
}
|
||||
|
||||
received := make(chan taskrun.Request, 1)
|
||||
// The published request is checked for its routing bindings, so a decode
|
||||
// failure is carried out rather than discarded: otherwise a payload-shape
|
||||
// break would surface as an empty binding and read as a routing bug.
|
||||
type published struct {
|
||||
req taskrun.Request
|
||||
err error
|
||||
}
|
||||
received := make(chan published, 1)
|
||||
sub, err := mustCoreConn(t, busClient).Subscribe(agentnats.SubjectForTask(task.ID), func(msg *natsio.Msg) {
|
||||
var req taskrun.Request
|
||||
_ = json.Unmarshal(msg.Data, &req)
|
||||
received <- req
|
||||
var p published
|
||||
p.err = json.Unmarshal(msg.Data, &p.req)
|
||||
received <- p
|
||||
data, _ := json.Marshal(taskrun.Response{Status: workflow.StatusPROpen})
|
||||
_ = msg.Respond(data)
|
||||
})
|
||||
@@ -769,11 +776,14 @@ func TestRunViaAgentSendsRoutingBindings(t *testing.T) {
|
||||
d.runViaAgent(ctx, task, config.Repo{Owner: "acme", Name: "widget", Base: "main"})
|
||||
|
||||
select {
|
||||
case req := <-received:
|
||||
if got := req.KindWorkflows[workintake.KindBug]; got != "custom-bug" {
|
||||
case p := <-received:
|
||||
if p.err != nil {
|
||||
t.Fatalf("decode published taskrun request: %v", p.err)
|
||||
}
|
||||
if got := p.req.KindWorkflows[workintake.KindBug]; got != "custom-bug" {
|
||||
t.Errorf("request KindWorkflows[bug] = %q, want %q", got, "custom-bug")
|
||||
}
|
||||
if got := req.LabelWorkflows["security"]; got != "security-review" {
|
||||
if got := p.req.LabelWorkflows["security"]; got != "security-review" {
|
||||
t.Errorf("request LabelWorkflows[security] = %q, want %q", got, "security-review")
|
||||
}
|
||||
case <-time.After(time.Second):
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"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) {
|
||||
@@ -92,3 +93,38 @@ func TestResponseJSONRoundTrip(t *testing.T) {
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user