mirror of
https://github.com/garrytan/gstack.git
synced 2026-11-20 23:23:26 +01:00
Rewrite of #2217. Required team mode wrote a bash check-gstack.sh and registered it as "$CLAUDE_PROJECT_DIR/.claude/hooks/check-gstack.sh", which only works when the hook runner is a POSIX shell. Under cmd.exe or PowerShell (Windows, Copilot CLI) the variable is not expanded and the .sh cannot run, so an intended allow or deny became a hook error. - The hook is now .claude/hooks/check-gstack.cjs: the same root search (GSTACK_ROOT, every host's global root, the migrated repo), with the home directory from HOME, then USERPROFILE, then os.homedir(). It prints {} when gstack resolves and the PreToolUse deny JSON with exit 2 when not. - The registered command is `node -e "require(require('path').resolve( process.env.CLAUDE_PROJECT_DIR || '.', '.claude', 'hooks', 'check-gstack.cjs'))"`: no `$` or `%`, so sh, cmd.exe and PowerShell all pass it through unchanged. - An existing check-gstack registration (matcher Skill or Skill|skill) is replaced in place; duplicates are dropped and unrelated hooks in the same entry kept. The legacy .sh is deleted only after the settings write succeeds (a tracked one joins the suggested git add), and is kept when bun is missing or the write fails, which now reports instead of claiming success. Tests: team-mode and the hook-schema test run the command as registered; test/team-init-hook-shell-neutral.test.ts runs it through the platform's default shell (cmd.exe on the Windows lane, which curates it in) and through PowerShell when installed (verified here in the pwsh 7 container). The routing-probe check for the bash `_GSTACK_ROOT` gate now pins the same contract in the .cjs: every host root probed, block only when none resolves. Co-authored-by: neallee2012 <1499497+neallee2012@users.noreply.github.com> Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
87 lines
3.8 KiB
TypeScript
87 lines
3.8 KiB
TypeScript
import { describe, test, expect, beforeEach, afterEach } from 'bun:test';
|
|
import { execSync, execFileSync } from 'child_process';
|
|
import * as fs from 'fs';
|
|
import * as path from 'path';
|
|
import * as os from 'os';
|
|
|
|
// Regression guard for #2413: gstack-team-init required generated a
|
|
// PreToolUse hook that emitted a flat {"permissionDecision":...} payload and
|
|
// exited 0. Claude Code only nests decisions under hookSpecificOutput, and
|
|
// only exit code 2 reliably blocks a PreToolUse call. The old shape was
|
|
// silently ignored as a non-blocking error, so `required` mode enforced
|
|
// nothing — the BLOCKED text printed to stderr but the tool call proceeded.
|
|
|
|
const ROOT = path.resolve(import.meta.dir, '..');
|
|
const TEAM_INIT = path.join(ROOT, 'bin', 'gstack-team-init');
|
|
|
|
let repoDir: string;
|
|
let fakeHomeAbsent: string;
|
|
let fakeHomePresent: string;
|
|
|
|
describe('gstack-team-init required: PreToolUse hook schema (#2413)', () => {
|
|
beforeEach(() => {
|
|
repoDir = fs.mkdtempSync(path.join(os.tmpdir(), 'gstack-team-init-repo-'));
|
|
execFileSync('git', ['init', '-q'], { cwd: repoDir });
|
|
execFileSync(TEAM_INIT, ['required'], { cwd: repoDir, encoding: 'utf-8' });
|
|
|
|
fakeHomeAbsent = fs.mkdtempSync(path.join(os.tmpdir(), 'gstack-team-init-home-absent-'));
|
|
|
|
fakeHomePresent = fs.mkdtempSync(path.join(os.tmpdir(), 'gstack-team-init-home-present-'));
|
|
fs.mkdirSync(path.join(fakeHomePresent, '.claude', 'skills', 'gstack', 'bin'), { recursive: true });
|
|
});
|
|
|
|
afterEach(() => {
|
|
fs.rmSync(repoDir, { recursive: true, force: true });
|
|
fs.rmSync(fakeHomeAbsent, { recursive: true, force: true });
|
|
fs.rmSync(fakeHomePresent, { recursive: true, force: true });
|
|
});
|
|
|
|
// Run the command exactly as registered in settings.json, the way a hook
|
|
// runner does: through a shell, with CLAUDE_PROJECT_DIR set.
|
|
function runHook(home: string): { status: number; stdout: string; stderr: string } {
|
|
const settings = JSON.parse(fs.readFileSync(path.join(repoDir, '.claude', 'settings.json'), 'utf-8'));
|
|
const command = settings.hooks.PreToolUse[0].hooks[0].command;
|
|
try {
|
|
const stdout = execSync(command, {
|
|
env: { ...process.env, HOME: home, CLAUDE_PROJECT_DIR: repoDir, GSTACK_ROOT: '' },
|
|
encoding: 'utf-8',
|
|
timeout: 30_000,
|
|
});
|
|
return { status: 0, stdout, stderr: '' };
|
|
} catch (err) {
|
|
const e = err as { status: number; stdout: string; stderr: string };
|
|
return { status: e.status, stdout: e.stdout, stderr: e.stderr };
|
|
}
|
|
}
|
|
|
|
test('generates the hook and registers it in settings.json', () => {
|
|
expect(fs.existsSync(path.join(repoDir, '.claude', 'hooks', 'check-gstack.cjs'))).toBe(true);
|
|
const settings = JSON.parse(
|
|
fs.readFileSync(path.join(repoDir, '.claude', 'settings.json'), 'utf-8'),
|
|
);
|
|
expect(JSON.stringify(settings)).toContain('check-gstack.cjs');
|
|
expect(settings.hooks.PreToolUse[0].hooks[0].command).not.toContain('$');
|
|
});
|
|
|
|
test('gstack absent: exits 2 (blocking) with schema-valid deny JSON', () => {
|
|
const { status, stdout, stderr } = runHook(fakeHomeAbsent);
|
|
expect(status).toBe(2);
|
|
expect(stderr).toContain('BLOCKED: gstack is not installed globally.');
|
|
|
|
const payload = JSON.parse(stdout.trim());
|
|
expect(payload.hookSpecificOutput.hookEventName).toBe('PreToolUse');
|
|
expect(payload.hookSpecificOutput.permissionDecision).toBe('deny');
|
|
expect(typeof payload.hookSpecificOutput.permissionDecisionReason).toBe('string');
|
|
// The old top-level shape must be gone, not just supplemented.
|
|
expect(payload.permissionDecision).toBeUndefined();
|
|
expect(payload.message).toBeUndefined();
|
|
});
|
|
|
|
test('gstack present: exits 0 with an empty (no-opinion) payload', () => {
|
|
const { status, stdout, stderr } = runHook(fakeHomePresent);
|
|
expect(status).toBe(0);
|
|
expect(stderr).toBe('');
|
|
expect(JSON.parse(stdout.trim())).toEqual({});
|
|
});
|
|
});
|