Files
archie/internal/daemon/intake_disabled_test.go
sam 41f44c43a1 feat(workflow): issue routing is a control-plane setting edited in the dashboard (#1959)
Label to workflow rules and a default workflow are the intake-routes
resource: ordered, case-insensitive, first rule wins, seeded with bug to
tdd, feature to feasibility, default triage. A write naming an undefined
workflow or an empty label is refused. The daemon reads it through the
workflow definitions client when pinning a task and at intake.

The kind indirection only carried this routing, so it is removed: the
per-kind NATS subjects (every consumer already bound archie.task.>) are
one subject, the envelope and playbook triggers lose their kind, and
x-intake-routes is dropped. The dashboard gets an Issue routing page
under Automation; the trigger card reads the same setting.
2026-11-13 20:28:52 +11:00

120 lines
5.4 KiB
Go

package daemon
import (
"bytes"
"context"
"errors"
"log/slog"
"strings"
"testing"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/domain/access"
"github.com/samcharles93/archie-core/internal/domain/org"
"github.com/samcharles93/archie-core/internal/domain/workflow"
workflowtask "github.com/samcharles93/archie-core/internal/domain/workflow/task"
"github.com/samcharles93/archie-core/internal/forge"
)
// reactCounter is a forge that counts pickup reactions, so a test can tell
// whether the daemon acknowledged an issue.
type reactCounter struct {
forge.Forge
reactions int
}
func (f *reactCounter) React(context.Context, string, string, int, string) error {
f.reactions++
return nil
}
// staticDefinitions serves one definition collection, or an error.
type staticDefinitions struct {
collection workflow.WorkflowDefinitionCollection
err error
}
func (s staticDefinitions) WorkflowDefinitions(context.Context) (workflow.WorkflowDefinitionCollection, int64, error) {
return s.collection, 1, s.err
}
func (staticDefinitions) IntakeRoutes(context.Context) (workflow.IntakeRoutes, error) {
return workflow.ShippedIntakeRoutes(), nil
}
// enablementSource serves one enablement value, or an error.
type enablementSource struct {
value workflowtask.WorkflowEnablement
err error
}
func (e enablementSource) WorkflowEnablement(context.Context) (workflowtask.WorkflowEnablement, error) {
return e.value, e.err
}
// An issue whose label-routed workflow is disabled for the polling identity's
// org is skipped at intake: no task is published and the issue is not
// acknowledged, so a later poll rediscovers it if the workflow is re-enabled.
// A failed read of org, definitions or enablement falls through to publishing
// rather than dropping the issue.
func TestPollNATSSkipsDisabledRoutedWorkflow(t *testing.T) {
repo := config.Repo{Owner: "acme", Name: "widget"}
cfg := config.Config{Dispatch: config.Dispatch{AckReaction: "eyes"}}
shipped := staticDefinitions{collection: workflow.ShippedDefinitions()}
principals := workflowPrincipals{"acme-bot": "acme"}
disabled := func(owner org.OrgID, workflowID string) enablementSource {
return enablementSource{value: workflowtask.WorkflowEnablement{}.SetEnabled(owner, workflowID, false)}
}
tests := []struct {
name string
labels string
definitions staticDefinitions
enablement enablementSource
principals access.PrincipalSource
wantCreated bool
wantPublished int
wantAck bool
wantSkipLog bool
}{
{"bug routes to tdd, disabled for the org, skipped", "bug", shipped, disabled("acme", "tdd"), principals, false, 0, false, true},
{"feature routes to feasibility, disabled for the org, skipped", "feature", shipped, disabled("acme", "feasibility"), principals, false, 0, false, true},
{"no recognised label routes to triage, disabled, skipped", "", shipped, disabled("acme", "triage"), principals, false, 0, false, true},
{"unrecognised labels route to triage, disabled, skipped", "archie,priority::high", shipped, disabled("acme", "triage"), principals, false, 0, false, true},
{"same workflow enabled publishes and acknowledges", "bug", shipped, enablementSource{}, principals, true, 1, true, false},
{"disabled for a different org still publishes", "bug", shipped, disabled("other", "tdd"), principals, true, 1, true, false},
{"enablement read error falls through to publish", "bug", shipped, enablementSource{err: errors.New("store down")}, principals, true, 1, true, false},
{"definitions read error falls through to publish", "bug", staticDefinitions{err: errors.New("store down")}, enablementSource{}, principals, true, 1, true, false},
// The org read fails again inside PublishTask, so nothing is queued,
// but the issue is not skipped or acknowledged and is re-polled.
{"org lookup error falls through, not skipped", "bug", shipped, enablementSource{}, workflowPrincipals{}, false, 0, false, false},
{"no routable definition publishes; the pin path parks", "", staticDefinitions{collection: workflow.WorkflowDefinitionCollection{Definitions: []workflow.WorkflowDefinitionEntry{{ID: "pr-review"}}}}, enablementSource{}, principals, true, 1, true, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
bus := &publishCounter{}
fg := &reactCounter{}
var logs bytes.Buffer
logger := slog.New(slog.NewTextHandler(&logs, nil))
d := wired(&Daemon{
Log: logger, Store: taskLookup{}, TaskTransport: bus,
WorkflowDefinitions: tt.definitions,
Identity: NewIdentityGate(neutralIdentities{}, neutralAssignments{}, allowAll{}, tt.principals, neutralDenials{}, nil, nil, logger),
})
d.BindingDispatch.enablement = tt.enablement
is := forge.Issue{Number: 42, Title: "crash on startup"}
if created := d.IssuePoller.pollNATS(t.Context(), fg, cfg, repo, is, tt.labels, "acme-bot"); created != tt.wantCreated {
t.Errorf("pollNATS created %v, want %v", created, tt.wantCreated)
}
if bus.published != tt.wantPublished {
t.Errorf("published %d tasks, want %d", bus.published, tt.wantPublished)
}
if acked := fg.reactions > 0; acked != tt.wantAck {
t.Errorf("acknowledged %v, want %v", acked, tt.wantAck)
}
if skipped := strings.Contains(logs.String(), "issue skipped"); skipped != tt.wantSkipLog {
t.Errorf("skip logged %v, want %v; logs: %s", skipped, tt.wantSkipLog, logs.String())
}
})
}
}