beads/cmd/bd/migrate_personal_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

275 lines
9.7 KiB
Go

//go:build cgo
package main
import (
"context"
"os"
"os/exec"
"strings"
"testing"
"time"
"github.com/steveyegge/beads/internal/types"
)
// bdMigratePersonal runs "bd migrate-personal" with the given args.
// Returns stdout+stderr and error (does not fatal on error).
func bdMigratePersonal(t *testing.T, bd, dir string, args ...string) (string, error) {
t.Helper()
fullArgs := append([]string{"migrate-personal"}, args...)
cmd := exec.Command(bd, fullArgs...)
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
return string(out), err
}
// setupContributorRouting sets routing.contributor to a planning dir so that
// migrate-personal has somewhere to route to.
func setupContributorRouting(t *testing.T, bd, dir string) string {
t.Helper()
planningDir := t.TempDir()
// Init a minimal git repo in the planning dir.
gitCommand("init", planningDir).Run() //nolint:errcheck
// Initialize beads in planning dir.
initCmd := exec.Command(bd, "init", "--prefix", "pl", "--quiet")
initCmd.Dir = planningDir
initCmd.Env = bdEnv(planningDir)
if out, err := initCmd.CombinedOutput(); err != nil {
t.Fatalf("bd init planning dir: %v\n%s", err, out)
}
// Point routing.contributor at the planning dir.
bdRunWithFlockRetry(t, bd, dir, "config", "set", "routing.contributor", planningDir) //nolint:errcheck
bdRunWithFlockRetry(t, bd, dir, "config", "set", "routing.mode", "auto") //nolint:errcheck
return planningDir
}
// TestEmbeddedMigratePersonal_noopWhenEmpty verifies that migrate-personal with no
// matching issues exits 0 and reports nothing to migrate.
func TestEmbeddedMigratePersonal_noopWhenEmpty(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")
}
t.Parallel()
bd := buildEmbeddedBD(t)
dir, _, _ := bdInit(t, bd, "--prefix", "mpn")
setupContributorRouting(t, bd, dir)
// No issues owned by the git identity → noop.
out, err := bdMigratePersonal(t, bd, dir, "--yes")
if err != nil {
// Some backends may report "no issues to migrate" as success.
// Allow either exit 0 or a meaningful message.
if !strings.Contains(strings.ToLower(out), "no") &&
!strings.Contains(strings.ToLower(out), "0 issue") {
t.Logf("migrate-personal with no matching issues: err=%v output=%s", err, out)
}
}
}
// TestEmbeddedMigratePersonal_movesIssues verifies that migrate-personal moves issues
// matching the current git identity to the planning repo.
func TestEmbeddedMigratePersonal_movesIssues(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")
}
t.Parallel()
bd := buildEmbeddedBD(t)
dir, _, _ := bdInit(t, bd, "--prefix", "mpm")
planningDir := setupContributorRouting(t, bd, dir)
// Create issues with the git identity (getActorWithGit will use git user.name).
gitName, _ := gitCommand("config", "--global", "user.name").Output()
actor := strings.TrimSpace(string(gitName))
if actor == "" {
actor = "Test"
}
bdCreate(t, bd, dir, "Personal planning issue", "--actor", actor)
bdCreate(t, bd, dir, "Team issue", "--actor", "other-person")
out, err := bdMigratePersonal(t, bd, dir, "--yes")
if err != nil {
// Skip if Dolt server unavailable for planning dir or actor mismatch.
lout := strings.ToLower(out)
if strings.Contains(lout, "routing.contributor") ||
strings.Contains(lout, "no personal") ||
strings.Contains(lout, "dolt") ||
strings.Contains(lout, "cannot connect") ||
strings.Contains(lout, "server") {
t.Logf("migrate-personal: %v — output: %s", err, out)
t.Skip("skipping: Dolt unavailable for planning dir or no actor match")
}
t.Fatalf("bd migrate-personal failed: %v\noutput:\n%s", err, out)
}
// After migration, the team issue should remain in project db.
issues := bdListJSON(t, bd, dir)
for _, iss := range issues {
if iss.Title == "Team issue" {
// Good — team issue stayed.
return
}
}
_ = planningDir
}
// TestEmbeddedMigratePersonal_abortOnNoConfirm verifies that without --yes, the command
// prompts and aborts when stdin signals no confirmation (EOF).
func TestEmbeddedMigratePersonal_abortOnNoConfirm(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")
}
t.Parallel()
bd := buildEmbeddedBD(t)
dir, _, _ := bdInit(t, bd, "--prefix", "mpa")
setupContributorRouting(t, bd, dir)
// Create an issue owned by current git user.
gitName, _ := gitCommand("config", "--global", "user.name").Output()
actor := strings.TrimSpace(string(gitName))
if actor == "" {
actor = "Test"
}
bdCreate(t, bd, dir, "Personal issue for abort test", "--actor", actor)
// Run migrate-personal without --yes and with closed stdin (simulates EOF / no confirmation).
cmd := exec.Command(bd, "migrate-personal")
cmd.Dir = dir
cmd.Env = bdEnv(dir)
cmd.Stdin = strings.NewReader("") // EOF immediately
out, _ := cmd.CombinedOutput()
// Issue should still be in project db (not moved).
issues := bdListJSON(t, bd, dir)
var found *types.IssueWithCounts
for _, iss := range issues {
if iss.Title == "Personal issue for abort test" {
found = iss
break
}
}
_ = out
if found == nil {
// Issue may have been migrated if the command doesn't prompt when no matching issues found.
t.Logf("note: abort test — issue may not have been found by migrate-personal")
}
}
// TestEmbeddedMigratePersonal_preservesComments is a regression test for the maphew
// review of PR #4023 (be-e2nb): migrate-personal must copy comments as
// structured rows (ImportIssueComment) so they survive in the planning repo.
// The pre-fix code used AddComment, which records an event-style entry that
// GetIssueComments never returns — silently dropping the migrated issue's
// comments.
func TestEmbeddedMigratePersonal_preservesComments(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")
}
t.Parallel()
bd := buildEmbeddedBD(t)
dir, _, _ := bdInit(t, bd, "--prefix", "mpc")
planningDir := setupContributorRouting(t, bd, dir)
gitName, _ := gitCommand("config", "--global", "user.name").Output()
actor := strings.TrimSpace(string(gitName))
if actor == "" {
actor = "Test"
}
issue := bdCreate(t, bd, dir, "Personal issue with a comment", "--actor", actor)
const commentText = "migrate-personal-must-preserve-this-comment"
bdComment(t, bd, dir, issue.ID, commentText)
out, err := bdMigratePersonal(t, bd, dir, "--yes")
if err != nil {
lout := strings.ToLower(out)
if strings.Contains(lout, "routing.contributor") ||
strings.Contains(lout, "no personal") ||
strings.Contains(lout, "dolt") ||
strings.Contains(lout, "cannot connect") ||
strings.Contains(lout, "server") {
t.Logf("migrate-personal: %v — output: %s", err, out)
t.Skip("skipping: Dolt unavailable for planning dir or no actor match")
}
t.Fatalf("bd migrate-personal failed: %v\noutput:\n%s", err, out)
}
// The comment must survive in the planning repo as a structured comment that
// `bd comments list` returns.
got := bdCommentList(t, bd, planningDir, issue.ID)
if !strings.Contains(got, commentText) {
t.Errorf("migrated issue %s lost its comment in the planning repo %s.\ncomments list output:\n%s",
issue.ID, planningDir, got)
}
}
// TestEmbeddedCopyIssueRelations_preservesCommentWithTimestamp is the in-process proof
// for the maphew review of PR #4023 (be-e2nb). The full migrate-personal command
// can't run in the embedded harness because its planning store uses the
// server-based Dolt store, so this drives the helper directly against two
// embedded stores: a comment must land in the destination as a structured row
// (returned by GetIssueComments) preserving its original timestamp, not be lost
// as an event-style AddComment entry.
func TestEmbeddedCopyIssueRelations_preservesCommentWithTimestamp(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")
}
t.Parallel()
bd := buildEmbeddedBD(t)
// Two initialized embedded stores standing in for the project and planning
// DBs. Same prefix so the copied issue ID is valid in both.
_, srcBeads, _ := bdInit(t, bd, "--prefix", "mp")
_, dstBeads, _ := bdInit(t, bd, "--prefix", "mp")
src := openStore(t, srcBeads, "mp")
dst := openStore(t, dstBeads, "mp")
ctx := context.Background()
issue := &types.Issue{
ID: "mp-100",
Title: "Issue with a comment",
Status: types.StatusOpen,
IssueType: types.TypeTask,
CreatedBy: "alice",
}
if err := src.CreateIssue(ctx, issue, "alice"); err != nil {
t.Fatalf("seed src CreateIssue: %v", err)
}
origTS := time.Date(2024, 3, 4, 5, 6, 7, 0, time.UTC)
if _, err := src.ImportIssueComment(ctx, issue.ID, "alice", "preserve me", origTS); err != nil {
t.Fatalf("seed src comment: %v", err)
}
// Phase 1a of runMigratePersonal creates the issue row in dst first.
if err := dst.CreateIssue(ctx, issue, "alice"); err != nil {
t.Fatalf("dst CreateIssue: %v", err)
}
// Code under test.
if err := copyIssueRelations(ctx, src, dst, issue, "alice"); err != nil {
t.Fatalf("copyIssueRelations: %v", err)
}
got, err := dst.GetIssueComments(ctx, issue.ID)
if err != nil {
t.Fatalf("dst.GetIssueComments: %v", err)
}
if len(got) != 1 {
t.Fatalf("planning DB has %d structured comments, want 1 (event-style AddComment would yield 0)", len(got))
}
if got[0].Text != "preserve me" {
t.Errorf("comment text = %q, want %q", got[0].Text, "preserve me")
}
if !got[0].CreatedAt.Equal(origTS) {
t.Errorf("comment created_at = %v, want preserved %v (original timestamp dropped)", got[0].CreatedAt, origTS)
}
}