Open History to see the last commit
1// Ordinary hash identity tests: fixed Temporal-shaped, RegExp, realm, and
2// colliding-control checks. Each compares production with literal expectations
3// and reaches no declared part. hash-identity-oracle.property.test.ts owns the
4// identity law and declares its checks and model.
5import { runInNewContext } from 'node:vm'
6import { describe, expect, it } from 'vitest'
7import { MultiSet } from '../src/multiset.js'
8import { equalHashValues, hash } from '../src/hashing/hash.js'
9import { colliding, collidingTopKBatch } from './hash-collision-support.js'
10
11describe(`hash identity oracle`, () => {
12 // Real Temporal types print distinct formats, so only a polyfill-shaped
13 // value can share text across types. The contract still names the tag.
14 it(`Temporal-shaped values with the same text and different types differ`, () => {
15 const temporalLike = (tag: string) =>
16 new (class {
17 get [Symbol.toStringTag]() {
18 return tag
19 }
20 toString() {
21 return `2024-01`
22 }
23 })()
24 const date = temporalLike(`Temporal.PlainDate`)
25 const month = temporalLike(`Temporal.PlainYearMonth`)
26 expect(equalHashValues(date, temporalLike(`Temporal.PlainDate`))).toBe(true)
27 expect(hash(date)).toBe(hash(temporalLike(`Temporal.PlainDate`)))
28 expect(equalHashValues(date, month)).toBe(false)
29 })
30
31 // Current behavior outside the grammar: equality compares RegExp fields
32 // with `===`, so a NaN `lastIndex` never equals itself, and `-0` equals `0`.
33 // Hashing normalizes both, so the NaN pair hashes equal.
34 it(`compares RegExp lastIndex with strict equality`, () => {
35 const withLastIndex = (lastIndex: number) =>
36 Object.assign(/a/g, { lastIndex })
37 expect(equalHashValues(withLastIndex(NaN), withLastIndex(NaN))).toBe(false)
38 expect(equalHashValues(withLastIndex(-0), withLastIndex(0))).toBe(true)
39 })
40
41 // Current behavior outside the grammar: an array from another realm takes
42 // the object marker, but equality still compares array lengths.
43 it(`keeps the equality length check for arrays from another realm`, () => {
44 const withHole = runInNewContext(`const a = [1]; a.length = 2; a`)
45 const short = runInNewContext(`[1]`)
46 expect(equalHashValues(withHole, short)).toBe(false)
47 expect(
48 equalHashValues(runInNewContext(`[1, 2]`), runInNewContext(`[1, 2]`)),
49 ).toBe(true)
50 })
51
52 // The colliding copy is only a useful control if its markers collide.
53 it(`gives every type marker the same hash number in the colliding module`, () => {
54 const empty = [
55 new Map(),
56 new Set(),
57 {},
58 Object.assign(new RegExp(``), { lastIndex: 0 }),
59 ]
60 const hashes = new Set(empty.map((value) => colliding.hash(value)))
61 expect(hashes.size).toBe(2) // A RegExp still hashes its header fields.
62 expect(colliding.hash(new Map())).toBe(colliding.hash(new Set()))
63 expect(colliding.hash(new Map())).toBe(colliding.hash({}))
64 })
65
66 it(`keeps a Map-to-Set replacement in topKBatch when markers collide`, () => {
67 const batch = collidingTopKBatch([
68 new MultiSet<[number, unknown]>([
69 [[1, new Map()], -1],
70 [[1, new Set()], 1],
71 ]),
72 ])
73 expect(
74 [...batch].map(([[, value], weight]) => [
75 value instanceof Map ? `map` : value instanceof Set ? `set` : `other`,
76 weight,
77 ]),
78 ).toEqual([
79 [`map`, -1],
80 [`set`, 1],
81 ])
82 })
83})