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

174 lines
5.1 KiB
Go

//go:build cgo
package main
import (
"context"
"os"
"path/filepath"
"testing"
"github.com/spf13/cobra"
"github.com/steveyegge/beads/internal/configfile"
"github.com/steveyegge/beads/internal/storage"
"github.com/steveyegge/beads/internal/types"
"github.com/steveyegge/beads/internal/utils"
)
func TestWithStorage_ReopensUsingMetadata(t *testing.T) {
ctx := context.Background()
testDBPath := filepath.Join(t.TempDir(), "dolt")
newTestStoreIsolatedDB(t, testDBPath, "cfg")
var gotPrefix string
err := withStorage(ctx, nil, testDBPath, func(s storage.DoltStorage) error {
var err error
gotPrefix, err = s.GetConfig(ctx, "issue_prefix")
return err
})
if err != nil {
t.Fatalf("withStorage() error = %v", err)
}
if gotPrefix != "cfg" {
t.Fatalf("issue_prefix = %q, want %q", gotPrefix, "cfg")
}
}
func TestResolveBeadsDirForDBPath_UsesRawBeadsDirForSymlinkedDBPath(t *testing.T) {
repoDir := t.TempDir()
beadsDir := filepath.Join(repoDir, ".beads")
actualDBPath := filepath.Join(repoDir, "external-dolt")
linkDBPath := filepath.Join(beadsDir, "dolt")
if err := os.MkdirAll(beadsDir, 0o755); err != nil {
t.Fatalf("mkdir beads dir: %v", err)
}
if err := os.MkdirAll(actualDBPath, 0o755); err != nil {
t.Fatalf("mkdir external dolt dir: %v", err)
}
if err := os.Symlink(actualDBPath, linkDBPath); err != nil {
t.Fatalf("symlink db path: %v", err)
}
cfg := &configfile.Config{
Database: "dolt",
Backend: configfile.BackendDolt,
}
if err := cfg.Save(beadsDir); err != nil {
t.Fatalf("save metadata: %v", err)
}
if got := resolveBeadsDirForDBPath(linkDBPath); !utils.PathsEqual(got, beadsDir) {
t.Fatalf("resolveBeadsDirForDBPath(%q) = %q, want %q", linkDBPath, got, beadsDir)
}
}
func TestIssueIDCompletion_UsesMetadataWhenStoreNil(t *testing.T) {
originalStore := store
originalDBPath := dbPath
originalRootCtx := rootCtx
defer func() {
store = originalStore
dbPath = originalDBPath
rootCtx = originalRootCtx
}()
ctx := context.Background()
rootCtx = ctx
testDBPath := filepath.Join(t.TempDir(), "dolt")
testStore := newTestStoreIsolatedDB(t, testDBPath, "cfg")
if err := testStore.CreateIssue(ctx, &types.Issue{
ID: "cfg-abc1",
Title: "Completion target",
Status: types.StatusOpen,
Priority: 1,
IssueType: types.TypeTask,
}, "test"); err != nil {
t.Fatalf("CreateIssue: %v", err)
}
store = nil
dbPath = testDBPath
completions, directive := issueIDCompletion(&cobra.Command{}, nil, "cfg-a")
if directive != cobra.ShellCompDirectiveNoFileComp {
t.Fatalf("directive = %d, want %d", directive, cobra.ShellCompDirectiveNoFileComp)
}
if len(completions) != 1 {
t.Fatalf("len(completions) = %d, want 1 (%v)", len(completions), completions)
}
if len(completions[0]) < len("cfg-abc1") || completions[0][:len("cfg-abc1")] != "cfg-abc1" {
t.Fatalf("completion = %q, want prefix %q", completions[0], "cfg-abc1")
}
}
func TestResolveCommandBeadsDir_NoCWDFallbackForExplicitPath(t *testing.T) {
// Set up project A with metadata so FindBeadsDir() discovers it from CWD.
projectA := t.TempDir()
beadsDirA := filepath.Join(projectA, ".beads")
if err := os.MkdirAll(filepath.Join(beadsDirA, "dolt"), 0o755); err != nil {
t.Fatalf("mkdir beads dir A: %v", err)
}
cfgA := &configfile.Config{
Database: "dolt",
Backend: configfile.BackendDolt,
DoltDatabase: "project_a_db",
}
if err := cfgA.Save(beadsDirA); err != nil {
t.Fatalf("save metadata A: %v", err)
}
// Project B: .beads/dolt exists but metadata.json is missing.
// This triggers the bug: filepath.Dir(dbPath) gives the correct
// .beads dir but configfile.Load returns nil, so the old code falls
// through to FindBeadsDir() which discovers project A instead.
projectB := t.TempDir()
beadsDirB := filepath.Join(projectB, ".beads")
if err := os.MkdirAll(filepath.Join(beadsDirB, "dolt"), 0o755); err != nil {
t.Fatalf("mkdir beads dir B: %v", err)
}
// CWD is inside project A so FindBeadsDir() discovers A
origDir, err := os.Getwd()
if err != nil {
t.Fatalf("getwd: %v", err)
}
if err := os.Chdir(projectA); err != nil {
t.Fatalf("chdir to project A: %v", err)
}
t.Cleanup(func() { os.Chdir(origDir) })
// Simulate --db pointing to project B's database path
dbPathB := filepath.Join(beadsDirB, "dolt")
got := resolveCommandBeadsDir(dbPathB)
// Must resolve to project B's .beads, NOT project A's.
// The old code falls back to FindBeadsDir() and returns beadsDirA.
if !utils.PathsEqual(got, beadsDirB) {
t.Fatalf("resolveCommandBeadsDir(%q) = %q, want %q", dbPathB, got, beadsDirB)
}
}
func TestGetGitHubConfigValue_UsesMetadataWhenStoreNil(t *testing.T) {
// github.token is now a YAML-only key (not stored in Dolt DB) to avoid
// leaking secrets when pushing to remotes. Test that the env-var fallback
// still works when the store is nil.
originalStore := store
originalDBPath := dbPath
defer func() {
store = originalStore
dbPath = originalDBPath
}()
ctx := context.Background()
store = nil
dbPath = ""
t.Setenv("GITHUB_TOKEN", "ghp_test_token")
if got := getGitHubConfigValue(ctx, "github.token"); got != "ghp_test_token" {
t.Fatalf("getGitHubConfigValue() = %q, want %q", got, "ghp_test_token")
}
}