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

90 lines
3 KiB
Go

//go:build cgo
package main
import (
"database/sql"
"errors"
"os"
"os/exec"
"strings"
"testing"
"github.com/steveyegge/beads/internal/storage/schema"
)
// TestEmbeddedMigrateSchema_IdentityMismatchRefusesBeforeApplying reproduces be-0gfcs:
// bd migrate schema's workspace-identity guard (validateWorkspaceIdentity)
// runs AFTER the shared pre-run has already opened the store and let the
// smart gate auto-apply a pending schema migration as a side effect. The
// refusal itself is correct -- exit 1, the mismatch message -- but it
// arrives after a real, permanent Dolt commit has already advanced
// schema_migrations. The fix must make the identity check run before (or
// otherwise gate) that auto-apply, so a refused migrate leaves the schema
// cursor untouched.
func TestEmbeddedMigrateSchema_IdentityMismatchRefusesBeforeApplying(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, beadsDir, _ := bdInit(t, bd, "--prefix", "idm")
latest := schema.LatestVersion()
behind := latest - 1
// Put the workspace one migration behind -- so opening the store has a
// pending migration for the smart gate to auto-apply -- and give the
// database a _project_id that no longer matches this workspace's
// config.yaml. Both conditions together are what the real incident
// needs: a decoy/mismatched database that is also behind on schema.
withEmbeddedMigrateSQL(t, beadsDir, "idm", func(db *sql.DB) error {
if _, err := db.ExecContext(t.Context(),
"DELETE FROM schema_migrations WHERE version = ?", latest); err != nil {
return err
}
_, err := db.ExecContext(t.Context(),
"UPDATE metadata SET value = ? WHERE `key` = ?", "corrupted-project-id-be-0gfcs", "_project_id")
return err
})
cursor := func() int {
t.Helper()
var v int
withEmbeddedMigrateSQL(t, beadsDir, "idm", func(db *sql.DB) error {
return db.QueryRowContext(t.Context(),
"SELECT COALESCE(MAX(version), 0) FROM schema_migrations").Scan(&v)
})
return v
}
if got := cursor(); got != behind {
t.Fatalf("precondition failed: schema cursor = %d, want %d", got, behind)
}
cmd := exec.Command(bd, "migrate", "schema")
cmd.Dir = dir
cmd.Env = bdEnv(dir)
stdout, stderr, err := runCommandBuffers(t, cmd)
code := 0
if err != nil {
var exitErr *exec.ExitError
if !errors.As(err, &exitErr) {
t.Fatalf("running bd migrate schema: %v", err)
}
code = exitErr.ExitCode()
}
if code != 1 {
t.Errorf("bd migrate schema exit = %d, want 1\nstdout:\n%s\nstderr:\n%s", code, stdout.String(), stderr.String())
}
if !strings.Contains(stderr.String(), "workspace identity mismatch detected") {
t.Errorf("bd migrate schema stderr missing identity mismatch message:\n%s", stderr.String())
}
if got := cursor(); got != behind {
t.Fatalf("bd migrate schema refused the workspace but still advanced schema_migrations from %d to %d -- "+
"the smart-gate auto-migration on store-open ran before the identity check (be-0gfcs)", behind, got)
}
}