jan/web/hooks/useGetSystemResources.test.ts
Faisal Amir daa7c0ca21
feat: better hardware setting (#4471)
* feat: better hardware setting

* chore: update layout

* feat: better hardware setting

* chore: fix title section

* chore: added hardware engine management

* chore: integrate gpus and enable set gpu activate

* chore: update calculate ram and vram

* chore: update calulate vram and ram used

* fix: set active gpus

* chore: fix progress bar spacing

* chore: always update cache vram gpu

* chore: update cpu usage percentage

* chore: fix type usage cpu

* chore: update ram cpus usage getsystemmonitor from new api harware engine management system

* test: update test case data using hardware management extension

* chore: resolve conflict lock json

* chore: cleanup app services

* chore: update type OperationSystemInfo

* chore: update app service

* chore: show list gpus on system monitor

* chore: remove monitoring extension

* chore: update test case app service

* chore: remove unused hooks useGpusSetting

* chore: remove monitor from shource index

* chore: fix test core

* chore: update gpu and cpu info on engine management ext

* chore: fix app service test

* chore: update test appService include cpu info

* chore: filter gpus show or hide on system monitor based activated gpu

* chore: remove unused run_mode

* chore: remove tensort

* chore: update check gpu run_mode

* chore: handle undefined gpus

* chore: cleanup PR

* chore: cleanup process node error

* chore: fix type
2025-02-03 22:01:08 +07:00

104 lines
2.6 KiB
TypeScript

// useGetSystemResources.test.ts
import { renderHook, act } from '@testing-library/react'
import useGetSystemResources from './useGetSystemResources'
import { extensionManager } from '@/extension/ExtensionManager'
// Mock the extensionManager
jest.mock('@/extension/ExtensionManager', () => ({
extensionManager: {
get: jest.fn(),
},
}))
// Mock the necessary dependencies
jest.mock('jotai', () => ({
useAtomValue: jest.fn(),
useSetAtom: () => jest.fn(),
useAtom: jest.fn(),
atom: jest.fn(),
}))
describe('useGetSystemResources', () => {
const mockMonitoringExtension = {
getHardware: jest.fn(),
getCurrentLoad: jest.fn(),
}
beforeEach(() => {
jest.useFakeTimers()
;(extensionManager.get as jest.Mock).mockReturnValue(
mockMonitoringExtension
)
})
afterEach(() => {
jest.clearAllMocks()
jest.useRealTimers()
})
it('should fetch system resources on initial render', async () => {
mockMonitoringExtension.getHardware.mockResolvedValue({
cpu: { usage: 50 },
ram: { available: 4000, total: 8000 },
})
mockMonitoringExtension.getCurrentLoad.mockResolvedValue({
gpu: [],
})
const { result } = renderHook(() => useGetSystemResources())
expect(mockMonitoringExtension.getHardware).toHaveBeenCalledTimes(1)
})
it('should start watching system resources when watch is called', () => {
const { result } = renderHook(() => useGetSystemResources())
act(() => {
result.current.watch()
})
expect(mockMonitoringExtension.getHardware).toHaveBeenCalled()
// Fast-forward time by 2 seconds
act(() => {
jest.advanceTimersByTime(2000)
})
expect(mockMonitoringExtension.getHardware).toHaveBeenCalled()
})
it('should stop watching when stopWatching is called', () => {
const { result } = renderHook(() => useGetSystemResources())
act(() => {
result.current.watch()
})
act(() => {
result.current.stopWatching()
})
// Fast-forward time by 2 seconds
act(() => {
jest.advanceTimersByTime(2000)
})
// Expect no additional calls after stopping
expect(mockMonitoringExtension.getHardware).toHaveBeenCalled()
})
it('should not fetch resources if monitoring extension is not available', async () => {
;(extensionManager.get as jest.Mock).mockReturnValue(null)
const { result } = renderHook(() => useGetSystemResources())
await act(async () => {
result.current.getSystemResources()
})
expect(mockMonitoringExtension.getHardware).not.toHaveBeenCalled()
expect(mockMonitoringExtension.getCurrentLoad).not.toHaveBeenCalled()
})
})