arocapi 1.0.5 → 1.2.0

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 (165) hide show
  1. package/README.md +597 -29
  2. package/dist/app.d.ts +27 -6
  3. package/dist/app.js +76 -17
  4. package/dist/app.js.map +1 -1
  5. package/dist/app.test.js +124 -0
  6. package/dist/app.test.js.map +1 -0
  7. package/dist/express.d.ts +3 -2
  8. package/dist/express.js +11 -11
  9. package/dist/express.js.map +1 -1
  10. package/dist/index.dev.d.ts +8 -0
  11. package/dist/index.dev.js +146 -0
  12. package/dist/index.dev.js.map +1 -0
  13. package/dist/routes/crate.d.ts +7 -0
  14. package/dist/routes/crate.js +99 -0
  15. package/dist/routes/crate.js.map +1 -0
  16. package/dist/routes/crate.test.d.ts +1 -0
  17. package/dist/routes/crate.test.js +360 -0
  18. package/dist/routes/crate.test.js.map +1 -0
  19. package/dist/routes/entities.d.ts +8 -0
  20. package/dist/routes/entities.js +75 -0
  21. package/dist/routes/entities.js.map +1 -0
  22. package/dist/routes/entities.test.d.ts +1 -0
  23. package/dist/routes/entities.test.js +245 -0
  24. package/dist/routes/entities.test.js.map +1 -0
  25. package/dist/routes/entity.d.ts +8 -0
  26. package/dist/routes/entity.js +45 -0
  27. package/dist/routes/entity.js.map +1 -0
  28. package/dist/routes/entity.test.d.ts +1 -0
  29. package/dist/routes/entity.test.js +109 -0
  30. package/dist/routes/entity.test.js.map +1 -0
  31. package/dist/routes/file.d.ts +7 -0
  32. package/dist/routes/file.js +111 -0
  33. package/dist/routes/file.js.map +1 -0
  34. package/dist/routes/file.test.d.ts +1 -0
  35. package/dist/routes/file.test.js +319 -0
  36. package/dist/routes/file.test.js.map +1 -0
  37. package/dist/routes/files.d.ts +8 -0
  38. package/dist/routes/files.js +61 -0
  39. package/dist/routes/files.js.map +1 -0
  40. package/dist/routes/files.test.d.ts +1 -0
  41. package/dist/routes/files.test.js +244 -0
  42. package/dist/routes/files.test.js.map +1 -0
  43. package/dist/routes/search.d.ts +8 -0
  44. package/dist/routes/{search/index.js → search.js} +45 -19
  45. package/dist/routes/search.js.map +1 -0
  46. package/dist/routes/search.test.d.ts +1 -0
  47. package/dist/routes/search.test.js +698 -0
  48. package/dist/routes/search.test.js.map +1 -0
  49. package/dist/test/helpers/fastify.d.ts +9 -0
  50. package/dist/test/helpers/fastify.js +24 -0
  51. package/dist/test/helpers/fastify.js.map +1 -0
  52. package/dist/test/integration.setup.d.ts +58 -0
  53. package/dist/test/integration.setup.js +181 -0
  54. package/dist/test/integration.setup.js.map +1 -0
  55. package/dist/test/integration.test.d.ts +1 -0
  56. package/dist/test/integration.test.js +337 -0
  57. package/dist/test/integration.test.js.map +1 -0
  58. package/dist/transformers/default.d.ts +45 -0
  59. package/dist/transformers/default.js +46 -0
  60. package/dist/transformers/default.js.map +1 -0
  61. package/dist/transformers/default.test.d.ts +1 -0
  62. package/dist/transformers/default.test.js +177 -0
  63. package/dist/transformers/default.test.js.map +1 -0
  64. package/dist/transformers/transformer.test.d.ts +1 -0
  65. package/dist/transformers/transformer.test.js +176 -0
  66. package/dist/transformers/transformer.test.js.map +1 -0
  67. package/dist/types/fileHandlers.d.ts +41 -0
  68. package/dist/types/fileHandlers.js +2 -0
  69. package/dist/types/fileHandlers.js.map +1 -0
  70. package/dist/types/transformers.d.ts +10 -0
  71. package/dist/types/transformers.js +2 -0
  72. package/dist/types/transformers.js.map +1 -0
  73. package/dist/utils/errors.d.ts +23 -0
  74. package/dist/utils/errors.js +21 -0
  75. package/dist/utils/errors.js.map +1 -0
  76. package/dist/utils/errors.test.d.ts +1 -0
  77. package/dist/utils/errors.test.js +66 -0
  78. package/dist/utils/errors.test.js.map +1 -0
  79. package/package.json +34 -25
  80. package/prisma/migrations/20250827033956_record_type_scalar/migration.sql +10 -0
  81. package/prisma/migrations/20250923032753_api_refactor/migration.sql +39 -0
  82. package/prisma/migrations/20250923223119_entity_type_to_string/migration.sql +8 -0
  83. package/prisma/migrations/20251020140329_add_meta_column/migration.sql +2 -0
  84. package/prisma/migrations/20251027010713_add_file_table/migration.sql +17 -0
  85. package/prisma/migrations/20251027221138_add_file_id_to_entity/migration.sql +2 -0
  86. package/prisma/models/entity.prisma +16 -12
  87. package/prisma/models/file.prisma +20 -0
  88. package/prisma/schema.prisma +1 -20
  89. package/prisma.config.ts +6 -7
  90. package/src/__snapshots__/app.test.ts.snap +8 -0
  91. package/src/app.test.ts +158 -0
  92. package/src/app.ts +150 -30
  93. package/src/express.ts +12 -11
  94. package/src/index.dev.ts +196 -0
  95. package/src/reset.d.ts +1 -0
  96. package/src/routes/__snapshots__/entities.test.ts.snap +68 -0
  97. package/src/routes/__snapshots__/entity.test.ts.snap +66 -0
  98. package/src/routes/__snapshots__/search.test.ts.snap +139 -0
  99. package/src/routes/crate.test.ts +441 -0
  100. package/src/routes/crate.ts +141 -0
  101. package/src/routes/entities.test.ts +284 -0
  102. package/src/routes/entities.ts +101 -0
  103. package/src/routes/entity.test.ts +132 -0
  104. package/src/routes/entity.ts +63 -0
  105. package/src/routes/file.test.ts +399 -0
  106. package/src/routes/file.ts +153 -0
  107. package/src/routes/files.test.ts +303 -0
  108. package/src/routes/files.ts +87 -0
  109. package/src/routes/search.test.ts +807 -0
  110. package/src/routes/{search/index.ts → search.ts} +67 -24
  111. package/src/test/__snapshots__/integration.test.ts.snap +18 -0
  112. package/src/test/helpers/fastify.ts +39 -0
  113. package/src/test/integration.setup.ts +200 -0
  114. package/src/test/integration.test.ts +411 -0
  115. package/src/transformers/default.test.ts +199 -0
  116. package/src/transformers/default.ts +165 -0
  117. package/src/transformers/transformer.test.ts +233 -0
  118. package/src/types/fileHandlers.ts +65 -0
  119. package/src/types/transformers.ts +52 -0
  120. package/src/utils/errors.test.ts +75 -0
  121. package/src/utils/errors.ts +45 -0
  122. package/dist/plugins/cors.d.ts +0 -3
  123. package/dist/plugins/cors.js +0 -6
  124. package/dist/plugins/cors.js.map +0 -1
  125. package/dist/plugins/opensearch.d.ts +0 -9
  126. package/dist/plugins/opensearch.js +0 -22
  127. package/dist/plugins/opensearch.js.map +0 -1
  128. package/dist/plugins/prisma.d.ts +0 -8
  129. package/dist/plugins/prisma.js +0 -11
  130. package/dist/plugins/prisma.js.map +0 -1
  131. package/dist/plugins/sensible.d.ts +0 -3
  132. package/dist/plugins/sensible.js +0 -6
  133. package/dist/plugins/sensible.js.map +0 -1
  134. package/dist/plugins/support.d.ts +0 -9
  135. package/dist/plugins/support.js +0 -5
  136. package/dist/plugins/support.js.map +0 -1
  137. package/dist/plugins/validation.d.ts +0 -3
  138. package/dist/plugins/validation.js +0 -32
  139. package/dist/plugins/validation.js.map +0 -1
  140. package/dist/routes/entities/index.d.ts +0 -3
  141. package/dist/routes/entities/index.js +0 -63
  142. package/dist/routes/entities/index.js.map +0 -1
  143. package/dist/routes/entity/index.d.ts +0 -3
  144. package/dist/routes/entity/index.js +0 -24
  145. package/dist/routes/entity/index.js.map +0 -1
  146. package/dist/routes/search/index.d.ts +0 -3
  147. package/dist/routes/search/index.js.map +0 -1
  148. package/dist/scripts/loadEntities.js +0 -170
  149. package/dist/scripts/loadEntities.js.map +0 -1
  150. package/dist/type.d.ts +0 -6
  151. package/dist/type.js +0 -2
  152. package/dist/type.js.map +0 -1
  153. package/src/plugins/README.md +0 -16
  154. package/src/plugins/cors.ts +0 -11
  155. package/src/plugins/opensearch.ts +0 -32
  156. package/src/plugins/prisma.ts +0 -23
  157. package/src/plugins/sensible.ts +0 -11
  158. package/src/plugins/support.ts +0 -20
  159. package/src/plugins/validation.ts +0 -48
  160. package/src/routes/README.md +0 -29
  161. package/src/routes/entities/index.ts +0 -83
  162. package/src/routes/entity/index.ts +0 -28
  163. package/src/scripts/loadEntities.ts +0 -223
  164. package/src/type.ts +0 -7
  165. /package/dist/{scripts/loadEntities.d.ts → app.test.d.ts} +0 -0
@@ -0,0 +1,698 @@
1
+ import { afterEach, beforeEach, describe, expect, it } from 'vitest';
2
+ import { fastify, fastifyAfter, fastifyBefore, opensearch, prisma } from '../test/helpers/fastify.js';
3
+ import { AllPublicAccessTransformer } from '../transformers/default.js';
4
+ import searchRoute from './search.js';
5
+ describe('Search Route', () => {
6
+ beforeEach(async () => {
7
+ await fastifyBefore();
8
+ await fastify.register(searchRoute, { accessTransformer: AllPublicAccessTransformer });
9
+ });
10
+ afterEach(async () => {
11
+ await fastifyAfter();
12
+ });
13
+ describe('POST /search', () => {
14
+ it('should perform basic search successfully', async () => {
15
+ const mockEntities = [
16
+ {
17
+ rocrateId: 'http://example.com/entity/1',
18
+ name: 'Test Entity 1',
19
+ description: 'A test entity',
20
+ entityType: 'http://pcdm.org/models#Collection',
21
+ memberOf: null,
22
+ rootCollection: null,
23
+ metadataLicenseId: null,
24
+ contentLicenseId: null,
25
+ createdAt: new Date('2024-01-01'),
26
+ updatedAt: new Date('2024-01-01'),
27
+ },
28
+ {
29
+ rocrateId: 'http://example.com/entity/2',
30
+ name: 'Test Entity 2',
31
+ description: 'Another test entity',
32
+ entityType: 'http://pcdm.org/models#Object',
33
+ memberOf: null,
34
+ rootCollection: null,
35
+ metadataLicenseId: null,
36
+ contentLicenseId: null,
37
+ createdAt: new Date('2024-01-01'),
38
+ updatedAt: new Date('2024-01-01'),
39
+ },
40
+ ];
41
+ const mockSearchResponse = {
42
+ body: {
43
+ took: 10,
44
+ hits: {
45
+ total: { value: 2 },
46
+ hits: [
47
+ {
48
+ _score: 1.5,
49
+ _source: {
50
+ rocrateId: 'http://example.com/entity/1',
51
+ },
52
+ highlight: {
53
+ name: ['<em>Test</em> Entity 1'],
54
+ },
55
+ },
56
+ {
57
+ _score: 1.2,
58
+ _source: {
59
+ rocrateId: 'http://example.com/entity/2',
60
+ },
61
+ highlight: {
62
+ description: ['Another <em>test</em> entity'],
63
+ },
64
+ },
65
+ ],
66
+ },
67
+ aggregations: {
68
+ entityType: {
69
+ buckets: [
70
+ { key: 'http://pcdm.org/models#Collection', doc_count: 1 },
71
+ { key: 'http://pcdm.org/models#Object', doc_count: 1 },
72
+ ],
73
+ },
74
+ },
75
+ },
76
+ };
77
+ opensearch.search.mockResolvedValue(mockSearchResponse);
78
+ prisma.entity.findMany.mockResolvedValue(mockEntities);
79
+ const response = await fastify.inject({
80
+ method: 'POST',
81
+ url: '/search',
82
+ payload: {
83
+ query: 'test',
84
+ searchType: 'basic',
85
+ },
86
+ });
87
+ expect(response.statusCode).toBe(200);
88
+ const body = JSON.parse(response.body);
89
+ expect(body).toMatchSnapshot();
90
+ expect(prisma.entity.findMany).toHaveBeenCalledWith({
91
+ where: {
92
+ rocrateId: {
93
+ in: ['http://example.com/entity/1', 'http://example.com/entity/2'],
94
+ },
95
+ },
96
+ });
97
+ expect(opensearch.search).toHaveBeenCalledWith({
98
+ index: 'entities',
99
+ body: {
100
+ query: {
101
+ bool: {
102
+ must: [
103
+ {
104
+ multi_match: {
105
+ query: 'test',
106
+ fields: ['name^2', 'description'],
107
+ type: 'best_fields',
108
+ fuzziness: 'AUTO',
109
+ },
110
+ },
111
+ ],
112
+ filter: [],
113
+ },
114
+ },
115
+ aggs: {
116
+ inLanguage: { terms: { field: 'inLanguage', size: 20 } },
117
+ mediaType: { terms: { field: 'mediaType', size: 20 } },
118
+ communicationMode: { terms: { field: 'communicationMode', size: 20 } },
119
+ entityType: { terms: { field: 'entityType', size: 20 } },
120
+ },
121
+ highlight: {
122
+ fields: {
123
+ name: {},
124
+ description: {},
125
+ },
126
+ },
127
+ sort: undefined,
128
+ from: 0,
129
+ size: 100,
130
+ },
131
+ });
132
+ });
133
+ it('should perform advanced search with query string', async () => {
134
+ const mockSearchResponse = {
135
+ body: {
136
+ took: 5,
137
+ hits: {
138
+ total: { value: 0 },
139
+ hits: [],
140
+ },
141
+ aggregations: {},
142
+ },
143
+ };
144
+ opensearch.search.mockResolvedValue(mockSearchResponse);
145
+ prisma.entity.findMany.mockResolvedValue([]);
146
+ const response = await fastify.inject({
147
+ method: 'POST',
148
+ url: '/search',
149
+ payload: {
150
+ query: 'name:test AND description:entity',
151
+ searchType: 'advanced',
152
+ },
153
+ });
154
+ expect(response.statusCode).toBe(200);
155
+ expect(opensearch.search).toHaveBeenCalledWith({
156
+ index: 'entities',
157
+ body: {
158
+ query: {
159
+ bool: {
160
+ must: [
161
+ {
162
+ query_string: {
163
+ query: 'name:test AND description:entity',
164
+ fields: ['name^2', 'description'],
165
+ default_operator: 'AND',
166
+ },
167
+ },
168
+ ],
169
+ filter: [],
170
+ },
171
+ },
172
+ aggs: {
173
+ inLanguage: { terms: { field: 'inLanguage', size: 20 } },
174
+ mediaType: { terms: { field: 'mediaType', size: 20 } },
175
+ communicationMode: { terms: { field: 'communicationMode', size: 20 } },
176
+ entityType: { terms: { field: 'entityType', size: 20 } },
177
+ },
178
+ highlight: {
179
+ fields: {
180
+ name: {},
181
+ description: {},
182
+ },
183
+ },
184
+ sort: undefined,
185
+ from: 0,
186
+ size: 100,
187
+ },
188
+ });
189
+ });
190
+ it('should apply filters correctly', async () => {
191
+ const mockSearchResponse = {
192
+ body: {
193
+ took: 8,
194
+ hits: {
195
+ total: { value: 0 },
196
+ hits: [],
197
+ },
198
+ aggregations: {},
199
+ },
200
+ };
201
+ opensearch.search.mockResolvedValue(mockSearchResponse);
202
+ prisma.entity.findMany.mockResolvedValue([]);
203
+ const response = await fastify.inject({
204
+ method: 'POST',
205
+ url: '/search',
206
+ payload: {
207
+ query: 'test',
208
+ filters: {
209
+ entityType: ['http://pcdm.org/models#Collection'],
210
+ inLanguage: ['en', 'fr'],
211
+ },
212
+ },
213
+ });
214
+ expect(response.statusCode).toBe(200);
215
+ const expectedFilters = [
216
+ {
217
+ terms: {
218
+ entityType: ['http://pcdm.org/models#Collection'],
219
+ },
220
+ },
221
+ {
222
+ terms: {
223
+ inLanguage: ['en', 'fr'],
224
+ },
225
+ },
226
+ ];
227
+ expect(opensearch.search).toHaveBeenCalledWith(expect.objectContaining({
228
+ body: expect.objectContaining({
229
+ query: expect.objectContaining({
230
+ bool: expect.objectContaining({
231
+ filter: expectedFilters,
232
+ }),
233
+ }),
234
+ }),
235
+ }));
236
+ });
237
+ it('should handle geospatial search with bounding box', async () => {
238
+ const mockSearchResponse = {
239
+ body: {
240
+ took: 12,
241
+ hits: {
242
+ total: { value: 0 },
243
+ hits: [],
244
+ },
245
+ aggregations: {
246
+ geohash_grid: {
247
+ buckets: [
248
+ { key: 'gbsuv', doc_count: 3 },
249
+ { key: 'gbsvb', doc_count: 1 },
250
+ ],
251
+ },
252
+ },
253
+ },
254
+ };
255
+ opensearch.search.mockResolvedValue(mockSearchResponse);
256
+ prisma.entity.findMany.mockResolvedValue([]);
257
+ const response = await fastify.inject({
258
+ method: 'POST',
259
+ url: '/search',
260
+ payload: {
261
+ query: 'test',
262
+ boundingBox: {
263
+ topRight: { lat: 51.5, lng: 0.1 },
264
+ bottomLeft: { lat: 51.4, lng: 0.0 },
265
+ },
266
+ geohashPrecision: 5,
267
+ },
268
+ });
269
+ expect(response.statusCode).toBe(200);
270
+ const body = JSON.parse(response.body);
271
+ expect(body.geohashGrid).toEqual({
272
+ gbsuv: 3,
273
+ gbsvb: 1,
274
+ });
275
+ expect(opensearch.search).toHaveBeenCalledWith(expect.objectContaining({
276
+ body: expect.objectContaining({
277
+ query: expect.objectContaining({
278
+ bool: expect.objectContaining({
279
+ filter: [
280
+ {
281
+ geo_bounding_box: {
282
+ location: {
283
+ top_left: { lat: 51.5, lon: 0.0 },
284
+ bottom_right: { lat: 51.4, lon: 0.1 },
285
+ },
286
+ },
287
+ },
288
+ ],
289
+ }),
290
+ }),
291
+ aggs: expect.objectContaining({
292
+ geohash_grid: {
293
+ geohash_grid: {
294
+ field: 'location',
295
+ precision: 5,
296
+ bounds: {
297
+ top_left: { lat: 51.5, lon: 0.0 },
298
+ bottom_right: { lat: 51.4, lon: 0.1 },
299
+ },
300
+ },
301
+ },
302
+ }),
303
+ }),
304
+ }));
305
+ });
306
+ it('should handle pagination and sorting', async () => {
307
+ const mockSearchResponse = {
308
+ body: {
309
+ took: 6,
310
+ hits: {
311
+ total: { value: 0 },
312
+ hits: [],
313
+ },
314
+ aggregations: {},
315
+ },
316
+ };
317
+ opensearch.search.mockResolvedValue(mockSearchResponse);
318
+ prisma.entity.findMany.mockResolvedValue([]);
319
+ const response = await fastify.inject({
320
+ method: 'POST',
321
+ url: '/search',
322
+ payload: {
323
+ query: 'test',
324
+ limit: 50,
325
+ offset: 20,
326
+ sort: 'name',
327
+ order: 'desc',
328
+ },
329
+ });
330
+ expect(response.statusCode).toBe(200);
331
+ expect(opensearch.search).toHaveBeenCalledWith(expect.objectContaining({
332
+ body: expect.objectContaining({
333
+ from: 20,
334
+ size: 50,
335
+ sort: [{ 'name.keyword': 'desc' }],
336
+ }),
337
+ }));
338
+ });
339
+ it('should handle opensearch errors', async () => {
340
+ opensearch.search.mockRejectedValue(new Error('OpenSearch connection failed'));
341
+ const response = await fastify.inject({
342
+ method: 'POST',
343
+ url: '/search',
344
+ payload: {
345
+ query: 'test',
346
+ },
347
+ });
348
+ expect(response.statusCode).toBe(500);
349
+ const body = JSON.parse(response.body);
350
+ expect(body).toMatchSnapshot();
351
+ });
352
+ it('should validate required query parameter', async () => {
353
+ const response = await fastify.inject({
354
+ method: 'POST',
355
+ url: '/search',
356
+ payload: {},
357
+ });
358
+ expect(response.statusCode).toBe(400);
359
+ });
360
+ it('should skip entities not found in database and log warning', async () => {
361
+ const mockEntities = [
362
+ {
363
+ rocrateId: 'http://example.com/entity/1',
364
+ name: 'Test Entity 1',
365
+ description: 'A test entity',
366
+ entityType: 'http://pcdm.org/models#Collection',
367
+ memberOf: null,
368
+ rootCollection: null,
369
+ metadataLicenseId: null,
370
+ contentLicenseId: null,
371
+ createdAt: new Date('2024-01-01'),
372
+ updatedAt: new Date('2024-01-01'),
373
+ },
374
+ ];
375
+ const mockSearchResponse = {
376
+ body: {
377
+ took: 10,
378
+ hits: {
379
+ total: { value: 2 },
380
+ hits: [
381
+ {
382
+ _score: 1.5,
383
+ _source: {
384
+ rocrateId: 'http://example.com/entity/1',
385
+ },
386
+ highlight: {},
387
+ },
388
+ {
389
+ _score: 1.2,
390
+ _source: {
391
+ rocrateId: 'http://example.com/entity/2',
392
+ },
393
+ highlight: {},
394
+ },
395
+ ],
396
+ },
397
+ aggregations: {},
398
+ },
399
+ };
400
+ opensearch.search.mockResolvedValue(mockSearchResponse);
401
+ prisma.entity.findMany.mockResolvedValue(mockEntities);
402
+ const response = await fastify.inject({
403
+ method: 'POST',
404
+ url: '/search',
405
+ payload: {
406
+ query: 'test',
407
+ },
408
+ });
409
+ expect(response.statusCode).toBe(200);
410
+ const body = JSON.parse(response.body);
411
+ expect(body).toMatchSnapshot();
412
+ });
413
+ it('should handle missing rocrateId in search hit', async () => {
414
+ const mockSearchResponse = {
415
+ body: {
416
+ took: 5,
417
+ hits: {
418
+ total: { value: 1 },
419
+ hits: [
420
+ {
421
+ _score: 1.5,
422
+ _source: {},
423
+ },
424
+ ],
425
+ },
426
+ aggregations: {},
427
+ },
428
+ };
429
+ opensearch.search.mockResolvedValue(mockSearchResponse);
430
+ prisma.entity.findMany.mockResolvedValue([]);
431
+ const response = await fastify.inject({
432
+ method: 'POST',
433
+ url: '/search',
434
+ payload: {
435
+ query: 'test',
436
+ },
437
+ });
438
+ expect(response.statusCode).toBe(500);
439
+ console.log(response.body);
440
+ });
441
+ it('should handle rocrateId explicitly set to undefined', async () => {
442
+ const mockSearchResponse = {
443
+ body: {
444
+ took: 5,
445
+ hits: {
446
+ total: { value: 1 },
447
+ hits: [
448
+ {
449
+ _score: 1.5,
450
+ _source: {
451
+ rocrateId: undefined,
452
+ name: 'Test Entity',
453
+ },
454
+ },
455
+ ],
456
+ },
457
+ aggregations: {},
458
+ },
459
+ };
460
+ opensearch.search.mockResolvedValue(mockSearchResponse);
461
+ const response = await fastify.inject({
462
+ method: 'POST',
463
+ url: '/search',
464
+ payload: {
465
+ query: 'test',
466
+ },
467
+ });
468
+ expect(response.statusCode).toBe(500);
469
+ const body = JSON.parse(response.body);
470
+ expect(body).toMatchSnapshot();
471
+ });
472
+ it('should handle invalid search response with missing hits data', async () => {
473
+ const mockSearchResponse = {
474
+ body: {
475
+ took: 5,
476
+ },
477
+ };
478
+ opensearch.search.mockResolvedValue(mockSearchResponse);
479
+ const response = await fastify.inject({
480
+ method: 'POST',
481
+ url: '/search',
482
+ payload: {
483
+ query: 'test',
484
+ },
485
+ });
486
+ expect(response.statusCode).toBe(500);
487
+ const body = JSON.parse(response.body);
488
+ expect(body.error.code).toBe('INTERNAL_ERROR');
489
+ expect(body.error.message).toBe('Search failed');
490
+ });
491
+ it('should apply custom entity transformers', async () => {
492
+ const customTransformer = async (entity) => ({
493
+ ...entity,
494
+ tested: true,
495
+ });
496
+ await fastifyBefore();
497
+ await fastify.register(searchRoute, {
498
+ accessTransformer: AllPublicAccessTransformer,
499
+ entityTransformers: [customTransformer],
500
+ });
501
+ const mockEntities = [
502
+ {
503
+ rocrateId: '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
+ ];
515
+ const mockSearchResponse = {
516
+ body: {
517
+ took: 10,
518
+ hits: {
519
+ total: { value: 1 },
520
+ hits: [
521
+ {
522
+ _score: 1.5,
523
+ _source: {
524
+ rocrateId: 'http://example.com/entity/1',
525
+ },
526
+ highlight: {},
527
+ },
528
+ ],
529
+ },
530
+ aggregations: {},
531
+ },
532
+ };
533
+ opensearch.search.mockResolvedValue(mockSearchResponse);
534
+ prisma.entity.findMany.mockResolvedValue(mockEntities);
535
+ const response = await fastify.inject({
536
+ method: 'POST',
537
+ url: '/search',
538
+ payload: {
539
+ query: 'test',
540
+ },
541
+ });
542
+ const body = JSON.parse(response.body);
543
+ expect(response.statusCode).toBe(200);
544
+ expect(body).toMatchSnapshot();
545
+ });
546
+ it('should handle search response with no aggregations field', async () => {
547
+ const mockEntities = [
548
+ {
549
+ rocrateId: 'http://example.com/entity/1',
550
+ name: 'Test Entity 1',
551
+ description: 'A test entity',
552
+ entityType: 'http://pcdm.org/models#Collection',
553
+ memberOf: null,
554
+ rootCollection: null,
555
+ metadataLicenseId: null,
556
+ contentLicenseId: null,
557
+ createdAt: new Date('2024-01-01'),
558
+ updatedAt: new Date('2024-01-01'),
559
+ },
560
+ ];
561
+ const mockSearchResponse = {
562
+ body: {
563
+ took: 10,
564
+ hits: {
565
+ total: { value: 1 },
566
+ hits: [
567
+ {
568
+ _score: 1.5,
569
+ _source: {
570
+ rocrateId: 'http://example.com/entity/1',
571
+ },
572
+ highlight: {},
573
+ },
574
+ ],
575
+ },
576
+ },
577
+ };
578
+ opensearch.search.mockResolvedValue(mockSearchResponse);
579
+ prisma.entity.findMany.mockResolvedValue(mockEntities);
580
+ const response = await fastify.inject({
581
+ method: 'POST',
582
+ url: '/search',
583
+ payload: {
584
+ query: 'test',
585
+ },
586
+ });
587
+ const body = JSON.parse(response.body);
588
+ expect(response.statusCode).toBe(200);
589
+ expect(body.facets).toBeUndefined();
590
+ expect(body.geohashGrid).toBeUndefined();
591
+ expect(body.entities).toHaveLength(1);
592
+ });
593
+ it('should handle search response with malformed aggregation buckets', async () => {
594
+ const mockEntities = [
595
+ {
596
+ rocrateId: 'http://example.com/entity/1',
597
+ name: 'Test Entity 1',
598
+ description: 'A test entity',
599
+ entityType: 'http://pcdm.org/models#Collection',
600
+ memberOf: null,
601
+ rootCollection: null,
602
+ metadataLicenseId: null,
603
+ contentLicenseId: null,
604
+ createdAt: new Date('2024-01-01'),
605
+ updatedAt: new Date('2024-01-01'),
606
+ },
607
+ ];
608
+ const mockSearchResponse = {
609
+ body: {
610
+ took: 10,
611
+ hits: {
612
+ total: { value: 1 },
613
+ hits: [
614
+ {
615
+ _score: 1.5,
616
+ _source: {
617
+ rocrateId: 'http://example.com/entity/1',
618
+ },
619
+ highlight: {},
620
+ },
621
+ ],
622
+ },
623
+ aggregations: {
624
+ entityType: {
625
+ buckets: null,
626
+ },
627
+ inLanguage: {},
628
+ },
629
+ },
630
+ };
631
+ opensearch.search.mockResolvedValue(mockSearchResponse);
632
+ prisma.entity.findMany.mockResolvedValue(mockEntities);
633
+ const response = await fastify.inject({
634
+ method: 'POST',
635
+ url: '/search',
636
+ payload: {
637
+ query: 'test',
638
+ },
639
+ });
640
+ const body = JSON.parse(response.body);
641
+ expect(response.statusCode).toBe(200);
642
+ expect(body.facets).toBeUndefined();
643
+ expect(body.entities).toHaveLength(1);
644
+ });
645
+ it('should handle search response with malformed geohash aggregation buckets', async () => {
646
+ const mockEntities = [
647
+ {
648
+ rocrateId: 'http://example.com/entity/1',
649
+ name: 'Test Entity 1',
650
+ description: 'A test entity',
651
+ entityType: 'http://pcdm.org/models#Collection',
652
+ memberOf: null,
653
+ rootCollection: null,
654
+ metadataLicenseId: null,
655
+ contentLicenseId: null,
656
+ createdAt: new Date('2024-01-01'),
657
+ updatedAt: new Date('2024-01-01'),
658
+ },
659
+ ];
660
+ const mockSearchResponse = {
661
+ body: {
662
+ took: 10,
663
+ hits: {
664
+ total: { value: 1 },
665
+ hits: [
666
+ {
667
+ _score: 1.5,
668
+ _source: {
669
+ rocrateId: 'http://example.com/entity/1',
670
+ },
671
+ highlight: {},
672
+ },
673
+ ],
674
+ },
675
+ aggregations: {
676
+ geohash_grid: {
677
+ buckets: 'invalid',
678
+ },
679
+ },
680
+ },
681
+ };
682
+ opensearch.search.mockResolvedValue(mockSearchResponse);
683
+ prisma.entity.findMany.mockResolvedValue(mockEntities);
684
+ const response = await fastify.inject({
685
+ method: 'POST',
686
+ url: '/search',
687
+ payload: {
688
+ query: 'test',
689
+ },
690
+ });
691
+ const body = JSON.parse(response.body);
692
+ expect(response.statusCode).toBe(200);
693
+ expect(body.geohashGrid).toBeUndefined();
694
+ expect(body.entities).toHaveLength(1);
695
+ });
696
+ });
697
+ });
698
+ //# sourceMappingURL=search.test.js.map