Skip to content

Use Garry Tan's exact Claude Code setup: 23 opinionated tools that serve as CEO, Designer, Eng Manager, Release Manager, Doc Engineer, and QA

TypeScript
7.0 KB
Raw Blame
import { describe, expect, test } from 'bun:test';
import { mkdtempSync, mkdirSync, readFileSync, rmSync, symlinkSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { spawnSync } from 'node:child_process';
import { generateAsideSetup, generateAsideResearch } from '../scripts/resolvers/aside';
import { HOST_PATHS } from '../scripts/resolvers/types';

const ctx = { skillName: 'browse', tmplPath: '', host: 'claude' as const, paths: HOST_PATHS.claude };
const probe = generateAsideSetup(ctx).match(/```bash\n([\s\S]*?)```/)![1];
const shells = ['bash', 'sh', ...(process.platform === 'win32' ? [] : ['zsh'])];
const launchers = {
  gtimeout: Bun.which('gtimeout') ?? Bun.which('timeout'),
  timeout: Bun.which('timeout') ?? Bun.which('gtimeout'),
  perl: Bun.which('perl'),
};

function run(shell: string, arm: keyof typeof launchers | 'none', mode = 'ready', skip = false, platform = 'Darwin', extraEnv: Record<string, string> = {}) {
  const root = mkdtempSync(join(tmpdir(), 'aside-probe-'));
  const bin = join(root, 'bin');
  mkdirSync(bin);
  try {
    const executable = Bun.which(shell);
    if (!executable) throw new Error(`Required shell unavailable: ${shell}`);
    symlinkSync(Bun.which('grep')!, join(bin, 'grep'));
    symlinkSync(Bun.which('uname')!, join(bin, 'uname'));
    if (arm !== 'none') symlinkSync(launchers[arm]!, join(bin, arm));
    if (mode !== 'absent') writeFileSync(join(bin, 'aside'), `#!/bin/sh
printf '%s\\n' "$@" >> "$CALLS"
case "$MODE" in
  ready) echo 'ASIDE_READY /private/session' ;;
  version-error) case "\${1}" in --version) echo 'VERSION_DIAGNOSTIC_MARKER'; exit 7;; *) echo 'ASIDE_READY /private/session';; esac ;;
  stopped) echo '[error] Aside app is not running' ;;
  stopped-exit1) echo 'Failed to request daemon auth challenge: fetch failed' >&2; echo "Aside isn't running on this machine." >&2; exit 1 ;;
  timeout) exit 124 ;;
  hang) exec /bin/sleep 5 ;;
  marker-error) echo 'ASIDE_READY /private/session'; exit 7 ;;
  prefix) echo 'ASIDE_READY_INVALID' ;;
  error) echo 'Cannot find module /private/person/token=SYNTHETIC_PRIVATE_VALUE' >&2; exit 7 ;;
esac
`, { mode: 0o755 });
    const script = mode === 'hang' ? probe.replaceAll('30 "$@"', '1 "$@"') : probe;
    const result = spawnSync(executable, ['-c', script], {
      encoding: 'utf8', timeout: 5000,
      env: { HOME: root, PATH: bin, MODE: mode, CALLS: join(root, 'calls'), GSTACK_SKIP_ASIDE: skip ? '1' : '', ...(platform ? { GSTACK_PLATFORM: platform } : {}), ...extraEnv },
    });
    let calls = '';
    try { calls = readFileSync(join(root, 'calls'), 'utf8'); } catch {}
    expect(result.status).toBe(0);
    return { output: result.stdout + result.stderr, calls };
  } finally { rmSync(root, { recursive: true, force: true }); }
}

describe('emitted Aside readiness probe', () => {
  test('research shares the complete setup probe', () => {
    expect(generateAsideResearch(ctx)).toContain(probe);
  });
  for (const shell of shells) {
    const shellTest = Bun.which(shell) ? test : test.skip;
    shellTest(`${shell}: without a deadline launcher even a hanging CLI is never invoked`, () => {
      for (const mode of ['ready', 'hang']) {
        const result = run(shell, 'none', mode);
        expect(result.output).toBe('ASIDE_UNAVAILABLE: bounded probe unavailable\n');
        expect(result.calls).toBe('');
      }
      for (const result of [run(shell, 'none', 'absent'), run(shell, 'none', 'ready', true)]) {
        expect(result.output).toBe('NEEDS_ASIDE: Darwin\n');
        expect(result.calls).toBe('');
      }
    });
    shellTest(`${shell}: NEEDS_ASIDE names GSTACK_PLATFORM, else the kernel uname reports`, () => {
      expect(run(shell, 'none', 'absent', false, 'Linux').output).toBe('NEEDS_ASIDE: Linux\n');
      const kernel = spawnSync('uname', [], { encoding: 'utf8' }).stdout.trim();
      expect(run(shell, 'none', 'absent', false, '').output).toBe(`NEEDS_ASIDE: ${kernel}\n`);
    });
    for (const arm of ['gtimeout', 'timeout', 'perl'] as const) {
      const armTest = Bun.which(shell) && launchers[arm] ? test : test.skip;
      armTest(`${shell}/${arm}: forwards the complete script as one argument`, () => {
        const result = run(shell, arm);
        expect(result.output).toBe('READY: aside\n');
        expect(result.calls).toBe('repl\nconsole.log("ASIDE_READY " + pwd)\n');
      });
      armTest(`${shell}/${arm}: READY never calls the optional version diagnostic channel`, () => {
        const result = run(shell, arm, 'version-error');
        expect(result.output).toBe('READY: aside\n');
        expect(result.output).not.toContain('VERSION_DIAGNOSTIC_MARKER');
        expect(result.calls).not.toContain('--version');
      });
      armTest(`${shell}/${arm}: absent and skipped never invoke Aside`, () => {
        for (const result of [run(shell, arm, 'absent'), run(shell, arm, 'ready', true)]) {
          expect(result.output).toBe('NEEDS_ASIDE: Darwin\n');
          expect(result.calls).toBe('');
        }
      });
      armTest(`${shell}/${arm}: CLI failures are distinct and do not leak raw output`, () => {
        const result = run(shell, arm, 'error');
        expect(result.output).toBe('ASIDE_CLI_ERROR: exit 7; inspect aside --help locally\n');
        expect(result.output).not.toContain('SYNTHETIC_PRIVATE_VALUE');
        expect(result.output).not.toContain('/private/person');
      });
      armTest(`${shell}/${arm}: deadline and stopped-app responses have distinct outcomes`, () => {
        expect(run(shell, arm, 'timeout').output).toBe('ASIDE_TIMEOUT: probe deadline exceeded\n');
        expect(run(shell, arm, 'stopped').output).toBe('ASIDE_NOT_RUNNING: no readiness marker\n');
        expect(run(shell, arm, 'stopped-exit1').output).toBe('ASIDE_NOT_RUNNING: app not running\n');
      });
      armTest(`${shell}/${arm}: readiness requires a successful exit and an exact marker`, () => {
        expect(run(shell, arm, 'marker-error').output).toBe('ASIDE_CLI_ERROR: exit 7; inspect aside --help locally\n');
        expect(run(shell, arm, 'prefix').output).toBe('ASIDE_NOT_RUNNING: no readiness marker\n');
      });
      armTest(`${shell}/${arm}: the native deadline stops a hung CLI`, () => {
        expect(run(shell, arm, 'hang').output).toBe('ASIDE_TIMEOUT: probe deadline exceeded\n');
      });
    }
  }
});

describe('the Aside probe under BROWSE_PUBLIC_ONLY (#4812)', () => {
  for (const shell of shells) {
    const shellTest = Bun.which(shell) && launchers.timeout ? test : test.skip;
    shellTest(`${shell}: on, or any value browse rejects, skips Aside without calling it; off and unset keep the probe`, () => {
      for (const value of ['1', 'true', 'on', 'TRUE', 'yes', ' 1']) {
        const r = run(shell, 'timeout', 'ready', false, 'Darwin', { BROWSE_PUBLIC_ONLY: value });
        expect(r.output).toBe('ASIDE_SKIPPED: BROWSE_PUBLIC_ONLY\n');
        expect(r.calls).toBe('');
      }
      for (const value of ['', '0', 'false', 'off']) {
        expect(run(shell, 'timeout', 'ready', false, 'Darwin', { BROWSE_PUBLIC_ONLY: value }).output).toStartWith('READY: ');
      }
    });
  }
});