Files
archie/internal/infrastructure/postgres/workflow_stats_test.go
T
sam 038fd2c7ec feat(taskstate): count completed runs in the per-workflow stats
The transition table moved the no-change outcomes to StatusCompleted, so a
run that opens no pull request now ends in a status the per-workflow stats
did not count at all: it was not merged, not in review, not parked, and the
workflows page would have shown it as a run that went nowhere. The count
travels the whole State Store path, since every dashboard consumer reads it
through the gRPC client rather than the local store.

- WorkflowStats filters `completed` beside merged and pr_open; the SQL is
  the count, so a wrong filter reads as zero rather than failing, and
  TestWorkflowStatsCountsCompletedRuns holds it against a real store.
- storecontract.WorkflowStat gains Completed (json "completed"), the proto
  message gains field 9, and workflowStatProto/workflowStatValue carry it;
  TestWorkflowStatProtoCarriesEveryField round-trips every field by name.
- The gateway's chat task report names the status.
- The dashboard counts a completed run as delivered in GatePulseCard's
  totals and badge numerator, and the WorkflowStat/WorkflowStats/WorkflowRow
  interfaces carry the field.
- TestOutcomeSitesAreTransitionsFromRunning is the guard that keeps the
  producer and the table agreeing: it re-reads this package's tc.Outcome
  assignment sites from source and requires each status to be one
  internal/taskstate allows from running, so a new or moved site has to be
  adjudicated against the table instead of discovered on a live run.
2026-09-26 04:01:08 +10:00

49 lines
1.7 KiB
Go

package postgres
import (
"testing"
"github.com/samcharles93/archie-core/internal/taskstate"
)
// TestWorkflowStatsCountsCompletedRuns pins the per-workflow count the
// transition table made reachable: a run that finishes with no pull request
// (a no-change build, a triage that needed no code) ends in taskstate.Completed,
// so the workflows page has to be able to see those runs at all. The count is a
// status filter in SQL, which fails silently when it is wrong -- a typo'd or
// missing filter reads as zero, not as an error -- so it is asserted against the
// store rather than left to the dashboard to notice.
func TestWorkflowStatsCountsCompletedRuns(t *testing.T) {
pool, q := migrated(t)
store := New(pool)
finished := insertChatTask(t, q)
merged := insertChatTask(t, q)
finish := func(id int64, to string) {
t.Helper()
if err := store.Transition(t.Context(), id, taskstate.Queued, taskstate.Running, ""); err != nil {
t.Fatalf("queued -> running: %v", err)
}
if err := store.Transition(t.Context(), id, taskstate.Running, to, ""); err != nil {
t.Fatalf("running -> %s: %v", to, err)
}
}
finish(finished.ID, taskstate.Completed)
finish(merged.ID, taskstate.Merged)
stats, err := store.WorkflowStats(t.Context())
if err != nil {
t.Fatalf("WorkflowStats: %v", err)
}
if len(stats) != 1 {
t.Fatalf("WorkflowStats = %+v, want one row for %q", stats, finished.Workflow)
}
got := stats[0]
if got.Workflow != merged.Workflow {
t.Fatalf("WorkflowStats row is %q, want %q", got.Workflow, merged.Workflow)
}
if got.Runs != 2 || got.Completed != 1 || got.Merged != 1 {
t.Errorf("WorkflowStats = %+v, want runs 2, completed 1, merged 1: the completed count has to be its own status, not a share of merged", got)
}
}