-
Notifications
You must be signed in to change notification settings - Fork 0
/
useGpuSetting.test.ts
87 lines (68 loc) · 2.53 KB
/
useGpuSetting.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// useGpuSetting.test.ts
import { renderHook, act } from '@testing-library/react'
import { ExtensionTypeEnum, MonitoringExtension } from '@janhq/core'
// Mock dependencies
jest.mock('@/extension')
import useGpuSetting from './useGpuSetting'
import { extensionManager } from '@/extension'
describe('useGpuSetting', () => {
beforeEach(() => {
jest.clearAllMocks()
})
it('should return GPU settings when available', async () => {
const mockGpuSettings = {
gpuCount: 2,
gpuNames: ['NVIDIA GeForce RTX 3080', 'NVIDIA GeForce RTX 3070'],
totalMemory: 20000,
freeMemory: 15000,
}
const mockMonitoringExtension: Partial<MonitoringExtension> = {
getGpuSetting: jest.fn().mockResolvedValue(mockGpuSettings),
}
jest
.spyOn(extensionManager, 'get')
.mockReturnValue(mockMonitoringExtension as MonitoringExtension)
const { result } = renderHook(() => useGpuSetting())
let gpuSettings
await act(async () => {
gpuSettings = await result.current.getGpuSettings()
})
expect(gpuSettings).toEqual(mockGpuSettings)
expect(extensionManager.get).toHaveBeenCalledWith(
ExtensionTypeEnum.SystemMonitoring
)
expect(mockMonitoringExtension.getGpuSetting).toHaveBeenCalled()
})
it('should return undefined when no GPU settings are found', async () => {
const mockMonitoringExtension: Partial<MonitoringExtension> = {
getGpuSetting: jest.fn().mockResolvedValue(undefined),
}
jest
.spyOn(extensionManager, 'get')
.mockReturnValue(mockMonitoringExtension as MonitoringExtension)
const { result } = renderHook(() => useGpuSetting())
let gpuSettings
await act(async () => {
gpuSettings = await result.current.getGpuSettings()
})
expect(gpuSettings).toBeUndefined()
expect(extensionManager.get).toHaveBeenCalledWith(
ExtensionTypeEnum.SystemMonitoring
)
expect(mockMonitoringExtension.getGpuSetting).toHaveBeenCalled()
})
it('should handle missing MonitoringExtension', async () => {
jest.spyOn(extensionManager, 'get').mockReturnValue(undefined)
jest.spyOn(console, 'debug').mockImplementation(() => {})
const { result } = renderHook(() => useGpuSetting())
let gpuSettings
await act(async () => {
gpuSettings = await result.current.getGpuSettings()
})
expect(gpuSettings).toBeUndefined()
expect(extensionManager.get).toHaveBeenCalledWith(
ExtensionTypeEnum.SystemMonitoring
)
expect(console.debug).toHaveBeenCalledWith('No GPU setting found')
})
})