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

162 lines
5.9 KiB
Go

package schema
import (
"context"
"crypto/sha256"
"encoding/hex"
"os"
"path/filepath"
"strconv"
"testing"
"github.com/DATA-DOG/go-sqlmock"
"github.com/steveyegge/beads/internal/testutil"
)
// TestEnsureContentHashColumnAddsWhenMissing verifies the idempotent upgrade adds
// the content_hash column to an existing cursor table that lacks it.
func TestEnsureContentHashColumnAddsWhenMissing(t *testing.T) {
db, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("sqlmock.New: %v", err)
}
defer db.Close()
mock.ExpectQuery(`SELECT COUNT\(\*\) FROM INFORMATION_SCHEMA\.COLUMNS`).
WillReturnRows(sqlmock.NewRows([]string{"count"}).AddRow(0))
mock.ExpectExec(`ALTER TABLE schema_migrations ADD COLUMN content_hash CHAR\(64\)`).
WillReturnResult(sqlmock.NewResult(0, 0))
added, err := mainSource.ensureContentHashColumn(context.Background(), db)
if err != nil {
t.Fatalf("ensureContentHashColumn: %v", err)
}
if !added {
t.Fatal("ensureContentHashColumn added = false, want true when the column was missing")
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Fatalf("unmet expectations: %v", err)
}
}
// TestEnsureContentHashColumnNoOpWhenPresent verifies it issues no ALTER when the
// column already exists.
func TestEnsureContentHashColumnNoOpWhenPresent(t *testing.T) {
db, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("sqlmock.New: %v", err)
}
defer db.Close()
mock.ExpectQuery(`SELECT COUNT\(\*\) FROM INFORMATION_SCHEMA\.COLUMNS`).
WillReturnRows(sqlmock.NewRows([]string{"count"}).AddRow(1))
// No ExpectExec: an ALTER here would be an unexpected call.
added, err := mainSource.ensureContentHashColumn(context.Background(), db)
if err != nil {
t.Fatalf("ensureContentHashColumn: %v", err)
}
if added {
t.Fatal("ensureContentHashColumn added = true, want false when the column already exists")
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Fatalf("unmet expectations: %v", err)
}
}
// TestAllMigrationsSQLRecordsContentHashes applies the full migration bundle
// through the dolt CLI and verifies every recorded migration carries the SHA-256
// of its migration file content (gastownhall/beads#4259 reporter fix No.2).
func TestAllMigrationsSQLRecordsContentHashes(t *testing.T) {
testutil.RequireDoltBinary(t)
dir := filepath.Join(t.TempDir(), "hash-bundle")
if err := os.MkdirAll(dir, 0o755); err != nil {
t.Fatalf("create bundle dir: %v", err)
}
runDoltCommand(t, dir, "init", "--name", "test", "--email", "test@example.com")
runDoltSQL(t, dir, AllMigrationsSQL())
got := map[string]string{}
for _, r := range queryDoltCSV(t, dir, `SELECT version, content_hash FROM schema_migrations`) {
got[r["version"]] = r["content_hash"]
}
for _, mf := range mainSource.list() {
data, err := mainSource.files.ReadFile(mainSource.dir + "/" + mf.name)
if err != nil {
t.Fatalf("read migration %s: %v", mf.name, err)
}
sum := sha256.Sum256(data)
want := hex.EncodeToString(sum[:])
if h := got[strconv.Itoa(mf.version)]; h != want {
t.Errorf("version %d content_hash = %q, want %q (sha256 of %s)", mf.version, h, want, mf.name)
}
}
}
// TestMigrationWorkNeededAddsContentHashColumnOnUpToDateDB is the regression for
// the silent no-op (gastownhall/beads#4259 reporter fix No.2 review): a database
// already at the latest migration version that predates the content_hash column
// must still report work needed, so MigrateUp runs migrate()'s idempotent ALTER
// instead of short-circuiting and leaving the recording/detection surface
// uninstalled.
func TestMigrationWorkNeededAddsContentHashColumnOnUpToDateDB(t *testing.T) {
db, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("sqlmock.New: %v", err)
}
defer db.Close()
// Both sources are already at their latest version...
expectScalar(mock, "SELECT COALESCE(MAX(version), 0) FROM schema_migrations", "version", LatestVersion())
expectScalar(mock, "SELECT COALESCE(MAX(version), 0) FROM ignored_schema_migrations", "version", LatestIgnoredVersion())
// ...but schema_migrations predates the content_hash column.
mock.ExpectQuery(`SELECT COUNT\(\*\) FROM INFORMATION_SCHEMA\.COLUMNS`).
WillReturnRows(sqlmock.NewRows([]string{"count"}).AddRow(0))
needed, err := migrationWorkNeeded(context.Background(), db)
if err != nil {
t.Fatalf("migrationWorkNeeded: %v", err)
}
if !needed {
t.Fatal("migrationWorkNeeded = false, want true when the content_hash column is missing")
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Fatalf("unmet expectations: %v", err)
}
}
// TestMigrationWorkNotNeededWhenContentHashColumnsPresent locks the probe's
// ordering: an up-to-date database that already has the content_hash columns and
// needs no backfill must not report work, so MigrateUp does not re-run the ALTER
// (or stage/commit) on every open.
func TestMigrationWorkNotNeededWhenContentHashColumnsPresent(t *testing.T) {
db, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("sqlmock.New: %v", err)
}
defer db.Close()
expectScalar(mock, "SELECT COALESCE(MAX(version), 0) FROM schema_migrations", "version", LatestVersion())
expectScalar(mock, "SELECT COALESCE(MAX(version), 0) FROM ignored_schema_migrations", "version", LatestIgnoredVersion())
mock.ExpectQuery(`SELECT COUNT\(\*\) FROM INFORMATION_SCHEMA\.COLUMNS`).
WillReturnRows(sqlmock.NewRows([]string{"count"}).AddRow(1))
mock.ExpectQuery(`SELECT COUNT\(\*\) FROM INFORMATION_SCHEMA\.COLUMNS`).
WillReturnRows(sqlmock.NewRows([]string{"count"}).AddRow(1))
// No backfill pending (custom tables already populated).
expectScalar(mock, "SELECT COUNT(*) FROM custom_types", "count", 1)
expectScalar(mock, "SELECT COUNT(*) FROM custom_statuses", "count", 1)
needed, err := migrationWorkNeeded(context.Background(), db)
if err != nil {
t.Fatalf("migrationWorkNeeded: %v", err)
}
if needed {
t.Fatal("migrationWorkNeeded = true, want false when columns are present and no backfill is pending")
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Fatalf("unmet expectations: %v", err)
}
}