musora-content-services 2.160.4 → 2.161.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.agent/decisions/2026-05-20-live-event-fetch-permissions-id.md +23 -0
- package/CHANGELOG.md +19 -0
- package/package.json +1 -1
- package/src/contentTypeConfig.js +13 -15
- package/src/index.d.ts +6 -2
- package/src/index.js +6 -2
- package/src/infrastructure/sanity/README.md +230 -0
- package/src/infrastructure/sanity/SanityClient.ts +105 -0
- package/src/infrastructure/sanity/clients/ContentClient.ts +164 -0
- package/src/infrastructure/sanity/examples/usage.ts +101 -0
- package/src/infrastructure/sanity/executors/FetchQueryExecutor.ts +110 -0
- package/src/infrastructure/sanity/index.ts +19 -0
- package/src/infrastructure/sanity/interfaces/ConfigProvider.ts +6 -0
- package/src/infrastructure/sanity/interfaces/FetchByIdOptions.ts +7 -0
- package/src/infrastructure/sanity/interfaces/QueryExecutor.ts +8 -0
- package/src/infrastructure/sanity/interfaces/SanityConfig.ts +10 -0
- package/src/infrastructure/sanity/interfaces/SanityError.ts +7 -0
- package/src/infrastructure/sanity/interfaces/SanityQuery.ts +5 -0
- package/src/infrastructure/sanity/interfaces/SanityResponse.ts +6 -0
- package/src/infrastructure/sanity/providers/DefaultConfigProvider.ts +38 -0
- package/src/lib/sanity/decorators/base.ts +142 -0
- package/src/lib/sanity/decorators/examples.ts +229 -0
- package/src/lib/sanity/decorators/navigate-to.ts +139 -0
- package/src/lib/sanity/decorators/need-access.ts +40 -0
- package/src/lib/sanity/decorators/page-type.ts +35 -0
- package/src/services/awards/award-query.js +71 -0
- package/src/services/contentAggregator.js +1 -1
- package/src/services/multi-user-accounts/multi-user-accounts.ts +11 -7
- package/src/services/user/memberships.ts +46 -34
- package/src/services/user/profile.ts +66 -0
- package/test/unit/infrastructure/sanity/ContentClient.test.ts +168 -0
- package/test/unit/infrastructure/sanity/DefaultConfigProvider.test.ts +93 -0
- package/test/unit/infrastructure/sanity/FetchQueryExecutor.test.ts +174 -0
- package/test/unit/infrastructure/sanity/SanityClient.test.ts +140 -0
- package/test/unit/lib/sanity/decorators/base.test.ts +368 -0
- package/test/unit/lib/sanity/decorators/navigate-to.test.ts +266 -0
- package/test/unit/lib/sanity/decorators/need-access.test.ts +89 -0
- package/test/unit/lib/sanity/decorators/page-type.test.ts +81 -0
- package/.claude/settings.local.json +0 -23
- package/src/services/user/profile.js +0 -43
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { SanityClient } from '../../../../src/infrastructure/sanity/SanityClient'
|
|
2
|
+
import { ConfigProvider } from '../../../../src/infrastructure/sanity/interfaces/ConfigProvider'
|
|
3
|
+
import { QueryExecutor } from '../../../../src/infrastructure/sanity/interfaces/QueryExecutor'
|
|
4
|
+
import { SanityConfig } from '../../../../src/infrastructure/sanity/interfaces/SanityConfig'
|
|
5
|
+
|
|
6
|
+
describe('SanityClient', () => {
|
|
7
|
+
const config: SanityConfig = {
|
|
8
|
+
projectId: 'p',
|
|
9
|
+
dataset: 'd',
|
|
10
|
+
version: '2021-06-07',
|
|
11
|
+
token: 't',
|
|
12
|
+
}
|
|
13
|
+
let mockConfigProvider: jest.Mocked<ConfigProvider>
|
|
14
|
+
let mockExecutor: jest.Mocked<QueryExecutor>
|
|
15
|
+
let client: SanityClient
|
|
16
|
+
|
|
17
|
+
beforeEach(() => {
|
|
18
|
+
mockConfigProvider = {
|
|
19
|
+
getConfig: jest.fn().mockReturnValue(config),
|
|
20
|
+
}
|
|
21
|
+
mockExecutor = {
|
|
22
|
+
execute: jest.fn(),
|
|
23
|
+
}
|
|
24
|
+
client = new SanityClient(mockConfigProvider, mockExecutor)
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
describe('fetchSingle', () => {
|
|
28
|
+
test('returns first item from result array', async () => {
|
|
29
|
+
mockExecutor.execute.mockResolvedValue({
|
|
30
|
+
result: [{ id: 1 }, { id: 2 }],
|
|
31
|
+
ms: 5,
|
|
32
|
+
query: 'q',
|
|
33
|
+
})
|
|
34
|
+
const result = await client.fetchSingle<{ id: number }>('*[_type=="x"]', { a: 1 })
|
|
35
|
+
expect(result).toEqual({ id: 1 })
|
|
36
|
+
expect(mockExecutor.execute).toHaveBeenCalledWith(
|
|
37
|
+
{ query: '*[_type=="x"]', params: { a: 1 } },
|
|
38
|
+
config
|
|
39
|
+
)
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
test('returns null when result array is empty', async () => {
|
|
43
|
+
mockExecutor.execute.mockResolvedValue({ result: [], ms: 1, query: 'q' })
|
|
44
|
+
expect(await client.fetchSingle('q')).toBeNull()
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
test('returns null when result is not an array', async () => {
|
|
48
|
+
mockExecutor.execute.mockResolvedValue({ result: { id: 1 } as any, ms: 1, query: 'q' })
|
|
49
|
+
expect(await client.fetchSingle('q')).toBeNull()
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
test('rethrows SanityError unchanged and returns nothing useful', async () => {
|
|
53
|
+
const sanityError = { message: 'boom', query: 'q' }
|
|
54
|
+
mockExecutor.execute.mockRejectedValue(sanityError)
|
|
55
|
+
await expect(client.fetchSingle('q')).rejects.toEqual(sanityError)
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
test('wraps non-SanityError as SanityError including query and originalError', async () => {
|
|
59
|
+
const raw = new Error('network down')
|
|
60
|
+
mockExecutor.execute.mockRejectedValue(raw)
|
|
61
|
+
await expect(client.fetchSingle('myquery')).rejects.toMatchObject({
|
|
62
|
+
message: 'network down',
|
|
63
|
+
query: 'myquery',
|
|
64
|
+
originalError: raw,
|
|
65
|
+
})
|
|
66
|
+
})
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
describe('fetchList', () => {
|
|
70
|
+
test('returns result array', async () => {
|
|
71
|
+
const data = [{ id: 1 }, { id: 2 }]
|
|
72
|
+
mockExecutor.execute.mockResolvedValue({ result: data, ms: 1, query: 'q' })
|
|
73
|
+
const result = await client.fetchList('q')
|
|
74
|
+
expect(result).toEqual(data)
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
test('returns empty array when result is null/undefined', async () => {
|
|
78
|
+
mockExecutor.execute.mockResolvedValue({ result: null as any, ms: 1, query: 'q' })
|
|
79
|
+
expect(await client.fetchList('q')).toEqual([])
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
test('rethrows existing SanityError', async () => {
|
|
83
|
+
const sanityError = { message: 'boom', query: 'q' }
|
|
84
|
+
mockExecutor.execute.mockRejectedValue(sanityError)
|
|
85
|
+
await expect(client.fetchList('q')).rejects.toEqual(sanityError)
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
test('wraps non-SanityError as SanityError', async () => {
|
|
89
|
+
mockExecutor.execute.mockRejectedValue(new Error('fail'))
|
|
90
|
+
await expect(client.fetchList('q')).rejects.toMatchObject({
|
|
91
|
+
message: 'fail',
|
|
92
|
+
query: 'q',
|
|
93
|
+
})
|
|
94
|
+
})
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
describe('executeQuery', () => {
|
|
98
|
+
test('returns the raw result', async () => {
|
|
99
|
+
mockExecutor.execute.mockResolvedValue({ result: { count: 7 }, ms: 1, query: 'q' })
|
|
100
|
+
const result = await client.executeQuery<{ count: number }>('q')
|
|
101
|
+
expect(result).toEqual({ count: 7 })
|
|
102
|
+
})
|
|
103
|
+
|
|
104
|
+
test('returns null when result is missing', async () => {
|
|
105
|
+
mockExecutor.execute.mockResolvedValue({ result: null as any, ms: 1, query: 'q' })
|
|
106
|
+
expect(await client.executeQuery('q')).toBeNull()
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
test('rethrows wrapped error on failure', async () => {
|
|
110
|
+
mockExecutor.execute.mockRejectedValue(new Error('explode'))
|
|
111
|
+
await expect(client.executeQuery('q')).rejects.toMatchObject({
|
|
112
|
+
message: 'explode',
|
|
113
|
+
query: 'q',
|
|
114
|
+
})
|
|
115
|
+
})
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
describe('config caching', () => {
|
|
119
|
+
test('loads config once across multiple calls', async () => {
|
|
120
|
+
mockExecutor.execute.mockResolvedValue({ result: [], ms: 1, query: 'q' })
|
|
121
|
+
await client.fetchList('q1')
|
|
122
|
+
await client.fetchList('q2')
|
|
123
|
+
await client.fetchSingle('q3')
|
|
124
|
+
expect(mockConfigProvider.getConfig).toHaveBeenCalledTimes(1)
|
|
125
|
+
})
|
|
126
|
+
|
|
127
|
+
test('refreshConfig forces a reload on the next call', async () => {
|
|
128
|
+
mockExecutor.execute.mockResolvedValue({ result: [], ms: 1, query: 'q' })
|
|
129
|
+
await client.fetchList('q1')
|
|
130
|
+
client.refreshConfig()
|
|
131
|
+
await client.fetchList('q2')
|
|
132
|
+
expect(mockConfigProvider.getConfig).toHaveBeenCalledTimes(2)
|
|
133
|
+
})
|
|
134
|
+
})
|
|
135
|
+
|
|
136
|
+
test('falls back to default providers when none are supplied', () => {
|
|
137
|
+
const defaultClient = new SanityClient()
|
|
138
|
+
expect(defaultClient).toBeInstanceOf(SanityClient)
|
|
139
|
+
})
|
|
140
|
+
})
|
|
@@ -0,0 +1,368 @@
|
|
|
1
|
+
import {
|
|
2
|
+
decorate,
|
|
3
|
+
decorateAll,
|
|
4
|
+
decorateAsync,
|
|
5
|
+
decorateAllAsync,
|
|
6
|
+
type Decoratable,
|
|
7
|
+
type FieldDecorator,
|
|
8
|
+
type FieldDecoratorAsync,
|
|
9
|
+
} from '../../../../../src/lib/sanity/decorators/base'
|
|
10
|
+
|
|
11
|
+
describe('base decorator', () => {
|
|
12
|
+
describe('decorate', () => {
|
|
13
|
+
test('sets field on single object', () => {
|
|
14
|
+
const item: Decoratable = { id: 1 }
|
|
15
|
+
decorate(item, 'mark', () => 'A')
|
|
16
|
+
expect(item.mark).toBe('A')
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
test('sets field on every item in array', () => {
|
|
20
|
+
const items: Decoratable[] = [{ id: 1 }, { id: 2 }, { id: 3 }]
|
|
21
|
+
decorate(items, 'mark', (i) => i.id)
|
|
22
|
+
expect(items.map((i) => i.mark)).toEqual([1, 2, 3])
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
test('returns the same reference it was given', () => {
|
|
26
|
+
const items: Decoratable[] = [{ id: 1 }]
|
|
27
|
+
const result = decorate(items, 'mark', () => true)
|
|
28
|
+
expect(result).toBe(items)
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
test('recurses children up to 3 levels deep', () => {
|
|
32
|
+
const tree: Decoratable = {
|
|
33
|
+
id: 1,
|
|
34
|
+
children: [
|
|
35
|
+
{
|
|
36
|
+
id: 2,
|
|
37
|
+
children: [
|
|
38
|
+
{
|
|
39
|
+
id: 3,
|
|
40
|
+
children: [{ id: 4 }],
|
|
41
|
+
},
|
|
42
|
+
],
|
|
43
|
+
},
|
|
44
|
+
],
|
|
45
|
+
}
|
|
46
|
+
decorate(tree, 'visited', () => true)
|
|
47
|
+
|
|
48
|
+
expect(tree.visited).toBe(true)
|
|
49
|
+
const lvl1 = (tree.children as Decoratable[])[0]
|
|
50
|
+
const lvl2 = (lvl1.children as Decoratable[])[0]
|
|
51
|
+
const lvl3 = (lvl2.children as Decoratable[])[0]
|
|
52
|
+
expect(lvl1.visited).toBe(true)
|
|
53
|
+
expect(lvl2.visited).toBe(true)
|
|
54
|
+
expect(lvl3.visited).toBeUndefined()
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
test('ignores missing or non-array children', () => {
|
|
58
|
+
const item: Decoratable = { id: 1 }
|
|
59
|
+
expect(() => decorate(item, 'mark', () => 1)).not.toThrow()
|
|
60
|
+
expect(item.mark).toBe(1)
|
|
61
|
+
|
|
62
|
+
const bad: Decoratable = { id: 1, children: 'not-an-array' as unknown as Decoratable[] }
|
|
63
|
+
expect(() => decorate(bad, 'mark', () => 1)).not.toThrow()
|
|
64
|
+
expect(bad.mark).toBe(1)
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
test('passes the visited item to the compute callback', () => {
|
|
68
|
+
const item: Decoratable = { id: 7, label: 'x' }
|
|
69
|
+
const compute = jest.fn(() => 'ok')
|
|
70
|
+
decorate(item, 'mark', compute)
|
|
71
|
+
expect(compute).toHaveBeenCalledTimes(1)
|
|
72
|
+
expect(compute).toHaveBeenCalledWith(item)
|
|
73
|
+
})
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
describe('decorateAll', () => {
|
|
77
|
+
test('applies every decorator on a single walk', () => {
|
|
78
|
+
const items: Decoratable[] = [{ id: 1, children: [{ id: 2 }] }]
|
|
79
|
+
const a: FieldDecorator<Decoratable> = { field: 'a', compute: () => 'A' }
|
|
80
|
+
const b: FieldDecorator<Decoratable> = { field: 'b', compute: () => 'B' }
|
|
81
|
+
decorateAll(items, [a, b])
|
|
82
|
+
|
|
83
|
+
expect(items[0].a).toBe('A')
|
|
84
|
+
expect(items[0].b).toBe('B')
|
|
85
|
+
const child = (items[0].children as Decoratable[])[0]
|
|
86
|
+
expect(child.a).toBe('A')
|
|
87
|
+
expect(child.b).toBe('B')
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
test('empty decorator list is a no-op', () => {
|
|
91
|
+
const items: Decoratable[] = [{ id: 1 }]
|
|
92
|
+
decorateAll(items, [])
|
|
93
|
+
expect(Object.keys(items[0])).toEqual(['id'])
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
test('visits each item exactly once even with multiple decorators', () => {
|
|
97
|
+
const items: Decoratable[] = [{ id: 1, children: [{ id: 2 }, { id: 3 }] }]
|
|
98
|
+
const seen: number[] = []
|
|
99
|
+
const probe: FieldDecorator<Decoratable> = {
|
|
100
|
+
field: 'probe',
|
|
101
|
+
compute: (item) => {
|
|
102
|
+
seen.push(item.id as number)
|
|
103
|
+
return true
|
|
104
|
+
},
|
|
105
|
+
}
|
|
106
|
+
decorateAll(items, [probe, probe])
|
|
107
|
+
expect(seen.sort()).toEqual([1, 1, 2, 2, 3, 3])
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
test('recurse:false decorator runs only at top level; recurse:true descends', () => {
|
|
111
|
+
const items: Decoratable[] = [{ id: 1, children: [{ id: 2 }, { id: 3 }] }]
|
|
112
|
+
const topOnlyHits: number[] = []
|
|
113
|
+
const everyHits: number[] = []
|
|
114
|
+
const topOnly: FieldDecorator<Decoratable> = {
|
|
115
|
+
field: 'top',
|
|
116
|
+
compute: (item) => {
|
|
117
|
+
topOnlyHits.push(item.id as number)
|
|
118
|
+
return true
|
|
119
|
+
},
|
|
120
|
+
recurse: false,
|
|
121
|
+
}
|
|
122
|
+
const every: FieldDecorator<Decoratable> = {
|
|
123
|
+
field: 'every',
|
|
124
|
+
compute: (item) => {
|
|
125
|
+
everyHits.push(item.id as number)
|
|
126
|
+
return true
|
|
127
|
+
},
|
|
128
|
+
}
|
|
129
|
+
decorateAll(items, [topOnly, every])
|
|
130
|
+
expect(topOnlyHits).toEqual([1])
|
|
131
|
+
expect(everyHits.sort()).toEqual([1, 2, 3])
|
|
132
|
+
expect(items[0].top).toBe(true)
|
|
133
|
+
const child = (items[0].children as Decoratable[])[0]
|
|
134
|
+
expect(child.top).toBeUndefined()
|
|
135
|
+
expect(child.every).toBe(true)
|
|
136
|
+
})
|
|
137
|
+
|
|
138
|
+
test('all decorators recurse:false → children untouched, walk skipped', () => {
|
|
139
|
+
const items: Decoratable[] = [{ id: 1, children: [{ id: 2 }] }]
|
|
140
|
+
const seen: number[] = []
|
|
141
|
+
const dec: FieldDecorator<Decoratable> = {
|
|
142
|
+
field: 'mark',
|
|
143
|
+
compute: (item) => {
|
|
144
|
+
seen.push(item.id as number)
|
|
145
|
+
return true
|
|
146
|
+
},
|
|
147
|
+
recurse: false,
|
|
148
|
+
}
|
|
149
|
+
decorateAll(items, [dec])
|
|
150
|
+
expect(seen).toEqual([1])
|
|
151
|
+
const child = (items[0].children as Decoratable[])[0]
|
|
152
|
+
expect(child.mark).toBeUndefined()
|
|
153
|
+
})
|
|
154
|
+
})
|
|
155
|
+
|
|
156
|
+
describe('decorateAsync', () => {
|
|
157
|
+
test('sets resolved value on single object', async () => {
|
|
158
|
+
const item: Decoratable = { id: 1 }
|
|
159
|
+
await decorateAsync(item, 'mark', async () => 'A')
|
|
160
|
+
expect(item.mark).toBe('A')
|
|
161
|
+
})
|
|
162
|
+
|
|
163
|
+
test('sets resolved value on every item in array', async () => {
|
|
164
|
+
const items: Decoratable[] = [{ id: 1 }, { id: 2 }, { id: 3 }]
|
|
165
|
+
await decorateAsync(items, 'mark', async (i) => i.id)
|
|
166
|
+
expect(items.map((i) => i.mark)).toEqual([1, 2, 3])
|
|
167
|
+
})
|
|
168
|
+
|
|
169
|
+
test('returns same reference it was given', async () => {
|
|
170
|
+
const items: Decoratable[] = [{ id: 1 }]
|
|
171
|
+
const result = await decorateAsync(items, 'mark', async () => true)
|
|
172
|
+
expect(result).toBe(items)
|
|
173
|
+
})
|
|
174
|
+
|
|
175
|
+
test('recurses children up to 3 levels deep', async () => {
|
|
176
|
+
const tree: Decoratable = {
|
|
177
|
+
id: 1,
|
|
178
|
+
children: [
|
|
179
|
+
{
|
|
180
|
+
id: 2,
|
|
181
|
+
children: [
|
|
182
|
+
{
|
|
183
|
+
id: 3,
|
|
184
|
+
children: [{ id: 4 }],
|
|
185
|
+
},
|
|
186
|
+
],
|
|
187
|
+
},
|
|
188
|
+
],
|
|
189
|
+
}
|
|
190
|
+
await decorateAsync(tree, 'visited', async () => true)
|
|
191
|
+
|
|
192
|
+
expect(tree.visited).toBe(true)
|
|
193
|
+
const lvl1 = (tree.children as Decoratable[])[0]
|
|
194
|
+
const lvl2 = (lvl1.children as Decoratable[])[0]
|
|
195
|
+
const lvl3 = (lvl2.children as Decoratable[])[0]
|
|
196
|
+
expect(lvl1.visited).toBe(true)
|
|
197
|
+
expect(lvl2.visited).toBe(true)
|
|
198
|
+
expect(lvl3.visited).toBeUndefined()
|
|
199
|
+
})
|
|
200
|
+
|
|
201
|
+
test('propagates rejection from compute', async () => {
|
|
202
|
+
const item: Decoratable = { id: 1 }
|
|
203
|
+
const failing = decorateAsync(item, 'mark', async () => {
|
|
204
|
+
throw new Error('boom')
|
|
205
|
+
})
|
|
206
|
+
await expect(failing).rejects.toThrow('boom')
|
|
207
|
+
})
|
|
208
|
+
})
|
|
209
|
+
|
|
210
|
+
describe('decorateAllAsync', () => {
|
|
211
|
+
test('applies every async decorator on a single walk', async () => {
|
|
212
|
+
const items: Decoratable[] = [{ id: 1, children: [{ id: 2 }] }]
|
|
213
|
+
const a: FieldDecoratorAsync<Decoratable> = {
|
|
214
|
+
field: 'a',
|
|
215
|
+
compute: async () => 'A',
|
|
216
|
+
}
|
|
217
|
+
const b: FieldDecoratorAsync<Decoratable> = {
|
|
218
|
+
field: 'b',
|
|
219
|
+
compute: async () => 'B',
|
|
220
|
+
}
|
|
221
|
+
await decorateAllAsync(items, [a, b])
|
|
222
|
+
|
|
223
|
+
expect(items[0].a).toBe('A')
|
|
224
|
+
expect(items[0].b).toBe('B')
|
|
225
|
+
const child = (items[0].children as Decoratable[])[0]
|
|
226
|
+
expect(child.a).toBe('A')
|
|
227
|
+
expect(child.b).toBe('B')
|
|
228
|
+
})
|
|
229
|
+
|
|
230
|
+
test('empty decorator list is a no-op', async () => {
|
|
231
|
+
const items: Decoratable[] = [{ id: 1 }]
|
|
232
|
+
await decorateAllAsync(items, [])
|
|
233
|
+
expect(Object.keys(items[0])).toEqual(['id'])
|
|
234
|
+
})
|
|
235
|
+
|
|
236
|
+
test('runs decorators for one item in parallel', async () => {
|
|
237
|
+
const items: Decoratable[] = [{ id: 1 }]
|
|
238
|
+
let active = 0
|
|
239
|
+
let peak = 0
|
|
240
|
+
const tracker = async () => {
|
|
241
|
+
active++
|
|
242
|
+
peak = Math.max(peak, active)
|
|
243
|
+
await new Promise((r) => setTimeout(r, 5))
|
|
244
|
+
active--
|
|
245
|
+
return true
|
|
246
|
+
}
|
|
247
|
+
await decorateAllAsync(items, [
|
|
248
|
+
{ field: 'a', compute: tracker },
|
|
249
|
+
{ field: 'b', compute: tracker },
|
|
250
|
+
{ field: 'c', compute: tracker },
|
|
251
|
+
])
|
|
252
|
+
expect(peak).toBe(3)
|
|
253
|
+
})
|
|
254
|
+
|
|
255
|
+
test('runs items in parallel', async () => {
|
|
256
|
+
const items: Decoratable[] = [{ id: 1 }, { id: 2 }, { id: 3 }]
|
|
257
|
+
let active = 0
|
|
258
|
+
let peak = 0
|
|
259
|
+
const tracker = async () => {
|
|
260
|
+
active++
|
|
261
|
+
peak = Math.max(peak, active)
|
|
262
|
+
await new Promise((r) => setTimeout(r, 5))
|
|
263
|
+
active--
|
|
264
|
+
return true
|
|
265
|
+
}
|
|
266
|
+
await decorateAllAsync(items, [{ field: 'mark', compute: tracker }])
|
|
267
|
+
expect(peak).toBe(3)
|
|
268
|
+
})
|
|
269
|
+
|
|
270
|
+
test('runs children in parallel', async () => {
|
|
271
|
+
const items: Decoratable[] = [
|
|
272
|
+
{ id: 1, children: [{ id: 2 }, { id: 3 }, { id: 4 }] },
|
|
273
|
+
]
|
|
274
|
+
let active = 0
|
|
275
|
+
let peak = 0
|
|
276
|
+
const tracker = async () => {
|
|
277
|
+
active++
|
|
278
|
+
peak = Math.max(peak, active)
|
|
279
|
+
await new Promise((r) => setTimeout(r, 5))
|
|
280
|
+
active--
|
|
281
|
+
return true
|
|
282
|
+
}
|
|
283
|
+
await decorateAllAsync(items, [{ field: 'mark', compute: tracker }])
|
|
284
|
+
expect(peak).toBeGreaterThanOrEqual(3)
|
|
285
|
+
})
|
|
286
|
+
|
|
287
|
+
test('visits each item exactly once with multiple decorators', async () => {
|
|
288
|
+
const items: Decoratable[] = [{ id: 1, children: [{ id: 2 }, { id: 3 }] }]
|
|
289
|
+
const seen: number[] = []
|
|
290
|
+
const probe: FieldDecoratorAsync<Decoratable> = {
|
|
291
|
+
field: 'probe',
|
|
292
|
+
compute: async (item) => {
|
|
293
|
+
seen.push(item.id as number)
|
|
294
|
+
return true
|
|
295
|
+
},
|
|
296
|
+
}
|
|
297
|
+
await decorateAllAsync(items, [probe, probe])
|
|
298
|
+
expect(seen.sort()).toEqual([1, 1, 2, 2, 3, 3])
|
|
299
|
+
})
|
|
300
|
+
|
|
301
|
+
test('rejects when any decorator throws', async () => {
|
|
302
|
+
const items: Decoratable[] = [{ id: 1 }]
|
|
303
|
+
const ok: FieldDecoratorAsync<Decoratable> = {
|
|
304
|
+
field: 'ok',
|
|
305
|
+
compute: async () => true,
|
|
306
|
+
}
|
|
307
|
+
const bad: FieldDecoratorAsync<Decoratable> = {
|
|
308
|
+
field: 'bad',
|
|
309
|
+
compute: async () => {
|
|
310
|
+
throw new Error('nope')
|
|
311
|
+
},
|
|
312
|
+
}
|
|
313
|
+
await expect(decorateAllAsync(items, [ok, bad])).rejects.toThrow('nope')
|
|
314
|
+
})
|
|
315
|
+
|
|
316
|
+
test('passes visited item to compute', async () => {
|
|
317
|
+
const item: Decoratable = { id: 7, label: 'x' }
|
|
318
|
+
const compute = jest.fn(async () => 'ok')
|
|
319
|
+
await decorateAllAsync(item, [{ field: 'mark', compute }])
|
|
320
|
+
expect(compute).toHaveBeenCalledTimes(1)
|
|
321
|
+
expect(compute).toHaveBeenCalledWith(item)
|
|
322
|
+
})
|
|
323
|
+
|
|
324
|
+
test('recurse:false decorator runs only at top level; recurse:true descends', async () => {
|
|
325
|
+
const items: Decoratable[] = [{ id: 1, children: [{ id: 2 }, { id: 3 }] }]
|
|
326
|
+
const topOnlyHits: number[] = []
|
|
327
|
+
const everyHits: number[] = []
|
|
328
|
+
const topOnly: FieldDecoratorAsync<Decoratable> = {
|
|
329
|
+
field: 'top',
|
|
330
|
+
compute: async (item) => {
|
|
331
|
+
topOnlyHits.push(item.id as number)
|
|
332
|
+
return true
|
|
333
|
+
},
|
|
334
|
+
recurse: false,
|
|
335
|
+
}
|
|
336
|
+
const every: FieldDecoratorAsync<Decoratable> = {
|
|
337
|
+
field: 'every',
|
|
338
|
+
compute: async (item) => {
|
|
339
|
+
everyHits.push(item.id as number)
|
|
340
|
+
return true
|
|
341
|
+
},
|
|
342
|
+
}
|
|
343
|
+
await decorateAllAsync(items, [topOnly, every])
|
|
344
|
+
expect(topOnlyHits).toEqual([1])
|
|
345
|
+
expect(everyHits.sort()).toEqual([1, 2, 3])
|
|
346
|
+
const child = (items[0].children as Decoratable[])[0]
|
|
347
|
+
expect(child.top).toBeUndefined()
|
|
348
|
+
expect(child.every).toBe(true)
|
|
349
|
+
})
|
|
350
|
+
|
|
351
|
+
test('all async decorators recurse:false → children untouched', async () => {
|
|
352
|
+
const items: Decoratable[] = [{ id: 1, children: [{ id: 2 }] }]
|
|
353
|
+
const seen: number[] = []
|
|
354
|
+
const dec: FieldDecoratorAsync<Decoratable> = {
|
|
355
|
+
field: 'mark',
|
|
356
|
+
compute: async (item) => {
|
|
357
|
+
seen.push(item.id as number)
|
|
358
|
+
return true
|
|
359
|
+
},
|
|
360
|
+
recurse: false,
|
|
361
|
+
}
|
|
362
|
+
await decorateAllAsync(items, [dec])
|
|
363
|
+
expect(seen).toEqual([1])
|
|
364
|
+
const child = (items[0].children as Decoratable[])[0]
|
|
365
|
+
expect(child.mark).toBeUndefined()
|
|
366
|
+
})
|
|
367
|
+
})
|
|
368
|
+
})
|