|
| 1 | +import { describe, test, expect } from 'bun:test'; |
| 2 | +import { spawnSync } from 'child_process'; |
| 3 | +import * as path from 'path'; |
| 4 | +import * as fs from 'fs'; |
| 5 | +import * as os from 'os'; |
| 6 | + |
| 7 | +const ROOT = path.resolve(import.meta.dir, '..'); |
| 8 | +const SETUP_SCRIPT = path.join(ROOT, 'setup'); |
| 9 | + |
| 10 | +describe('setup: Conductor worktree guard', () => { |
| 11 | + test('setup contains the real-dir guard before the ln -snf into ~/.claude/skills/', () => { |
| 12 | + const content = fs.readFileSync(SETUP_SCRIPT, 'utf-8'); |
| 13 | + const guardIdx = content.indexOf('_SKIP_CLAUDE_REGISTER=0'); |
| 14 | + const lnIdx = content.indexOf('ln -snf "$SOURCE_GSTACK_DIR" "$CLAUDE_GSTACK_LINK"'); |
| 15 | + expect(guardIdx).toBeGreaterThan(-1); |
| 16 | + expect(lnIdx).toBeGreaterThan(-1); |
| 17 | + expect(guardIdx).toBeLessThan(lnIdx); |
| 18 | + }); |
| 19 | + |
| 20 | + test('guard resolves the existing real dir with `pwd -P` and compares against source', () => { |
| 21 | + const content = fs.readFileSync(SETUP_SCRIPT, 'utf-8'); |
| 22 | + expect(content).toContain('[ -d "$CLAUDE_GSTACK_LINK" ] && [ ! -L "$CLAUDE_GSTACK_LINK" ]'); |
| 23 | + expect(content).toContain('cd "$CLAUDE_GSTACK_LINK" 2>/dev/null && pwd -P'); |
| 24 | + expect(content).toContain('"$_EXISTING_REAL" != "$SOURCE_GSTACK_DIR"'); |
| 25 | + }); |
| 26 | + |
| 27 | + test('skip branch prints "registration skipped" + remediation hint', () => { |
| 28 | + const content = fs.readFileSync(SETUP_SCRIPT, 'utf-8'); |
| 29 | + expect(content).toContain('Skipping Claude skill registration'); |
| 30 | + expect(content).toContain('claude registration skipped'); |
| 31 | + expect(content).toContain('rm -rf $CLAUDE_GSTACK_LINK'); |
| 32 | + }); |
| 33 | + |
| 34 | + // Reproduce the BSD/macOS `ln -snf` behavior that caused the bug, then |
| 35 | + // confirm the guard avoids it. This is a behavioral test of the guard logic |
| 36 | + // running in an isolated tmpdir — not the full setup script. |
| 37 | + test('BSD ln -snf into an existing real dir creates a child symlink (bug reproduces)', () => { |
| 38 | + const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'gstack-setup-guard-')); |
| 39 | + try { |
| 40 | + const source = path.join(tmp, 'source-worktree'); |
| 41 | + const dest = path.join(tmp, 'dest-real-dir'); |
| 42 | + fs.mkdirSync(source); |
| 43 | + fs.mkdirSync(dest); |
| 44 | + // The buggy invocation: target dest is an existing real dir. |
| 45 | + const result = spawnSync('ln', ['-snf', source, dest], { encoding: 'utf-8' }); |
| 46 | + expect(result.status).toBe(0); |
| 47 | + // Child symlink leaked inside dest. |
| 48 | + const leaked = path.join(dest, path.basename(source)); |
| 49 | + expect(fs.existsSync(leaked)).toBe(true); |
| 50 | + expect(fs.lstatSync(leaked).isSymbolicLink()).toBe(true); |
| 51 | + expect(fs.readlinkSync(leaked)).toBe(source); |
| 52 | + // dest itself stayed a real directory (not replaced). |
| 53 | + expect(fs.lstatSync(dest).isSymbolicLink()).toBe(false); |
| 54 | + expect(fs.lstatSync(dest).isDirectory()).toBe(true); |
| 55 | + } finally { |
| 56 | + fs.rmSync(tmp, { recursive: true, force: true }); |
| 57 | + } |
| 58 | + }); |
| 59 | + |
| 60 | + test('guard logic refuses to ln when dest is a real dir pointing elsewhere', () => { |
| 61 | + const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'gstack-setup-guard-')); |
| 62 | + try { |
| 63 | + const source = path.join(tmp, 'source-worktree'); |
| 64 | + const dest = path.join(tmp, 'dest-real-dir'); |
| 65 | + fs.mkdirSync(source); |
| 66 | + fs.mkdirSync(dest); |
| 67 | + // Inline the guard logic from setup. If it triggers, $_SKIP=1 is echoed |
| 68 | + // and no ln is performed; otherwise ln runs and we'd see the leak. |
| 69 | + const script = ` |
| 70 | + set -e |
| 71 | + SOURCE_GSTACK_DIR='${source}' |
| 72 | + CLAUDE_GSTACK_LINK='${dest}' |
| 73 | + _SKIP_CLAUDE_REGISTER=0 |
| 74 | + if [ -d "$CLAUDE_GSTACK_LINK" ] && [ ! -L "$CLAUDE_GSTACK_LINK" ]; then |
| 75 | + _EXISTING_REAL=$(cd "$CLAUDE_GSTACK_LINK" 2>/dev/null && pwd -P || echo "") |
| 76 | + if [ -n "$_EXISTING_REAL" ] && [ "$_EXISTING_REAL" != "$SOURCE_GSTACK_DIR" ]; then |
| 77 | + _SKIP_CLAUDE_REGISTER=1 |
| 78 | + fi |
| 79 | + fi |
| 80 | + if [ "$_SKIP_CLAUDE_REGISTER" -eq 1 ]; then |
| 81 | + echo "SKIP" |
| 82 | + else |
| 83 | + ln -snf "$SOURCE_GSTACK_DIR" "$CLAUDE_GSTACK_LINK" |
| 84 | + echo "LINKED" |
| 85 | + fi |
| 86 | + `; |
| 87 | + const result = spawnSync('bash', ['-c', script], { encoding: 'utf-8' }); |
| 88 | + expect(result.status).toBe(0); |
| 89 | + expect(result.stdout.trim()).toBe('SKIP'); |
| 90 | + // No child symlink leaked. |
| 91 | + const leaked = path.join(dest, path.basename(source)); |
| 92 | + expect(fs.existsSync(leaked)).toBe(false); |
| 93 | + } finally { |
| 94 | + fs.rmSync(tmp, { recursive: true, force: true }); |
| 95 | + } |
| 96 | + }); |
| 97 | + |
| 98 | + test('guard allows ln when dest does not exist (fresh install path)', () => { |
| 99 | + const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'gstack-setup-guard-')); |
| 100 | + try { |
| 101 | + const source = path.join(tmp, 'source-worktree'); |
| 102 | + const dest = path.join(tmp, 'fresh-dest'); |
| 103 | + fs.mkdirSync(source); |
| 104 | + const script = ` |
| 105 | + set -e |
| 106 | + SOURCE_GSTACK_DIR='${source}' |
| 107 | + CLAUDE_GSTACK_LINK='${dest}' |
| 108 | + _SKIP_CLAUDE_REGISTER=0 |
| 109 | + if [ -d "$CLAUDE_GSTACK_LINK" ] && [ ! -L "$CLAUDE_GSTACK_LINK" ]; then |
| 110 | + _EXISTING_REAL=$(cd "$CLAUDE_GSTACK_LINK" 2>/dev/null && pwd -P || echo "") |
| 111 | + if [ -n "$_EXISTING_REAL" ] && [ "$_EXISTING_REAL" != "$SOURCE_GSTACK_DIR" ]; then |
| 112 | + _SKIP_CLAUDE_REGISTER=1 |
| 113 | + fi |
| 114 | + fi |
| 115 | + if [ "$_SKIP_CLAUDE_REGISTER" -eq 1 ]; then |
| 116 | + echo "SKIP" |
| 117 | + else |
| 118 | + ln -snf "$SOURCE_GSTACK_DIR" "$CLAUDE_GSTACK_LINK" |
| 119 | + echo "LINKED" |
| 120 | + fi |
| 121 | + `; |
| 122 | + const result = spawnSync('bash', ['-c', script], { encoding: 'utf-8' }); |
| 123 | + expect(result.status).toBe(0); |
| 124 | + expect(result.stdout.trim()).toBe('LINKED'); |
| 125 | + expect(fs.lstatSync(dest).isSymbolicLink()).toBe(true); |
| 126 | + expect(fs.readlinkSync(dest)).toBe(source); |
| 127 | + } finally { |
| 128 | + fs.rmSync(tmp, { recursive: true, force: true }); |
| 129 | + } |
| 130 | + }); |
| 131 | + |
| 132 | + test('guard allows ln when dest is an existing symlink (upgrade-in-place path)', () => { |
| 133 | + const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'gstack-setup-guard-')); |
| 134 | + try { |
| 135 | + const source = path.join(tmp, 'new-source'); |
| 136 | + const oldSource = path.join(tmp, 'old-source'); |
| 137 | + const dest = path.join(tmp, 'dest-symlink'); |
| 138 | + fs.mkdirSync(source); |
| 139 | + fs.mkdirSync(oldSource); |
| 140 | + fs.symlinkSync(oldSource, dest); |
| 141 | + // Existing symlink: -L is true, so the guard does NOT trigger. ln -snf |
| 142 | + // should atomically retarget the symlink to the new source. |
| 143 | + const script = ` |
| 144 | + set -e |
| 145 | + SOURCE_GSTACK_DIR='${source}' |
| 146 | + CLAUDE_GSTACK_LINK='${dest}' |
| 147 | + _SKIP_CLAUDE_REGISTER=0 |
| 148 | + if [ -d "$CLAUDE_GSTACK_LINK" ] && [ ! -L "$CLAUDE_GSTACK_LINK" ]; then |
| 149 | + _EXISTING_REAL=$(cd "$CLAUDE_GSTACK_LINK" 2>/dev/null && pwd -P || echo "") |
| 150 | + if [ -n "$_EXISTING_REAL" ] && [ "$_EXISTING_REAL" != "$SOURCE_GSTACK_DIR" ]; then |
| 151 | + _SKIP_CLAUDE_REGISTER=1 |
| 152 | + fi |
| 153 | + fi |
| 154 | + if [ "$_SKIP_CLAUDE_REGISTER" -eq 1 ]; then |
| 155 | + echo "SKIP" |
| 156 | + else |
| 157 | + ln -snf "$SOURCE_GSTACK_DIR" "$CLAUDE_GSTACK_LINK" |
| 158 | + echo "LINKED" |
| 159 | + fi |
| 160 | + `; |
| 161 | + const result = spawnSync('bash', ['-c', script], { encoding: 'utf-8' }); |
| 162 | + expect(result.status).toBe(0); |
| 163 | + expect(result.stdout.trim()).toBe('LINKED'); |
| 164 | + expect(fs.readlinkSync(dest)).toBe(source); |
| 165 | + } finally { |
| 166 | + fs.rmSync(tmp, { recursive: true, force: true }); |
| 167 | + } |
| 168 | + }); |
| 169 | + |
| 170 | + test('guard allows ln when dest is a real dir already pointing to source (self-rerun)', () => { |
| 171 | + const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'gstack-setup-guard-')); |
| 172 | + try { |
| 173 | + const source = path.join(tmp, 'source-worktree'); |
| 174 | + fs.mkdirSync(source); |
| 175 | + // Mirror setup's SOURCE_GSTACK_DIR resolution (`pwd -P`) so the comparison |
| 176 | + // is fair on macOS where /tmp itself is a symlink to /private/tmp. |
| 177 | + const resolvedSource = fs.realpathSync(source); |
| 178 | + // Degenerate case: existing real dir IS the source. |
| 179 | + const dest = source; |
| 180 | + const script = ` |
| 181 | + set -e |
| 182 | + SOURCE_GSTACK_DIR='${resolvedSource}' |
| 183 | + CLAUDE_GSTACK_LINK='${dest}' |
| 184 | + _SKIP_CLAUDE_REGISTER=0 |
| 185 | + if [ -d "$CLAUDE_GSTACK_LINK" ] && [ ! -L "$CLAUDE_GSTACK_LINK" ]; then |
| 186 | + _EXISTING_REAL=$(cd "$CLAUDE_GSTACK_LINK" 2>/dev/null && pwd -P || echo "") |
| 187 | + if [ -n "$_EXISTING_REAL" ] && [ "$_EXISTING_REAL" != "$SOURCE_GSTACK_DIR" ]; then |
| 188 | + _SKIP_CLAUDE_REGISTER=1 |
| 189 | + fi |
| 190 | + fi |
| 191 | + echo "skip=$_SKIP_CLAUDE_REGISTER" |
| 192 | + `; |
| 193 | + const result = spawnSync('bash', ['-c', script], { encoding: 'utf-8' }); |
| 194 | + expect(result.status).toBe(0); |
| 195 | + expect(result.stdout.trim()).toBe('skip=0'); |
| 196 | + } finally { |
| 197 | + fs.rmSync(tmp, { recursive: true, force: true }); |
| 198 | + } |
| 199 | + }); |
| 200 | +}); |
0 commit comments