Files
archie/internal/domain/workintake/dispatch_test.go
T
sam 875b7a50d5 feat(forge): GitHub webhook receiver for immediate issue dispatch
Add [forge] intake = "poll" (default) | "webhook" | "both", and a forge
webhook receiver (internal/forge/webhook) that verifies a GitHub webhook's
HMAC signature, decodes the "issues" event, and publishes a matched issue
through the same path the poller uses -- so a labelled or assigned issue
starts a task the moment it happens instead of waiting up to poll_interval
(bead archie-core-7d5u.4).

The dispatch predicate (workintake.MatchesDispatch/RequiresLabel) is now a
single shared definition consumed by both the poller and the webhook
receiver, so the two cannot drift -- including the empty-label guard from
e19ff3a (an empty label must never match every open issue). Assignee
matching is case-insensitive to agree with AssignedIssues' server-side
GitHub filter; label matching stays case-sensitive, since GitHub labels
are case-distinct.

daemon.publishTask is exported as PublishTask, the single enqueue path
now shared by the poller and the webhook intake.

Two failure modes fixed during adversarial self-review before this landed:
a webhook secret that fails to resolve at runtime now degrades (logs and
skips the receiver) instead of aborting the whole daemon -- under
intake="both" the poll exists specifically as a backstop for this exact
misconfiguration, so crashing boot over it defeated its own purpose;
multi-identity deployments ([[identities]]) are explicitly unsupported for
now (webhook intake refuses to start, logs why, and leaves each identity's
own poll loop unaffected) rather than silently matching issues against the
wrong identity's dispatch rules.
2026-08-22 19:21:09 +10:00

143 lines
3.5 KiB
Go

package workintake
import (
"strings"
"testing"
)
func TestRequiresLabel(t *testing.T) {
tests := []struct {
trigger string
want bool
}{
{trigger: "label", want: true},
{trigger: "either", want: true},
{trigger: "assignee", want: false},
{trigger: "", want: false},
{trigger: "unknown", want: false},
}
for _, tc := range tests {
if got := RequiresLabel(tc.trigger); got != tc.want {
t.Errorf("RequiresLabel(%q) = %v, want %v", tc.trigger, got, tc.want)
}
}
}
func TestMatchesDispatch(t *testing.T) {
const (
label = "archie"
botUser = "archie-bot"
)
tests := []struct {
name string
trigger string
cfgLabel string
labels []string
assignees []string
want bool
}{
{
name: "label trigger matches the configured label",
trigger: "label",
cfgLabel: label,
labels: []string{"bug", label},
want: true,
},
{
name: "label trigger misses without the label",
trigger: "label",
cfgLabel: label,
labels: []string{"bug", "feature"},
want: false,
},
{
// 3209a1f: an empty configured label must match nothing, never
// every issue.
name: "label trigger with empty label never matches",
trigger: "label",
cfgLabel: "",
labels: []string{"bug"},
want: false,
},
{
name: "assignee trigger matches the bot",
trigger: "assignee",
assignees: []string{"someone", botUser},
want: true,
},
{
name: "assignee trigger misses without the bot",
trigger: "assignee",
assignees: []string{"someone-else"},
want: false,
},
{
name: "either matches on label alone",
trigger: "either",
cfgLabel: label,
labels: []string{label},
assignees: []string{"someone-else"},
want: true,
},
{
name: "either matches on assignee alone",
trigger: "either",
cfgLabel: label,
labels: []string{"bug"},
assignees: []string{botUser},
want: true,
},
{
name: "either misses on neither",
trigger: "either",
cfgLabel: label,
labels: []string{"bug"},
assignees: []string{"someone-else"},
want: false,
},
{
name: "either with empty label falls back to assignee only",
trigger: "either",
cfgLabel: "",
labels: []string{"anything"},
assignees: []string{botUser},
want: true,
},
{
name: "unknown trigger behaves as assignee",
trigger: "",
assignees: []string{botUser},
want: true,
},
{
// GitHub's AssignedIssues server-side filter matches assignee
// case-insensitively (usernames are unique regardless of case), so
// the webhook path's client-side comparison must agree, or a
// bot_user configured with different case than the login GitHub
// reports on the event silently never matches.
name: "assignee match is case-insensitive",
trigger: "assignee",
assignees: []string{"Archie-Bot"},
want: true,
},
{
// Labels, unlike usernames, are case-sensitive on GitHub: "Bug"
// and "bug" can coexist as distinct labels.
name: "label match is case-sensitive",
trigger: "label",
cfgLabel: label,
labels: []string{strings.ToUpper(label)},
want: false,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
got := MatchesDispatch(tc.trigger, tc.cfgLabel, botUser, tc.labels, tc.assignees)
if got != tc.want {
t.Errorf("MatchesDispatch(%q, %q, %q, %v, %v) = %v, want %v",
tc.trigger, tc.cfgLabel, botUser, tc.labels, tc.assignees, got, tc.want)
}
})
}
}