Open History to see the last commit
1// Ordinary structural-hash graph tests: a cycle through each container kind,
2// and a completed sibling left uncached when a cycle rejects the root. Both
3// compare production with fixed expectations. hash-graph-oracle.property.test.ts
4// owns the reachable-graph law and declares its check.
5import { describe, expect, it } from 'vitest'
6import { hash } from '../src/hashing/hash'
7
8describe(`structural hash graph boundary`, () => {
9 it.each([
10 `object`,
11 `array`,
12 `map-key`,
13 `map-value`,
14 `set`,
15 `symbol`,
16 ] as const)(`rejects a cycle through %s on every attempt`, (kind) => {
17 const record: Record<PropertyKey, unknown> = {}
18 const array: Array<unknown> = []
19 const map = new Map<unknown, unknown>()
20 const set = new Set<unknown>()
21 const input =
22 kind === `array`
23 ? array
24 : kind.startsWith(`map`)
25 ? map
26 : kind === `set`
27 ? set
28 : record
29 if (kind === `object`) record.self = input
30 if (kind === `symbol`) record[Symbol(`self`)] = input
31 if (kind === `array`) array.push(input)
32 if (kind === `map-key`) map.set(input, 1)
33 if (kind === `map-value`) map.set(1, input)
34 if (kind === `set`) set.add(input)
35 for (let attempt = 0; attempt < 2; attempt++) {
36 expect(() => hash(input)).toThrow(`Cannot hash cyclic structural values`)
37 }
38 })
39
40 it(`leaves completed siblings uncached after a cycle rejects the root`, () => {
41 let reads = 0
42 const sibling = {
43 get value() {
44 return ++reads
45 },
46 }
47 const root: Record<string, unknown> = { a: sibling }
48 root.z = root
49 expect(() => hash(root)).toThrow(`Cannot hash cyclic structural values`)
50 expect(() => hash(root)).toThrow(`Cannot hash cyclic structural values`)
51 expect(reads).toBe(2)
52 delete root.z
53 expect(hash(root)).toBe(hash({ a: { value: 3 } }))
54 expect(reads).toBe(3)
55 })
56})