gitgram/aitimemachine/beads/cmd/bd/test_dolt_server_cgo_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

110 lines
3.3 KiB
Go

//go:build cgo
package main
import (
"context"
"database/sql"
"fmt"
"os"
"github.com/steveyegge/beads/internal/storage/dolt"
"github.com/steveyegge/beads/internal/testutil"
)
func init() {
beforeTestsHook = startTestDoltServer
}
// testSharedDB is the name of the shared database for branch-per-test isolation.
var testSharedDB string
// testSharedConn is a raw *sql.DB for branch operations in the shared database.
var testSharedConn *sql.DB
// startTestDoltServer starts a dedicated Dolt SQL server in a container
// on a dynamic port using the shared testutil helper. This prevents tests
// from creating testdb_* databases on the production Dolt server.
// Returns a cleanup function that stops the server and removes the container.
func startTestDoltServer() func() {
if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") == "1" {
return func() {}
}
if os.Getenv("BEADS_TEST_PROXIED_SERVER") == "1" {
return func() {}
}
if err := testutil.EnsureDoltContainerForTestMain(); err != nil {
fmt.Fprintf(os.Stderr, "WARN: %v, skipping Dolt tests\n", err)
return func() {}
}
testDoltServerPort = testutil.DoltContainerPortInt()
// Set up shared database for branch-per-test isolation (bd-xmf).
// Instead of CREATE/DROP DATABASE per test, tests branch from this
// shared DB, eliminating ~1-2s of overhead per test.
testSharedDB = "cmdbd_pkg_shared"
db, err := testutil.SetupSharedTestDB(testDoltServerPort, testSharedDB)
if err != nil {
fmt.Fprintf(os.Stderr, "WARNING: shared DB setup failed: %v (falling back to per-test DBs)\n", err)
testSharedDB = ""
} else {
testSharedConn = db
if err := initCmdBDSharedSchema(testDoltServerPort); err != nil {
fmt.Fprintf(os.Stderr, "WARNING: shared schema init failed: %v (falling back to per-test DBs)\n", err)
testSharedDB = ""
db.Close()
testSharedConn = nil
}
}
return func() {
if testSharedConn != nil {
testSharedConn.Close()
testSharedConn = nil
}
testSharedDB = ""
testDoltServerPort = 0
os.Unsetenv("BEADS_DOLT_PORT")
testutil.TerminateDoltContainer()
}
}
// initCmdBDSharedSchema initializes the schema and config on the shared database
// and commits to main so branches get a clean snapshot.
func initCmdBDSharedSchema(port int) error {
ctx := context.Background()
cfg := &dolt.Config{
Path: "/tmp/cmdbd-shared-init",
ServerHost: "127.0.0.1",
ServerPort: port,
Database: testSharedDB,
}
store, err := dolt.New(ctx, cfg)
if err != nil {
return fmt.Errorf("New: %w", err)
}
defer store.Close()
if err := store.SetConfig(ctx, "issue_prefix", "test"); err != nil {
return fmt.Errorf("SetConfig(issue_prefix): %w", err)
}
if err := store.SetConfig(ctx, "types.custom", "molecule,gate,convoy,merge-request,slot,agent,role,rig,event,message"); err != nil {
return fmt.Errorf("SetConfig(types.custom): %w", err)
}
// Commit schema to main so branches get a clean snapshot
db := store.DB()
if _, err := db.ExecContext(ctx, "CALL DOLT_ADD('-A')"); err != nil {
return fmt.Errorf("DOLT_ADD: %w", err)
}
if _, err := db.ExecContext(ctx, "CALL DOLT_COMMIT('--allow-empty', '-m', 'test: init shared schema')"); err != nil {
return fmt.Errorf("DOLT_COMMIT: %w", err)
}
if err := testutil.MaterializeLocalTableSchemasForBranchTests(ctx, db); err != nil {
return fmt.Errorf("materialize local table schemas: %w", err)
}
return nil
}