Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

explorerSearch.test.ts

198 lines7.1 KB

1import { runInNewContext } from 'node:vm'
2import { describe, expect, it, vi } from 'vitest'
3import {
4 createSearchText,
5 searchEntries,
6 searchExplorer,
7} from '../explorerSearch'
8​
9describe('explorer search', () => {
10 it.each([
11 { expression: "new Map([['name', 'needle']])", label: 'name' },
12 { expression: "new Set(['needle'])", label: '0' },
13 {
14 expression: "new (class extends Map {})([['name', 'needle']])",
15 label: 'name',
16 },
17 ])(
18 'searches cross-realm collections: $expression',
19 ({ expression, label }) => {
20 const value: unknown = runInNewContext(expression)
21 expect(searchExplorer(value, 'needle').hasMatch).toBe(true)
22 expect(searchEntries(value)?.[0]?.label).toBe(label)
23 },
24 )
25​
26 it('does not read collection tag accessors', () => {
27 const read = vi.fn<() => never>(() => {
28 throw new Error('Do not read')
29 })
30 const value = { name: 'needle' }
31 Object.defineProperty(value, Symbol.toStringTag, { get: read })
32 expect(searchExplorer(value, 'needle').hasMatch).toBe(true)
33 expect(read).not.toHaveBeenCalled()
34 })
35​
36 it('searches ordinary data when a collection tag has no native brand', () => {
37 const value = { name: 'needle', [Symbol.toStringTag]: 'Map' }
38 expect(searchExplorer(value, 'needle').hasMatch).toBe(true)
39 })
40​
41 it('finds the native brand behind an overridden collection tag', () => {
42 const value: unknown = runInNewContext(
43 "Object.defineProperty(new Map([['name', 'needle']]), Symbol.toStringTag, {value: 'Set'})",
44 )
45 expect(searchExplorer(value, 'needle').hasMatch).toBe(true)
46 expect(searchEntries(value)?.[0]?.label).toBe('name')
47 })
48​
49 it('treats proxy collections and throwing introspection as opaque', () => {
50 for (const collection of [
51 new Map([['name', 'needle']]),
52 new Set(['needle']),
53 ]) {
54 expect(searchEntries(new Proxy(collection, {}))).toBeUndefined()
55 }
56 const value = new Proxy(
57 { name: 'needle' },
58 {
59 ownKeys() {
60 throw new Error('Cannot inspect')
61 },
62 },
63 )
64 expect(searchExplorer(value, 'needle').hasMatch).toBe(false)
65 })
66​
67 it('searches own class data fields without invoking custom code', () => {
68 const read = vi.fn<() => never>(() => {
69 throw new Error('Do not read')
70 })
71 class Profile {
72 *[Symbol.iterator]() {
73 yield read()
74 }
75 name = 'needle'
76 toJSON = read
77 }
78 const value = new Profile()
79 Object.defineProperty(value, 'secret', { enumerable: true, get: read })
80 const match = searchExplorer(value, 'needle')
81 expect(match.hasMatch).toBe(true)
82 expect(match.children.get('name')?.value).toBe(true)
83 expect(match.children.get('secret')?.kind).toBe('accessor')
84 expect(read).not.toHaveBeenCalled()
85 })
86​
87 it.each([
88 { kind: 'Map', value: new Map([['entry', 'needle']]), key: 'entry' },
89 { kind: 'Set', value: new Set(['needle']), key: '0' },
90 ])('searches a $kind with a null prototype', ({ value, key }) => {
91 Object.setPrototypeOf(value, null)
92 expect(searchEntries(value)).toStrictEqual([
93 { key, label: key, value: 'needle' },
94 ])
95 expect(searchExplorer(value, 'needle').children.get(key)?.value).toBe(true)
96 })
97​
98 it('keeps typed arrays and boxed strings opaque', () => {
99 expect(searchEntries(new Uint8Array(100_000))).toBeUndefined()
100 expect(searchEntries(Object('needle'))).toBeUndefined()
101 const value: unknown = runInNewContext("new String('needle')")
102 const read = vi.fn<() => never>(() => {
103 throw new Error('Do not read')
104 })
105 Object.defineProperty(Object.getPrototypeOf(value), 'constructor', {
106 get: read,
107 })
108 expect(searchEntries(value)).toBeUndefined()
109 expect(read).not.toHaveBeenCalled()
110 Object.setPrototypeOf(value, null)
111 expect(searchEntries(value)).toBeUndefined()
112 })
113​
114 it('bounds deeply nested searches and keeps nearby matches', () => {
115 let value: unknown = 'needle'
116 for (let depth = 0; depth < 10_000; depth++) value = { next: value }
117 expect(
118 searchExplorer({ nearby: 'needle', deep: value }, 'needle').hasMatch,
119 ).toBe(true)
120 expect(searchExplorer(value, 'needle').hasMatch).toBe(false)
121 })
122​
123 it.each([
124 { depth: 500, matches: true },
125 { depth: 501, matches: false },
126 ])('bounds matching entries at object depth $depth', ({ depth, matches }) => {
127 let value: unknown = { name: 'needle' }
128 for (let index = 0; index < depth; index++) value = { next: value }
129 expect(searchExplorer(value, 'needle').hasMatch).toBe(matches)
130 expect(createSearchText(value).includes('needle')).toBe(matches)
131 })
132​
133 describe('createSearchText', () => {
134 const matches = (value: unknown, term: string) =>
135 createSearchText(value).includes(term)
136​
137 it.each([
138 { value: { name: 'needle' }, term: 'needle' },
139 { value: { needle: 1 }, term: 'needle' },
140 { value: [1, 2, { deep: [42] }], term: '42' },
141 { value: new Map([['a', 'needle']]), term: 'needle' },
142 { value: new Map([[{}, 'value']]), term: 'object key' },
143 { value: new Set(['needle']), term: 'needle' },
144 { value: { flag: true }, term: 'true' },
145 { value: { empty: null }, term: 'null' },
146 { value: { name: 'other' }, term: 'needle' },
147 { value: { Name: 'NEEDLE' }, term: 'needle' },
148 { value: 'needle', term: 'eed' },
149 { value: 42, term: '4' },
150 { value: new Uint8Array([1]), term: '1' },
151 { value: { ab: 'cd' }, term: 'bc' },
152 { value: ['ab', 'cd'], term: 'bc' },
153 ])('agrees with searchExplorer for $term', ({ value, term }) => {
154 expect(matches(value, term)).toBe(searchExplorer(value, term).hasMatch)
155 })
156​
157 it('handles circular data', () => {
158 const value: Record<string, unknown> = { name: 'other' }
159 value.self = value
160 expect(matches(value, 'needle')).toBe(false)
161 value.found = 'needle'
162 expect(matches(value, 'needle')).toBe(true)
163 })
164​
165 it('matches accessor labels without evaluating getters', () => {
166 const read = vi.fn<() => string>(() => 'needle')
167 const value = {}
168 Object.defineProperty(value, 'getter', { get: read, enumerable: true })
169 expect(matches(value, 'needle')).toBe(false)
170 expect(matches(value, 'getter')).toBe(true)
171 expect(read).not.toHaveBeenCalled()
172 })
173​
174 it('bounds deeply nested searches', () => {
175 let value: unknown = 'needle'
176 for (let depth = 0; depth < 10_000; depth++) value = { next: value }
177 expect(matches(value, 'needle')).toBe(false)
178 expect(matches({ nearby: 'needle', deep: value }, 'needle')).toBe(true)
179 })
180​
181 it('searches shared data once', () => {
182 let value: unknown = { name: 'needle' }
183 for (let depth = 0; depth < 40; depth++) {
184 value = { left: value, right: value }
185 }
186 expect(createSearchText(value).split('needle')).toHaveLength(2)
187 })
188​
189 it('searches shared data at its shallowest depth', () => {
190 const shared = { name: 'needle' }
191 let deep: unknown = shared
192 for (let depth = 0; depth < 600; depth++) deep = { next: deep }
193 const value = { deep, near: shared }
194 expect(matches(value, 'needle')).toBe(true)
195 expect(searchExplorer(value, 'needle').hasMatch).toBe(true)
196 })
197 })
198})