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

explorerSearch.test.ts

118 lines4.1 KB

1import { runInNewContext } from 'node:vm'
2import { describe, expect, it, vi } from 'vitest'
3import { searchEntries, searchExplorer } from '../explorerSearch'
4​
5describe('explorer search', () => {
6 it.each([
7 { expression: "new Map([['name', 'needle']])", label: 'name' },
8 { expression: "new Set(['needle'])", label: '0' },
9 {
10 expression: "new (class extends Map {})([['name', 'needle']])",
11 label: 'name',
12 },
13 ])(
14 'searches cross-realm collections: $expression',
15 ({ expression, label }) => {
16 const value: unknown = runInNewContext(expression)
17 expect(searchExplorer(value, 'needle').hasMatch).toBe(true)
18 expect(searchEntries(value)?.[0]?.label).toBe(label)
19 },
20 )
21​
22 it('does not read collection tag accessors', () => {
23 const read = vi.fn<() => never>(() => {
24 throw new Error('Do not read')
25 })
26 const value = { name: 'needle' }
27 Object.defineProperty(value, Symbol.toStringTag, { get: read })
28 expect(searchExplorer(value, 'needle').hasMatch).toBe(true)
29 expect(read).not.toHaveBeenCalled()
30 })
31​
32 it('searches ordinary data when a collection tag has no native brand', () => {
33 const value = { name: 'needle', [Symbol.toStringTag]: 'Map' }
34 expect(searchExplorer(value, 'needle').hasMatch).toBe(true)
35 })
36​
37 it('finds the native brand behind an overridden collection tag', () => {
38 const value: unknown = runInNewContext(
39 "Object.defineProperty(new Map([['name', 'needle']]), Symbol.toStringTag, {value: 'Set'})",
40 )
41 expect(searchExplorer(value, 'needle').hasMatch).toBe(true)
42 expect(searchEntries(value)?.[0]?.label).toBe('name')
43 })
44​
45 it('treats proxy collections and throwing introspection as opaque', () => {
46 for (const collection of [
47 new Map([['name', 'needle']]),
48 new Set(['needle']),
49 ]) {
50 expect(searchEntries(new Proxy(collection, {}))).toBeUndefined()
51 }
52 const value = new Proxy(
53 { name: 'needle' },
54 {
55 ownKeys() {
56 throw new Error('Cannot inspect')
57 },
58 },
59 )
60 expect(searchExplorer(value, 'needle').hasMatch).toBe(false)
61 })
62​
63 it('searches own class data fields without invoking custom code', () => {
64 const read = vi.fn<() => never>(() => {
65 throw new Error('Do not read')
66 })
67 class Profile {
68 *[Symbol.iterator]() {
69 yield read()
70 }
71 name = 'needle'
72 toJSON = read
73 }
74 const value = new Profile()
75 Object.defineProperty(value, 'secret', { enumerable: true, get: read })
76 const match = searchExplorer(value, 'needle')
77 expect(match.hasMatch).toBe(true)
78 expect(match.children.get('name')?.value).toBe(true)
79 expect(match.children.get('secret')?.kind).toBe('accessor')
80 expect(read).not.toHaveBeenCalled()
81 })
82​
83 it.each([
84 { kind: 'Map', value: new Map([['entry', 'needle']]), key: 'entry' },
85 { kind: 'Set', value: new Set(['needle']), key: '0' },
86 ])('searches a $kind with a null prototype', ({ value, key }) => {
87 Object.setPrototypeOf(value, null)
88 expect(searchEntries(value)).toStrictEqual([
89 { key, label: key, value: 'needle' },
90 ])
91 expect(searchExplorer(value, 'needle').children.get(key)?.value).toBe(true)
92 })
93​
94 it('keeps typed arrays and boxed strings opaque', () => {
95 expect(searchEntries(new Uint8Array(100_000))).toBeUndefined()
96 expect(searchEntries(Object('needle'))).toBeUndefined()
97 const value: unknown = runInNewContext("new String('needle')")
98 const read = vi.fn<() => never>(() => {
99 throw new Error('Do not read')
100 })
101 Object.defineProperty(Object.getPrototypeOf(value), 'constructor', {
102 get: read,
103 })
104 expect(searchEntries(value)).toBeUndefined()
105 expect(read).not.toHaveBeenCalled()
106 Object.setPrototypeOf(value, null)
107 expect(searchEntries(value)).toBeUndefined()
108 })
109​
110 it('bounds deeply nested searches and keeps nearby matches', () => {
111 let value: unknown = 'needle'
112 for (let depth = 0; depth < 10_000; depth++) value = { next: value }
113 expect(
114 searchExplorer({ nearby: 'needle', deep: value }, 'needle').hasMatch,
115 ).toBe(true)
116 expect(searchExplorer(value, 'needle').hasMatch).toBe(false)
117 })
118})