|
| 1 | +import { describe, test, expect } from 'bun:test'; |
| 2 | +import { spawnSync } from 'child_process'; |
| 3 | +import * as fs from 'fs'; |
| 4 | +import * as path from 'path'; |
| 5 | +import * as os from 'os'; |
| 6 | + |
| 7 | +const ROOT = path.resolve(import.meta.dir, '..'); |
| 8 | +const SETUP_SRC = fs.readFileSync(path.join(ROOT, 'setup'), 'utf-8'); |
| 9 | + |
| 10 | +// Run a bash snippet, return {stdout, stderr, status}. |
| 11 | +function runBash(script: string): { stdout: string; stderr: string; status: number } { |
| 12 | + const r = spawnSync('bash', ['-c', script], { encoding: 'utf-8' }); |
| 13 | + return { stdout: r.stdout || '', stderr: r.stderr || '', status: r.status ?? -1 }; |
| 14 | +} |
| 15 | + |
| 16 | +describe('setup: gen:skill-docs:user exit-code propagation (pipe-masking fix)', () => { |
| 17 | + // The bug: `cmd 2>&1 | tail -3` makes the subshell exit status `tail`'s, |
| 18 | + // so `(...) || log "warning"` never fires when `cmd` fails. The fix removes |
| 19 | + // the pipe. These tests RUN the pattern (not grep source) to prove the |
| 20 | + // exit-code semantics actually change. |
| 21 | + |
| 22 | + test('without pipe: failing bun_cmd triggers the || warning clause', () => { |
| 23 | + const r = runBash(` |
| 24 | + set +e |
| 25 | + bun_cmd() { return 1; } # stub: gen:skill-docs:user failed |
| 26 | + log() { echo "LOG:$*"; } |
| 27 | + ( |
| 28 | + cd /tmp |
| 29 | + bun_cmd run gen:skill-docs:user --host claude |
| 30 | + ) || log " warning: gen:skill-docs:user failed" |
| 31 | + `); |
| 32 | + expect(r.stdout).toContain('LOG: warning: gen:skill-docs:user failed'); |
| 33 | + }); |
| 34 | + |
| 35 | + test('with pipe (the bug shape): failing bun_cmd does NOT trigger the warning', () => { |
| 36 | + const r = runBash(` |
| 37 | + set +e |
| 38 | + bun_cmd() { return 1; } # stub: gen:skill-docs:user failed |
| 39 | + log() { echo "LOG:$*"; } |
| 40 | + ( |
| 41 | + cd /tmp |
| 42 | + bun_cmd run gen:skill-docs:user --host claude 2>&1 | tail -3 |
| 43 | + ) || log " warning: gen:skill-docs:user failed" |
| 44 | + `); |
| 45 | + expect(r.stdout).not.toContain('LOG: warning'); |
| 46 | + }); |
| 47 | + |
| 48 | + test('setup: the live gbrain regen block has no pipe before the || guard', () => { |
| 49 | + // Slice the exact block from setup and confirm the fix is in place |
| 50 | + // without resorting to a fragile line-number check. |
| 51 | + const start = SETUP_SRC.indexOf('gbrain detected — regenerating'); |
| 52 | + expect(start).toBeGreaterThan(-1); |
| 53 | + const end = SETUP_SRC.indexOf('|| log', start); |
| 54 | + expect(end).toBeGreaterThan(start); |
| 55 | + const block = SETUP_SRC.slice(start, end); |
| 56 | + expect(block).toContain('bun_cmd run gen:skill-docs:user --host claude'); |
| 57 | + // The bug shape: `... | tail -N` between the call and the `|| log` guard. |
| 58 | + expect(block).not.toMatch(/gen:skill-docs:user[^\n]*\|\s*tail/); |
| 59 | + }); |
| 60 | +}); |
| 61 | + |
| 62 | +describe('setup: bun_cmd routing in link_*_skill_dirs (Windows non-ASCII path fix)', () => { |
| 63 | + // The bug: `bun run ...` bypasses the BUN_CMD wrapper installed by |
| 64 | + // prepare_bun_for_windows_compile. On a non-ASCII Windows username, |
| 65 | + // BUN_CMD points to an ASCII-path copy of bun and the literal `bun` |
| 66 | + // on PATH may not work. Test by stubbing BUN_CMD to a sentinel that |
| 67 | + // writes a marker file, and proving the wrapper path actually invokes it. |
| 68 | + |
| 69 | + test('bun_cmd wrapper invokes $BUN_CMD (not literal bun on PATH)', () => { |
| 70 | + const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'gstack-buncmd-')); |
| 71 | + const marker = path.join(tmp, 'invoked'); |
| 72 | + const sentinel = path.join(tmp, 'fake-bun'); |
| 73 | + fs.writeFileSync(sentinel, `#!/usr/bin/env bash\necho "ARGS:$*" > "${marker}"\n`); |
| 74 | + fs.chmodSync(sentinel, 0o755); |
| 75 | + |
| 76 | + const r = runBash(` |
| 77 | + set -e |
| 78 | + BUN_CMD="${sentinel}" |
| 79 | + bun_cmd() { "$BUN_CMD" "$@"; } |
| 80 | + ( cd /tmp && bun_cmd run gen:skill-docs --host codex ) |
| 81 | + `); |
| 82 | + |
| 83 | + expect(r.status).toBe(0); |
| 84 | + expect(fs.existsSync(marker)).toBe(true); |
| 85 | + expect(fs.readFileSync(marker, 'utf-8').trim()).toBe('ARGS:run gen:skill-docs --host codex'); |
| 86 | + |
| 87 | + fs.rmSync(tmp, { recursive: true, force: true }); |
| 88 | + }); |
| 89 | + |
| 90 | + test('setup: the three link_*_skill_dirs helpers all use bun_cmd, not literal bun', () => { |
| 91 | + // Extract each helper body and check the gen:skill-docs invocation |
| 92 | + // inside it. Source-anchored (not line-number) and not a global grep, |
| 93 | + // so we only assert about the actual code path the bug fix touches. |
| 94 | + for (const fn of [ |
| 95 | + 'link_codex_skill_dirs', |
| 96 | + 'link_factory_skill_dirs', |
| 97 | + 'link_opencode_skill_dirs', |
| 98 | + ]) { |
| 99 | + const start = SETUP_SRC.indexOf(`${fn}() {`); |
| 100 | + expect(start).toBeGreaterThan(-1); |
| 101 | + const end = SETUP_SRC.indexOf('\n}\n', start); |
| 102 | + expect(end).toBeGreaterThan(start); |
| 103 | + const body = SETUP_SRC.slice(start, end); |
| 104 | + // Must call through the wrapper. |
| 105 | + expect(body).toMatch(/bun_cmd run gen:skill-docs/); |
| 106 | + // Bug shape: a literal `bun run gen:skill-docs` in executable position |
| 107 | + // (skipping any comment / warning-string mentions that aren't being run). |
| 108 | + const lines = body.split('\n').filter((l) => { |
| 109 | + const t = l.trim(); |
| 110 | + if (!t || t.startsWith('#')) return false; |
| 111 | + // Strings inside echo/warning messages don't execute bun. |
| 112 | + if (/echo\s+['"]/.test(t)) return false; |
| 113 | + return true; |
| 114 | + }); |
| 115 | + for (const l of lines) { |
| 116 | + // `bun_cmd run ...` is fine; a bare `bun run ...` is the bug. |
| 117 | + const stripped = l.replace(/bun_cmd run/g, ''); |
| 118 | + expect(stripped).not.toMatch(/\bbun run gen:skill-docs/); |
| 119 | + } |
| 120 | + } |
| 121 | + }); |
| 122 | +}); |
0 commit comments