mirror of
https://github.com/keven1024/015.git
synced 2026-05-26 07:08:02 +00:00
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:
33
front/lib/calcFileHash.test.ts
Normal file
33
front/lib/calcFileHash.test.ts
Normal 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)
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user