package daemon import ( "context" "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 } 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 := &Daemon{Log: slog.New(slog.DiscardHandler), Store: taskLookup{have: have}, Tasks: 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.publishDiscovered(t.Context(), nil, config.Config{}, repo, issues, "", &room) if bus.published != tt.want { t.Fatalf("published %d tasks, want %d", bus.published, tt.want) } }) } }