fix(scripts): 5 critical bugs from Phase 9 self-test
Bug 1: merge-mcp.sh fails on Cyrillic brain repo paths (jq fopen UTF-8 issue)
→ use --argjson with $(cat ...) instead of --slurpfile
Bug 2: install.sh stripped only trailing .template, missed middle .template.
→ strip both '.template.' middle and '.template' trailing patterns
Bug 3: manifest.json used brain-repo paths but verify.sh checked target paths
→ restructure manifest.files into {project-mode, user-mode, brain-internal} maps
→ verify.sh now picks map based on detected target mode
Bug 4: make_backup_dir created empty backup dir, no file preservation
→ rsync/cp -r target tree to backup before any modifications
Bug 5: brain's project-files/README.md overwrote consumer's README.md
→ install.sh now skips brain-internal READMEs in project/user copy loops
Phase 9 self-test re-run on c:/tmp/test-consumer-fix: install + verify both PASS.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -46,6 +46,83 @@ BRAIN_ROOT="$brain_dir" bash "$brain_dir/scripts/install.sh" --target="$tmpdir"
|
||||
|
||||
rm -rf "$tmpdir" "$brain_dir"
|
||||
|
||||
# Test 4: template strip handles BOTH middle .template. and trailing .template
|
||||
# Bug 2 regression: files like docs/CHANGELOG_claude_md.template.md must become
|
||||
# docs/CHANGELOG_claude_md.md, not be left with .template. in the middle.
|
||||
tmpdir=$(mktemp -d)
|
||||
mkdir "$tmpdir/docs"
|
||||
brain_dir=$(mktemp -d)
|
||||
mkdir -p "$brain_dir/project-files/docs" "$brain_dir/scripts/lib"
|
||||
echo "# trailing" > "$brain_dir/project-files/CLAUDE.md.template"
|
||||
echo "# middle" > "$brain_dir/project-files/docs/CHANGELOG_claude_md.template.md"
|
||||
echo '{"version":"brain-v1.0","files":{}}' > "$brain_dir/manifest.json"
|
||||
cp "$SCRIPT_DIR/install.sh" "$brain_dir/scripts/"
|
||||
cp "$SCRIPT_DIR/verify.sh" "$brain_dir/scripts/"
|
||||
cp "$SCRIPT_DIR/lib/common.sh" "$brain_dir/scripts/lib/"
|
||||
|
||||
BRAIN_ROOT="$brain_dir" bash "$brain_dir/scripts/install.sh" --target="$tmpdir" --version=brain-v1.0 --with-plugins=no --with-mcp=no --skip-secrets --force >/dev/null 2>&1
|
||||
|
||||
# Trailing .template stripped → CLAUDE.md
|
||||
[ -f "$tmpdir/CLAUDE.md" ] && assert_eq "yes" "yes" "template strip: trailing .template removed (CLAUDE.md)" || assert_eq "yes" "no" "template strip: trailing .template removed (CLAUDE.md)"
|
||||
|
||||
# Middle .template. stripped → docs/CHANGELOG_claude_md.md
|
||||
[ -f "$tmpdir/docs/CHANGELOG_claude_md.md" ] && assert_eq "yes" "yes" "template strip: middle .template. removed (CHANGELOG_claude_md.md)" || assert_eq "yes" "no" "template strip: middle .template. removed (CHANGELOG_claude_md.md)"
|
||||
|
||||
# Negative: file with .template. in middle must NOT exist with .template. still in name
|
||||
[ ! -f "$tmpdir/docs/CHANGELOG_claude_md.template.md" ] && assert_eq "yes" "yes" "template strip: no leftover .template. in name" || assert_eq "yes" "no" "template strip: no leftover .template. in name"
|
||||
|
||||
rm -rf "$tmpdir" "$brain_dir"
|
||||
|
||||
# Test 5: backup directory actually contains pre-existing consumer files (Bug 4)
|
||||
tmpdir=$(mktemp -d)
|
||||
mkdir "$tmpdir/docs"
|
||||
echo "ORIGINAL_README_CONTENT" > "$tmpdir/README.md"
|
||||
echo "ORIGINAL_MARKER" > "$tmpdir/CONSUMER_KEEP.txt"
|
||||
brain_dir=$(mktemp -d)
|
||||
mkdir -p "$brain_dir/project-files/docs" "$brain_dir/scripts/lib"
|
||||
echo "# brain template" > "$brain_dir/project-files/CLAUDE.md.template"
|
||||
echo '{"version":"brain-v1.0","files":{}}' > "$brain_dir/manifest.json"
|
||||
cp "$SCRIPT_DIR/install.sh" "$brain_dir/scripts/"
|
||||
cp "$SCRIPT_DIR/verify.sh" "$brain_dir/scripts/"
|
||||
cp "$SCRIPT_DIR/lib/common.sh" "$brain_dir/scripts/lib/"
|
||||
|
||||
BRAIN_ROOT="$brain_dir" bash "$brain_dir/scripts/install.sh" --target="$tmpdir" --version=brain-v1.0 --with-plugins=no --with-mcp=no --skip-secrets >/dev/null 2>&1
|
||||
|
||||
# Backup dir should exist
|
||||
backup_count=$(find "$tmpdir" -maxdepth 1 -type d -name ".brain-backup-*" | wc -l)
|
||||
assert_eq "1" "$backup_count" "backup: .brain-backup-<ts>/ created"
|
||||
|
||||
# Backup should contain consumer's original README.md
|
||||
backup_dir=$(find "$tmpdir" -maxdepth 1 -type d -name ".brain-backup-*" | head -1)
|
||||
[ -f "$backup_dir/README.md" ] && assert_eq "yes" "yes" "backup: README.md preserved" || assert_eq "yes" "no" "backup: README.md preserved"
|
||||
backup_readme_content=$(cat "$backup_dir/README.md" 2>/dev/null || echo "")
|
||||
assert_eq "ORIGINAL_README_CONTENT" "$backup_readme_content" "backup: README.md content matches original"
|
||||
|
||||
# Backup should also contain CONSUMER_KEEP.txt
|
||||
[ -f "$backup_dir/CONSUMER_KEEP.txt" ] && assert_eq "yes" "yes" "backup: CONSUMER_KEEP.txt preserved" || assert_eq "yes" "no" "backup: CONSUMER_KEEP.txt preserved"
|
||||
|
||||
rm -rf "$tmpdir" "$brain_dir"
|
||||
|
||||
# Test 6: consumer's pre-existing README.md is NOT overwritten by brain's project-files/README.md (Bug 5)
|
||||
tmpdir=$(mktemp -d)
|
||||
mkdir "$tmpdir/docs"
|
||||
echo "CONSUMER_ORIGINAL_README" > "$tmpdir/README.md"
|
||||
brain_dir=$(mktemp -d)
|
||||
mkdir -p "$brain_dir/project-files/docs" "$brain_dir/scripts/lib"
|
||||
echo "# brain template" > "$brain_dir/project-files/CLAUDE.md.template"
|
||||
echo "BRAIN_INTERNAL_README" > "$brain_dir/project-files/README.md"
|
||||
echo '{"version":"brain-v1.0","files":{}}' > "$brain_dir/manifest.json"
|
||||
cp "$SCRIPT_DIR/install.sh" "$brain_dir/scripts/"
|
||||
cp "$SCRIPT_DIR/verify.sh" "$brain_dir/scripts/"
|
||||
cp "$SCRIPT_DIR/lib/common.sh" "$brain_dir/scripts/lib/"
|
||||
|
||||
BRAIN_ROOT="$brain_dir" bash "$brain_dir/scripts/install.sh" --target="$tmpdir" --version=brain-v1.0 --with-plugins=no --with-mcp=no --skip-secrets >/dev/null 2>&1
|
||||
|
||||
readme_content=$(cat "$tmpdir/README.md")
|
||||
assert_eq "CONSUMER_ORIGINAL_README" "$readme_content" "consumer README.md preserved (not overwritten by brain's internal README.md)"
|
||||
|
||||
rm -rf "$tmpdir" "$brain_dir"
|
||||
|
||||
echo "---"
|
||||
echo "Failures: $FAILURES"
|
||||
exit $FAILURES
|
||||
|
||||
@@ -68,6 +68,29 @@ assert_eq "npx" "$pw" "project-mode: playwright added"
|
||||
|
||||
rm -rf "$tmpdir"
|
||||
|
||||
# Test 3: Cyrillic path (Bug 1 regression) — jq must handle non-ASCII paths
|
||||
# On Windows MSYS2 jq's fopen cannot open Cyrillic paths via --slurpfile.
|
||||
# Fix uses --argjson with $(cat …) — shell handles UTF-8 paths fine.
|
||||
cyr_dir="/tmp/тест-merge-$$"
|
||||
mkdir -p "$cyr_dir"
|
||||
cat > "$cyr_dir/brain.json" <<'EOF4'
|
||||
{"mcpServers":{"playwright":{"command":"npx","args":["-y","@playwright/mcp"]}}}
|
||||
EOF4
|
||||
echo '{"mcpServers":{"laravel-boost":{"command":"php"}}}' > "$cyr_dir/target.json"
|
||||
|
||||
bash "$SCRIPT_DIR/lib/merge-mcp.sh" --mode=project "$cyr_dir/target.json" "$cyr_dir/brain.json" 2>/dev/null
|
||||
merge_exit=$?
|
||||
assert_eq "0" "$merge_exit" "cyrillic-path: merge-mcp.sh exits 0"
|
||||
|
||||
if [ "$merge_exit" -eq 0 ]; then
|
||||
pw=$(jq -r '.mcpServers.playwright.command' "$cyr_dir/target.json" 2>/dev/null || echo "")
|
||||
assert_eq "npx" "$pw" "cyrillic-path: playwright merged from Cyrillic brain path"
|
||||
lb=$(jq -r '.mcpServers["laravel-boost"].command' "$cyr_dir/target.json" 2>/dev/null || echo "")
|
||||
assert_eq "php" "$lb" "cyrillic-path: laravel-boost preserved"
|
||||
fi
|
||||
|
||||
rm -rf "$cyr_dir"
|
||||
|
||||
echo "---"
|
||||
echo "Failures: $FAILURES"
|
||||
exit $FAILURES
|
||||
|
||||
@@ -25,6 +25,74 @@ BRAIN_ROOT="$brain_dir" bash "$SCRIPT_DIR/verify.sh" --target="$tmpdir" >/dev/nu
|
||||
assert_eq "0" "$?" "valid .brain-version returns exit 0"
|
||||
rm -rf "$tmpdir" "$brain_dir"
|
||||
|
||||
# Test 3: project-mode verify with real consumer layout (Bug 3 regression)
|
||||
# Manifest uses project-mode section with target-relative paths.
|
||||
tmpdir=$(mktemp -d)
|
||||
echo "brain-v1.0" > "$tmpdir/.brain-version"
|
||||
echo "sha: deadbeef" >> "$tmpdir/.brain-version"
|
||||
mkdir -p "$tmpdir/docs"
|
||||
echo "consumer CLAUDE" > "$tmpdir/CLAUDE.md"
|
||||
echo "consumer pravila" > "$tmpdir/docs/Pravila_raboty_Claude.md"
|
||||
|
||||
# Compute SHAs
|
||||
sha_claude=$(sha256sum "$tmpdir/CLAUDE.md" | cut -d' ' -f1)
|
||||
sha_pravila=$(sha256sum "$tmpdir/docs/Pravila_raboty_Claude.md" | cut -d' ' -f1)
|
||||
|
||||
brain_dir=$(mktemp -d)
|
||||
cat > "$brain_dir/manifest.json" <<EOF7
|
||||
{
|
||||
"version": "brain-v1.0",
|
||||
"files": {
|
||||
"project-mode": {
|
||||
"CLAUDE.md": "$sha_claude",
|
||||
"docs/Pravila_raboty_Claude.md": "$sha_pravila"
|
||||
},
|
||||
"user-mode": {},
|
||||
"brain-internal": {}
|
||||
}
|
||||
}
|
||||
EOF7
|
||||
|
||||
BRAIN_ROOT="$brain_dir" bash "$SCRIPT_DIR/verify.sh" --target="$tmpdir" >/tmp/verify_test3.log 2>&1
|
||||
exit_code=$?
|
||||
assert_eq "0" "$exit_code" "project-mode: verify passes on consumer layout"
|
||||
|
||||
# Verify it used the right section
|
||||
grep -q "Manifest section: project-mode" /tmp/verify_test3.log && \
|
||||
assert_eq "yes" "yes" "project-mode: section auto-detected" || \
|
||||
assert_eq "yes" "no" "project-mode: section auto-detected"
|
||||
|
||||
rm -rf "$tmpdir" "$brain_dir" /tmp/verify_test3.log
|
||||
|
||||
# Test 4: brain-internal verify when target is brain root itself
|
||||
brain_dir=$(mktemp -d)
|
||||
mkdir -p "$brain_dir/project-files"
|
||||
echo "brain-v1.0" > "$brain_dir/.brain-version"
|
||||
echo "sha: deadbeef" >> "$brain_dir/.brain-version"
|
||||
echo "internal" > "$brain_dir/project-files/CLAUDE.md.template"
|
||||
sha_internal=$(sha256sum "$brain_dir/project-files/CLAUDE.md.template" | cut -d' ' -f1)
|
||||
cat > "$brain_dir/manifest.json" <<EOF8
|
||||
{
|
||||
"version": "brain-v1.0",
|
||||
"files": {
|
||||
"project-mode": {},
|
||||
"user-mode": {},
|
||||
"brain-internal": {
|
||||
"project-files/CLAUDE.md.template": "$sha_internal"
|
||||
}
|
||||
}
|
||||
}
|
||||
EOF8
|
||||
|
||||
BRAIN_ROOT="$brain_dir" bash "$SCRIPT_DIR/verify.sh" --target="$brain_dir" >/tmp/verify_test4.log 2>&1
|
||||
exit_code=$?
|
||||
assert_eq "0" "$exit_code" "brain-internal: verify passes on brain root"
|
||||
grep -q "Manifest section: brain-internal" /tmp/verify_test4.log && \
|
||||
assert_eq "yes" "yes" "brain-internal: section auto-detected" || \
|
||||
assert_eq "yes" "no" "brain-internal: section auto-detected"
|
||||
|
||||
rm -rf "$brain_dir" /tmp/verify_test4.log
|
||||
|
||||
echo "---"
|
||||
echo "Failures: $FAILURES"
|
||||
exit $FAILURES
|
||||
|
||||
Reference in New Issue
Block a user