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) HasWriteAccess(context.Context, string, string, string) (bool, error) { return true, 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 := wired(&Daemon{ Log: slog.New(slog.DiscardHandler), Store: taskLookup{}, TaskTransport: bus, Cfg: config.NewHolder(config.Config{}), }) d.IssuePoller.sources = 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.IssuePoller.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) } }) } }