* fix(setup): keep bd setup claude --global's section in ~/.claude/CLAUDE.md bd setup claude --global put its hook in ~/.claude/settings.json but wrote the managed beads section into the current directory's CLAUDE.md, and --remove --global stripped it from there. Run from $HOME it created ~/CLAUDE.md with project scaffolding, which stops Claude Code reading AGENTS.md in projects below it; run inside a project it removed the section that project's own install had written, leaving its hooks. A global install and removal now manage the section in Claude Code's user-level ~/.claude/CLAUDE.md (created holding only the section, no project scaffolding), with hints naming bd setup claude --global. With global or plugin hooks, --check reports that file when the project's instructions have no beads section; a project install is still judged on the project's file alone. Project installs are unchanged. Agent-Signature: claude-code-claude-opus-5-5-unknown-reasoning on behalf of Julian Knutsen Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * fix(setup): render the user-level CLAUDE.md section independently of the project ~/.claude/CLAUDE.md is shared by every project, but bd setup claude --global rendered and --check judged its section with the current directory's beads config (sync.remote, sync.git-remote, no-push). From a project with a remote and one without, --check called the other's install stale and its hint flipped the file back. The user-level file now always renders with the default options, so install and --check agree across projects. Also pin --check's fallback to the project's file and project hint when neither file has a section, tell existing --global users to re-run it, and name ~/.claude/CLAUDE.md in the ide-setup recipe table. Agent-Signature: claude-code-claude-opus-5-5-unknown-reasoning on behalf of Julian Knutsen Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * fix(setup): judge the user-level Claude files from $HOME and ~/.claude Run from the home directory, the project's .claude/settings.json is the global one, so bd setup claude --check reported a global install as project hooks and judged ~/CLAUDE.md; its "Run: bd setup claude" hint then created the ~/CLAUDE.md a global install exists to avoid. Run from ~/.claude, the project's CLAUDE.md is the user-level file, and --check judged it with that directory's render options and the project hint. --check now reports hooks as global from $HOME and judges the user-level file there and from ~/.claude with the fixed options and the --global hint. With user-level hooks and no section in either file it names --global unless the project has its own CLAUDE.md. A --remove run from $HOME, which removes the global hooks, also removes the user-level section. The init agent-artifact test ran bd setup claude with HOME at the project, so its checks would now see a home directory; it gets a separate HOME and its goldens are unchanged. Rebase note (maintainer): rebased onto main after #7678 merged; main's agent-artifact harness already runs bd with a separate HOME, so this commit's harness edits to cmd/bd/agent_artifacts_embedded_test.go were dropped. Agent-Signature: claude-code-claude-opus-5-5-unknown-reasoning on behalf of Julian Knutsen Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * test(setup): pin --check's user-level choice beside project instructions With global hooks, --check judges ~/.claude/CLAUDE.md when the project's own CLAUDE.md has no beads section but the user-level file has one, and when run from the home directory whose ~/CLAUDE.md has no section. Nothing exercised either half of that condition: replacing it with false left the suite and goldens green. Add a subtest for each; each now fails when its half of the condition is dropped. Agent-Signature: claude-code-claude-opus-5-5-unknown-reasoning on behalf of Julian Knutsen Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * fix(setup): name --global when --check finds no hooks from $HOME From the home directory --check with no hooks said 'Run: bd setup claude'. Following it runs a project install there, which creates ~/CLAUDE.md with project scaffolding, the file that stops Claude Code reading AGENTS.md in the projects below it. The missing-section hint at home already names --global; make the missing-hooks hint match, with a subtest. Agent-Signature: claude-code-claude-opus-5-5-unknown-reasoning on behalf of Julian Knutsen Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * docs(setup): qualify the --global CHANGELOG entry; watch ~/.claude/CLAUDE.md The CHANGELOG said --check from the home directory judges the user-level files, but a beads section still in ~/CLAUDE.md is judged instead, and the --global hint for a missing section applies only when the project has no CLAUDE.md of its own. Say both, mention the new missing-hooks hint at home, and re-wrap the paragraph. The embedded agent-artifact harness comment said a global install writes $HOME/CLAUDE.md; it now writes $HOME/.claude/CLAUDE.md. Track that path too and reword the comment. The bd init goldens do not change. Agent-Signature: claude-code-claude-opus-5-5-unknown-reasoning on behalf of Julian Knutsen Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * fix(setup): word the user-level CLAUDE.md section for repositories that use beads bd setup claude --global writes its section to ~/.claude/CLAUDE.md, which Claude Code loads in every project, but rendered it with the project wording: "This project uses bd (beads)" and "Use bd for ALL task tracking — do NOT use TodoWrite". In a repository without beads that told the agent to give up its own task tools and pointed it at bd, whose answer there is "run 'bd init'", while the global hook, bd prime, stays silent outside a workspace. RenderOpts.UserLevel rewords the opening line and the task-tracking rules of both profiles so they apply only in beads repositories. The global env fixes it through agents.UserLevelRenderOpts, so install, in-place update and --check agree on the hash from every directory, and a project-worded section left in ~/.claude/CLAUDE.md reads as stale and is reworded by --global. The new minimal-user and full-user variants are pinned by golden and hash; the project rendering is unchanged. Also pin sameDir's symlink following: --check from a symlink to $HOME or to ~/.claude still judges the user-level files. Refreshed onto main past #7692, whose --remove takes the blank lines around the section with it and whose setup appends a section after one blank line: claude-setup-global-beside-project-install.golden no longer records the old hole (an extra blank line before the appended section, two left behind by --remove --global). Agent-Signature: claude-code-claude-opus-5-5-unknown-reasoning on behalf of Julian Knutsen Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Co-authored-by: Eddie the Engineer <338826946+gascity-eddie[bot]@users.noreply.github.com>
577 lines
19 KiB
Go
577 lines
19 KiB
Go
package agents
|
|
|
|
import (
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestProfileConstants(t *testing.T) {
|
|
if ProfileFull != "full" {
|
|
t.Errorf("ProfileFull = %q, want %q", ProfileFull, "full")
|
|
}
|
|
if ProfileMinimal != "minimal" {
|
|
t.Errorf("ProfileMinimal = %q, want %q", ProfileMinimal, "minimal")
|
|
}
|
|
}
|
|
|
|
func TestRenderSectionFull(t *testing.T) {
|
|
section := RenderSection(ProfileFull)
|
|
if section == "" {
|
|
t.Fatal("RenderSection(full) returned empty string")
|
|
}
|
|
|
|
// Must start with begin marker containing profile and hash metadata
|
|
if !strings.HasPrefix(section, "<!-- BEGIN BEADS INTEGRATION") {
|
|
t.Error("section should start with begin marker")
|
|
}
|
|
|
|
// Must contain profile metadata
|
|
if !strings.Contains(section, "profile:full") {
|
|
t.Error("begin marker should contain profile:full")
|
|
}
|
|
|
|
// Must contain hash metadata
|
|
if !strings.Contains(section, "hash:") {
|
|
t.Error("begin marker should contain hash:")
|
|
}
|
|
|
|
// Must end with end marker
|
|
trimmed := strings.TrimSpace(section)
|
|
if !strings.HasSuffix(trimmed, "<!-- END BEADS INTEGRATION -->") {
|
|
t.Error("section should end with end marker")
|
|
}
|
|
|
|
// Full profile must contain command references
|
|
for _, want := range []string{"bd create", "bd update", "bd close", "bd ready", "discovered-from"} {
|
|
if !strings.Contains(section, want) {
|
|
t.Errorf("full profile missing %q", want)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestRenderSectionFullSingleMarkers guards against #3552: when bd is built
|
|
// from a Windows checkout, //go:embed picks up CRLF bytes from the working
|
|
// tree, and templateBody()'s LF-only marker stripping previously left the
|
|
// legacy markers inside the body. RenderSection then wrapped them in fresh
|
|
// markers, producing doubled BEGIN/END pairs. Output must always contain
|
|
// exactly one of each — regardless of the embedded file's line endings.
|
|
func TestRenderSectionFullSingleMarkers(t *testing.T) {
|
|
section := RenderSection(ProfileFull)
|
|
if got := strings.Count(section, "<!-- BEGIN BEADS INTEGRATION"); got != 1 {
|
|
t.Errorf("expected exactly 1 BEGIN marker, got %d", got)
|
|
}
|
|
if got := strings.Count(section, "<!-- END BEADS INTEGRATION -->"); got != 1 {
|
|
t.Errorf("expected exactly 1 END marker, got %d", got)
|
|
}
|
|
}
|
|
|
|
// TestRenderSectionFullIncludesArchPointer guards GH#3683: the rich
|
|
// architecture blockquote in agents.md.tmpl only reaches greenfield
|
|
// AGENTS.md files. For users running `bd init` over an existing AGENTS.md,
|
|
// ReplaceSection only swaps content between the BEGIN/END markers — so a
|
|
// one-line architecture pointer must live inside beads-section.md too, so
|
|
// hash-freshness re-renders propagate it on subsequent runs.
|
|
func TestRenderSectionFullIncludesArchPointer(t *testing.T) {
|
|
section := RenderSection(ProfileFull)
|
|
for _, want := range []string{"refs/dolt/data", "docs/core-concepts/sync-concepts.md"} {
|
|
if !strings.Contains(section, want) {
|
|
t.Errorf("full profile missing architecture pointer fragment %q", want)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestRenderSectionMinimalIncludesArchPointer guards GH#3683 for the
|
|
// minimal profile. The minimal body is intentionally pointer-only, but a
|
|
// single architecture sentence with the canonical sync-concepts URL
|
|
// fits the profile's "point at the docs" character.
|
|
func TestRenderSectionMinimalIncludesArchPointer(t *testing.T) {
|
|
section := RenderSection(ProfileMinimal)
|
|
for _, want := range []string{"refs/dolt/data", "docs/core-concepts/sync-concepts.md"} {
|
|
if !strings.Contains(section, want) {
|
|
t.Errorf("minimal profile missing architecture pointer fragment %q", want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestRenderSectionMinimal(t *testing.T) {
|
|
section := RenderSection(ProfileMinimal)
|
|
if section == "" {
|
|
t.Fatal("RenderSection(minimal) returned empty string")
|
|
}
|
|
|
|
// Must start with begin marker containing profile and hash metadata
|
|
if !strings.HasPrefix(section, "<!-- BEGIN BEADS INTEGRATION") {
|
|
t.Error("section should start with begin marker")
|
|
}
|
|
if !strings.Contains(section, "profile:minimal") {
|
|
t.Error("begin marker should contain profile:minimal")
|
|
}
|
|
if !strings.Contains(section, "hash:") {
|
|
t.Error("begin marker should contain hash:")
|
|
}
|
|
|
|
// Minimal should reference bd prime
|
|
if !strings.Contains(section, "bd prime") {
|
|
t.Error("minimal profile should reference bd prime")
|
|
}
|
|
|
|
// Minimal should NOT contain full command references
|
|
if strings.Contains(section, "### Issue Types") {
|
|
t.Error("minimal profile should not contain full issue types section")
|
|
}
|
|
if strings.Contains(section, "### Priorities") {
|
|
t.Error("minimal profile should not contain full priorities section")
|
|
}
|
|
|
|
// GH#6111: the memory bullet says which store each content class belongs
|
|
// in instead of prohibiting MEMORY.md, because harnesses ship a
|
|
// first-party memory whose index file has exactly that name. Assert the
|
|
// prohibition's absence rather than the replacement prose, so the pin
|
|
// survives any future rewording of the sentence.
|
|
if !strings.Contains(section, "bd remember") {
|
|
t.Error("minimal profile should still teach bd remember")
|
|
}
|
|
if strings.Contains(section, "MEMORY.md") {
|
|
t.Error("minimal profile should not name MEMORY.md")
|
|
}
|
|
}
|
|
|
|
func TestCodexSectionBody(t *testing.T) {
|
|
body := CodexSectionBody()
|
|
if body == "" {
|
|
t.Fatal("CodexSectionBody returned empty string")
|
|
}
|
|
for _, want := range []string{
|
|
"## Beads Issue Tracker",
|
|
"`beads` skill",
|
|
"bd ready",
|
|
"bd prime",
|
|
"bd remember",
|
|
"refs/dolt/data",
|
|
"docs/core-concepts/sync-concepts.md",
|
|
} {
|
|
if !strings.Contains(body, want) {
|
|
t.Errorf("CodexSectionBody missing %q", want)
|
|
}
|
|
}
|
|
if strings.Contains(body, "BEGIN BEADS") || strings.Contains(body, "END BEADS") {
|
|
t.Error("CodexSectionBody should not include managed markers")
|
|
}
|
|
|
|
// GH#6111, as above: the memory bullet describes the split instead of
|
|
// banning per-tool memory files. This body spelled its prohibition "ad hoc
|
|
// memory files" and never named MEMORY.md, so checking only the file name
|
|
// would assert nothing about what this surface used to emit.
|
|
for _, unwanted := range []string{"MEMORY.md", "ad hoc memory files"} {
|
|
if strings.Contains(body, unwanted) {
|
|
t.Errorf("CodexSectionBody should not prohibit %q", unwanted)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestRenderSectionHashStability(t *testing.T) {
|
|
// Same profile should produce same hash
|
|
s1 := RenderSection(ProfileFull)
|
|
s2 := RenderSection(ProfileFull)
|
|
if s1 != s2 {
|
|
t.Error("RenderSection should be deterministic (same input, same output)")
|
|
}
|
|
|
|
// Different profiles should produce different hashes
|
|
full := RenderSection(ProfileFull)
|
|
minimal := RenderSection(ProfileMinimal)
|
|
meta1 := ParseMarker(strings.SplitN(full, "\n", 2)[0])
|
|
meta2 := ParseMarker(strings.SplitN(minimal, "\n", 2)[0])
|
|
if meta1.Hash == meta2.Hash {
|
|
t.Error("different profiles should produce different hashes")
|
|
}
|
|
}
|
|
|
|
func TestRenderSectionFullBackcompat(t *testing.T) {
|
|
// Full profile should contain everything the legacy EmbeddedBeadsSection had
|
|
section := RenderSection(ProfileFull)
|
|
legacy := EmbeddedBeadsSection()
|
|
|
|
// Check that key content pieces from legacy are present in full
|
|
for _, want := range []string{
|
|
"bd create",
|
|
"bd update",
|
|
"bd close",
|
|
"bd ready",
|
|
"discovered-from",
|
|
"Session Completion",
|
|
} {
|
|
if !strings.Contains(legacy, want) {
|
|
t.Fatalf("test precondition: legacy section missing %q", want)
|
|
}
|
|
if !strings.Contains(section, want) {
|
|
t.Errorf("full profile missing legacy content %q", want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestParseMarker(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
line string
|
|
want SectionMeta
|
|
wantOK bool
|
|
}{
|
|
{
|
|
name: "versioned format with profile and hash",
|
|
line: "<!-- BEGIN BEADS INTEGRATION v:1 profile:full hash:a1b2c3d4 -->",
|
|
want: SectionMeta{Version: 1, Profile: ProfileFull, Hash: "a1b2c3d4"},
|
|
wantOK: true,
|
|
},
|
|
{
|
|
name: "versioned format minimal profile",
|
|
line: "<!-- BEGIN BEADS INTEGRATION v:1 profile:minimal hash:deadbeef -->",
|
|
want: SectionMeta{Version: 1, Profile: ProfileMinimal, Hash: "deadbeef"},
|
|
wantOK: true,
|
|
},
|
|
{
|
|
name: "pre-version format with profile and hash",
|
|
line: "<!-- BEGIN BEADS INTEGRATION profile:full hash:a1b2c3d4 -->",
|
|
want: SectionMeta{Version: 0, Profile: ProfileFull, Hash: "a1b2c3d4"},
|
|
wantOK: true,
|
|
},
|
|
{
|
|
name: "legacy format (no metadata)",
|
|
line: "<!-- BEGIN BEADS INTEGRATION -->",
|
|
want: SectionMeta{Version: 0, Profile: "", Hash: ""},
|
|
wantOK: true,
|
|
},
|
|
{
|
|
name: "not a marker",
|
|
line: "## Some heading",
|
|
wantOK: false,
|
|
},
|
|
{
|
|
name: "end marker (not begin)",
|
|
line: "<!-- END BEADS INTEGRATION -->",
|
|
wantOK: false,
|
|
},
|
|
{
|
|
name: "empty string",
|
|
line: "",
|
|
wantOK: false,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got := ParseMarker(tt.line)
|
|
if tt.wantOK {
|
|
if got == nil {
|
|
t.Fatal("ParseMarker returned nil, expected non-nil")
|
|
}
|
|
if got.Version != tt.want.Version {
|
|
t.Errorf("Version = %d, want %d", got.Version, tt.want.Version)
|
|
}
|
|
if got.Profile != tt.want.Profile {
|
|
t.Errorf("Profile = %q, want %q", got.Profile, tt.want.Profile)
|
|
}
|
|
if got.Hash != tt.want.Hash {
|
|
t.Errorf("Hash = %q, want %q", got.Hash, tt.want.Hash)
|
|
}
|
|
} else {
|
|
if got != nil {
|
|
t.Errorf("ParseMarker returned %+v, expected nil", got)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestIsStaleFreshness(t *testing.T) {
|
|
// Render a section and parse its marker — should not be stale
|
|
section := RenderSection(ProfileFull)
|
|
firstLine := strings.SplitN(section, "\n", 2)[0]
|
|
meta := ParseMarker(firstLine)
|
|
if meta == nil {
|
|
t.Fatal("ParseMarker returned nil for rendered section")
|
|
}
|
|
|
|
currentHash := CurrentHash(ProfileFull)
|
|
if meta.Hash != currentHash {
|
|
t.Errorf("rendered hash %q != current hash %q (should be fresh)", meta.Hash, currentHash)
|
|
}
|
|
|
|
// Legacy marker (no hash) should be considered stale
|
|
legacyMeta := ParseMarker("<!-- BEGIN BEADS INTEGRATION -->")
|
|
if legacyMeta == nil {
|
|
t.Fatal("ParseMarker returned nil for legacy marker")
|
|
}
|
|
if legacyMeta.Hash == currentHash {
|
|
t.Error("legacy marker with empty hash should not match current hash")
|
|
}
|
|
}
|
|
|
|
func TestReplaceSectionNoMarkers(t *testing.T) {
|
|
content := "# My Project\n\nSome content\n"
|
|
_, _, err := ReplaceSection(content, ProfileFull)
|
|
if !errors.Is(err, ErrNoSection) {
|
|
t.Fatalf("expected ErrNoSection, got %v", err)
|
|
}
|
|
}
|
|
|
|
func TestReplaceSectionMissingEnd(t *testing.T) {
|
|
content := "# Header\n<!-- BEGIN BEADS INTEGRATION -->\nContent without end\n"
|
|
_, _, err := ReplaceSection(content, ProfileFull)
|
|
if !errors.Is(err, ErrMalformedMarkers) {
|
|
t.Fatalf("expected ErrMalformedMarkers, got %v", err)
|
|
}
|
|
}
|
|
|
|
func TestReplaceSectionReversedMarkers(t *testing.T) {
|
|
content := "<!-- END BEADS INTEGRATION -->\nContent\n<!-- BEGIN BEADS INTEGRATION -->\n"
|
|
_, _, err := ReplaceSection(content, ProfileFull)
|
|
if !errors.Is(err, ErrMalformedMarkers) {
|
|
t.Fatalf("expected ErrMalformedMarkers, got %v", err)
|
|
}
|
|
}
|
|
|
|
func TestReplaceSectionIdempotent(t *testing.T) {
|
|
// Render a current section, then ReplaceSection should be a no-op
|
|
section := RenderSection(ProfileFull)
|
|
content := "# Header\n\n" + section + "\n# Footer\n"
|
|
|
|
result, changed, err := ReplaceSection(content, ProfileFull)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if changed {
|
|
t.Error("should not change when section is already current")
|
|
}
|
|
if result != content {
|
|
t.Error("content should be unchanged when already current")
|
|
}
|
|
}
|
|
|
|
func TestReplaceSectionUpgradesLegacy(t *testing.T) {
|
|
content := "# Header\n\n<!-- BEGIN BEADS INTEGRATION -->\nOld content\n<!-- END BEADS INTEGRATION -->\n\n# Footer\n"
|
|
|
|
result, changed, err := ReplaceSection(content, ProfileFull)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if !changed {
|
|
t.Error("should change when upgrading legacy markers")
|
|
}
|
|
if !strings.Contains(result, "profile:full") {
|
|
t.Error("should have profile:full in upgraded marker")
|
|
}
|
|
if !strings.Contains(result, "v:1") {
|
|
t.Error("should have v:1 in upgraded marker")
|
|
}
|
|
if strings.Contains(result, "Old content") {
|
|
t.Error("old content should be replaced")
|
|
}
|
|
if !strings.Contains(result, "# Header") || !strings.Contains(result, "# Footer") {
|
|
t.Error("surrounding content should be preserved")
|
|
}
|
|
}
|
|
|
|
func TestReplaceSectionUpgradesStaleHash(t *testing.T) {
|
|
content := "# Header\n\n<!-- BEGIN BEADS INTEGRATION profile:full hash:00000000 -->\nStale\n<!-- END BEADS INTEGRATION -->\n\n# Footer\n"
|
|
|
|
result, changed, err := ReplaceSection(content, ProfileFull)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if !changed {
|
|
t.Error("should change when hash is stale")
|
|
}
|
|
if strings.Contains(result, "00000000") {
|
|
t.Error("stale hash should be replaced")
|
|
}
|
|
if strings.Contains(result, "Stale") {
|
|
t.Error("stale content should be replaced")
|
|
}
|
|
}
|
|
|
|
func TestReplaceSectionMultipleTrailingNewlines(t *testing.T) {
|
|
// END marker followed by multiple newlines — only one should be consumed
|
|
content := "# Header\n<!-- BEGIN BEADS INTEGRATION -->\nOld\n<!-- END BEADS INTEGRATION -->\n\n\n# Footer\n"
|
|
|
|
result, changed, err := ReplaceSection(content, ProfileFull)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if !changed {
|
|
t.Error("should change")
|
|
}
|
|
// The two extra newlines before Footer should be preserved
|
|
if !strings.Contains(result, "\n\n# Footer") {
|
|
t.Error("extra newlines before footer should be preserved")
|
|
}
|
|
}
|
|
|
|
func TestReplaceSectionProfileSwitch(t *testing.T) {
|
|
// Replace a full section with minimal
|
|
fullSection := RenderSection(ProfileFull)
|
|
content := "# Header\n\n" + fullSection + "\n# Footer\n"
|
|
|
|
result, changed, err := ReplaceSection(content, ProfileMinimal)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if !changed {
|
|
t.Error("should change when switching profiles")
|
|
}
|
|
if !strings.Contains(result, "profile:minimal") {
|
|
t.Error("should have minimal profile after switch")
|
|
}
|
|
}
|
|
|
|
func TestRenderSectionIncludesVersion(t *testing.T) {
|
|
section := RenderSection(ProfileFull)
|
|
firstLine := strings.SplitN(section, "\n", 2)[0]
|
|
if !strings.Contains(firstLine, "v:1") {
|
|
t.Errorf("begin marker should contain v:1, got: %s", firstLine)
|
|
}
|
|
meta := ParseMarker(firstLine)
|
|
if meta == nil {
|
|
t.Fatal("ParseMarker returned nil")
|
|
}
|
|
if meta.Version != MarkerVersion {
|
|
t.Errorf("Version = %d, want %d", meta.Version, MarkerVersion)
|
|
}
|
|
}
|
|
|
|
func TestRenderSectionWithOptsNoRemoteFull(t *testing.T) {
|
|
opts := RenderOpts{HasRemote: false}
|
|
section := RenderSectionWithOpts(ProfileFull, opts)
|
|
|
|
if strings.Contains(section, "bd dolt push") {
|
|
t.Error("full profile with HasRemote=false should not contain 'bd dolt push'")
|
|
}
|
|
|
|
// Should still contain other session completion content
|
|
if !strings.Contains(section, "git push") {
|
|
t.Error("should still contain 'git push'")
|
|
}
|
|
if !strings.Contains(section, "Session Completion") {
|
|
t.Error("should still contain Session Completion section")
|
|
}
|
|
}
|
|
|
|
func TestRenderSectionWithOptsNoRemoteMinimal(t *testing.T) {
|
|
opts := RenderOpts{HasRemote: false}
|
|
section := RenderSectionWithOpts(ProfileMinimal, opts)
|
|
|
|
if strings.Contains(section, "bd dolt push") {
|
|
t.Error("minimal profile with HasRemote=false should not contain 'bd dolt push'")
|
|
}
|
|
|
|
if !strings.Contains(section, "git push") {
|
|
t.Error("should still contain 'git push'")
|
|
}
|
|
}
|
|
|
|
func TestRenderSectionWithOptsHasRemoteIncludesDoltPush(t *testing.T) {
|
|
opts := RenderOpts{HasRemote: true}
|
|
for _, profile := range []Profile{ProfileFull, ProfileMinimal} {
|
|
section := RenderSectionWithOpts(profile, opts)
|
|
if !strings.Contains(section, "bd dolt push") {
|
|
t.Errorf("%s profile with HasRemote=true should contain 'bd dolt push'", profile)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestRenderSectionBackwardCompatIncludesDoltPush(t *testing.T) {
|
|
// RenderSection (no opts) should default to HasRemote=true for backward compat
|
|
for _, profile := range []Profile{ProfileFull, ProfileMinimal} {
|
|
section := RenderSection(profile)
|
|
if !strings.Contains(section, "bd dolt push") {
|
|
t.Errorf("RenderSection(%s) should include 'bd dolt push' (backward compat)", profile)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestRenderSectionWithOptsDifferentHashes(t *testing.T) {
|
|
// HasRemote=true and HasRemote=false should produce different hashes
|
|
// since the content differs
|
|
withRemote := RenderSectionWithOpts(ProfileFull, RenderOpts{HasRemote: true})
|
|
withoutRemote := RenderSectionWithOpts(ProfileFull, RenderOpts{HasRemote: false})
|
|
|
|
meta1 := ParseMarker(strings.SplitN(withRemote, "\n", 2)[0])
|
|
meta2 := ParseMarker(strings.SplitN(withoutRemote, "\n", 2)[0])
|
|
|
|
if meta1 == nil || meta2 == nil {
|
|
t.Fatal("ParseMarker returned nil")
|
|
}
|
|
if meta1.Hash == meta2.Hash {
|
|
t.Error("HasRemote=true and HasRemote=false should produce different hashes")
|
|
}
|
|
}
|
|
|
|
func TestReplaceSectionWithOptsIdempotent(t *testing.T) {
|
|
opts := RenderOpts{HasRemote: false}
|
|
section := RenderSectionWithOpts(ProfileFull, opts)
|
|
content := "# Header\n\n" + section + "\n# Footer\n"
|
|
|
|
result, changed, err := ReplaceSectionWithOpts(content, ProfileFull, opts)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if changed {
|
|
t.Error("should not change when section is already current with same opts")
|
|
}
|
|
if result != content {
|
|
t.Error("content should be unchanged")
|
|
}
|
|
}
|
|
|
|
func TestReplaceSectionWithOptsDetectsRemoteChange(t *testing.T) {
|
|
// Render with remote, then replace with no-remote opts → should detect as stale
|
|
withRemote := RenderSectionWithOpts(ProfileFull, RenderOpts{HasRemote: true})
|
|
content := "# Header\n\n" + withRemote + "\n# Footer\n"
|
|
|
|
noRemoteOpts := RenderOpts{HasRemote: false}
|
|
result, changed, err := ReplaceSectionWithOpts(content, ProfileFull, noRemoteOpts)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if !changed {
|
|
t.Error("should detect stale hash when remote state changes")
|
|
}
|
|
if strings.Contains(result, "bd dolt push") {
|
|
t.Error("replaced content should not contain 'bd dolt push' with HasRemote=false")
|
|
}
|
|
}
|
|
|
|
// A user-level section loads in every repository, so it must not say the
|
|
// current one uses bd, and its task-tracking rules hold only in repositories
|
|
// that do. The wording is matched against the templates' text, so a template
|
|
// edit that the rewording no longer finds fails here.
|
|
func TestRenderSectionUserLevelAppliesOnlyInBeadsRepositories(t *testing.T) {
|
|
for _, profile := range []Profile{ProfileMinimal, ProfileFull} {
|
|
section := RenderSectionWithOpts(profile, UserLevelRenderOpts())
|
|
if strings.Contains(section, "This project uses") {
|
|
t.Errorf("%s user-level section says the current project uses bd:\n%s", profile, section)
|
|
}
|
|
if n := strings.Count(section, userLevelScope); n != 1 {
|
|
t.Errorf("%s user-level section states its scope %d times, want 1", profile, n)
|
|
}
|
|
}
|
|
rules := map[Profile][]string{
|
|
ProfileMinimal: {
|
|
"- In a beads repository, use `bd` for ALL task tracking — do NOT use TodoWrite",
|
|
"- In a beads repository, use `bd remember` for durable project facts",
|
|
},
|
|
ProfileFull: {
|
|
"**IMPORTANT**: In a beads repository, use **bd (beads)** for ALL issue tracking.",
|
|
"- ✅ In a beads repository, use bd for ALL task tracking",
|
|
},
|
|
}
|
|
for profile, want := range rules {
|
|
section := RenderSectionWithOpts(profile, UserLevelRenderOpts())
|
|
for _, rule := range want {
|
|
if !strings.Contains(section, rule) {
|
|
t.Errorf("%s user-level section lacks the scoped rule %q", profile, rule)
|
|
}
|
|
}
|
|
}
|
|
}
|