Files
archie/internal/daemon/poll_identity_test.go
T
sam e3ffefdc7d
deploy / plan (push) Successful in 25s
deploy / gate (push) Successful in 7m57s
deploy / agent-image (push) Successful in 21s
deploy / dist-zip (push) Skipped
deploy / archied-image (push) Successful in 2m12s
fix(identity): use System as the one root identity and record who created every task (#1851)
Drop the bot_user-derived root identity and the empty-identity sentinel. System
polls forge connections and owns instance-level work; a polled task stores the
polling identity, task_queued and work_request_submitted carry task id, actor
and principal, and an empty task identity is refused. Migration 0072 moves
empty tasks.identity, the identity-grants root key and the old root's
connection permits to System so a re-poll dedups.
2026-11-10 20:01:16 +11:00

89 lines
2.8 KiB
Go

package daemon
import (
"context"
"log/slog"
"testing"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/domain/identity"
"github.com/samcharles93/archie-core/internal/domain/workintake"
"github.com/samcharles93/archie-core/internal/forge"
)
type labelledIssues struct {
forge.Forge
issues []forge.Issue
}
func (f labelledIssues) IssuesWithLabel(context.Context, string, string, string) ([]forge.Issue, error) {
return f.issues, nil
}
func (labelledIssues) React(context.Context, string, string, int, string) error { return nil }
type publishedKeys struct {
stubTasks
payloads [][]byte
keys []string
}
func (p *publishedKeys) PublishUnique(_ context.Context, _, key string, payload []byte) error {
p.keys = append(p.keys, key)
p.payloads = append(p.payloads, payload)
return nil
}
// migratedRootIdentity is what migration 0072 writes over the empty identity
// the root's tasks used to carry. The poller must publish under the same
// value, or a re-poll after the migration no longer dedups against the rows.
const migratedRootIdentity = "00000000-0000-5000-8000-000000000001"
// TestPolledTaskKeepsItsCreatingIdentity pins that a polled task is published
// under the identity that polled it, never empty, and that System's key is the
// one a migrated row carries.
func TestPolledTaskKeepsItsCreatingIdentity(t *testing.T) {
tests := []struct {
name string
source identity.IdentityID
want string
}{
{"System poll matches the migrated root", identity.SystemID, migratedRootIdentity},
{"named identity poll keeps its identity", "bot-7", "bot-7"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
bus := &publishedKeys{}
d := &Daemon{
Log: slog.New(slog.DiscardHandler), Store: taskLookup{}, Tasks: bus,
Cfg: config.NewHolder(config.Config{}),
IssueSources: func(context.Context) []IssueSource {
return []IssueSource{{
Identity: tt.source,
Forge: labelledIssues{issues: []forge.Issue{{Number: 12, Title: "t"}}},
Dispatch: config.Dispatch{Trigger: "label"}, Label: "archie",
Repos: []config.Repo{{Owner: "acme", Name: "widget"}},
}}
},
}
if err := d.poll(t.Context()); err != nil {
t.Fatal(err)
}
if len(bus.payloads) != 1 {
t.Fatalf("published %d tasks, want 1", len(bus.payloads))
}
envelope, err := workintake.DecodeTask(bus.payloads[0])
if err != nil {
t.Fatal(err)
}
if envelope.Identity != tt.want {
t.Fatalf("published identity %q, want %q", envelope.Identity, tt.want)
}
want := workintake.TaskEnvelope{Org: "org-sys", Identity: tt.want, Owner: "acme", Repo: "widget", Number: 12}.IdempotencyKey()
if bus.keys[0] != want {
t.Fatalf("idempotency key %q, want %q", bus.keys[0], want)
}
})
}
}