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

215 lines
6.3 KiB
Go

package molecules
import (
"context"
"os"
"path/filepath"
"testing"
"github.com/steveyegge/beads/internal/storage"
"github.com/steveyegge/beads/internal/storage/dolt"
"github.com/steveyegge/beads/internal/testutil"
"github.com/steveyegge/beads/internal/types"
)
// newTestMoleculeStore creates a dolt store on the shared database with branch isolation.
func newTestMoleculeStore(t *testing.T) *dolt.DoltStore {
t.Helper()
if testSharedDB == "" {
t.Skip("shared DB not available")
}
ctx := context.Background()
store, err := dolt.New(ctx, &dolt.Config{
Path: t.TempDir(),
ServerHost: "127.0.0.1",
ServerPort: testServerPort,
Database: testSharedDB,
MaxOpenConns: 1,
})
if err != nil {
t.Fatalf("Failed to create dolt store: %v", err)
}
_, branchCleanup := testutil.StartTestBranch(t, store.DB(), testSharedDB)
t.Cleanup(func() {
branchCleanup()
store.Close()
})
return store
}
func TestLoadMoleculesFromFile(t *testing.T) {
// Create a temporary directory
tempDir := t.TempDir()
// Create a test molecules.jsonl file
moleculesPath := filepath.Join(tempDir, "molecules.jsonl")
content := `{"id":"mol-test-1","title":"Test Molecule 1","issue_type":"molecule","status":"open"}
{"id":"mol-test-2","title":"Test Molecule 2","issue_type":"molecule","status":"open"}`
if err := os.WriteFile(moleculesPath, []byte(content), 0600); err != nil {
t.Fatalf("Failed to write test file: %v", err)
}
// Load molecules
molecules, err := loadMoleculesFromFile(moleculesPath)
if err != nil {
t.Fatalf("Failed to load molecules: %v", err)
}
if len(molecules) != 2 {
t.Errorf("Expected 2 molecules, got %d", len(molecules))
}
// Check that IsTemplate is set
for _, mol := range molecules {
if !mol.IsTemplate {
t.Errorf("Molecule %s should have IsTemplate=true", mol.ID)
}
}
// Check specific fields
if molecules[0].ID != "mol-test-1" {
t.Errorf("Expected ID 'mol-test-1', got '%s'", molecules[0].ID)
}
if molecules[0].Title != "Test Molecule 1" {
t.Errorf("Expected Title 'Test Molecule 1', got '%s'", molecules[0].Title)
}
}
func TestLoadMoleculesFromNonexistentFile(t *testing.T) {
molecules, err := loadMoleculesFromFile("/nonexistent/path/molecules.jsonl")
if err != nil {
t.Errorf("Expected nil error for nonexistent file, got: %v", err)
}
if molecules != nil {
t.Errorf("Expected nil molecules for nonexistent file, got: %v", molecules)
}
}
func TestLoader_LoadAll(t *testing.T) {
ctx := context.Background()
store := newTestMoleculeStore(t)
// Create temporary directories
tempDir := t.TempDir()
beadsDir := filepath.Join(tempDir, ".beads")
if err := os.MkdirAll(beadsDir, 0750); err != nil {
t.Fatalf("Failed to create beads dir: %v", err)
}
// Create a project-level molecules.jsonl
moleculesPath := filepath.Join(beadsDir, "molecules.jsonl")
content := `{"id":"mol-feature","title":"Feature Template","issue_type":"molecule","status":"open","description":"Standard feature workflow"}
{"id":"mol-bugfix","title":"Bugfix Template","issue_type":"molecule","status":"open","description":"Bug fix workflow"}`
if err := os.WriteFile(moleculesPath, []byte(content), 0600); err != nil {
t.Fatalf("Failed to write molecules file: %v", err)
}
// Load molecules
loader := NewLoader(store)
result, err := loader.LoadAll(ctx, beadsDir)
if err != nil {
t.Fatalf("LoadAll failed: %v", err)
}
if result.Loaded != 2 {
t.Errorf("Expected 2 loaded molecules, got %d", result.Loaded)
}
// Verify molecules are in the database
mol1, err := store.GetIssue(ctx, "mol-feature")
if err != nil {
t.Fatalf("Failed to get mol-feature: %v", err)
}
if mol1 == nil {
t.Fatal("mol-feature not found in database")
}
if !mol1.IsTemplate {
t.Error("mol-feature should be marked as template")
}
if mol1.Title != "Feature Template" {
t.Errorf("Expected title 'Feature Template', got '%s'", mol1.Title)
}
mol2, err := store.GetIssue(ctx, "mol-bugfix")
if err != nil {
t.Fatalf("Failed to get mol-bugfix: %v", err)
}
if mol2 == nil {
t.Fatal("mol-bugfix not found in database")
}
if !mol2.IsTemplate {
t.Error("mol-bugfix should be marked as template")
}
}
func TestLoader_SkipExistingMolecules(t *testing.T) {
ctx := context.Background()
store := newTestMoleculeStore(t)
// Create temporary directories
tempDir := t.TempDir()
beadsDir := filepath.Join(tempDir, ".beads")
if err := os.MkdirAll(beadsDir, 0750); err != nil {
t.Fatalf("Failed to create beads dir: %v", err)
}
// Pre-create a molecule in the database (skip prefix validation for mol-* IDs)
existingMol := &types.Issue{
ID: "mol-existing",
Title: "Existing Molecule",
IssueType: "molecule",
Status: types.StatusOpen,
IsTemplate: true,
}
opts := storage.BatchCreateOptions{SkipPrefixValidation: true, OrphanHandling: storage.OrphanAllow}
if err := store.CreateIssuesWithFullOptions(ctx, []*types.Issue{existingMol}, "test", opts); err != nil {
t.Fatalf("Failed to create existing molecule: %v", err)
}
// Create a molecules.jsonl with the same ID
moleculesPath := filepath.Join(beadsDir, "molecules.jsonl")
content := `{"id":"mol-existing","title":"Updated Molecule","issue_type":"molecule","status":"open"}
{"id":"mol-new","title":"New Molecule","issue_type":"molecule","status":"open"}`
if err := os.WriteFile(moleculesPath, []byte(content), 0600); err != nil {
t.Fatalf("Failed to write molecules file: %v", err)
}
// Load molecules
loader := NewLoader(store)
result, err := loader.LoadAll(ctx, beadsDir)
if err != nil {
t.Fatalf("LoadAll failed: %v", err)
}
// Should only load the new one (existing one is skipped)
if result.Loaded != 1 {
t.Errorf("Expected 1 loaded molecule, got %d", result.Loaded)
}
// Verify the existing molecule wasn't updated
mol, err := store.GetIssue(ctx, "mol-existing")
if err != nil {
t.Fatalf("Failed to get mol-existing: %v", err)
}
if mol.Title != "Existing Molecule" {
t.Errorf("Expected title 'Existing Molecule' (unchanged), got '%s'", mol.Title)
}
}
func TestGetBuiltinMolecules(t *testing.T) {
molecules := getBuiltinMolecules()
// For now, we expect no built-in molecules (can be added later)
if molecules == nil {
// This is expected for now
return
}
// When built-in molecules are added, verify they all have IsTemplate=true
for _, mol := range molecules {
if !mol.IsTemplate {
t.Errorf("Built-in molecule %s should have IsTemplate=true", mol.ID)
}
}
}