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>
242 lines
7.4 KiB
Go
242 lines
7.4 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/steveyegge/beads/internal/config"
|
|
)
|
|
|
|
// beforeTestsHook is set by CGO-tagged test files to perform setup before tests run
|
|
// (e.g., starting a shared test Dolt server). Returns a cleanup function.
|
|
var beforeTestsHook func() func()
|
|
|
|
// testTempRoot is the parent directory for per-process test temp dirs.
|
|
// It is set by testMainInner and used by the package-level sync.Once
|
|
// helpers (build binaries, isolated HOMEs) that previously called
|
|
// os.MkdirTemp("", ...) and leaked on every run. Anchoring those temp
|
|
// dirs under testTempRoot means the defer in testMainInner cleans them
|
|
// all up in one place (bd-3q2u / gastownhall/beads#4106).
|
|
//
|
|
// When tests run without TestMain (e.g. a single test invoked with the
|
|
// internal test binary directly), testTempRoot is empty and helpers
|
|
// fall back to os.TempDir().
|
|
var testTempRoot string
|
|
|
|
// testTempDir returns os.MkdirTemp under testTempRoot when it is set,
|
|
// otherwise it falls back to the system temp dir (os.MkdirTemp's
|
|
// default). Use this in package-level sync.Once builders so leaked
|
|
// directories get reaped by testMainInner's deferred cleanup.
|
|
func testTempDir(pattern string) (string, error) {
|
|
return os.MkdirTemp(testTempRoot, pattern)
|
|
}
|
|
|
|
// Guardrail: ensure the cmd/bd test suite does not touch the real repo .beads state.
|
|
// Disable with BEADS_TEST_GUARD_DISABLE=1 (useful when running tests while actively using beads).
|
|
func TestMain(m *testing.M) {
|
|
// Delegate to testMainInner so defers run before os.Exit.
|
|
os.Exit(testMainInner(m))
|
|
}
|
|
|
|
func testMainInner(m *testing.M) int {
|
|
origWD, _ := os.Getwd()
|
|
|
|
// Isolate config discovery from the repo's tracked `.beads/config.yaml`.
|
|
// Many tests expect default config values; running from within this repo would
|
|
// cause config.Initialize() to walk up from CWD and load `.beads/config.yaml`,
|
|
// which may set non-default config values and makes tests assert the wrong behavior.
|
|
tmp, err := os.MkdirTemp("", "beads-bd-tests-*")
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "failed to create temp dir: %v\n", err)
|
|
return 1
|
|
}
|
|
defer func() { _ = forceRemoveAll(tmp) }()
|
|
|
|
// Anchor package-level sync.Once builders (test binaries, isolated
|
|
// HOMEs) under this directory so the defer above sweeps them up too.
|
|
// Without this, those helpers leaked ~179MB-1.4GB per test run into
|
|
// /tmp and exhausted tmpfs over time (bd-3q2u).
|
|
testTempRoot = tmp
|
|
|
|
// Preserve Go build cache before changing HOME.
|
|
// On macOS, GOCACHE defaults to $HOME/Library/Caches/go-build.
|
|
// Changing HOME would cause tests that run `go build` (e.g., TestShow)
|
|
// to miss the cache and do a full CGO rebuild (~80s each).
|
|
if os.Getenv("GOCACHE") == "" {
|
|
if out, err := exec.Command("go", "env", "GOCACHE").Output(); err == nil {
|
|
_ = os.Setenv("GOCACHE", strings.TrimSpace(string(out)))
|
|
}
|
|
}
|
|
|
|
// Same for the module cache: GOMODCACHE defaults to $HOME/go/pkg/mod,
|
|
// so without this the in-test `go build` (buildEmbeddedBD) re-downloads
|
|
// every dependency into the temp HOME on each run — slow, and a hard
|
|
// failure when the network is unavailable.
|
|
if os.Getenv("GOMODCACHE") == "" {
|
|
if out, err := exec.Command("go", "env", "GOMODCACHE").Output(); err == nil {
|
|
_ = os.Setenv("GOMODCACHE", strings.TrimSpace(string(out)))
|
|
}
|
|
}
|
|
|
|
_ = os.Setenv("HOME", tmp)
|
|
_ = os.Setenv("USERPROFILE", tmp) // Windows compatibility
|
|
_ = os.Setenv("XDG_CONFIG_HOME", filepath.Join(tmp, "xdg-config"))
|
|
_ = os.Setenv("BEADS_TEST_IGNORE_REPO_CONFIG", "1")
|
|
|
|
// Also reset viper state that was loaded by main.go's init().
|
|
config.ResetForTesting()
|
|
|
|
// Enable test mode that forces accessor functions to use legacy globals.
|
|
// This ensures backward compatibility with tests that manipulate globals directly.
|
|
enableTestModeGlobals()
|
|
|
|
// Set BEADS_TEST_MODE once for the entire test run (bd-cqjoi).
|
|
// Previously each test set/unset this env var via ensureTestMode(),
|
|
// which raced under t.Parallel().
|
|
_ = os.Setenv("BEADS_TEST_MODE", "1")
|
|
|
|
// Clear BEADS_DIR to prevent tests from accidentally picking up the project's
|
|
// .beads directory via git repo detection when there's a redirect file.
|
|
// Each test that needs a .beads directory should set BEADS_DIR explicitly.
|
|
origBeadsDir := os.Getenv("BEADS_DIR")
|
|
os.Unsetenv("BEADS_DIR")
|
|
defer func() {
|
|
if origBeadsDir != "" {
|
|
os.Setenv("BEADS_DIR", origBeadsDir)
|
|
}
|
|
}()
|
|
|
|
// BD_BRANCH is no longer used (all writers operate on main with transactions).
|
|
|
|
// Start shared test Dolt server if the hook is registered (CGO builds).
|
|
// This must happen after HOME is changed so dolt config goes to the temp dir.
|
|
if beforeTestsHook != nil {
|
|
cleanup := beforeTestsHook()
|
|
defer cleanup()
|
|
}
|
|
|
|
if os.Getenv("BEADS_TEST_GUARD_DISABLE") != "" {
|
|
return m.Run()
|
|
}
|
|
|
|
repoRoot := findRepoRootFrom(origWD)
|
|
if repoRoot == "" {
|
|
return m.Run()
|
|
}
|
|
|
|
repoBeadsDir := filepath.Join(repoRoot, ".beads")
|
|
if _, err := os.Stat(repoBeadsDir); err != nil {
|
|
return m.Run()
|
|
}
|
|
|
|
watch := []string{
|
|
"beads.db",
|
|
"beads.db-wal",
|
|
"beads.db-shm",
|
|
"beads.db-journal",
|
|
"issues.jsonl",
|
|
"beads.jsonl",
|
|
"metadata.json",
|
|
// interactions.jsonl excluded: legitimately created by init during tests
|
|
"deletions.jsonl",
|
|
"molecules.jsonl",
|
|
}
|
|
|
|
before := snapshotFiles(repoBeadsDir, watch)
|
|
code := m.Run()
|
|
after := snapshotFiles(repoBeadsDir, watch)
|
|
|
|
if diff := diffSnapshots(before, after); diff != "" {
|
|
fmt.Fprintf(os.Stderr, "ERROR: test suite modified repo .beads state:\n%s\n", diff)
|
|
if code == 0 {
|
|
code = 1
|
|
}
|
|
}
|
|
|
|
return code
|
|
}
|
|
|
|
type fileSnap struct {
|
|
exists bool
|
|
size int64
|
|
modUnix int64
|
|
}
|
|
|
|
func snapshotFiles(dir string, names []string) map[string]fileSnap {
|
|
out := make(map[string]fileSnap, len(names))
|
|
for _, name := range names {
|
|
p := filepath.Join(dir, name)
|
|
info, err := os.Stat(p)
|
|
if err != nil {
|
|
out[name] = fileSnap{exists: false}
|
|
continue
|
|
}
|
|
out[name] = fileSnap{exists: true, size: info.Size(), modUnix: info.ModTime().UnixNano()}
|
|
}
|
|
return out
|
|
}
|
|
|
|
func diffSnapshots(before, after map[string]fileSnap) string {
|
|
var out string
|
|
for name, b := range before {
|
|
a := after[name]
|
|
if b.exists != a.exists {
|
|
out += fmt.Sprintf("- %s: exists %v → %v\n", name, b.exists, a.exists)
|
|
continue
|
|
}
|
|
if !b.exists {
|
|
continue
|
|
}
|
|
// Only report size changes (actual content modification).
|
|
// Ignore mtime-only changes - SQLite shm/wal files can have mtime updated
|
|
// from read-only operations (config loading, etc.) which is not pollution.
|
|
if b.size != a.size {
|
|
out += fmt.Sprintf("- %s: size %d → %d\n", name, b.size, a.size)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
func findRepoRoot() string {
|
|
wd, err := os.Getwd()
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
return findRepoRootFrom(wd)
|
|
}
|
|
|
|
// forceRemoveAll removes a directory tree, handling read-only files
|
|
// (e.g., Go module cache entries under $HOME/go/pkg/mod/).
|
|
// os.RemoveAll fails silently on read-only files; this makes them
|
|
// writable first so cleanup actually succeeds.
|
|
func forceRemoveAll(dir string) error {
|
|
_ = filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
if info.IsDir() && info.Mode()&0200 == 0 {
|
|
_ = os.Chmod(path, info.Mode()|0200)
|
|
}
|
|
return nil
|
|
})
|
|
return os.RemoveAll(dir)
|
|
}
|
|
|
|
func findRepoRootFrom(wd string) string {
|
|
for i := 0; i < 25; i++ {
|
|
if _, err := os.Stat(filepath.Join(wd, "go.mod")); err == nil {
|
|
return wd
|
|
}
|
|
parent := filepath.Dir(wd)
|
|
if parent == wd {
|
|
break
|
|
}
|
|
wd = parent
|
|
}
|
|
return ""
|
|
}
|