diff --git a/cmd/bd/formula_fixture_proxied_test.go b/cmd/bd/formula_fixture_proxied_test.go new file mode 100644 index 000000000..3a880875b --- /dev/null +++ b/cmd/bd/formula_fixture_proxied_test.go @@ -0,0 +1,27 @@ +//go:build cgo + +package main + +import ( + "encoding/json" + "os" + "path/filepath" + "testing" + + "github.com/steveyegge/beads/internal/formula" +) + +func writeFormulaFixture(t *testing.T, p proxiedProject, f *formula.Formula) { + t.Helper() + data, err := json.Marshal(f) + if err != nil { + t.Fatalf("marshal formula %s: %v", f.Formula, err) + } + formulasDir := filepath.Join(p.beadsDir, "formulas") + if err := os.MkdirAll(formulasDir, 0o755); err != nil { + t.Fatalf("mkdir formulas dir: %v", err) + } + if err := os.WriteFile(filepath.Join(formulasDir, f.Formula+formula.FormulaExt), data, 0o644); err != nil { + t.Fatalf("write formula fixture %s: %v", f.Formula, err) + } +} diff --git a/cmd/bd/mol_bond_proxied_integration_test.go b/cmd/bd/mol_bond_proxied_integration_test.go new file mode 100644 index 000000000..6b36773a3 --- /dev/null +++ b/cmd/bd/mol_bond_proxied_integration_test.go @@ -0,0 +1,197 @@ +//go:build cgo + +package main + +import ( + "encoding/json" + "strings" + "testing" + + "github.com/steveyegge/beads/internal/formula" +) + +func twoStepFormula(name, stepTitle string) *formula.Formula { + return &formula.Formula{ + Formula: name, + Version: 1, + Type: formula.TypeWorkflow, + Steps: []*formula.Step{ + {ID: "step1", Title: stepTitle, Type: "task"}, + }, + } +} + +func TestProxiedServerMolBond(t *testing.T) { + requireSharedProxiedServer(t) + t.Parallel() + bd := buildEmbeddedBD(t) + + t.Run("label_only_protos_bond_as_compound_molecule_not_proto", func(t *testing.T) { + t.Parallel() + p := newSharedProxiedProject(t, bd, "mbpp") + protoA := bdProxiedCreate(t, bd, p.dir, "Proto A", "--type", "epic", "--label", "template") + protoB := bdProxiedCreate(t, bd, p.dir, "Proto B", "--type", "epic", "--label", "template") + + out, err := bdProxiedRun(t, bd, p.dir, "mol", "bond", protoA.ID, protoB.ID, "--type", "sequential", "--json") + if err != nil { + t.Fatalf("bd mol bond --json: %v\n%s", err, out) + } + var got struct { + ResultID string `json:"result_id"` + ResultType string `json:"result_type"` + } + if err := json.Unmarshal(out, &got); err != nil { + t.Fatalf("unmarshal: %v\n%s", err, out) + } + if got.ResultType != "compound_molecule" { + t.Errorf("result_type = %s, want compound_molecule (label-only DB protos are not detected as protos by bond dispatch — update this test if that changes)", got.ResultType) + } + if got.ResultID != protoA.ID { + t.Errorf("result_id = %s, want %s (mol+mol bonds attach B to A)", got.ResultID, protoA.ID) + } + + db := openProxiedDB(t, p) + assertProxiedDepExistsWithType(t, db, protoB.ID, protoA.ID, "blocks") + }) + + t.Run("formula_plus_formula_currently_errors", func(t *testing.T) { + t.Parallel() + p := newSharedProxiedProject(t, bd, "mbppf") + writeFormulaFixture(t, p, twoStepFormula("proto-a", "Step of proto A")) + writeFormulaFixture(t, p, twoStepFormula("proto-b", "Step of proto B")) + + stdout, stderr, err := bdProxiedRunBuffers(t, bd, p.dir, "mol", "bond", "proto-a", "proto-b", "--type", "sequential") + if err == nil { + t.Fatalf("expected bonding two cooked-but-unpersisted formulas to error (pre-existing limitation); got success: %s", stdout) + } + if !strings.Contains(stdout+stderr, "proto-a") { + t.Errorf("expected error to reference proto-a, got stdout:%s stderr:%s", stdout, stderr) + } + }) + + t.Run("proto_plus_mol_spawns_and_attaches", func(t *testing.T) { + t.Parallel() + p := newSharedProxiedProject(t, bd, "mbpm") + writeFormulaFixture(t, p, twoStepFormula("diagnostic", "Diagnostic step")) + mol := bdProxiedCreate(t, bd, p.dir, "Live molecule", "--type", "epic") + + out, err := bdProxiedRun(t, bd, p.dir, "mol", "bond", "diagnostic", mol.ID, "--type", "sequential", "--json") + if err != nil { + t.Fatalf("bd mol bond --json: %v\n%s", err, out) + } + var got struct { + ResultID string `json:"result_id"` + ResultType string `json:"result_type"` + Spawned int `json:"spawned"` + } + if err := json.Unmarshal(out, &got); err != nil { + t.Fatalf("unmarshal: %v\n%s", err, out) + } + if got.ResultType != "compound_molecule" || got.ResultID != mol.ID { + t.Fatalf("expected compound_molecule attached to %s, got %+v", mol.ID, got) + } + if got.Spawned != 2 { + t.Fatalf("spawned = %d, want 2 (proto root + step)", got.Spawned) + } + + show := bdProxiedShow(t, bd, p.dir, mol.ID) + if show.ID != mol.ID { + t.Fatalf("expected molecule %s to still exist after bond", mol.ID) + } + }) + + t.Run("mol_plus_mol_adds_dependency_and_rejects_cycle", func(t *testing.T) { + t.Parallel() + p := newSharedProxiedProject(t, bd, "mbmm") + molA := bdProxiedCreate(t, bd, p.dir, "Mol A", "--type", "epic") + molB := bdProxiedCreate(t, bd, p.dir, "Mol B", "--type", "epic") + + out, err := bdProxiedRun(t, bd, p.dir, "mol", "bond", molA.ID, molB.ID, "--type", "sequential", "--json") + if err != nil { + t.Fatalf("bd mol bond --json: %v\n%s", err, out) + } + var got struct { + ResultType string `json:"result_type"` + } + if err := json.Unmarshal(out, &got); err != nil { + t.Fatalf("unmarshal: %v\n%s", err, out) + } + if got.ResultType != "compound_molecule" { + t.Fatalf("result_type = %s, want compound_molecule", got.ResultType) + } + + db := openProxiedDB(t, p) + assertProxiedDepExistsWithType(t, db, molB.ID, molA.ID, "blocks") + + stdout, stderr, err := bdProxiedRunBuffers(t, bd, p.dir, "mol", "bond", molB.ID, molA.ID, "--type", "sequential") + if err == nil { + t.Fatalf("expected cycle rejection, got success: %s", stdout) + } + if !strings.Contains(stdout+stderr, "cycle") { + t.Errorf("expected 'cycle' in error, got stdout:%s stderr:%s", stdout, stderr) + } + }) + + t.Run("ref_dynamic_bonding_generates_readable_ids", func(t *testing.T) { + t.Parallel() + p := newSharedProxiedProject(t, bd, "mbref") + writeFormulaFixture(t, p, twoStepFormula("arm", "Capture {{worker_name}}")) + patrol := bdProxiedCreate(t, bd, p.dir, "Patrol", "--type", "epic") + + out, err := bdProxiedRun(t, bd, p.dir, "mol", "bond", "arm", patrol.ID, + "--ref", "arm-{{worker_name}}", "--var", "worker_name=ace", "--json") + if err != nil { + t.Fatalf("bd mol bond --ref --json: %v\n%s", err, out) + } + var got struct { + IDMapping map[string]string `json:"id_mapping"` + } + if err := json.Unmarshal(out, &got); err != nil { + t.Fatalf("unmarshal: %v\n%s", err, out) + } + expectedRootID := patrol.ID + ".arm-ace" + found := false + for _, v := range got.IDMapping { + if v == expectedRootID { + found = true + } + } + if !found { + t.Fatalf("expected id_mapping to contain root ID %q, got %+v", expectedRootID, got.IDMapping) + } + + substituted := false + for _, newID := range got.IDMapping { + if newID == expectedRootID { + continue + } + issue := bdProxiedShow(t, bd, p.dir, newID) + if strings.Contains(issue.Title, "ace") { + substituted = true + } + } + if !substituted { + t.Errorf("expected a spawned step title to contain 'ace' after {{worker_name}} substitution, id_mapping: %+v", got.IDMapping) + } + }) + + t.Run("dry_run_creates_nothing", func(t *testing.T) { + t.Parallel() + p := newSharedProxiedProject(t, bd, "mbdr") + writeFormulaFixture(t, p, twoStepFormula("dry-proto-a", "Dry proto A step")) + writeFormulaFixture(t, p, twoStepFormula("dry-proto-b", "Dry proto B step")) + + before := bdProxiedListJSON(t, bd, p, "--all") + stdout, _, err := bdProxiedRunBuffers(t, bd, p.dir, "mol", "bond", "dry-proto-a", "dry-proto-b", "--dry-run") + if err != nil { + t.Fatalf("bd mol bond --dry-run: %v\n%s", err, stdout) + } + if !strings.Contains(stdout, "Dry run") { + t.Errorf("expected dry-run preview, got: %s", stdout) + } + after := bdProxiedListJSON(t, bd, p, "--all") + if len(after) != len(before) { + t.Errorf("expected no new issues from --dry-run: before=%d after=%d", len(before), len(after)) + } + }) +} diff --git a/cmd/bd/mol_burn_proxied_integration_test.go b/cmd/bd/mol_burn_proxied_integration_test.go new file mode 100644 index 000000000..767371f3f --- /dev/null +++ b/cmd/bd/mol_burn_proxied_integration_test.go @@ -0,0 +1,117 @@ +//go:build cgo + +package main + +import ( + "encoding/json" + "strings" + "testing" +) + +func TestProxiedServerMolBurn(t *testing.T) { + requireSharedProxiedServer(t) + t.Parallel() + bd := buildEmbeddedBD(t) + + t.Run("burns_single_wisp_molecule", func(t *testing.T) { + t.Parallel() + p := newSharedProxiedProject(t, bd, "mbu") + root := bdProxiedCreate(t, bd, p.dir, "Wisp burn root", "--type", "epic", "--ephemeral") + child := bdProxiedCreate(t, bd, p.dir, "Wisp burn child", "--type", "task", "--parent", root.ID, "--ephemeral") + + out, err := bdProxiedRun(t, bd, p.dir, "mol", "burn", root.ID, "--force", "--json") + if err != nil { + t.Fatalf("bd mol burn --force --json: %v\n%s", err, out) + } + + db := openProxiedDB(t, p) + for _, id := range []string{root.ID, child.ID} { + var count int + if err := db.QueryRow("SELECT COUNT(*) FROM wisps WHERE id = ?", id).Scan(&count); err != nil { + t.Fatalf("query wisps for %s: %v", id, err) + } + if count != 0 { + t.Errorf("expected %s to be deleted from wisps table", id) + } + } + }) + + t.Run("burns_single_persistent_molecule", func(t *testing.T) { + t.Parallel() + p := newSharedProxiedProject(t, bd, "mbup") + root := bdProxiedCreate(t, bd, p.dir, "Persistent burn root", "--type", "epic") + child := bdProxiedCreate(t, bd, p.dir, "Persistent burn child", "--type", "task", "--parent", root.ID) + + out, err := bdProxiedRun(t, bd, p.dir, "mol", "burn", root.ID, "--force", "--json") + if err != nil { + t.Fatalf("bd mol burn --force --json: %v\n%s", err, out) + } + + db := openProxiedDB(t, p) + for _, id := range []string{root.ID, child.ID} { + var count int + if err := db.QueryRow("SELECT COUNT(*) FROM issues WHERE id = ?", id).Scan(&count); err != nil { + t.Fatalf("query issues for %s: %v", id, err) + } + if count != 0 { + t.Errorf("expected %s to be deleted from issues table", id) + } + } + }) + + t.Run("burns_multiple_mixed_wisp_and_persistent", func(t *testing.T) { + t.Parallel() + p := newSharedProxiedProject(t, bd, "mbum") + wispRoot := bdProxiedCreate(t, bd, p.dir, "Mixed wisp root", "--type", "epic", "--ephemeral") + persistentRoot := bdProxiedCreate(t, bd, p.dir, "Mixed persistent root", "--type", "epic") + + out, err := bdProxiedRun(t, bd, p.dir, "mol", "burn", wispRoot.ID, persistentRoot.ID, "--force", "--json") + if err != nil { + t.Fatalf("bd mol burn --force --json (batch): %v\n%s", err, out) + } + var got struct { + TotalDeleted int `json:"total_deleted"` + } + if err := json.Unmarshal(out, &got); err != nil { + t.Fatalf("unmarshal: %v\n%s", err, out) + } + if got.TotalDeleted != 2 { + t.Errorf("total_deleted = %d, want 2", got.TotalDeleted) + } + + db := openProxiedDB(t, p) + var wispCount int + if err := db.QueryRow("SELECT COUNT(*) FROM wisps WHERE id = ?", wispRoot.ID).Scan(&wispCount); err != nil { + t.Fatalf("query wisps: %v", err) + } + if wispCount != 0 { + t.Errorf("expected wisp root %s deleted", wispRoot.ID) + } + var issueCount int + if err := db.QueryRow("SELECT COUNT(*) FROM issues WHERE id = ?", persistentRoot.ID).Scan(&issueCount); err != nil { + t.Fatalf("query issues: %v", err) + } + if issueCount != 0 { + t.Errorf("expected persistent root %s deleted", persistentRoot.ID) + } + }) + + t.Run("dry_run_deletes_nothing", func(t *testing.T) { + t.Parallel() + p := newSharedProxiedProject(t, bd, "mbud") + root := bdProxiedCreate(t, bd, p.dir, "Dry burn root", "--type", "epic") + + stdout, _, err := bdProxiedRunBuffers(t, bd, p.dir, "mol", "burn", root.ID, "--dry-run") + if err != nil { + t.Fatalf("bd mol burn --dry-run: %v\n%s", err, stdout) + } + if !strings.Contains(stdout, "Dry run") { + t.Errorf("expected dry-run preview, got: %s", stdout) + } + + spawned := bdProxiedShow(t, bd, p.dir, root.ID) + if spawned.ID != root.ID { + t.Errorf("expected root %s to survive --dry-run", root.ID) + } + }) +} diff --git a/cmd/bd/mol_current_proxied_integration_test.go b/cmd/bd/mol_current_proxied_integration_test.go new file mode 100644 index 000000000..ed3cbae5c --- /dev/null +++ b/cmd/bd/mol_current_proxied_integration_test.go @@ -0,0 +1,180 @@ +//go:build cgo + +package main + +import ( + "encoding/json" + "fmt" + "strings" + "testing" + + "github.com/steveyegge/beads/internal/formula" +) + +func TestProxiedServerMolCurrent(t *testing.T) { + requireSharedProxiedServer(t) + t.Parallel() + bd := buildEmbeddedBD(t) + + t.Run("explicit_id_shows_step_statuses", func(t *testing.T) { + t.Parallel() + p := newSharedProxiedProject(t, bd, "mcu") + root := bdProxiedCreate(t, bd, p.dir, "Current root", "--type", "epic") + done := bdProxiedCreate(t, bd, p.dir, "Done step", "--type", "task", "--parent", root.ID) + current := bdProxiedCreate(t, bd, p.dir, "Current step", "--type", "task", "--parent", root.ID) + bdProxiedCreate(t, bd, p.dir, "Ready step", "--type", "task", "--parent", root.ID) + + if out, err := bdProxiedRun(t, bd, p.dir, "close", done.ID); err != nil { + t.Fatalf("bd close %s: %v\n%s", done.ID, err, out) + } + if out, err := bdProxiedRun(t, bd, p.dir, "update", current.ID, "--status", "in_progress"); err != nil { + t.Fatalf("bd update %s: %v\n%s", current.ID, err, out) + } + + out, err := bdProxiedRun(t, bd, p.dir, "mol", "current", root.ID, "--json") + if err != nil { + t.Fatalf("bd mol current --json: %v\n%s", err, out) + } + var got []struct { + MoleculeID string `json:"molecule_id"` + Completed int `json:"completed"` + Total int `json:"total"` + Steps []struct { + Status string `json:"status"` + Issue struct { + ID string `json:"id"` + } `json:"issue"` + } `json:"steps"` + } + if err := json.Unmarshal(out, &got); err != nil { + t.Fatalf("unmarshal: %v\n%s", err, out) + } + if len(got) != 1 { + t.Fatalf("expected 1 molecule, got %d", len(got)) + } + if got[0].Completed != 1 || got[0].Total != 3 { + t.Errorf("Completed/Total = %d/%d, want 1/3", got[0].Completed, got[0].Total) + } + statusByID := map[string]string{} + for _, s := range got[0].Steps { + statusByID[s.Issue.ID] = s.Status + } + if statusByID[done.ID] != "done" { + t.Errorf("done step status = %s, want done", statusByID[done.ID]) + } + if statusByID[current.ID] != "current" { + t.Errorf("current step status = %s, want current", statusByID[current.ID]) + } + }) + + t.Run("infers_from_in_progress_assignee", func(t *testing.T) { + t.Parallel() + p := newSharedProxiedProject(t, bd, "mcia") + root := bdProxiedCreate(t, bd, p.dir, "Inferred root", "--type", "epic") + step := bdProxiedCreate(t, bd, p.dir, "Inferred step", "--type", "task", "--parent", root.ID) + if out, err := bdProxiedRun(t, bd, p.dir, "update", step.ID, "--status", "in_progress", "--assignee", "tester"); err != nil { + t.Fatalf("bd update: %v\n%s", err, out) + } + + out, err := bdProxiedRun(t, bd, p.dir, "mol", "current", "--for", "tester", "--json") + if err != nil { + t.Fatalf("bd mol current --for tester --json: %v\n%s", err, out) + } + var got []struct { + MoleculeID string `json:"molecule_id"` + } + if err := json.Unmarshal(out, &got); err != nil { + t.Fatalf("unmarshal: %v\n%s", err, out) + } + if len(got) != 1 || got[0].MoleculeID != root.ID { + t.Fatalf("expected inferred molecule %s, got %+v", root.ID, got) + } + }) + + t.Run("no_molecules_in_progress_empty", func(t *testing.T) { + t.Parallel() + p := newSharedProxiedProject(t, bd, "mcne") + out, err := bdProxiedRun(t, bd, p.dir, "mol", "current", "--for", "nobody", "--json") + if err != nil { + t.Fatalf("bd mol current --json: %v\n%s", err, out) + } + if strings.TrimSpace(string(out)) != "[]" { + t.Errorf("expected empty JSON array, got: %s", out) + } + }) + + t.Run("limit_and_range_truncate_steps", func(t *testing.T) { + t.Parallel() + p := newSharedProxiedProject(t, bd, "mclr") + root := bdProxiedCreate(t, bd, p.dir, "Range root", "--type", "epic") + for i := 0; i < 5; i++ { + bdProxiedCreate(t, bd, p.dir, fmt.Sprintf("Step %d", i), "--type", "task", "--parent", root.ID) + } + + out, err := bdProxiedRun(t, bd, p.dir, "mol", "current", root.ID, "--limit", "2", "--json") + if err != nil { + t.Fatalf("bd mol current --limit 2 --json: %v\n%s", err, out) + } + var got []struct { + Steps []struct{} `json:"steps"` + } + if err := json.Unmarshal(out, &got); err != nil { + t.Fatalf("unmarshal: %v\n%s", err, out) + } + if len(got) != 1 || len(got[0].Steps) != 2 { + t.Fatalf("expected 2 steps with --limit 2, got %+v", got) + } + }) + + t.Run("large_molecule_shows_summary", func(t *testing.T) { + t.Parallel() + p := newSharedProxiedProject(t, bd, "mclg") + + steps := make([]*formula.Step, 0, 101) + for i := 0; i < 101; i++ { + steps = append(steps, &formula.Step{ + ID: fmt.Sprintf("step%d", i), + Title: fmt.Sprintf("Step %d", i), + Type: "task", + }) + } + f := &formula.Formula{ + Formula: "big", + Version: 1, + Type: formula.TypeWorkflow, + Steps: steps, + } + writeFormulaFixture(t, p, f) + + pourOut, err := bdProxiedRun(t, bd, p.dir, "mol", "pour", "big", "--json") + if err != nil { + t.Fatalf("bd mol pour big --json: %v\n%s", err, pourOut) + } + var pourResult struct { + NewEpicID string `json:"new_epic_id"` + Created int `json:"created"` + } + if err := json.Unmarshal(pourOut, &pourResult); err != nil { + t.Fatalf("unmarshal pour result: %v\n%s", err, pourOut) + } + if pourResult.Created != 102 { + t.Fatalf("expected 102 issues created (root+101 steps), got %d", pourResult.Created) + } + + stdout, _, err := bdProxiedRunBuffers(t, bd, p.dir, "mol", "current", pourResult.NewEpicID) + if err != nil { + t.Fatalf("bd mol current: %v\n%s", err, stdout) + } + if !strings.Contains(stdout, "has 101 steps") { + t.Errorf("expected large-molecule summary message, got: %s", stdout) + } + + stdoutFull, _, err := bdProxiedRunBuffers(t, bd, p.dir, "mol", "current", pourResult.NewEpicID, "--limit", "50") + if err != nil { + t.Fatalf("bd mol current --limit 50: %v\n%s", err, stdoutFull) + } + if strings.Contains(stdoutFull, "has 101 steps") { + t.Errorf("--limit 50 should print full step list, not the summary: %s", stdoutFull) + } + }) +} diff --git a/cmd/bd/mol_distill_proxied_integration_test.go b/cmd/bd/mol_distill_proxied_integration_test.go new file mode 100644 index 000000000..568ecd1c0 --- /dev/null +++ b/cmd/bd/mol_distill_proxied_integration_test.go @@ -0,0 +1,121 @@ +//go:build cgo + +package main + +import ( + "encoding/json" + "os" + "path/filepath" + "strings" + "testing" +) + +func TestProxiedServerMolDistill(t *testing.T) { + requireSharedProxiedServer(t) + t.Parallel() + bd := buildEmbeddedBD(t) + + t.Run("distills_epic_to_formula_file", func(t *testing.T) { + t.Parallel() + p := newSharedProxiedProject(t, bd, "mds") + epic := bdProxiedCreate(t, bd, p.dir, "Ship feature auth", "--type", "epic") + bdProxiedCreate(t, bd, p.dir, "Implement auth", "--type", "task", "--parent", epic.ID) + bdProxiedCreate(t, bd, p.dir, "Test auth", "--type", "task", "--parent", epic.ID) + + outputDir := filepath.Join(p.dir, "out") + if err := os.MkdirAll(outputDir, 0o755); err != nil { + t.Fatalf("mkdir output dir: %v", err) + } + + out, err := bdProxiedRun(t, bd, p.dir, "mol", "distill", epic.ID, "auth-workflow", "--output", outputDir, "--json") + if err != nil { + t.Fatalf("bd mol distill --json: %v\n%s", err, out) + } + var got struct { + FormulaName string `json:"formula_name"` + FormulaPath string `json:"formula_path"` + Steps int `json:"steps"` + } + if err := json.Unmarshal(out, &got); err != nil { + t.Fatalf("unmarshal: %v\n%s", err, out) + } + if got.FormulaName != "auth-workflow" { + t.Errorf("formula_name = %s, want auth-workflow", got.FormulaName) + } + if got.Steps != 2 { + t.Errorf("steps = %d, want 2", got.Steps) + } + if _, err := os.Stat(got.FormulaPath); err != nil { + t.Errorf("expected formula file at %s: %v", got.FormulaPath, err) + } + }) + + t.Run("dry_run_writes_nothing", func(t *testing.T) { + t.Parallel() + p := newSharedProxiedProject(t, bd, "mdsd") + epic := bdProxiedCreate(t, bd, p.dir, "Dry run epic", "--type", "epic") + bdProxiedCreate(t, bd, p.dir, "Dry run step", "--type", "task", "--parent", epic.ID) + + outputDir := filepath.Join(p.dir, "dryout") + if err := os.MkdirAll(outputDir, 0o755); err != nil { + t.Fatalf("mkdir output dir: %v", err) + } + + stdout, _, err := bdProxiedRunBuffers(t, bd, p.dir, "mol", "distill", epic.ID, "dry-workflow", "--output", outputDir, "--dry-run") + if err != nil { + t.Fatalf("bd mol distill --dry-run: %v\n%s", err, stdout) + } + if !strings.Contains(stdout, "Dry run") { + t.Errorf("expected dry-run preview, got: %s", stdout) + } + entries, err := os.ReadDir(outputDir) + if err != nil { + t.Fatalf("read output dir: %v", err) + } + if len(entries) != 0 { + t.Errorf("expected no files written under --dry-run, found: %+v", entries) + } + }) + + t.Run("var_flag_both_syntaxes", func(t *testing.T) { + t.Parallel() + p := newSharedProxiedProject(t, bd, "mdsv") + epic := bdProxiedCreate(t, bd, p.dir, "Release feature-auth", "--type", "epic") + bdProxiedCreate(t, bd, p.dir, "Deploy feature-auth to prod", "--type", "task", "--parent", epic.ID) + + outputDir := filepath.Join(p.dir, "varout") + if err := os.MkdirAll(outputDir, 0o755); err != nil { + t.Fatalf("mkdir output dir: %v", err) + } + + out, err := bdProxiedRun(t, bd, p.dir, "mol", "distill", epic.ID, "var-workflow-1", + "--output", outputDir, "--var", "branch=feature-auth", "--json") + if err != nil { + t.Fatalf("bd mol distill (spawn-style var) --json: %v\n%s", err, out) + } + var got1 struct { + Variables []string `json:"variables"` + } + if err := json.Unmarshal(out, &got1); err != nil { + t.Fatalf("unmarshal: %v\n%s", err, out) + } + if len(got1.Variables) != 1 || got1.Variables[0] != "branch" { + t.Errorf("variables = %+v, want [branch]", got1.Variables) + } + + out2, err := bdProxiedRun(t, bd, p.dir, "mol", "distill", epic.ID, "var-workflow-2", + "--output", outputDir, "--var", "feature-auth=branch", "--json") + if err != nil { + t.Fatalf("bd mol distill (substitution-style var) --json: %v\n%s", err, out2) + } + var got2 struct { + Variables []string `json:"variables"` + } + if err := json.Unmarshal(out2, &got2); err != nil { + t.Fatalf("unmarshal: %v\n%s", err, out2) + } + if len(got2.Variables) != 1 || got2.Variables[0] != "branch" { + t.Errorf("variables = %+v, want [branch]", got2.Variables) + } + }) +} diff --git a/cmd/bd/mol_last_activity_proxied_integration_test.go b/cmd/bd/mol_last_activity_proxied_integration_test.go new file mode 100644 index 000000000..be3a7f7fb --- /dev/null +++ b/cmd/bd/mol_last_activity_proxied_integration_test.go @@ -0,0 +1,101 @@ +//go:build cgo + +package main + +import ( + "encoding/json" + "testing" + "time" +) + +func TestProxiedServerMolLastActivity(t *testing.T) { + requireSharedProxiedServer(t) + t.Parallel() + bd := buildEmbeddedBD(t) + + t.Run("root_only_reports_molecule_updated", func(t *testing.T) { + t.Parallel() + p := newSharedProxiedProject(t, bd, "mla") + root := bdProxiedCreate(t, bd, p.dir, "Lonely root", "--type", "epic") + + out, err := bdProxiedRun(t, bd, p.dir, "mol", "last-activity", root.ID, "--json") + if err != nil { + t.Fatalf("bd mol last-activity --json: %v\n%s", err, out) + } + var got struct { + MoleculeID string `json:"molecule_id"` + Source string `json:"source"` + } + if err := json.Unmarshal(out, &got); err != nil { + t.Fatalf("unmarshal: %v\n%s", err, out) + } + if got.Source != "molecule_updated" { + t.Errorf("source = %s, want molecule_updated", got.Source) + } + }) + + t.Run("child_update_wins_over_root", func(t *testing.T) { + t.Parallel() + p := newSharedProxiedProject(t, bd, "mlac") + root := bdProxiedCreate(t, bd, p.dir, "Root before child", "--type", "epic") + child := bdProxiedCreate(t, bd, p.dir, "Child updated later", "--type", "task", "--parent", root.ID) + + db := openProxiedDB(t, p) + now := time.Now().UTC() + if _, err := db.Exec("UPDATE issues SET updated_at = ? WHERE id = ?", now.Add(-time.Hour), root.ID); err != nil { + t.Fatalf("backdate root: %v", err) + } + if _, err := db.Exec("UPDATE issues SET updated_at = ? WHERE id = ?", now, child.ID); err != nil { + t.Fatalf("set child updated_at: %v", err) + } + + out, err := bdProxiedRun(t, bd, p.dir, "mol", "last-activity", root.ID, "--json") + if err != nil { + t.Fatalf("bd mol last-activity --json: %v\n%s", err, out) + } + var got struct { + Source string `json:"source"` + SourceStepID string `json:"source_step_id"` + } + if err := json.Unmarshal(out, &got); err != nil { + t.Fatalf("unmarshal: %v\n%s", err, out) + } + if got.Source != "step_updated" || got.SourceStepID != child.ID { + t.Errorf("source/step = %s/%s, want step_updated/%s", got.Source, got.SourceStepID, child.ID) + } + }) + + t.Run("child_closed_after_update_wins", func(t *testing.T) { + t.Parallel() + p := newSharedProxiedProject(t, bd, "mlacc") + root := bdProxiedCreate(t, bd, p.dir, "Root for closed-child test", "--type", "epic") + child := bdProxiedCreate(t, bd, p.dir, "Child closed last", "--type", "task", "--parent", root.ID) + if out, err := bdProxiedRun(t, bd, p.dir, "close", child.ID); err != nil { + t.Fatalf("bd close: %v\n%s", err, out) + } + + db := openProxiedDB(t, p) + now := time.Now().UTC() + if _, err := db.Exec("UPDATE issues SET updated_at = ? WHERE id = ?", now.Add(-time.Hour), root.ID); err != nil { + t.Fatalf("backdate root: %v", err) + } + if _, err := db.Exec("UPDATE issues SET updated_at = ?, closed_at = ? WHERE id = ?", now.Add(-30*time.Minute), now, child.ID); err != nil { + t.Fatalf("set child closed_at: %v", err) + } + + out, err := bdProxiedRun(t, bd, p.dir, "mol", "last-activity", root.ID, "--json") + if err != nil { + t.Fatalf("bd mol last-activity --json: %v\n%s", err, out) + } + var got struct { + Source string `json:"source"` + SourceStepID string `json:"source_step_id"` + } + if err := json.Unmarshal(out, &got); err != nil { + t.Fatalf("unmarshal: %v\n%s", err, out) + } + if got.Source != "step_closed" || got.SourceStepID != child.ID { + t.Errorf("source/step = %s/%s, want step_closed/%s", got.Source, got.SourceStepID, child.ID) + } + }) +} diff --git a/cmd/bd/mol_port.go b/cmd/bd/mol_port.go index 4b481f5d0..92590010f 100644 --- a/cmd/bd/mol_port.go +++ b/cmd/bd/mol_port.go @@ -116,6 +116,9 @@ func (r uowMolReader) GetIssue(ctx context.Context, id string) (*types.Issue, er if issue == nil { return nil, fmt.Errorf("issue %s not found", id) } + if labels, err := r.uw.LabelUseCase().GetLabels(ctx, id); err == nil { + issue.Labels = labels + } return issue, nil } @@ -126,9 +129,15 @@ func (r uowMolReader) GetIssuesByIDs(ctx context.Context, ids []string) ([]*type } wisps, err := r.uw.IssueUseCase().GetWispsByIDs(ctx, ids) if err != nil { - return issues, nil //nolint:nilerr // wisps table may not exist; issues result still valid + wisps = nil //nolint:staticcheck // wisps table may not exist; issues result still valid } - return append(issues, wisps...), nil + all := append(issues, wisps...) + if labelMap, err := r.uw.LabelUseCase().GetLabelsForIssues(ctx, ids); err == nil { + for _, issue := range all { + issue.Labels = labelMap[issue.ID] + } + } + return all, nil } func (r uowMolReader) GetIssuesByLabel(ctx context.Context, label string) ([]*types.Issue, error) { @@ -306,7 +315,7 @@ func (w *uowMolWriter) CreateIssue(ctx context.Context, issue *types.Issue, acto } func (w *uowMolWriter) AddDependency(ctx context.Context, dep *types.Dependency, actor string) error { - if w.isWisp(ctx, dep.IssueID) || w.isWisp(ctx, dep.DependsOnID) { + if w.isWisp(ctx, dep.IssueID) { return w.uw.DependencyUseCase().AddWispDependency(ctx, dep, actor) } return w.uw.DependencyUseCase().AddDependency(ctx, dep, actor) diff --git a/cmd/bd/mol_progress_proxied_integration_test.go b/cmd/bd/mol_progress_proxied_integration_test.go new file mode 100644 index 000000000..213372392 --- /dev/null +++ b/cmd/bd/mol_progress_proxied_integration_test.go @@ -0,0 +1,81 @@ +//go:build cgo + +package main + +import ( + "encoding/json" + "strings" + "testing" +) + +func TestProxiedServerMolProgress(t *testing.T) { + requireSharedProxiedServer(t) + t.Parallel() + bd := buildEmbeddedBD(t) + + t.Run("explicit_id_reports_totals", func(t *testing.T) { + t.Parallel() + p := newSharedProxiedProject(t, bd, "mpg") + root := bdProxiedCreate(t, bd, p.dir, "Progress root", "--type", "epic") + step1 := bdProxiedCreate(t, bd, p.dir, "Step 1", "--type", "task", "--parent", root.ID) + bdProxiedCreate(t, bd, p.dir, "Step 2", "--type", "task", "--parent", root.ID) + if out, err := bdProxiedRun(t, bd, p.dir, "close", step1.ID); err != nil { + t.Fatalf("bd close: %v\n%s", err, out) + } + + out, err := bdProxiedRun(t, bd, p.dir, "mol", "progress", root.ID, "--json") + if err != nil { + t.Fatalf("bd mol progress --json: %v\n%s", err, out) + } + var got struct { + MoleculeID string `json:"molecule_id"` + Total int `json:"total"` + Completed int `json:"completed"` + } + if err := json.Unmarshal(out, &got); err != nil { + t.Fatalf("unmarshal: %v\n%s", err, out) + } + if got.MoleculeID != root.ID { + t.Errorf("molecule_id = %s, want %s", got.MoleculeID, root.ID) + } + if got.Total != 2 || got.Completed != 1 { + t.Errorf("total/completed = %d/%d, want 2/1", got.Total, got.Completed) + } + }) + + t.Run("infers_current_from_in_progress", func(t *testing.T) { + t.Parallel() + p := newSharedProxiedProject(t, bd, "mpgi") + root := bdProxiedCreate(t, bd, p.dir, "Inferred progress root", "--type", "epic") + step := bdProxiedCreate(t, bd, p.dir, "In progress step", "--type", "task", "--parent", root.ID) + if out, err := bdProxiedRun(t, bd, p.dir, "update", step.ID, "--claim"); err != nil { + t.Fatalf("bd update --claim: %v\n%s", err, out) + } + + out, err := bdProxiedRun(t, bd, p.dir, "mol", "progress", "--json") + if err != nil { + t.Fatalf("bd mol progress --json: %v\n%s", err, out) + } + var got struct { + MoleculeID string `json:"molecule_id"` + } + if err := json.Unmarshal(out, &got); err != nil { + t.Fatalf("unmarshal: %v\n%s", err, out) + } + if got.MoleculeID != root.ID { + t.Errorf("molecule_id = %s, want %s", got.MoleculeID, root.ID) + } + }) + + t.Run("no_molecules_in_progress_empty", func(t *testing.T) { + t.Parallel() + p := newSharedProxiedProject(t, bd, "mpgn") + out, err := bdProxiedRun(t, bd, p.dir, "mol", "progress", "--json") + if err != nil { + t.Fatalf("bd mol progress --json: %v\n%s", err, out) + } + if strings.TrimSpace(string(out)) != "[]" { + t.Errorf("expected empty JSON array, got: %s", out) + } + }) +} diff --git a/cmd/bd/mol_ready_gated_proxied_integration_test.go b/cmd/bd/mol_ready_gated_proxied_integration_test.go new file mode 100644 index 000000000..7bf48b1ee --- /dev/null +++ b/cmd/bd/mol_ready_gated_proxied_integration_test.go @@ -0,0 +1,191 @@ +//go:build cgo + +package main + +import ( + "encoding/json" + "testing" +) + +func TestProxiedServerMolReadyGated(t *testing.T) { + requireSharedProxiedServer(t) + t.Parallel() + bd := buildEmbeddedBD(t) + + setupGateType := func(t *testing.T, bd, dir string) { + t.Helper() + if out, err := bdProxiedRun(t, bd, dir, "config", "set", "types.custom", "gate"); err != nil { + t.Fatalf("bd config set types.custom gate: %v\n%s", err, out) + } + } + + molReadyGated := func(t *testing.T, bd, dir string) []struct { + MoleculeID string `json:"molecule_id"` + } { + t.Helper() + out, err := bdProxiedRun(t, bd, dir, "mol", "ready", "--gated", "--json") + if err != nil { + t.Fatalf("bd mol ready --gated --json: %v\n%s", err, out) + } + var got struct { + Molecules []struct { + MoleculeID string `json:"molecule_id"` + } `json:"molecules"` + } + if err := json.Unmarshal(out, &got); err != nil { + t.Fatalf("unmarshal: %v\n%s", err, out) + } + return got.Molecules + } + + t.Run("no_gates_returns_empty", func(t *testing.T) { + t.Parallel() + p := newSharedProxiedProject(t, bd, "mrgn") + root := bdProxiedCreate(t, bd, p.dir, "No gates root", "--type", "epic") + bdProxiedCreate(t, bd, p.dir, "Plain step", "--type", "task", "--parent", root.ID) + + molecules := molReadyGated(t, bd, p.dir) + if len(molecules) != 0 { + t.Errorf("expected 0 gate-ready molecules, got %d: %+v", len(molecules), molecules) + } + }) + + t.Run("closed_gate_makes_molecule_ready", func(t *testing.T) { + t.Parallel() + p := newSharedProxiedProject(t, bd, "mrgc") + setupGateType(t, bd, p.dir) + + root := bdProxiedCreate(t, bd, p.dir, "Gated root", "--type", "epic") + gate := bdProxiedCreate(t, bd, p.dir, "Gate: ci", "--type", "gate", "--parent", root.ID) + step := bdProxiedCreate(t, bd, p.dir, "Deploy after CI", "--type", "task", "--parent", root.ID) + if out, err := bdProxiedRun(t, bd, p.dir, "dep", "add", step.ID, gate.ID); err != nil { + t.Fatalf("bd dep add: %v\n%s", err, out) + } + if out, err := bdProxiedRun(t, bd, p.dir, "close", gate.ID); err != nil { + t.Fatalf("bd close gate: %v\n%s", err, out) + } + + molecules := molReadyGated(t, bd, p.dir) + if len(molecules) != 1 || molecules[0].MoleculeID != root.ID { + t.Fatalf("expected 1 gate-ready molecule %s, got %+v", root.ID, molecules) + } + }) + + t.Run("open_gate_not_ready", func(t *testing.T) { + t.Parallel() + p := newSharedProxiedProject(t, bd, "mrgo") + setupGateType(t, bd, p.dir) + + root := bdProxiedCreate(t, bd, p.dir, "Open gate root", "--type", "epic") + gate := bdProxiedCreate(t, bd, p.dir, "Gate: pending", "--type", "gate", "--parent", root.ID) + step := bdProxiedCreate(t, bd, p.dir, "Blocked step", "--type", "task", "--parent", root.ID) + if out, err := bdProxiedRun(t, bd, p.dir, "dep", "add", step.ID, gate.ID); err != nil { + t.Fatalf("bd dep add: %v\n%s", err, out) + } + + molecules := molReadyGated(t, bd, p.dir) + if len(molecules) != 0 { + t.Errorf("expected 0 gate-ready molecules with an open gate, got %d: %+v", len(molecules), molecules) + } + }) + + t.Run("hooked_molecule_excluded", func(t *testing.T) { + t.Parallel() + p := newSharedProxiedProject(t, bd, "mrgh") + setupGateType(t, bd, p.dir) + + root := bdProxiedCreate(t, bd, p.dir, "Hooked root", "--type", "epic") + gate := bdProxiedCreate(t, bd, p.dir, "Gate: hooked", "--type", "gate", "--parent", root.ID) + step := bdProxiedCreate(t, bd, p.dir, "Ready step under hooked mol", "--type", "task", "--parent", root.ID) + if out, err := bdProxiedRun(t, bd, p.dir, "dep", "add", step.ID, gate.ID); err != nil { + t.Fatalf("bd dep add: %v\n%s", err, out) + } + if out, err := bdProxiedRun(t, bd, p.dir, "close", gate.ID); err != nil { + t.Fatalf("bd close gate: %v\n%s", err, out) + } + + db := openProxiedDB(t, p) + if _, err := db.Exec("UPDATE issues SET status = 'hooked' WHERE id = ?", root.ID); err != nil { + t.Fatalf("mark root hooked: %v", err) + } + + molecules := molReadyGated(t, bd, p.dir) + for _, m := range molecules { + if m.MoleculeID == root.ID { + t.Errorf("hooked molecule %s should be excluded from gate-ready results", root.ID) + } + } + }) + + t.Run("multiple_gates_multiple_molecules", func(t *testing.T) { + t.Parallel() + p := newSharedProxiedProject(t, bd, "mrgm") + setupGateType(t, bd, p.dir) + + var roots []string + for i := 0; i < 3; i++ { + root := bdProxiedCreate(t, bd, p.dir, "Multi gate root", "--type", "epic") + gate := bdProxiedCreate(t, bd, p.dir, "Gate", "--type", "gate", "--parent", root.ID) + step := bdProxiedCreate(t, bd, p.dir, "Step", "--type", "task", "--parent", root.ID) + if out, err := bdProxiedRun(t, bd, p.dir, "dep", "add", step.ID, gate.ID); err != nil { + t.Fatalf("bd dep add: %v\n%s", err, out) + } + if out, err := bdProxiedRun(t, bd, p.dir, "close", gate.ID); err != nil { + t.Fatalf("bd close gate: %v\n%s", err, out) + } + roots = append(roots, root.ID) + } + + molecules := molReadyGated(t, bd, p.dir) + if len(molecules) != 3 { + t.Fatalf("expected 3 gate-ready molecules, got %d: %+v", len(molecules), molecules) + } + got := map[string]bool{} + for _, m := range molecules { + got[m.MoleculeID] = true + } + for _, r := range roots { + if !got[r] { + t.Errorf("expected root %s in gate-ready results, got %+v", r, molecules) + } + } + }) + + t.Run("bd_ready_gated_matches_bd_mol_ready_gated", func(t *testing.T) { + t.Parallel() + p := newSharedProxiedProject(t, bd, "mrgeq") + setupGateType(t, bd, p.dir) + + root := bdProxiedCreate(t, bd, p.dir, "Equivalence root", "--type", "epic") + gate := bdProxiedCreate(t, bd, p.dir, "Gate: eq", "--type", "gate", "--parent", root.ID) + step := bdProxiedCreate(t, bd, p.dir, "Eq step", "--type", "task", "--parent", root.ID) + if out, err := bdProxiedRun(t, bd, p.dir, "dep", "add", step.ID, gate.ID); err != nil { + t.Fatalf("bd dep add: %v\n%s", err, out) + } + if out, err := bdProxiedRun(t, bd, p.dir, "close", gate.ID); err != nil { + t.Fatalf("bd close gate: %v\n%s", err, out) + } + + viaMol := molReadyGated(t, bd, p.dir) + + out, err := bdProxiedRun(t, bd, p.dir, "ready", "--gated", "--json") + if err != nil { + t.Fatalf("bd ready --gated --json: %v\n%s", err, out) + } + var viaReady struct { + Molecules []struct { + MoleculeID string `json:"molecule_id"` + } `json:"molecules"` + } + if err := json.Unmarshal(out, &viaReady); err != nil { + t.Fatalf("unmarshal: %v\n%s", err, out) + } + + if len(viaMol) != len(viaReady.Molecules) { + t.Fatalf("bd mol ready --gated found %d molecules, bd ready --gated found %d", len(viaMol), len(viaReady.Molecules)) + } + if len(viaMol) != 1 || viaMol[0].MoleculeID != root.ID { + t.Fatalf("expected 1 gate-ready molecule %s, got %+v", root.ID, viaMol) + } + }) +} diff --git a/cmd/bd/mol_show_proxied_integration_test.go b/cmd/bd/mol_show_proxied_integration_test.go new file mode 100644 index 000000000..58aaa699e --- /dev/null +++ b/cmd/bd/mol_show_proxied_integration_test.go @@ -0,0 +1,98 @@ +//go:build cgo + +package main + +import ( + "encoding/json" + "testing" +) + +func TestProxiedServerMolShow(t *testing.T) { + requireSharedProxiedServer(t) + t.Parallel() + bd := buildEmbeddedBD(t) + + t.Run("shows_root_children_and_deps", func(t *testing.T) { + t.Parallel() + p := newSharedProxiedProject(t, bd, "msh") + root := bdProxiedCreate(t, bd, p.dir, "Molecule root", "--type", "epic") + child := bdProxiedCreate(t, bd, p.dir, "Step one", "--type", "task", "--parent", root.ID) + + out, err := bdProxiedRun(t, bd, p.dir, "mol", "show", root.ID, "--json") + if err != nil { + t.Fatalf("bd mol show --json: %v\n%s", err, out) + } + var got struct { + Root struct { + ID string `json:"id"` + } `json:"root"` + Issues []struct { + ID string `json:"id"` + } `json:"issues"` + Dependencies []struct { + IssueID string `json:"issue_id"` + DependsOnID string `json:"depends_on_id"` + } `json:"dependencies"` + } + if err := json.Unmarshal(out, &got); err != nil { + t.Fatalf("unmarshal: %v\n%s", err, out) + } + if got.Root.ID != root.ID { + t.Errorf("root.id = %s, want %s", got.Root.ID, root.ID) + } + if len(got.Issues) != 2 { + t.Fatalf("expected 2 issues (root+child), got %d: %+v", len(got.Issues), got.Issues) + } + foundDep := false + for _, d := range got.Dependencies { + if d.IssueID == child.ID && d.DependsOnID == root.ID { + foundDep = true + } + } + if !foundDep { + t.Errorf("expected parent-child dependency %s -> %s, got %+v", child.ID, root.ID, got.Dependencies) + } + }) + + t.Run("parallel_marks_ready_and_blocked_steps", func(t *testing.T) { + t.Parallel() + p := newSharedProxiedProject(t, bd, "mshp") + root := bdProxiedCreate(t, bd, p.dir, "Parallel root", "--type", "epic") + blocker := bdProxiedCreate(t, bd, p.dir, "Blocker step", "--type", "task", "--parent", root.ID) + blocked := bdProxiedCreate(t, bd, p.dir, "Blocked step", "--type", "task", "--parent", root.ID) + if out, err := bdProxiedRun(t, bd, p.dir, "dep", "add", blocked.ID, blocker.ID); err != nil { + t.Fatalf("bd dep add: %v\n%s", err, out) + } + + out, err := bdProxiedRun(t, bd, p.dir, "mol", "show", root.ID, "--parallel", "--json") + if err != nil { + t.Fatalf("bd mol show --parallel --json: %v\n%s", err, out) + } + var got struct { + Parallel struct { + Steps map[string]struct { + IsReady bool `json:"is_ready"` + BlockedBy []string `json:"blocked_by"` + } `json:"steps"` + } `json:"parallel"` + } + if err := json.Unmarshal(out, &got); err != nil { + t.Fatalf("unmarshal: %v\n%s", err, out) + } + if !got.Parallel.Steps[blocker.ID].IsReady { + t.Errorf("blocker %s should be ready: %+v", blocker.ID, got.Parallel.Steps[blocker.ID]) + } + if got.Parallel.Steps[blocked.ID].IsReady { + t.Errorf("blocked step %s should not be ready", blocked.ID) + } + }) + + t.Run("unknown_id_errors", func(t *testing.T) { + t.Parallel() + p := newSharedProxiedProject(t, bd, "mshu") + stdout, stderr, err := bdProxiedRunBuffers(t, bd, p.dir, "mol", "show", "bd-doesnotexist") + if err == nil { + t.Fatalf("expected error, got stdout:%s stderr:%s", stdout, stderr) + } + }) +} diff --git a/cmd/bd/mol_squash_proxied_integration_test.go b/cmd/bd/mol_squash_proxied_integration_test.go new file mode 100644 index 000000000..2168b4237 --- /dev/null +++ b/cmd/bd/mol_squash_proxied_integration_test.go @@ -0,0 +1,118 @@ +//go:build cgo + +package main + +import ( + "encoding/json" + "testing" +) + +func TestProxiedServerMolSquash(t *testing.T) { + requireSharedProxiedServer(t) + t.Parallel() + bd := buildEmbeddedBD(t) + + t.Run("basic_squash_deletes_ephemeral_children", func(t *testing.T) { + t.Parallel() + p := newSharedProxiedProject(t, bd, "msq") + root := bdProxiedCreate(t, bd, p.dir, "Squash root", "--type", "epic", "--ephemeral") + child := bdProxiedCreate(t, bd, p.dir, "Squash child", "--type", "task", "--parent", root.ID, "--ephemeral") + + out, err := bdProxiedRun(t, bd, p.dir, "mol", "squash", root.ID, "--json") + if err != nil { + t.Fatalf("bd mol squash --json: %v\n%s", err, out) + } + var got struct { + DigestID string `json:"digest_id"` + SquashedCount int `json:"squashed_count"` + DeletedCount int `json:"deleted_count"` + WispSquash bool `json:"wisp_squash"` + } + if err := json.Unmarshal(out, &got); err != nil { + t.Fatalf("unmarshal: %v\n%s", err, out) + } + if got.SquashedCount != 1 || got.DeletedCount != 1 { + t.Errorf("squashed/deleted = %d/%d, want 1/1", got.SquashedCount, got.DeletedCount) + } + if got.DigestID == "" { + t.Fatal("expected non-empty digest_id") + } + if !got.WispSquash { + t.Error("expected wisp_squash=true for an ephemeral root") + } + + db := openProxiedDB(t, p) + var count int + if err := db.QueryRow("SELECT COUNT(*) FROM wisps WHERE id = ?", child.ID).Scan(&count); err != nil { + t.Fatalf("query wisps: %v", err) + } + if count != 0 { + t.Errorf("expected squashed child %s to be deleted from wisps table", child.ID) + } + + var rootStatus string + var rootEphemeral bool + if err := db.QueryRow("SELECT status, ephemeral FROM wisps WHERE id = ?", root.ID).Scan(&rootStatus, &rootEphemeral); err != nil { + t.Fatalf("query root wisp row: %v", err) + } + if rootStatus != "closed" { + t.Errorf("root status = %s, want closed", rootStatus) + } + if rootEphemeral { + t.Error("root wisp flag should be cleared after squash") + } + }) + + t.Run("keep_children_preserves_wisps", func(t *testing.T) { + t.Parallel() + p := newSharedProxiedProject(t, bd, "msqk") + root := bdProxiedCreate(t, bd, p.dir, "Keep-children root", "--type", "epic", "--ephemeral") + child := bdProxiedCreate(t, bd, p.dir, "Keep-children child", "--type", "task", "--parent", root.ID, "--ephemeral") + + out, err := bdProxiedRun(t, bd, p.dir, "mol", "squash", root.ID, "--keep-children", "--json") + if err != nil { + t.Fatalf("bd mol squash --keep-children --json: %v\n%s", err, out) + } + var got struct { + DeletedCount int `json:"deleted_count"` + KeptChildren bool `json:"kept_children"` + } + if err := json.Unmarshal(out, &got); err != nil { + t.Fatalf("unmarshal: %v\n%s", err, out) + } + if got.DeletedCount != 0 || !got.KeptChildren { + t.Errorf("deleted_count/kept_children = %d/%v, want 0/true", got.DeletedCount, got.KeptChildren) + } + + db := openProxiedDB(t, p) + var count int + if err := db.QueryRow("SELECT COUNT(*) FROM wisps WHERE id = ?", child.ID).Scan(&count); err != nil { + t.Fatalf("query wisps: %v", err) + } + if count != 1 { + t.Errorf("expected kept child %s to survive squash", child.ID) + } + }) + + t.Run("agent_summary_used_verbatim", func(t *testing.T) { + t.Parallel() + p := newSharedProxiedProject(t, bd, "msqs") + root := bdProxiedCreate(t, bd, p.dir, "Summary root", "--type", "epic", "--ephemeral") + bdProxiedCreate(t, bd, p.dir, "Summary child", "--type", "task", "--parent", root.ID, "--ephemeral") + + out, err := bdProxiedRun(t, bd, p.dir, "mol", "squash", root.ID, "--summary", "Agent-provided summary text", "--json") + if err != nil { + t.Fatalf("bd mol squash --summary --json: %v\n%s", err, out) + } + var got struct { + DigestID string `json:"digest_id"` + } + if err := json.Unmarshal(out, &got); err != nil { + t.Fatalf("unmarshal: %v\n%s", err, out) + } + digest := bdProxiedShow(t, bd, p.dir, got.DigestID) + if digest.Description != "Agent-provided summary text" { + t.Errorf("digest description = %q, want %q", digest.Description, "Agent-provided summary text") + } + }) +} diff --git a/cmd/bd/mol_stale_proxied_integration_test.go b/cmd/bd/mol_stale_proxied_integration_test.go new file mode 100644 index 000000000..a75e82515 --- /dev/null +++ b/cmd/bd/mol_stale_proxied_integration_test.go @@ -0,0 +1,129 @@ +//go:build cgo + +package main + +import ( + "encoding/json" + "testing" +) + +func TestProxiedServerMolStale(t *testing.T) { + requireSharedProxiedServer(t) + t.Parallel() + bd := buildEmbeddedBD(t) + + t.Run("complete_but_unclosed_appears_then_clears_on_close", func(t *testing.T) { + t.Parallel() + p := newSharedProxiedProject(t, bd, "mst") + root := bdProxiedCreate(t, bd, p.dir, "Stale root", "--type", "epic") + child := bdProxiedCreate(t, bd, p.dir, "Stale child", "--type", "task", "--parent", root.ID) + if out, err := bdProxiedRun(t, bd, p.dir, "close", child.ID); err != nil { + t.Fatalf("bd close child: %v\n%s", err, out) + } + + out, err := bdProxiedRun(t, bd, p.dir, "mol", "stale", "--json") + if err != nil { + t.Fatalf("bd mol stale --json: %v\n%s", err, out) + } + var got struct { + StaleMolecules []struct { + ID string `json:"id"` + } `json:"stale_molecules"` + } + if err := json.Unmarshal(out, &got); err != nil { + t.Fatalf("unmarshal: %v\n%s", err, out) + } + found := false + for _, m := range got.StaleMolecules { + if m.ID == root.ID { + found = true + } + } + if !found { + t.Fatalf("expected %s in stale molecules, got %+v", root.ID, got.StaleMolecules) + } + + if out, err := bdProxiedRun(t, bd, p.dir, "close", root.ID); err != nil { + t.Fatalf("bd close root: %v\n%s", err, out) + } + out2, err := bdProxiedRun(t, bd, p.dir, "mol", "stale", "--json") + if err != nil { + t.Fatalf("bd mol stale --json (after close): %v\n%s", err, out2) + } + var got2 struct { + StaleMolecules []struct { + ID string `json:"id"` + } `json:"stale_molecules"` + } + if err := json.Unmarshal(out2, &got2); err != nil { + t.Fatalf("unmarshal: %v\n%s", err, out2) + } + for _, m := range got2.StaleMolecules { + if m.ID == root.ID { + t.Errorf("root %s should no longer be stale after closing", root.ID) + } + } + }) + + t.Run("unassigned_filter", func(t *testing.T) { + t.Parallel() + p := newSharedProxiedProject(t, bd, "mstu") + assignedRoot := bdProxiedCreate(t, bd, p.dir, "Assigned stale root", "--type", "epic", "--assignee", "someone") + assignedChild := bdProxiedCreate(t, bd, p.dir, "Assigned stale child", "--type", "task", "--parent", assignedRoot.ID) + if out, err := bdProxiedRun(t, bd, p.dir, "close", assignedChild.ID); err != nil { + t.Fatalf("bd close: %v\n%s", err, out) + } + unassignedRoot := bdProxiedCreate(t, bd, p.dir, "Unassigned stale root", "--type", "epic") + unassignedChild := bdProxiedCreate(t, bd, p.dir, "Unassigned stale child", "--type", "task", "--parent", unassignedRoot.ID) + if out, err := bdProxiedRun(t, bd, p.dir, "close", unassignedChild.ID); err != nil { + t.Fatalf("bd close: %v\n%s", err, out) + } + + out, err := bdProxiedRun(t, bd, p.dir, "mol", "stale", "--unassigned", "--json") + if err != nil { + t.Fatalf("bd mol stale --unassigned --json: %v\n%s", err, out) + } + var got struct { + StaleMolecules []struct { + ID string `json:"id"` + } `json:"stale_molecules"` + } + if err := json.Unmarshal(out, &got); err != nil { + t.Fatalf("unmarshal: %v\n%s", err, out) + } + ids := map[string]bool{} + for _, m := range got.StaleMolecules { + ids[m.ID] = true + } + if ids[assignedRoot.ID] { + t.Errorf("assigned root %s should be excluded by --unassigned", assignedRoot.ID) + } + if !ids[unassignedRoot.ID] { + t.Errorf("unassigned root %s should be included by --unassigned", unassignedRoot.ID) + } + }) + + t.Run("all_flag_does_not_surface_childless_epics", func(t *testing.T) { + t.Parallel() + p := newSharedProxiedProject(t, bd, "msta") + lonely := bdProxiedCreate(t, bd, p.dir, "Childless open epic", "--type", "epic") + + out, err := bdProxiedRun(t, bd, p.dir, "mol", "stale", "--all", "--json") + if err != nil { + t.Fatalf("bd mol stale --all --json: %v\n%s", err, out) + } + var got struct { + StaleMolecules []struct { + ID string `json:"id"` + } `json:"stale_molecules"` + } + if err := json.Unmarshal(out, &got); err != nil { + t.Fatalf("unmarshal: %v\n%s", err, out) + } + for _, m := range got.StaleMolecules { + if m.ID == lonely.ID { + t.Errorf("childless epic %s unexpectedly present; GetEpicsEligibleForClosure may have been fixed to surface it — update this test", lonely.ID) + } + } + }) +} diff --git a/cmd/bd/pour_proxied_integration_test.go b/cmd/bd/pour_proxied_integration_test.go new file mode 100644 index 000000000..dbe1c2856 --- /dev/null +++ b/cmd/bd/pour_proxied_integration_test.go @@ -0,0 +1,109 @@ +//go:build cgo + +package main + +import ( + "encoding/json" + "strings" + "testing" +) + +func TestProxiedServerPour(t *testing.T) { + requireSharedProxiedServer(t) + t.Parallel() + bd := buildEmbeddedBD(t) + + t.Run("pours_db_proto_persistent", func(t *testing.T) { + t.Parallel() + p := newSharedProxiedProject(t, bd, "pou") + proto := bdProxiedCreate(t, bd, p.dir, "Feature proto", "--type", "epic", "--label", "template") + bdProxiedCreate(t, bd, p.dir, "Implement", "--type", "task", "--parent", proto.ID) + + out, err := bdProxiedRun(t, bd, p.dir, "mol", "pour", proto.ID, "--assignee", "worker1", "--json") + if err != nil { + t.Fatalf("bd mol pour --json: %v\n%s", err, out) + } + var got struct { + NewEpicID string `json:"new_epic_id"` + Created int `json:"created"` + Phase string `json:"phase"` + } + if err := json.Unmarshal(out, &got); err != nil { + t.Fatalf("unmarshal: %v\n%s", err, out) + } + if got.Created != 2 { + t.Errorf("created = %d, want 2", got.Created) + } + if got.Phase != "liquid" { + t.Errorf("phase = %s, want liquid", got.Phase) + } + + spawned := bdProxiedShow(t, bd, p.dir, got.NewEpicID) + if spawned.Ephemeral { + t.Error("poured mol should not be ephemeral") + } + if spawned.Assignee != "worker1" { + t.Errorf("assignee = %s, want worker1", spawned.Assignee) + } + }) + + t.Run("missing_required_variable_errors", func(t *testing.T) { + t.Parallel() + p := newSharedProxiedProject(t, bd, "pouv") + proto := bdProxiedCreate(t, bd, p.dir, "Proto with {{env}}", "--type", "epic", "--label", "template") + bdProxiedCreate(t, bd, p.dir, "Deploy to {{env}}", "--type", "task", "--parent", proto.ID) + + stdout, stderr, err := bdProxiedRunBuffers(t, bd, p.dir, "mol", "pour", proto.ID) + if err == nil { + t.Fatalf("expected missing-variable error, got stdout:%s stderr:%s", stdout, stderr) + } + combined := stdout + stderr + if !strings.Contains(combined, "env") { + t.Errorf("expected error to mention missing variable 'env', got: %s", combined) + } + }) + + t.Run("attach_bonds_second_proto", func(t *testing.T) { + t.Parallel() + p := newSharedProxiedProject(t, bd, "poua") + main := bdProxiedCreate(t, bd, p.dir, "Main proto", "--type", "epic", "--label", "template") + bdProxiedCreate(t, bd, p.dir, "Main step", "--type", "task", "--parent", main.ID) + attach := bdProxiedCreate(t, bd, p.dir, "Attach proto", "--type", "epic", "--label", "template") + bdProxiedCreate(t, bd, p.dir, "Attach step", "--type", "task", "--parent", attach.ID) + + out, err := bdProxiedRun(t, bd, p.dir, "mol", "pour", main.ID, "--attach", attach.ID, "--attach-type", "parallel", "--json") + if err != nil { + t.Fatalf("bd mol pour --attach --json: %v\n%s", err, out) + } + var got struct { + NewEpicID string `json:"new_epic_id"` + Attached int `json:"attached"` + } + if err := json.Unmarshal(out, &got); err != nil { + t.Fatalf("unmarshal: %v\n%s", err, out) + } + if got.Attached != 2 { + t.Errorf("attached = %d, want 2 (attach root + attach step)", got.Attached) + } + }) + + t.Run("dry_run_creates_nothing", func(t *testing.T) { + t.Parallel() + p := newSharedProxiedProject(t, bd, "poud") + proto := bdProxiedCreate(t, bd, p.dir, "Dry proto", "--type", "epic", "--label", "template") + bdProxiedCreate(t, bd, p.dir, "Dry step", "--type", "task", "--parent", proto.ID) + + before := bdProxiedListJSON(t, bd, p, "--all") + stdout, _, err := bdProxiedRunBuffers(t, bd, p.dir, "mol", "pour", proto.ID, "--dry-run") + if err != nil { + t.Fatalf("bd mol pour --dry-run: %v\n%s", err, stdout) + } + if !strings.Contains(stdout, "Dry run") { + t.Errorf("expected dry-run preview, got: %s", stdout) + } + after := bdProxiedListJSON(t, bd, p, "--all") + if len(after) != len(before) { + t.Errorf("expected no new issues from --dry-run: before=%d after=%d", len(before), len(after)) + } + }) +} diff --git a/cmd/bd/wisp_proxied_integration_test.go b/cmd/bd/wisp_proxied_integration_test.go new file mode 100644 index 000000000..fe255f9d5 --- /dev/null +++ b/cmd/bd/wisp_proxied_integration_test.go @@ -0,0 +1,373 @@ +//go:build cgo + +package main + +import ( + "encoding/json" + "strings" + "testing" + "time" +) + +func TestProxiedServerWispCreate(t *testing.T) { + requireSharedProxiedServer(t) + t.Parallel() + bd := buildEmbeddedBD(t) + + t.Run("bare_wisp_and_wisp_create_materialize_same_dag", func(t *testing.T) { + t.Parallel() + p := newSharedProxiedProject(t, bd, "wcd") + proto := bdProxiedCreate(t, bd, p.dir, "DAG proto", "--type", "epic", "--label", "template") + bdProxiedCreate(t, bd, p.dir, "DAG step one", "--type", "task", "--parent", proto.ID) + bdProxiedCreate(t, bd, p.dir, "DAG step two", "--type", "task", "--parent", proto.ID) + + out, err := bdProxiedRun(t, bd, p.dir, "mol", "wisp", proto.ID, "--json") + if err != nil { + t.Fatalf("bd mol wisp --json: %v\n%s", err, out) + } + var got struct { + Created int `json:"created"` + Phase string `json:"phase"` + } + if err := json.Unmarshal(out, &got); err != nil { + t.Fatalf("unmarshal: %v\n%s", err, out) + } + if got.Created != 3 { + t.Errorf("created = %d, want 3 (root + 2 steps)", got.Created) + } + if got.Phase != "vapor" { + t.Errorf("phase = %s, want vapor", got.Phase) + } + + out2, err := bdProxiedRun(t, bd, p.dir, "mol", "wisp", "create", proto.ID, "--json") + if err != nil { + t.Fatalf("bd mol wisp create --json: %v\n%s", err, out2) + } + var got2 struct { + Created int `json:"created"` + } + if err := json.Unmarshal(out2, &got2); err != nil { + t.Fatalf("unmarshal: %v\n%s", err, out2) + } + if got2.Created != got.Created { + t.Errorf("bd mol wisp create --json created %d, want %d (parity with bare wisp)", got2.Created, got.Created) + } + }) + + t.Run("root_only_skips_children", func(t *testing.T) { + t.Parallel() + p := newSharedProxiedProject(t, bd, "wcr") + proto := bdProxiedCreate(t, bd, p.dir, "Root-only proto", "--type", "epic", "--label", "template") + bdProxiedCreate(t, bd, p.dir, "Skipped step", "--type", "task", "--parent", proto.ID) + + out, err := bdProxiedRun(t, bd, p.dir, "mol", "wisp", "create", proto.ID, "--root-only", "--json") + if err != nil { + t.Fatalf("bd mol wisp create --root-only --json: %v\n%s", err, out) + } + var got struct { + Created int `json:"created"` + } + if err := json.Unmarshal(out, &got); err != nil { + t.Fatalf("unmarshal: %v\n%s", err, out) + } + if got.Created != 1 { + t.Errorf("created = %d, want 1 (root only)", got.Created) + } + }) + + t.Run("dry_run_creates_nothing", func(t *testing.T) { + t.Parallel() + p := newSharedProxiedProject(t, bd, "wcdr") + proto := bdProxiedCreate(t, bd, p.dir, "Dry wisp proto", "--type", "epic", "--label", "template") + bdProxiedCreate(t, bd, p.dir, "Dry wisp step", "--type", "task", "--parent", proto.ID) + + stdout, _, err := bdProxiedRunBuffers(t, bd, p.dir, "mol", "wisp", "create", proto.ID, "--dry-run") + if err != nil { + t.Fatalf("bd mol wisp create --dry-run: %v\n%s", err, stdout) + } + if !strings.Contains(stdout, "Dry run") { + t.Errorf("expected dry-run message, got: %s", stdout) + } + + db := openProxiedDB(t, p) + var count int + if err := db.QueryRow("SELECT COUNT(*) FROM wisps").Scan(&count); err != nil { + t.Fatalf("count wisps: %v", err) + } + if count != 0 { + t.Errorf("expected no wisps created by --dry-run, found %d", count) + } + }) +} + +func TestProxiedServerWispList(t *testing.T) { + requireSharedProxiedServer(t) + t.Parallel() + bd := buildEmbeddedBD(t) + + t.Run("default_excludes_closed_all_includes", func(t *testing.T) { + t.Parallel() + p := newSharedProxiedProject(t, bd, "wl") + openWisp := bdProxiedCreate(t, bd, p.dir, "Open wisp", "--ephemeral") + closedWisp := bdProxiedCreate(t, bd, p.dir, "Closed wisp", "--ephemeral") + if out, err := bdProxiedRun(t, bd, p.dir, "close", closedWisp.ID); err != nil { + t.Fatalf("bd close: %v\n%s", err, out) + } + + out, err := bdProxiedRun(t, bd, p.dir, "mol", "wisp", "list", "--json") + if err != nil { + t.Fatalf("bd mol wisp list --json: %v\n%s", err, out) + } + var got struct { + Wisps []struct { + ID string `json:"id"` + } `json:"wisps"` + } + if err := json.Unmarshal(out, &got); err != nil { + t.Fatalf("unmarshal: %v\n%s", err, out) + } + ids := map[string]bool{} + for _, w := range got.Wisps { + ids[w.ID] = true + } + if !ids[openWisp.ID] { + t.Errorf("expected open wisp %s in default list", openWisp.ID) + } + if ids[closedWisp.ID] { + t.Errorf("closed wisp %s should be excluded by default", closedWisp.ID) + } + + outAll, err := bdProxiedRun(t, bd, p.dir, "mol", "wisp", "list", "--all", "--json") + if err != nil { + t.Fatalf("bd mol wisp list --all --json: %v\n%s", err, outAll) + } + var gotAll struct { + Wisps []struct { + ID string `json:"id"` + } `json:"wisps"` + } + if err := json.Unmarshal(outAll, &gotAll); err != nil { + t.Fatalf("unmarshal: %v\n%s", err, outAll) + } + idsAll := map[string]bool{} + for _, w := range gotAll.Wisps { + idsAll[w.ID] = true + } + if !idsAll[closedWisp.ID] { + t.Errorf("expected closed wisp %s in --all list", closedWisp.ID) + } + }) + + t.Run("type_filter", func(t *testing.T) { + t.Parallel() + p := newSharedProxiedProject(t, bd, "wlt") + taskWisp := bdProxiedCreate(t, bd, p.dir, "Task wisp", "--ephemeral", "--type", "task") + bdProxiedCreate(t, bd, p.dir, "Chore wisp", "--ephemeral", "--type", "chore") + + out, err := bdProxiedRun(t, bd, p.dir, "mol", "wisp", "list", "--type", "task", "--json") + if err != nil { + t.Fatalf("bd mol wisp list --type task --json: %v\n%s", err, out) + } + var got struct { + Wisps []struct { + ID string `json:"id"` + Type string `json:"type"` + } `json:"wisps"` + } + if err := json.Unmarshal(out, &got); err != nil { + t.Fatalf("unmarshal: %v\n%s", err, out) + } + if len(got.Wisps) != 1 || got.Wisps[0].ID != taskWisp.ID { + t.Fatalf("expected only task wisp %s, got %+v", taskWisp.ID, got.Wisps) + } + }) + + t.Run("old_wisp_flagged", func(t *testing.T) { + t.Parallel() + p := newSharedProxiedProject(t, bd, "wlo") + oldWisp := bdProxiedCreate(t, bd, p.dir, "Old wisp", "--ephemeral") + + db := openProxiedDB(t, p) + if _, err := db.Exec("UPDATE wisps SET updated_at = ? WHERE id = ?", + time.Now().UTC().Add(-48*time.Hour), oldWisp.ID); err != nil { + t.Fatalf("backdate wisp: %v", err) + } + + out, err := bdProxiedRun(t, bd, p.dir, "mol", "wisp", "list", "--json") + if err != nil { + t.Fatalf("bd mol wisp list --json: %v\n%s", err, out) + } + var got struct { + Wisps []struct { + ID string `json:"id"` + Old bool `json:"old"` + } `json:"wisps"` + OldCount int `json:"old_count"` + } + if err := json.Unmarshal(out, &got); err != nil { + t.Fatalf("unmarshal: %v\n%s", err, out) + } + if got.OldCount != 1 { + t.Errorf("old_count = %d, want 1", got.OldCount) + } + found := false + for _, w := range got.Wisps { + if w.ID == oldWisp.ID && w.Old { + found = true + } + } + if !found { + t.Errorf("expected wisp %s flagged old, got %+v", oldWisp.ID, got.Wisps) + } + }) +} + +func TestProxiedServerWispGC(t *testing.T) { + requireSharedProxiedServer(t) + t.Parallel() + bd := buildEmbeddedBD(t) + + t.Run("age_threshold_cleans_abandoned_wisps", func(t *testing.T) { + t.Parallel() + p := newSharedProxiedProject(t, bd, "wg") + fresh := bdProxiedCreate(t, bd, p.dir, "Fresh wisp", "--ephemeral") + abandoned := bdProxiedCreate(t, bd, p.dir, "Abandoned wisp", "--ephemeral") + + db := openProxiedDB(t, p) + if _, err := db.Exec("UPDATE wisps SET updated_at = ? WHERE id = ?", + time.Now().UTC().Add(-2*time.Hour), abandoned.ID); err != nil { + t.Fatalf("backdate wisp: %v", err) + } + + out, err := bdProxiedRun(t, bd, p.dir, "mol", "wisp", "gc", "--age", "1h", "--json") + if err != nil { + t.Fatalf("bd mol wisp gc --age 1h --json: %v\n%s", err, out) + } + var got struct { + CleanedIDs []string `json:"cleaned_ids"` + CleanedCount int `json:"cleaned_count"` + } + if err := json.Unmarshal(out, &got); err != nil { + t.Fatalf("unmarshal: %v\n%s", err, out) + } + if got.CleanedCount != 1 || got.CleanedIDs[0] != abandoned.ID { + t.Fatalf("expected only %s cleaned, got %+v", abandoned.ID, got) + } + + var count int + if err := db.QueryRow("SELECT COUNT(*) FROM wisps WHERE id = ?", fresh.ID).Scan(&count); err != nil { + t.Fatalf("query fresh wisp: %v", err) + } + if count != 1 { + t.Errorf("expected fresh wisp %s to survive gc", fresh.ID) + } + }) + + t.Run("cascade_sweeps_dependent_step_wisps", func(t *testing.T) { + t.Parallel() + p := newSharedProxiedProject(t, bd, "wgc") + parentWisp := bdProxiedCreate(t, bd, p.dir, "Abandoned parent wisp", "--ephemeral") + childWisp := bdProxiedCreate(t, bd, p.dir, "Fresh child wisp", "--ephemeral") + + db := openProxiedDB(t, p) + if _, err := db.Exec( + "INSERT INTO wisp_dependencies (id, issue_id, depends_on_wisp_id, type, created_at, created_by) VALUES (UUID(), ?, ?, ?, NOW(), 'test')", + childWisp.ID, parentWisp.ID, "blocks"); err != nil { + t.Fatalf("plant child->parent wisp edge: %v", err) + } + if _, err := db.Exec("UPDATE wisps SET updated_at = ? WHERE id = ?", + time.Now().UTC().Add(-2*time.Hour), parentWisp.ID); err != nil { + t.Fatalf("backdate parent wisp: %v", err) + } + + out, err := bdProxiedRun(t, bd, p.dir, "mol", "wisp", "gc", "--age", "1h", "--json") + if err != nil { + t.Fatalf("bd mol wisp gc --age 1h --json: %v\n%s", err, out) + } + var got struct { + CleanedIDs []string `json:"cleaned_ids"` + } + if err := json.Unmarshal(out, &got); err != nil { + t.Fatalf("unmarshal: %v\n%s", err, out) + } + cleaned := map[string]bool{} + for _, id := range got.CleanedIDs { + cleaned[id] = true + } + if !cleaned[parentWisp.ID] { + t.Errorf("expected abandoned parent %s cleaned", parentWisp.ID) + } + if !cleaned[childWisp.ID] { + t.Errorf("expected dependent child %s cascaded even though it's not itself old", childWisp.ID) + } + }) + + t.Run("exclude_type_protects_wisps", func(t *testing.T) { + t.Parallel() + p := newSharedProxiedProject(t, bd, "wge") + if out, err := bdProxiedRun(t, bd, p.dir, "config", "set", "types.custom", "agent"); err != nil { + t.Fatalf("bd config set types.custom agent: %v\n%s", err, out) + } + protected := bdProxiedCreate(t, bd, p.dir, "Protected agent wisp", "--ephemeral", "--type", "agent") + unprotected := bdProxiedCreate(t, bd, p.dir, "Unprotected wisp", "--ephemeral") + + db := openProxiedDB(t, p) + old := time.Now().UTC().Add(-2 * time.Hour) + if _, err := db.Exec("UPDATE wisps SET updated_at = ? WHERE id IN (?, ?)", old, protected.ID, unprotected.ID); err != nil { + t.Fatalf("backdate wisps: %v", err) + } + + out, err := bdProxiedRun(t, bd, p.dir, "mol", "wisp", "gc", "--age", "1h", "--exclude-type", "agent", "--json") + if err != nil { + t.Fatalf("bd mol wisp gc --exclude-type agent --json: %v\n%s", err, out) + } + var got struct { + CleanedIDs []string `json:"cleaned_ids"` + } + if err := json.Unmarshal(out, &got); err != nil { + t.Fatalf("unmarshal: %v\n%s", err, out) + } + cleaned := map[string]bool{} + for _, id := range got.CleanedIDs { + cleaned[id] = true + } + if cleaned[protected.ID] { + t.Errorf("expected agent-type wisp %s to be protected", protected.ID) + } + if !cleaned[unprotected.ID] { + t.Errorf("expected unprotected wisp %s to be cleaned", unprotected.ID) + } + }) + + t.Run("closed_force_purges_regardless_of_age", func(t *testing.T) { + t.Parallel() + p := newSharedProxiedProject(t, bd, "wgcl") + closedWisp := bdProxiedCreate(t, bd, p.dir, "Just-closed wisp", "--ephemeral") + if out, err := bdProxiedRun(t, bd, p.dir, "close", closedWisp.ID); err != nil { + t.Fatalf("bd close: %v\n%s", err, out) + } + + out, err := bdProxiedRun(t, bd, p.dir, "mol", "wisp", "gc", "--closed", "--force", "--json") + if err != nil { + t.Fatalf("bd mol wisp gc --closed --force --json: %v\n%s", err, out) + } + var got struct { + DeletedCount int `json:"deleted_count"` + } + if err := json.Unmarshal(out, &got); err != nil { + t.Fatalf("unmarshal: %v\n%s", err, out) + } + if got.DeletedCount != 1 { + t.Errorf("deleted_count = %d, want 1", got.DeletedCount) + } + + db := openProxiedDB(t, p) + var count int + if err := db.QueryRow("SELECT COUNT(*) FROM wisps WHERE id = ?", closedWisp.ID).Scan(&count); err != nil { + t.Fatalf("query wisps: %v", err) + } + if count != 0 { + t.Errorf("expected closed wisp %s purged", closedWisp.ID) + } + }) +} diff --git a/cmd/bd/wisp_proxied_server.go b/cmd/bd/wisp_proxied_server.go index 433b29d17..f18111f54 100644 --- a/cmd/bd/wisp_proxied_server.go +++ b/cmd/bd/wisp_proxied_server.go @@ -202,8 +202,8 @@ func runWispPurgeClosedProxiedServer(ctx context.Context, dryRun, force bool, ex Limit: 5000, } closedIssues, err := r.SearchIssues(ctx, "", filter) - uw.Close(ctx) if err != nil { + uw.Close(ctx) return HandleError("listing closed wisps: %v", err) } @@ -222,6 +222,7 @@ func runWispPurgeClosedProxiedServer(ctx context.Context, dryRun, force bool, ex filtered = append(filtered, issue) } closedIssues = filtered + uw.Close(ctx) if pinnedCount > 0 && !jsonOutput { fmt.Printf("Skipping %d pinned issue(s) (protected from cleanup)\n", pinnedCount)