* test(testutil): local dolt sql-server backend behind the Dolt test server API Add a second backend to internal/testutil behind the unchanged container API (EnsureDoltContainerForTestMain, RequireDoltContainer, StartIsolatedDoltContainer[Handle], NewContainerProvider, RestartSharedDoltContainer, DoltContainerCrashed, IsolatedDoltContainer Stop/Start/CurrentPort/Exec): the test process spawns `dolt sql-server` from the pinned dolt CLI instead of a dolthub/dolt-sql-server container. - BEADS_TEST_DOLT_SERVER=local|container selects it; unset keeps plain `go test` on the container (no behavior change) and makes `bazel test` use the local backend. Other values fail instead of guessing. - The CLI (BEADS_TEST_DOLT_BINARY, else PATH) must be the image's version. - Provisioned like testcontainers' dolt module: root@% (DOLT_ROOT_HOST=%), database beads_test, user test/test. Clean env, TZ=UTC, GOMAXPROCS=4, own dolt global config with metrics and the version check disabled. - Loopback port picked here (dolt rejects -P 0), never 3307, retried when another process wins the port; readiness requires the server's own "Server ready" line plus a bounded ping, so a foreign listener on a contested port cannot pass for it. - State lives under the TMPDIR captured at package init, outside every PinSuiteTempRoot root, so suite leak sweeps never see it. The server stays in the test's process group (bazel's group kill reaps it) with Pdeathsig on Linux. - BEADS_TEST_REQUIRE_DOLT_CONTAINER=1 now also makes every TestMain that only warned about an unavailable server exit 1 (DoltUnavailableForTestMain), plus the shared proxied harness. - dolt_benchmark_purge_test finds .dolt_dropped_databases relative to Exec's working directory (the data dir in both backends). - TestDoltServerFingerprint checks each available backend against one checked-in fingerprint (users, grants, databases, server variables); local-backend tests cover the bind-race retry, 3307, crash detection, Stop/Start, Exec cwd, state outside the suite root, and teardown. - Bazel: the requires-docker variants pin BEADS_TEST_DOLT_SERVER=container so the docker lane stays the container A/B control; the test wrapper's dolt global config disables metrics and the version check. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * fix(testutil): select the local Dolt test server only when asked for Under Bazel an unset BEADS_TEST_DOLT_SERVER used to mean the local backend, so every non-ci `bazel test` started servers for Dolt suites no lane runs with one (//internal/storage/dolt:dolt_test went from a 16 s skip to a timeout). Unset now means container everywhere: in a Bazel action without docker that is the pre-existing warn+skip, and only a target's env or --test_env=BEADS_TEST_DOLT_SERVER=local starts local servers. Plain `go test` is unchanged. The docker variants keep their container pin: a rule env beats --test_env, so a local-backend invocation cannot flip the docker lane. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * fix(testutil): harden the local Dolt test server's host footprint - DOLT_DISABLE_EVENT_FLUSH=1 for every dolt the backend runs (and in the Bazel test wrapper): the metrics/versioncheck keys stop network calls but not the detached `dolt send-metrics` child each command forks on exit, which re-created the version probe's removed HOME (one leaked bdt-doltver-* per test process) and raced terminate's RemoveAll. - Every bdt-dolt-*/bdt-doltver-* root records its owner PID, and the first use of the backend in a process removes this user's roots whose owner is dead (the SweepDeadSuiteRoots rule; a copy, since doltserver's tests import testutil). A panicking, timed-out or killed test binary, or a package whose TestMain never terminates the singleton, no longer leaks a 40-100 MB state dir per run. - secure_file_priv is the state root's sfp/ directory, set through a per-launch --config file (dolt ignores other flags with --config): a local server runs as the test user on the host with a passwordless root@%, so LOAD_FILE / INTO OUTFILE must not reach the user's files. The fingerprint treats that path as the container's "". - Fingerprint: character_set_server, collation_server, max_allowed_packet, transaction_isolation, lower_case_table_names, lower_case_file_system (compared on Linux only: APFS reports ON), wait_timeout, innodb_lock_wait_timeout. - The local backend's own tests skip off Linux (no Pdeathsig, so a killed test binary would orphan servers on the macOS runners) unless BEADS_TEST_DOLT_SERVER=local asks for them. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * ci: compare the container and local Dolt test servers in the domain+uow job TestDoltServerFingerprint's container half never ran in CI: only the test-domain-uow job has both the pulled image and the pinned dolt CLI, and it never ran ./internal/testutil. Run the fingerprint there (with BEADS_TEST_REQUIRE_DOLT_CONTAINER=1 so the container half cannot skip) so an image bump that drifts the container side fails in the PR. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
966 lines
26 KiB
Go
966 lines
26 KiB
Go
//go:build regression
|
|
|
|
// Package regression implements differential testing between a pinned baseline
|
|
// bd binary (v0.49.6) and the current worktree build. Each test scenario runs
|
|
// the same CLI commands against both binaries in isolated workspaces, snapshots
|
|
// state via bd list + bd show, normalizes volatile fields and IDs, and diffs
|
|
// the results.
|
|
//
|
|
// Run: go test -tags=regression -timeout=10m ./tests/regression/...
|
|
// Or: make test-regression
|
|
package regression
|
|
|
|
import (
|
|
"archive/tar"
|
|
"compress/gzip"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"hash/fnv"
|
|
"io"
|
|
"net/http"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"regexp"
|
|
"runtime"
|
|
"sort"
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/steveyegge/beads/internal/testutil"
|
|
)
|
|
|
|
// baselineBin is the path to the pinned baseline bd binary.
|
|
var baselineBin string
|
|
|
|
// candidateBin is the path to the bd binary built from the current worktree.
|
|
var candidateBin string
|
|
|
|
// testDoltServerPort is the port of the isolated Dolt server started by TestMain.
|
|
var testDoltServerPort int
|
|
|
|
func TestMain(m *testing.M) {
|
|
os.Exit(testMainInner(m))
|
|
}
|
|
|
|
// testMainInner holds TestMain's body so its defer runs before the process
|
|
// exits — os.Exit skips deferred calls, so TestMain itself must never defer
|
|
// anything (be-5kkk6).
|
|
func testMainInner(m *testing.M) int {
|
|
if runtime.GOOS == "windows" {
|
|
fmt.Fprintln(os.Stderr, "regression tests not yet supported on Windows (zip extraction needed)")
|
|
return 0
|
|
}
|
|
|
|
// Start an isolated Dolt server so regression tests don't pollute
|
|
// the production database on port 3307.
|
|
if _, err := exec.LookPath("dolt"); err != nil {
|
|
if os.Getenv("GITHUB_ACTIONS") == "true" {
|
|
fmt.Fprintln(os.Stderr, "FAIL: dolt missing under GITHUB_ACTIONS — CI workflow must install dolt")
|
|
return 1
|
|
}
|
|
fmt.Fprintln(os.Stderr, "SKIP: dolt not found in PATH; regression tests require dolt")
|
|
return 0
|
|
}
|
|
os.Setenv("BEADS_TEST_MODE", "1")
|
|
// AD-01 (be-c5p): allow regression tests to connect to the test container.
|
|
os.Setenv("BEADS_TEST_SERVER", "1")
|
|
if err := testutil.EnsureDoltContainerForTestMain(); err != nil {
|
|
if testutil.DoltUnavailableForTestMain(err) {
|
|
return 1
|
|
}
|
|
} else {
|
|
defer testutil.TerminateDoltContainer()
|
|
testDoltServerPort = testutil.DoltContainerPortInt()
|
|
fmt.Fprintf(os.Stderr, "Test Dolt server running on port %d\n", testDoltServerPort)
|
|
}
|
|
|
|
tmpDir, err := os.MkdirTemp("", "bd-regression-bin-*")
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "creating temp dir: %v\n", err)
|
|
return 1
|
|
}
|
|
|
|
// Build candidate from current worktree
|
|
candidateBin = filepath.Join(tmpDir, "bd-candidate")
|
|
fmt.Fprintln(os.Stderr, "Building candidate binary...")
|
|
if err := buildCandidate(candidateBin); err != nil {
|
|
fmt.Fprintf(os.Stderr, "building candidate: %v\n", err)
|
|
os.RemoveAll(tmpDir)
|
|
return 1
|
|
}
|
|
|
|
// Get baseline (env override > cache > download)
|
|
fmt.Fprintln(os.Stderr, "Getting baseline binary...")
|
|
baselineBin, err = getBaseline()
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "getting baseline: %v\n", err)
|
|
os.RemoveAll(tmpDir)
|
|
return 1
|
|
}
|
|
|
|
fmt.Fprintf(os.Stderr, "Baseline: %s\nCandidate: %s\n\n", baselineBin, candidateBin)
|
|
code := m.Run()
|
|
os.RemoveAll(tmpDir)
|
|
return code
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Binary management
|
|
// ---------------------------------------------------------------------------
|
|
|
|
func findModuleRoot() string {
|
|
_, filename, _, ok := runtime.Caller(0)
|
|
if !ok {
|
|
panic("could not determine test file location")
|
|
}
|
|
dir := filepath.Dir(filename)
|
|
for {
|
|
if _, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil {
|
|
return dir
|
|
}
|
|
parent := filepath.Dir(dir)
|
|
if parent == dir {
|
|
panic("could not find go.mod")
|
|
}
|
|
dir = parent
|
|
}
|
|
}
|
|
|
|
func buildCandidate(outPath string) error {
|
|
modRoot := findModuleRoot()
|
|
cmd := exec.Command("go", "build", "-tags", "gms_pure_go", "-o", outPath, "./cmd/bd")
|
|
cmd.Dir = modRoot
|
|
cmd.Env = buildEnv()
|
|
|
|
out, err := cmd.CombinedOutput()
|
|
if err != nil {
|
|
return fmt.Errorf("go build: %w\n%s", err, out)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func buildEnv() []string {
|
|
env := os.Environ()
|
|
if prefix := icuPrefixPath(); prefix != "" {
|
|
env = append(env,
|
|
"CGO_CFLAGS=-I"+prefix+"/include",
|
|
"CGO_CPPFLAGS=-I"+prefix+"/include",
|
|
"CGO_LDFLAGS=-L"+prefix+"/lib",
|
|
)
|
|
}
|
|
return env
|
|
}
|
|
|
|
func icuPrefixPath() string {
|
|
out, err := exec.Command("brew", "--prefix", "icu4c").Output()
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
return strings.TrimSpace(string(out))
|
|
}
|
|
|
|
func getBaseline() (string, error) {
|
|
if bin := os.Getenv("BD_REGRESSION_BASELINE_BIN"); bin != "" {
|
|
if _, err := os.Stat(bin); err != nil {
|
|
return "", fmt.Errorf("BD_REGRESSION_BASELINE_BIN=%q: %w", bin, err)
|
|
}
|
|
return bin, nil
|
|
}
|
|
|
|
versionFile := filepath.Join(findModuleRoot(), "tests", "regression", "BASELINE_VERSION")
|
|
data, err := os.ReadFile(versionFile)
|
|
if err != nil {
|
|
return "", fmt.Errorf("reading BASELINE_VERSION: %w", err)
|
|
}
|
|
version := strings.TrimSpace(string(data))
|
|
|
|
cacheDir, err := os.UserCacheDir()
|
|
if err != nil {
|
|
cacheDir = os.TempDir()
|
|
}
|
|
cacheDir = filepath.Join(cacheDir, "beads-regression")
|
|
cachedBin := filepath.Join(cacheDir, "bd-"+version)
|
|
|
|
if info, err := os.Stat(cachedBin); err == nil && info.Size() > 0 {
|
|
fmt.Fprintf(os.Stderr, "Using cached baseline: %s\n", cachedBin)
|
|
return cachedBin, nil
|
|
}
|
|
|
|
if err := os.MkdirAll(cacheDir, 0o755); err != nil {
|
|
return "", fmt.Errorf("creating cache dir: %w", err)
|
|
}
|
|
ver := strings.TrimPrefix(version, "v")
|
|
asset := fmt.Sprintf("beads_%s_%s_%s.tar.gz", ver, runtime.GOOS, runtime.GOARCH)
|
|
url := fmt.Sprintf("https://github.com/steveyegge/beads/releases/download/%s/%s", version, asset)
|
|
|
|
fmt.Fprintf(os.Stderr, "Downloading baseline: %s\n", url)
|
|
if err := downloadAndExtract(url, cachedBin); err != nil {
|
|
return "", fmt.Errorf("downloading baseline: %w", err)
|
|
}
|
|
|
|
return cachedBin, nil
|
|
}
|
|
|
|
func downloadAndExtract(url, destPath string) error {
|
|
resp, err := http.Get(url) //nolint:gosec // URL constructed from trusted constants
|
|
if err != nil {
|
|
return fmt.Errorf("GET %s: %w", url, err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
return fmt.Errorf("GET %s: HTTP %d", url, resp.StatusCode)
|
|
}
|
|
|
|
gzr, err := gzip.NewReader(resp.Body)
|
|
if err != nil {
|
|
return fmt.Errorf("gzip: %w", err)
|
|
}
|
|
defer gzr.Close()
|
|
|
|
tr := tar.NewReader(gzr)
|
|
for {
|
|
hdr, err := tr.Next()
|
|
if err == io.EOF {
|
|
break
|
|
}
|
|
if err != nil {
|
|
return fmt.Errorf("tar: %w", err)
|
|
}
|
|
if filepath.Base(hdr.Name) == "bd" {
|
|
f, err := os.OpenFile(destPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o755)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
_, copyErr := io.Copy(f, tr)
|
|
closeErr := f.Close()
|
|
if copyErr != nil {
|
|
return copyErr
|
|
}
|
|
return closeErr
|
|
}
|
|
}
|
|
return fmt.Errorf("bd binary not found in archive from %s", url)
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Workspace: isolated temp dir with git repo + bd init
|
|
// ---------------------------------------------------------------------------
|
|
|
|
type workspace struct {
|
|
dir string
|
|
bdPath string
|
|
t *testing.T
|
|
createdIDs []string // issue IDs in creation order
|
|
}
|
|
|
|
func newWorkspace(t *testing.T, bdPath string) *workspace {
|
|
t.Helper()
|
|
dir, err := os.MkdirTemp("", "bd-regression-workspace-*")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() {
|
|
removeWorkspaceDir(t, dir)
|
|
})
|
|
w := &workspace{dir: dir, bdPath: bdPath, t: t}
|
|
w.cleanupBaselineDaemon()
|
|
|
|
w.git("init")
|
|
w.git("config", "user.name", "regression-test")
|
|
w.git("config", "user.email", "test@regression.test")
|
|
|
|
if err := os.WriteFile(filepath.Join(dir, ".gitkeep"), nil, 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
w.git("add", ".")
|
|
w.git("commit", "-m", "initial")
|
|
|
|
// Use a unique prefix per workspace so each gets its own Dolt database
|
|
// (t<hash>). The init command creates database "<prefix>",
|
|
// and subsequent commands read the database name from metadata.json.
|
|
h := fnv.New64a()
|
|
_, _ = h.Write([]byte(dir))
|
|
prefix := fmt.Sprintf("t%x", h.Sum64())
|
|
w.run("init", "--prefix", prefix, "--quiet")
|
|
|
|
return w
|
|
}
|
|
|
|
func removeWorkspaceDir(t *testing.T, dir string) {
|
|
t.Helper()
|
|
deadline := time.Now().Add(5 * time.Second)
|
|
var err error
|
|
for {
|
|
err = os.RemoveAll(dir)
|
|
if err == nil {
|
|
return
|
|
}
|
|
if time.Now().After(deadline) {
|
|
t.Fatalf("remove workspace dir %s: %v", dir, err)
|
|
}
|
|
time.Sleep(50 * time.Millisecond)
|
|
}
|
|
}
|
|
|
|
func (w *workspace) cleanupBaselineDaemon() {
|
|
w.t.Helper()
|
|
if baselineBin == "" || w.bdPath != baselineBin {
|
|
return
|
|
}
|
|
|
|
w.t.Cleanup(func() {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
|
|
cmd := exec.CommandContext(ctx, w.bdPath, "daemon", "stop")
|
|
cmd.Dir = w.dir
|
|
cmd.Env = w.runEnv()
|
|
_ = cmd.Run()
|
|
|
|
if pkill, err := exec.LookPath("pkill"); err == nil {
|
|
pkillCtx, pkillCancel := context.WithTimeout(context.Background(), 2*time.Second)
|
|
defer pkillCancel()
|
|
|
|
pattern := "^" + regexp.QuoteMeta(w.bdPath) + " daemon start$"
|
|
_ = exec.CommandContext(pkillCtx, pkill, "-f", pattern).Run()
|
|
}
|
|
})
|
|
}
|
|
|
|
func (w *workspace) runEnv() []string {
|
|
env := []string{
|
|
"PATH=" + os.Getenv("PATH"),
|
|
"HOME=" + w.dir,
|
|
"BEADS_TEST_MODE=1",
|
|
// The pinned v0.49.6 baseline still has daemon mode. Disable it
|
|
// up front so it cannot race t.TempDir cleanup by writing .beads files.
|
|
"BD_NO_DAEMON=1",
|
|
"BEADS_NO_DAEMON=1",
|
|
"BD_DISABLE_METRICS=1",
|
|
"BD_DISABLE_EVENT_FLUSH=1",
|
|
"GIT_CONFIG_NOSYSTEM=1",
|
|
}
|
|
if testDoltServerPort != 0 {
|
|
portStr := strconv.Itoa(testDoltServerPort)
|
|
env = append(env,
|
|
"BEADS_DOLT_PORT="+portStr,
|
|
"BEADS_DOLT_SERVER_PORT="+portStr,
|
|
)
|
|
}
|
|
if v := os.Getenv("TMPDIR"); v != "" {
|
|
env = append(env, "TMPDIR="+v)
|
|
}
|
|
return env
|
|
}
|
|
|
|
func (w *workspace) git(args ...string) string {
|
|
w.t.Helper()
|
|
cmd := exec.Command("git", args...)
|
|
cmd.Dir = w.dir
|
|
cmd.Env = w.runEnv()
|
|
out, err := cmd.CombinedOutput()
|
|
if err != nil {
|
|
w.t.Fatalf("git %s: %v\n%s", strings.Join(args, " "), err, out)
|
|
}
|
|
return string(out)
|
|
}
|
|
|
|
// run executes a bd command (combined stdout+stderr), fataling on error.
|
|
func (w *workspace) run(args ...string) string {
|
|
w.t.Helper()
|
|
cmd := exec.Command(w.bdPath, args...)
|
|
cmd.Dir = w.dir
|
|
cmd.Env = w.runEnv()
|
|
out, err := cmd.CombinedOutput()
|
|
if err != nil {
|
|
w.t.Fatalf("bd %s: %v\n%s", strings.Join(args, " "), err, out)
|
|
}
|
|
return string(out)
|
|
}
|
|
|
|
// create runs bd create --silent and returns the created issue ID.
|
|
// The ID is also appended to createdIDs for canonical mapping.
|
|
func (w *workspace) create(args ...string) string {
|
|
w.t.Helper()
|
|
allArgs := append([]string{"create", "--silent"}, args...)
|
|
cmd := exec.Command(w.bdPath, allArgs...)
|
|
cmd.Dir = w.dir
|
|
cmd.Env = w.runEnv()
|
|
out, err := cmd.Output()
|
|
if err != nil {
|
|
stderr := ""
|
|
if ee, ok := err.(*exec.ExitError); ok {
|
|
stderr = string(ee.Stderr)
|
|
}
|
|
w.t.Fatalf("bd create %s: %v\n%s", strings.Join(args, " "), err, stderr)
|
|
}
|
|
id := strings.TrimSpace(string(out))
|
|
if id == "" {
|
|
w.t.Fatal("bd create returned empty ID")
|
|
}
|
|
w.createdIDs = append(w.createdIDs, id)
|
|
return id
|
|
}
|
|
|
|
// snapshot collects workspace state via bd list + bd show, returning JSONL
|
|
// (one JSON object per line). This replaces the old bd export command which
|
|
// was removed from main. The listArgs are passed to the bd list invocation
|
|
// (e.g., "--status", "open").
|
|
func (w *workspace) snapshot(listArgs ...string) string {
|
|
w.t.Helper()
|
|
|
|
// Step 1: get all issue IDs via bd list (--all includes closed issues,
|
|
// matching the old bd export behavior)
|
|
args := []string{"list", "--json", "-n", "0", "--all"}
|
|
args = append(args, listArgs...)
|
|
listOut := w.run(args...)
|
|
|
|
// Parse JSON array to extract IDs
|
|
var issues []map[string]any
|
|
if err := json.Unmarshal([]byte(listOut), &issues); err != nil {
|
|
// Try JSONL fallback
|
|
for _, line := range strings.Split(strings.TrimSpace(listOut), "\n") {
|
|
if line == "" {
|
|
continue
|
|
}
|
|
var m map[string]any
|
|
if err2 := json.Unmarshal([]byte(line), &m); err2 == nil {
|
|
issues = append(issues, m)
|
|
}
|
|
}
|
|
}
|
|
|
|
if len(issues) == 0 {
|
|
return ""
|
|
}
|
|
|
|
// Step 2: for each ID, run bd show --json and emit one JSONL line
|
|
var buf strings.Builder
|
|
enc := json.NewEncoder(&buf)
|
|
enc.SetEscapeHTML(false)
|
|
for _, issue := range issues {
|
|
id, ok := issue["id"].(string)
|
|
if !ok || id == "" {
|
|
continue
|
|
}
|
|
showOut := w.showJSONForSnapshot(id)
|
|
|
|
// bd show --json may return a JSON array or single object
|
|
var showArr []map[string]any
|
|
if err := json.Unmarshal([]byte(showOut), &showArr); err == nil && len(showArr) > 0 {
|
|
_ = enc.Encode(showArr[0])
|
|
continue
|
|
}
|
|
var showObj map[string]any
|
|
if err := json.Unmarshal([]byte(showOut), &showObj); err == nil {
|
|
_ = enc.Encode(showObj)
|
|
continue
|
|
}
|
|
w.t.Logf("WARNING: snapshot: bd show %s returned unparseable JSON: %s", id, showOut)
|
|
}
|
|
return buf.String()
|
|
}
|
|
|
|
func (w *workspace) showJSONForSnapshot(id string) string {
|
|
args := []string{"show", id, "--json"}
|
|
if w.supportsStreamedShowPayloads() {
|
|
args = append(args, "--include-dependents", "--include-comments")
|
|
}
|
|
return w.run(args...)
|
|
}
|
|
|
|
func (w *workspace) supportsStreamedShowPayloads() bool {
|
|
return candidateBin != "" && w.bdPath == candidateBin
|
|
}
|
|
|
|
// export returns a JSONL snapshot of the workspace. This replaces the removed
|
|
// bd export command with list+show based snapshots. Filter args (e.g.,
|
|
// "--status", "open") are translated to bd list flags.
|
|
func (w *workspace) export(extraArgs ...string) string {
|
|
w.t.Helper()
|
|
// Translate old export flags to list flags.
|
|
// export supported: --status, --assignee, -o (output file, not needed)
|
|
var listArgs []string
|
|
for i := 0; i < len(extraArgs); i++ {
|
|
switch extraArgs[i] {
|
|
case "--status":
|
|
if i+1 < len(extraArgs) {
|
|
listArgs = append(listArgs, "--status", extraArgs[i+1])
|
|
i++
|
|
}
|
|
case "--assignee":
|
|
if i+1 < len(extraArgs) {
|
|
listArgs = append(listArgs, "--assignee", extraArgs[i+1])
|
|
i++
|
|
}
|
|
case "-o", "--output":
|
|
// Skip output file flag and its argument
|
|
if i+1 < len(extraArgs) {
|
|
i++
|
|
}
|
|
default:
|
|
// Pass through unknown flags
|
|
listArgs = append(listArgs, extraArgs[i])
|
|
}
|
|
}
|
|
return w.snapshot(listArgs...)
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Normalization
|
|
// ---------------------------------------------------------------------------
|
|
|
|
var volatileFields = []string{
|
|
"created_at", "updated_at", "closed_at",
|
|
"started_at",
|
|
"compacted_at", "compacted_at_commit",
|
|
"last_activity", "closed_by_session",
|
|
"compaction_level", "original_size",
|
|
"content_hash",
|
|
// revision (row_lock) is the guarded-write optimistic-concurrency token that
|
|
// bd show --json began exposing (bd-bwa7n): a random value the engine
|
|
// rewrites on every write, and absent from the v0.49.6 baseline's show
|
|
// output entirely, so it is pure cross-version noise for this oracle.
|
|
"revision",
|
|
}
|
|
|
|
// showOnlyFields are present in bd show --json but were not in bd export.
|
|
// Strip them so snapshot output is comparable with baseline.
|
|
var showOnlyFields = []string{
|
|
"events",
|
|
}
|
|
|
|
var versionSpecificFields = []string{
|
|
"_type", "deleted_at", "deleted_by", "delete_reason", "original_type",
|
|
"comment_count", "dependency_count", "dependent_count",
|
|
"epic_total_children", "epic_closed_children", "epic_closeable",
|
|
}
|
|
|
|
// normalizeJSONL parses JSONL, normalizes each issue, applies ID canonicalization,
|
|
// sorts by canonical ID, and returns deterministic JSONL for comparison.
|
|
func normalizeJSONL(data string, idMap map[string]string) (string, error) {
|
|
lines := strings.Split(strings.TrimSpace(data), "\n")
|
|
if len(lines) == 0 || (len(lines) == 1 && lines[0] == "") {
|
|
return "", nil
|
|
}
|
|
|
|
var issues []map[string]any
|
|
for i, line := range lines {
|
|
if strings.TrimSpace(line) == "" {
|
|
continue
|
|
}
|
|
var m map[string]any
|
|
if err := json.Unmarshal([]byte(line), &m); err != nil {
|
|
return "", fmt.Errorf("line %d: %w", i+1, err)
|
|
}
|
|
// Skip tombstoned issues: v0.49.6 exports deleted issues as
|
|
// tombstones (with deleted_at set); main omits them entirely.
|
|
if _, hasTombstone := m["deleted_at"]; hasTombstone {
|
|
continue
|
|
}
|
|
normalizeIssue(m)
|
|
canonicalizeIDs(m, idMap)
|
|
// Re-sort deps/comments after canonicalization: real IDs are random
|
|
// so pre-canonicalization sort may produce different orderings.
|
|
sortSubobjects(m)
|
|
issues = append(issues, m)
|
|
}
|
|
|
|
sort.Slice(issues, func(i, j int) bool {
|
|
a, _ := issues[i]["id"].(string)
|
|
b, _ := issues[j]["id"].(string)
|
|
return a < b
|
|
})
|
|
|
|
var buf strings.Builder
|
|
enc := json.NewEncoder(&buf)
|
|
enc.SetEscapeHTML(false)
|
|
for _, issue := range issues {
|
|
if err := enc.Encode(issue); err != nil {
|
|
return "", err
|
|
}
|
|
}
|
|
return buf.String(), nil
|
|
}
|
|
|
|
func normalizeIssue(m map[string]any) {
|
|
for _, f := range volatileFields {
|
|
delete(m, f)
|
|
}
|
|
for _, f := range versionSpecificFields {
|
|
delete(m, f)
|
|
}
|
|
for _, f := range showOnlyFields {
|
|
delete(m, f)
|
|
}
|
|
|
|
// Normalize date-only fields (due_at, defer_until) to date-only format.
|
|
// SQLite stores local-timezone midnight, Dolt stores UTC midnight for
|
|
// the same date input (e.g. "2099-01-15"). Truncate to date to avoid
|
|
// false positives from timezone representation differences.
|
|
for _, df := range []string{"due_at", "defer_until"} {
|
|
if s, ok := m[df].(string); ok && s != "" {
|
|
if t, err := time.Parse(time.RFC3339, s); err == nil {
|
|
m[df] = t.Format("2006-01-02")
|
|
}
|
|
}
|
|
}
|
|
if _, hasDefer := m["defer_until"]; hasDefer {
|
|
if status, _ := m["status"].(string); status == "open" || status == "deferred" {
|
|
delete(m, "status")
|
|
}
|
|
}
|
|
|
|
// Sort labels lexicographically
|
|
if labels, ok := m["labels"].([]any); ok && len(labels) > 0 {
|
|
sort.Slice(labels, func(i, j int) bool {
|
|
a, _ := labels[i].(string)
|
|
b, _ := labels[j].(string)
|
|
return a < b
|
|
})
|
|
}
|
|
|
|
// Normalize comments: strip volatile fields, sort by (author, text)
|
|
if comments, ok := m["comments"].([]any); ok {
|
|
for _, c := range comments {
|
|
if cm, ok := c.(map[string]any); ok {
|
|
delete(cm, "created_at")
|
|
delete(cm, "id")
|
|
}
|
|
}
|
|
sort.Slice(comments, func(i, j int) bool {
|
|
ci, _ := comments[i].(map[string]any)
|
|
cj, _ := comments[j].(map[string]any)
|
|
ai, _ := ci["author"].(string)
|
|
aj, _ := cj["author"].(string)
|
|
if ai != aj {
|
|
return ai < aj
|
|
}
|
|
ti, _ := ci["text"].(string)
|
|
tj, _ := cj["text"].(string)
|
|
return ti < tj
|
|
})
|
|
}
|
|
|
|
// normalizeDepSubobject strips volatile/internal fields from a dependency
|
|
// or dependent sub-object as returned by bd show --json. The candidate's
|
|
// streamed show payload intentionally returns shallow related issues, while
|
|
// the old baseline export included deeper issue fields. Compare the stable
|
|
// relationship identity, not the nested issue's full payload.
|
|
normalizeDepSubobject := func(dm map[string]any) {
|
|
// Normalize dependency_type → type
|
|
if dt, ok := dm["dependency_type"]; ok {
|
|
if _, hasType := dm["type"]; !hasType {
|
|
dm["type"] = dt
|
|
}
|
|
delete(dm, "dependency_type")
|
|
}
|
|
// Dolt stores metadata="{}" where SQLite omits it entirely
|
|
if md, ok := dm["metadata"]; ok {
|
|
if mdStr, _ := md.(string); mdStr == "" || mdStr == "{}" {
|
|
delete(dm, "metadata")
|
|
}
|
|
}
|
|
keep := map[string]bool{
|
|
"id": true,
|
|
"issue_id": true,
|
|
"depends_on_id": true,
|
|
"title": true,
|
|
"status": true,
|
|
"issue_type": true,
|
|
"priority": true,
|
|
"type": true,
|
|
"metadata": true,
|
|
}
|
|
for k := range dm {
|
|
if !keep[k] {
|
|
delete(dm, k)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Normalize dependencies and dependents arrays
|
|
for _, field := range []string{"dependencies", "dependents"} {
|
|
if deps, ok := m[field].([]any); ok {
|
|
for _, d := range deps {
|
|
if dm, ok := d.(map[string]any); ok {
|
|
normalizeDepSubobject(dm)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Remove nil, empty strings, empty collections to handle omitempty differences.
|
|
// Preserve "priority" (0 = P0/critical) and "id".
|
|
for k, v := range m {
|
|
switch val := v.(type) {
|
|
case nil:
|
|
delete(m, k)
|
|
case string:
|
|
if val == "" && k != "id" {
|
|
delete(m, k)
|
|
}
|
|
case []any:
|
|
if len(val) == 0 {
|
|
delete(m, k)
|
|
}
|
|
case map[string]any:
|
|
if len(val) == 0 {
|
|
delete(m, k)
|
|
}
|
|
case bool:
|
|
if !val {
|
|
delete(m, k)
|
|
}
|
|
case float64:
|
|
if val == 0 && k != "priority" {
|
|
delete(m, k)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// canonicalizeIDs replaces real issue IDs with canonical names (ISSUE-1, ISSUE-2, ...)
|
|
// based on creation order, so two runs with different ID schemes can be compared.
|
|
func canonicalizeIDs(m map[string]any, idMap map[string]string) {
|
|
if len(idMap) == 0 {
|
|
return
|
|
}
|
|
|
|
replaceID := func(field string, obj map[string]any) {
|
|
if id, ok := obj[field].(string); ok {
|
|
if canonical, ok := idMap[id]; ok {
|
|
obj[field] = canonical
|
|
}
|
|
}
|
|
}
|
|
|
|
replaceID("id", m)
|
|
replaceID("parent", m)
|
|
|
|
// Canonicalize IDs in dependencies and dependents sub-objects.
|
|
// bd show --json embeds full issue-like objects with "id" field,
|
|
// while the old export format used "issue_id" and "depends_on_id".
|
|
for _, field := range []string{"dependencies", "dependents"} {
|
|
if deps, ok := m[field].([]any); ok {
|
|
for _, d := range deps {
|
|
if dm, ok := d.(map[string]any); ok {
|
|
replaceID("id", dm)
|
|
replaceID("issue_id", dm)
|
|
replaceID("depends_on_id", dm)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if comments, ok := m["comments"].([]any); ok {
|
|
for _, c := range comments {
|
|
if cm, ok := c.(map[string]any); ok {
|
|
replaceID("issue_id", cm)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// sortSubobjects re-sorts dependencies and comments by their canonical IDs.
|
|
// Must be called after canonicalizeIDs, since real IDs are random and
|
|
// pre-canonicalization sort order is non-deterministic across runs.
|
|
func sortSubobjects(m map[string]any) {
|
|
// Sort both dependencies and dependents by (id, type)
|
|
for _, field := range []string{"dependencies", "dependents"} {
|
|
if deps, ok := m[field].([]any); ok && len(deps) > 1 {
|
|
sort.Slice(deps, func(i, j int) bool {
|
|
di, _ := deps[i].(map[string]any)
|
|
dj, _ := deps[j].(map[string]any)
|
|
// Try canonical id first, then depends_on_id
|
|
a, _ := di["id"].(string)
|
|
b, _ := dj["id"].(string)
|
|
if a == "" {
|
|
a, _ = di["depends_on_id"].(string)
|
|
}
|
|
if b == "" {
|
|
b, _ = dj["depends_on_id"].(string)
|
|
}
|
|
if a != b {
|
|
return a < b
|
|
}
|
|
ta, _ := di["type"].(string)
|
|
tb, _ := dj["type"].(string)
|
|
return ta < tb
|
|
})
|
|
}
|
|
}
|
|
if comments, ok := m["comments"].([]any); ok && len(comments) > 1 {
|
|
sort.Slice(comments, func(i, j int) bool {
|
|
ci, _ := comments[i].(map[string]any)
|
|
cj, _ := comments[j].(map[string]any)
|
|
ai, _ := ci["author"].(string)
|
|
aj, _ := cj["author"].(string)
|
|
if ai != aj {
|
|
return ai < aj
|
|
}
|
|
ti, _ := ci["text"].(string)
|
|
tj, _ := cj["text"].(string)
|
|
return ti < tj
|
|
})
|
|
}
|
|
}
|
|
|
|
// canonicalIDMap builds ISSUE-1, ISSUE-2, ... from a slice of IDs in creation order.
|
|
func canonicalIDMap(ids []string) map[string]string {
|
|
m := make(map[string]string, len(ids))
|
|
for i, id := range ids {
|
|
m[id] = fmt.Sprintf("ISSUE-%d", i+1)
|
|
}
|
|
return m
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Comparison helpers
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// compareExports runs a scenario against both binaries, snapshots state via
|
|
// list+show, canonicalizes IDs based on creation order, and diffs.
|
|
func compareExports(t *testing.T, scenario func(w *workspace)) {
|
|
t.Helper()
|
|
|
|
baselineWS := newWorkspace(t, baselineBin)
|
|
scenario(baselineWS)
|
|
baselineRaw := baselineWS.snapshot()
|
|
|
|
candidateWS := newWorkspace(t, candidateBin)
|
|
scenario(candidateWS)
|
|
candidateRaw := candidateWS.snapshot()
|
|
|
|
diffNormalized(t,
|
|
baselineRaw, candidateRaw,
|
|
canonicalIDMap(baselineWS.createdIDs),
|
|
canonicalIDMap(candidateWS.createdIDs),
|
|
)
|
|
}
|
|
|
|
// diffNormalized normalizes two JSONL strings and reports differences.
|
|
func diffNormalized(t *testing.T, baselineRaw, candidateRaw string, baselineIDMap, candidateIDMap map[string]string) {
|
|
t.Helper()
|
|
|
|
baselineNorm, err := normalizeJSONL(baselineRaw, baselineIDMap)
|
|
if err != nil {
|
|
t.Fatalf("normalizing baseline: %v", err)
|
|
}
|
|
candidateNorm, err := normalizeJSONL(candidateRaw, candidateIDMap)
|
|
if err != nil {
|
|
t.Fatalf("normalizing candidate: %v", err)
|
|
}
|
|
|
|
if baselineNorm == candidateNorm {
|
|
return
|
|
}
|
|
|
|
t.Error("JSONL mismatch between baseline and candidate")
|
|
|
|
bMap := issuesByID(baselineNorm)
|
|
cMap := issuesByID(candidateNorm)
|
|
|
|
for id, bLine := range bMap {
|
|
cLine, ok := cMap[id]
|
|
if !ok {
|
|
t.Errorf(" %s: in baseline, missing from candidate", id)
|
|
continue
|
|
}
|
|
if bLine != cLine {
|
|
t.Errorf(" %s differs:\n baseline: %s\n candidate: %s", id, bLine, cLine)
|
|
}
|
|
}
|
|
for id := range cMap {
|
|
if _, ok := bMap[id]; !ok {
|
|
t.Errorf(" %s: in candidate, missing from baseline", id)
|
|
}
|
|
}
|
|
}
|
|
|
|
func issuesByID(jsonl string) map[string]string {
|
|
m := make(map[string]string)
|
|
for _, line := range strings.Split(strings.TrimSpace(jsonl), "\n") {
|
|
if line == "" {
|
|
continue
|
|
}
|
|
var obj map[string]any
|
|
if err := json.Unmarshal([]byte(line), &obj); err != nil {
|
|
continue
|
|
}
|
|
if id, ok := obj["id"].(string); ok {
|
|
m[id] = line
|
|
}
|
|
}
|
|
return m
|
|
}
|
|
|
|
// parseReadyIDs runs bd ready --json and returns the set of issue IDs.
|
|
func parseReadyIDs(t *testing.T, w *workspace) map[string]bool {
|
|
t.Helper()
|
|
out := w.run("ready", "--json")
|
|
ids := make(map[string]bool)
|
|
|
|
// Try JSON array first
|
|
var issues []map[string]any
|
|
if err := json.Unmarshal([]byte(out), &issues); err == nil {
|
|
for _, m := range issues {
|
|
if id, ok := m["id"].(string); ok {
|
|
ids[id] = true
|
|
}
|
|
}
|
|
return ids
|
|
}
|
|
|
|
// Fall back to JSONL
|
|
for _, line := range strings.Split(strings.TrimSpace(out), "\n") {
|
|
if line == "" {
|
|
continue
|
|
}
|
|
var m map[string]any
|
|
if err := json.Unmarshal([]byte(line), &m); err != nil {
|
|
continue
|
|
}
|
|
if id, ok := m["id"].(string); ok {
|
|
ids[id] = true
|
|
}
|
|
}
|
|
return ids
|
|
}
|
|
|
|
// tryRun executes a bd command and returns (output, error) without fataling.
|
|
func (w *workspace) tryRun(args ...string) (string, error) {
|
|
w.t.Helper()
|
|
cmd := exec.Command(w.bdPath, args...)
|
|
cmd.Dir = w.dir
|
|
cmd.Env = w.runEnv()
|
|
out, err := cmd.CombinedOutput()
|
|
return string(out), err
|
|
}
|
|
|
|
// tryCreate runs bd create --silent and returns (issueID, error) without fataling.
|
|
// On success the ID is appended to createdIDs for canonical mapping.
|
|
func (w *workspace) tryCreate(args ...string) (string, error) {
|
|
w.t.Helper()
|
|
allArgs := append([]string{"create", "--silent"}, args...)
|
|
cmd := exec.Command(w.bdPath, allArgs...)
|
|
cmd.Dir = w.dir
|
|
cmd.Env = w.runEnv()
|
|
out, err := cmd.Output()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
id := strings.TrimSpace(string(out))
|
|
if id == "" {
|
|
return "", fmt.Errorf("bd create returned empty ID")
|
|
}
|
|
w.createdIDs = append(w.createdIDs, id)
|
|
return id, nil
|
|
}
|
|
|
|
// Test Dolt server cleanup is handled by testutil.TerminateDoltContainer.
|