arocapi 1.1.0 → 1.2.2

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