Skip to content
Source
Sign inSign up
TanStack/db
Open History to see the last commit

hash.test.ts

38 lines1.3 KB

1// Ordinary hash tests: a key-order case with holes and extra keys, and the
2// calibration of the equal-hash assertion. hash-oracle.property.test.ts owns
3// the hash laws and declares the key-order check.
4import { describe, expect, it } from 'vitest'
5import { expectEqualHashes } from './hash-support.js'
6​
7describe(`hash property-based tests`, () => {
8 describe(`property order independence`, () => {
9 it(`hashes an array with holes and extra keys the same in any key order`, () => {
10 // Only a dense array keeps its index order unsorted; this one sorts.
11 const first = [] as unknown as Array<number> & Record<string, number>
12 first.length = 4
13 first[1] = 1
14 first[3] = 3
15 first.x = 1
16 first.y = 2
17 const second = [] as unknown as Array<number> & Record<string, number>
18 second.length = 4
19 second[1] = 1
20 second[3] = 3
21 second.y = 2
22 second.x = 1
23 expectEqualHashes(first, second)
24 })
25 })
26​
27 describe(`law checker calibration`, () => {
28 it.each([0, 42, ``, `reconstructed`])(
29 `rejects inconsistent equal-value hashes for %j`,
30 (value) => {
31 let calls = 0
32 expect(() => expectEqualHashes(value, value, () => ++calls)).toThrow()
33 expect(calls).toBe(2)
34 expectEqualHashes(value, value)
35 },
36 )
37 })
38})