test(front): add unit tests for calcFileHash to verify consistency between native and wasm engines, and ensure correct hash format and uniqueness

This commit is contained in:
keven1024
2026-04-09 08:01:04 +08:00
parent 10a82a1751
commit 2fc2f4ccf7

View File

@@ -0,0 +1,33 @@
import { describe, it, expect } from 'vitest'
import calcFileHash from './calcFileHash'
const makeFile = (content: string) => new File([content], 'test.txt', { type: 'text/plain' })
describe('calcFileHash 引擎一致性', () => {
it('对于小体积内容native 和 wasm 生成相同哈希', async () => {
const file = makeFile('hello world')
const [native, wasm] = await Promise.all([calcFileHash({ file, engine: 'native' }), calcFileHash({ file, engine: 'wasm' })])
expect(native).toBe(wasm)
})
it('对于二进制内容native 和 wasm 生成相同哈希', async () => {
const bytes = new Uint8Array(1024).map((_, i) => i % 256)
const file = new File([bytes], 'bin.bin', { type: 'application/octet-stream' })
const [native, wasm] = await Promise.all([calcFileHash({ file, engine: 'native' }), calcFileHash({ file, engine: 'wasm' })])
expect(native).toBe(wasm)
})
it('哈希值应为 40 位十六进制字符串', async () => {
const file = makeFile('abc')
const hash = await calcFileHash({ file, engine: 'native' })
expect(hash).toMatch(/^[0-9a-f]{40}$/)
})
it('不同内容应生成不同哈希', async () => {
const [h1, h2] = await Promise.all([
calcFileHash({ file: makeFile('foo'), engine: 'native' }),
calcFileHash({ file: makeFile('bar'), engine: 'native' }),
])
expect(h1).not.toBe(h2)
})
})