diff --git a/integration-tests/hook-integration/hooks.test.ts b/integration-tests/hook-integration/hooks.test.ts index c0166a390..2c6d682cd 100644 --- a/integration-tests/hook-integration/hooks.test.ts +++ b/integration-tests/hook-integration/hooks.test.ts @@ -1,19 +1,18 @@ -/** - * @license - * Copyright 2026 Qwen Team - * SPDX-License-Identifier: Apache-2.0 - */ - import { describe, it, expect, beforeEach, afterEach } from 'vitest'; +import { join } from 'node:path'; import { TestRig, validateModelOutput } from '../test-helper.js'; /** - * Hooks Integration Tests - * Tests for UserPromptSubmit and Stop event hooks - * Reference: qwen_integration.md + * Path to responses directory for mock LLM responses */ +const RESPONSES_DIR = join(import.meta.dirname, 'responses'); -describe('Hooks Integration - UserPromptSubmit', () => { +/** + * Hooks System Integration Tests + * Tests for complete hook system flow including UserPromptSubmit, Stop hooks + * Uses responses files for deterministic testing + */ +describe('Hooks System Integration', () => { let rig: TestRig; beforeEach(() => { @@ -26,418 +25,1044 @@ describe('Hooks Integration - UserPromptSubmit', () => { } }); - // ==================== UPS-001: Allow Decision ==================== - describe('UPS-001: Hook returns allow decision', () => { - it('should allow prompt when hook returns allow decision', async () => { - await rig.setup('ups-001-allow-decision', { - settings: { - hooks: { - UserPromptSubmit: [ - { - hooks: [ - { - type: 'command', - command: - 'echo \'{"decision":"allow","reason":"approved by hook"}\'', - name: 'ups-allow-hook', - }, - ], - }, - ], + // ==================== UserPromptSubmit Hooks ==================== + describe('UserPromptSubmit Hooks', () => { + describe('Allow Decision', () => { + it('should allow prompt when hook returns allow decision', async () => { + const hookScript = + "console.log(JSON.stringify({decision: 'allow', reason: 'approved by hook'}));"; + + await rig.setup('ups-allow-decision', { + fakeResponsesPath: join( + RESPONSES_DIR, + 'qwen-userpromptsubmit-allow.responses', + ), + settings: { + hooks: { + enabled: true, + UserPromptSubmit: [ + { + hooks: [ + { + type: 'command', + command: `node -e "${hookScript}"`, + name: 'ups-allow-hook', + timeout: 5000, + }, + ], + }, + ], + }, + trusted: true, }, - trusted: true, - }, + }); + + const result = await rig.run('Say hello'); + expect(result).toBeDefined(); + expect(result.length).toBeGreaterThan(0); }); - const result = await rig.run('Say hello'); - expect(result).toBeDefined(); - expect(result.length).toBeGreaterThan(0); + it('should allow tool execution with allow decision and verify tool was called', async () => { + const hookScript = + "console.log(JSON.stringify({decision: 'allow', reason: 'Tool execution approved'}));"; + + await rig.setup('ups-allow-tool', { + fakeResponsesPath: join( + RESPONSES_DIR, + 'qwen-userpromptsubmit-allow.responses', + ), + settings: { + hooks: { + enabled: true, + UserPromptSubmit: [ + { + hooks: [ + { + type: 'command', + command: `node -e "${hookScript}"`, + name: 'ups-allow-tool-hook', + timeout: 5000, + }, + ], + }, + ], + }, + trusted: true, + }, + }); + + await rig.run('Create a file test.txt with content "hello"'); + + const foundToolCall = await rig.waitForToolCall('write_file'); + expect(foundToolCall).toBeTruthy(); + + const fileContent = rig.readFile('test.txt'); + expect(fileContent).toContain('hello'); + }); }); - it('should allow tool execution and verify tool was called with allow decision', async () => { - await rig.setup('ups-001-allow-tool', { - settings: { - hooks: { - UserPromptSubmit: [ - { - hooks: [ - { - type: 'command', - command: 'echo \'{"decision":"allow"}\'', - name: 'ups-allow-tool-hook', - }, - ], - }, - ], + describe('Block Decision', () => { + it('should block prompt when hook returns block decision', async () => { + const blockScript = `console.log(JSON.stringify({decision: 'block', reason: 'Prompt blocked by security policy'}));`; + + await rig.setup('ups-block-decision', { + fakeResponsesPath: join( + RESPONSES_DIR, + 'qwen-userpromptsubmit-block.responses', + ), + settings: { + hooks: { + enabled: true, + UserPromptSubmit: [ + { + hooks: [ + { + type: 'command', + command: `node -e "${blockScript}"`, + name: 'ups-block-hook', + timeout: 5000, + }, + ], + }, + ], + }, + trusted: true, }, - trusted: true, - }, + }); + + const result = await rig.run('Create a file'); + + // Blocked prompts should show the block reason + expect(result.toLowerCase()).toContain('block'); }); - await rig.run('Create a file test.txt with content "hello"'); + it('should block tool execution when hook returns block and verify no tool was called', async () => { + const blockScript = `console.log(JSON.stringify({decision: 'block', reason: 'File writing blocked by security policy'}));`; - const foundToolCall = await rig.waitForToolCall('write_file'); - expect(foundToolCall).toBeTruthy(); - - const fileContent = rig.readFile('test.txt'); - expect(fileContent).toContain('hello'); - }); - }); - - // ==================== UPS-002: Block Decision ==================== - describe('UPS-002: Hook returns block decision', () => { - it('should block prompt when hook returns block decision', async () => { - await rig.setup('ups-002-block-decision', { - settings: { - hooks: { - UserPromptSubmit: [ - { - hooks: [ - { - type: 'command', - command: - 'echo \'{"decision":"block","reason":"Prompt blocked by security policy"}\'', - name: 'ups-block-hook', - }, - ], - }, - ], + await rig.setup('ups-block-tool', { + fakeResponsesPath: join( + RESPONSES_DIR, + 'qwen-userpromptsubmit-block.responses', + ), + settings: { + hooks: { + enabled: true, + UserPromptSubmit: [ + { + hooks: [ + { + type: 'command', + command: `node -e "${blockScript}"`, + name: 'ups-block-tool-hook', + timeout: 5000, + }, + ], + }, + ], + }, + trusted: true, }, - trusted: true, - }, + }); + + const result = await rig.run('Create a file test.txt with "hello"'); + + // Tool should not be called due to blocking hook + const toolLogs = rig.readToolLogs(); + const writeFileCalls = toolLogs.filter( + (t) => + t.toolRequest.name === 'write_file' && + t.toolRequest.success === true, + ); + expect(writeFileCalls).toHaveLength(0); + + // Result should mention the blocking reason + expect(result).toContain('block'); + }); + }); + + describe('Modify Prompt', () => { + it('should use modified prompt when hook provides modification', async () => { + const modifyScript = `console.log(JSON.stringify({decision: 'allow', hookSpecificOutput: {hookEventName: 'UserPromptSubmit', modifiedPrompt: 'Modified prompt content', additionalContext: 'Context added by hook'}}));`; + + await rig.setup('ups-modify-prompt', { + fakeResponsesPath: join( + RESPONSES_DIR, + 'qwen-userpromptsubmit-modify.responses', + ), + settings: { + hooks: { + enabled: true, + UserPromptSubmit: [ + { + hooks: [ + { + type: 'command', + command: `node -e "${modifyScript}"`, + name: 'ups-modify-hook', + timeout: 5000, + }, + ], + }, + ], + }, + trusted: true, + }, + }); + + const result = await rig.run('Say test'); + expect(result).toBeDefined(); + }); + }); + + describe('Additional Context', () => { + it('should include additional context in response when hook provides it', async () => { + const contextScript = `console.log(JSON.stringify({decision: 'allow', hookSpecificOutput: {additionalContext: 'Extra context information from hook'}}));`; + + await rig.setup('ups-add-context', { + fakeResponsesPath: join( + RESPONSES_DIR, + 'qwen-userpromptsubmit-add-context.responses', + ), + settings: { + hooks: { + enabled: true, + UserPromptSubmit: [ + { + hooks: [ + { + type: 'command', + command: `node -e "${contextScript}"`, + name: 'ups-context-hook', + timeout: 5000, + }, + ], + }, + ], + }, + trusted: true, + }, + }); + + const result = await rig.run('What is 1+1?'); + expect(result).toBeDefined(); + }); + }); + + describe('Timeout Handling', () => { + it('should continue execution when hook times out', async () => { + await rig.setup('ups-timeout', { + fakeResponsesPath: join( + RESPONSES_DIR, + 'qwen-userpromptsubmit-timeout.responses', + ), + settings: { + hooks: { + enabled: true, + UserPromptSubmit: [ + { + hooks: [ + { + type: 'command', + command: 'sleep 60', + name: 'ups-timeout-hook', + timeout: 1000, + }, + ], + }, + ], + }, + trusted: true, + }, + }); + + const result = await rig.run('Say timeout test'); + // Should continue despite timeout + expect(result).toBeDefined(); + }); + }); + + describe('Error Handling', () => { + it('should continue execution when hook exits with non-blocking error (exit code 1)', async () => { + await rig.setup('ups-nonblocking-error', { + fakeResponsesPath: join( + RESPONSES_DIR, + 'qwen-userpromptsubmit-error-nonblocking.responses', + ), + settings: { + hooks: { + enabled: true, + UserPromptSubmit: [ + { + hooks: [ + { + type: 'command', + command: 'echo warning && exit 1', + name: 'ups-error-hook', + timeout: 5000, + }, + ], + }, + ], + }, + trusted: true, + }, + }); + + const result = await rig.run('Say error test'); + // Non-blocking error should not prevent execution + expect(result).toBeDefined(); }); - const result = await rig.run('Create a file'); - - // Blocked prompts should show the block reason - expect(result.toLowerCase()).toContain('block'); - }); - - it('should block tool execution when hook returns block', async () => { - await rig.setup('ups-002-block-tool', { - settings: { - hooks: { - UserPromptSubmit: [ - { - hooks: [ - { - type: 'command', - command: - 'echo \'{"decision":"block","reason":"File writing blocked"}\'', - name: 'ups-block-tool-hook', - }, - ], - }, - ], + it('should block execution when hook exits with blocking error (exit code 2)', async () => { + await rig.setup('ups-blocking-error', { + fakeResponsesPath: join( + RESPONSES_DIR, + 'qwen-userpromptsubmit-error-blocking.responses', + ), + settings: { + hooks: { + enabled: true, + UserPromptSubmit: [ + { + hooks: [ + { + type: 'command', + command: + 'node -e "console.error(\'Critical security error\'); process.exit(2)"', + name: 'ups-blocking-error-hook', + timeout: 5000, + }, + ], + }, + ], + }, + trusted: true, }, - trusted: true, - }, + }); + + const result = await rig.run('Create a file'); + expect(result).toBeDefined(); }); - await rig.run('Create a file test.txt with "hello"'); - - // Tool should not be called due to blocking hook - const toolLogs = rig.readToolLogs(); - const writeFileCalls = toolLogs.filter( - (t) => - t.toolRequest.name === 'write_file' && t.toolRequest.success === true, - ); - expect(writeFileCalls).toHaveLength(0); - }); - }); - - // ==================== UPS-003: Modify Prompt ==================== - describe('UPS-003: Hook modifies prompt content', () => { - it('should use modified prompt when hook provides modification', async () => { - await rig.setup('ups-003-modify-prompt', { - settings: { - hooks: { - UserPromptSubmit: [ - { - hooks: [ - { - type: 'command', - command: - 'echo \'{"decision":"allow","hookSpecificOutput":{"hookEventName":"UserPromptSubmit","additionalContext":"modified"}}\'', - name: 'ups-modify-hook', - }, - ], - }, - ], + it('should continue execution when hook command does not exist', async () => { + await rig.setup('ups-missing-command', { + fakeResponsesPath: join( + RESPONSES_DIR, + 'qwen-userpromptsubmit-missing-command.responses', + ), + settings: { + hooks: { + enabled: true, + UserPromptSubmit: [ + { + hooks: [ + { + type: 'command', + command: '/nonexistent/command/path', + name: 'ups-missing-hook', + timeout: 5000, + }, + ], + }, + ], + }, + trusted: true, }, - trusted: true, - }, + }); + + const result = await rig.run('Say missing test'); + // Missing command should not prevent execution (non-blocking) + expect(result).toBeDefined(); }); - - const result = await rig.run('Say test'); - expect(result).toBeDefined(); - }); - }); - - // ==================== UPS-004: Additional Context ==================== - describe('UPS-004: Hook adds additionalContext', () => { - it('should include additional context in response when hook provides it', async () => { - await rig.setup('ups-004-add-context', { - settings: { - hooks: { - UserPromptSubmit: [ - { - hooks: [ - { - type: 'command', - command: - 'echo \'{"decision":"allow","hookSpecificOutput":{"additionalContext":"extra info from hook"}}\'', - name: 'ups-context-hook', - }, - ], - }, - ], - }, - trusted: true, - }, - }); - - const result = await rig.run('What is 1+1?'); - expect(result).toBeDefined(); }); - it('should generate hook telemetry with additional context', async () => { - await rig.setup('ups-004-telemetry', { - settings: { - hooks: { - UserPromptSubmit: [ - { - hooks: [ - { - type: 'command', - command: - 'echo \'{"decision":"allow","hookSpecificOutput":{"additionalContext":"telemetry test"}}\'', - name: 'ups-telemetry-hook', - }, - ], - }, - ], - }, - trusted: true, - }, - }); - - await rig.run('Say telemetry'); - - const hookTelemetryFound = await rig.waitForTelemetryEvent('hook_call'); - expect(hookTelemetryFound).toBeTruthy(); - }); - }); - - // ==================== UPS-005: Timeout ==================== - describe('UPS-005: Hook execution timeout', () => { - it('should continue execution when hook times out', async () => { - await rig.setup('ups-005-timeout', { - settings: { - hooks: { - UserPromptSubmit: [ - { - hooks: [ - { - type: 'command', - command: 'sleep 60', - name: 'ups-timeout-hook', - timeout: 1000, - }, - ], - }, - ], - }, - trusted: true, - }, - }); - - const result = await rig.run('Say timeout test'); - // Should continue despite timeout - expect(result).toBeDefined(); - }); - }); - - // ==================== UPS-006: Non-blocking Error ==================== - describe('UPS-006: Hook returns non-blocking error (exit code 1)', () => { - it('should continue execution when hook exits with code 1', async () => { - await rig.setup('ups-006-nonblocking-error', { - settings: { - hooks: { - UserPromptSubmit: [ - { - hooks: [ - { - type: 'command', - command: 'echo warning && exit 1', - name: 'ups-error-hook', - }, - ], - }, - ], - }, - trusted: true, - }, - }); - - const result = await rig.run('Say error test'); - // Non-blocking error should not prevent execution - expect(result).toBeDefined(); - }); - - it('should handle stdout + stderr with exit code 0 as system message', async () => { - await rig.setup('ups-006-mixed-output', { - settings: { - hooks: { - UserPromptSubmit: [ - { - hooks: [ - { - type: 'command', - command: - 'echo "stdout message" && echo "stderr message" >&2 && exit 0', - name: 'ups-mixed-output-hook', - }, - ], - }, - ], - }, - trusted: true, - }, - }); - - const result = await rig.run('Say mixed output'); - expect(result).toBeDefined(); - }); - }); - - // ==================== UPS-007: Blocking Error ==================== - describe('UPS-007: Hook returns blocking error (exit code 2)', () => { - it('should block execution when hook exits with code 2', async () => { - await rig.setup('ups-007-blocking-error', { - settings: { - hooks: { - UserPromptSubmit: [ - { - hooks: [ - { - type: 'command', - command: 'echo denied && exit 2', - name: 'ups-blocking-error-hook', - }, - ], - }, - ], - }, - trusted: true, - }, - }); - - const result = await rig.run('Create a file'); - expect(result).toBeDefined(); - }); - - it('should use stderr as reason when hook exits with code 2', async () => { - await rig.setup('ups-007-stderr-reason', { - settings: { - hooks: { - UserPromptSubmit: [ - { - hooks: [ - { - type: 'command', - command: - 'process.stderr.write("Critical security error") && exit 2', - name: 'ups-stderr-hook', - }, - ], - }, - ], - }, - trusted: true, - }, - }); - - const result = await rig.run('Create a file'); - expect(result.toLowerCase()).toContain('error'); - }); - }); - - // ==================== UPS-008: Missing Command ==================== - describe('UPS-008: Hook command does not exist', () => { - it('should continue execution when hook command does not exist', async () => { - await rig.setup('ups-008-missing-command', { - settings: { - hooks: { - UserPromptSubmit: [ - { - hooks: [ - { - type: 'command', - command: '/nonexistent/command/path', - name: 'ups-missing-hook', - }, - ], - }, - ], - }, - trusted: true, - }, - }); - - const result = await rig.run('Say missing test'); - // Missing command should not prevent execution (non-blocking) - expect(result).toBeDefined(); - }); - }); - - // ==================== UPS-009: Correct Input Format ==================== - describe('UPS-009: Hook receives correct input format', () => { - it('should receive properly formatted input when hook is called', async () => { - await rig.setup('ups-009-correct-input', { - settings: { - hooks: { - UserPromptSubmit: [ - { - hooks: [ - { - type: 'command', - command: `node -e " -const input = JSON.parse(process.argv[2]); -const hasRequired = input.session_id && input.transcript_path && input.cwd && input.hook_event_name && input.prompt; + describe('Input Format Validation', () => { + it('should receive properly formatted input when hook is called', async () => { + const inputValidationScript = ` +const input = JSON.parse(process.argv[2] || '{}'); +const hasRequired = input.session_id && input.cwd && input.hook_event_name && input.prompt !== undefined; console.log(JSON.stringify({ decision: 'allow', - hookSpecificOutput: { hookEventName: 'UserPromptSubmit', additionalContext: hasRequired ? 'Valid input' : 'Invalid' } + hookSpecificOutput: { + hookEventName: 'UserPromptSubmit', + additionalContext: hasRequired ? 'Valid input format' : 'Invalid input format' + } })); -"`, - name: 'ups-input-hook', +`; + + await rig.setup('ups-correct-input', { + fakeResponsesPath: join( + RESPONSES_DIR, + 'qwen-userpromptsubmit-allow.responses', + ), + settings: { + hooks: { + enabled: true, + UserPromptSubmit: [ + { + hooks: [ + { + type: 'command', + command: `node -e "${inputValidationScript.replace(/\n/g, ' ')}"`, + name: 'ups-input-hook', + timeout: 5000, + }, + ], + }, + ], + }, + trusted: true, + }, + }); + + const result = await rig.run('Say input test'); + validateModelOutput(result, 'input test', 'UPS: correct input'); + }); + }); + + describe('System Message', () => { + it('should include system message in response when hook provides it', async () => { + const systemMsgScript = `console.log(JSON.stringify({decision: 'allow', systemMessage: 'This is a system message from hook'}));`; + + await rig.setup('ups-system-message', { + fakeResponsesPath: join( + RESPONSES_DIR, + 'qwen-userpromptsubmit-allow.responses', + ), + settings: { + hooks: { + enabled: true, + UserPromptSubmit: [ + { + hooks: [ + { + type: 'command', + command: `node -e "${systemMsgScript}"`, + name: 'ups-system-msg-hook', + timeout: 5000, + }, + ], + }, + ], + }, + trusted: true, + }, + }); + + const result = await rig.run('Say system message'); + expect(result).toBeDefined(); + }); + }); + }); + + // ==================== Stop Hooks ==================== + describe('Stop Hooks', () => { + describe('Allow Decision', () => { + it('should allow stopping when hook returns allow decision', async () => { + const allowStopScript = `console.log(JSON.stringify({decision: 'allow', reason: 'Stop allowed'}));`; + + await rig.setup('stop-allow', { + fakeResponsesPath: join(RESPONSES_DIR, 'qwen-stop-allow.responses'), + settings: { + hooks: { + enabled: true, + Stop: [ + { + hooks: [ + { + type: 'command', + command: `node -e "${allowStopScript}"`, + name: 'stop-allow-hook', + timeout: 5000, + }, + ], + }, + ], + }, + trusted: true, + }, + }); + + const result = await rig.run('Say stop test'); + expect(result).toBeDefined(); + }); + + it('should allow stopping and verify final response is produced', async () => { + const allowFinalScript = `console.log(JSON.stringify({decision: 'allow', hookSpecificOutput: {additionalContext: 'Final context from stop hook'}}));`; + + await rig.setup('stop-allow-final', { + fakeResponsesPath: join(RESPONSES_DIR, 'qwen-stop-allow.responses'), + settings: { + hooks: { + enabled: true, + Stop: [ + { + hooks: [ + { + type: 'command', + command: `node -e "${allowFinalScript}"`, + name: 'stop-final-hook', + timeout: 5000, + }, + ], + }, + ], + }, + trusted: true, + }, + }); + + const result = await rig.run('Say goodbye'); + expect(result).toBeDefined(); + expect(result.length).toBeGreaterThan(0); + }); + }); + + describe('Continue False', () => { + it('should request continue execution when hook returns continue: false', async () => { + const continueScript = `console.log(JSON.stringify({continue: false, stopReason: 'More work needed'}));`; + + await rig.setup('stop-continue-false', { + fakeResponsesPath: join( + RESPONSES_DIR, + 'qwen-stop-continue-false.responses', + ), + settings: { + hooks: { + enabled: true, + Stop: [ + { + hooks: [ + { + type: 'command', + command: `node -e "${continueScript}"`, + name: 'stop-continue-hook', + timeout: 5000, + }, + ], + }, + ], + }, + trusted: true, + }, + }); + + const result = await rig.run('Say continue'); + // When continue: false, the agent may try to continue + expect(result).toBeDefined(); + }); + }); + + describe('Additional Context', () => { + it('should include additional context in final response', async () => { + const contextScript = `console.log(JSON.stringify({decision: 'allow', hookSpecificOutput: {additionalContext: 'Final context from hook'}}));`; + + await rig.setup('stop-add-context', { + fakeResponsesPath: join( + RESPONSES_DIR, + 'qwen-stop-add-context.responses', + ), + settings: { + hooks: { + enabled: true, + Stop: [ + { + hooks: [ + { + type: 'command', + command: `node -e "${contextScript}"`, + name: 'stop-context-hook', + timeout: 5000, + }, + ], + }, + ], + }, + trusted: true, + }, + }); + + const result = await rig.run('What is 3+3?'); + expect(result).toBeDefined(); + }); + + it('should concatenate multiple additionalContext from multiple hooks', async () => { + const context1Script = `console.log(JSON.stringify({decision: 'allow', hookSpecificOutput: {additionalContext: 'context1'}}));`; + const context2Script = `console.log(JSON.stringify({decision: 'allow', hookSpecificOutput: {additionalContext: 'context2'}}));`; + + await rig.setup('stop-multi-context', { + fakeResponsesPath: join( + RESPONSES_DIR, + 'qwen-stop-add-context.responses', + ), + settings: { + hooks: { + enabled: true, + Stop: [ + { + hooks: [ + { + type: 'command', + command: `node -e "${context1Script}"`, + name: 'stop-context-1', + timeout: 5000, + }, + { + type: 'command', + command: `node -e "${context2Script}"`, + name: 'stop-context-2', + timeout: 5000, + }, + ], + }, + ], + }, + trusted: true, + }, + }); + + const result = await rig.run('Say multi context'); + expect(result).toBeDefined(); + }); + }); + + describe('Stop Reason', () => { + it('should include stop reason when hook provides it', async () => { + const reasonScript = `console.log(JSON.stringify({decision: 'allow', stopReason: 'Custom stop reason from hook'}));`; + + await rig.setup('stop-set-reason', { + fakeResponsesPath: join( + RESPONSES_DIR, + 'qwen-stop-set-reason.responses', + ), + settings: { + hooks: { + enabled: true, + Stop: [ + { + hooks: [ + { + type: 'command', + command: `node -e "${reasonScript}"`, + name: 'stop-reason-hook', + timeout: 5000, + }, + ], + }, + ], + }, + trusted: true, + }, + }); + + const result = await rig.run('Say reason test'); + expect(result).toBeDefined(); + }); + }); + + describe('Timeout Handling', () => { + it('should continue stopping when hook times out', async () => { + await rig.setup('stop-timeout', { + fakeResponsesPath: join(RESPONSES_DIR, 'qwen-stop-timeout.responses'), + settings: { + hooks: { + enabled: true, + Stop: [ + { + hooks: [ + { + type: 'command', + command: 'sleep 60', + name: 'stop-timeout-hook', + timeout: 1000, + }, + ], + }, + ], + }, + trusted: true, + }, + }); + + const result = await rig.run('Say timeout'); + // Timeout should not prevent stopping + expect(result).toBeDefined(); + }); + }); + + describe('Error Handling', () => { + it('should continue stopping when hook has non-blocking error', async () => { + await rig.setup('stop-error', { + fakeResponsesPath: join(RESPONSES_DIR, 'qwen-stop-error.responses'), + settings: { + hooks: { + enabled: true, + Stop: [ + { + hooks: [ + { + type: 'command', + command: 'echo warning && exit 1', + name: 'stop-error-hook', + timeout: 5000, + }, + ], + }, + ], + }, + trusted: true, + }, + }); + + const result = await rig.run('Say error'); + // Error should not prevent stopping + expect(result).toBeDefined(); + }); + + it('should continue stopping when hook command does not exist', async () => { + await rig.setup('stop-missing-command', { + fakeResponsesPath: join(RESPONSES_DIR, 'qwen-stop-error.responses'), + settings: { + hooks: { + enabled: true, + Stop: [ + { + hooks: [ + { + type: 'command', + command: '/nonexistent/stop/command', + name: 'stop-missing-hook', + timeout: 5000, + }, + ], + }, + ], + }, + trusted: true, + }, + }); + + const result = await rig.run('Say missing'); + // Missing command should not prevent stopping + expect(result).toBeDefined(); + }); + }); + + describe('System Message', () => { + it('should include system message in final response', async () => { + const systemMsgScript = `console.log(JSON.stringify({decision: 'allow', systemMessage: 'Final system message from stop hook'}));`; + + await rig.setup('stop-system-message', { + fakeResponsesPath: join( + RESPONSES_DIR, + 'qwen-stop-with-message.responses', + ), + settings: { + hooks: { + enabled: true, + Stop: [ + { + hooks: [ + { + type: 'command', + command: `node -e "${systemMsgScript}"`, + name: 'stop-system-msg-hook', + timeout: 5000, + }, + ], + }, + ], + }, + trusted: true, + }, + }); + + const result = await rig.run('Say final'); + expect(result).toBeDefined(); + }); + }); + }); + + // ==================== Multiple Hooks ==================== + describe('Multiple Hooks', () => { + describe('Sequential Execution', () => { + it('should execute hooks sequentially when sequential: true', async () => { + const hook1Script = `console.log(JSON.stringify({decision: 'allow', hookSpecificOutput: {additionalContext: 'first'}}));`; + const hook2Script = `console.log(JSON.stringify({decision: 'allow', hookSpecificOutput: {additionalContext: 'second'}}));`; + + await rig.setup('multi-sequential', { + fakeResponsesPath: join( + RESPONSES_DIR, + 'qwen-sequential-passthrough.responses', + ), + settings: { + hooks: { + enabled: true, + UserPromptSubmit: [ + { + sequential: true, + hooks: [ + { + type: 'command', + command: `node -e "${hook1Script}"`, + name: 'seq-hook-1', + timeout: 5000, + }, + { + type: 'command', + command: `node -e "${hook2Script}"`, + name: 'seq-hook-2', + timeout: 5000, + }, + ], + }, + ], + }, + trusted: true, + }, + }); + + const result = await rig.run('Say sequential'); + expect(result).toBeDefined(); + }); + + it('should stop at first blocking hook and not execute subsequent', async () => { + const blockScript = `console.log(JSON.stringify({decision: 'block', reason: 'Blocked by first hook'}));`; + const allowScript = `console.log(JSON.stringify({decision: 'allow'}));`; + + await rig.setup('multi-first-blocks', { + fakeResponsesPath: join( + RESPONSES_DIR, + 'qwen-sequential-first-blocks.responses', + ), + settings: { + hooks: { + enabled: true, + UserPromptSubmit: [ + { + sequential: true, + hooks: [ + { + type: 'command', + command: `node -e "${blockScript}"`, + name: 'seq-block-hook', + timeout: 5000, + }, + { + type: 'command', + command: `node -e "${allowScript}"`, + name: 'seq-should-not-run', + timeout: 5000, + }, + ], + }, + ], + }, + trusted: true, + }, + }); + + const result = await rig.run('Create a file'); + // First hook blocks, second should not run + expect(result.toLowerCase()).toContain('block'); + }); + + it('should pass output from first hook to second hook input', async () => { + const passScript1 = `console.log(JSON.stringify({decision: 'allow', hookSpecificOutput: {additionalContext: 'from first', passthrough: 'data'}}));`; + const passScript2 = `console.log(JSON.stringify({decision: 'allow', hookSpecificOutput: {additionalContext: 'received passthrough'}}));`; + + await rig.setup('multi-passthrough', { + fakeResponsesPath: join( + RESPONSES_DIR, + 'qwen-sequential-passthrough.responses', + ), + settings: { + hooks: { + enabled: true, + UserPromptSubmit: [ + { + sequential: true, + hooks: [ + { + type: 'command', + command: `node -e "${passScript1}"`, + name: 'passthrough-hook-1', + timeout: 5000, + }, + { + type: 'command', + command: `node -e "${passScript2}"`, + name: 'passthrough-hook-2', + timeout: 5000, + }, + ], + }, + ], + }, + trusted: true, + }, + }); + + const result = await rig.run('Say passthrough'); + expect(result).toBeDefined(); + }); + }); + + describe('Parallel Execution', () => { + it('should execute hooks in parallel when sequential is not set', async () => { + const hook1Script = `console.log(JSON.stringify({decision: 'allow'}));`; + const hook2Script = `console.log(JSON.stringify({decision: 'allow'}));`; + + await rig.setup('multi-parallel', { + fakeResponsesPath: join( + RESPONSES_DIR, + 'qwen-userpromptsubmit-allow.responses', + ), + settings: { + hooks: { + enabled: true, + UserPromptSubmit: [ + { + hooks: [ + { + type: 'command', + command: `node -e "${hook1Script}"`, + name: 'parallel-hook-1', + timeout: 5000, + }, + { + type: 'command', + command: `node -e "${hook2Script}"`, + name: 'parallel-hook-2', + timeout: 5000, + }, + ], + }, + ], + }, + trusted: true, + }, + }); + + const result = await rig.run('Say parallel'); + expect(result).toBeDefined(); + }); + + it('should handle mixed success/failure results from parallel hooks', async () => { + const allowScript = `console.log(JSON.stringify({decision: 'allow'}));`; + + await rig.setup('multi-mixed', { + fakeResponsesPath: join( + RESPONSES_DIR, + 'qwen-parallel-mixed-results.responses', + ), + settings: { + hooks: { + enabled: true, + UserPromptSubmit: [ + { + hooks: [ + { + type: 'command', + command: `node -e "${allowScript}"`, + name: 'mixed-allow-hook', + timeout: 5000, + }, + { + type: 'command', + command: '/nonexistent/command', + name: 'mixed-error-hook', + timeout: 5000, + }, + ], + }, + ], + }, + trusted: true, + }, + }); + + const result = await rig.run('Say mixed'); + // Mixed results: one succeeds, one fails - should continue + expect(result).toBeDefined(); + }); + + it('should allow when any hook returns allow in parallel (OR logic)', async () => { + const blockScript = `console.log(JSON.stringify({decision: 'block', reason: 'blocked'}));`; + const allowScript = `console.log(JSON.stringify({decision: 'allow'}));`; + + await rig.setup('multi-or-logic', { + fakeResponsesPath: join( + RESPONSES_DIR, + 'qwen-userpromptsubmit-allow.responses', + ), + settings: { + hooks: { + enabled: true, + UserPromptSubmit: [ + { + hooks: [ + { + type: 'command', + command: `node -e "${blockScript}"`, + name: 'block-hook', + timeout: 5000, + }, + { + type: 'command', + command: `node -e "${allowScript}"`, + name: 'allow-hook', + timeout: 5000, + }, + ], + }, + ], + }, + trusted: true, + }, + }); + + const result = await rig.run('Say or logic'); + // With OR logic, allow should win + expect(result).toBeDefined(); + }); + }); + }); + + // ==================== Combined Hooks ==================== + describe('Combined Hooks', () => { + it('should execute both Stop and UserPromptSubmit hooks in same session', async () => { + const stopScript = `console.log(JSON.stringify({decision: 'allow'}));`; + const upsScript = `console.log(JSON.stringify({decision: 'allow'}));`; + + await rig.setup('combined-both-hooks', { + fakeResponsesPath: join( + RESPONSES_DIR, + 'qwen-userpromptsubmit-allow.responses', + ), + settings: { + hooksConfig: { enabled: true }, + hooks: { + Stop: [ + { + hooks: [ + { + type: 'command', + command: `node -e "${stopScript}"`, + name: 'stop-hook', + timeout: 5000, }, ], }, ], - }, - trusted: true, - }, - }); - - const result = await rig.run('Say input test'); - validateModelOutput(result, 'input test', 'UPS-009: correct input'); - }); - }); - - // ==================== UPS-010: Empty Prompt ==================== - describe('UPS-010: Hook receives empty prompt', () => { - it('should handle empty prompt correctly', async () => { - await rig.setup('ups-010-empty-prompt', { - settings: { - hooks: { UserPromptSubmit: [ { hooks: [ { type: 'command', - command: 'echo \'{"decision":"allow"}\'', - name: 'ups-empty-hook', + command: `node -e "${upsScript}"`, + name: 'ups-hook', + timeout: 5000, }, ], }, @@ -447,16 +1072,21 @@ console.log(JSON.stringify({ }, }); - const result = await rig.run(''); + const result = await rig.run('Say both hooks'); expect(result).toBeDefined(); }); }); - // ==================== UPS-011: System Message ==================== - describe('UPS-011: Hook returns systemMessage', () => { - it('should include system message in response when hook provides it', async () => { - await rig.setup('ups-011-system-message', { + // ==================== Hook Script File Tests ==================== + describe('Hook Script File Tests', () => { + it('should execute hook from script file', async () => { + await rig.setup('script-file-hook', { + fakeResponsesPath: join( + RESPONSES_DIR, + 'qwen-userpromptsubmit-allow.responses', + ), settings: { + hooksConfig: { enabled: true }, hooks: { UserPromptSubmit: [ { @@ -464,8 +1094,9 @@ console.log(JSON.stringify({ { type: 'command', command: - 'echo \'{"decision":"allow","systemMessage":"This is a system message from hook"}\'', - name: 'ups-system-msg-hook', + "node -e \"console.log(JSON.stringify({decision: 'allow', reason: 'Approved by script file', hookSpecificOutput: {additionalContext: 'Script file executed successfully'}}))\"", + name: 'script-file-hook', + timeout: 5000, }, ], }, @@ -475,16 +1106,18 @@ console.log(JSON.stringify({ }, }); - const result = await rig.run('Say system message'); + const result = await rig.run('Say script file test'); expect(result).toBeDefined(); }); - }); - // ==================== UPS-012: Suppress Output ==================== - describe('UPS-012: Hook returns suppressOutput', () => { - it('should suppress output when hook provides suppressOutput: true', async () => { - await rig.setup('ups-012-suppress-output', { + it('should execute blocking hook from script file', async () => { + await rig.setup('script-file-block-hook', { + fakeResponsesPath: join( + RESPONSES_DIR, + 'qwen-userpromptsubmit-block.responses', + ), settings: { + hooksConfig: { enabled: true }, hooks: { UserPromptSubmit: [ { @@ -492,553 +1125,9 @@ console.log(JSON.stringify({ { type: 'command', command: - 'echo \'{"decision":"allow","suppressOutput":true}\'', - name: 'ups-suppress-hook', - }, - ], - }, - ], - }, - trusted: true, - }, - }); - - const result = await rig.run('Say suppress'); - expect(result).toBeDefined(); - }); - }); -}); - -describe('Hooks Integration - Stop', () => { - let rig: TestRig; - - beforeEach(() => { - rig = new TestRig(); - }); - - afterEach(async () => { - if (rig) { - await rig.cleanup(); - } - }); - - // ==================== STP-001: Allow Decision ==================== - describe('STP-001: Hook returns allow decision', () => { - it('should allow stopping when hook returns allow decision', async () => { - await rig.setup('stp-001-allow-stop', { - settings: { - hooks: { - Stop: [ - { - hooks: [ - { - type: 'command', - command: 'echo \'{"decision":"allow"}\'', - name: 'stop-allow-hook', - }, - ], - }, - ], - }, - trusted: true, - }, - }); - - const result = await rig.run('Say stop test'); - expect(result).toBeDefined(); - }); - - it('should allow stopping and verify final response is produced', async () => { - await rig.setup('stp-001-allow-final', { - settings: { - hooks: { - Stop: [ - { - hooks: [ - { - type: 'command', - command: - 'echo \'{"decision":"allow","hookSpecificOutput":{"additionalContext":"Final context"}}\'', - name: 'stop-final-hook', - }, - ], - }, - ], - }, - trusted: true, - }, - }); - - const result = await rig.run('Say goodbye'); - expect(result).toBeDefined(); - expect(result.length).toBeGreaterThan(0); - }); - }); - - // ==================== STP-002: Continue False ==================== - describe('STP-002: Hook returns continue: false', () => { - it('should request continue execution when hook returns continue: false', async () => { - await rig.setup('stp-002-continue-false', { - settings: { - hooks: { - Stop: [ - { - hooks: [ - { - type: 'command', - command: - 'echo \'{"continue":false,"stopReason":"more work needed"}\'', - name: 'stop-continue-hook', - }, - ], - }, - ], - }, - trusted: true, - }, - }); - - const result = await rig.run('Say continue'); - // When continue: false, the agent may try to continue - expect(result).toBeDefined(); - }); - - it('should continue agent execution when stop hook returns continue: false', async () => { - await rig.setup('stp-002-continue-execution', { - settings: { - hooks: { - Stop: [ - { - hooks: [ - { - type: 'command', - command: - 'echo \'{"continue":false,"stopReason":"Not done yet"}\'', - name: 'stop-continue-exec-hook', - }, - ], - }, - ], - }, - trusted: true, - }, - }); - - const result = await rig.run('Complete this task: say notdone'); - // Agent should continue due to continue: false - expect(result).toBeDefined(); - }); - }); - - // ==================== STP-003: Additional Context ==================== - describe('STP-003: Hook adds additionalContext', () => { - it('should include additional context in final response', async () => { - await rig.setup('stp-003-add-context', { - settings: { - hooks: { - Stop: [ - { - hooks: [ - { - type: 'command', - command: - 'echo \'{"decision":"allow","hookSpecificOutput":{"additionalContext":"final context from hook"}}\'', - name: 'stop-context-hook', - }, - ], - }, - ], - }, - trusted: true, - }, - }); - - const result = await rig.run('What is 3+3?'); - expect(result).toBeDefined(); - }); - - it('should concatenate multiple additionalContext from multiple hooks', async () => { - await rig.setup('stp-003-multi-context', { - settings: { - hooks: { - Stop: [ - { - hooks: [ - { - type: 'command', - command: - 'echo \'{"decision":"allow","hookSpecificOutput":{"additionalContext":"context1"}}\'', - name: 'stop-context-1', - }, - { - type: 'command', - command: - 'echo \'{"decision":"allow","hookSpecificOutput":{"additionalContext":"context2"}}\'', - name: 'stop-context-2', - }, - ], - }, - ], - }, - trusted: true, - }, - }); - - const result = await rig.run('Say multi context'); - expect(result).toBeDefined(); - }); - }); - - // ==================== STP-004: Stop Reason ==================== - describe('STP-004: Hook sets stopReason', () => { - it('should include stop reason when hook provides it', async () => { - await rig.setup('stp-004-set-reason', { - settings: { - hooks: { - Stop: [ - { - hooks: [ - { - type: 'command', - command: - 'echo \'{"decision":"allow","stopReason":"custom stop reason"}\'', - name: 'stop-reason-hook', - }, - ], - }, - ], - }, - trusted: true, - }, - }); - - const result = await rig.run('Say reason test'); - expect(result).toBeDefined(); - }); - }); - - // ==================== STP-005: Timeout ==================== - describe('STP-005: Hook execution timeout', () => { - it('should continue stopping when hook times out', async () => { - await rig.setup('stp-005-timeout', { - settings: { - hooks: { - Stop: [ - { - hooks: [ - { - type: 'command', - command: 'sleep 60', - name: 'stop-timeout-hook', - timeout: 1000, - }, - ], - }, - ], - }, - trusted: true, - }, - }); - - const result = await rig.run('Say timeout'); - // Timeout should not prevent stopping - expect(result).toBeDefined(); - }); - }); - - // ==================== STP-006: Error ==================== - describe('STP-006: Hook execution error', () => { - it('should continue stopping when hook has non-blocking error', async () => { - await rig.setup('stp-006-error', { - settings: { - hooks: { - Stop: [ - { - hooks: [ - { - type: 'command', - command: 'echo warning && exit 1', - name: 'stop-error-hook', - }, - ], - }, - ], - }, - trusted: true, - }, - }); - - const result = await rig.run('Say error'); - // Error should not prevent stopping - expect(result).toBeDefined(); - }); - }); - - // ==================== STP-007: Missing Command ==================== - describe('STP-007: Hook command does not exist', () => { - it('should continue stopping when hook command does not exist', async () => { - await rig.setup('stp-007-missing-command', { - settings: { - hooks: { - Stop: [ - { - hooks: [ - { - type: 'command', - command: '/nonexistent/stop/command', - name: 'stop-missing-hook', - }, - ], - }, - ], - }, - trusted: true, - }, - }); - - const result = await rig.run('Say missing'); - // Missing command should not prevent stopping - expect(result).toBeDefined(); - }); - }); - - // ==================== STP-008: stop_hook_active = true ==================== - describe('STP-008: Hook receives stop_hook_active=true', () => { - it('should receive stop_hook_active=true when stop hook is active', async () => { - await rig.setup('stp-008-active-true', { - settings: { - hooks: { - Stop: [ - { - hooks: [ - { - type: 'command', - command: - 'echo \'{"decision":"allow","hookSpecificOutput":{"additionalContext":"active=true"}}\'', - name: 'stop-active-true-hook', - }, - ], - }, - ], - }, - trusted: true, - }, - }); - - const result = await rig.run('Say active'); - expect(result).toBeDefined(); - }); - }); - - // ==================== STP-009: stop_hook_active = false ==================== - describe('STP-009: Hook receives stop_hook_active=false', () => { - it('should receive stop_hook_active=false when stop hook is not active', async () => { - await rig.setup('stp-009-active-false', { - settings: { - hooks: { - Stop: [ - { - hooks: [ - { - type: 'command', - command: 'echo \'{"decision":"allow"}\'', - name: 'stop-active-false-hook', - }, - ], - }, - ], - }, - trusted: true, - }, - }); - - const result = await rig.run('Say inactive'); - expect(result).toBeDefined(); - }); - }); - - // ==================== STP-010: Last Assistant Message ==================== - describe('STP-010: Hook receives lastAssistantMessage', () => { - it('should receive last assistant message in hook input', async () => { - await rig.setup('stp-010-last-message', { - settings: { - hooks: { - Stop: [ - { - hooks: [ - { - type: 'command', - command: 'echo \'{"decision":"allow"}\'', - name: 'stop-last-msg-hook', - }, - ], - }, - ], - }, - trusted: true, - }, - }); - - const result = await rig.run('Say last msg'); - expect(result).toBeDefined(); - }); - }); - - // ==================== STP-011: System Message ==================== - describe('STP-011: Hook returns systemMessage', () => { - it('should include system message in final response', async () => { - await rig.setup('stp-011-system-message', { - settings: { - hooks: { - Stop: [ - { - hooks: [ - { - type: 'command', - command: - 'echo \'{"decision":"allow","systemMessage":"Final system message"}}\'', - name: 'stop-system-msg-hook', - }, - ], - }, - ], - }, - trusted: true, - }, - }); - - const result = await rig.run('Say final'); - expect(result).toBeDefined(); - }); - }); - - // ==================== STP-012: Decision Deny ==================== - describe('STP-012: Hook returns deny decision', () => { - it('should handle deny decision from stop hook', async () => { - await rig.setup('stp-012-deny', { - settings: { - hooks: { - Stop: [ - { - hooks: [ - { - type: 'command', - command: - 'echo \'{"decision":"deny","reason":"Stopping denied"}\'', - name: 'stop-deny-hook', - }, - ], - }, - ], - }, - trusted: true, - }, - }); - - const result = await rig.run('Say deny test'); - expect(result).toBeDefined(); - }); - }); -}); - -describe('Hooks Integration - Multiple Hooks', () => { - let rig: TestRig; - - beforeEach(() => { - rig = new TestRig(); - }); - - afterEach(async () => { - if (rig) { - await rig.cleanup(); - } - }); - - // ==================== MUL-001: Sequential Execution ==================== - describe('MUL-001: Sequential execution', () => { - it('should execute hooks sequentially when sequential: true', async () => { - await rig.setup('mul-001-sequential', { - settings: { - hooks: { - UserPromptSubmit: [ - { - sequential: true, - hooks: [ - { - type: 'command', - command: 'echo \'{"decision":"allow"}\'', - name: 'seq-hook-1', - }, - { - type: 'command', - command: 'echo \'{"decision":"allow"}\'', - name: 'seq-hook-2', - }, - ], - }, - ], - }, - trusted: true, - }, - }); - - const result = await rig.run('Say sequential'); - expect(result).toBeDefined(); - }); - - it('should execute both hooks in order when sequential: true', async () => { - await rig.setup('mul-001-sequential-order', { - settings: { - hooks: { - UserPromptSubmit: [ - { - sequential: true, - hooks: [ - { - type: 'command', - command: - 'echo \'{"decision":"allow","hookSpecificOutput":{"additionalContext":"first"}}\'', - name: 'seq-first', - }, - { - type: 'command', - command: - 'echo \'{"decision":"allow","hookSpecificOutput":{"additionalContext":"second"}}\'', - name: 'seq-second', - }, - ], - }, - ], - }, - trusted: true, - }, - }); - - const result = await rig.run('Say order'); - expect(result).toBeDefined(); - }); - }); - - // ==================== MUL-002: First Hook Blocks ==================== - describe('MUL-002: Sequential first hook blocks', () => { - it('should stop at first blocking hook and not execute subsequent', async () => { - await rig.setup('mul-002-first-blocks', { - settings: { - hooks: { - UserPromptSubmit: [ - { - sequential: true, - hooks: [ - { - type: 'command', - command: - 'echo \'{"decision":"block","reason":"blocked by first"}\'', - name: 'seq-block-hook', - }, - { - type: 'command', - command: 'echo \'{"decision":"allow"}\'', - name: 'seq-should-not-run', + "node -e \"console.log(JSON.stringify({decision: 'block', reason: 'Blocked by security script'}))\"", + name: 'script-block-hook', + timeout: 5000, }, ], }, @@ -1049,327 +1138,9 @@ describe('Hooks Integration - Multiple Hooks', () => { }); const result = await rig.run('Create a file'); - // First hook blocks, second should not run + + // Prompt should be blocked expect(result.toLowerCase()).toContain('block'); }); }); - - // ==================== MUL-003: Output Passthrough ==================== - describe('MUL-003: Sequential output passthrough', () => { - it('should pass output from first hook to second hook input', async () => { - await rig.setup('mul-003-passthrough', { - settings: { - hooks: { - UserPromptSubmit: [ - { - sequential: true, - hooks: [ - { - type: 'command', - command: - 'echo \'{"decision":"allow","hookSpecificOutput":{"additionalContext":"from first"}}\'', - name: 'passthrough-hook-1', - }, - { - type: 'command', - command: 'echo \'{"decision":"allow"}\'', - name: 'passthrough-hook-2', - }, - ], - }, - ], - }, - trusted: true, - }, - }); - - const result = await rig.run('Say passthrough'); - expect(result).toBeDefined(); - }); - }); - - // ==================== MUL-004: Parallel Execution ==================== - describe('MUL-004: Parallel execution', () => { - it('should execute hooks in parallel when sequential is not set', async () => { - await rig.setup('mul-004-parallel', { - settings: { - hooks: { - UserPromptSubmit: [ - { - hooks: [ - { - type: 'command', - command: 'echo \'{"decision":"allow"}\'', - name: 'parallel-hook-1', - }, - { - type: 'command', - command: 'echo \'{"decision":"allow"}\'', - name: 'parallel-hook-2', - }, - ], - }, - ], - }, - trusted: true, - }, - }); - - const result = await rig.run('Say parallel'); - expect(result).toBeDefined(); - }); - }); - - // ==================== MUL-005: Mixed Results ==================== - describe('MUL-005: Parallel with mixed results', () => { - it('should handle mixed success/failure results from parallel hooks', async () => { - await rig.setup('mul-005-mixed', { - settings: { - hooks: { - UserPromptSubmit: [ - { - hooks: [ - { - type: 'command', - command: 'echo \'{"decision":"allow"}\'', - name: 'mixed-allow-hook', - }, - { - type: 'command', - command: '/nonexistent/command', - name: 'mixed-error-hook', - }, - ], - }, - ], - }, - trusted: true, - }, - }); - - const result = await rig.run('Say mixed'); - // Mixed results: one succeeds, one fails - should continue - expect(result).toBeDefined(); - }); - }); - - // ==================== MUL-006: All Block ==================== - describe('MUL-006: All hooks return block', () => { - it('should block when all hooks return block in sequential execution', async () => { - await rig.setup('mul-006-all-block', { - settings: { - hooks: { - UserPromptSubmit: [ - { - sequential: true, - hooks: [ - { - type: 'command', - command: - 'echo \'{"decision":"block","reason":"first block"}\'', - name: 'block-hook-1', - }, - { - type: 'command', - command: - 'echo \'{"decision":"block","reason":"second block"}\'', - name: 'block-hook-2', - }, - ], - }, - ], - }, - trusted: true, - }, - }); - - const result = await rig.run('Create file'); - expect(result.toLowerCase()).toContain('block'); - }); - }); - - // ==================== MUL-007: OR Logic for Decisions ==================== - describe('MUL-007: OR logic for parallel hook decisions', () => { - it('should allow when any hook returns allow in parallel', async () => { - await rig.setup('mul-007-or-logic', { - settings: { - hooks: { - UserPromptSubmit: [ - { - hooks: [ - { - type: 'command', - command: 'echo \'{"decision":"block","reason":"blocked"}\'', - name: 'block-hook', - }, - { - type: 'command', - command: 'echo \'{"decision":"allow"}\'', - name: 'allow-hook', - }, - ], - }, - ], - }, - trusted: true, - }, - }); - - const result = await rig.run('Say or logic'); - // With OR logic, allow should win - expect(result).toBeDefined(); - }); - }); -}); - -describe('Hooks Integration - Combined Stop and UserPromptSubmit', () => { - let rig: TestRig; - - beforeEach(() => { - rig = new TestRig(); - }); - - afterEach(async () => { - if (rig) { - await rig.cleanup(); - } - }); - - it('should execute both Stop and UserPromptSubmit hooks in same session', async () => { - await rig.setup('combined-both-hooks', { - settings: { - hooks: { - Stop: [ - { - hooks: [ - { - type: 'command', - command: 'echo \'{"decision":"allow"}\'', - name: 'stop-hook', - }, - ], - }, - ], - UserPromptSubmit: [ - { - hooks: [ - { - type: 'command', - command: 'echo \'{"decision":"allow"}\'', - name: 'ups-hook', - }, - ], - }, - ], - }, - trusted: true, - }, - }); - - const result = await rig.run('Say both hooks'); - expect(result).toBeDefined(); - }); - - it('should support matcher for Stop hook', async () => { - await rig.setup('matcher-stop-hook', { - settings: { - hooks: { - Stop: [ - { - matcher: 'write_file', - hooks: [ - { - type: 'command', - command: 'echo \'{"decision":"allow"}\'', - name: 'matcher-stop-hook', - }, - ], - }, - ], - }, - trusted: true, - }, - }); - - await rig.run('Create a file matcher_test.txt with content hello'); - - const foundToolCall = await rig.waitForToolCall('write_file'); - expect(foundToolCall).toBeTruthy(); - - const fileContent = rig.readFile('matcher_test.txt'); - expect(fileContent).toContain('hello'); - }); - - it('should execute multiple hooks with different matchers', async () => { - await rig.setup('multiple-matchers', { - settings: { - hooks: { - Stop: [ - { - matcher: 'read_file', - hooks: [ - { - type: 'command', - command: 'echo \'{"decision":"allow"}\'', - name: 'matcher-read', - }, - ], - }, - { - matcher: 'write_file', - hooks: [ - { - type: 'command', - command: 'echo \'{"decision":"allow"}\'', - name: 'matcher-write', - }, - ], - }, - ], - }, - trusted: true, - }, - }); - - const result = await rig.run( - 'Create file multi.txt with content test and read it', - ); - expect(result).toBeDefined(); - }); - - it('should handle UPS allow + Stop block combination', async () => { - await rig.setup('ups-allow-stop-block', { - settings: { - hooks: { - Stop: [ - { - hooks: [ - { - type: 'command', - command: - 'echo \'{"decision":"block","reason":"stop blocked"}}\'', - name: 'stop-block-hook', - }, - ], - }, - ], - UserPromptSubmit: [ - { - hooks: [ - { - type: 'command', - command: 'echo \'{"decision":"allow"}\'', - name: 'ups-allow-hook', - }, - ], - }, - ], - }, - trusted: true, - }, - }); - - const result = await rig.run('Say combined test'); - expect(result).toBeDefined(); - }); }); diff --git a/integration-tests/hook-integration/responses/qwen-parallel-mixed-results.responses b/integration-tests/hook-integration/responses/qwen-parallel-mixed-results.responses index e2ff28d45..89abce836 100644 --- a/integration-tests/hook-integration/responses/qwen-parallel-mixed-results.responses +++ b/integration-tests/hook-integration/responses/qwen-parallel-mixed-results.responses @@ -1 +1 @@ -{"method":"generateContentStream","response":[{"candidates":[{"content":{"parts":[{"text":"Parallel hooks executed with mixed results.","thought":true}],"role":"model"},"index":0}],"usageMetadata":{"promptTokenCount":100,"totalTokenCount":120}}]} +{"method":"generateContentStream","response":[{"candidates":[{"content":{"parts":[{"text":"Parallel hooks executed with mixed results - one succeeded, one failed.","thought":true}],"role":"model"},"index":0}],"usageMetadata":{"promptTokenCount":100,"totalTokenCount":120}}]} diff --git a/integration-tests/hook-integration/responses/qwen-sequential-first-blocks.responses b/integration-tests/hook-integration/responses/qwen-sequential-first-blocks.responses index 24f26749d..6ca910919 100644 --- a/integration-tests/hook-integration/responses/qwen-sequential-first-blocks.responses +++ b/integration-tests/hook-integration/responses/qwen-sequential-first-blocks.responses @@ -1 +1 @@ -{"method":"generateContentStream","response":[{"candidates":[{"content":{"parts":[{"text":"Sequential hook 1 executed first.","thought":true}],"role":"model"},"index":0}],"usageMetadata":{"promptTokenCount":100,"totalTokenCount":120}}]} +{"method":"generateContentStream","response":[{"candidates":[{"content":{"parts":[{"text":"The first hook blocked the request. Second hook was not executed.","thought":true}],"role":"model"},"index":0}],"usageMetadata":{"promptTokenCount":100,"totalTokenCount":120}}]} diff --git a/integration-tests/hook-integration/responses/qwen-sequential-passthrough.responses b/integration-tests/hook-integration/responses/qwen-sequential-passthrough.responses index b938dc22e..1ea3fd78a 100644 --- a/integration-tests/hook-integration/responses/qwen-sequential-passthrough.responses +++ b/integration-tests/hook-integration/responses/qwen-sequential-passthrough.responses @@ -1 +1 @@ -{"method":"generateContentStream","response":[{"candidates":[{"content":{"parts":[{"text":"Sequential hooks executed with output passthrough.","thought":true}],"role":"model"},"index":0}],"usageMetadata":{"promptTokenCount":100,"totalTokenCount":120}}]} +{"method":"generateContentStream","response":[{"candidates":[{"content":{"parts":[{"text":"Output from first hook was passed to second hook successfully.","thought":true}],"role":"model"},"index":0}],"usageMetadata":{"promptTokenCount":100,"totalTokenCount":120}}]} diff --git a/integration-tests/hook-integration/responses/qwen-stop-add-context.responses b/integration-tests/hook-integration/responses/qwen-stop-add-context.responses index bf087d303..6e24bd8a6 100644 --- a/integration-tests/hook-integration/responses/qwen-stop-add-context.responses +++ b/integration-tests/hook-integration/responses/qwen-stop-add-context.responses @@ -1 +1 @@ -{"method":"generateContentStream","response":[{"candidates":[{"content":{"parts":[{"text":"Final response with additional context from hook.","thought":true}],"role":"model"},"index":0}],"usageMetadata":{"promptTokenCount":100,"totalTokenCount":120}}]} +{"method":"generateContentStream","response":[{"candidates":[{"content":{"parts":[{"text":"I have received the final context from the stop hook and included it in my response.","thought":true}],"role":"model"},"index":0}],"usageMetadata":{"promptTokenCount":100,"totalTokenCount":120}}]} diff --git a/integration-tests/hook-integration/responses/qwen-stop-error.responses b/integration-tests/hook-integration/responses/qwen-stop-error.responses index ab57a0cc3..514d3ee9c 100644 --- a/integration-tests/hook-integration/responses/qwen-stop-error.responses +++ b/integration-tests/hook-integration/responses/qwen-stop-error.responses @@ -1 +1 @@ -{"method":"generateContentStream","response":[{"candidates":[{"content":{"parts":[{"text":"Stopping with warning from hook.","thought":true}],"role":"model"},"index":0}],"usageMetadata":{"promptTokenCount":100,"totalTokenCount":120}}]} +{"method":"generateContentStream","response":[{"candidates":[{"content":{"parts":[{"text":"The stop hook had an error but I completed my response.","thought":true}],"role":"model"},"index":0}],"usageMetadata":{"promptTokenCount":100,"totalTokenCount":120}}]} diff --git a/integration-tests/hook-integration/responses/qwen-stop-set-reason.responses b/integration-tests/hook-integration/responses/qwen-stop-set-reason.responses index 14336964d..45ec154fd 100644 --- a/integration-tests/hook-integration/responses/qwen-stop-set-reason.responses +++ b/integration-tests/hook-integration/responses/qwen-stop-set-reason.responses @@ -1 +1 @@ -{"method":"generateContentStream","response":[{"candidates":[{"content":{"parts":[{"text":"Stopping now. Reason: Hook specified stop reason.","thought":true}],"role":"model"},"index":0}],"usageMetadata":{"promptTokenCount":100,"totalTokenCount":120}}]} +{"method":"generateContentStream","response":[{"candidates":[{"content":{"parts":[{"text":"Task completed with custom stop reason from hook.","thought":true}],"role":"model"},"index":0}],"usageMetadata":{"promptTokenCount":100,"totalTokenCount":120}}]} diff --git a/integration-tests/hook-integration/responses/qwen-stop-timeout.responses b/integration-tests/hook-integration/responses/qwen-stop-timeout.responses index 3a893dcc0..7759a2268 100644 --- a/integration-tests/hook-integration/responses/qwen-stop-timeout.responses +++ b/integration-tests/hook-integration/responses/qwen-stop-timeout.responses @@ -1 +1 @@ -{"method":"generateContentStream","response":[{"candidates":[{"content":{"parts":[{"text":"The stop hook timed out but stopping anyway.","thought":true}],"role":"model"},"index":0}],"usageMetadata":{"promptTokenCount":100,"totalTokenCount":120}}]} +{"method":"generateContentStream","response":[{"candidates":[{"content":{"parts":[{"text":"The stop hook timed out but I completed my response.","thought":true}],"role":"model"},"index":0}],"usageMetadata":{"promptTokenCount":100,"totalTokenCount":120}}]} diff --git a/integration-tests/hook-integration/responses/qwen-stop-with-message.responses b/integration-tests/hook-integration/responses/qwen-stop-with-message.responses index 1c4a1fc0f..d7bd04416 100644 --- a/integration-tests/hook-integration/responses/qwen-stop-with-message.responses +++ b/integration-tests/hook-integration/responses/qwen-stop-with-message.responses @@ -1 +1 @@ -{"method":"generateContentStream","response":[{"candidates":[{"content":{"parts":[{"text":"Received last assistant message in hook.","thought":true}],"role":"model"},"index":0}],"usageMetadata":{"promptTokenCount":100,"totalTokenCount":120}}]} +{"method":"generateContentStream","response":[{"candidates":[{"content":{"parts":[{"text":"Final system message from stop hook has been processed.","thought":true}],"role":"model"},"index":0}],"usageMetadata":{"promptTokenCount":100,"totalTokenCount":120}}]} diff --git a/integration-tests/hook-integration/responses/qwen-userpromptsubmit-add-context.responses b/integration-tests/hook-integration/responses/qwen-userpromptsubmit-add-context.responses index 128412e00..651ff0f89 100644 --- a/integration-tests/hook-integration/responses/qwen-userpromptsubmit-add-context.responses +++ b/integration-tests/hook-integration/responses/qwen-userpromptsubmit-add-context.responses @@ -1 +1 @@ -{"method":"generateContentStream","response":[{"candidates":[{"content":{"parts":[{"text":"The hook has added additional context to your request.","thought":true}],"role":"model"},"index":0}],"usageMetadata":{"promptTokenCount":120,"totalTokenCount":150}}]} +{"method":"generateContentStream","response":[{"candidates":[{"content":{"parts":[{"text":"I have received additional context from the hook and will use it in my response.","thought":true}],"role":"model"},"index":0}],"usageMetadata":{"promptTokenCount":100,"totalTokenCount":120}}]} diff --git a/integration-tests/hook-integration/responses/qwen-userpromptsubmit-empty-prompt.responses b/integration-tests/hook-integration/responses/qwen-userpromptsubmit-empty-prompt.responses index 33b439d5c..adece08f2 100644 --- a/integration-tests/hook-integration/responses/qwen-userpromptsubmit-empty-prompt.responses +++ b/integration-tests/hook-integration/responses/qwen-userpromptsubmit-empty-prompt.responses @@ -1 +1 @@ -{"method":"generateContentStream","response":[{"candidates":[{"content":{"parts":[{"text":"Empty prompt received and handled.","thought":true}],"role":"model"},"index":0}],"usageMetadata":{"promptTokenCount":50,"totalTokenCount":70}}]} +{"method":"generateContentStream","response":[{"candidates":[{"content":{"parts":[{"text":"I received an empty prompt. How can I help you?","thought":true}],"role":"model"},"index":0}],"usageMetadata":{"promptTokenCount":100,"totalTokenCount":120}}]} diff --git a/integration-tests/hook-integration/responses/qwen-userpromptsubmit-error-blocking.responses b/integration-tests/hook-integration/responses/qwen-userpromptsubmit-error-blocking.responses index 909c62c9d..4210e2a02 100644 --- a/integration-tests/hook-integration/responses/qwen-userpromptsubmit-error-blocking.responses +++ b/integration-tests/hook-integration/responses/qwen-userpromptsubmit-error-blocking.responses @@ -1 +1 @@ -{"method":"generateContentStream","response":[{"candidates":[{"content":{"parts":[{"text":"Request denied due to critical error.","thought":true}],"role":"model"},"index":0}],"usageMetadata":{"promptTokenCount":100,"totalTokenCount":120}}]} +{"method":"generateContentStream","response":[{"candidates":[{"content":{"parts":[{"text":"The hook had a blocking error and your request was blocked.","thought":true}],"role":"model"},"index":0}],"usageMetadata":{"promptTokenCount":100,"totalTokenCount":120}}]} diff --git a/integration-tests/hook-integration/responses/qwen-userpromptsubmit-error-nonblocking.responses b/integration-tests/hook-integration/responses/qwen-userpromptsubmit-error-nonblocking.responses index 0b96f90c7..2488b6bc0 100644 --- a/integration-tests/hook-integration/responses/qwen-userpromptsubmit-error-nonblocking.responses +++ b/integration-tests/hook-integration/responses/qwen-userpromptsubmit-error-nonblocking.responses @@ -1 +1 @@ -{"method":"generateContentStream","response":[{"candidates":[{"content":{"parts":[{"text":"There was a warning but the request continues.","thought":true}],"role":"model"},"index":0}],"usageMetadata":{"promptTokenCount":100,"totalTokenCount":120}}]} +{"method":"generateContentStream","response":[{"candidates":[{"content":{"parts":[{"text":"The hook had a non-blocking error but I continued processing your request.","thought":true}],"role":"model"},"index":0}],"usageMetadata":{"promptTokenCount":100,"totalTokenCount":120}}]} diff --git a/integration-tests/hook-integration/responses/qwen-userpromptsubmit-missing-command.responses b/integration-tests/hook-integration/responses/qwen-userpromptsubmit-missing-command.responses index ae8aed3f0..ed89d05e5 100644 --- a/integration-tests/hook-integration/responses/qwen-userpromptsubmit-missing-command.responses +++ b/integration-tests/hook-integration/responses/qwen-userpromptsubmit-missing-command.responses @@ -1 +1 @@ -{"method":"generateContentStream","response":[{"candidates":[{"content":{"parts":[{"text":"The command was not found but continuing.","thought":true}],"role":"model"},"index":0}],"usageMetadata":{"promptTokenCount":100,"totalTokenCount":120}}]} +{"method":"generateContentStream","response":[{"candidates":[{"content":{"parts":[{"text":"The hook command was not found but I continued processing your request.","thought":true}],"role":"model"},"index":0}],"usageMetadata":{"promptTokenCount":100,"totalTokenCount":120}}]} diff --git a/integration-tests/hook-integration/responses/qwen-userpromptsubmit-modify.responses b/integration-tests/hook-integration/responses/qwen-userpromptsubmit-modify.responses index 2600900a9..7e1074911 100644 --- a/integration-tests/hook-integration/responses/qwen-userpromptsubmit-modify.responses +++ b/integration-tests/hook-integration/responses/qwen-userpromptsubmit-modify.responses @@ -1 +1 @@ -{"method":"generateContentStream","response":[{"candidates":[{"content":{"parts":[{"text":"I've received your modified request. I'll respond to the modified version.","thought":true}],"role":"model"},"index":0}],"usageMetadata":{"promptTokenCount":120,"totalTokenCount":150}}]} +{"method":"generateContentStream","response":[{"candidates":[{"content":{"parts":[{"text":"I received your modified prompt and will process it accordingly.","thought":true}],"role":"model"},"index":0}],"usageMetadata":{"promptTokenCount":100,"totalTokenCount":120}}]} diff --git a/integration-tests/hook-integration/responses/qwen-userpromptsubmit-timeout.responses b/integration-tests/hook-integration/responses/qwen-userpromptsubmit-timeout.responses index e435a8902..ed304ea8d 100644 --- a/integration-tests/hook-integration/responses/qwen-userpromptsubmit-timeout.responses +++ b/integration-tests/hook-integration/responses/qwen-userpromptsubmit-timeout.responses @@ -1 +1 @@ -{"method":"generateContentStream","response":[{"candidates":[{"content":{"parts":[{"text":"The request timed out but will continue.","thought":true}],"role":"model"},"index":0}],"usageMetadata":{"promptTokenCount":100,"totalTokenCount":120}}]} +{"method":"generateContentStream","response":[{"candidates":[{"content":{"parts":[{"text":"The hook timed out but I continued processing your request.","thought":true}],"role":"model"},"index":0}],"usageMetadata":{"promptTokenCount":100,"totalTokenCount":120}}]} diff --git a/integration-tests/hooks.test.ts b/integration-tests/hooks.test.ts deleted file mode 100644 index ae8759a03..000000000 --- a/integration-tests/hooks.test.ts +++ /dev/null @@ -1,325 +0,0 @@ -/** - * @license - * Copyright 2026 Qwen Team - * SPDX-License-Identifier: Apache-2.0 - */ - -import { describe, it, expect } from 'vitest'; -import { TestRig, printDebugInfo, validateModelOutput } from './test-helper.js'; - -describe('hooks', () => { - it('should execute Stop hook when response finishes', async () => { - const rig = new TestRig(); - await rig.setup('should execute Stop hook when response finishes', { - settings: { - hooks: { - Stop: [ - { - hooks: [ - { - type: 'command', - command: 'echo "STOP_HOOK_EXECUTED" > stop_hook_result.txt', - name: 'test-stop-hook', - }, - ], - }, - ], - }, - trusted: true, - }, - }); - - const prompt = `Say "hello" and that's it.`; - - const result = await rig.run(prompt); - - // Wait for telemetry to be ready (hook should have executed) - await rig.waitForTelemetryReady(); - - // Check that the Stop hook executed by looking for the output file - try { - const hookOutput = rig.readFile('stop_hook_result.txt'); - expect(hookOutput).toContain('STOP_HOOK_EXECUTED'); - } catch { - // Hook file might not exist - check telemetry for hook execution - // Stop hook is a command hook, it may not appear in tool logs - // but the test should at least complete without errors - } - - // Validate model output - validateModelOutput(result, 'hello', 'Stop hook test'); - }); - - it('should execute UserPromptSubmit hook when user submits prompt', async () => { - const rig = new TestRig(); - await rig.setup( - 'should execute UserPromptSubmit hook when user submits prompt', - { - settings: { - hooks: { - UserPromptSubmit: [ - { - hooks: [ - { - type: 'command', - command: - 'echo "USER_PROMPT_SUBMITTED: $QWEN_HOOK_PROMPT" > prompt_hook_result.txt', - name: 'test-prompt-submit-hook', - }, - ], - }, - ], - }, - trusted: true, - }, - }, - ); - - const prompt = `Just say "received" and nothing else.`; - - const result = await rig.run(prompt); - - // Wait for telemetry - await rig.waitForTelemetryReady(); - - // Check that the UserPromptSubmit hook executed - try { - const hookOutput = rig.readFile('prompt_hook_result.txt'); - expect(hookOutput).toContain('USER_PROMPT_SUBMITTED'); - } catch { - // Hook file might not exist - that's okay, the test verifies the CLI runs - } - - // Validate model output - validateModelOutput(result, 'received', 'UserPromptSubmit hook test'); - }); - - it('should execute both Stop and UserPromptSubmit hooks', async () => { - const rig = new TestRig(); - await rig.setup('should execute both Stop and UserPromptSubmit hooks', { - settings: { - hooks: { - Stop: [ - { - hooks: [ - { - type: 'command', - command: 'echo "stop_executed" > both_stop.txt', - name: 'stop-hook', - }, - ], - }, - ], - UserPromptSubmit: [ - { - hooks: [ - { - type: 'command', - command: 'echo "prompt_submitted" > both_prompt.txt', - name: 'prompt-hook', - }, - ], - }, - ], - }, - trusted: true, - }, - }); - - const prompt = `Say "testing both hooks".`; - - const result = await rig.run(prompt); - - // Wait for telemetry - await rig.waitForTelemetryReady(); - - // Check both hooks executed - try { - const stopOutput = rig.readFile('both_stop.txt'); - expect(stopOutput).toContain('stop_executed'); - } catch { - /* empty */ - } - - try { - const promptOutput = rig.readFile('both_prompt.txt'); - expect(promptOutput).toContain('prompt_submitted'); - } catch { - /* empty */ - } - - validateModelOutput(result, 'testing both hooks', 'Both hooks test'); - }); - - it('should support sequential hook execution for Stop event', async () => { - const rig = new TestRig(); - await rig.setup('should support sequential hook execution for Stop event', { - settings: { - hooks: { - Stop: [ - { - sequential: true, - hooks: [ - { - type: 'command', - command: 'echo "first" > seq1.txt', - name: 'seq-hook-1', - }, - { - type: 'command', - command: 'echo "second" > seq2.txt', - name: 'seq-hook-2', - }, - ], - }, - ], - }, - trusted: true, - }, - }); - - const prompt = `Say "sequential test".`; - - const result = await rig.run(prompt); - - // Wait for telemetry - await rig.waitForTelemetryReady(); - - // Check that both sequential hooks executed - try { - const firstOutput = rig.readFile('seq1.txt'); - expect(firstOutput).toContain('first'); - } catch { - /* empty */ - } - - try { - const secondOutput = rig.readFile('seq2.txt'); - expect(secondOutput).toContain('second'); - } catch { - /* empty */ - } - - validateModelOutput( - result, - 'sequential test', - 'Sequential Stop hooks test', - ); - }); - - it('should support matcher for Stop hook', async () => { - const rig = new TestRig(); - await rig.setup('should support matcher for Stop hook', { - settings: { - hooks: { - Stop: [ - { - matcher: 'write_file', - hooks: [ - { - type: 'command', - command: 'echo "matched_stop" > matcher_stop.txt', - name: 'matcher-stop-hook', - }, - ], - }, - ], - }, - trusted: true, - }, - }); - - const prompt = `Create a file "matcher_test.txt" with content "hello".`; - - const result = await rig.run(prompt); - - const foundToolCall = await rig.waitForToolCall('write_file'); - - if (!foundToolCall) { - printDebugInfo(rig, result); - } - - expect(foundToolCall).toBeTruthy(); - validateModelOutput(result, 'matcher_test.txt', 'Matcher Stop hook test'); - - const fileContent = rig.readFile('matcher_test.txt'); - expect(fileContent).toContain('hello'); - }); - - it('should allow Stop hook to add additional context to response', async () => { - const rig = new TestRig(); - await rig.setup( - 'should allow Stop hook to add additional context to response', - { - settings: { - hooks: { - Stop: [ - { - hooks: [ - { - type: 'command', - command: - 'echo "{\\"continue\\": true, \\"hookSpecificOutput\\": {\\"additionalContext\\": \\"Custom context from hook\\"}}}"', - name: 'context-hook', - }, - ], - }, - ], - }, - trusted: true, - }, - }, - ); - - const prompt = `Say "test complete".`; - - const result = await rig.run(prompt); - - // Wait for telemetry - await rig.waitForTelemetryReady(); - - // The hook can add context to the response - // Check that the model produced output - validateModelOutput(result, 'test complete', 'Stop hook with context test'); - }); - - it('should allow UserPromptSubmit hook to add system message', async () => { - const rig = new TestRig(); - await rig.setup( - 'should allow UserPromptSubmit hook to add system message', - { - settings: { - hooks: { - UserPromptSubmit: [ - { - hooks: [ - { - type: 'command', - command: - 'echo "{\\"continue\\": true, \\"systemMessage\\": \\"You are being tested.\\"}}}"', - name: 'system-msg-hook', - }, - ], - }, - ], - }, - trusted: true, - }, - }, - ); - - const prompt = `What is 2+2?`; - - const result = await rig.run(prompt); - - // Wait for telemetry - await rig.waitForTelemetryReady(); - - // The hook can add a system message that influences the response - validateModelOutput( - result, - '4', - 'UserPromptSubmit with system message test', - ); - }); -}); diff --git a/integration-tests/test-helper.ts b/integration-tests/test-helper.ts index a08b3df50..4e5bd6abc 100644 --- a/integration-tests/test-helper.ts +++ b/integration-tests/test-helper.ts @@ -145,6 +145,7 @@ export class TestRig { testName?: string; _lastRunStdout?: string; _interactiveOutput = ''; + _fakeResponsesPath?: string; constructor() { this.bundlePath = join(__dirname, '..', 'dist/cli.js'); @@ -160,13 +161,21 @@ export class TestRig { setup( testName: string, - options: { settings?: Record } = {}, + options: { + settings?: Record; + fakeResponsesPath?: string; + } = {}, ) { this.testName = testName; const sanitizedName = sanitizeTestName(testName); this.testDir = join(env['INTEGRATION_TEST_FILE_DIR']!, sanitizedName); mkdirSync(this.testDir, { recursive: true }); + // Store fake responses path for use in run() + if (options.fakeResponsesPath) { + this._fakeResponsesPath = options.fakeResponsesPath; + } + // Create a settings file to point the CLI to the local collector const qwenDir = join(this.testDir, '.qwen'); mkdirSync(qwenDir, { recursive: true }); @@ -190,6 +199,16 @@ export class TestRig { ); } + /** + * Creates a script file in the test directory and returns its path. + * Useful for creating hook scripts that need to be executed. + */ + createScript(fileName: string, content: string): string { + const filePath = join(this.testDir!, fileName); + writeFileSync(filePath, content, { mode: 0o755 }); + return filePath; + } + createFile(fileName: string, content: string) { const filePath = join(this.testDir!, fileName); writeFileSync(filePath, content); @@ -256,10 +275,16 @@ export class TestRig { commandArgs.push(...args); + // Set up environment with fake responses path if configured + const childEnv = { ...process.env }; + if (this._fakeResponsesPath) { + childEnv['QWEN_FAKE_RESPONSES_PATH'] = this._fakeResponsesPath; + } + const child = spawn(command, commandArgs, { cwd: this.testDir!, stdio: 'pipe', - env: process.env, + env: childEnv, }); let stdout = ''; diff --git a/packages/cli/src/config/config.ts b/packages/cli/src/config/config.ts index 0a4e0a6c4..0b54b901c 100755 --- a/packages/cli/src/config/config.ts +++ b/packages/cli/src/config/config.ts @@ -125,6 +125,7 @@ export interface CliArgs { acp: boolean | undefined; experimentalAcp: boolean | undefined; experimentalLsp: boolean | undefined; + experimentalHooks: boolean | undefined; extensions: string[] | undefined; listExtensions: boolean | undefined; openaiLogging: boolean | undefined; @@ -338,6 +339,12 @@ export async function parseArguments(): Promise { 'Enable experimental LSP (Language Server Protocol) feature for code intelligence', default: false, }) + .option('experimental-hooks', { + type: 'boolean', + description: + 'Enable experimental hooks feature for lifecycle event customization', + default: false, + }) .option('channel', { type: 'string', choices: ['VSCode', 'ACP', 'SDK', 'CI'], @@ -1027,6 +1034,8 @@ export async function loadCliConfig( format: outputSettingsFormat, }, hooks: settings.hooks, + enableHooks: + argv.experimentalHooks === true || settings.hooks?.enabled === true, channel: argv.channel, // Precedence: explicit CLI flag > settings file > default(true). // NOTE: do NOT set a yargs default for `chat-recording`, otherwise argv will diff --git a/packages/cli/src/config/settingsSchema.ts b/packages/cli/src/config/settingsSchema.ts index d505cdca1..9ef11cf4f 100644 --- a/packages/cli/src/config/settingsSchema.ts +++ b/packages/cli/src/config/settingsSchema.ts @@ -1187,6 +1187,16 @@ const SETTINGS_SCHEMA = { 'Hook configurations for extending CLI behavior at various lifecycle points.', showInDialog: false, properties: { + enabled: { + type: 'boolean', + label: 'Enable Hooks', + category: 'Advanced', + requiresRestart: false, + default: false, + description: + 'Enable the hooks feature. When enabled, hooks defined in UserPromptSubmit and Stop will be executed.', + showInDialog: false, + }, disabled: { type: 'array', label: 'Disabled Hooks', diff --git a/packages/cli/src/gemini.test.tsx b/packages/cli/src/gemini.test.tsx index 6c48658ad..8c9cd687f 100644 --- a/packages/cli/src/gemini.test.tsx +++ b/packages/cli/src/gemini.test.tsx @@ -497,6 +497,7 @@ describe('gemini.tsx main function kitty protocol', () => { authType: undefined, maxSessionTurns: undefined, experimentalLsp: undefined, + experimentalHooks: undefined, channel: undefined, chatRecording: undefined, sessionId: undefined, diff --git a/packages/core/src/config/config.ts b/packages/core/src/config/config.ts index 8293730f9..bf20f1172 100644 --- a/packages/core/src/config/config.ts +++ b/packages/core/src/config/config.ts @@ -688,7 +688,7 @@ export class Config { enabledExtensionOverrides: this.overrideExtensions, isWorkspaceTrusted: this.isTrustedFolder(), }); - this.enableHooks = params.enableHooks ?? true; + this.enableHooks = params.enableHooks ?? false; this.hooks = params.hooks; }