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>
449 lines
14 KiB
Go
449 lines
14 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestOutputContextFunction(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
mcpMode bool
|
|
stealthMode bool
|
|
ephemeralMode bool
|
|
localOnlyMode bool
|
|
noPushMode bool
|
|
expectText []string
|
|
rejectText []string
|
|
}{
|
|
{
|
|
name: "CLI Normal (non-ephemeral)",
|
|
mcpMode: false,
|
|
stealthMode: false,
|
|
ephemeralMode: false,
|
|
localOnlyMode: false,
|
|
expectText: []string{"Beads Workflow Context", "bd dolt push", "Team-maintainer behavior is opt-in", "conservative by default"},
|
|
rejectText: []string{"bd export", "--from-main"},
|
|
},
|
|
{
|
|
name: "CLI Normal (ephemeral)",
|
|
mcpMode: false,
|
|
stealthMode: false,
|
|
ephemeralMode: true,
|
|
localOnlyMode: false,
|
|
expectText: []string{"Beads Workflow Context", "bd dolt pull", "ephemeral branch"},
|
|
rejectText: []string{"bd export", "git push", "--from-main"},
|
|
},
|
|
{
|
|
name: "CLI no-push",
|
|
mcpMode: false,
|
|
stealthMode: false,
|
|
ephemeralMode: false,
|
|
localOnlyMode: false,
|
|
noPushMode: true,
|
|
expectText: []string{"Beads Workflow Context", "push disabled", "report handoff"},
|
|
rejectText: []string{"bd export", "--from-main"},
|
|
},
|
|
{
|
|
name: "CLI Stealth",
|
|
mcpMode: false,
|
|
stealthMode: true,
|
|
ephemeralMode: false, // stealth mode overrides ephemeral detection
|
|
localOnlyMode: false,
|
|
expectText: []string{"Beads Workflow Context", "bd close"},
|
|
rejectText: []string{"git push", "git pull", "git commit", "git status", "git add", "bd export"},
|
|
},
|
|
{
|
|
name: "CLI Local-only (no git remote)",
|
|
mcpMode: false,
|
|
stealthMode: false,
|
|
ephemeralMode: false,
|
|
localOnlyMode: true,
|
|
expectText: []string{"Beads Workflow Context", "bd close", "No git remote configured"},
|
|
rejectText: []string{"git push", "git pull", "--from-main", "bd export"},
|
|
},
|
|
{
|
|
name: "CLI Local-only overrides ephemeral",
|
|
mcpMode: false,
|
|
stealthMode: false,
|
|
ephemeralMode: true, // ephemeral is true but local-only takes precedence
|
|
localOnlyMode: true,
|
|
expectText: []string{"Beads Workflow Context", "bd close", "No git remote configured"},
|
|
rejectText: []string{"git push", "--from-main", "ephemeral branch", "bd export"},
|
|
},
|
|
{
|
|
name: "CLI Stealth overrides local-only",
|
|
mcpMode: false,
|
|
stealthMode: true,
|
|
ephemeralMode: false,
|
|
localOnlyMode: true, // local-only is true but stealth takes precedence
|
|
expectText: []string{"Beads Workflow Context", "bd close"},
|
|
rejectText: []string{"git push", "git pull", "git commit", "git status", "git add", "No git remote configured", "bd export"},
|
|
},
|
|
{
|
|
name: "MCP Normal (non-ephemeral)",
|
|
mcpMode: true,
|
|
stealthMode: false,
|
|
ephemeralMode: false,
|
|
localOnlyMode: false,
|
|
expectText: []string{"Beads Issue Tracker Active", "Team-maintainer behavior is opt-in"},
|
|
rejectText: []string{"bd export", "--from-main"},
|
|
},
|
|
{
|
|
name: "MCP Normal (ephemeral)",
|
|
mcpMode: true,
|
|
stealthMode: false,
|
|
ephemeralMode: true,
|
|
localOnlyMode: false,
|
|
expectText: []string{"Beads Issue Tracker Active", "ephemeral branch"},
|
|
rejectText: []string{"bd export", "git push", "--from-main"},
|
|
},
|
|
{
|
|
name: "MCP no-push",
|
|
mcpMode: true,
|
|
stealthMode: false,
|
|
ephemeralMode: false,
|
|
localOnlyMode: false,
|
|
noPushMode: true,
|
|
expectText: []string{"Beads Issue Tracker Active", "push disabled"},
|
|
rejectText: []string{"bd export", "--from-main"},
|
|
},
|
|
{
|
|
name: "MCP Stealth",
|
|
mcpMode: true,
|
|
stealthMode: true,
|
|
ephemeralMode: false, // stealth mode overrides ephemeral detection
|
|
localOnlyMode: false,
|
|
expectText: []string{"Beads Issue Tracker Active", "bd close"},
|
|
rejectText: []string{"git push", "git pull", "git commit", "git status", "git add", "bd export"},
|
|
},
|
|
{
|
|
name: "MCP Local-only (no git remote)",
|
|
mcpMode: true,
|
|
stealthMode: false,
|
|
ephemeralMode: false,
|
|
localOnlyMode: true,
|
|
expectText: []string{"Beads Issue Tracker Active", "bd close"},
|
|
rejectText: []string{"git push", "git pull", "--from-main", "bd export"},
|
|
},
|
|
{
|
|
name: "MCP Local-only overrides ephemeral",
|
|
mcpMode: true,
|
|
stealthMode: false,
|
|
ephemeralMode: true, // ephemeral is true but local-only takes precedence
|
|
localOnlyMode: true,
|
|
expectText: []string{"Beads Issue Tracker Active", "bd close"},
|
|
rejectText: []string{"git push", "--from-main", "ephemeral branch", "bd export"},
|
|
},
|
|
{
|
|
name: "MCP Stealth overrides local-only",
|
|
mcpMode: true,
|
|
stealthMode: true,
|
|
ephemeralMode: false,
|
|
localOnlyMode: true, // local-only is true but stealth takes precedence
|
|
expectText: []string{"Beads Issue Tracker Active", "bd close"},
|
|
rejectText: []string{"git push", "git pull", "git commit", "git status", "git add", "bd export"},
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
defer stubIsEphemeralBranch(tt.ephemeralMode)()
|
|
defer stubPrimeHasGitRemote(!tt.localOnlyMode)() // localOnly = !primeHasGitRemote
|
|
defer stubPrimeNoPushConfigured(tt.noPushMode)()
|
|
|
|
var buf bytes.Buffer
|
|
err := outputPrimeContext(&buf, tt.mcpMode, tt.stealthMode)
|
|
if err != nil {
|
|
t.Fatalf("outputPrimeContext failed: %v", err)
|
|
}
|
|
|
|
output := buf.String()
|
|
|
|
for _, expected := range tt.expectText {
|
|
if !strings.Contains(output, expected) {
|
|
t.Errorf("Expected text not found: %s", expected)
|
|
}
|
|
}
|
|
|
|
for _, rejected := range tt.rejectText {
|
|
if strings.Contains(output, rejected) {
|
|
t.Errorf("Unexpected text found: %s", rejected)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestPrimeClaimGuidanceUsesAtomicClaim(t *testing.T) {
|
|
defer stubIsEphemeralBranch(false)()
|
|
defer stubPrimeHasGitRemote(true)()
|
|
|
|
var buf bytes.Buffer
|
|
if err := outputPrimeContext(&buf, false, false); err != nil {
|
|
t.Fatalf("outputPrimeContext failed: %v", err)
|
|
}
|
|
|
|
output := buf.String()
|
|
if !strings.Contains(output, "bd update <id> --claim") {
|
|
t.Fatal("prime output should teach bd update <id> --claim")
|
|
}
|
|
if strings.Contains(output, "bd update <id> --status=in_progress") {
|
|
t.Fatal("prime output should not teach bd update <id> --status=in_progress")
|
|
}
|
|
}
|
|
|
|
func TestPrimeStartsWithTruncationDirective(t *testing.T) {
|
|
defer stubIsEphemeralBranch(false)()
|
|
defer stubPrimeHasGitRemote(true)()
|
|
|
|
for _, mcpMode := range []bool{false, true} {
|
|
var buf bytes.Buffer
|
|
if err := outputPrimeContext(&buf, mcpMode, false); err != nil {
|
|
t.Fatalf("outputPrimeContext failed: %v", err)
|
|
}
|
|
if !strings.HasPrefix(buf.String(), primeTruncationDirective) {
|
|
t.Fatalf("prime output should start with truncation directive; got %q", buf.String()[:min(120, buf.Len())])
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestPrimeMemoriesOnlyNoMemories(t *testing.T) {
|
|
var buf bytes.Buffer
|
|
if err := outputPrimeContextWithOptions(&buf, false, false, true); err != nil {
|
|
t.Fatalf("outputPrimeContextWithOptions failed: %v", err)
|
|
}
|
|
|
|
output := buf.String()
|
|
if !strings.HasPrefix(output, primeTruncationDirective) {
|
|
t.Fatal("memories-only output should start with truncation directive")
|
|
}
|
|
if strings.Contains(output, "Essential Commands") {
|
|
t.Fatalf("memories-only output should not include the full workflow guide: %s", output)
|
|
}
|
|
}
|
|
|
|
func TestFormatMemoriesForPrimeTimesOutOpeningStore(t *testing.T) {
|
|
oldStore := store
|
|
oldStoreActive := storeActive
|
|
oldEnsure := ensureStoreActiveForPrime
|
|
store = nil
|
|
storeActive = false
|
|
ensureStoreActiveForPrime = func(ctx context.Context) error {
|
|
<-ctx.Done()
|
|
return ctx.Err()
|
|
}
|
|
t.Cleanup(func() {
|
|
store = oldStore
|
|
storeActive = oldStoreActive
|
|
ensureStoreActiveForPrime = oldEnsure
|
|
})
|
|
t.Setenv(primeStoreTimeoutEnv, "1ms")
|
|
|
|
out := formatMemoriesForPrime(false)
|
|
if !strings.Contains(out, "timed out") {
|
|
t.Fatalf("expected timeout warning in prime memory output, got %q", out)
|
|
}
|
|
if !strings.Contains(out, "stale storage lock") {
|
|
t.Fatalf("expected stale-lock guidance in prime memory output, got %q", out)
|
|
}
|
|
}
|
|
|
|
func TestPrimeStoreTimeoutNonPositiveUsesDefault(t *testing.T) {
|
|
for _, value := range []string{"0", "0s", "-5s"} {
|
|
t.Run(value, func(t *testing.T) {
|
|
t.Setenv(primeStoreTimeoutEnv, value)
|
|
if got := primeStoreTimeout(); got != primeStoreTimeoutDefault {
|
|
t.Fatalf("primeStoreTimeout() = %s, want default %s", got, primeStoreTimeoutDefault)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestPrimeContextUsesWorkspaceLanguage(t *testing.T) {
|
|
defer stubIsEphemeralBranch(false)()
|
|
defer stubPrimeHasGitRemote(true)()
|
|
|
|
var buf bytes.Buffer
|
|
if err := outputPrimeContext(&buf, false, false); err != nil {
|
|
t.Fatalf("outputPrimeContext failed: %v", err)
|
|
}
|
|
|
|
output := buf.String()
|
|
if !strings.Contains(output, "resolved") || !strings.Contains(output, "workspace") {
|
|
t.Fatalf("prime output should describe resolved workspace semantics: %s", output)
|
|
}
|
|
if strings.Contains(output, "when .beads/ detected") {
|
|
t.Fatal("prime output should not imply local .beads detection is required")
|
|
}
|
|
}
|
|
|
|
// stubIsEphemeralBranch temporarily replaces isEphemeralBranch
|
|
// with a stub returning returnValue.
|
|
//
|
|
// Returns a function to restore the original isEphemeralBranch.
|
|
// Usage:
|
|
//
|
|
// defer stubIsEphemeralBranch(true)()
|
|
func stubIsEphemeralBranch(isEphem bool) func() {
|
|
original := isEphemeralBranch
|
|
isEphemeralBranch = func() bool {
|
|
return isEphem
|
|
}
|
|
return func() {
|
|
isEphemeralBranch = original
|
|
}
|
|
}
|
|
|
|
// stubPrimeHasGitRemote temporarily replaces primeHasGitRemote
|
|
// with a stub returning returnValue.
|
|
//
|
|
// Returns a function to restore the original primeHasGitRemote.
|
|
// Usage:
|
|
//
|
|
// defer stubPrimeHasGitRemote(true)()
|
|
func stubPrimeHasGitRemote(hasRemote bool) func() {
|
|
original := primeHasGitRemote
|
|
primeHasGitRemote = func() bool {
|
|
return hasRemote
|
|
}
|
|
return func() {
|
|
primeHasGitRemote = original
|
|
}
|
|
}
|
|
|
|
// stubPrimeNoPushConfigured temporarily replaces primeNoPushConfigured
|
|
// with a stub returning noPush.
|
|
func stubPrimeNoPushConfigured(noPush bool) func() {
|
|
original := primeNoPushConfigured
|
|
primeNoPushConfigured = func() bool {
|
|
return noPush
|
|
}
|
|
return func() {
|
|
primeNoPushConfigured = original
|
|
}
|
|
}
|
|
|
|
func TestPrimeGlobalFallback(t *testing.T) {
|
|
// Create a temp directory to act as config dir
|
|
tmpDir := t.TempDir()
|
|
beadsConfigDir := filepath.Join(tmpDir, "beads")
|
|
if err := os.MkdirAll(beadsConfigDir, 0755); err != nil {
|
|
t.Fatalf("mkdir: %v", err)
|
|
}
|
|
|
|
content := "# Global PRIME override\nCustom instructions here.\n"
|
|
if err := os.WriteFile(filepath.Join(beadsConfigDir, "PRIME.md"), []byte(content), 0644); err != nil {
|
|
t.Fatalf("write PRIME.md: %v", err)
|
|
}
|
|
|
|
// Call the helper that resolves the global prime path
|
|
got := resolveGlobalPrimePath(tmpDir)
|
|
if got == "" {
|
|
t.Fatal("resolveGlobalPrimePath returned empty, want path to global PRIME.md")
|
|
}
|
|
|
|
data, err := os.ReadFile(got)
|
|
if err != nil {
|
|
t.Fatalf("ReadFile(%s): %v", got, err)
|
|
}
|
|
if string(data) != content {
|
|
t.Errorf("content = %q, want %q", string(data), content)
|
|
}
|
|
}
|
|
|
|
func TestPrimeGlobalFallback_Missing(t *testing.T) {
|
|
// When no global PRIME.md exists, should return empty string
|
|
tmpDir := t.TempDir()
|
|
got := resolveGlobalPrimePath(tmpDir)
|
|
if got != "" {
|
|
t.Errorf("resolveGlobalPrimePath = %q, want empty for missing file", got)
|
|
}
|
|
}
|
|
|
|
// hookJSONEnvelope mirrors the JSON shape produced by outputHookJSON —
|
|
// kept in test code so the assertion fails loudly if the production shape
|
|
// drifts.
|
|
type hookJSONEnvelope struct {
|
|
HookSpecificOutput struct {
|
|
HookEventName string `json:"hookEventName"`
|
|
AdditionalContext string `json:"additionalContext"`
|
|
} `json:"hookSpecificOutput"`
|
|
}
|
|
|
|
func TestOutputHookJSON_ShapeWithContent(t *testing.T) {
|
|
var buf bytes.Buffer
|
|
const payload = "# Hello\n\nbd ready\n"
|
|
if err := outputHookJSON(&buf, payload); err != nil {
|
|
t.Fatalf("outputHookJSON: %v", err)
|
|
}
|
|
|
|
// json.Encoder.Encode appends a trailing newline; the JSON itself must
|
|
// still be valid.
|
|
out := strings.TrimRight(buf.String(), "\n")
|
|
|
|
var env hookJSONEnvelope
|
|
if err := json.Unmarshal([]byte(out), &env); err != nil {
|
|
t.Fatalf("output is not valid JSON: %v\noutput: %s", err, buf.String())
|
|
}
|
|
if env.HookSpecificOutput.HookEventName != "SessionStart" {
|
|
t.Errorf("hookEventName = %q, want SessionStart", env.HookSpecificOutput.HookEventName)
|
|
}
|
|
if env.HookSpecificOutput.AdditionalContext != payload {
|
|
t.Errorf("additionalContext = %q, want %q", env.HookSpecificOutput.AdditionalContext, payload)
|
|
}
|
|
}
|
|
|
|
func TestOutputHookJSON_EmptyContent(t *testing.T) {
|
|
// Empty envelope is the contract for "nothing to inject" — the hook host
|
|
// still requires valid JSON on stdout, so we cannot just emit nothing.
|
|
var buf bytes.Buffer
|
|
if err := outputHookJSON(&buf, ""); err != nil {
|
|
t.Fatalf("outputHookJSON: %v", err)
|
|
}
|
|
|
|
var env hookJSONEnvelope
|
|
if err := json.Unmarshal(bytes.TrimRight(buf.Bytes(), "\n"), &env); err != nil {
|
|
t.Fatalf("output is not valid JSON: %v\noutput: %s", err, buf.String())
|
|
}
|
|
if env.HookSpecificOutput.HookEventName != "SessionStart" {
|
|
t.Errorf("hookEventName = %q, want SessionStart", env.HookSpecificOutput.HookEventName)
|
|
}
|
|
if env.HookSpecificOutput.AdditionalContext != "" {
|
|
t.Errorf("additionalContext = %q, want empty", env.HookSpecificOutput.AdditionalContext)
|
|
}
|
|
}
|
|
|
|
// TestPrime_RawMarkdown_NotJSON_WithoutFlag is a regression guard: without
|
|
// --hook-json, prime output must remain raw markdown (used by CLI users and
|
|
// any hook-free integrations). It would be a regression if the JSON envelope
|
|
// leaked into the default path.
|
|
func TestPrime_RawMarkdown_NotJSON_WithoutFlag(t *testing.T) {
|
|
defer stubIsEphemeralBranch(false)()
|
|
defer stubPrimeHasGitRemote(true)()
|
|
|
|
var buf bytes.Buffer
|
|
if err := outputPrimeContext(&buf, false, false); err != nil {
|
|
t.Fatalf("outputPrimeContext: %v", err)
|
|
}
|
|
|
|
output := buf.String()
|
|
if strings.HasPrefix(strings.TrimSpace(output), "{") {
|
|
preview := output
|
|
if len(preview) > 200 {
|
|
preview = preview[:200]
|
|
}
|
|
t.Fatalf("prime output without --hook-json should be raw markdown, got JSON-looking content: %q", preview)
|
|
}
|
|
// Best-effort: confirm the raw markdown contract holds.
|
|
var envelope map[string]interface{}
|
|
if err := json.Unmarshal([]byte(output), &envelope); err == nil {
|
|
t.Fatal("prime output without --hook-json should not be valid JSON (regression guard)")
|
|
}
|
|
}
|