2024-07-29 14:14:12 +00:00
|
|
|
import { describe, expect, it } from 'vitest'
|
2022-10-18 13:53:43 +00:00
|
|
|
import { Bytes, Hash, Hashes, Hex } from '../dist/index.js'
|
|
|
|
import { Precomputed } from './values.js'
|
|
|
|
|
|
|
|
describe('Hash', () => {
|
|
|
|
for (const type of Object.keys(Hashes)) {
|
|
|
|
describe(type, () => {
|
|
|
|
const values = Precomputed.Hash[type]
|
|
|
|
for (const [input, output] of Object.entries(values)) {
|
2024-07-29 14:14:12 +00:00
|
|
|
if (typeof output !== 'string') throw new Error('Bad test data')
|
|
|
|
|
2022-10-18 13:53:43 +00:00
|
|
|
it(`Should hash "${input}" to "${output.slice(0, 8)}..."`, async () => {
|
|
|
|
const hashed = await Hash.hash(input, Hashes[type])
|
2024-07-29 14:14:12 +00:00
|
|
|
expect(hashed).toEqual(output)
|
2022-10-18 13:53:43 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it(`Should hash "${input}" to "${output.slice(0, 8)}..." as buffer`, async () => {
|
|
|
|
const outputBuffer = Hex.decode(output)
|
|
|
|
const inputBuffer = Bytes.encode(input)
|
|
|
|
const hashed = await Hash.hash(inputBuffer, Hashes[type])
|
2024-07-29 14:14:12 +00:00
|
|
|
expect(hashed).toEqual(outputBuffer)
|
2022-10-18 13:53:43 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|