`bd status` (and its `bd stats` alias) had two implementations of the same command. The direct route asked the store for statistics and, under --assigned, built an assignee filter, ran a ready-work query and folded five status counts itself; status_proxied_server.go did all four again through a unit of work. Both now build an issueops request and ask for the role, and the difference between the two routes is which accessor answers. THE ROLE. Its numbers are DEPENDENCY-AWARE — the blocked count reads the denormalized transitive is_blocked flag and readiness is derived from it — so no predicate over the issues table describes them and this is its own role rather than a sixth Counter dimension, which is what issueops/ counter.go already said it would be. Stats and AssigneeStats are born together because they answer the same question of two sets; they are two METHODS rather than one with an Assignee field because the assignee answer fills four fields of the same struct by different definitions, and a request whose optional string silently changed what a number meant is the shape this programme exists to remove. WHAT THE LEAF DOC NOW SAYS OUT LOUD, none of it written down anywhere before: BlockedIssues counts the graph, not the "blocked" status; ReadyIssues is arithmetic and NOT the cardinality of `bd ready`; PinnedIssues overlaps every status bucket so the buckets do not sum to Total; the workspace-wide answer excludes the wisp tier while the assignee-scoped one merges it; --no-blocked is a HINT whose only report is BlockedIssues being nil; and EpicsEligibleForClosure and AverageLeadTime are structurally always zero, which is why `bd status`'s "Extended:" branch has never once printed. CONFORMANCE. backend/conformance/stats_reporter_contract.go holds thirteen cases, each citing the leaf doc by line, run at all three implementations with ZERO parks and zero divergences. Unusually for this suite they are THREE genuine votes: the two stores share the role body (storestats) but dolt/queries.go, embeddeddolt/statistics.go and domain/db/issue.go each spell the blocked count and the ready subtraction themselves. Every workspace-wide case asserts a before/after DELTA — a summary takes no predicate, so there is no IDFilter to scope it with the way the Counter contract scopes its cases. HTTP. GET /v0/beads/stats is added to the Tier-1 document FIRST and the types generated from it: a `Statistics` schema x-go-type-pinned to types.Statistics (checked: the pin survives, and the schema is now in the JSON-tag bijection test), a StatsResponse envelope mirroring the CLI's --json shape minus `recent_activity`, which no shipped path populates. The handler calls the same role both CLI routes call. `blocked_count_skipped` is DERIVED from the answer, never echoed from the request, because the hint is a hint. httpapi.Config's roles source grows a third field and requires it with the other two: a source that answers some operations and faults on others is not a source. ENFORCEMENT. cmd/bd/status.go and status_proxied_server.go no longer name types.IssueFilter or types.WorkFilter, so `status` LEAVES the forbidigo exception list (63 -> 61 of 388 files). The role-constructor depguard list gains storestats. TWO USER-VISIBLE CHANGES, both improvements and both small: `bd status --assigned` reports the underlying failure instead of the bare "failed to get assigned statistics", identically on both routes; and the proxied --no-blocked warning now fires from the ANSWER rather than from the route, so a backend that gains a fast path stops printing it with no edit. Verified: build, vet, gofmt clean; golangci-lint 0 issues; make api-check clean (no codegen drift, whole httpapi package green); TestStatsReporterContract 13/13 at dolt, embeddeddolt and uow; TestEmbeddedStatus and TestProxiedServerStatus green with their gates set; TestProxiedLookup green; TestCorpusGolden green. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
138 lines
4.9 KiB
Go
138 lines
4.9 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/steveyegge/beads/internal/types"
|
|
)
|
|
|
|
// The assignee fold this command used to own moved to
|
|
// internal/workapi.FoldStatsAssigneeSummary when `bd status` went behind
|
|
// issueops.StatsReporter, and its test moved with it
|
|
// (internal/workapi/stats_test.go). It is the definition of what
|
|
// `bd status --assigned` means and both routes now read it from there, so a
|
|
// copy of the test here would pin a function this package no longer has.
|
|
|
|
func TestRenderStatusJSON(t *testing.T) {
|
|
t.Setenv("BD_JSON_ENVELOPE", "0")
|
|
|
|
count := func(n int) *int { return &n }
|
|
tests := []struct {
|
|
name string
|
|
stats *types.Statistics
|
|
skipped bool
|
|
blocked *int
|
|
ready *int
|
|
}{
|
|
{
|
|
name: "includes computed counts",
|
|
stats: &types.Statistics{TotalIssues: 3, OpenIssues: 1, BlockedIssues: count(1), ReadyIssues: count(1)},
|
|
blocked: count(1), ready: count(1),
|
|
},
|
|
{
|
|
name: "preserves skipped counts as null",
|
|
stats: &types.Statistics{TotalIssues: 3, OpenIssues: 2, ClosedIssues: 1},
|
|
skipped: true,
|
|
},
|
|
}
|
|
|
|
oldJSON := jsonOutput
|
|
jsonOutput = true
|
|
defer func() { jsonOutput = oldJSON }()
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
out := captureStdout(t, func() error { return renderStatus(tt.stats, nil) })
|
|
var got StatusOutput
|
|
if err := json.Unmarshal([]byte(out), &got); err != nil {
|
|
t.Fatalf("unmarshal status JSON: %v\n%s", err, out)
|
|
}
|
|
if got.Summary == nil || got.Summary.TotalIssues != tt.stats.TotalIssues {
|
|
t.Errorf("summary = %+v, want %+v", got.Summary, tt.stats)
|
|
}
|
|
if got.BlockedCountSkipped != tt.skipped {
|
|
t.Errorf("blocked_count_skipped = %t, want %t", got.BlockedCountSkipped, tt.skipped)
|
|
}
|
|
if (got.Summary.BlockedIssues == nil) != (tt.blocked == nil) || (got.Summary.BlockedIssues != nil && *got.Summary.BlockedIssues != *tt.blocked) {
|
|
t.Errorf("blocked_issues = %v, want %v", got.Summary.BlockedIssues, tt.blocked)
|
|
}
|
|
if (got.Summary.ReadyIssues == nil) != (tt.ready == nil) || (got.Summary.ReadyIssues != nil && *got.Summary.ReadyIssues != *tt.ready) {
|
|
t.Errorf("ready_issues = %v, want %v", got.Summary.ReadyIssues, tt.ready)
|
|
}
|
|
if got.RecentActivity != nil {
|
|
t.Errorf("recent activity = %+v, want nil", got.RecentActivity)
|
|
}
|
|
if tt.skipped && (!strings.Contains(out, `"blocked_issues": null`) || !strings.Contains(out, `"ready_issues": null`)) {
|
|
t.Errorf("skipped counts must be encoded as null:\n%s", out)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestRenderStatusHuman(t *testing.T) {
|
|
count := func(n int) *int { return &n }
|
|
tests := []struct {
|
|
name string
|
|
stats *types.Statistics
|
|
activity *RecentActivitySummary
|
|
contains []string
|
|
absent []string
|
|
}{
|
|
{
|
|
name: "normal counts",
|
|
stats: &types.Statistics{TotalIssues: 3, OpenIssues: 1, InProgressIssues: 1, ClosedIssues: 1, BlockedIssues: count(1), ReadyIssues: count(2)},
|
|
contains: []string{"Issue Database Status", "Total Issues: 3", "Blocked: 1", "Ready to Work: 2"},
|
|
absent: []string{"(skipped)"},
|
|
},
|
|
{
|
|
name: "skipped counts",
|
|
stats: &types.Statistics{TotalIssues: 1},
|
|
contains: []string{"Blocked: (skipped)", "Ready to Work: (skipped)"},
|
|
},
|
|
{
|
|
name: "extended statistics",
|
|
stats: &types.Statistics{BlockedIssues: count(0), ReadyIssues: count(0), PinnedIssues: 2, EpicsEligibleForClosure: 3, AverageLeadTime: 4.5},
|
|
contains: []string{"Extended:", "Pinned: 2", "Epics Ready to Close: 3", "Avg Lead Time: 4.5 hours"},
|
|
},
|
|
{
|
|
name: "recent activity",
|
|
stats: &types.Statistics{BlockedIssues: count(0), ReadyIssues: count(0)},
|
|
activity: &RecentActivitySummary{HoursTracked: 24, CommitCount: 1, TotalChanges: 2, IssuesCreated: 3, IssuesClosed: 4, IssuesReopened: 5, IssuesUpdated: 6},
|
|
contains: []string{"Recent Activity (last 24 hours):", "Commits: 1", "Total Changes: 2", "Issues Created: 3", "Issues Closed: 4", "Issues Reopened: 5", "Issues Updated: 6"},
|
|
},
|
|
}
|
|
|
|
oldJSON := jsonOutput
|
|
jsonOutput = false
|
|
defer func() { jsonOutput = oldJSON }()
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
out := captureStdout(t, func() error { return renderStatus(tt.stats, tt.activity) })
|
|
for _, want := range tt.contains {
|
|
if !strings.Contains(out, want) {
|
|
t.Errorf("output does not contain %q:\n%s", want, out)
|
|
}
|
|
}
|
|
for _, unwanted := range tt.absent {
|
|
if strings.Contains(out, unwanted) {
|
|
t.Errorf("output unexpectedly contains %q:\n%s", unwanted, out)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestGetGitActivityReturnsNil(t *testing.T) {
|
|
if got := getGitActivity(24); got != nil {
|
|
t.Errorf("getGitActivity(24) = %+v, want nil", got)
|
|
}
|
|
}
|
|
|
|
func TestStatusCommandAliases(t *testing.T) {
|
|
if len(statusCmd.Aliases) != 1 || statusCmd.Aliases[0] != "stats" {
|
|
t.Errorf("status aliases = %q, want [stats]", statusCmd.Aliases)
|
|
}
|
|
}
|