// Grok Build (formerly Cursor): node9's native entries in ~/.cursor/hooks.json. // Real files in a temp home: the merge must keep the user's own hooks. import { describe, it, expect, beforeEach, afterEach } from 'vitest'; import fs from 'fs'; import os from 'os'; import path from 'path'; import { unwireCursorHooks, wireCursorHooks } from '../setup'; import { nativeCursorHookWired } from '../cursor-hooks'; import { getAgentWiring } from '../agent-wiring'; let home: string; let file: string; const read = () => JSON.parse(fs.readFileSync(file, 'utf-8')); beforeEach(() => { process.env.NODE9_TESTING = '1'; // fullPathCommand -> `node9 ` home = fs.mkdtempSync(path.join(os.tmpdir(), 'n9-cursor-setup-')); file = path.join(home, '.cursor', 'hooks.json'); }); afterEach(() => fs.rmSync(home, { recursive: true, force: true })); describe('wireCursorHooks', () => { it('writes the five node9 entries with a 600 s timeout', () => { expect(wireCursorHooks(home)).toBe(true); const f = read(); expect(f.version).toBe(1); expect(f.hooks.preToolUse).toEqual([{ command: 'node9 check --agent cursor', timeout: 600 }]); expect(f.hooks.beforeSubmitPrompt).toEqual([ { command: 'node9 check --agent cursor', timeout: 600 }, ]); expect(f.hooks.postToolUse).toEqual([{ command: 'node9 log --agent cursor', timeout: 600 }]); expect(f.hooks.stop).toEqual([{ command: 'node9 log --agent cursor', timeout: 600 }]); expect(f.hooks.beforeReadFile).toEqual([ { command: 'node9 check --agent cursor', timeout: 600 }, ]); // The hook reads them back as wired: the import route then steps aside. expect(nativeCursorHookWired('preToolUse', home, 'linux')).toBe(true); // A second run changes nothing. expect(wireCursorHooks(home)).toBe(false); }); it("keeps the user's own hooks and other events untouched", () => { fs.mkdirSync(path.dirname(file), { recursive: true }); const mine = { command: 'node /home/me/audit.js', matcher: 'Shell' }; fs.writeFileSync( file, JSON.stringify({ version: 1, hooks: { preToolUse: [mine], stop: [{ command: 'x' }], sessionEnd: [{ command: 'y' }] }, }) ); wireCursorHooks(home); const f = read(); expect(f.hooks.preToolUse[0]).toEqual(mine); expect(f.hooks.preToolUse[1].command).toBe('node9 check --agent cursor'); // The user's stop hook stays first; node9's is added after it. expect(f.hooks.stop[0]).toEqual({ command: 'x' }); expect(f.hooks.stop[1].command).toBe('node9 log --agent cursor'); // An event node9 does not wire is not touched. expect(f.hooks.sessionEnd).toEqual([{ command: 'y' }]); }); it('repairs a stale node9 entry and drops a duplicate', () => { fs.mkdirSync(path.dirname(file), { recursive: true }); fs.writeFileSync( file, JSON.stringify({ version: 1, hooks: { preToolUse: [ { command: '"/gone/node" "/gone/cli.js" check --agent cursor' }, { command: 'node9 check --agent cursor' }, ], }, }) ); expect(wireCursorHooks(home)).toBe(true); expect(read().hooks.preToolUse).toEqual([ { command: 'node9 check --agent cursor', timeout: 600 }, ]); }); it('a node9 entry with the wrong subcommand is rewritten', () => { fs.mkdirSync(path.dirname(file), { recursive: true }); fs.writeFileSync( file, JSON.stringify({ version: 1, hooks: { preToolUse: [{ command: 'node9 log --agent cursor' }] }, }) ); wireCursorHooks(home); expect(read().hooks.preToolUse[0].command).toBe('node9 check --agent cursor'); }); it('failClosed follows the machine onHookError policy', () => { fs.mkdirSync(path.join(home, '.node9'), { recursive: true }); fs.writeFileSync( path.join(home, '.node9', 'config.json'), JSON.stringify({ settings: { onHookError: 'block' } }) ); wireCursorHooks(home); expect(read().hooks.preToolUse[0].failClosed).toBe(true); }); it("leaves an event alone when it holds the user's own non-list value", () => { fs.mkdirSync(path.dirname(file), { recursive: true }); const odd = JSON.stringify({ version: 1, hooks: { preToolUse: { command: 'mine' } } }); fs.writeFileSync(file, odd); expect(wireCursorHooks(home)).toBe('unreadable'); expect(fs.readFileSync(file, 'utf-8')).toBe(odd); }); it('leaves a file that does not parse alone', () => { fs.mkdirSync(path.dirname(file), { recursive: true }); fs.writeFileSync(file, '{ "hooks": '); expect(wireCursorHooks(home)).toBe('unreadable'); expect(fs.readFileSync(file, 'utf-8')).toBe('{ "hooks": '); }); }); describe('unwireCursorHooks', () => { it("removes only node9's entries, and the file when node9 wrote all of it", () => { wireCursorHooks(home); expect(unwireCursorHooks(home)).toBe(true); expect(fs.existsSync(file)).toBe(false); }); it('leaves a hooks file that does not parse alone on teardown', () => { fs.mkdirSync(path.dirname(file), { recursive: true }); fs.writeFileSync(file, '{ "hooks": '); expect(unwireCursorHooks(home)).toBe(false); expect(fs.readFileSync(file, 'utf-8')).toBe('{ "hooks": '); }); it("keeps a file that holds the user's own hooks", () => { fs.mkdirSync(path.dirname(file), { recursive: true }); fs.writeFileSync( file, JSON.stringify({ version: 1, hooks: { preToolUse: [{ command: 'mine' }] } }) ); wireCursorHooks(home); expect(unwireCursorHooks(home)).toBe(true); expect(read().hooks).toEqual({ preToolUse: [{ command: 'mine' }] }); }); }); describe('status and doctor see the Cursor hooks', () => { it('reads Cursor as wired once the entries are in', () => { wireCursorHooks(home); const cursor = getAgentWiring(home).find((a) => a.id === 'cursor')!; expect(cursor.wireState).toBe('wired'); expect(cursor.hooks.every((h) => h.wired)).toBe(true); }); });