arocapi 5.0.0-beta.4 → 5.0.0-beta.6

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