beads/cmd/bd/formula_primitives_test.go
Jack J Garzella 7be354810f
feat(formula): make primitives discoverable to coding agents (GH#3621) (#3781)
* feat(formula): add bd formula schema reflection-driven primitive index (beads-9ib)

Generates internal/formula/schema_gen.go from types.go via go:generate,
exposing all 18 exported structs (Formula, Step, LoopSpec, OnCompleteSpec,
etc.) with field types, json/toml tags, required-flag, and doc comments.

Surfaced via:
  bd formula schema                 # list every primitive
  bd formula schema loop            # show LoopSpec fields
  bd formula primitives on_complete # alias + snake_case match
  bd formula schema --json          # machine-readable

Lookup prefers exact match over Spec/Rule(s) suffix-strip, so "gate"
resolves to Gate (not GateRule). Fields tagged json:"-" (Step.SourceFormula,
Step.SourceLocation) are excluded.

TestSchemaGenIsCurrent re-runs the AST walker in-memory and byte-diffs
against the committed schema_gen.go, so a new struct in types.go without
a regen will fail CI.

Item 2 of GH#3621.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* feat(formula): point bd formula --help at the primitive index (beads-lnf)

Extends the cobra Long descriptions on `formula`, `formula list`, and
`formula show` so an agent reading any of them is told that
`bd formula schema` (alias `primitives`), the curated
`examples/formulas/primitives/` directory, and the website primitives
reference exist. Without these pointers, agents land on the formula
surface and conclude there is no primitive index — which is the exact
failure mode GH#3621 is closing.

Adds cmd/bd/testdata/formula_help.txt as the regression gate: a
scripttest that runs `bd formula --help` (plus `--help` on the list
and show subcommands) and asserts the four pointers are present. If
someone deletes a pointer, the snapshot fails.

website/docs/cli-reference/formula.md is auto-generated from the Long
descriptions via `bd help --doc formula`, so the website doc inherits
the pointers on its next regeneration with no manual edit.

Item 1 of GH#3621.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* feat(formula): curated primitive examples + smoke harness (beads-0sp)

Adds examples/formulas/primitives/ with one minimal `.formula.toml` per
empirically wired primitive in internal/formula/types.go:

  loop-count    LoopSpec.count       expand body 3x → .iter1.work…
  loop-range    LoopSpec.range/var   {n} substitutes into title
  children-epic Step.Children        epic + children w/ sibling needs
  branch-fanin  ComposeRules.Branch  fork-join, deploy depends on 3 sibs
  condition     Step.Condition       var-driven step inclusion
  gate-timer    Step.Gate            sibling gate-typed issue blocks step

Each fixture has a header doc-comment explaining the primitive in
plain language; README.md indexes them with a one-line description
each and points back at `bd formula schema`.

cmd/bd/formula_primitives_test.go is the smoke harness: for each
fixture it parses, cooks through the full transformation pipeline
(loops → branches → gates → advice → expansions → conditions), and
asserts the primitive's observable effect on the cooked subgraph
(issue IDs, blocking deps, substituted titles, gate await_type).
Cooks against an in-memory subgraph rather than a Dolt-backed temp
DB so the harness exercises the primitive transformation, not the
persistence/auto-export infrastructure.

A subtest fails if a fixture is added to the directory without a
registered assertion in cases[] — the harness exists to prove
primitives are wired, not just that fixtures parse.

Explicitly omits an `on_complete` fixture; that primitive is a
runtime no-op as of v1.0.3 and ships with beads-i02's resolution,
not this epic.

Item 3 of GH#3621.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* fix(formula): satisfy CI gosec G306 + regen CLI doc for new help text

Two CI fixes for the formula primitives PR:

  - internal/formula/cmd/schemagen/main.go:26 — drop WriteFile perms
    from 0644 to 0600 (gosec G306).
  - website/docs/cli-reference/formula.md and the v1.0.0 mirror —
    regenerated from the updated `bd formula --help` Long text via
    scripts/generate-cli-docs.sh, using a CGO_ENABLED=0 bd build to
    match CI.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* fix(formula): distinguish schema from wired primitives

* fix(formula): regen schema_gen.go for rebase onto current main

Upstream main's Formula struct gained an Intent field after PR #3781 was
opened; regenerate schema_gen.go via go generate so
TestSchemaGenIsCurrent passes after rebasing the PR's commits onto
current main.

Maintainer fix-merge for gastownhall/beads#3781.

Agent-Signature: claude-fable-5-medium on behalf of matt wilkie

---------

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Co-authored-by: matt wilkie <maphew@gmail.com>
2026-07-06 22:26:41 -07:00

236 lines
6.4 KiB
Go

package main
import (
"os"
"path/filepath"
"strings"
"testing"
"github.com/steveyegge/beads/internal/types"
)
// TestFormulaPrimitiveExamples is the smoke harness for
// examples/formulas/primitives/. Each entry parses a fixture, cooks it
// through the full transformation pipeline, and asserts the primitive's
// observable effect on the resulting subgraph.
//
// A new fixture added to the directory without a registered case here is
// a deliberate failure (subTestForUnregisteredFixtures): the harness
// exists to prove primitives are wired, not that fixtures parse.
func TestFormulaPrimitiveExamples(t *testing.T) {
primDir := primitivesDir(t)
cases := []struct {
name string
vars map[string]string // non-nil enables Step.Condition filtering with formula defaults merged in
assert func(t *testing.T, sg *TemplateSubgraph)
}{
{
name: "loop-count",
assert: func(t *testing.T, sg *TemplateSubgraph) {
for _, want := range []string{
"loop-count.process.iter1.work",
"loop-count.process.iter2.work",
"loop-count.process.iter3.work",
} {
if !hasIssueID(sg, want) {
t.Errorf("missing issue %q; have %v", want, allIssueIDs(sg))
}
}
},
},
{
name: "loop-range",
assert: func(t *testing.T, sg *TemplateSubgraph) {
for _, want := range []string{"Move 1", "Move 2", "Move 3"} {
if !hasIssueTitle(sg, want) {
t.Errorf("missing title %q; have %v", want, allIssueTitles(sg))
}
}
},
},
{
name: "children-epic",
assert: func(t *testing.T, sg *TemplateSubgraph) {
// Children get the parent step's ID prefixed to their own.
for _, want := range []string{
"children-epic.ship-feature",
"children-epic.ship-feature.draft",
"children-epic.ship-feature.polish",
} {
if !hasIssueID(sg, want) {
t.Errorf("missing issue %q; have %v", want, allIssueIDs(sg))
}
}
if !hasBlockingDep(sg, "children-epic.ship-feature.polish", "children-epic.ship-feature.draft") {
t.Errorf("polish must block on draft; deps: %v", allDeps(sg))
}
},
},
{
name: "branch-fanin",
assert: func(t *testing.T, sg *TemplateSubgraph) {
for _, src := range []string{"test", "lint", "build"} {
if !hasBlockingDep(sg, "branch-fanin.deploy", "branch-fanin."+src) {
t.Errorf("deploy must block on %s; deps: %v", src, allDeps(sg))
}
}
},
},
{
name: "condition",
vars: map[string]string{}, // trigger filtering with formula's deploy=false default
assert: func(t *testing.T, sg *TemplateSubgraph) {
children := childIssueIDs(sg, "condition")
if len(children) != 2 {
t.Errorf("default deploy=false → want 2 child issues, got %d: %v", len(children), children)
}
if hasIssueID(sg, "condition.deploy") {
t.Error("deploy must be excluded when deploy=false")
}
},
},
{
name: "condition-true",
vars: map[string]string{"deploy": "true"},
assert: func(t *testing.T, sg *TemplateSubgraph) {
children := childIssueIDs(sg, "condition")
if len(children) != 3 {
t.Errorf("deploy=true → want 3 child issues, got %d: %v", len(children), children)
}
if !hasIssueID(sg, "condition.deploy") {
t.Error("deploy must be included when deploy=true")
}
},
},
{
name: "gate-timer",
assert: func(t *testing.T, sg *TemplateSubgraph) {
gate := findGate(sg)
if gate == nil {
t.Fatalf("no gate-typed sibling issue in subgraph; issues: %v", allIssueIDs(sg))
}
if gate.AwaitType != "timer" {
t.Errorf("gate.AwaitType = %q, want %q", gate.AwaitType, "timer")
}
if !hasBlockingDep(sg, "gate-timer.verify", gate.ID) {
t.Errorf("verify must block on gate %s; deps: %v", gate.ID, allDeps(sg))
}
},
},
}
registered := map[string]bool{}
for _, c := range cases {
// "condition-true" is a second pass on the "condition" fixture.
formulaName := strings.TrimSuffix(c.name, "-true")
registered[formulaName] = true
t.Run(c.name, func(t *testing.T) {
sg, err := resolveAndCookFormulaWithVars(formulaName, []string{primDir}, c.vars)
if err != nil {
t.Fatalf("cook %q: %v", formulaName, err)
}
c.assert(t, sg)
})
}
t.Run("every_fixture_has_a_registered_case", func(t *testing.T) {
entries, err := os.ReadDir(primDir)
if err != nil {
t.Fatalf("readdir %s: %v", primDir, err)
}
for _, e := range entries {
if !strings.HasSuffix(e.Name(), ".formula.toml") && !strings.HasSuffix(e.Name(), ".formula.json") {
continue
}
name := strings.TrimSuffix(e.Name(), ".formula.toml")
name = strings.TrimSuffix(name, ".formula.json")
if !registered[name] {
t.Errorf("fixture %q has no smoke assertion in cases[]; add one or delete the fixture", e.Name())
}
}
})
}
func primitivesDir(t *testing.T) string {
t.Helper()
dir, err := filepath.Abs(filepath.Join("..", "..", "examples", "formulas", "primitives"))
if err != nil {
t.Fatalf("abs path: %v", err)
}
if _, err := os.Stat(dir); err != nil {
t.Fatalf("primitives dir missing: %v", err)
}
return dir
}
func hasIssueID(sg *TemplateSubgraph, id string) bool {
for _, i := range sg.Issues {
if i.ID == id {
return true
}
}
return false
}
func hasIssueTitle(sg *TemplateSubgraph, title string) bool {
for _, i := range sg.Issues {
if i.Title == title {
return true
}
}
return false
}
func hasBlockingDep(sg *TemplateSubgraph, from, to string) bool {
for _, d := range sg.Dependencies {
if d.IssueID == from && d.DependsOnID == to && d.Type == "blocks" {
return true
}
}
return false
}
func findGate(sg *TemplateSubgraph) *types.Issue {
for _, i := range sg.Issues {
if i.IssueType == "gate" {
return i
}
}
return nil
}
func childIssueIDs(sg *TemplateSubgraph, rootID string) []string {
var out []string
for _, i := range sg.Issues {
if i.ID != rootID {
out = append(out, i.ID)
}
}
return out
}
func allIssueIDs(sg *TemplateSubgraph) []string {
out := make([]string, 0, len(sg.Issues))
for _, i := range sg.Issues {
out = append(out, i.ID)
}
return out
}
func allIssueTitles(sg *TemplateSubgraph) []string {
out := make([]string, 0, len(sg.Issues))
for _, i := range sg.Issues {
out = append(out, i.Title)
}
return out
}
func allDeps(sg *TemplateSubgraph) []string {
out := make([]string, 0, len(sg.Dependencies))
for _, d := range sg.Dependencies {
out = append(out, d.IssueID+" -> "+d.DependsOnID+" ("+string(d.Type)+")")
}
return out
}