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