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

utils.test.ts

442 lines13.8 KB

1import { describe, expect, it } from "vitest"
2import { DefaultMap } from "../src/utils.js"
3import { hash } from "../src/hashing/index.js"
4​
5describe(`DefaultMap`, () => {
6 it(`should return default value for missing keys`, () => {
7 const map = new DefaultMap(() => 0)
8 expect(map.get(`missing`)).toBe(0)
9 })
10​
11 it(`should store and retrieve values`, () => {
12 const map = new DefaultMap(() => 0)
13 map.set(`key`, 42)
14 expect(map.get(`key`)).toBe(42)
15 })
16​
17 it(`should accept initial entries`, () => {
18 const map = new DefaultMap(() => 0, [[`key`, 1]])
19 expect(map.get(`key`)).toBe(1)
20 })
21​
22 it(`should update values using the update method`, () => {
23 const map = new DefaultMap(() => 0)
24 map.update(`key`, (value) => value + 1)
25 expect(map.get(`key`)).toBe(1)
26​
27 map.update(`key`, (value) => value * 2)
28 expect(map.get(`key`)).toBe(2)
29 })
30})
31​
32const hashType = `number`
33describe(`hash`, () => {
34 describe(`primitive types`, () => {
35 it(`should hash null`, () => {
36 const result = hash(null)
37 expect(typeof result).toBe(hashType)
38 })
39​
40 it(`should hash undefined`, () => {
41 const result = hash(undefined)
42 expect(typeof result).toBe(hashType)
43 })
44​
45 it(`should hash strings`, () => {
46 const result1 = hash(`hello`)
47 const result2 = hash(``)
48 const result3 = hash(`test with spaces`)
49 const result4 = hash(`special\nchars\t"`)
50​
51 expect(typeof result1).toBe(hashType)
52 expect(typeof result2).toBe(hashType)
53 expect(typeof result3).toBe(hashType)
54 expect(typeof result4).toBe(hashType)
55​
56 // Same strings should have same hash
57 expect(hash(`hello`)).toBe(result1)
58 })
59​
60 it(`should hash numbers`, () => {
61 const result1 = hash(42)
62 const result2 = hash(0)
63 const result3 = hash(-1)
64 const result4 = hash(3.14159)
65 const result5 = hash(Infinity)
66 const result6 = hash(-Infinity)
67 const result7 = hash(NaN)
68​
69 expect(typeof result1).toBe(hashType)
70 expect(typeof result2).toBe(hashType)
71 expect(typeof result3).toBe(hashType)
72 expect(typeof result4).toBe(hashType)
73 expect(typeof result5).toBe(hashType)
74 expect(typeof result6).toBe(hashType)
75 expect(typeof result7).toBe(hashType)
76​
77 // Same numbers should have same hash
78 expect(hash(42)).toBe(result1)
79 expect(hash(2.0)).not.toBe(hash(2.5))
80 expect(hash(3.14159)).toBe(result4)
81 })
82​
83 it(`should hash booleans`, () => {
84 const result1 = hash(true)
85 const result2 = hash(false)
86​
87 expect(typeof result1).toBe(hashType)
88 expect(typeof result2).toBe(hashType)
89 expect(result1).not.toBe(result2)
90​
91 // Same booleans should have same hash
92 expect(hash(true)).toBe(result1)
93 expect(hash(false)).toBe(result2)
94 })
95​
96 it(`should hash bigint`, () => {
97 const result1 = hash(123n)
98 const result2 = hash(456n)
99 const result3 = hash(123n)
100​
101 expect(typeof result1).toBe(hashType)
102 expect(typeof result2).toBe(hashType)
103 expect(typeof result3).toBe(hashType)
104 expect(result1).toBe(result3) // Same bigint should have same hash
105 expect(result1).not.toBe(result2) // Different bigints should have different hash
106 })
107​
108 it(`should hash symbols`, () => {
109 const sym1 = Symbol(`test`)
110 const sym2 = Symbol(`test`)
111 const sym3 = Symbol(`different`)
112 const sym4 = Symbol()
113 const sym5 = Symbol()
114​
115 const result1 = hash(sym1)
116 const result2 = hash(sym2)
117 const result3 = hash(sym3)
118 const result4 = hash(sym4)
119 const result5 = hash(sym5)
120​
121 expect(typeof result1).toBe(hashType)
122 expect(typeof result2).toBe(hashType)
123 expect(typeof result3).toBe(hashType)
124 // Note: Different symbol instances with same description have same hash
125 expect(result1).toBe(result2)
126 expect(result1).not.toBe(result3)
127 expect(result4).toBe(result5)
128 expect(result1).not.toBe(result4)
129 })
130 })
131​
132 describe(`object types`, () => {
133 it(`should hash plain objects`, () => {
134 const obj1 = { a: 1, b: 2 }
135 const obj2 = { b: 2, a: 1 } // Different key order
136​
137 const hash1 = hash(obj1)
138 const hash2 = hash(obj2)
139​
140 expect(typeof hash1).toBe(hashType)
141 expect(typeof hash2).toBe(hashType)
142 // Note: Different key orders might produce different hashes depending on JSON.stringify behavior
143 })
144​
145 it(`should hash arrays`, () => {
146 const arr1 = [1, 2, 3]
147 const arr2 = [1, 2, 3]
148 const arr3 = [3, 2, 1]
149​
150 const hash1 = hash(arr1)
151 const hash2 = hash(arr2)
152 const hash3 = hash(arr3)
153​
154 expect(typeof hash1).toBe(hashType)
155 expect(hash1).toBe(hash2) // Same content should have same hash
156 expect(hash1).not.toBe(hash3) // Different content should have different hash
157 })
158​
159 it(`should hash Date objects`, () => {
160 const date1 = new Date(`2023-01-01`)
161 const date2 = new Date(`2023-01-01`)
162 const date3 = new Date(`2023-01-02`)
163​
164 const hash1 = hash(date1)
165 const hash2 = hash(date2)
166 const hash3 = hash(date3)
167​
168 expect(typeof hash1).toBe(hashType)
169 expect(hash1).toBe(hash2) // Same date should have same hash
170 expect(hash1).not.toBe(hash3) // Different dates should have different hash
171 })
172​
173 it(`should hash RegExp objects`, () => {
174 const regex1 = /test/g
175 const regex2 = /test/g
176 const regex3 = /different/i
177​
178 const hash1 = hash(regex1)
179 const hash2 = hash(regex2)
180 const hash3 = hash(regex3)
181​
182 expect(typeof hash1).toBe(hashType)
183 expect(hash1).toBe(hash2) // Same regex should have same hash
184 // Note: RegExp don't have enumerable properties so they all produce the same hash
185 expect(hash1).toBe(hash3) // All RegExp objects have the same hash
186 })
187​
188 it(`should hash nested objects`, () => {
189 const nested1 = { a: { b: { c: 1 } } }
190 const nested2 = { a: { b: { c: 1 } } }
191 const nested3 = { a: { b: { c: 2 } } }
192​
193 const hash1 = hash(nested1)
194 const hash2 = hash(nested2)
195 const hash3 = hash(nested3)
196​
197 expect(typeof hash1).toBe(hashType)
198 expect(hash1).toBe(hash2)
199 expect(hash1).not.toBe(hash3)
200 })
201​
202 it(`should hash functions`, () => {
203 const func1 = function test() {
204 return 1
205 }
206 const func2 = function test() {
207 return 1
208 }
209 const func3 = function different() {
210 return 2
211 }
212​
213 const hash1 = hash(func1)
214 const hash2 = hash(func2)
215 const hash3 = hash(func3)
216​
217 expect(typeof hash1).toBe(hashType)
218 expect(typeof hash2).toBe(hashType)
219 expect(typeof hash3).toBe(hashType)
220 expect(hash1).not.toBe(hash2) // Different function should have different hash
221 expect(hash1).not.toBe(hash3) // Different function should have different hash
222 expect(hash1).toBe(hash(func1)) // hashing same function should return same hash
223 })
224​
225 it(`should hash Set objects`, () => {
226 const set1 = new Set([1, 2, 3])
227 const set2 = new Set([1, 2, 3])
228 const set3 = new Set([1, 2, 3, 4])
229​
230 const hash1 = hash(set1)
231 const hash2 = hash(set2)
232 const hash3 = hash(set3)
233​
234 expect(typeof hash1).toBe(hashType)
235 expect(hash1).toBe(hash2) // Same content should have same hash
236 expect(hash1).not.toBe(hash3) // Different content should have different hash
237 })
238​
239 it(`should hash Map objects`, () => {
240 const map1 = new Map([
241 [`a`, 1],
242 [`b`, 2],
243 ])
244 const map2 = new Map([
245 [`a`, 1],
246 [`b`, 2],
247 ])
248 const map3 = new Map([
249 [`a`, 1],
250 [`b`, 2],
251 [`c`, 3],
252 ])
253​
254 const hash1 = hash(map1)
255 const hash2 = hash(map2)
256 const hash3 = hash(map3)
257​
258 expect(typeof hash1).toBe(hashType)
259 expect(hash1).toBe(hash2) // Same content should have same hash
260 expect(hash1).not.toBe(hash3) // Different content should have different hash
261 })
262​
263 it(`should hash Maps and Sets with unsupported types`, () => {
264 // Map with BigInt values
265 const mapWithBigInt1 = new Map([
266 [`a`, 123n],
267 [`b`, 456n],
268 ])
269 const mapWithBigInt2 = new Map([
270 [`a`, 123n],
271 [`b`, 456n],
272 ])
273 const mapWithBigInt3 = new Map([
274 [`a`, 123n],
275 [`b`, 789n],
276 ])
277​
278 const hash1 = hash(mapWithBigInt1)
279 const hash2 = hash(mapWithBigInt2)
280 const hash3 = hash(mapWithBigInt3)
281​
282 expect(typeof hash1).toBe(hashType)
283 expect(hash1).toBe(hash2) // Same BigInt content should have same hash
284 expect(hash1).not.toBe(hash3) // Different BigInt content should have different hash
285​
286 // Set with Symbol values
287 const sym1 = Symbol(`test`)
288 const sym2 = Symbol(`different`)
289 const setWithSymbols1 = new Set([sym1, sym2])
290 const setWithSymbols2 = new Set([sym1, sym2])
291 const setWithSymbols3 = new Set([sym1])
292​
293 const hash4 = hash(setWithSymbols1)
294 const hash5 = hash(setWithSymbols2)
295 const hash6 = hash(setWithSymbols3)
296​
297 expect(typeof hash4).toBe(hashType)
298 expect(hash4).toBe(hash5) // Same Symbol content should have same hash
299 expect(hash4).not.toBe(hash6) // Different Symbol content should have different hash
300 })
301​
302 it(`should hash Buffers, Uint8Arrays and File objects by reference`, () => {
303 const buffer1 = Buffer.from([1, 2, 3])
304 const buffer2 = Buffer.from([1, 2, 3])
305 const buffer3 = Buffer.from([1, 2, 3, 4])
306​
307 const hash1 = hash(buffer1)
308 const hash2 = hash(buffer2)
309 const hash3 = hash(buffer3)
310​
311 expect(typeof hash1).toBe(hashType)
312 expect(hash1).not.toBe(hash2) // Same content but different buffer instances have a different hash because it would be too costly to deeply hash buffers
313 expect(hash1).not.toBe(hash3) // Different Buffer content should have different hash
314 expect(hash1).toBe(hash(buffer1)) // Hashing same buffer should return same hash
315​
316 const uint8Array1 = new Uint8Array([1, 2, 3])
317 const uint8Array2 = new Uint8Array([1, 2, 3])
318 const uint8Array3 = new Uint8Array([1, 2, 3, 4])
319​
320 const hash4 = hash(uint8Array1)
321 const hash5 = hash(uint8Array2)
322 const hash6 = hash(uint8Array3)
323​
324 expect(typeof hash4).toBe(hashType)
325 expect(hash4).not.toBe(hash5) // Same content but different uint8Array instances have a different hash because it would be too costly to deeply hash uint8Arrays
326 expect(hash4).not.toBe(hash6) // Different uint8Array content should have different hash
327 expect(hash4).toBe(hash(uint8Array1)) // Hashing same uint8Array should return same hash
328​
329 const file1 = new File([`Hello, world!`], `test.txt`)
330 const file2 = new File([`Hello, world!`], `test.txt`)
331 const file3 = new File([`Hello, world!`], `test.txt`)
332​
333 const hash7 = hash(file1)
334 const hash8 = hash(file2)
335 const hash9 = hash(file3)
336​
337 expect(typeof hash7).toBe(hashType)
338 expect(hash7).not.toBe(hash8) // Same content but different file instances have a different hash because it would be too costly to deeply hash files
339 expect(hash7).not.toBe(hash9) // Different file content should have different hash
340 expect(hash7).toBe(hash(file1)) // Hashing same file should return same hash
341 })
342 })
343​
344 describe(`caching behavior`, () => {
345 it(`should cache hash values for objects`, () => {
346 const obj = { test: `value` }
347​
348 const hash1 = hash(obj)
349 const hash2 = hash(obj)
350​
351 expect(hash1).toBe(hash2)
352 expect(typeof hash1).toBe(hashType)
353 })
354​
355 it(`should return cached values on subsequent calls`, () => {
356 const obj = { complex: { nested: { data: [1, 2, 3] } } }
357​
358 // First call should compute and cache
359 const hash1 = hash(obj)
360​
361 // Second call should return cached value
362 const hash2 = hash(obj)
363​
364 expect(hash1).toBe(hash2)
365 expect(typeof hash1).toBe(hashType)
366 })
367​
368 it(`should not cache primitive values`, () => {
369 // Primitives should not be cached as they use JSON.stringify directly
370 const hash1 = hash(`test`)
371 const hash2 = hash(`test`)
372​
373 expect(hash1).toBe(hash2)
374 expect(typeof hash1).toBe(hashType)
375 })
376 })
377​
378 describe(`edge cases`, () => {
379 it(`should handle empty objects and arrays`, () => {
380 expect(typeof hash({})).toBe(hashType)
381 expect(typeof hash([])).toBe(hashType)
382 expect(hash({})).not.toBe(hash([]))
383 })
384​
385 it(`should handle objects with null and undefined values`, () => {
386 const obj1 = { a: null, b: undefined }
387 const obj2 = { a: null, b: undefined }
388​
389 const hash1 = hash(obj1)
390 const hash2 = hash(obj2)
391​
392 expect(hash1).toBe(hash2)
393 expect(typeof hash1).toBe(hashType)
394 })
395​
396 it(`should distinguish between arrays and maps`, () => {
397 const array = [
398 [1, 2],
399 [3, 4],
400 ] as const
401 const map = new Map(array)
402​
403 const hash1 = hash(array)
404 const hash2 = hash(map)
405​
406 expect(typeof hash1).toBe(hashType)
407 expect(typeof hash2).toBe(hashType)
408 expect(hash1).not.toBe(hash2)
409 })
410​
411 it(`should handle mixed type arrays`, () => {
412 const mixedArray = [1, `string`, true, null, { key: `value` }]
413 const sameArray = [1, `string`, true, null, { key: `value` }]
414​
415 const hash1 = hash(mixedArray)
416 const hash2 = hash(sameArray)
417​
418 expect(hash1).toBe(hash2)
419 expect(typeof hash1).toBe(hashType)
420 })
421​
422 it(`should produce consistent hashes for same content`, () => {
423 const obj = {
424 string: `test`,
425 number: 42,
426 boolean: true,
427 array: [1, 2, 3],
428 nested: { inner: `value` },
429 buffer: Buffer.from([1, 2, 3]),
430 uint8Array: new Uint8Array([1, 2, 3]),
431 file: new File([`Hello, world!`], `test.txt`),
432 }
433​
434 // Multiple calls should return the same hash
435 const hashes = Array.from({ length: 5 }, () => hash(obj))
436 const firstHash = hashes[0]
437​
438 expect(hashes.every((h) => h === firstHash)).toBe(true)
439 expect(typeof firstHash).toBe(hashType)
440 })
441 })
442})