test: fix test

This commit is contained in:
Louis 2025-08-07 20:09:07 +07:00
parent c1668a4e4a
commit ab44faeda3
No known key found for this signature in database
GPG Key ID: 44FA9F4D33C37DE2

View File

@ -1,14 +1,6 @@
import { describe, it, expect, vi, beforeEach } from 'vitest'
import { renderHook, act } from '@testing-library/react'
import {
useHardware,
HardwareData,
SystemUsage,
CPU,
GPU,
OS,
RAM,
} from '../useHardware'
import { useHardware, HardwareData, OS, RAM } from '../useHardware'
// Mock dependencies
vi.mock('@/constants/localStorage', () => ({
@ -43,7 +35,6 @@ describe('useHardware', () => {
name: '',
usage: 0,
},
gpus: [],
os_type: '',
os_name: '',
total_memory: 0,
@ -52,9 +43,7 @@ describe('useHardware', () => {
cpu: 0,
used_memory: 0,
total_memory: 0,
gpus: [],
})
expect(result.current.gpuLoading).toEqual({})
expect(result.current.pollingPaused).toBe(false)
})
@ -74,26 +63,6 @@ describe('useHardware', () => {
available: 0,
total: 0,
},
gpus: [
{
name: 'NVIDIA RTX 3080',
total_memory: 10737418240,
vendor: 'NVIDIA',
uuid: 'GPU-12345',
driver_version: '470.57.02',
activated: true,
nvidia_info: {
index: 0,
compute_capability: '8.6',
},
vulkan_info: {
index: 0,
device_id: 8704,
device_type: 'discrete',
api_version: '1.2.0',
},
},
],
os_type: 'linux',
os_name: 'Ubuntu',
total_memory: 17179869184,
@ -124,37 +93,6 @@ describe('useHardware', () => {
expect(result.current.hardwareData.cpu).toEqual(testCPU)
})
it('should set GPUs data', () => {
const { result } = renderHook(() => useHardware())
const testGPUs = [
{
name: 'NVIDIA RTX 3080',
total_memory: 10737418240,
vendor: 'NVIDIA',
uuid: 'GPU-12345',
driver_version: '470.57.02',
activated: true,
nvidia_info: {
index: 0,
compute_capability: '8.6',
},
vulkan_info: {
index: 0,
device_id: 8704,
device_type: 'discrete',
api_version: '1.2.0',
},
},
]
act(() => {
result.current.setGPUs(testGPUs)
})
expect(result.current.hardwareData.gpus).toEqual(testGPUs)
})
it('should update system usage', () => {
const { result } = renderHook(() => useHardware())
@ -162,13 +100,6 @@ describe('useHardware', () => {
cpu: 45.2,
used_memory: 8589934592,
total_memory: 17179869184,
gpus: [
{
uuid: 'GPU-12345',
used_memory: 2147483648,
total_memory: 10737418240,
},
],
}
act(() => {
@ -178,48 +109,6 @@ describe('useHardware', () => {
expect(result.current.systemUsage).toEqual(testSystemUsage)
})
it('should manage GPU loading state', () => {
const { result } = renderHook(() => useHardware())
// First set up some GPU data so we have a UUID to work with
const testGPUs = [
{
name: 'NVIDIA RTX 3080',
total_memory: 10737418240,
vendor: 'NVIDIA',
uuid: 'GPU-12345',
driver_version: '470.57.02',
activated: true,
nvidia_info: {
index: 0,
compute_capability: '8.6',
},
vulkan_info: {
index: 0,
device_id: 8704,
device_type: 'discrete',
api_version: '1.2.0',
},
},
]
act(() => {
result.current.setGPUs(testGPUs)
})
act(() => {
result.current.setGpuLoading(0, true)
})
expect(result.current.gpuLoading['GPU-12345']).toBe(true)
act(() => {
result.current.setGpuLoading(0, false)
})
expect(result.current.gpuLoading['GPU-12345']).toBe(false)
})
it('should manage polling state', () => {
const { result } = renderHook(() => useHardware())
@ -271,179 +160,4 @@ describe('useHardware', () => {
expect(result.current.hardwareData.ram).toEqual(ram)
})
})
describe('updateGPU', () => {
it('should update specific GPU at index', () => {
const { result } = renderHook(() => useHardware())
const initialGpus: GPU[] = [
{
name: 'GPU 1',
total_memory: 8192,
vendor: 'NVIDIA',
uuid: 'gpu-1',
driver_version: '1.0',
activated: false,
nvidia_info: { index: 0, compute_capability: '8.0' },
vulkan_info: {
index: 0,
device_id: 1,
device_type: 'discrete',
api_version: '1.0',
},
},
{
name: 'GPU 2',
total_memory: 4096,
vendor: 'AMD',
uuid: 'gpu-2',
driver_version: '2.0',
activated: false,
nvidia_info: { index: 1, compute_capability: '7.0' },
vulkan_info: {
index: 1,
device_id: 2,
device_type: 'discrete',
api_version: '1.0',
},
},
]
act(() => {
result.current.setGPUs(initialGpus)
})
const updatedGpu: GPU = {
...initialGpus[0],
name: 'Updated GPU 1',
activated: true,
}
act(() => {
result.current.updateGPU(0, updatedGpu)
})
expect(result.current.hardwareData.gpus[0].name).toBe('Updated GPU 1')
expect(result.current.hardwareData.gpus[0].activated).toBe(true)
expect(result.current.hardwareData.gpus[1]).toEqual(initialGpus[1])
})
it('should handle invalid index gracefully', () => {
const { result } = renderHook(() => useHardware())
const initialGpus: GPU[] = [
{
name: 'GPU 1',
total_memory: 8192,
vendor: 'NVIDIA',
uuid: 'gpu-1',
driver_version: '1.0',
activated: false,
nvidia_info: { index: 0, compute_capability: '8.0' },
vulkan_info: {
index: 0,
device_id: 1,
device_type: 'discrete',
api_version: '1.0',
},
},
]
act(() => {
result.current.setGPUs(initialGpus)
})
const updatedGpu: GPU = {
...initialGpus[0],
name: 'Updated GPU',
}
act(() => {
result.current.updateGPU(5, updatedGpu)
})
expect(result.current.hardwareData.gpus[0]).toEqual(initialGpus[0])
})
})
describe('setHardwareData with GPU activation', () => {
it('should initialize GPUs as inactive when activated is not specified', () => {
const { result } = renderHook(() => useHardware())
const hardwareData: HardwareData = {
cpu: {
arch: 'x86_64',
core_count: 4,
extensions: [],
name: 'CPU',
usage: 0,
},
gpus: [
{
name: 'GPU 1',
total_memory: 8192,
vendor: 'NVIDIA',
uuid: 'gpu-1',
driver_version: '1.0',
nvidia_info: { index: 0, compute_capability: '8.0' },
vulkan_info: {
index: 0,
device_id: 1,
device_type: 'discrete',
api_version: '1.0',
},
},
],
os_type: 'windows',
os_name: 'Windows 11',
total_memory: 16384,
}
act(() => {
result.current.setHardwareData(hardwareData)
})
expect(result.current.hardwareData.gpus[0].activated).toBe(false)
})
it('should preserve existing activation states when set', () => {
const { result } = renderHook(() => useHardware())
const hardwareData: HardwareData = {
cpu: {
arch: 'x86_64',
core_count: 4,
extensions: [],
name: 'CPU',
usage: 0,
},
gpus: [
{
name: 'GPU 1',
total_memory: 8192,
vendor: 'NVIDIA',
uuid: 'gpu-1',
driver_version: '1.0',
activated: true,
nvidia_info: { index: 0, compute_capability: '8.0' },
vulkan_info: {
index: 0,
device_id: 1,
device_type: 'discrete',
api_version: '1.0',
},
},
],
os_type: 'windows',
os_name: 'Windows 11',
total_memory: 16384,
}
act(() => {
result.current.setHardwareData(hardwareData)
})
expect(result.current.hardwareData.gpus[0].activated).toBe(true)
})
})
})