arocapi 4.0.1 → 5.0.0-beta.10

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.
Files changed (242) hide show
  1. package/LICENSE.md +1 -1
  2. package/README.md +37 -781
  3. package/dist/app.d.ts +38 -21
  4. package/dist/app.js +63 -45
  5. package/dist/app.js.map +1 -1
  6. package/dist/routes/capabilities.d.ts +7 -0
  7. package/dist/routes/capabilities.js +13 -0
  8. package/dist/routes/capabilities.js.map +1 -0
  9. package/dist/routes/entities.d.ts +5 -2
  10. package/dist/routes/entities.js +38 -58
  11. package/dist/routes/entities.js.map +1 -1
  12. package/dist/routes/entity.d.ts +7 -2
  13. package/dist/routes/entity.js +14 -30
  14. package/dist/routes/entity.js.map +1 -1
  15. package/dist/routes/file.d.ts +8 -2
  16. package/dist/routes/file.js +38 -73
  17. package/dist/routes/file.js.map +1 -1
  18. package/dist/routes/files.d.ts +5 -2
  19. package/dist/routes/files.js +25 -40
  20. package/dist/routes/files.js.map +1 -1
  21. package/dist/routes/metadata.d.ts +15 -0
  22. package/dist/routes/metadata.js +38 -0
  23. package/dist/routes/metadata.js.map +1 -0
  24. package/dist/routes/search.d.ts +8 -7
  25. package/dist/routes/search.js +91 -120
  26. package/dist/routes/search.js.map +1 -1
  27. package/dist/search/advanced.d.ts +8 -0
  28. package/dist/search/advanced.js +198 -0
  29. package/dist/search/advanced.js.map +1 -0
  30. package/dist/search/backend.d.ts +95 -0
  31. package/dist/search/backend.js +5 -0
  32. package/dist/search/backend.js.map +1 -0
  33. package/dist/search/fields.d.ts +63 -0
  34. package/dist/search/fields.js +203 -0
  35. package/dist/search/fields.js.map +1 -0
  36. package/dist/search/opensearch/advanced.d.ts +4 -0
  37. package/dist/search/opensearch/advanced.js +28 -0
  38. package/dist/search/opensearch/advanced.js.map +1 -0
  39. package/dist/search/opensearch/opensearch.d.ts +16 -0
  40. package/dist/search/opensearch/opensearch.js +187 -0
  41. package/dist/search/opensearch/opensearch.js.map +1 -0
  42. package/dist/search/opensearch/visibility.d.ts +3 -0
  43. package/dist/search/opensearch/visibility.js +27 -0
  44. package/dist/search/opensearch/visibility.js.map +1 -0
  45. package/dist/search/postgres/advanced.d.ts +7 -0
  46. package/dist/search/postgres/advanced.js +54 -0
  47. package/dist/search/postgres/advanced.js.map +1 -0
  48. package/dist/search/postgres/facets.d.ts +2 -0
  49. package/dist/search/postgres/facets.js +27 -0
  50. package/dist/search/postgres/facets.js.map +1 -0
  51. package/dist/search/postgres/geo.d.ts +6 -0
  52. package/dist/search/postgres/geo.js +42 -0
  53. package/dist/search/postgres/geo.js.map +1 -0
  54. package/dist/search/postgres/postgres.d.ts +19 -0
  55. package/dist/search/postgres/postgres.js +157 -0
  56. package/dist/search/postgres/postgres.js.map +1 -0
  57. package/dist/search/postgres/values.d.ts +8 -0
  58. package/dist/search/postgres/values.js +20 -0
  59. package/dist/search/postgres/values.js.map +1 -0
  60. package/dist/search/postgres/visibility.d.ts +4 -0
  61. package/dist/search/postgres/visibility.js +38 -0
  62. package/dist/search/postgres/visibility.js.map +1 -0
  63. package/dist/search/requestFilters.d.ts +39 -0
  64. package/dist/search/requestFilters.js +125 -0
  65. package/dist/search/requestFilters.js.map +1 -0
  66. package/dist/search/visibility.d.ts +51 -0
  67. package/dist/search/visibility.js +100 -0
  68. package/dist/search/visibility.js.map +1 -0
  69. package/dist/storage/backend.d.ts +58 -0
  70. package/dist/storage/backend.js +2 -0
  71. package/dist/storage/backend.js.map +1 -0
  72. package/dist/storage/deliver.d.ts +12 -0
  73. package/dist/storage/deliver.js +34 -0
  74. package/dist/storage/deliver.js.map +1 -0
  75. package/dist/storage/directory/directory.d.ts +6 -0
  76. package/dist/storage/directory/directory.js +24 -0
  77. package/dist/storage/directory/directory.js.map +1 -0
  78. package/dist/storage/entity-meta/entity-meta.d.ts +5 -0
  79. package/dist/storage/entity-meta/entity-meta.js +14 -0
  80. package/dist/storage/entity-meta/entity-meta.js.map +1 -0
  81. package/dist/storage/location.d.ts +19 -0
  82. package/dist/storage/location.js +36 -0
  83. package/dist/storage/location.js.map +1 -0
  84. package/dist/storage/ocfl/ocfl.d.ts +6 -0
  85. package/dist/storage/ocfl/ocfl.js +45 -0
  86. package/dist/storage/ocfl/ocfl.js.map +1 -0
  87. package/dist/storage/results.d.ts +7 -0
  88. package/dist/storage/results.js +22 -0
  89. package/dist/storage/results.js.map +1 -0
  90. package/dist/storage/s3/s3.d.ts +10 -0
  91. package/dist/storage/s3/s3.js +47 -0
  92. package/dist/storage/s3/s3.js.map +1 -0
  93. package/dist/transformers/default.d.ts +11 -22
  94. package/dist/transformers/default.js +41 -15
  95. package/dist/transformers/default.js.map +1 -1
  96. package/dist/types/access.d.ts +9 -0
  97. package/dist/types/access.js +2 -0
  98. package/dist/types/access.js.map +1 -0
  99. package/dist/types/context.d.ts +6 -0
  100. package/dist/types/context.js +2 -0
  101. package/dist/types/context.js.map +1 -0
  102. package/dist/types/transformers.d.ts +4 -10
  103. package/dist/types/visibility.d.ts +6 -0
  104. package/dist/types/visibility.js +2 -0
  105. package/dist/types/visibility.js.map +1 -0
  106. package/dist/utils/access.d.ts +17 -0
  107. package/dist/utils/access.js +41 -0
  108. package/dist/utils/access.js.map +1 -0
  109. package/dist/utils/errors.d.ts +19 -10
  110. package/dist/utils/errors.js +44 -8
  111. package/dist/utils/errors.js.map +1 -1
  112. package/dist/utils/headers.d.ts +1 -1
  113. package/dist/utils/headers.js +10 -1
  114. package/dist/utils/headers.js.map +1 -1
  115. package/dist/utils/validation.d.ts +2 -0
  116. package/dist/utils/validation.js +8 -0
  117. package/dist/utils/validation.js.map +1 -0
  118. package/dist/utils/visibility.d.ts +18 -0
  119. package/dist/utils/visibility.js +16 -0
  120. package/dist/utils/visibility.js.map +1 -0
  121. package/package.json +84 -45
  122. package/prisma/models/entity.prisma +9 -8
  123. package/prisma/models/entitySearch.prisma +21 -0
  124. package/prisma/models/file.prisma +3 -3
  125. package/src/app.ts +169 -102
  126. package/src/routes/capabilities.ts +22 -0
  127. package/src/routes/entities.ts +49 -68
  128. package/src/routes/entity.ts +22 -36
  129. package/src/routes/file.ts +54 -104
  130. package/src/routes/files.ts +32 -48
  131. package/src/routes/metadata.ts +67 -0
  132. package/src/routes/search.ts +132 -152
  133. package/src/search/advanced.ts +258 -0
  134. package/src/search/backend.ts +91 -0
  135. package/src/search/fields.ts +320 -0
  136. package/src/search/opensearch/advanced.ts +36 -0
  137. package/src/search/opensearch/opensearch.ts +290 -0
  138. package/src/search/opensearch/visibility.ts +38 -0
  139. package/src/search/postgres/advanced.ts +76 -0
  140. package/src/search/postgres/facets.ts +34 -0
  141. package/src/search/postgres/geo.ts +58 -0
  142. package/src/search/postgres/postgres.ts +232 -0
  143. package/src/search/postgres/values.ts +33 -0
  144. package/src/search/postgres/visibility.ts +54 -0
  145. package/src/search/requestFilters.ts +178 -0
  146. package/src/search/visibility.ts +193 -0
  147. package/src/storage/backend.ts +75 -0
  148. package/src/storage/deliver.ts +65 -0
  149. package/src/storage/directory/directory.ts +39 -0
  150. package/src/storage/entity-meta/entity-meta.ts +25 -0
  151. package/src/storage/location.ts +78 -0
  152. package/src/storage/ocfl/ocfl.ts +71 -0
  153. package/src/storage/results.ts +32 -0
  154. package/src/storage/s3/s3.ts +79 -0
  155. package/src/transformers/default.ts +115 -85
  156. package/src/types/access.ts +25 -0
  157. package/src/types/context.ts +15 -0
  158. package/src/types/transformers.ts +8 -34
  159. package/src/types/visibility.ts +21 -0
  160. package/src/utils/access.ts +80 -0
  161. package/src/utils/errors.ts +68 -11
  162. package/src/utils/headers.ts +22 -2
  163. package/src/utils/validation.ts +11 -0
  164. package/src/utils/visibility.ts +56 -0
  165. package/dist/app.test.d.ts +0 -1
  166. package/dist/app.test.js +0 -89
  167. package/dist/app.test.js.map +0 -1
  168. package/dist/express.d.ts +0 -4
  169. package/dist/express.js +0 -14
  170. package/dist/express.js.map +0 -1
  171. package/dist/index.dev.d.ts +0 -1
  172. package/dist/index.dev.js +0 -156
  173. package/dist/index.dev.js.map +0 -1
  174. package/dist/routes/crate.d.ts +0 -9
  175. package/dist/routes/crate.js +0 -82
  176. package/dist/routes/crate.js.map +0 -1
  177. package/dist/routes/crate.test.d.ts +0 -1
  178. package/dist/routes/crate.test.js +0 -342
  179. package/dist/routes/crate.test.js.map +0 -1
  180. package/dist/routes/entities.test.d.ts +0 -1
  181. package/dist/routes/entities.test.js +0 -273
  182. package/dist/routes/entities.test.js.map +0 -1
  183. package/dist/routes/entity.test.d.ts +0 -1
  184. package/dist/routes/entity.test.js +0 -130
  185. package/dist/routes/entity.test.js.map +0 -1
  186. package/dist/routes/file.test.d.ts +0 -1
  187. package/dist/routes/file.test.js +0 -315
  188. package/dist/routes/file.test.js.map +0 -1
  189. package/dist/routes/files.test.d.ts +0 -1
  190. package/dist/routes/files.test.js +0 -222
  191. package/dist/routes/files.test.js.map +0 -1
  192. package/dist/routes/search.test.d.ts +0 -1
  193. package/dist/routes/search.test.js +0 -780
  194. package/dist/routes/search.test.js.map +0 -1
  195. package/dist/test/helpers/fastify.d.ts +0 -9
  196. package/dist/test/helpers/fastify.js +0 -18
  197. package/dist/test/helpers/fastify.js.map +0 -1
  198. package/dist/test/integration.setup.d.ts +0 -29
  199. package/dist/test/integration.setup.js +0 -188
  200. package/dist/test/integration.setup.js.map +0 -1
  201. package/dist/test/integration.test.d.ts +0 -1
  202. package/dist/test/integration.test.js +0 -396
  203. package/dist/test/integration.test.js.map +0 -1
  204. package/dist/transformers/default.test.d.ts +0 -1
  205. package/dist/transformers/default.test.js +0 -163
  206. package/dist/transformers/default.test.js.map +0 -1
  207. package/dist/transformers/transformer.test.d.ts +0 -1
  208. package/dist/transformers/transformer.test.js +0 -187
  209. package/dist/transformers/transformer.test.js.map +0 -1
  210. package/dist/types/fileHandlers.d.ts +0 -41
  211. package/dist/types/fileHandlers.js +0 -2
  212. package/dist/types/fileHandlers.js.map +0 -1
  213. package/dist/utils/errors.test.d.ts +0 -1
  214. package/dist/utils/errors.test.js +0 -66
  215. package/dist/utils/errors.test.js.map +0 -1
  216. package/dist/utils/queryBuilder.d.ts +0 -35
  217. package/dist/utils/queryBuilder.js +0 -98
  218. package/dist/utils/queryBuilder.js.map +0 -1
  219. package/prisma.config.ts +0 -9
  220. package/src/app.test.ts +0 -119
  221. package/src/express.ts +0 -21
  222. package/src/index.dev.ts +0 -200
  223. package/src/reset.d.ts +0 -1
  224. package/src/routes/__snapshots__/entities.test.ts.snap +0 -96
  225. package/src/routes/__snapshots__/entity.test.ts.snap +0 -74
  226. package/src/routes/__snapshots__/search.test.ts.snap +0 -165
  227. package/src/routes/crate.test.ts +0 -423
  228. package/src/routes/crate.ts +0 -123
  229. package/src/routes/entities.test.ts +0 -320
  230. package/src/routes/entity.test.ts +0 -160
  231. package/src/routes/file.test.ts +0 -395
  232. package/src/routes/files.test.ts +0 -278
  233. package/src/routes/search.test.ts +0 -903
  234. package/src/test/__snapshots__/integration.test.ts.snap +0 -18
  235. package/src/test/helpers/fastify.ts +0 -27
  236. package/src/test/integration.setup.ts +0 -210
  237. package/src/test/integration.test.ts +0 -482
  238. package/src/transformers/default.test.ts +0 -184
  239. package/src/transformers/transformer.test.ts +0 -252
  240. package/src/types/fileHandlers.ts +0 -65
  241. package/src/utils/errors.test.ts +0 -75
  242. package/src/utils/queryBuilder.ts +0 -132
@@ -1,903 +0,0 @@
1
- import { afterEach, beforeEach, describe, expect, it } from 'vitest';
2
-
3
- import { fastify, fastifyAfter, fastifyBefore, opensearch, prisma } from '../test/helpers/fastify.js';
4
- import { AllPublicAccessTransformer } from '../transformers/default.js';
5
- import type { StandardErrorResponse } from '../utils/errors.js';
6
- import searchRoute from './search.js';
7
-
8
- describe('Search Route', () => {
9
- beforeEach(async () => {
10
- await fastifyBefore();
11
- await fastify.register(searchRoute, { prisma, opensearch, accessTransformer: AllPublicAccessTransformer });
12
- });
13
-
14
- afterEach(async () => {
15
- await fastifyAfter();
16
- });
17
-
18
- describe('POST /search', () => {
19
- it('should perform basic search successfully', async () => {
20
- const mockEntities = [
21
- {
22
- id: 'http://example.com/entity/1',
23
- name: 'Test Entity 1',
24
- description: 'A test entity',
25
- entityType: 'http://pcdm.org/models#Collection',
26
- memberOf: null,
27
- rootCollection: null,
28
- metadataLicenseId: null,
29
- contentLicenseId: null,
30
- createdAt: new Date('2024-01-01'),
31
- updatedAt: new Date('2024-01-01'),
32
- },
33
- {
34
- id: 'http://example.com/entity/2',
35
- name: 'Test Entity 2',
36
- description: 'Another test entity',
37
- entityType: 'http://pcdm.org/models#Object',
38
- memberOf: null,
39
- rootCollection: null,
40
- metadataLicenseId: null,
41
- contentLicenseId: null,
42
- createdAt: new Date('2024-01-01'),
43
- updatedAt: new Date('2024-01-01'),
44
- },
45
- ];
46
-
47
- const mockSearchResponse = {
48
- body: {
49
- took: 10,
50
- hits: {
51
- total: { value: 2 },
52
- hits: [
53
- {
54
- _score: 1.5,
55
- _source: {
56
- id: 'http://example.com/entity/1',
57
- },
58
- highlight: {
59
- name: ['<em>Test</em> Entity 1'],
60
- },
61
- },
62
- {
63
- _score: 1.2,
64
- _source: {
65
- id: 'http://example.com/entity/2',
66
- },
67
- highlight: {
68
- description: ['Another <em>test</em> entity'],
69
- },
70
- },
71
- ],
72
- },
73
- aggregations: {
74
- entityType: {
75
- buckets: [
76
- { key: 'http://pcdm.org/models#Collection', doc_count: 1 },
77
- { key: 'http://pcdm.org/models#Object', doc_count: 1 },
78
- ],
79
- },
80
- },
81
- },
82
- };
83
-
84
- // @ts-expect-error TS is looking at the wrong function signature
85
- opensearch.search.mockResolvedValue(mockSearchResponse);
86
- // @ts-expect-error TS is looking at the wrong function signature
87
- prisma.entity.findMany.mockResolvedValue(mockEntities);
88
-
89
- const response = await fastify.inject({
90
- method: 'POST',
91
- url: '/search',
92
- payload: {
93
- query: 'test',
94
- searchType: 'basic',
95
- },
96
- });
97
-
98
- expect(response.statusCode).toBe(200);
99
- const body = JSON.parse(response.body);
100
- expect(body).toMatchSnapshot();
101
-
102
- // Verify database was queried with correct ids
103
- expect(prisma.entity.findMany).toHaveBeenCalledWith({
104
- where: {
105
- id: {
106
- in: ['http://example.com/entity/1', 'http://example.com/entity/2'],
107
- },
108
- },
109
- include: { file: { select: { id: true } } },
110
- });
111
-
112
- expect(opensearch.search).toHaveBeenCalledWith({
113
- index: 'entities',
114
- body: {
115
- query: {
116
- bool: {
117
- must: [
118
- {
119
- multi_match: {
120
- query: 'test',
121
- fields: ['name^2', 'description'],
122
- type: 'best_fields',
123
- fuzziness: 'AUTO',
124
- zero_terms_query: 'all',
125
- },
126
- },
127
- ],
128
- filter: [],
129
- },
130
- },
131
- aggs: {
132
- inLanguage: { terms: { field: 'inLanguage.keyword', size: 20 } },
133
- mediaType: { terms: { field: 'mediaType.keyword', size: 20 } },
134
- communicationMode: { terms: { field: 'communicationMode.keyword', size: 20 } },
135
- entityType: { terms: { field: 'entityType.keyword', size: 20 } },
136
- },
137
- highlight: {
138
- fields: {
139
- name: {},
140
- description: {},
141
- },
142
- },
143
- sort: undefined,
144
- from: 0,
145
- size: 100,
146
- },
147
- });
148
- });
149
-
150
- it('should perform advanced search with query string', async () => {
151
- const mockSearchResponse = {
152
- body: {
153
- took: 5,
154
- hits: {
155
- total: { value: 0 },
156
- hits: [],
157
- },
158
- aggregations: {},
159
- },
160
- };
161
-
162
- // @ts-expect-error TS is looking at the wrong function signature
163
- opensearch.search.mockResolvedValue(mockSearchResponse);
164
- prisma.entity.findMany.mockResolvedValue([]);
165
-
166
- const response = await fastify.inject({
167
- method: 'POST',
168
- url: '/search',
169
- payload: {
170
- query: 'name:test AND description:entity',
171
- searchType: 'advanced',
172
- },
173
- });
174
-
175
- expect(response.statusCode).toBe(200);
176
- expect(opensearch.search).toHaveBeenCalledWith({
177
- index: 'entities',
178
- body: {
179
- query: {
180
- bool: {
181
- must: [
182
- {
183
- query_string: {
184
- query: 'name:test AND description:entity',
185
- fields: ['name^2', 'description'],
186
- default_operator: 'AND',
187
- },
188
- },
189
- ],
190
- filter: [],
191
- },
192
- },
193
- aggs: {
194
- inLanguage: { terms: { field: 'inLanguage.keyword', size: 20 } },
195
- mediaType: { terms: { field: 'mediaType.keyword', size: 20 } },
196
- communicationMode: { terms: { field: 'communicationMode.keyword', size: 20 } },
197
- entityType: { terms: { field: 'entityType.keyword', size: 20 } },
198
- },
199
- highlight: {
200
- fields: {
201
- name: {},
202
- description: {},
203
- },
204
- },
205
- sort: undefined,
206
- from: 0,
207
- size: 100,
208
- },
209
- });
210
- });
211
-
212
- it('should apply filters correctly', async () => {
213
- const mockSearchResponse = {
214
- body: {
215
- took: 8,
216
- hits: {
217
- total: { value: 0 },
218
- hits: [],
219
- },
220
- aggregations: {},
221
- },
222
- };
223
-
224
- // @ts-expect-error TS is looking at the wrong function signature
225
- opensearch.search.mockResolvedValue(mockSearchResponse);
226
- prisma.entity.findMany.mockResolvedValue([]);
227
-
228
- const response = await fastify.inject({
229
- method: 'POST',
230
- url: '/search',
231
- payload: {
232
- query: 'test',
233
- filters: {
234
- entityType: ['http://pcdm.org/models#Collection'],
235
- inLanguage: ['en', 'fr'],
236
- },
237
- },
238
- });
239
-
240
- expect(response.statusCode).toBe(200);
241
- const expectedFilters = [
242
- {
243
- terms: {
244
- entityType: ['http://pcdm.org/models#Collection'],
245
- },
246
- },
247
- {
248
- terms: {
249
- inLanguage: ['en', 'fr'],
250
- },
251
- },
252
- ];
253
-
254
- expect(opensearch.search).toHaveBeenCalledWith(
255
- expect.objectContaining({
256
- body: expect.objectContaining({
257
- query: expect.objectContaining({
258
- bool: expect.objectContaining({
259
- filter: expectedFilters,
260
- }),
261
- }),
262
- }),
263
- }),
264
- );
265
- });
266
-
267
- it('should handle geospatial search with bounding box', async () => {
268
- const mockSearchResponse = {
269
- body: {
270
- took: 12,
271
- hits: {
272
- total: { value: 0 },
273
- hits: [],
274
- },
275
- aggregations: {
276
- geohash_grid: {
277
- buckets: [
278
- { key: 'gbsuv', doc_count: 3 },
279
- { key: 'gbsvb', doc_count: 1 },
280
- ],
281
- },
282
- },
283
- },
284
- };
285
-
286
- // @ts-expect-error TS is looking at the wrong function signature
287
- opensearch.search.mockResolvedValue(mockSearchResponse);
288
- prisma.entity.findMany.mockResolvedValue([]);
289
-
290
- const response = await fastify.inject({
291
- method: 'POST',
292
- url: '/search',
293
- payload: {
294
- query: 'test',
295
- boundingBox: {
296
- topRight: { lat: 51.5, lng: 0.1 },
297
- bottomLeft: { lat: 51.4, lng: 0.0 },
298
- },
299
- geohashPrecision: 5,
300
- },
301
- });
302
-
303
- expect(response.statusCode).toBe(200);
304
- const body = JSON.parse(response.body) as { geohashGrid: Record<string, number> };
305
- expect(body.geohashGrid).toEqual({
306
- gbsuv: 3,
307
- gbsvb: 1,
308
- });
309
-
310
- expect(opensearch.search).toHaveBeenCalledWith(
311
- expect.objectContaining({
312
- body: expect.objectContaining({
313
- query: expect.objectContaining({
314
- bool: expect.objectContaining({
315
- filter: [
316
- {
317
- geo_bounding_box: {
318
- location: {
319
- top_left: { lat: 51.5, lon: 0.0 },
320
- bottom_right: { lat: 51.4, lon: 0.1 },
321
- },
322
- },
323
- },
324
- ],
325
- }),
326
- }),
327
- aggs: expect.objectContaining({
328
- geohash_grid: {
329
- geohash_grid: {
330
- field: 'location',
331
- precision: 5,
332
- bounds: {
333
- top_left: { lat: 51.5, lon: 0.0 },
334
- bottom_right: { lat: 51.4, lon: 0.1 },
335
- },
336
- },
337
- },
338
- }),
339
- }),
340
- }),
341
- );
342
- });
343
-
344
- it('should handle pagination and sorting', async () => {
345
- const mockSearchResponse = {
346
- body: {
347
- took: 6,
348
- hits: {
349
- total: { value: 0 },
350
- hits: [],
351
- },
352
- aggregations: {},
353
- },
354
- };
355
-
356
- // @ts-expect-error TS is looking at the wrong function signature
357
- opensearch.search.mockResolvedValue(mockSearchResponse);
358
- prisma.entity.findMany.mockResolvedValue([]);
359
-
360
- const response = await fastify.inject({
361
- method: 'POST',
362
- url: '/search',
363
- payload: {
364
- query: 'test',
365
- limit: 50,
366
- offset: 20,
367
- sort: 'name',
368
- order: 'desc',
369
- },
370
- });
371
-
372
- expect(response.statusCode).toBe(200);
373
- expect(opensearch.search).toHaveBeenCalledWith(
374
- expect.objectContaining({
375
- body: expect.objectContaining({
376
- from: 20,
377
- size: 50,
378
- sort: [{ 'name.keyword': 'desc' }],
379
- }),
380
- }),
381
- );
382
- });
383
-
384
- it('should return 422 for malformed opensearch query', async () => {
385
- const opensearchError = Object.assign(new Error('parsing_exception'), { statusCode: 400 });
386
- opensearch.search.mockRejectedValue(opensearchError);
387
-
388
- const response = await fastify.inject({
389
- method: 'POST',
390
- url: '/search',
391
- payload: {
392
- query: 'test',
393
- searchType: 'advanced',
394
- },
395
- });
396
-
397
- expect(response.statusCode).toBe(422);
398
- const body = JSON.parse(response.body) as StandardErrorResponse;
399
- expect(body.error.code).toBe('INVALID_REQUEST');
400
- });
401
-
402
- it('should handle opensearch errors', async () => {
403
- opensearch.search.mockRejectedValue(new Error('OpenSearch connection failed'));
404
-
405
- const response = await fastify.inject({
406
- method: 'POST',
407
- url: '/search',
408
- payload: {
409
- query: 'test',
410
- },
411
- });
412
-
413
- expect(response.statusCode).toBe(500);
414
- const body = JSON.parse(response.body);
415
- expect(body).toMatchSnapshot();
416
- });
417
-
418
- it('should return 422 for malformed opensearch query', async () => {
419
- const error = new Error('parsing_exception') as Error & { statusCode: number };
420
- error.statusCode = 400;
421
- opensearch.search.mockRejectedValue(error);
422
-
423
- const response = await fastify.inject({
424
- method: 'POST',
425
- url: '/search',
426
- payload: {
427
- query: 'test',
428
- },
429
- });
430
-
431
- expect(response.statusCode).toBe(422);
432
- const body = JSON.parse(response.body) as StandardErrorResponse;
433
- expect(body.error.code).toBe('INVALID_REQUEST');
434
- expect(body.error.message).toBe('parsing_exception');
435
- });
436
-
437
- it('should validate required query parameter', async () => {
438
- const response = await fastify.inject({
439
- method: 'POST',
440
- url: '/search',
441
- payload: {},
442
- });
443
-
444
- expect(response.statusCode).toBe(400);
445
- });
446
-
447
- it('should skip entities not found in database and log warning', async () => {
448
- const mockEntities = [
449
- {
450
- id: 'http://example.com/entity/1',
451
- name: 'Test Entity 1',
452
- description: 'A test entity',
453
- entityType: 'http://pcdm.org/models#Collection',
454
- memberOf: null,
455
- rootCollection: null,
456
- metadataLicenseId: null,
457
- contentLicenseId: null,
458
- createdAt: new Date('2024-01-01'),
459
- updatedAt: new Date('2024-01-01'),
460
- },
461
- // Entity 2 is missing from database
462
- ];
463
-
464
- const mockSearchResponse = {
465
- body: {
466
- took: 10,
467
- hits: {
468
- total: { value: 2 },
469
- hits: [
470
- {
471
- _score: 1.5,
472
- _source: {
473
- id: 'http://example.com/entity/1',
474
- },
475
- highlight: {},
476
- },
477
- {
478
- _score: 1.2,
479
- _source: {
480
- id: 'http://example.com/entity/2',
481
- },
482
- highlight: {},
483
- },
484
- ],
485
- },
486
- aggregations: {},
487
- },
488
- };
489
-
490
- // @ts-expect-error TS is looking at the wrong function signature
491
- opensearch.search.mockResolvedValue(mockSearchResponse);
492
- // @ts-expect-error TS is looking at the wrong function signature
493
- prisma.entity.findMany.mockResolvedValue(mockEntities);
494
-
495
- const response = await fastify.inject({
496
- method: 'POST',
497
- url: '/search',
498
- payload: {
499
- query: 'test',
500
- },
501
- });
502
-
503
- expect(response.statusCode).toBe(200);
504
- const body = JSON.parse(response.body);
505
-
506
- expect(body).toMatchSnapshot();
507
- });
508
-
509
- it('should handle missing id in search hit', async () => {
510
- const mockSearchResponse = {
511
- body: {
512
- took: 5,
513
- hits: {
514
- total: { value: 1 },
515
- hits: [
516
- {
517
- _score: 1.5,
518
- _source: {
519
- // Missing id
520
- },
521
- },
522
- ],
523
- },
524
- aggregations: {},
525
- },
526
- };
527
-
528
- // @ts-expect-error TS is looking at the wrong function signature
529
- opensearch.search.mockResolvedValue(mockSearchResponse);
530
- prisma.entity.findMany.mockResolvedValue([]);
531
-
532
- const response = await fastify.inject({
533
- method: 'POST',
534
- url: '/search',
535
- payload: {
536
- query: 'test',
537
- },
538
- });
539
-
540
- expect(response.statusCode).toBe(500);
541
- console.log(response.body);
542
- });
543
-
544
- it('should handle id explicitly set to undefined', async () => {
545
- const mockSearchResponse = {
546
- body: {
547
- took: 5,
548
- hits: {
549
- total: { value: 1 },
550
- hits: [
551
- {
552
- _score: 1.5,
553
- _source: {
554
- id: undefined,
555
- name: 'Test Entity',
556
- },
557
- },
558
- ],
559
- },
560
- aggregations: {},
561
- },
562
- };
563
-
564
- // @ts-expect-error TS is looking at the wrong function signature
565
- opensearch.search.mockResolvedValue(mockSearchResponse);
566
-
567
- const response = await fastify.inject({
568
- method: 'POST',
569
- url: '/search',
570
- payload: {
571
- query: 'test',
572
- },
573
- });
574
-
575
- expect(response.statusCode).toBe(500);
576
- const body = JSON.parse(response.body);
577
- expect(body).toMatchSnapshot();
578
- });
579
-
580
- it('should handle invalid search response with missing hits data', async () => {
581
- const mockSearchResponse = {
582
- body: {
583
- took: 5,
584
- // Missing hits object
585
- },
586
- };
587
-
588
- // @ts-expect-error TS is looking at the wrong function signature
589
- opensearch.search.mockResolvedValue(mockSearchResponse);
590
-
591
- const response = await fastify.inject({
592
- method: 'POST',
593
- url: '/search',
594
- payload: {
595
- query: 'test',
596
- },
597
- });
598
-
599
- expect(response.statusCode).toBe(500);
600
- const body = JSON.parse(response.body) as { error: { code: string; message: string } };
601
- expect(body.error.code).toBe('INTERNAL_ERROR');
602
- expect(body.error.message).toBe('Search failed');
603
- });
604
-
605
- it('should apply custom entity transformers', async () => {
606
- // biome-ignore lint/suspicious/noExplicitAny: fine in tests
607
- const customTransformer = async (entity: any) => ({
608
- ...entity,
609
- tested: true,
610
- });
611
-
612
- await fastifyBefore();
613
- await fastify.register(searchRoute, {
614
- prisma,
615
- opensearch,
616
- accessTransformer: AllPublicAccessTransformer,
617
- entityTransformers: [customTransformer],
618
- });
619
-
620
- const mockEntities = [
621
- {
622
- id: 'http://example.com/entity/1',
623
- name: 'Test Entity 1',
624
- description: 'A test entity',
625
- entityType: 'http://pcdm.org/models#Collection',
626
- memberOf: null,
627
- rootCollection: null,
628
- metadataLicenseId: null,
629
- contentLicenseId: null,
630
- createdAt: new Date('2024-01-01'),
631
- updatedAt: new Date('2024-01-01'),
632
- },
633
- ];
634
-
635
- const mockSearchResponse = {
636
- body: {
637
- took: 10,
638
- hits: {
639
- total: { value: 1 },
640
- hits: [
641
- {
642
- _score: 1.5,
643
- _source: {
644
- id: 'http://example.com/entity/1',
645
- },
646
- highlight: {},
647
- },
648
- ],
649
- },
650
- aggregations: {},
651
- },
652
- };
653
-
654
- // @ts-expect-error TS is looking at the wrong function signature
655
- opensearch.search.mockResolvedValue(mockSearchResponse);
656
- // @ts-expect-error TS is looking at the wrong function signature
657
- prisma.entity.findMany.mockResolvedValue(mockEntities);
658
-
659
- const response = await fastify.inject({
660
- method: 'POST',
661
- url: '/search',
662
- payload: {
663
- query: 'test',
664
- },
665
- });
666
-
667
- const body = JSON.parse(response.body);
668
-
669
- expect(response.statusCode).toBe(200);
670
- expect(body).toMatchSnapshot();
671
- });
672
-
673
- it('should handle search response with no aggregations field', async () => {
674
- const mockEntities = [
675
- {
676
- id: 'http://example.com/entity/1',
677
- name: 'Test Entity 1',
678
- description: 'A test entity',
679
- entityType: 'http://pcdm.org/models#Collection',
680
- memberOf: null,
681
- rootCollection: null,
682
- metadataLicenseId: null,
683
- contentLicenseId: null,
684
- createdAt: new Date('2024-01-01'),
685
- updatedAt: new Date('2024-01-01'),
686
- },
687
- ];
688
-
689
- const mockSearchResponse = {
690
- body: {
691
- took: 10,
692
- hits: {
693
- total: { value: 1 },
694
- hits: [
695
- {
696
- _score: 1.5,
697
- _source: {
698
- id: 'http://example.com/entity/1',
699
- },
700
- highlight: {},
701
- },
702
- ],
703
- },
704
- // No aggregations field
705
- },
706
- };
707
-
708
- // @ts-expect-error TS is looking at the wrong function signature
709
- opensearch.search.mockResolvedValue(mockSearchResponse);
710
- // @ts-expect-error TS is looking at the wrong function signature
711
- prisma.entity.findMany.mockResolvedValue(mockEntities);
712
-
713
- const response = await fastify.inject({
714
- method: 'POST',
715
- url: '/search',
716
- payload: {
717
- query: 'test',
718
- },
719
- });
720
- const body = JSON.parse(response.body) as { facets?: unknown; geohashGrid?: unknown; entities: unknown[] };
721
-
722
- expect(response.statusCode).toBe(200);
723
- expect(body.facets).toBeUndefined();
724
- expect(body.geohashGrid).toBeUndefined();
725
- expect(body.entities).toHaveLength(1);
726
- });
727
-
728
- it('should handle search response with malformed aggregation buckets', async () => {
729
- const mockEntities = [
730
- {
731
- id: 'http://example.com/entity/1',
732
- name: 'Test Entity 1',
733
- description: 'A test entity',
734
- entityType: 'http://pcdm.org/models#Collection',
735
- memberOf: null,
736
- rootCollection: null,
737
- metadataLicenseId: null,
738
- contentLicenseId: null,
739
- createdAt: new Date('2024-01-01'),
740
- updatedAt: new Date('2024-01-01'),
741
- },
742
- ];
743
-
744
- const mockSearchResponse = {
745
- body: {
746
- took: 10,
747
- hits: {
748
- total: { value: 1 },
749
- hits: [
750
- {
751
- _score: 1.5,
752
- _source: {
753
- id: 'http://example.com/entity/1',
754
- },
755
- highlight: {},
756
- },
757
- ],
758
- },
759
- aggregations: {
760
- entityType: {
761
- // buckets is not an array
762
- buckets: null,
763
- },
764
- inLanguage: {
765
- // buckets is undefined
766
- },
767
- },
768
- },
769
- };
770
-
771
- // @ts-expect-error TS is looking at the wrong function signature
772
- opensearch.search.mockResolvedValue(mockSearchResponse);
773
- // @ts-expect-error TS is looking at the wrong function signature
774
- prisma.entity.findMany.mockResolvedValue(mockEntities);
775
-
776
- const response = await fastify.inject({
777
- method: 'POST',
778
- url: '/search',
779
- payload: {
780
- query: 'test',
781
- },
782
- });
783
- const body = JSON.parse(response.body) as { facets?: unknown; entities: unknown[] };
784
-
785
- expect(response.statusCode).toBe(200);
786
- expect(body.facets).toBeUndefined();
787
- expect(body.entities).toHaveLength(1);
788
- });
789
-
790
- it('should handle search response with malformed geohash aggregation buckets', async () => {
791
- const mockEntities = [
792
- {
793
- id: 'http://example.com/entity/1',
794
- name: 'Test Entity 1',
795
- description: 'A test entity',
796
- entityType: 'http://pcdm.org/models#Collection',
797
- memberOf: null,
798
- rootCollection: null,
799
- metadataLicenseId: null,
800
- contentLicenseId: null,
801
- createdAt: new Date('2024-01-01'),
802
- updatedAt: new Date('2024-01-01'),
803
- },
804
- ];
805
-
806
- const mockSearchResponse = {
807
- body: {
808
- took: 10,
809
- hits: {
810
- total: { value: 1 },
811
- hits: [
812
- {
813
- _score: 1.5,
814
- _source: {
815
- id: 'http://example.com/entity/1',
816
- },
817
- highlight: {},
818
- },
819
- ],
820
- },
821
- aggregations: {
822
- geohash_grid: {
823
- // buckets is not an array
824
- buckets: 'invalid',
825
- },
826
- },
827
- },
828
- };
829
-
830
- // @ts-expect-error TS is looking at the wrong function signature
831
- opensearch.search.mockResolvedValue(mockSearchResponse);
832
- // @ts-expect-error TS is looking at the wrong function signature
833
- prisma.entity.findMany.mockResolvedValue(mockEntities);
834
-
835
- const response = await fastify.inject({
836
- method: 'POST',
837
- url: '/search',
838
- payload: {
839
- query: 'test',
840
- },
841
- });
842
- const body = JSON.parse(response.body) as { geohashGrid?: unknown; entities: unknown[] };
843
-
844
- expect(response.statusCode).toBe(200);
845
- expect(body.geohashGrid).toBeUndefined();
846
- expect(body.entities).toHaveLength(1);
847
- });
848
-
849
- it('should return null for memberOf/rootCollection when parent entity not found', async () => {
850
- const mockEntities = [
851
- {
852
- meta: {},
853
- id: 'http://example.com/entity/1',
854
- name: 'Test Entity 1',
855
- description: 'Entity with missing parent',
856
- entityType: 'http://pcdm.org/models#Object',
857
- memberOf: 'http://example.com/entity/deleted',
858
- rootCollection: 'http://example.com/entity/deleted',
859
- metadataLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
860
- contentLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
861
- createdAt: new Date('2024-01-01'),
862
- updatedAt: new Date('2024-01-01'),
863
- },
864
- ];
865
-
866
- const mockSearchResponse = {
867
- body: {
868
- took: 10,
869
- hits: {
870
- total: { value: 1 },
871
- hits: [
872
- {
873
- _score: 1.5,
874
- _source: {
875
- id: 'http://example.com/entity/1',
876
- },
877
- },
878
- ],
879
- },
880
- aggregations: {},
881
- },
882
- };
883
-
884
- // @ts-expect-error TS is looking at the wrong function signature
885
- opensearch.search.mockResolvedValue(mockSearchResponse);
886
- // First findMany returns the entities, second (for reference resolution) returns empty
887
- prisma.entity.findMany.mockResolvedValueOnce(mockEntities);
888
- prisma.entity.findMany.mockResolvedValueOnce([]);
889
-
890
- const response = await fastify.inject({
891
- method: 'POST',
892
- url: '/search',
893
- payload: {
894
- query: 'test',
895
- },
896
- });
897
- const body = JSON.parse(response.body);
898
-
899
- expect(response.statusCode).toBe(200);
900
- expect(body).toMatchSnapshot();
901
- });
902
- });
903
- });