@posthog/wizard
Version:
The PostHog wizard helps you to configure your project
137 lines • 5.74 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const agent_interface_1 = require("../agent-interface");
// Mock dependencies
jest.mock('../../utils/clack');
jest.mock('../../utils/analytics');
jest.mock('../../utils/debug');
// Mock the SDK module
const mockQuery = jest.fn();
jest.mock('@anthropic-ai/claude-agent-sdk', () => ({
query: (...args) => mockQuery(...args),
}));
// Get mocked clack for spinner
const clack_1 = __importDefault(require("../../utils/clack"));
const mockClack = clack_1.default;
describe('runAgent', () => {
let mockSpinner;
const defaultOptions = {
debug: false,
installDir: '/test/dir',
forceInstall: false,
default: false,
signup: false,
localMcp: false,
ci: false,
};
const defaultAgentConfig = {
workingDirectory: '/test/dir',
mcpServers: {},
model: 'claude-opus-4-5-20251101',
};
beforeEach(() => {
jest.clearAllMocks();
mockSpinner = {
start: jest.fn(),
stop: jest.fn(),
message: '',
};
mockClack.spinner = jest.fn().mockReturnValue(mockSpinner);
mockClack.log = {
step: jest.fn(),
success: jest.fn(),
error: jest.fn(),
warn: jest.fn(),
warning: jest.fn(),
info: jest.fn(),
message: jest.fn(),
};
});
describe('race condition handling', () => {
it('should return success when agent completes successfully then SDK cleanup fails', async () => {
// This simulates the race condition:
// 1. Agent completes with success result
// 2. signalDone() is called, completing the prompt generator
// 3. SDK tries to send cleanup command while streaming is active
// 4. SDK throws an error
// The fix should recognize we already got a success and return success anyway
function* mockGeneratorWithCleanupError() {
yield {
type: 'system',
subtype: 'init',
model: 'claude-opus-4-5-20251101',
tools: [],
mcp_servers: [],
};
yield {
type: 'result',
subtype: 'success',
is_error: false,
result: 'Agent completed successfully',
};
// Simulate the SDK cleanup error that occurs after success
throw new Error('only prompt commands are supported in streaming mode');
}
mockQuery.mockReturnValue(mockGeneratorWithCleanupError());
const result = await (0, agent_interface_1.runAgent)(defaultAgentConfig, 'test prompt', defaultOptions, mockSpinner, {
successMessage: 'Test success',
errorMessage: 'Test error',
});
// Should return success (empty object), not throw
expect(result).toEqual({});
expect(mockSpinner.stop).toHaveBeenCalledWith('Test success');
});
it('should still throw when no success result was received before error', async () => {
// If we never got a success result, errors should propagate normally
function* mockGeneratorWithOnlyError() {
yield {
type: 'system',
subtype: 'init',
model: 'claude-opus-4-5-20251101',
tools: [],
mcp_servers: [],
};
// No success result, just an error
throw new Error('Actual SDK error');
}
mockQuery.mockReturnValue(mockGeneratorWithOnlyError());
await expect((0, agent_interface_1.runAgent)(defaultAgentConfig, 'test prompt', defaultOptions, mockSpinner, {
successMessage: 'Test success',
errorMessage: 'Test error',
})).rejects.toThrow('Actual SDK error');
expect(mockSpinner.stop).toHaveBeenCalledWith('Test error');
});
it('should not treat error results as success', async () => {
// A result with is_error: true should not count as success
// Even if subtype is 'success', the is_error flag takes precedence
function* mockGeneratorWithErrorResult() {
yield {
type: 'system',
subtype: 'init',
model: 'claude-opus-4-5-20251101',
tools: [],
mcp_servers: [],
};
yield {
type: 'result',
subtype: 'success', // subtype can be success but is_error true
is_error: true,
result: 'API Error: 500 Internal Server Error',
};
throw new Error('Process exited with code 1');
}
mockQuery.mockReturnValue(mockGeneratorWithErrorResult());
const result = await (0, agent_interface_1.runAgent)(defaultAgentConfig, 'test prompt', defaultOptions, mockSpinner, {
successMessage: 'Test success',
errorMessage: 'Test error',
});
// Should return API error, not success
expect(result.error).toBe('WIZARD_API_ERROR');
expect(result.message).toContain('API Error');
});
});
});
//# sourceMappingURL=agent-interface.test.js.map