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