package daemon import ( "context" "errors" "log/slog" "testing" "github.com/samcharles93/archie-core/internal/config" "github.com/samcharles93/archie-core/internal/domain/storecontract" "github.com/samcharles93/archie-core/internal/domain/workflow" "github.com/samcharles93/archie-core/internal/forge" ) type taskLookup struct { storecontract.TaskStore have map[int]bool } func (s taskLookup) TaskByIssue(_ context.Context, _, _ string, number int) (*workflow.Task, error) { if s.have[number] { return &workflow.Task{IssueNumber: number}, nil } return nil, nil } // writerForge answers write access from a fixed set of logins; the rest of // the Forge surface is unused. type writerForge struct { forge.Forge writers map[string]bool err error } func (f writerForge) HasWriteAccess(_ context.Context, _, _, user string) (bool, error) { return f.writers[user], f.err } type publishCounter struct { stubTasks published int } func (p *publishCounter) PublishUnique(context.Context, string, string, []byte) error { p.published++ return nil } // One poll creates at most the cap of tasks; the rest wait for a later poll, // and an issue that already has a task neither counts nor is created twice. func TestPublishDiscoveredCap(t *testing.T) { tests := []struct { name string issues int existing []int cap int want int }{ {"cap below backlog", 10, nil, 3, 3}, {"backlog below cap", 2, nil, 3, 2}, {"existing tasks do not use the cap", 6, []int{1, 2}, 3, 3}, {"no cap", 10, nil, 0, 10}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { bus := &publishCounter{} have := map[int]bool{} for _, n := range tt.existing { have[n] = true } d := wired(&Daemon{Log: slog.New(slog.DiscardHandler), Store: taskLookup{have: have}, TaskTransport: bus}) var issues []forge.Issue for n := 1; n <= tt.issues; n++ { issues = append(issues, forge.Issue{Number: n, Labels: []string{"archie"}}) } room := tt.cap repo := config.Repo{Owner: "acme", Name: "widget"} d.IssuePoller.publishDiscovered(t.Context(), writerForge{writers: map[string]bool{"": true}}, config.Config{}, repo, issues, string(sys), &room) if bus.published != tt.want { t.Fatalf("published %d tasks, want %d", bus.published, tt.want) } }) } } // Forge text reaches an agent only from authors with write access: a labelled // or assigned issue opened by a stranger is not published, and an unanswerable // forge publishes nothing. Guards a public-repo stranger steering an agent // through an issue body. func TestPublishDiscoveredDropsNonWriters(t *testing.T) { issues := []forge.Issue{ {Number: 1, Author: "maint"}, {Number: 2, Author: "stranger"}, {Number: 3, Author: "maint"}, } tests := []struct { name string forge writerForge want int }{ {"stranger dropped", writerForge{writers: map[string]bool{"maint": true}}, 2}, {"forge error publishes nothing", writerForge{err: errors.New("down")}, 0}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { bus := &publishCounter{} d := wired(&Daemon{Log: slog.New(slog.DiscardHandler), Store: taskLookup{}, TaskTransport: bus}) room := 0 repo := config.Repo{Owner: "acme", Name: "widget"} d.IssuePoller.publishDiscovered(t.Context(), tt.forge, config.Config{}, repo, issues, string(sys), &room) if bus.published != tt.want { t.Fatalf("published %d tasks, want %d", bus.published, tt.want) } }) } }