265 lines
8.1 KiB
Go
265 lines
8.1 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestOutputContextFunction(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
mcpMode bool
|
|
stealthMode bool
|
|
ephemeralMode bool
|
|
localOnlyMode 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", "git push"},
|
|
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 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", "git push"},
|
|
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 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
|
|
|
|
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 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
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|