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

170 lines
5.4 KiB
Go

package dolt
import (
"os"
"path/filepath"
"testing"
"github.com/steveyegge/beads/internal/doltserver"
)
func TestAutoStart_DisabledWithExternalMode(t *testing.T) {
t.Chdir(t.TempDir())
t.Setenv("BEADS_TEST_MODE", "")
t.Setenv("GT_ROOT", "")
t.Setenv("BEADS_DOLT_AUTO_START", "")
got := resolveAutoStart(false, "", ServerModeExternal)
if got != false {
t.Error("resolveAutoStart should return false when server mode is External")
}
}
// TestAutoStart_ExternalMode_CallerOverrideIgnored verifies that even a caller
// requesting AutoStart=true is overridden when the server is externally managed.
func TestAutoStart_ExternalMode_CallerOverrideIgnored(t *testing.T) {
t.Chdir(t.TempDir())
t.Setenv("BEADS_TEST_MODE", "")
t.Setenv("GT_ROOT", "")
t.Setenv("BEADS_DOLT_AUTO_START", "")
got := resolveAutoStart(true, "", ServerModeExternal)
if got != false {
t.Error("resolveAutoStart should return false with External mode even when caller requests true")
}
}
// TestAutoStart_ConfigFalse_OverridesCallerTrue verifies that dolt.auto-start: false
// in config.yaml takes precedence over a caller passing current=true. This is the
// core fix for the auto-start bug where ApplyCLIAutoStart and bootstrap paths
// hardcoded current=true, ignoring the user's config.yaml opt-out.
func TestAutoStart_ConfigFalse_OverridesCallerTrue(t *testing.T) {
t.Chdir(t.TempDir())
t.Setenv("BEADS_TEST_MODE", "")
t.Setenv("GT_ROOT", "")
t.Setenv("BEADS_DOLT_AUTO_START", "")
for _, cfgVal := range []string{"false", "False", "FALSE", "0", "off", "Off", "OFF"} {
got := resolveAutoStart(true, cfgVal, ServerModeOwned)
if got != false {
t.Errorf("resolveAutoStart(true, %q, Owned) = true, want false: config.yaml opt-out must override caller", cfgVal)
}
}
}
// TestAutoStart_ConfigEmpty_CallerTrueWins verifies that when config.yaml
// does not set dolt.auto-start, the caller's current=true is respected.
func TestAutoStart_ConfigEmpty_CallerTrueWins(t *testing.T) {
t.Chdir(t.TempDir())
t.Setenv("BEADS_TEST_MODE", "")
t.Setenv("GT_ROOT", "")
t.Setenv("BEADS_DOLT_AUTO_START", "")
got := resolveAutoStart(true, "", ServerModeOwned)
if got != true {
t.Error("resolveAutoStart(true, \"\", Owned) should return true when config is not set")
}
}
// TestAutoStart_EnvOverrideStillWins verifies that BEADS_DOLT_AUTO_START=0
// still takes precedence even in Owned mode (defense-in-depth).
func TestAutoStart_EnvOverrideStillWins(t *testing.T) {
t.Chdir(t.TempDir())
t.Setenv("BEADS_TEST_MODE", "")
t.Setenv("GT_ROOT", "")
t.Setenv("BEADS_DOLT_AUTO_START", "0")
got := resolveAutoStart(true, "", ServerModeOwned) // owned mode, but env says no
if got != false {
t.Error("BEADS_DOLT_AUTO_START=0 should still disable auto-start in Owned mode")
}
}
// TestAutoStartReleaseNoErrorWhenServerAlreadyStopped verifies that
// autoStartRelease does not return an error when the server is already
// gone, preventing false "failed to stop auto-started dolt server"
// warnings (GH#2670). Also verifies stale PID/port files are cleaned up.
func TestAutoStartReleaseNoErrorWhenServerAlreadyStopped(t *testing.T) {
dir := t.TempDir()
// Create stale PID/port files to verify cleanup
pidFile := filepath.Join(dir, doltserver.PIDFileName)
portFile := filepath.Join(dir, doltserver.PortFileName)
if err := os.WriteFile(pidFile, []byte("999999999"), 0600); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(portFile, []byte("13307"), 0600); err != nil {
t.Fatal(err)
}
// Simulate: acquire a reference, then release it when no server is running.
autoStartAcquire(dir)
t.Cleanup(func() {
// Ensure global refcount is cleaned up even if test fails early.
autoStartRefs.mu.Lock()
delete(autoStartRefs.m, dir)
autoStartRefs.mu.Unlock()
})
err := autoStartRelease(dir)
if err != nil {
t.Errorf("autoStartRelease should not error when server is already stopped, got: %v", err)
}
// Verify stale state files were cleaned up by Stop → cleanupStateFiles
if _, statErr := os.Stat(pidFile); !os.IsNotExist(statErr) {
t.Error("PID file should be removed after autoStartRelease on dead server")
}
if _, statErr := os.Stat(portFile); !os.IsNotExist(statErr) {
t.Error("port file should be removed after autoStartRelease on dead server")
}
}
// TestAutoStartReleaseNilMap verifies autoStartRelease returns nil
// when the refcount map was never initialized.
func TestAutoStartReleaseNilMap(t *testing.T) {
// Save and clear the global map.
autoStartRefs.mu.Lock()
saved := autoStartRefs.m
autoStartRefs.m = nil
autoStartRefs.mu.Unlock()
t.Cleanup(func() {
autoStartRefs.mu.Lock()
autoStartRefs.m = saved
autoStartRefs.mu.Unlock()
})
err := autoStartRelease("/nonexistent")
if err != nil {
t.Errorf("expected nil when map is nil, got: %v", err)
}
}
// TestAutoStartReleaseRefcountAboveZero verifies autoStartRelease
// doesn't call Stop when the refcount is still above zero.
func TestAutoStartReleaseRefcountAboveZero(t *testing.T) {
dir := t.TempDir()
// Acquire twice so the first release leaves refcount > 0.
autoStartAcquire(dir)
autoStartAcquire(dir)
t.Cleanup(func() {
autoStartRefs.mu.Lock()
delete(autoStartRefs.m, dir)
autoStartRefs.mu.Unlock()
})
err := autoStartRelease(dir)
if err != nil {
t.Errorf("expected nil when refcount > 0, got: %v", err)
}
// Verify refcount is now 1, not 0.
autoStartRefs.mu.Lock()
count := autoStartRefs.m[dir]
autoStartRefs.mu.Unlock()
if count != 1 {
t.Errorf("expected refcount 1, got %d", count)
}
}