From ca66e813917d1b38af5ab0c0f709671f05d2c2cf Mon Sep 17 00:00:00 2001 From: keven1024 Date: Thu, 9 Apr 2026 08:01:04 +0800 Subject: [PATCH] test(front): add unit tests for calcFileHash to verify consistency between native and wasm engines, and ensure correct hash format and uniqueness --- front/lib/calcFileHash.test.ts | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 front/lib/calcFileHash.test.ts diff --git a/front/lib/calcFileHash.test.ts b/front/lib/calcFileHash.test.ts new file mode 100644 index 0000000..ac0c8c5 --- /dev/null +++ b/front/lib/calcFileHash.test.ts @@ -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) + }) +})