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