beads/cmd/bd/mol_embedded_test.go
Julian Knutsen 8988340994
ci(embedded): run every BEADS_TEST_EMBEDDED_DOLT-gated cmd/bd test in the embedded lane (#7708)
* test(cmd/bd): repair the embedded-gated tests that rotted while no lane ran them

These tests skip unless BEADS_TEST_EMBEDDED_DOLT=1 but no CI lane selected
them, so they rotted unseen. Running them shows:

- TestEventsTailFollowReportsTruncationOnTheStream raced on a plain
  bytes.Buffer that os/exec's copy goroutine writes while the test polls it.
  Use the package's syncBuffer.
- TestCorruptMetadataDiagnosticsRunAndDataFailsLoud still asserted that
  doctor and init run on a corrupt metadata.json. #4857 deliberately made
  every workspace command refuse instead (pinned for doctor by
  TestDoctorRejectsCorruptMetadataBeforeSharedServerChecks). Rewrite it to
  the shipped contract as TestEmbeddedCorruptMetadataFailsLoudUntilRestored:
  version warns, doctor/list/init refuse naming the file without touching
  it, and restoring the file brings the data back.
- dolt_remote_origin_guard_embedded_test.go was tagged
  `embeddeddolt && cgo`, a tag no lane sets, so it never compiled in CI
  (gazelle left it out of bd_test srcs). Retag it `cgo`, as #4153 did for
  its dolt_local_only sibling, and add it to srcs. Drop its four bd doctor
  git-origin tests: bd doctor runs no checks on an embedded workspace
  (GH#3794), so two could never pass and two passed vacuously on empty
  output. Rename the surviving tests into the TestEmbedded* prefix.

Agent-Signature: claude-code-claude-opus-5-5-unknown-reasoning on behalf of Julian Knutsen
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* ci(embedded): run every BEADS_TEST_EMBEDDED_DOLT-gated cmd/bd test in the embedded lane

The embedded-Dolt cmd lane selected tests with
grep '^func TestEmbedded' cmd/bd/*_embedded_test.go, and only its two
Bazel targets set BEADS_TEST_EMBEDDED_DOLT. A test that skips without the
variable but missed the name prefix or the file suffix skipped in every
lane and reported green: 92 of them, including #7203's --if-revision CLI
coverage (TestIfRevisionMatchAndMismatch, TestReassignFenceCLI, ...).

- embedded-test-shard.sh and gen_embedded_cmd_shard_manifest.py select
  TestEmbedded* by name across cmd/bd/*_test.go, as proxied-test-shard.sh
  does, leaving authors one rule.
- Rename the gated tests into the TestEmbedded* prefix
  (TestDoctorEmbedded<X> -> TestEmbeddedDoctor<X>) and update every
  reference.
- Place the new names with gen_embedded_cmd_shard_manifest.py 100
  --weights=duration --write (incremental).
- Add TestEmbeddedGatedTestsRunInTheEmbeddedLane: it walks cmd/bd's tests
  for any that reach os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" and fails
  if a shard of the lane never lists one. The proxied guard's helpers now
  take the variable and the script; a fixture test pins the walk.

Agent-Signature: claude-code-claude-opus-5-5-unknown-reasoning on behalf of Julian Knutsen
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-19 22:02:22 +00:00

175 lines
5.3 KiB
Go

//go:build cgo
package main
import (
"encoding/json"
"os"
"testing"
"github.com/steveyegge/beads/internal/formula"
"github.com/steveyegge/beads/internal/storage/embeddeddolt"
"github.com/steveyegge/beads/internal/types"
"github.com/steveyegge/beads/internal/validation"
)
// TestEmbeddedSpawnMolecule_PreservesStepLabels verifies that a cooked formula with
// per-step labels results in spawned issues whose labels are persisted to the
// database. Regression for labels being silently dropped by cloneSubgraph in
// the same shape as the metadata bug fixed by gastownhall/beads#3341.
func TestEmbeddedSpawnMolecule_PreservesStepLabels(t *testing.T) {
if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" {
t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt integration tests")
}
ctx := t.Context()
s, err := embeddeddolt.Open(ctx, t.TempDir(), "beads", "main")
if err != nil {
t.Fatalf("embeddeddolt.Open failed: %v", err)
}
defer s.Close()
if err := s.SetConfig(ctx, "issue_prefix", "test"); err != nil {
t.Fatalf("SetConfig failed: %v", err)
}
f := &formula.Formula{
Formula: "label-test",
Version: 1,
Type: formula.TypeWorkflow,
Steps: []*formula.Step{
{
ID: "work",
Title: "Do the work",
Labels: []string{"worker", "phase:build"},
},
},
}
subgraph, err := cookFormulaToSubgraph(f, "label-test")
if err != nil {
t.Fatalf("cookFormulaToSubgraph failed: %v", err)
}
result, err := spawnMolecule(ctx, s, subgraph, nil, "", "test", false, types.IDPrefixMol)
if err != nil {
t.Fatalf("spawnMolecule failed: %v", err)
}
newWorkID, ok := result.IDMapping["label-test.work"]
if !ok {
t.Fatalf("result.IDMapping missing entry for label-test.work; got %v", result.IDMapping)
}
labels, err := s.GetLabels(ctx, newWorkID)
if err != nil {
t.Fatalf("GetLabels(%s) failed: %v", newWorkID, err)
}
got := make(map[string]bool, len(labels))
for _, l := range labels {
got[l] = true
}
for _, want := range []string{"worker", "phase:build"} {
if !got[want] {
t.Errorf("spawned issue %s missing label %q; got %v", newWorkID, want, labels)
}
}
}
// TestEmbeddedSpawnMolecule_SubstitutesTemplatedStepFields is the cook -> pour
// regression test for GH#5110 (labels, metadata) and GH#5754 (assignee).
// TestEmbeddedSpawnMolecule_PreservesStepLabels above fixed labels being dropped
// entirely, but its labels carry no {{vars}}, so the missing substitution went
// unnoticed: the labels ARE carried through - just literal, which silently
// stops matching every label query downstream.
//
// The assignee case is the severe one. An unsubstituted assignee makes the
// spawned bead unclosable by the actor that poured it, so the molecule can
// never advance - which is what the AssigneeMatches assertion pins, not just
// the string value.
func TestEmbeddedSpawnMolecule_SubstitutesTemplatedStepFields(t *testing.T) {
if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" {
t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt integration tests")
}
ctx := t.Context()
s, err := embeddeddolt.Open(ctx, t.TempDir(), "beads", "main")
if err != nil {
t.Fatalf("embeddeddolt.Open failed: %v", err)
}
defer s.Close()
if err := s.SetConfig(ctx, "issue_prefix", "test"); err != nil {
t.Fatalf("SetConfig failed: %v", err)
}
const actor = "design-agent"
f := &formula.Formula{
Formula: "subst-test",
Version: 1,
Type: formula.TypeWorkflow,
Steps: []*formula.Step{
{
ID: "qa",
Title: "API-contract QA: {{widget_id}}",
Assignee: "{{agent}}",
Labels: []string{"qa-run", "widget:{{widget_id}}", "domain:{{domain}}"},
Metadata: map[string]interface{}{"ado_id": "{{ado_id}}"},
},
},
}
subgraph, err := cookFormulaToSubgraph(f, "subst-test")
if err != nil {
t.Fatalf("cookFormulaToSubgraph failed: %v", err)
}
vars := map[string]string{
"widget_id": "my_widget",
"domain": "payroll",
"agent": actor,
"ado_id": "731878",
}
result, err := spawnMolecule(ctx, s, subgraph, vars, "", actor, false, types.IDPrefixMol)
if err != nil {
t.Fatalf("spawnMolecule failed: %v", err)
}
newQAID, ok := result.IDMapping["subst-test.qa"]
if !ok {
t.Fatalf("result.IDMapping missing entry for subst-test.qa; got %v", result.IDMapping)
}
issue, err := s.GetIssue(ctx, newQAID)
if err != nil {
t.Fatalf("GetIssue(%s) failed: %v", newQAID, err)
}
labels, err := s.GetLabels(ctx, newQAID)
if err != nil {
t.Fatalf("GetLabels(%s) failed: %v", newQAID, err)
}
gotLabels := make(map[string]bool, len(labels))
for _, l := range labels {
gotLabels[l] = true
}
for _, want := range []string{"qa-run", "widget:my_widget", "domain:payroll"} {
if !gotLabels[want] {
t.Errorf("spawned issue %s missing substituted label %q; got %v", newQAID, want, labels)
}
}
var metadata struct {
ADOID string `json:"ado_id"`
}
if err := json.Unmarshal(issue.Metadata, &metadata); err != nil {
t.Fatalf("spawned issue metadata = %s, not valid JSON: %v", issue.Metadata, err)
}
if metadata.ADOID != "731878" {
t.Errorf("spawned issue metadata.ado_id = %q, want %q", metadata.ADOID, "731878")
}
if issue.Assignee != actor {
t.Errorf("spawned issue assignee = %q, want %q", issue.Assignee, actor)
}
if err := validation.AssigneeMatches(actor, false)(newQAID, issue); err != nil {
t.Errorf("spawned issue is not closable by the pouring actor %q: %v", actor, err)
}
}