gitgram/aitimemachine/beads/cmd/bd/flatten_embedded_test.go
caelum0x 49b183818c Unify gitgram + aitimemachine into the monorepo (de-submodule) and merge vendored engines
Convert gitgram/ and aitimemachine/ from embedded git repos (which showed as
opaque submodule gitlinks on GitHub) into fully-tracked first-party content.

AITM now merges its 7 vendored repos as first-party engines:
- memory: beads (Go task DAG) + context7 (docs SDK), unified into saved_contexts
- git: vendored C git (bundled binary) + gitnexus (in-process code intelligence)
- agentic: a2a (@a2a-js/sdk) + pal (AITM-managed Python subprocess)

Includes the full session's product build: Gitgram web/mobile/backend features,
AITM website/CLI/MCP, the unified memory layer, engine build tooling
(scripts/build-engines.sh, Dockerfile, CI), docs (README, ENGINES.md, ARCHITECTURE.md),
and extensive test suites.

Build artifacts, node_modules, external/, and gemini-cli are gitignored.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-23 14:14:09 +03:00

173 lines
5.3 KiB
Go

//go:build cgo
package main
import (
"fmt"
"os"
"os/exec"
"strings"
"sync"
"testing"
)
// bdFlatten runs "bd flatten" with the given args and returns stdout.
func bdFlatten(t *testing.T, bd, dir string, args ...string) string {
t.Helper()
fullArgs := append([]string{"flatten"}, args...)
cmd := exec.Command(bd, fullArgs...)
cmd.Dir = dir
cmd.Env = bdEnv(dir)
stdout, stderr, err := runCommandBuffers(t, cmd)
if err != nil {
t.Fatalf("bd flatten %s failed: %v\nstdout:\n%s\nstderr:\n%s", strings.Join(args, " "), err, stdout.String(), stderr.String())
}
return stdout.String()
}
// bdFlattenFail runs "bd flatten" expecting failure.
func bdFlattenFail(t *testing.T, bd, dir string, args ...string) string {
t.Helper()
fullArgs := append([]string{"flatten"}, args...)
cmd := exec.Command(bd, fullArgs...)
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
if err == nil {
t.Fatalf("expected bd flatten %s to fail, but succeeded:\n%s", strings.Join(args, " "), out)
}
return string(out)
}
func TestEmbeddedFlatten(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)
// ===== Dry Run =====
t.Run("flatten_dry_run", func(t *testing.T) {
dir, _, _ := bdInit(t, bd, "--prefix", "fd")
// Create issues to generate commits
bdCreate(t, bd, dir, "Flatten dry-run issue 1", "--type", "task")
bdCreate(t, bd, dir, "Flatten dry-run issue 2", "--type", "task")
out := bdFlatten(t, bd, dir, "--dry-run")
if !strings.Contains(out, "Commits") || !strings.Contains(out, "DRY RUN") {
t.Errorf("expected commit count and DRY RUN in output: %s", out)
}
})
// ===== Already Flat =====
t.Run("flatten_fresh_db", func(t *testing.T) {
dir, _, _ := bdInit(t, bd, "--prefix", "fa")
// Fresh init may have 1 or more commits depending on schema setup.
// Either "Already flat" or successful flatten is fine.
out := bdFlatten(t, bd, dir, "--force")
if !strings.Contains(out, "Already flat") && !strings.Contains(out, "Flattened") && !strings.Contains(out, "already flat") {
t.Errorf("expected 'Already flat' or 'Flattened' message: %s", out)
}
})
// ===== No --force Errors =====
// Note: In embedded mode, store.Log() may return 0 entries even when
// Dolt commits exist, causing flatten to always report "Already flat".
// These tests skip when that happens rather than failing.
t.Run("flatten_no_force", func(t *testing.T) {
dir, _, _ := bdInit(t, bd, "--prefix", "fn")
bdCreate(t, bd, dir, "Flatten no-force 1", "--type", "task")
bdCreate(t, bd, dir, "Flatten no-force 2", "--type", "task")
dryOut := bdFlatten(t, bd, dir, "--dry-run")
if strings.Contains(dryOut, "Already flat") || strings.Contains(dryOut, "Commits: 0") {
t.Skip("store.Log() returns 0 commits in embedded mode — flatten guard untestable")
}
bdFlattenFail(t, bd, dir)
})
// ===== Force =====
t.Run("flatten_force", func(t *testing.T) {
dir, _, _ := bdInit(t, bd, "--prefix", "ff")
bdCreate(t, bd, dir, "Flatten force 1", "--type", "task")
bdCreate(t, bd, dir, "Flatten force 2", "--type", "task")
bdCreate(t, bd, dir, "Flatten force 3", "--type", "task")
dryOut := bdFlatten(t, bd, dir, "--dry-run")
if strings.Contains(dryOut, "Already flat") || strings.Contains(dryOut, "Commits: 0") {
t.Skip("store.Log() returns 0 commits in embedded mode — flatten untestable")
}
out := bdFlatten(t, bd, dir, "--force")
if !strings.Contains(out, "Flattened") {
t.Errorf("expected 'Flattened' in output: %s", out)
}
// Verify only 1 commit remains
dryOut = bdFlatten(t, bd, dir, "--dry-run")
if !strings.Contains(dryOut, "Already flat") && !strings.Contains(dryOut, "1") {
t.Errorf("expected 1 commit after flatten: %s", dryOut)
}
// Verify issues still exist after flatten
cmd := exec.Command(bd, "count")
cmd.Dir = dir
cmd.Env = bdEnv(dir)
stdout, stderr, err := runCommandBuffers(t, cmd)
if err != nil {
t.Fatalf("bd count after flatten failed: %v\nstdout:\n%s\nstderr:\n%s", err, stdout.String(), stderr.String())
}
if strings.Contains(stdout.String(), ": 0") {
t.Errorf("expected issues to survive flatten: %s", stdout.String())
}
})
}
// TestEmbeddedFlattenConcurrent exercises flatten --dry-run concurrently.
func TestEmbeddedFlattenConcurrent(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", "fx")
bdCreate(t, bd, dir, "Flatten concurrent issue", "--type", "task")
const numWorkers = 8
type workerResult struct {
worker int
err error
}
results := make([]workerResult, numWorkers)
var wg sync.WaitGroup
wg.Add(numWorkers)
for w := 0; w < numWorkers; w++ {
go func(worker int) {
defer wg.Done()
r := workerResult{worker: worker}
cmd := exec.Command(bd, "flatten", "--dry-run")
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
if err != nil {
r.err = fmt.Errorf("flatten --dry-run (worker %d): %v\n%s", worker, err, out)
}
results[worker] = r
}(w)
}
wg.Wait()
for _, r := range results {
if r.err != nil && !strings.Contains(r.err.Error(), "one writer at a time") {
t.Errorf("worker %d failed: %v", r.worker, r.err)
}
}
}