Files
archie/internal/daemon/intake_disabled_test.go
T
sam 804d306f75
Quality / check (pull_request) Canceled after 0s
ci/woodpecker/pr/quality Pipeline was successful
fix(daemon): skip issue intake when the routed workflow is disabled (#1913)
pollNATS queued every discovered issue and let the pin path park it with
'workflow triage is disabled'. Now, after the TaskByIssue existence check
and before PublishTask, the label-routed workflow is resolved against the
active definitions and checked against the identity org's enablement, the
same filter dispatchOrgBindings applies to armed bindings. A disabled
route logs one Info line and returns false without acknowledging, so the
issue is rediscovered on a later poll and picked up if the workflow is
re-enabled. Read failures of org, definitions or enablement log a Warn
and fall through to publishing; no routable definition keeps the current
publish behaviour. processNATSTask's park path is unchanged.
2026-11-13 00:42:40 +11:00

114 lines
5.1 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
}
// 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
d := &Daemon{
Log: slog.New(slog.NewTextHandler(&logs, nil)), Store: taskLookup{}, Tasks: bus,
WorkflowDefinitions: tt.definitions, WorkflowEnablement: tt.enablement,
Principals: tt.principals,
}
is := forge.Issue{Number: 42, Title: "crash on startup"}
if created := d.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())
}
})
}
}