Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions src/core/target-handlers/acp-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,12 @@ export class ACPTargetHandler implements TargetHandler {
try {
const msg = JSON.parse(line) as ACPMessage;
this.handleMessage(msg);
} catch {
// Ignore parse errors (partial messages)
} catch (err) {
// Log parse errors in debug mode
if (process.env['DEBUG'] || process.env['WORK_DEBUG']) {
console.warn('ACP message parse error:', err instanceof Error ? err.message : String(err));
console.warn('Problematic line:', line.substring(0, 200));
}
}
}
}
Expand Down
105 changes: 105 additions & 0 deletions tests/unit/core/target-handlers/acp-handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,17 @@ describe('ACPTargetHandler', () => {
let mockProcess: any;
const mockSpawn = spawn as any;

let savedDebug: string | undefined;
let savedWorkDebug: string | undefined;

beforeEach(() => {
vi.clearAllMocks();
vi.useFakeTimers();

// Save environment variables for restoration
savedDebug = process.env['DEBUG'];
savedWorkDebug = process.env['WORK_DEBUG'];

// Create a more realistic mock process
mockProcess = {
stdin: {
Expand Down Expand Up @@ -70,6 +77,18 @@ describe('ACPTargetHandler', () => {

afterEach(() => {
vi.useRealTimers();

// Restore environment variables
if (savedDebug !== undefined) {
process.env['DEBUG'] = savedDebug;
} else {
delete process.env['DEBUG'];
}
if (savedWorkDebug !== undefined) {
process.env['WORK_DEBUG'] = savedWorkDebug;
} else {
delete process.env['WORK_DEBUG'];
}
});

describe('formatWorkItems', () => {
Expand Down Expand Up @@ -384,6 +403,92 @@ describe('ACPTargetHandler', () => {
// Should not throw
expect(true).toBe(true);
});

it('should not log parse errors when DEBUG is not set', () => {
const consoleWarnSpy = vi.spyOn(console, 'warn');
delete process.env['DEBUG'];
delete process.env['WORK_DEBUG'];

(handler as any).ensureProcess(mockConfig);

// Send invalid JSON
mockProcess.stdout.emit('data', 'invalid json\n');

// Should not log anything
expect(consoleWarnSpy).not.toHaveBeenCalled();

consoleWarnSpy.mockRestore();
});

it('should log parse errors when DEBUG is set', () => {
const consoleWarnSpy = vi.spyOn(console, 'warn');
process.env['DEBUG'] = '1';

(handler as any).ensureProcess(mockConfig);

// Send invalid JSON
mockProcess.stdout.emit('data', 'invalid json\n');

// Should log error message first, then problematic line
expect(consoleWarnSpy).toHaveBeenCalledTimes(2);
expect(consoleWarnSpy).toHaveBeenNthCalledWith(
1,
'ACP message parse error:',
expect.stringContaining('JSON')
);
expect(consoleWarnSpy).toHaveBeenNthCalledWith(
2,
'Problematic line:',
'invalid json'
);

consoleWarnSpy.mockRestore();
});

it('should log parse errors when WORK_DEBUG is set', () => {
const consoleWarnSpy = vi.spyOn(console, 'warn');
process.env['WORK_DEBUG'] = '1';

(handler as any).ensureProcess(mockConfig);

// Send invalid JSON
mockProcess.stdout.emit('data', 'invalid json\n');

// Should log error message first, then problematic line
expect(consoleWarnSpy).toHaveBeenCalledTimes(2);
expect(consoleWarnSpy).toHaveBeenNthCalledWith(
1,
'ACP message parse error:',
expect.stringContaining('JSON')
);
expect(consoleWarnSpy).toHaveBeenNthCalledWith(
2,
'Problematic line:',
'invalid json'
);

consoleWarnSpy.mockRestore();
});

it('should truncate long problematic lines to 200 characters', () => {
const consoleWarnSpy = vi.spyOn(console, 'warn');
process.env['DEBUG'] = '1';

(handler as any).ensureProcess(mockConfig);

// Send invalid JSON that's longer than 200 characters
const longInvalidJson = 'x'.repeat(300) + '\n';
mockProcess.stdout.emit('data', longInvalidJson);

// Should log truncated line (200 chars max)
expect(consoleWarnSpy).toHaveBeenNthCalledWith(
2,
'Problematic line:',
'x'.repeat(200)
);

consoleWarnSpy.mockRestore();
});
});

describe('cleanup', () => {
Expand Down