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>
455 lines
14 KiB
Go
455 lines
14 KiB
Go
package dolt
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
_ "github.com/go-sql-driver/mysql"
|
|
"github.com/steveyegge/beads/internal/storage/doltutil"
|
|
)
|
|
|
|
// Tests for the CreateIfMissing guard on CREATE DATABASE.
|
|
//
|
|
// REQUIREMENTS (from shadow database bug analysis):
|
|
// 1. Normal bd operations (list, show, ready, etc.) must NOT create databases.
|
|
// If the database doesn't exist on the server, error out with a clear message.
|
|
// 2. Only bd init should create databases (CreateIfMissing=true).
|
|
// 3. When a configured server is unreachable and an explicit port is set,
|
|
// auto-start must NOT launch a different server.
|
|
// 4. Existing databases with data must remain accessible after server restart.
|
|
// 5. When connecting to a server that lacks the expected database, the error
|
|
// message must clearly indicate the database was not found (not a generic
|
|
// connection error).
|
|
// 6. ReadOnly mode with a missing database must still error (not silently skip).
|
|
//
|
|
// NOTE: TLS/Hosted Dolt paths are not separately tested here — the guard logic
|
|
// is DSN-agnostic (TLS only affects connection parameters, not the SHOW DATABASES
|
|
// or CREATE DATABASE flow).
|
|
|
|
// --- DRY test helpers ---
|
|
|
|
// rawTestConn opens a raw MySQL connection to the test server without selecting
|
|
// a database. Caller must defer db.Close().
|
|
func rawTestConn(t *testing.T, port int) *sql.DB {
|
|
t.Helper()
|
|
dsn := doltutil.ServerDSN{Host: "127.0.0.1", Port: port, User: "root"}.String()
|
|
db, err := sql.Open("mysql", dsn)
|
|
if err != nil {
|
|
t.Fatalf("failed to connect to test server on port %d: %v", port, err)
|
|
}
|
|
return db
|
|
}
|
|
|
|
func databaseExists(t *testing.T, port int, dbName string) bool {
|
|
t.Helper()
|
|
db := rawTestConn(t, port)
|
|
defer db.Close()
|
|
|
|
// Use SHOW DATABASES + iterate for exact match (not LIKE, which treats
|
|
// underscores as wildcards and Dolt doesn't support backslash escaping).
|
|
rows, err := db.Query("SHOW DATABASES")
|
|
if err != nil {
|
|
t.Fatalf("failed to list databases: %v", err)
|
|
}
|
|
defer rows.Close()
|
|
|
|
for rows.Next() {
|
|
var name string
|
|
if err := rows.Scan(&name); err != nil {
|
|
t.Fatalf("failed to scan database name: %v", err)
|
|
}
|
|
if name == dbName {
|
|
return true
|
|
}
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
t.Fatalf("error iterating databases: %v", err)
|
|
}
|
|
return false
|
|
}
|
|
|
|
func assertDatabaseExists(t *testing.T, port int, dbName string) {
|
|
t.Helper()
|
|
if !databaseExists(t, port, dbName) {
|
|
t.Fatalf("expected database %q to exist on port %d", dbName, port)
|
|
}
|
|
}
|
|
|
|
func assertDatabaseNotExists(t *testing.T, port int, dbName string) {
|
|
t.Helper()
|
|
if databaseExists(t, port, dbName) {
|
|
t.Fatalf("expected database %q to NOT exist on port %d", dbName, port)
|
|
}
|
|
}
|
|
|
|
func createTestDatabase(t *testing.T, port int, dbName string) {
|
|
t.Helper()
|
|
db := rawTestConn(t, port)
|
|
defer db.Close()
|
|
|
|
if _, err := db.Exec(fmt.Sprintf("CREATE DATABASE `%s`", dbName)); err != nil {
|
|
t.Fatalf("failed to create database %q: %v", dbName, err)
|
|
}
|
|
}
|
|
|
|
func dropTestDatabase(t *testing.T, port int, dbName string) {
|
|
// No-op: rapid DROP DATABASE crashes the Dolt test container.
|
|
// Orphan databases are cleaned up when the container terminates.
|
|
_ = port
|
|
_ = dbName
|
|
}
|
|
|
|
func containsAny(s string, substrs ...string) bool {
|
|
lower := strings.ToLower(s)
|
|
for _, sub := range substrs {
|
|
if strings.Contains(lower, strings.ToLower(sub)) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// skipIfNoServer skips the test if the shared test Dolt server is not running.
|
|
// Acquires a semaphore slot (released via t.Cleanup) to limit container load.
|
|
func skipIfNoServer(t *testing.T) {
|
|
t.Helper()
|
|
if testServerPort == 0 {
|
|
t.Skip("no test Dolt server running")
|
|
}
|
|
acquireTestSlot()
|
|
t.Cleanup(releaseTestSlot)
|
|
}
|
|
|
|
// --- Guard tests ---
|
|
|
|
// TestCreateGuard_MissingDB_DefaultConfig verifies that opening a store
|
|
// against a non-existent database FAILS when CreateIfMissing is false (default).
|
|
// This is the PRIMARY regression test for the shadow database bug.
|
|
func TestCreateGuard_MissingDB_DefaultConfig(t *testing.T) {
|
|
skipIfNoServer(t)
|
|
|
|
ctx := context.Background()
|
|
dbName := fmt.Sprintf("test_guard_missing_%d", testServerPort)
|
|
|
|
assertDatabaseNotExists(t, testServerPort, dbName)
|
|
|
|
cfg := &Config{
|
|
Path: t.TempDir(),
|
|
ServerHost: "127.0.0.1",
|
|
ServerPort: testServerPort,
|
|
Database: dbName,
|
|
MaxOpenConns: 1,
|
|
// CreateIfMissing is NOT set — defaults to false
|
|
}
|
|
|
|
_, err := New(ctx, cfg)
|
|
if err == nil {
|
|
t.Fatal("expected error when database doesn't exist and CreateIfMissing=false, got nil")
|
|
}
|
|
|
|
if !containsAny(err.Error(), "not found", "does not exist", "unknown database") {
|
|
t.Errorf("error should indicate database not found, got: %v", err)
|
|
}
|
|
|
|
// Verify the database was NOT created as a side effect
|
|
assertDatabaseNotExists(t, testServerPort, dbName)
|
|
}
|
|
|
|
// TestCreateGuard_MissingDB_CreateIfMissing verifies that bd init CAN create
|
|
// a new database when CreateIfMissing=true. This ensures the fix doesn't
|
|
// break the init flow.
|
|
func TestCreateGuard_MissingDB_CreateIfMissing(t *testing.T) {
|
|
skipIfNoServer(t)
|
|
|
|
ctx := context.Background()
|
|
dbName := fmt.Sprintf("test_guard_create_%d", testServerPort)
|
|
|
|
assertDatabaseNotExists(t, testServerPort, dbName)
|
|
t.Cleanup(func() { dropTestDatabase(t, testServerPort, dbName) })
|
|
|
|
cfg := &Config{
|
|
Path: t.TempDir(),
|
|
ServerHost: "127.0.0.1",
|
|
ServerPort: testServerPort,
|
|
Database: dbName,
|
|
MaxOpenConns: 1,
|
|
CreateIfMissing: true,
|
|
}
|
|
|
|
store, err := New(ctx, cfg)
|
|
if err != nil {
|
|
t.Fatalf("expected success when CreateIfMissing=true, got: %v", err)
|
|
}
|
|
defer store.Close()
|
|
|
|
assertDatabaseExists(t, testServerPort, dbName)
|
|
}
|
|
|
|
// TestCreateGuard_ExistingDB_NoFlag verifies that opening an EXISTING database
|
|
// succeeds even when CreateIfMissing=false. Normal happy path.
|
|
func TestCreateGuard_ExistingDB_NoFlag(t *testing.T) {
|
|
skipIfNoServer(t)
|
|
|
|
ctx := context.Background()
|
|
dbName := fmt.Sprintf("test_guard_existing_%d", testServerPort)
|
|
|
|
createTestDatabase(t, testServerPort, dbName)
|
|
t.Cleanup(func() { dropTestDatabase(t, testServerPort, dbName) })
|
|
|
|
cfg := &Config{
|
|
Path: t.TempDir(),
|
|
ServerHost: "127.0.0.1",
|
|
ServerPort: testServerPort,
|
|
Database: dbName,
|
|
MaxOpenConns: 1,
|
|
}
|
|
|
|
store, err := New(ctx, cfg)
|
|
if err != nil {
|
|
t.Fatalf("expected success opening existing database, got: %v", err)
|
|
}
|
|
defer store.Close()
|
|
}
|
|
|
|
// TestCreateGuard_ExistingDB_DoesNotCreateMissingBeadsDir verifies that a
|
|
// normal write-capable store open does not materialize a missing .beads/
|
|
// directory as a side effect. Only explicit init/bootstrap-style flows should
|
|
// create local filesystem state.
|
|
func TestCreateGuard_ExistingDB_DoesNotCreateMissingBeadsDir(t *testing.T) {
|
|
skipIfNoServer(t)
|
|
|
|
ctx := context.Background()
|
|
baseDir := t.TempDir()
|
|
beadsDir := filepath.Join(baseDir, ".beads")
|
|
dbPath := filepath.Join(beadsDir, "dolt")
|
|
dbName := fmt.Sprintf("test_guard_missing_beads_%d", testServerPort)
|
|
|
|
createTestDatabase(t, testServerPort, dbName)
|
|
t.Cleanup(func() { dropTestDatabase(t, testServerPort, dbName) })
|
|
|
|
cfg := &Config{
|
|
Path: dbPath,
|
|
BeadsDir: beadsDir,
|
|
ServerHost: "127.0.0.1",
|
|
ServerPort: testServerPort,
|
|
Database: dbName,
|
|
MaxOpenConns: 1,
|
|
}
|
|
|
|
store, err := New(ctx, cfg)
|
|
if err != nil {
|
|
t.Fatalf("expected success opening existing database, got: %v", err)
|
|
}
|
|
defer store.Close()
|
|
|
|
if _, err := os.Stat(beadsDir); !os.IsNotExist(err) {
|
|
t.Fatalf("expected missing beadsDir to stay absent, got err=%v", err)
|
|
}
|
|
if _, err := os.Stat(filepath.Join(beadsDir, credentialKeyFile)); !os.IsNotExist(err) {
|
|
t.Fatalf("expected no credential key side effect, got err=%v", err)
|
|
}
|
|
if _, err := os.Stat(filepath.Join(beadsDir, "dolt-server.port")); !os.IsNotExist(err) {
|
|
t.Fatalf("expected no port-file side effect, got err=%v", err)
|
|
}
|
|
}
|
|
|
|
// TestCreateGuard_ExistingDB_WithData verifies that data is preserved when
|
|
// reconnecting to an existing database. Simulates server restart scenario.
|
|
func TestCreateGuard_ExistingDB_WithData(t *testing.T) {
|
|
skipIfNoServer(t)
|
|
|
|
ctx := context.Background()
|
|
dbName := fmt.Sprintf("test_guard_data_%d", testServerPort)
|
|
|
|
createTestDatabase(t, testServerPort, dbName)
|
|
t.Cleanup(func() { dropTestDatabase(t, testServerPort, dbName) })
|
|
|
|
// First connection: create store and write data
|
|
store1, err := New(ctx, &Config{
|
|
Path: t.TempDir(),
|
|
ServerHost: "127.0.0.1",
|
|
ServerPort: testServerPort,
|
|
Database: dbName,
|
|
MaxOpenConns: 1,
|
|
CreateIfMissing: true, // init path
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("first connection failed: %v", err)
|
|
}
|
|
|
|
if err := store1.SetConfig(ctx, "test_key", "test_value"); err != nil {
|
|
t.Fatalf("SetConfig failed: %v", err)
|
|
}
|
|
store1.Close()
|
|
|
|
// Second connection: open WITHOUT CreateIfMissing, verify data persists
|
|
store2, err := New(ctx, &Config{
|
|
Path: t.TempDir(),
|
|
ServerHost: "127.0.0.1",
|
|
ServerPort: testServerPort,
|
|
Database: dbName,
|
|
MaxOpenConns: 1,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("second connection failed: %v", err)
|
|
}
|
|
defer store2.Close()
|
|
|
|
val, err := store2.GetConfig(ctx, "test_key")
|
|
if err != nil {
|
|
t.Fatalf("GetConfig failed: %v", err)
|
|
}
|
|
if val != "test_value" {
|
|
t.Errorf("data not preserved: got %q, want %q", val, "test_value")
|
|
}
|
|
}
|
|
|
|
// TestCreateGuard_ReadOnly_MissingDB verifies that ReadOnly mode with a missing
|
|
// database still errors (doesn't silently skip the guard).
|
|
func TestCreateGuard_ReadOnly_MissingDB(t *testing.T) {
|
|
skipIfNoServer(t)
|
|
|
|
ctx := context.Background()
|
|
dbName := fmt.Sprintf("test_guard_readonly_%d", testServerPort)
|
|
assertDatabaseNotExists(t, testServerPort, dbName)
|
|
|
|
cfg := &Config{
|
|
Path: t.TempDir(),
|
|
ServerHost: "127.0.0.1",
|
|
ServerPort: testServerPort,
|
|
Database: dbName,
|
|
MaxOpenConns: 1,
|
|
ReadOnly: true,
|
|
// CreateIfMissing=false (default)
|
|
}
|
|
|
|
_, err := New(ctx, cfg)
|
|
if err == nil {
|
|
t.Fatal("expected error when database doesn't exist in ReadOnly mode, got nil")
|
|
}
|
|
|
|
if !containsAny(err.Error(), "not found", "does not exist") {
|
|
t.Errorf("error should indicate database not found, got: %v", err)
|
|
}
|
|
|
|
assertDatabaseNotExists(t, testServerPort, dbName)
|
|
}
|
|
|
|
// TestCreateGuard_UnderscoreInName verifies that the exact-match existence check
|
|
// works correctly for database names containing underscores (common in beads
|
|
// naming: "beads_vulcan-clean"). A similar-named database must not cause a
|
|
// false positive.
|
|
func TestCreateGuard_UnderscoreInName(t *testing.T) {
|
|
skipIfNoServer(t)
|
|
|
|
ctx := context.Background()
|
|
// Create a database with a name that would match if _ is treated as wildcard
|
|
similarName := fmt.Sprintf("test1guard1underscore1%d", testServerPort)
|
|
targetName := fmt.Sprintf("test_guard_underscore_%d", testServerPort)
|
|
|
|
createTestDatabase(t, testServerPort, similarName)
|
|
t.Cleanup(func() { dropTestDatabase(t, testServerPort, similarName) })
|
|
|
|
// Target database does NOT exist — only the similar-named one does
|
|
assertDatabaseNotExists(t, testServerPort, targetName)
|
|
|
|
cfg := &Config{
|
|
Path: t.TempDir(),
|
|
ServerHost: "127.0.0.1",
|
|
ServerPort: testServerPort,
|
|
Database: targetName,
|
|
MaxOpenConns: 1,
|
|
}
|
|
|
|
_, err := New(ctx, cfg)
|
|
if err == nil {
|
|
t.Fatal("expected error: target DB doesn't exist, only a similar-named one does")
|
|
}
|
|
|
|
if !containsAny(err.Error(), "not found") {
|
|
t.Errorf("error should indicate database not found, got: %v", err)
|
|
}
|
|
}
|
|
|
|
// TestCreateGuard_UnderscoreBothExist verifies that when BOTH a similar-named
|
|
// database and the target database exist, the exact-match existence check
|
|
// correctly identifies the target. Uses CreateIfMissing=true because this
|
|
// test focuses on the existence check accuracy, not the guard itself.
|
|
func TestCreateGuard_UnderscoreBothExist(t *testing.T) {
|
|
skipIfNoServer(t)
|
|
|
|
ctx := context.Background()
|
|
similarName := fmt.Sprintf("test1guard1both1%d", testServerPort)
|
|
targetName := fmt.Sprintf("test_guard_both_%d", testServerPort)
|
|
|
|
createTestDatabase(t, testServerPort, similarName)
|
|
t.Cleanup(func() {
|
|
dropTestDatabase(t, testServerPort, similarName)
|
|
dropTestDatabase(t, testServerPort, targetName)
|
|
})
|
|
|
|
// Open targetName with CreateIfMissing — this tests that the LIKE escaping
|
|
// doesn't false-match similarName and incorrectly report the DB as existing
|
|
// when it isn't the right one.
|
|
cfg := &Config{
|
|
Path: t.TempDir(),
|
|
ServerHost: "127.0.0.1",
|
|
ServerPort: testServerPort,
|
|
Database: targetName,
|
|
MaxOpenConns: 1,
|
|
CreateIfMissing: true,
|
|
}
|
|
|
|
store, err := New(ctx, cfg)
|
|
if err != nil {
|
|
t.Fatalf("expected success creating target DB when similar-named DB exists, got: %v", err)
|
|
}
|
|
defer store.Close()
|
|
|
|
// Store opened successfully — the LIKE query matched the correct target
|
|
// (or created it) despite similarName also existing. The equality check
|
|
// (existingName == cfg.Database) prevents false matches from LIKE wildcards.
|
|
}
|
|
|
|
// TestCreateGuard_ErrorMessage verifies the error message is clear and
|
|
// actionable when the database is not found.
|
|
func TestCreateGuard_ErrorMessage(t *testing.T) {
|
|
skipIfNoServer(t)
|
|
|
|
ctx := context.Background()
|
|
dbName := fmt.Sprintf("test_guard_errmsg_%d", testServerPort)
|
|
assertDatabaseNotExists(t, testServerPort, dbName)
|
|
|
|
cfg := &Config{
|
|
Path: t.TempDir(),
|
|
ServerHost: "127.0.0.1",
|
|
ServerPort: testServerPort,
|
|
Database: dbName,
|
|
MaxOpenConns: 1,
|
|
}
|
|
|
|
_, err := New(ctx, cfg)
|
|
if err == nil {
|
|
t.Fatal("expected error, got nil")
|
|
}
|
|
|
|
errMsg := err.Error()
|
|
// Should mention the database name
|
|
if !strings.Contains(errMsg, dbName) {
|
|
t.Errorf("error should mention database name %q, got: %s", dbName, errMsg)
|
|
}
|
|
// Should suggest bd init
|
|
if !strings.Contains(errMsg, "bd init") {
|
|
t.Errorf("error should suggest 'bd init', got: %s", errMsg)
|
|
}
|
|
// Should suggest bd doctor
|
|
if !strings.Contains(errMsg, "bd doctor") {
|
|
t.Errorf("error should suggest 'bd doctor', got: %s", errMsg)
|
|
}
|
|
}
|