// Grok Build (formerly Cursor) payloads read in node9's vocabulary. import { describe, it, expect } from 'vitest'; import fs from 'fs'; import os from 'os'; import path from 'path'; import { CURSOR_AGENT, cursorWorkspaceDir, isCursorPayload, normalizeCursorPayload, } from '../utils/cursor-payload'; import { detectAiAgent } from '../cli/commands/check'; import { agentFromRawPayload } from '../hook-error'; import { agentLabelFromFlag } from '../utils/hook-payload'; import { cursorImportRouteYields, isNode9CursorHookCommand, nativeCursorHookWired, noteNativeCursorSession, resolveCursorMcpTool, } from '../cursor-hooks'; import { CURSOR_WORKSPACE, cursorBeforeShell, cursorPostToolShell, cursorPreToolRead, cursorPreToolShell, cursorPreToolWrite, cursorPrompt, cursorSessionStart, cursorStop, } from './fixtures/cursor-payloads'; const NO_ENV = {} as NodeJS.ProcessEnv; describe('isCursorPayload and agent detection', () => { it('a Cursor preToolUse is Cursor, not Claude Code, although it carries tool_use_id', () => { const p = cursorPreToolShell('ls'); expect(isCursorPayload(p)).toBe(true); expect(detectAiAgent(p)).toBe(CURSOR_AGENT); }); it('every captured event is Cursor', () => { for (const p of [ cursorPrompt('hi'), cursorPreToolRead('/x'), cursorPostToolShell('ls', ''), cursorBeforeShell('ls'), cursorStop, cursorSessionStart, ]) { expect(detectAiAgent(p)).toBe(CURSOR_AGENT); } }); it('a Claude Code payload is unchanged', () => { const claude = { session_id: 's', tool_use_id: 't', hook_event_name: 'PreToolUse', tool_name: 'Bash', tool_input: { command: 'ls' }, }; expect(isCursorPayload(claude)).toBe(false); expect(detectAiAgent(claude)).toBe('Claude Code'); }); it('a camelCase event name alone is not taken for Cursor', () => { expect(isCursorPayload({ hook_event_name: 'preToolUse', tool_name: 'bash' })).toBe(false); }); it('the raw fingerprint of a payload that did not parse names Cursor', () => { expect(agentFromRawPayload('{"cursor_version":"3.32.24","tool_use_id":"x","tool_')).toBe( CURSOR_AGENT ); }); it('--agent cursor and --agent grok-build map to Cursor', () => { expect(agentLabelFromFlag('cursor')).toBe(CURSOR_AGENT); expect(agentLabelFromFlag('Grok-Build')).toBe(CURSOR_AGENT); }); }); describe('normalizeCursorPayload', () => { it('preToolUse Shell: Bash, the empty cwd dropped from the input, the workspace as cwd', () => { const n = normalizeCursorPayload(cursorPreToolShell('rm -rf build'), NO_ENV); expect(n.kind).toBe('tool'); expect(n.event).toBe('preToolUse'); expect(n.payload.hook_event_name).toBe('PreToolUse'); expect(n.payload.tool_name).toBe('Bash'); expect(n.payload.tool_input).toEqual({ command: 'rm -rf build', timeout: 30000 }); expect(n.payload.cwd).toBe(CURSOR_WORKSPACE); // The agent's own payload is untouched. expect(n.payload.cursor_version).toBe('3.32.24'); }); it('keeps an absolute shell cwd', () => { const p = cursorPreToolShell('ls'); (p.tool_input as Record).cwd = '/home/dev/other'; const n = normalizeCursorPayload(p, NO_ENV); expect(n.payload.tool_input).toEqual({ command: 'ls', cwd: '/home/dev/other', timeout: 30000 }); }); it('Read and Write keep their names and inputs', () => { const r = normalizeCursorPayload(cursorPreToolRead('/home/dev/grok-test/.env'), NO_ENV); expect(r.payload.tool_name).toBe('Read'); expect(r.payload.tool_input).toEqual({ file_path: '/home/dev/grok-test/.env' }); const w = normalizeCursorPayload(cursorPreToolWrite('/home/dev/a.txt', 'hi\n'), NO_ENV); expect(w.payload.tool_input).toEqual({ file_path: '/home/dev/a.txt', content: 'hi\n' }); }); it('beforeSubmitPrompt is a UserPromptSubmit with the prompt', () => { const n = normalizeCursorPayload(cursorPrompt('hello'), NO_ENV); expect(n.kind).toBe('prompt'); expect(n.payload.hook_event_name).toBe('UserPromptSubmit'); expect(n.payload.prompt).toBe('hello'); }); it('postToolUse: tool_output read into tool_response', () => { const n = normalizeCursorPayload(cursorPostToolShell('ls', 'a\nb\n'), NO_ENV); expect(n.kind).toBe('after'); expect(n.payload.hook_event_name).toBe('PostToolUse'); expect(n.payload.tool_name).toBe('Bash'); expect(n.payload.tool_response).toEqual({ output: 'a\nb\n', exitCode: 0 }); }); it('a tool_output that is not JSON stays text', () => { const p = cursorPostToolShell('ls', ''); p.tool_output = 'not json'; expect(normalizeCursorPayload(p, NO_ENV).payload.tool_response).toBe('not json'); }); it('an event node9 does not know: a tool name is a call, a prompt a prompt (F2)', () => { const tool = { ...cursorPreToolShell('ls'), hook_event_name: 'beforeToolRun' }; expect(normalizeCursorPayload(tool, NO_ENV).kind).toBe('tool'); expect(normalizeCursorPayload(tool, NO_ENV).payload.tool_name).toBe('Bash'); const claudeSpelling = { ...cursorPreToolShell('ls'), hook_event_name: 'PreToolUse' }; expect(normalizeCursorPayload(claudeSpelling, NO_ENV).kind).toBe('tool'); const missing = { ...cursorPreToolShell('ls') } as Record; delete missing.hook_event_name; expect(normalizeCursorPayload(missing, NO_ENV).kind).toBe('tool'); const prompt = { ...cursorPrompt('hi'), hook_event_name: 'beforePromptSend' }; expect(normalizeCursorPayload(prompt, NO_ENV).kind).toBe('prompt'); const bare = { ...cursorStop, hook_event_name: 'somethingNew' }; expect(normalizeCursorPayload(bare, NO_ENV).kind).toBe('pass'); }); it('other events pass, unchanged', () => { for (const p of [cursorBeforeShell('ls'), cursorStop, cursorSessionStart]) { const n = normalizeCursorPayload(p, NO_ENV); expect(n.kind).toBe('pass'); expect(n.payload.hook_event_name).toBe(p.hook_event_name); } }); }); describe('cursorWorkspaceDir', () => { it('workspace_roots[0] first, then CURSOR_PROJECT_DIR, absolute only', () => { expect(cursorWorkspaceDir({ workspace_roots: ['/a'] }, { CURSOR_PROJECT_DIR: '/b' })).toBe( '/a' ); expect(cursorWorkspaceDir({ workspace_roots: [] }, { CURSOR_PROJECT_DIR: '/b' })).toBe('/b'); expect( cursorWorkspaceDir({ workspace_roots: ['rel'] }, { CURSOR_PROJECT_DIR: '../x' }) ).toBeUndefined(); }); it('no usable workspace: the empty cwd is dropped, never read as relative', () => { const p = cursorPreToolShell('ls'); p.workspace_roots = []; const n = normalizeCursorPayload(p, NO_ENV); expect('cwd' in n.payload).toBe(false); }); }); describe('native Cursor hook entries', () => { it('recognises node9 entries with --agent cursor only', () => { expect(isNode9CursorHookCommand('node9 check --agent cursor')).toBe(true); expect(isNode9CursorHookCommand('"/h/.node9/bin/hook" check --agent cursor')).toBe(true); expect(isNode9CursorHookCommand('node9 check')).toBe(false); expect(isNode9CursorHookCommand('node /x/capture.js preToolUse')).toBe(false); }); it('a wired event in the user file counts; a missing, stale or foreign entry does not', () => { const home = fs.mkdtempSync(path.join(os.tmpdir(), 'n9-cursor-')); try { fs.mkdirSync(path.join(home, '.cursor')); const file = path.join(home, '.cursor', 'hooks.json'); fs.writeFileSync( file, JSON.stringify({ version: 1, hooks: { preToolUse: [{ command: 'node9 check --agent cursor' }], beforeSubmitPrompt: [{ command: '"/gone/node" "/gone/cli.js" check --agent cursor' }], postToolUse: [{ command: 'node /x/capture.js postToolUse' }], }, }) ); // 'linux' so the machine file is /etc/cursor/hooks.json, absent in CI. expect(nativeCursorHookWired('preToolUse', home, 'linux')).toBe(true); expect(nativeCursorHookWired('beforeSubmitPrompt', home, 'linux')).toBe(false); expect(nativeCursorHookWired('postToolUse', home, 'linux')).toBe(false); fs.writeFileSync(file, '{ not json'); expect(nativeCursorHookWired('preToolUse', home, 'linux')).toBe(false); // A node9 entry narrowed by a matcher does not see every call. fs.writeFileSync( file, JSON.stringify({ hooks: { preToolUse: [{ command: 'node9 check --agent cursor', matcher: 'Shell' }] }, }) ); expect(nativeCursorHookWired('preToolUse', home, 'linux')).toBe(false); fs.writeFileSync( file, JSON.stringify({ hooks: { preToolUse: [{ command: 'node9 check --agent cursor', matcher: '.*' }] }, }) ); expect(nativeCursorHookWired('preToolUse', home, 'linux')).toBe(true); } finally { fs.rmSync(home, { recursive: true, force: true }); } }); }); describe('cursorImportRouteYields', () => { it('only once the native hook has run in this session, not on the file alone', () => { const home = fs.mkdtempSync(path.join(os.tmpdir(), 'n9-cursor-yield-')); try { fs.mkdirSync(path.join(home, '.cursor')); fs.writeFileSync( path.join(home, '.cursor', 'hooks.json'), JSON.stringify({ hooks: { preToolUse: [{ command: 'node9 check --agent cursor' }] } }) ); // The entry is there, but the app has not run it yet (no restart since). expect(cursorImportRouteYields('preToolUse', 's1', home, 'linux')).toBe(false); // A note for another event (the prompt hook) does not count. noteNativeCursorSession('s1', 'beforeSubmitPrompt', home); expect(cursorImportRouteYields('preToolUse', 's1', home, 'linux')).toBe(false); noteNativeCursorSession('s1', 'preToolUse', home); expect(cursorImportRouteYields('preToolUse', 's1', home, 'linux')).toBe(true); // Another session, or an event with no native entry: the import route checks. expect(cursorImportRouteYields('preToolUse', 's2', home, 'linux')).toBe(false); expect(cursorImportRouteYields('postToolUse', 's1', home, 'linux')).toBe(false); expect(cursorImportRouteYields('preToolUse', undefined, home, 'linux')).toBe(false); } finally { fs.rmSync(home, { recursive: true, force: true }); } }); }); describe('Delete and MCP calls (R1, R2: shapes from the live capture)', () => { it('MCP: is named as an MCP tool: the resolved server, else unknown', () => { const p = { ...cursorPreToolRead('/x'), tool_name: 'MCP:node9_status', tool_input: {} }; expect(normalizeCursorPayload(p, NO_ENV, () => 'node9').payload.tool_name).toBe( 'mcp__node9__node9_status' ); expect(normalizeCursorPayload(p, NO_ENV).payload.tool_name).toBe('mcp__unknown__node9_status'); // A server name that breaks the mcp____ form is not used. expect(normalizeCursorPayload(p, NO_ENV, () => 'a__b').payload.tool_name).toBe( 'mcp__unknown__node9_status' ); }); it('resolves the server from the app MCP files and node9 tool lists', () => { const home = fs.mkdtempSync(path.join(os.tmpdir(), 'n9-cursor-mcp-')); try { fs.mkdirSync(path.join(home, '.cursor')); fs.mkdirSync(path.join(home, '.node9')); fs.writeFileSync( path.join(home, '.cursor', 'mcp.json'), JSON.stringify({ mcpServers: { node9: { command: 'node9', args: ['mcp-server'] }, gmail: { command: 'node9', args: ['mcp-gateway', '--config-name', 'gmail', '--upstream', 'npx gmail-mcp'], }, github: { command: 'npx', args: ['github-mcp'] }, other: { command: 'npx', args: ['other-mcp'] }, }, }) ); fs.writeFileSync( path.join(home, '.node9', 'mcp-tools.json'), JSON.stringify({ a: { name: 'gmail', tools: [{ name: 'send_email' }], disabled: [], status: 'ok' }, b: { name: 'github', tools: [{ name: 'list_issues' }, { name: 'search' }] }, c: { name: 'other', tools: [{ name: 'search' }] }, }) ); expect(resolveCursorMcpTool('node9_status', undefined, home)).toMatchObject({ name: 'node9', wrapped: false, }); expect(resolveCursorMcpTool('send_email', undefined, home)).toMatchObject({ name: 'gmail', wrapped: true, }); expect(resolveCursorMcpTool('list_issues', undefined, home)?.name).toBe('github'); // Two servers have it, or none: no guess. expect(resolveCursorMcpTool('search', undefined, home)).toBeUndefined(); expect(resolveCursorMcpTool('nope', undefined, home)).toBeUndefined(); } finally { fs.rmSync(home, { recursive: true, force: true }); } }); });