arocapi 2.1.0 → 4.0.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 (105) hide show
  1. package/README.md +70 -26
  2. package/dist/app.d.ts +3 -9
  3. package/dist/app.js +20 -34
  4. package/dist/app.js.map +1 -1
  5. package/dist/app.test.js +5 -40
  6. package/dist/app.test.js.map +1 -1
  7. package/dist/index.dev.d.ts +1 -8
  8. package/dist/index.dev.js +12 -2
  9. package/dist/index.dev.js.map +1 -1
  10. package/dist/routes/crate.d.ts +2 -0
  11. package/dist/routes/crate.js +8 -27
  12. package/dist/routes/crate.js.map +1 -1
  13. package/dist/routes/crate.test.js +21 -23
  14. package/dist/routes/crate.test.js.map +1 -1
  15. package/dist/routes/entities.d.ts +2 -0
  16. package/dist/routes/entities.js +16 -20
  17. package/dist/routes/entities.js.map +1 -1
  18. package/dist/routes/entities.test.js +20 -19
  19. package/dist/routes/entities.test.js.map +1 -1
  20. package/dist/routes/entity.d.ts +2 -0
  21. package/dist/routes/entity.js +7 -14
  22. package/dist/routes/entity.js.map +1 -1
  23. package/dist/routes/entity.test.js +13 -17
  24. package/dist/routes/entity.test.js.map +1 -1
  25. package/dist/routes/file.d.ts +2 -0
  26. package/dist/routes/file.js +8 -27
  27. package/dist/routes/file.js.map +1 -1
  28. package/dist/routes/file.test.js +19 -23
  29. package/dist/routes/file.test.js.map +1 -1
  30. package/dist/routes/files.d.ts +2 -0
  31. package/dist/routes/files.js +15 -20
  32. package/dist/routes/files.js.map +1 -1
  33. package/dist/routes/files.test.js +13 -35
  34. package/dist/routes/files.test.js.map +1 -1
  35. package/dist/routes/search.d.ts +4 -0
  36. package/dist/routes/search.js +21 -24
  37. package/dist/routes/search.js.map +1 -1
  38. package/dist/routes/search.test.js +56 -24
  39. package/dist/routes/search.test.js.map +1 -1
  40. package/dist/test/helpers/fastify.js +0 -6
  41. package/dist/test/helpers/fastify.js.map +1 -1
  42. package/dist/test/integration.setup.d.ts +2 -4
  43. package/dist/test/integration.setup.js +41 -21
  44. package/dist/test/integration.setup.js.map +1 -1
  45. package/dist/test/integration.test.js +59 -0
  46. package/dist/test/integration.test.js.map +1 -1
  47. package/dist/transformers/default.d.ts +11 -18
  48. package/dist/transformers/default.js +5 -18
  49. package/dist/transformers/default.js.map +1 -1
  50. package/dist/transformers/default.test.js +6 -15
  51. package/dist/transformers/default.test.js.map +1 -1
  52. package/dist/transformers/transformer.test.js +2 -6
  53. package/dist/transformers/transformer.test.js.map +1 -1
  54. package/dist/utils/errors.d.ts +7 -1
  55. package/dist/utils/errors.js +2 -1
  56. package/dist/utils/errors.js.map +1 -1
  57. package/dist/utils/headers.d.ts +7 -0
  58. package/dist/utils/headers.js +11 -0
  59. package/dist/utils/headers.js.map +1 -0
  60. package/dist/utils/queryBuilder.d.ts +2 -2
  61. package/dist/utils/queryBuilder.js +2 -3
  62. package/dist/utils/queryBuilder.js.map +1 -1
  63. package/package.json +24 -24
  64. package/prisma/models/entity.prisma +17 -13
  65. package/prisma/models/file.prisma +9 -13
  66. package/prisma.config.ts +0 -2
  67. package/src/app.test.ts +5 -44
  68. package/src/app.ts +25 -47
  69. package/src/index.dev.ts +13 -9
  70. package/src/routes/__snapshots__/search.test.ts.snap +2 -2
  71. package/src/routes/crate.test.ts +21 -23
  72. package/src/routes/crate.ts +11 -33
  73. package/src/routes/entities.test.ts +23 -19
  74. package/src/routes/entities.ts +19 -23
  75. package/src/routes/entity.test.ts +15 -17
  76. package/src/routes/entity.ts +9 -14
  77. package/src/routes/file.test.ts +19 -23
  78. package/src/routes/file.ts +11 -33
  79. package/src/routes/files.test.ts +14 -39
  80. package/src/routes/files.ts +18 -23
  81. package/src/routes/search.test.ts +65 -26
  82. package/src/routes/search.ts +30 -23
  83. package/src/test/helpers/fastify.ts +0 -12
  84. package/src/test/integration.setup.ts +44 -21
  85. package/src/test/integration.test.ts +72 -1
  86. package/src/transformers/default.test.ts +10 -20
  87. package/src/transformers/default.ts +14 -27
  88. package/src/transformers/transformer.test.ts +4 -6
  89. package/src/utils/errors.ts +10 -6
  90. package/src/utils/headers.ts +16 -0
  91. package/src/utils/queryBuilder.ts +3 -5
  92. package/prisma/migrations/20250515034400_entities/migration.sql +0 -14
  93. package/prisma/migrations/20250515050009_extend_entity_fields/migration.sql +0 -7
  94. package/prisma/migrations/20250804225438_add_record_type/migration.sql +0 -8
  95. package/prisma/migrations/20250805040110_add_rocrate_field/migration.sql +0 -2
  96. package/prisma/migrations/20250827033956_record_type_scalar/migration.sql +0 -10
  97. package/prisma/migrations/20250923032753_api_refactor/migration.sql +0 -39
  98. package/prisma/migrations/20250923223119_entity_type_to_string/migration.sql +0 -8
  99. package/prisma/migrations/20251020140329_add_meta_column/migration.sql +0 -2
  100. package/prisma/migrations/20251027010713_add_file_table/migration.sql +0 -17
  101. package/prisma/migrations/20251027221138_add_file_id_to_entity/migration.sql +0 -2
  102. package/prisma/migrations/20251211025510_remove_rocrate_column/migration.sql +0 -8
  103. package/prisma/migrations/migration_lock.toml +0 -3
  104. package/prisma/schema.prisma +0 -17
  105. package/src/__snapshots__/app.test.ts.snap +0 -8
@@ -2,12 +2,13 @@ import { afterEach, beforeEach, describe, expect, it } from 'vitest';
2
2
 
3
3
  import { fastify, fastifyAfter, fastifyBefore, opensearch, prisma } from '../test/helpers/fastify.js';
4
4
  import { AllPublicAccessTransformer } from '../transformers/default.js';
5
+ import type { StandardErrorResponse } from '../utils/errors.js';
5
6
  import searchRoute from './search.js';
6
7
 
7
8
  describe('Search Route', () => {
8
9
  beforeEach(async () => {
9
10
  await fastifyBefore();
10
- await fastify.register(searchRoute, { accessTransformer: AllPublicAccessTransformer });
11
+ await fastify.register(searchRoute, { prisma, opensearch, accessTransformer: AllPublicAccessTransformer });
11
12
  });
12
13
 
13
14
  afterEach(async () => {
@@ -18,7 +19,7 @@ describe('Search Route', () => {
18
19
  it('should perform basic search successfully', async () => {
19
20
  const mockEntities = [
20
21
  {
21
- rocrateId: 'http://example.com/entity/1',
22
+ id: 'http://example.com/entity/1',
22
23
  name: 'Test Entity 1',
23
24
  description: 'A test entity',
24
25
  entityType: 'http://pcdm.org/models#Collection',
@@ -30,7 +31,7 @@ describe('Search Route', () => {
30
31
  updatedAt: new Date('2024-01-01'),
31
32
  },
32
33
  {
33
- rocrateId: 'http://example.com/entity/2',
34
+ id: 'http://example.com/entity/2',
34
35
  name: 'Test Entity 2',
35
36
  description: 'Another test entity',
36
37
  entityType: 'http://pcdm.org/models#Object',
@@ -52,7 +53,7 @@ describe('Search Route', () => {
52
53
  {
53
54
  _score: 1.5,
54
55
  _source: {
55
- rocrateId: 'http://example.com/entity/1',
56
+ id: 'http://example.com/entity/1',
56
57
  },
57
58
  highlight: {
58
59
  name: ['<em>Test</em> Entity 1'],
@@ -61,7 +62,7 @@ describe('Search Route', () => {
61
62
  {
62
63
  _score: 1.2,
63
64
  _source: {
64
- rocrateId: 'http://example.com/entity/2',
65
+ id: 'http://example.com/entity/2',
65
66
  },
66
67
  highlight: {
67
68
  description: ['Another <em>test</em> entity'],
@@ -98,13 +99,14 @@ describe('Search Route', () => {
98
99
  const body = JSON.parse(response.body);
99
100
  expect(body).toMatchSnapshot();
100
101
 
101
- // Verify database was queried with correct rocrateIds
102
+ // Verify database was queried with correct ids
102
103
  expect(prisma.entity.findMany).toHaveBeenCalledWith({
103
104
  where: {
104
- rocrateId: {
105
+ id: {
105
106
  in: ['http://example.com/entity/1', 'http://example.com/entity/2'],
106
107
  },
107
108
  },
109
+ include: { file: { select: { id: true } } },
108
110
  });
109
111
 
110
112
  expect(opensearch.search).toHaveBeenCalledWith({
@@ -379,6 +381,24 @@ describe('Search Route', () => {
379
381
  );
380
382
  });
381
383
 
384
+ it('should return 422 for malformed opensearch query', async () => {
385
+ const opensearchError = Object.assign(new Error('parsing_exception'), { statusCode: 400 });
386
+ opensearch.search.mockRejectedValue(opensearchError);
387
+
388
+ const response = await fastify.inject({
389
+ method: 'POST',
390
+ url: '/search',
391
+ payload: {
392
+ query: 'test',
393
+ searchType: 'advanced',
394
+ },
395
+ });
396
+
397
+ expect(response.statusCode).toBe(422);
398
+ const body = JSON.parse(response.body) as StandardErrorResponse;
399
+ expect(body.error.code).toBe('INVALID_REQUEST');
400
+ });
401
+
382
402
  it('should handle opensearch errors', async () => {
383
403
  opensearch.search.mockRejectedValue(new Error('OpenSearch connection failed'));
384
404
 
@@ -395,6 +415,25 @@ describe('Search Route', () => {
395
415
  expect(body).toMatchSnapshot();
396
416
  });
397
417
 
418
+ it('should return 422 for malformed opensearch query', async () => {
419
+ const error = new Error('parsing_exception') as Error & { statusCode: number };
420
+ error.statusCode = 400;
421
+ opensearch.search.mockRejectedValue(error);
422
+
423
+ const response = await fastify.inject({
424
+ method: 'POST',
425
+ url: '/search',
426
+ payload: {
427
+ query: 'test',
428
+ },
429
+ });
430
+
431
+ expect(response.statusCode).toBe(422);
432
+ const body = JSON.parse(response.body) as StandardErrorResponse;
433
+ expect(body.error.code).toBe('INVALID_REQUEST');
434
+ expect(body.error.message).toBe('parsing_exception');
435
+ });
436
+
398
437
  it('should validate required query parameter', async () => {
399
438
  const response = await fastify.inject({
400
439
  method: 'POST',
@@ -408,7 +447,7 @@ describe('Search Route', () => {
408
447
  it('should skip entities not found in database and log warning', async () => {
409
448
  const mockEntities = [
410
449
  {
411
- rocrateId: 'http://example.com/entity/1',
450
+ id: 'http://example.com/entity/1',
412
451
  name: 'Test Entity 1',
413
452
  description: 'A test entity',
414
453
  entityType: 'http://pcdm.org/models#Collection',
@@ -431,14 +470,14 @@ describe('Search Route', () => {
431
470
  {
432
471
  _score: 1.5,
433
472
  _source: {
434
- rocrateId: 'http://example.com/entity/1',
473
+ id: 'http://example.com/entity/1',
435
474
  },
436
475
  highlight: {},
437
476
  },
438
477
  {
439
478
  _score: 1.2,
440
479
  _source: {
441
- rocrateId: 'http://example.com/entity/2',
480
+ id: 'http://example.com/entity/2',
442
481
  },
443
482
  highlight: {},
444
483
  },
@@ -467,7 +506,7 @@ describe('Search Route', () => {
467
506
  expect(body).toMatchSnapshot();
468
507
  });
469
508
 
470
- it('should handle missing rocrateId in search hit', async () => {
509
+ it('should handle missing id in search hit', async () => {
471
510
  const mockSearchResponse = {
472
511
  body: {
473
512
  took: 5,
@@ -477,7 +516,7 @@ describe('Search Route', () => {
477
516
  {
478
517
  _score: 1.5,
479
518
  _source: {
480
- // Missing rocrateId
519
+ // Missing id
481
520
  },
482
521
  },
483
522
  ],
@@ -502,7 +541,7 @@ describe('Search Route', () => {
502
541
  console.log(response.body);
503
542
  });
504
543
 
505
- it('should handle rocrateId explicitly set to undefined', async () => {
544
+ it('should handle id explicitly set to undefined', async () => {
506
545
  const mockSearchResponse = {
507
546
  body: {
508
547
  took: 5,
@@ -512,7 +551,7 @@ describe('Search Route', () => {
512
551
  {
513
552
  _score: 1.5,
514
553
  _source: {
515
- rocrateId: undefined,
554
+ id: undefined,
516
555
  name: 'Test Entity',
517
556
  },
518
557
  },
@@ -572,13 +611,15 @@ describe('Search Route', () => {
572
611
 
573
612
  await fastifyBefore();
574
613
  await fastify.register(searchRoute, {
614
+ prisma,
615
+ opensearch,
575
616
  accessTransformer: AllPublicAccessTransformer,
576
617
  entityTransformers: [customTransformer],
577
618
  });
578
619
 
579
620
  const mockEntities = [
580
621
  {
581
- rocrateId: 'http://example.com/entity/1',
622
+ id: 'http://example.com/entity/1',
582
623
  name: 'Test Entity 1',
583
624
  description: 'A test entity',
584
625
  entityType: 'http://pcdm.org/models#Collection',
@@ -600,7 +641,7 @@ describe('Search Route', () => {
600
641
  {
601
642
  _score: 1.5,
602
643
  _source: {
603
- rocrateId: 'http://example.com/entity/1',
644
+ id: 'http://example.com/entity/1',
604
645
  },
605
646
  highlight: {},
606
647
  },
@@ -632,7 +673,7 @@ describe('Search Route', () => {
632
673
  it('should handle search response with no aggregations field', async () => {
633
674
  const mockEntities = [
634
675
  {
635
- rocrateId: 'http://example.com/entity/1',
676
+ id: 'http://example.com/entity/1',
636
677
  name: 'Test Entity 1',
637
678
  description: 'A test entity',
638
679
  entityType: 'http://pcdm.org/models#Collection',
@@ -654,7 +695,7 @@ describe('Search Route', () => {
654
695
  {
655
696
  _score: 1.5,
656
697
  _source: {
657
- rocrateId: 'http://example.com/entity/1',
698
+ id: 'http://example.com/entity/1',
658
699
  },
659
700
  highlight: {},
660
701
  },
@@ -687,7 +728,7 @@ describe('Search Route', () => {
687
728
  it('should handle search response with malformed aggregation buckets', async () => {
688
729
  const mockEntities = [
689
730
  {
690
- rocrateId: 'http://example.com/entity/1',
731
+ id: 'http://example.com/entity/1',
691
732
  name: 'Test Entity 1',
692
733
  description: 'A test entity',
693
734
  entityType: 'http://pcdm.org/models#Collection',
@@ -709,7 +750,7 @@ describe('Search Route', () => {
709
750
  {
710
751
  _score: 1.5,
711
752
  _source: {
712
- rocrateId: 'http://example.com/entity/1',
753
+ id: 'http://example.com/entity/1',
713
754
  },
714
755
  highlight: {},
715
756
  },
@@ -749,7 +790,7 @@ describe('Search Route', () => {
749
790
  it('should handle search response with malformed geohash aggregation buckets', async () => {
750
791
  const mockEntities = [
751
792
  {
752
- rocrateId: 'http://example.com/entity/1',
793
+ id: 'http://example.com/entity/1',
753
794
  name: 'Test Entity 1',
754
795
  description: 'A test entity',
755
796
  entityType: 'http://pcdm.org/models#Collection',
@@ -771,7 +812,7 @@ describe('Search Route', () => {
771
812
  {
772
813
  _score: 1.5,
773
814
  _source: {
774
- rocrateId: 'http://example.com/entity/1',
815
+ id: 'http://example.com/entity/1',
775
816
  },
776
817
  highlight: {},
777
818
  },
@@ -808,10 +849,8 @@ describe('Search Route', () => {
808
849
  it('should return null for memberOf/rootCollection when parent entity not found', async () => {
809
850
  const mockEntities = [
810
851
  {
811
- id: 1,
812
- fileId: null,
813
852
  meta: {},
814
- rocrateId: 'http://example.com/entity/1',
853
+ id: 'http://example.com/entity/1',
815
854
  name: 'Test Entity 1',
816
855
  description: 'Entity with missing parent',
817
856
  entityType: 'http://pcdm.org/models#Object',
@@ -833,7 +872,7 @@ describe('Search Route', () => {
833
872
  {
834
873
  _score: 1.5,
835
874
  _source: {
836
- rocrateId: 'http://example.com/entity/1',
875
+ id: 'http://example.com/entity/1',
837
876
  },
838
877
  },
839
878
  ],
@@ -1,11 +1,13 @@
1
+ import type { Client } from '@opensearch-project/opensearch';
1
2
  import type { MultiBucketAggregateBaseFiltersBucket } from '@opensearch-project/opensearch/api/_types/_common.aggregations.js';
2
3
  import type { Search_Request } from '@opensearch-project/opensearch/api/index.js';
3
4
  import type { FastifyPluginAsync } from 'fastify';
4
5
  import type { ZodTypeProvider } from 'fastify-type-provider-zod';
5
6
  import { z } from 'zod/v4';
7
+ import type { PrismaClient } from '../generated/prisma/client.js';
6
8
  import { baseEntityTransformer, resolveEntityReferences } from '../transformers/default.js';
7
9
  import type { AccessTransformer, EntityTransformer } from '../types/transformers.js';
8
- import { createInternalError } from '../utils/errors.js';
10
+ import { createInternalError, createInvalidRequestError } from '../utils/errors.js';
9
11
  import { OpensearchQueryBuilder, type QueryBuilderOptions } from '../utils/queryBuilder.js';
10
12
 
11
13
  const boundingBoxSchema = z.object({
@@ -24,7 +26,7 @@ const searchParamsSchema = z.object({
24
26
  query: z.string(),
25
27
  filters: z.record(z.string(), z.array(z.string())).optional(),
26
28
  boundingBox: boundingBoxSchema.optional(),
27
- geohashPrecision: z.number().int().min(0).max(12).default(5),
29
+ geohashPrecision: z.number().int().min(1).max(12).optional(),
28
30
  limit: z.number().int().min(1).max(1000).default(100),
29
31
  offset: z.number().int().min(0).default(0),
30
32
  sort: z.enum(['id', 'name', 'createdAt', 'updatedAt', 'relevance']).default('relevance'),
@@ -32,6 +34,8 @@ const searchParamsSchema = z.object({
32
34
  });
33
35
 
34
36
  type SearchRouteOptions = {
37
+ prisma: PrismaClient;
38
+ opensearch: Client;
35
39
  accessTransformer: AccessTransformer;
36
40
  entityTransformers?: EntityTransformer[];
37
41
  queryBuilderClass?: typeof OpensearchQueryBuilder;
@@ -40,6 +44,8 @@ type SearchRouteOptions = {
40
44
 
41
45
  const search: FastifyPluginAsync<SearchRouteOptions> = async (fastify, opts) => {
42
46
  const {
47
+ prisma,
48
+ opensearch,
43
49
  accessTransformer,
44
50
  entityTransformers = [],
45
51
  queryBuilderClass = OpensearchQueryBuilder,
@@ -74,38 +80,37 @@ const search: FastifyPluginAsync<SearchRouteOptions> = async (fastify, opts) =>
74
80
  },
75
81
  };
76
82
  fastify.log.debug(opensearchQuery);
77
- const response = await fastify.opensearch.search(opensearchQuery);
83
+ const response = await opensearch.search(opensearchQuery);
78
84
 
79
85
  if (!response.body?.hits?.hits) {
80
86
  throw new Error('Invalid search response: missing hits data');
81
87
  }
82
88
 
83
- const rocrateIds = response.body.hits.hits
84
- .map((hit) => hit._source?.rocrateId as string | undefined)
85
- .filter(Boolean);
89
+ const entityIds = response.body.hits.hits.map((hit) => hit._source?.id as string | undefined).filter(Boolean);
86
90
 
87
- const dbEntities = await fastify.prisma.entity.findMany({
91
+ const dbEntities = await prisma.entity.findMany({
88
92
  where: {
89
- rocrateId: {
90
- in: rocrateIds,
93
+ id: {
94
+ in: entityIds,
91
95
  },
92
96
  },
97
+ include: { file: { select: { id: true } } },
93
98
  });
94
99
 
95
- const entityMap = new Map(dbEntities.map((entity) => [entity.rocrateId, entity]));
100
+ const entityMap = new Map(dbEntities.map((entity) => [entity.id, entity]));
96
101
 
97
102
  // Resolve memberOf and rootCollection references
98
- const refMap = await resolveEntityReferences(dbEntities, fastify.prisma);
103
+ const refMap = await resolveEntityReferences(dbEntities, prisma);
99
104
 
100
105
  const entities = await Promise.all(
101
106
  response.body.hits.hits.map(async (hit) => {
102
- if (!hit._source?.rocrateId) {
103
- throw new Error('Missing rocrateId in search hit');
107
+ if (!hit._source?.id) {
108
+ throw new Error('Missing id in search hit');
104
109
  }
105
110
 
106
- const dbEntity = entityMap.get(hit._source.rocrateId);
111
+ const dbEntity = entityMap.get(hit._source.id);
107
112
  if (!dbEntity) {
108
- fastify.log.warn(`Entity ${hit._source.rocrateId} found in OpenSearch but not in database`);
113
+ fastify.log.warn(`Entity ${hit._source.id} found in OpenSearch but not in database`);
109
114
 
110
115
  return null;
111
116
  }
@@ -116,17 +121,11 @@ const search: FastifyPluginAsync<SearchRouteOptions> = async (fastify, opts) =>
116
121
  memberOf: base.memberOf ? (refMap.get(base.memberOf) ?? null) : null,
117
122
  rootCollection: base.rootCollection ? (refMap.get(base.rootCollection) ?? null) : null,
118
123
  };
119
- const authorisedEntity = await accessTransformer(standardEntity, {
120
- request,
121
- fastify,
122
- });
124
+ const authorisedEntity = await accessTransformer(standardEntity, { request, fastify });
123
125
 
124
126
  let result = authorisedEntity;
125
127
  for (const transformer of entityTransformers) {
126
- result = await transformer(result, {
127
- request,
128
- fastify,
129
- });
128
+ result = await transformer(result, { request, fastify });
130
129
  }
131
130
 
132
131
  // Add search-specific metadata
@@ -183,6 +182,14 @@ const search: FastifyPluginAsync<SearchRouteOptions> = async (fastify, opts) =>
183
182
  return result;
184
183
  } catch (error) {
185
184
  const err = error as Error;
185
+
186
+ // OpenSearch returns 400 for malformed queries (e.g. invalid query_string syntax)
187
+ if ('statusCode' in err && (err as { statusCode: number }).statusCode === 400) {
188
+ fastify.log.warn(`Invalid search query: ${err.message}`);
189
+
190
+ return reply.code(422).send(createInvalidRequestError(err.message));
191
+ }
192
+
186
193
  fastify.log.error(`Search error: ${err.message}`);
187
194
 
188
195
  return reply.code(500).send(createInternalError('Search failed'));
@@ -10,23 +10,11 @@ export let fastify: FastifyInstance;
10
10
  export const prisma = mockDeep<PrismaClient>();
11
11
  export const opensearch = mockDeep<Client>();
12
12
 
13
- // @ts-expect-error Mock sets these and fastify decorate breaks as it looks for them
14
- prisma.getter = undefined;
15
- // @ts-expect-error Mock sets these and fastify decorate breaks as it looks for them
16
- prisma.setter = undefined;
17
-
18
- // @ts-expect-error Mock sets these and fastify decorate breaks as it looks for them
19
- opensearch.getter = undefined;
20
- // @ts-expect-error Mock sets these and fastify decorate breaks as it looks for them
21
- opensearch.setter = undefined;
22
-
23
13
  export const fastifyBefore = async () => {
24
14
  mockReset(prisma);
25
15
  mockReset(opensearch);
26
16
 
27
17
  fastify = Fastify({ logger: false });
28
- fastify.decorate('prisma', prisma);
29
- fastify.decorate('opensearch', opensearch);
30
18
 
31
19
  fastify.setValidatorCompiler(validatorCompiler);
32
20
  fastify.setSerializerCompiler(serializerCompiler);
@@ -1,4 +1,5 @@
1
1
  import { Client } from '@opensearch-project/opensearch';
2
+ import { PrismaMariaDb } from '@prisma/adapter-mariadb';
2
3
  import type { FastifyInstance } from 'fastify';
3
4
  import Fastify from 'fastify';
4
5
  import app, { AllPublicAccessTransformer, AllPublicFileAccessTransformer } from '../app.js';
@@ -15,13 +16,17 @@ export async function setupIntegrationTests() {
15
16
  process.env.OPENSEARCH_URL = 'http://localhost:9201';
16
17
  process.env.NODE_ENV = 'test';
17
18
 
18
- prisma = new PrismaClient({
19
- datasources: {
20
- db: {
21
- url: process.env.DATABASE_URL,
22
- },
23
- },
19
+ const adapter = new PrismaMariaDb({
20
+ host: 'localhost',
21
+ user: 'root',
22
+ password: 'password',
23
+ database: 'catalog_test',
24
+ port: 3307,
25
+ connectionLimit: 5,
26
+ // NOTE: This is required when using the default MariaDB server configuration with SSL disabled. In production, you should properly configure SSL and remove this option.
27
+ allowPublicKeyRetrieval: true,
24
28
  });
29
+ prisma = new PrismaClient({ adapter });
25
30
 
26
31
  opensearch = new Client({
27
32
  node: process.env.OPENSEARCH_URL,
@@ -65,6 +70,7 @@ export async function teardownIntegrationTests() {
65
70
  }
66
71
 
67
72
  export async function cleanupTestData() {
73
+ await prisma.file.deleteMany({});
68
74
  await prisma.entity.deleteMany({});
69
75
 
70
76
  await opensearch.indices.delete({
@@ -78,8 +84,7 @@ export async function seedTestData() {
78
84
 
79
85
  const testEntities = [
80
86
  {
81
- id: 1,
82
- rocrateId: 'http://example.com/entity/1',
87
+ id: 'http://example.com/entity/1',
83
88
  name: 'Test Collection',
84
89
  description: 'First test entity',
85
90
  entityType: 'http://pcdm.org/models#Collection',
@@ -91,8 +96,7 @@ export async function seedTestData() {
91
96
  updatedAt: new Date(),
92
97
  },
93
98
  {
94
- id: 2,
95
- rocrateId: 'http://example.com/entity/2',
99
+ id: 'http://example.com/entity/2',
96
100
  name: 'Test Object',
97
101
  description: 'Second test entity',
98
102
  entityType: 'http://pcdm.org/models#Object',
@@ -104,8 +108,7 @@ export async function seedTestData() {
104
108
  updatedAt: new Date(),
105
109
  },
106
110
  {
107
- id: 3,
108
- rocrateId: 'http://example.com/entity/3',
111
+ id: 'http://example.com/entity/3',
109
112
  name: 'Test Person',
110
113
  description: 'Third test entity',
111
114
  entityType: 'http://schema.org/Person',
@@ -117,8 +120,7 @@ export async function seedTestData() {
117
120
  updatedAt: new Date(),
118
121
  },
119
122
  {
120
- id: 4,
121
- rocrateId: 'http://example.com/entity/4',
123
+ id: 'http://example.com/entity/4',
122
124
  name: 'test-audio.wav',
123
125
  description: 'Test audio file',
124
126
  entityType: 'http://schema.org/MediaObject',
@@ -130,8 +132,7 @@ export async function seedTestData() {
130
132
  updatedAt: new Date(),
131
133
  },
132
134
  {
133
- id: 5,
134
- rocrateId: 'http://example.com/entity/5',
135
+ id: 'http://example.com/entity/5',
135
136
  name: 'collection-metadata.csv',
136
137
  description: 'Collection metadata file',
137
138
  entityType: 'http://schema.org/MediaObject',
@@ -148,12 +149,37 @@ export async function seedTestData() {
148
149
  data: testEntities,
149
150
  });
150
151
 
152
+ const testFiles = [
153
+ {
154
+ id: 'http://example.com/entity/4',
155
+ filename: 'test-audio.wav',
156
+ mediaType: 'audio/wav',
157
+ size: BigInt(2048),
158
+ meta: {},
159
+ createdAt: new Date(),
160
+ updatedAt: new Date(),
161
+ },
162
+ {
163
+ id: 'http://example.com/entity/5',
164
+ filename: 'collection-metadata.csv',
165
+ mediaType: 'text/csv',
166
+ size: BigInt(512),
167
+ meta: {},
168
+ createdAt: new Date(),
169
+ updatedAt: new Date(),
170
+ },
171
+ ];
172
+
173
+ await prisma.file.createMany({
174
+ data: testFiles,
175
+ });
176
+
151
177
  await opensearch.indices.create({
152
178
  index: 'entities',
153
179
  body: {
154
180
  mappings: {
155
181
  properties: {
156
- rocrateId: { type: 'keyword' },
182
+ id: { type: 'keyword' },
157
183
  name: {
158
184
  type: 'text',
159
185
  fields: {
@@ -173,10 +199,7 @@ export async function seedTestData() {
173
199
  },
174
200
  });
175
201
 
176
- const testDocs = testEntities.flatMap((entity, index) => [
177
- { index: { _index: 'entities', _id: `${index + 1}` } },
178
- entity,
179
- ]);
202
+ const testDocs = testEntities.flatMap((entity) => [{ index: { _index: 'entities', _id: entity.id } }, entity]);
180
203
 
181
204
  await opensearch.bulk({
182
205
  body: testDocs,
@@ -1,5 +1,5 @@
1
1
  import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it } from 'vitest';
2
- import type { AuthorisedEntity } from '../transformers/default.js';
2
+ import type { AuthorisedEntity, AuthorisedFile } from '../transformers/default.js';
3
3
  import type { StandardErrorResponse } from '../utils/errors.js';
4
4
  import {
5
5
  cleanupTestData,
@@ -235,6 +235,77 @@ describe('Integration Tests', () => {
235
235
  });
236
236
  });
237
237
 
238
+ describe('GET /files', () => {
239
+ it('should return all files', async () => {
240
+ const app = getTestApp();
241
+
242
+ const response = await app.inject({
243
+ method: 'GET',
244
+ url: '/files',
245
+ });
246
+ const body = JSON.parse(response.body) as { total: number; files: AuthorisedFile[] };
247
+
248
+ expect(response.statusCode).toBe(200);
249
+ expect(body.total).toBe(2);
250
+ expect(body.files).toHaveLength(2);
251
+ });
252
+
253
+ it('should filter files by memberOf (Object parent)', async () => {
254
+ const app = getTestApp();
255
+
256
+ const response = await app.inject({
257
+ method: 'GET',
258
+ url: '/files',
259
+ query: {
260
+ memberOf: 'http://example.com/entity/2',
261
+ },
262
+ });
263
+ const body = JSON.parse(response.body) as { total: number; files: AuthorisedFile[] };
264
+
265
+ expect(response.statusCode).toBe(200);
266
+ expect(body.total).toBe(1);
267
+ expect(body.files).toHaveLength(1);
268
+ expect(body.files[0].id).toBe('http://example.com/entity/4');
269
+ expect(body.files[0].filename).toBe('test-audio.wav');
270
+ });
271
+
272
+ it('should filter files by memberOf (Collection parent)', async () => {
273
+ const app = getTestApp();
274
+
275
+ const response = await app.inject({
276
+ method: 'GET',
277
+ url: '/files',
278
+ query: {
279
+ memberOf: 'http://example.com/entity/1',
280
+ },
281
+ });
282
+ const body = JSON.parse(response.body) as { total: number; files: AuthorisedFile[] };
283
+
284
+ expect(response.statusCode).toBe(200);
285
+ expect(body.total).toBe(1);
286
+ expect(body.files).toHaveLength(1);
287
+ expect(body.files[0].id).toBe('http://example.com/entity/5');
288
+ expect(body.files[0].filename).toBe('collection-metadata.csv');
289
+ });
290
+
291
+ it('should return empty list when memberOf matches no entity', async () => {
292
+ const app = getTestApp();
293
+
294
+ const response = await app.inject({
295
+ method: 'GET',
296
+ url: '/files',
297
+ query: {
298
+ memberOf: 'http://example.com/entity/does-not-exist',
299
+ },
300
+ });
301
+ const body = JSON.parse(response.body) as { total: number; files: AuthorisedFile[] };
302
+
303
+ expect(response.statusCode).toBe(200);
304
+ expect(body.total).toBe(0);
305
+ expect(body.files).toHaveLength(0);
306
+ });
307
+ });
308
+
238
309
  describe('POST /search', () => {
239
310
  it('should perform basic search', async () => {
240
311
  const app = getTestApp();