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