mirror of
https://github.com/AutoMaker-Org/automaker.git
synced 2026-01-31 06:42:03 +00:00
test: enhance ClaudeUsageService tests with promise handling and exit callback
- Added an `afterEach` hook to clean up after tests in `claude-usage-service.test.ts`. - Updated the mock for `onExit` to include an exit callback, ensuring proper handling of process termination. - Modified the `fetchUsageData` test to await the promise resolution, preventing unhandled promise rejections. These changes improve the reliability and robustness of the unit tests for the ClaudeUsageService.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
||||
import { ClaudeUsageService } from '@/services/claude-usage-service.js';
|
||||
import { spawn } from 'child_process';
|
||||
import * as pty from 'node-pty';
|
||||
@@ -563,18 +563,21 @@ Resets in 2h
|
||||
const mockOutput = 'Current session\n65% left';
|
||||
|
||||
let dataCallback: Function | undefined;
|
||||
let exitCallback: Function | undefined;
|
||||
|
||||
const mockPty = {
|
||||
onData: vi.fn((callback: Function) => {
|
||||
dataCallback = callback;
|
||||
}),
|
||||
onExit: vi.fn(),
|
||||
onExit: vi.fn((callback: Function) => {
|
||||
exitCallback = callback;
|
||||
}),
|
||||
write: vi.fn(),
|
||||
kill: vi.fn(),
|
||||
};
|
||||
vi.mocked(pty.spawn).mockReturnValue(mockPty as any);
|
||||
|
||||
windowsService.fetchUsageData();
|
||||
const promise = windowsService.fetchUsageData();
|
||||
|
||||
// Simulate seeing usage data
|
||||
dataCallback!(mockOutput);
|
||||
@@ -584,6 +587,10 @@ Resets in 2h
|
||||
|
||||
expect(mockPty.write).toHaveBeenCalledWith('\x1b');
|
||||
|
||||
// Complete the promise to avoid unhandled rejection
|
||||
exitCallback!({ exitCode: 0 });
|
||||
await promise;
|
||||
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { useEffect } from 'react';
|
||||
import { useEffect, useRef } from 'react';
|
||||
|
||||
interface UseSidebarAutoCollapseProps {
|
||||
sidebarOpen: boolean;
|
||||
@@ -9,6 +9,8 @@ export function useSidebarAutoCollapse({
|
||||
sidebarOpen,
|
||||
toggleSidebar,
|
||||
}: UseSidebarAutoCollapseProps) {
|
||||
const isMountedRef = useRef(false);
|
||||
|
||||
// Auto-collapse sidebar on small screens
|
||||
useEffect(() => {
|
||||
const mediaQuery = window.matchMedia('(max-width: 1024px)'); // lg breakpoint
|
||||
@@ -20,8 +22,11 @@ export function useSidebarAutoCollapse({
|
||||
}
|
||||
};
|
||||
|
||||
// Check on mount
|
||||
handleResize();
|
||||
// Check on mount only
|
||||
if (!isMountedRef.current) {
|
||||
isMountedRef.current = true;
|
||||
handleResize();
|
||||
}
|
||||
|
||||
// Listen for changes
|
||||
mediaQuery.addEventListener('change', handleResize);
|
||||
|
||||
Reference in New Issue
Block a user