test: update claude-usage-service tests for improved error handling and timeout management

- Modified command arguments in tests to include '--add-dir' for better context.
- Updated error messages for authentication and timeout scenarios to provide clearer guidance.
- Adjusted timer values in tests to align with implementation delays, ensuring accurate simulation of usage data retrieval.
This commit is contained in:
Shirone
2026-01-13 21:37:16 +01:00
parent ca3b013a7b
commit 6cb2af8757

View File

@@ -551,7 +551,7 @@ Resets in 2h
expect(result.sessionPercentage).toBe(35); expect(result.sessionPercentage).toBe(35);
expect(pty.spawn).toHaveBeenCalledWith( expect(pty.spawn).toHaveBeenCalledWith(
'cmd.exe', 'cmd.exe',
['/c', 'claude', '/usage'], ['/c', 'claude', '--add-dir', 'C:\\Users\\testuser'],
expect.any(Object) expect.any(Object)
); );
}); });
@@ -582,8 +582,8 @@ Resets in 2h
// Simulate seeing usage data // Simulate seeing usage data
dataCallback!(mockOutput); dataCallback!(mockOutput);
// Advance time to trigger escape key sending // Advance time to trigger escape key sending (impl uses 3000ms delay)
vi.advanceTimersByTime(2100); vi.advanceTimersByTime(3100);
expect(mockPty.write).toHaveBeenCalledWith('\x1b'); expect(mockPty.write).toHaveBeenCalledWith('\x1b');
@@ -614,9 +614,10 @@ Resets in 2h
const promise = windowsService.fetchUsageData(); const promise = windowsService.fetchUsageData();
dataCallback!('authentication_error'); dataCallback!('authentication_error');
exitCallback!({ exitCode: 1 });
await expect(promise).rejects.toThrow('Authentication required'); await expect(promise).rejects.toThrow(
"Claude CLI authentication issue. Please run 'claude logout' and then 'claude login' in your terminal to refresh permissions."
);
}); });
it('should handle timeout with no data on Windows', async () => { it('should handle timeout with no data on Windows', async () => {
@@ -628,14 +629,18 @@ Resets in 2h
onExit: vi.fn(), onExit: vi.fn(),
write: vi.fn(), write: vi.fn(),
kill: vi.fn(), kill: vi.fn(),
killed: false,
}; };
vi.mocked(pty.spawn).mockReturnValue(mockPty as any); vi.mocked(pty.spawn).mockReturnValue(mockPty as any);
const promise = windowsService.fetchUsageData(); const promise = windowsService.fetchUsageData();
vi.advanceTimersByTime(31000); // Advance time past timeout (45 seconds)
vi.advanceTimersByTime(46000);
await expect(promise).rejects.toThrow('Command timed out'); await expect(promise).rejects.toThrow(
'The Claude CLI took too long to respond. This can happen if the CLI is waiting for a trust prompt or is otherwise busy.'
);
expect(mockPty.kill).toHaveBeenCalled(); expect(mockPty.kill).toHaveBeenCalled();
vi.useRealTimers(); vi.useRealTimers();
@@ -654,6 +659,7 @@ Resets in 2h
onExit: vi.fn(), onExit: vi.fn(),
write: vi.fn(), write: vi.fn(),
kill: vi.fn(), kill: vi.fn(),
killed: false,
}; };
vi.mocked(pty.spawn).mockReturnValue(mockPty as any); vi.mocked(pty.spawn).mockReturnValue(mockPty as any);
@@ -662,8 +668,8 @@ Resets in 2h
// Simulate receiving usage data // Simulate receiving usage data
dataCallback!('Current session\n65% left\nResets in 2h'); dataCallback!('Current session\n65% left\nResets in 2h');
// Advance time past timeout (30 seconds) // Advance time past timeout (45 seconds)
vi.advanceTimersByTime(31000); vi.advanceTimersByTime(46000);
// Should resolve with data instead of rejecting // Should resolve with data instead of rejecting
const result = await promise; const result = await promise;
@@ -686,6 +692,7 @@ Resets in 2h
onExit: vi.fn(), onExit: vi.fn(),
write: vi.fn(), write: vi.fn(),
kill: vi.fn(), kill: vi.fn(),
killed: false,
}; };
vi.mocked(pty.spawn).mockReturnValue(mockPty as any); vi.mocked(pty.spawn).mockReturnValue(mockPty as any);
@@ -694,8 +701,8 @@ Resets in 2h
// Simulate seeing usage data // Simulate seeing usage data
dataCallback!('Current session\n65% left'); dataCallback!('Current session\n65% left');
// Advance 2s to trigger ESC // Advance 3s to trigger ESC (impl uses 3000ms delay)
vi.advanceTimersByTime(2100); vi.advanceTimersByTime(3100);
expect(mockPty.write).toHaveBeenCalledWith('\x1b'); expect(mockPty.write).toHaveBeenCalledWith('\x1b');
// Advance another 2s to trigger SIGTERM fallback // Advance another 2s to trigger SIGTERM fallback