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

393 lines
11 KiB
Go

package git
import (
"os"
"path/filepath"
"testing"
)
// makeJJPrimaryWorkspace creates a minimal jj primary workspace directory.
// The primary workspace has .jj/repo as a directory (not a file).
func makeJJPrimaryWorkspace(t *testing.T, dir string) {
t.Helper()
if err := os.MkdirAll(filepath.Join(dir, ".jj", "repo"), 0750); err != nil {
t.Fatalf("failed to create primary .jj/repo directory: %v", err)
}
}
// makeJJSecondaryWorkspace creates a minimal jj secondary workspace directory.
// The secondary workspace has .jj/repo as a file pointing to the primary's .jj/repo.
// repoTarget is the path to write into the .jj/repo file (relative or absolute).
func makeJJSecondaryWorkspace(t *testing.T, dir, repoTarget string) {
t.Helper()
if err := os.MkdirAll(filepath.Join(dir, ".jj"), 0750); err != nil {
t.Fatalf("failed to create secondary .jj directory: %v", err)
}
if err := os.WriteFile(filepath.Join(dir, ".jj", "repo"), []byte(repoTarget+"\n"), 0640); err != nil {
t.Fatalf("failed to write .jj/repo file: %v", err)
}
}
func TestIsJujutsuRepo(t *testing.T) {
// Save original directory
origDir, err := os.Getwd()
if err != nil {
t.Fatalf("Failed to get current directory: %v", err)
}
defer func() {
_ = os.Chdir(origDir)
ResetCaches()
}()
t.Run("not a jj repo", func(t *testing.T) {
tmpDir := t.TempDir()
if err := os.Chdir(tmpDir); err != nil {
t.Fatalf("Failed to chdir: %v", err)
}
ResetCaches()
if IsJujutsuRepo() {
t.Error("Expected IsJujutsuRepo() to return false for non-jj directory")
}
})
t.Run("jj repo root", func(t *testing.T) {
tmpDir := t.TempDir()
jjDir := filepath.Join(tmpDir, ".jj")
if err := os.Mkdir(jjDir, 0750); err != nil {
t.Fatalf("Failed to create .jj directory: %v", err)
}
if err := os.Chdir(tmpDir); err != nil {
t.Fatalf("Failed to chdir: %v", err)
}
ResetCaches()
if !IsJujutsuRepo() {
t.Error("Expected IsJujutsuRepo() to return true for jj repo root")
}
})
t.Run("jj repo subdirectory", func(t *testing.T) {
tmpDir := t.TempDir()
jjDir := filepath.Join(tmpDir, ".jj")
if err := os.Mkdir(jjDir, 0750); err != nil {
t.Fatalf("Failed to create .jj directory: %v", err)
}
subDir := filepath.Join(tmpDir, "src", "lib")
if err := os.MkdirAll(subDir, 0750); err != nil {
t.Fatalf("Failed to create subdirectory: %v", err)
}
if err := os.Chdir(subDir); err != nil {
t.Fatalf("Failed to chdir: %v", err)
}
ResetCaches()
if !IsJujutsuRepo() {
t.Error("Expected IsJujutsuRepo() to return true for jj repo subdirectory")
}
})
}
func TestIsColocatedJJGit(t *testing.T) {
// Save original directory
origDir, err := os.Getwd()
if err != nil {
t.Fatalf("Failed to get current directory: %v", err)
}
defer func() {
_ = os.Chdir(origDir)
ResetCaches()
}()
t.Run("jj only (not colocated)", func(t *testing.T) {
tmpDir := t.TempDir()
jjDir := filepath.Join(tmpDir, ".jj")
if err := os.Mkdir(jjDir, 0750); err != nil {
t.Fatalf("Failed to create .jj directory: %v", err)
}
if err := os.Chdir(tmpDir); err != nil {
t.Fatalf("Failed to chdir: %v", err)
}
ResetCaches()
if IsColocatedJJGit() {
t.Error("Expected IsColocatedJJGit() to return false for jj-only repo")
}
})
t.Run("not a repo", func(t *testing.T) {
tmpDir := t.TempDir()
if err := os.Chdir(tmpDir); err != nil {
t.Fatalf("Failed to chdir: %v", err)
}
ResetCaches()
if IsColocatedJJGit() {
t.Error("Expected IsColocatedJJGit() to return false for non-repo")
}
})
}
func TestGetJujutsuRoot(t *testing.T) {
// Save original directory
origDir, err := os.Getwd()
if err != nil {
t.Fatalf("Failed to get current directory: %v", err)
}
defer func() {
_ = os.Chdir(origDir)
ResetCaches()
}()
t.Run("not a jj repo", func(t *testing.T) {
tmpDir := t.TempDir()
if err := os.Chdir(tmpDir); err != nil {
t.Fatalf("Failed to chdir: %v", err)
}
ResetCaches()
_, err := GetJujutsuRoot()
if err == nil {
t.Error("Expected GetJujutsuRoot() to return error for non-jj directory")
}
})
t.Run("jj repo root", func(t *testing.T) {
tmpDir := t.TempDir()
// Resolve symlinks for comparison (macOS /var -> /private/var)
tmpDir, _ = filepath.EvalSymlinks(tmpDir)
jjDir := filepath.Join(tmpDir, ".jj")
if err := os.Mkdir(jjDir, 0750); err != nil {
t.Fatalf("Failed to create .jj directory: %v", err)
}
if err := os.Chdir(tmpDir); err != nil {
t.Fatalf("Failed to chdir: %v", err)
}
ResetCaches()
root, err := GetJujutsuRoot()
if err != nil {
t.Fatalf("GetJujutsuRoot() returned error: %v", err)
}
if root != tmpDir {
t.Errorf("Expected root %q, got %q", tmpDir, root)
}
})
t.Run("jj repo subdirectory", func(t *testing.T) {
tmpDir := t.TempDir()
// Resolve symlinks for comparison (macOS /var -> /private/var)
tmpDir, _ = filepath.EvalSymlinks(tmpDir)
jjDir := filepath.Join(tmpDir, ".jj")
if err := os.Mkdir(jjDir, 0750); err != nil {
t.Fatalf("Failed to create .jj directory: %v", err)
}
subDir := filepath.Join(tmpDir, "src", "lib")
if err := os.MkdirAll(subDir, 0750); err != nil {
t.Fatalf("Failed to create subdirectory: %v", err)
}
if err := os.Chdir(subDir); err != nil {
t.Fatalf("Failed to chdir: %v", err)
}
ResetCaches()
root, err := GetJujutsuRoot()
if err != nil {
t.Fatalf("GetJujutsuRoot() returned error: %v", err)
}
if root != tmpDir {
t.Errorf("Expected root %q, got %q", tmpDir, root)
}
})
}
// TestGetJujutsuRootStopsAtGitBoundary verifies that GetJujutsuRoot() stops
// walking up the directory tree when it encounters a .git directory, even when
// a .jj directory exists above the .git boundary. This prevents a plain git
// repo nested inside a JJ workspace from inheriting the parent JJ context.
func TestGetJujutsuRootStopsAtGitBoundary(t *testing.T) {
origDir, err := os.Getwd()
if err != nil {
t.Fatalf("Failed to get current directory: %v", err)
}
defer func() {
_ = os.Chdir(origDir)
ResetCaches()
}()
// Directory structure:
// tmpDir/
// +-- jj-parent/ <- has .jj/ (simulates a JJ workspace root)
// +-- nested-git/ <- has .git/ (plain git repo; no .jj at this level)
//
// When CWD is nested-git/, GetJujutsuRoot() must stop at the .git boundary
// and NOT walk up into jj-parent/ where .jj/ lives.
tmpDir := t.TempDir()
tmpDir, _ = filepath.EvalSymlinks(tmpDir) // resolve macOS /var -> /private/var
jjParent := filepath.Join(tmpDir, "jj-parent")
if err := os.MkdirAll(jjParent, 0750); err != nil {
t.Fatalf("Failed to create jj-parent: %v", err)
}
if err := os.Mkdir(filepath.Join(jjParent, ".jj"), 0750); err != nil {
t.Fatalf("Failed to create .jj: %v", err)
}
nestedGit := filepath.Join(jjParent, "nested-git")
if err := os.MkdirAll(nestedGit, 0750); err != nil {
t.Fatalf("Failed to create nested-git: %v", err)
}
if err := os.Mkdir(filepath.Join(nestedGit, ".git"), 0750); err != nil {
t.Fatalf("Failed to create .git: %v", err)
}
if err := os.Chdir(nestedGit); err != nil {
t.Fatalf("Failed to chdir to nested-git: %v", err)
}
ResetCaches()
root, err := GetJujutsuRoot()
if err == nil {
t.Errorf("Expected error: .git boundary should prevent walking to parent .jj, but got root: %q", root)
}
}
func TestIsJJSecondaryWorkspace(t *testing.T) {
origDir, err := os.Getwd()
if err != nil {
t.Fatalf("Failed to get current directory: %v", err)
}
defer func() {
_ = os.Chdir(origDir)
ResetCaches()
}()
t.Run("primary workspace (repo is directory)", func(t *testing.T) {
tmpDir := t.TempDir()
tmpDir, _ = filepath.EvalSymlinks(tmpDir)
makeJJPrimaryWorkspace(t, tmpDir)
if err := os.Chdir(tmpDir); err != nil {
t.Fatalf("Failed to chdir: %v", err)
}
ResetCaches()
if IsJJSecondaryWorkspace() {
t.Error("Expected IsJJSecondaryWorkspace() = false for primary workspace")
}
})
t.Run("secondary workspace (repo is file)", func(t *testing.T) {
tmpDir := t.TempDir()
tmpDir, _ = filepath.EvalSymlinks(tmpDir)
primaryDir := filepath.Join(tmpDir, "primary")
secondaryDir := filepath.Join(tmpDir, "secondary")
makeJJPrimaryWorkspace(t, primaryDir)
makeJJSecondaryWorkspace(t, secondaryDir, filepath.Join(primaryDir, ".jj", "repo"))
if err := os.Chdir(secondaryDir); err != nil {
t.Fatalf("Failed to chdir: %v", err)
}
ResetCaches()
if !IsJJSecondaryWorkspace() {
t.Error("Expected IsJJSecondaryWorkspace() = true for secondary workspace")
}
})
t.Run("not a jj repo", func(t *testing.T) {
tmpDir := t.TempDir()
if err := os.Chdir(tmpDir); err != nil {
t.Fatalf("Failed to chdir: %v", err)
}
ResetCaches()
if IsJJSecondaryWorkspace() {
t.Error("Expected IsJJSecondaryWorkspace() = false for non-jj directory")
}
})
}
func TestGetJJPrimaryWorkspaceRoot(t *testing.T) {
origDir, err := os.Getwd()
if err != nil {
t.Fatalf("Failed to get current directory: %v", err)
}
defer func() {
_ = os.Chdir(origDir)
ResetCaches()
}()
t.Run("secondary workspace with absolute path in repo file", func(t *testing.T) {
tmpDir := t.TempDir()
tmpDir, _ = filepath.EvalSymlinks(tmpDir)
primaryDir := filepath.Join(tmpDir, "primary")
secondaryDir := filepath.Join(tmpDir, "secondary")
makeJJPrimaryWorkspace(t, primaryDir)
// Write absolute path to primary's .jj/repo
makeJJSecondaryWorkspace(t, secondaryDir, filepath.Join(primaryDir, ".jj", "repo"))
if err := os.Chdir(secondaryDir); err != nil {
t.Fatalf("Failed to chdir: %v", err)
}
ResetCaches()
got, err := GetJJPrimaryWorkspaceRoot()
if err != nil {
t.Fatalf("GetJJPrimaryWorkspaceRoot() returned error: %v", err)
}
if got != primaryDir {
t.Errorf("Expected primary root %q, got %q", primaryDir, got)
}
})
t.Run("secondary workspace with relative path in repo file", func(t *testing.T) {
tmpDir := t.TempDir()
tmpDir, _ = filepath.EvalSymlinks(tmpDir)
primaryDir := filepath.Join(tmpDir, "primary")
secondaryDir := filepath.Join(tmpDir, "secondary")
makeJJPrimaryWorkspace(t, primaryDir)
// Write relative path: from secondary/.jj/ to primary/.jj/repo
// secondary/.jj/ -> ../../primary/.jj/repo
makeJJSecondaryWorkspace(t, secondaryDir, "../../primary/.jj/repo")
if err := os.Chdir(secondaryDir); err != nil {
t.Fatalf("Failed to chdir: %v", err)
}
ResetCaches()
got, err := GetJJPrimaryWorkspaceRoot()
if err != nil {
t.Fatalf("GetJJPrimaryWorkspaceRoot() returned error: %v", err)
}
if got != primaryDir {
t.Errorf("Expected primary root %q, got %q", primaryDir, got)
}
})
t.Run("primary workspace returns error", func(t *testing.T) {
tmpDir := t.TempDir()
tmpDir, _ = filepath.EvalSymlinks(tmpDir)
makeJJPrimaryWorkspace(t, tmpDir)
if err := os.Chdir(tmpDir); err != nil {
t.Fatalf("Failed to chdir: %v", err)
}
ResetCaches()
// GetJJPrimaryWorkspaceRoot reads .jj/repo as a file; in a primary workspace
// .jj/repo is a directory, so ReadFile should fail.
_, err := GetJJPrimaryWorkspaceRoot()
if err == nil {
t.Error("Expected error from GetJJPrimaryWorkspaceRoot() in primary workspace")
}
})
}