forked from imbytecat/fullstack-starter
- 添加类型检查和代码修复脚本以提升开发效率和代码质量。 - 添加硬件指纹生成函数的完整测试用例,覆盖正常情况、边界条件和mock数据验证。 - 添加通用工具包的 TypeScript 配置并继承基础配置文件
158 lines
4.4 KiB
TypeScript
158 lines
4.4 KiB
TypeScript
import { beforeEach, describe, expect, it, mock } from 'bun:test'
|
|
import { getHardwareFingerprint } from './fingerprint'
|
|
|
|
// Mock systeminformation module
|
|
const mockSystemInfo = {
|
|
uuid: mock(() =>
|
|
Promise.resolve({
|
|
os: 'test-os-uuid',
|
|
hardware: 'test-hardware-uuid',
|
|
}),
|
|
),
|
|
baseboard: mock(() =>
|
|
Promise.resolve({
|
|
manufacturer: 'Test Manufacturer',
|
|
model: 'Test Model',
|
|
version: '1.0',
|
|
serial: 'TEST123',
|
|
}),
|
|
),
|
|
bios: mock(() =>
|
|
Promise.resolve({
|
|
vendor: 'Test Vendor',
|
|
version: '1.0.0',
|
|
releaseDate: '2024-01-01',
|
|
}),
|
|
),
|
|
system: mock(() =>
|
|
Promise.resolve({
|
|
manufacturer: 'Test System',
|
|
model: 'Test Model',
|
|
version: '1.0',
|
|
sku: 'TEST-SKU',
|
|
}),
|
|
),
|
|
diskLayout: mock(() =>
|
|
Promise.resolve([
|
|
{
|
|
device: '/dev/sda',
|
|
type: 'SSD',
|
|
name: 'Test Disk',
|
|
size: 512000000000,
|
|
},
|
|
]),
|
|
),
|
|
networkInterfaces: mock(() =>
|
|
Promise.resolve([
|
|
{
|
|
iface: 'eth0',
|
|
mac: '00:11:22:33:44:55',
|
|
ip4: '192.168.1.1',
|
|
},
|
|
]),
|
|
),
|
|
}
|
|
|
|
mock.module('systeminformation', () => ({
|
|
default: mockSystemInfo,
|
|
}))
|
|
|
|
describe('fingerprint', () => {
|
|
beforeEach(() => {
|
|
// Reset all mocks before each test
|
|
mockSystemInfo.uuid.mockClear()
|
|
mockSystemInfo.baseboard.mockClear()
|
|
mockSystemInfo.bios.mockClear()
|
|
mockSystemInfo.system.mockClear()
|
|
mockSystemInfo.diskLayout.mockClear()
|
|
mockSystemInfo.networkInterfaces.mockClear()
|
|
})
|
|
|
|
describe('getHardwareFingerprint', () => {
|
|
it('should return a fingerprint hash', async () => {
|
|
const fingerprint = await getHardwareFingerprint()
|
|
|
|
expect(fingerprint).toBeDefined()
|
|
expect(typeof fingerprint).toBe('string')
|
|
expect(fingerprint.length).toBeGreaterThan(0)
|
|
})
|
|
|
|
it('should call all system information methods', async () => {
|
|
await getHardwareFingerprint()
|
|
|
|
expect(mockSystemInfo.uuid).toHaveBeenCalledTimes(1)
|
|
expect(mockSystemInfo.baseboard).toHaveBeenCalledTimes(1)
|
|
expect(mockSystemInfo.bios).toHaveBeenCalledTimes(1)
|
|
expect(mockSystemInfo.system).toHaveBeenCalledTimes(1)
|
|
expect(mockSystemInfo.diskLayout).toHaveBeenCalledTimes(1)
|
|
expect(mockSystemInfo.networkInterfaces).toHaveBeenCalledTimes(1)
|
|
})
|
|
|
|
it('should return the same fingerprint for the same system info', async () => {
|
|
const fingerprint1 = await getHardwareFingerprint()
|
|
const fingerprint2 = await getHardwareFingerprint()
|
|
|
|
expect(fingerprint1).toBe(fingerprint2)
|
|
})
|
|
|
|
it('should return different fingerprint when system info changes', async () => {
|
|
const fingerprint1 = await getHardwareFingerprint()
|
|
|
|
// Change mock data
|
|
mockSystemInfo.uuid.mockImplementationOnce(() =>
|
|
Promise.resolve({
|
|
os: 'different-os-uuid',
|
|
hardware: 'different-hardware-uuid',
|
|
}),
|
|
)
|
|
|
|
const fingerprint2 = await getHardwareFingerprint()
|
|
|
|
expect(fingerprint1).not.toBe(fingerprint2)
|
|
})
|
|
|
|
it('should handle empty system information gracefully', async () => {
|
|
// Mock empty responses
|
|
mockSystemInfo.uuid.mockImplementationOnce(() => Promise.resolve({}))
|
|
mockSystemInfo.baseboard.mockImplementationOnce(() => Promise.resolve({}))
|
|
mockSystemInfo.bios.mockImplementationOnce(() => Promise.resolve({}))
|
|
mockSystemInfo.system.mockImplementationOnce(() => Promise.resolve({}))
|
|
mockSystemInfo.diskLayout.mockImplementationOnce(() =>
|
|
Promise.resolve([]),
|
|
)
|
|
mockSystemInfo.networkInterfaces.mockImplementationOnce(() =>
|
|
Promise.resolve([]),
|
|
)
|
|
|
|
const fingerprint = await getHardwareFingerprint()
|
|
|
|
expect(fingerprint).toBeDefined()
|
|
expect(typeof fingerprint).toBe('string')
|
|
})
|
|
|
|
it('should handle partial system information', async () => {
|
|
mockSystemInfo.baseboard.mockImplementationOnce(() =>
|
|
Promise.resolve({
|
|
manufacturer: 'Only Manufacturer',
|
|
}),
|
|
)
|
|
|
|
const fingerprint = await getHardwareFingerprint()
|
|
|
|
expect(fingerprint).toBeDefined()
|
|
expect(typeof fingerprint).toBe('string')
|
|
})
|
|
|
|
it('should be deterministic with the same input', async () => {
|
|
const results = await Promise.all([
|
|
getHardwareFingerprint(),
|
|
getHardwareFingerprint(),
|
|
getHardwareFingerprint(),
|
|
])
|
|
|
|
expect(results[0]).toBe(results[1])
|
|
expect(results[1]).toBe(results[2])
|
|
})
|
|
})
|
|
})
|