Open History to see the last commit
1import { describe, expect, it, vi } from 'vitest'
2import { hash, registerOpaqueHash } from '../src/hashing/hash'
3
4function countTraversalAllocations(run: () => void): number {
5 let allocations = 0
6 for (const name of [`Map`, `Set`, `WeakMap`] as const) {
7 vi.stubGlobal(
8 name,
9 new Proxy(globalThis[name], {
10 construct(target, args) {
11 allocations++
12 return Reflect.construct(target, args)
13 },
14 }),
15 )
16 }
17 try {
18 run()
19 } finally {
20 vi.unstubAllGlobals()
21 }
22 return allocations
23}
24
25describe(`hash traversal work`, () => {
26 it.each([`object`, `array`] as const)(
27 `does not let a rejected %s traversal subsidize its own retry`,
28 (kind) => {
29 const left = Array.from({ length: 500_001 }, () => 0)
30 const right = Array.from({ length: 500_001 }, () => 0)
31 const root = kind === `object` ? { left, right } : [left, right]
32 // Either child fits, but this fresh root exceeds the combined work cap.
33 // Keeping the completed left child's cache after failure lets retry pass.
34 for (let attempt = 0; attempt < 2; attempt++) {
35 expect(() => hash(root)).toThrow(
36 `Value is too complex to hash safely: structural work`,
37 )
38 }
39 },
40 )
41
42 it(`does not allocate traversal collections for primitive and cached inputs`, () => {
43 const cached = { id: 1, title: `cached` }
44 hash(cached)
45 const inputs = [null, undefined, false, 0, 1n, `row`, Symbol(`key`), cached]
46 expect(
47 countTraversalAllocations(() => {
48 for (const input of inputs) hash(input)
49 }),
50 ).toBe(0)
51 })
52
53 it(`measures traversal collections for fresh structural inputs`, () => {
54 expect(countTraversalAllocations(() => hash({ id: 1 }))).toBeGreaterThan(0)
55 })
56
57 it(`uses identity for registered handles without traversing mutable internals`, () => {
58 const first: Record<string, unknown> = {}
59 const second: Record<string, unknown> = {}
60 for (const value of [first, second]) {
61 value.self = value
62 Object.defineProperty(value, `state`, {
63 enumerable: true,
64 get() {
65 throw new Error(`must not read handle state`)
66 },
67 })
68 registerOpaqueHash(value)
69 }
70 const before = hash({ handle: first })
71 first.changed = true
72 expect(hash({ handle: first })).toBe(before)
73 expect(hash({ handle: second })).not.toBe(before)
74 expect(
75 countTraversalAllocations(() => {
76 hash(first)
77 hash(second)
78 }),
79 ).toBe(0)
80 })
81
82 it(`visits each shared acyclic subtree once`, () => {
83 let reads = 0
84 let root: object = { value: 1 }
85 for (let depth = 0; depth < 200; depth++) {
86 const child = root
87 root = {
88 get left() {
89 reads++
90 return child
91 },
92 get right() {
93 reads++
94 return child
95 },
96 }
97 }
98 const result = hash(root)
99 expect(reads).toBe(400)
100 expect(hash(root)).toBe(result)
101 expect(reads).toBe(400)
102 })
103
104 it(`bounds value visits without publishing partial structural caches`, () => {
105 let reads = 0
106 const shared = {}
107 const root = {
108 a: {
109 get value() {
110 reads++
111 return 1
112 },
113 },
114 // Repeated references must still count as work, even when their hashes
115 // are cached; no expanded tree is needed to reach the bound.
116 z: Array.from({ length: 1_000_001 }, () => shared),
117 }
118 for (let attempt = 1; attempt <= 2; attempt++) {
119 expect(() => hash(root)).toThrow(
120 `Value is too complex to hash safely: structural work`,
121 )
122 expect(reads).toBe(attempt)
123 }
124 expect(hash({ value: 1 })).toBe(hash({ value: 1 }))
125 })
126})