Add expand and map operators that apply macro-style template expansion:
- expand(target, template) - replace a single step with expanded template
- map(select, template) - replace all matching steps with expanded template
The expansion formula type (e.g., rule-of-five) uses a template field with
{target} and {target.description} placeholders that are substituted when
applied to target steps.
Changes:
- Add Template field to Formula struct for expansion formulas
- Add ExpandRule and MapRule types to ComposeRules
- Implement ApplyExpansions in new expand.go
- Add LoadByName method to Parser for loading expansion formulas
- Integrate expansion into bd cook command
- Add comprehensive tests
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
315 lines
7.8 KiB
Go
315 lines
7.8 KiB
Go
package formula
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func TestSubstituteTargetPlaceholders(t *testing.T) {
|
|
target := &Step{
|
|
ID: "implement",
|
|
Title: "Implement the feature",
|
|
Description: "Write the code for the feature",
|
|
}
|
|
|
|
tests := []struct {
|
|
name string
|
|
input string
|
|
expected string
|
|
}{
|
|
{
|
|
name: "basic target substitution",
|
|
input: "{target}.draft",
|
|
expected: "implement.draft",
|
|
},
|
|
{
|
|
name: "target.id substitution",
|
|
input: "{target.id}.refine",
|
|
expected: "implement.refine",
|
|
},
|
|
{
|
|
name: "target.title substitution",
|
|
input: "Working on: {target.title}",
|
|
expected: "Working on: Implement the feature",
|
|
},
|
|
{
|
|
name: "target.description substitution",
|
|
input: "Task: {target.description}",
|
|
expected: "Task: Write the code for the feature",
|
|
},
|
|
{
|
|
name: "multiple substitutions",
|
|
input: "{target}: {target.description}",
|
|
expected: "implement: Write the code for the feature",
|
|
},
|
|
{
|
|
name: "no placeholders",
|
|
input: "plain text",
|
|
expected: "plain text",
|
|
},
|
|
{
|
|
name: "empty string",
|
|
input: "",
|
|
expected: "",
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
result := substituteTargetPlaceholders(tt.input, target)
|
|
if result != tt.expected {
|
|
t.Errorf("substituteTargetPlaceholders(%q) = %q, want %q", tt.input, result, tt.expected)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestExpandStep(t *testing.T) {
|
|
target := &Step{
|
|
ID: "implement",
|
|
Title: "Implement the feature",
|
|
Description: "Write the code",
|
|
}
|
|
|
|
template := []*Step{
|
|
{
|
|
ID: "{target}.draft",
|
|
Title: "Draft: {target.title}",
|
|
Description: "Initial attempt at: {target.description}",
|
|
},
|
|
{
|
|
ID: "{target}.refine",
|
|
Title: "Refine: {target.title}",
|
|
Needs: []string{"{target}.draft"},
|
|
},
|
|
}
|
|
|
|
result := expandStep(target, template)
|
|
|
|
if len(result) != 2 {
|
|
t.Fatalf("expected 2 steps, got %d", len(result))
|
|
}
|
|
|
|
// Check first step
|
|
if result[0].ID != "implement.draft" {
|
|
t.Errorf("step 0 ID = %q, want %q", result[0].ID, "implement.draft")
|
|
}
|
|
if result[0].Title != "Draft: Implement the feature" {
|
|
t.Errorf("step 0 Title = %q, want %q", result[0].Title, "Draft: Implement the feature")
|
|
}
|
|
if result[0].Description != "Initial attempt at: Write the code" {
|
|
t.Errorf("step 0 Description = %q, want %q", result[0].Description, "Initial attempt at: Write the code")
|
|
}
|
|
|
|
// Check second step
|
|
if result[1].ID != "implement.refine" {
|
|
t.Errorf("step 1 ID = %q, want %q", result[1].ID, "implement.refine")
|
|
}
|
|
if len(result[1].Needs) != 1 || result[1].Needs[0] != "implement.draft" {
|
|
t.Errorf("step 1 Needs = %v, want [implement.draft]", result[1].Needs)
|
|
}
|
|
}
|
|
|
|
func TestReplaceStep(t *testing.T) {
|
|
steps := []*Step{
|
|
{ID: "design", Title: "Design"},
|
|
{ID: "implement", Title: "Implement"},
|
|
{ID: "test", Title: "Test"},
|
|
}
|
|
|
|
replacement := []*Step{
|
|
{ID: "implement.draft", Title: "Draft"},
|
|
{ID: "implement.refine", Title: "Refine"},
|
|
}
|
|
|
|
result := replaceStep(steps, "implement", replacement)
|
|
|
|
if len(result) != 4 {
|
|
t.Fatalf("expected 4 steps, got %d", len(result))
|
|
}
|
|
|
|
expected := []string{"design", "implement.draft", "implement.refine", "test"}
|
|
for i, exp := range expected {
|
|
if result[i].ID != exp {
|
|
t.Errorf("result[%d].ID = %q, want %q", i, result[i].ID, exp)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestApplyExpansions(t *testing.T) {
|
|
// Create a temporary directory with an expansion formula
|
|
tmpDir := t.TempDir()
|
|
|
|
// Create rule-of-five expansion formula
|
|
ruleOfFive := `{
|
|
"formula": "rule-of-five",
|
|
"type": "expansion",
|
|
"version": 1,
|
|
"template": [
|
|
{"id": "{target}.draft", "title": "Draft: {target.title}"},
|
|
{"id": "{target}.refine", "title": "Refine", "needs": ["{target}.draft"]}
|
|
]
|
|
}`
|
|
err := os.WriteFile(filepath.Join(tmpDir, "rule-of-five.formula.json"), []byte(ruleOfFive), 0644)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// Create parser with temp dir as search path
|
|
parser := NewParser(tmpDir)
|
|
|
|
// Test expand operator
|
|
t.Run("expand single step", func(t *testing.T) {
|
|
steps := []*Step{
|
|
{ID: "design", Title: "Design"},
|
|
{ID: "implement", Title: "Implement the feature"},
|
|
{ID: "test", Title: "Test"},
|
|
}
|
|
|
|
compose := &ComposeRules{
|
|
Expand: []*ExpandRule{
|
|
{Target: "implement", With: "rule-of-five"},
|
|
},
|
|
}
|
|
|
|
result, err := ApplyExpansions(steps, compose, parser)
|
|
if err != nil {
|
|
t.Fatalf("ApplyExpansions failed: %v", err)
|
|
}
|
|
|
|
if len(result) != 4 {
|
|
t.Fatalf("expected 4 steps, got %d", len(result))
|
|
}
|
|
|
|
expected := []string{"design", "implement.draft", "implement.refine", "test"}
|
|
for i, exp := range expected {
|
|
if result[i].ID != exp {
|
|
t.Errorf("result[%d].ID = %q, want %q", i, result[i].ID, exp)
|
|
}
|
|
}
|
|
})
|
|
|
|
// Test map operator
|
|
t.Run("map over pattern", func(t *testing.T) {
|
|
steps := []*Step{
|
|
{ID: "design", Title: "Design"},
|
|
{ID: "impl.auth", Title: "Implement auth"},
|
|
{ID: "impl.api", Title: "Implement API"},
|
|
{ID: "test", Title: "Test"},
|
|
}
|
|
|
|
compose := &ComposeRules{
|
|
Map: []*MapRule{
|
|
{Select: "impl.*", With: "rule-of-five"},
|
|
},
|
|
}
|
|
|
|
result, err := ApplyExpansions(steps, compose, parser)
|
|
if err != nil {
|
|
t.Fatalf("ApplyExpansions failed: %v", err)
|
|
}
|
|
|
|
// design + (impl.auth -> 2 steps) + (impl.api -> 2 steps) + test = 6
|
|
if len(result) != 6 {
|
|
t.Fatalf("expected 6 steps, got %d", len(result))
|
|
}
|
|
|
|
// Verify the expanded IDs
|
|
expectedIDs := []string{
|
|
"design",
|
|
"impl.auth.draft", "impl.auth.refine",
|
|
"impl.api.draft", "impl.api.refine",
|
|
"test",
|
|
}
|
|
for i, exp := range expectedIDs {
|
|
if result[i].ID != exp {
|
|
t.Errorf("result[%d].ID = %q, want %q", i, result[i].ID, exp)
|
|
}
|
|
}
|
|
})
|
|
|
|
// Test missing formula
|
|
t.Run("missing expansion formula", func(t *testing.T) {
|
|
steps := []*Step{{ID: "test", Title: "Test"}}
|
|
compose := &ComposeRules{
|
|
Expand: []*ExpandRule{
|
|
{Target: "test", With: "nonexistent"},
|
|
},
|
|
}
|
|
|
|
_, err := ApplyExpansions(steps, compose, parser)
|
|
if err == nil {
|
|
t.Error("expected error for missing formula")
|
|
}
|
|
})
|
|
|
|
// Test missing target step
|
|
t.Run("missing target step", func(t *testing.T) {
|
|
steps := []*Step{{ID: "test", Title: "Test"}}
|
|
compose := &ComposeRules{
|
|
Expand: []*ExpandRule{
|
|
{Target: "nonexistent", With: "rule-of-five"},
|
|
},
|
|
}
|
|
|
|
_, err := ApplyExpansions(steps, compose, parser)
|
|
if err == nil {
|
|
t.Error("expected error for missing target step")
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestBuildStepMap(t *testing.T) {
|
|
steps := []*Step{
|
|
{
|
|
ID: "parent",
|
|
Title: "Parent",
|
|
Children: []*Step{
|
|
{ID: "child1", Title: "Child 1"},
|
|
{ID: "child2", Title: "Child 2"},
|
|
},
|
|
},
|
|
{ID: "sibling", Title: "Sibling"},
|
|
}
|
|
|
|
stepMap := buildStepMap(steps)
|
|
|
|
if len(stepMap) != 4 {
|
|
t.Errorf("expected 4 steps in map, got %d", len(stepMap))
|
|
}
|
|
|
|
expectedIDs := []string{"parent", "child1", "child2", "sibling"}
|
|
for _, id := range expectedIDs {
|
|
if _, ok := stepMap[id]; !ok {
|
|
t.Errorf("step %q not found in map", id)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestUpdateDependenciesForExpansion(t *testing.T) {
|
|
steps := []*Step{
|
|
{ID: "design", Title: "Design"},
|
|
{ID: "test", Title: "Test", Needs: []string{"implement"}},
|
|
{ID: "deploy", Title: "Deploy", DependsOn: []string{"implement", "test"}},
|
|
}
|
|
|
|
result := UpdateDependenciesForExpansion(steps, "implement", "implement.refine")
|
|
|
|
// Check test step
|
|
if len(result[1].Needs) != 1 || result[1].Needs[0] != "implement.refine" {
|
|
t.Errorf("test step Needs = %v, want [implement.refine]", result[1].Needs)
|
|
}
|
|
|
|
// Check deploy step
|
|
if len(result[2].DependsOn) != 2 {
|
|
t.Fatalf("deploy step DependsOn length = %d, want 2", len(result[2].DependsOn))
|
|
}
|
|
if result[2].DependsOn[0] != "implement.refine" {
|
|
t.Errorf("deploy step DependsOn[0] = %q, want %q", result[2].DependsOn[0], "implement.refine")
|
|
}
|
|
if result[2].DependsOn[1] != "test" {
|
|
t.Errorf("deploy step DependsOn[1] = %q, want %q", result[2].DependsOn[1], "test")
|
|
}
|
|
}
|