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

114 lines
3.4 KiB
Go

//go:build cgo
package doctor
import (
"context"
"database/sql"
"fmt"
"path/filepath"
"testing"
"time"
_ "github.com/go-sql-driver/mysql"
"github.com/steveyegge/beads/internal/storage/dolt"
"github.com/steveyegge/beads/internal/storage/doltutil"
"github.com/steveyegge/beads/internal/testutil"
"github.com/steveyegge/beads/internal/types"
)
// doctorTestServerPort returns the Dolt server port for doctor tests.
// Uses DoltContainerPortInt so the port is only non-zero when TestMain
// actually started a container, ignoring any external BEADS_DOLT_PORT.
func doctorTestServerPort() int {
return testutil.DoltContainerPortInt()
}
// newTestDoltStore creates a DoltStore for testing in the doctor package.
// Each test gets an isolated branch on the shared database for fast isolation.
func newTestDoltStore(t *testing.T, prefix string) *dolt.DoltStore {
t.Helper()
ctx := context.Background()
if testutil.DoltContainerCrashed() {
t.Skipf("Dolt test server crashed: %v", testutil.DoltContainerCrashError())
}
port := doctorTestServerPort()
if port == 0 {
t.Skip("Dolt test server not available, skipping")
}
// Open store with MaxOpenConns=1 (required for DOLT_CHECKOUT session affinity)
store, err := dolt.New(ctx, &dolt.Config{
Path: filepath.Join(t.TempDir(), "test.db"),
ServerHost: "127.0.0.1",
ServerPort: port,
Database: testSharedDB,
MaxOpenConns: 1,
})
if err != nil {
t.Skipf("skipping: Dolt not available: %v", err)
}
// Create isolated branch for this test on the store's own connection
_, branchCleanup := testutil.StartTestBranch(t, store.DB(), testSharedDB)
// Set prefix for this test (overrides the shared schema's default)
if err := store.SetConfig(ctx, "issue_prefix", prefix); err != nil {
branchCleanup()
store.Close()
t.Fatalf("Failed to set issue_prefix: %v", err)
}
t.Cleanup(func() {
branchCleanup()
store.Close()
})
return store
}
// dropDoctorTestDatabase drops a test database (best-effort cleanup).
func dropDoctorTestDatabase(dbName string, port int) {
dsn := doltutil.ServerDSN{Host: "127.0.0.1", Port: port, User: "root"}.String()
db, err := sql.Open("mysql", dsn)
if err != nil {
return
}
defer db.Close()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
//nolint:gosec // G201: dbName is generated by test (doctest_ + random hex)
_, _ = db.ExecContext(ctx, fmt.Sprintf("DROP DATABASE IF EXISTS `%s`", dbName))
// Purge dropped databases from Dolt's trash directory to reclaim disk space
_, _ = db.ExecContext(ctx, "CALL dolt_purge_dropped_databases()")
}
// newTestIssue creates a minimal test issue with the given ID.
func newTestIssue(id string) *types.Issue {
return &types.Issue{
ID: id,
Title: "Test issue " + id,
Status: types.StatusOpen,
Priority: 2,
IssueType: types.TypeTask,
CreatedAt: time.Now(),
}
}
// insertIssueDirectly inserts an issue with a pre-set ID into the dolt store.
// This simulates cross-rig contamination where foreign-prefix issues end up in the store.
func insertIssueDirectly(t *testing.T, store *dolt.DoltStore, id string) {
t.Helper()
ctx := context.Background()
issue := newTestIssue(id)
if err := store.CreateIssue(ctx, issue, "test"); err != nil {
t.Fatalf("failed to insert issue %s: %v", id, err)
}
}
// ptrTime returns a pointer to a time.Time value.
func ptrTime(t time.Time) *time.Time {
return &t
}