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,276 +0,0 @@
1
- import type { FastifyInstance, FastifyRequest } from 'fastify';
2
- import { beforeEach, describe, expect, it, vi } from 'vitest';
3
- import { mockReset } from 'vitest-mock-extended';
4
- import type { Entity } from '../generated/prisma/client.js';
5
- import { prisma } from '../test/helpers/fastify.js';
6
- import type { AccessFunction } from '../types/access.js';
7
- import type { RequestContext } from '../types/context.js';
8
- import { AllPublicAccess } from '../utils/access.js';
9
- import { applyTransformers, authoriseEntities, authoriseFiles, baseEntityTransformer } from './default.js';
10
-
11
- const context: RequestContext = {
12
- request: {} as FastifyRequest,
13
- fastify: {} as FastifyInstance,
14
- };
15
-
16
- describe('baseEntityTransformer', () => {
17
- it('should transform entity to standard entity shape', () => {
18
- const entity: Entity & { file?: { id: string } | null } = {
19
- id: 'http://example.com/entity/123',
20
- name: 'Test Entity',
21
- description: 'A test entity description',
22
- entityType: 'http://pcdm.org/models#Collection',
23
- memberOf: 'http://example.com/parent',
24
- rootCollection: 'http://example.com/root',
25
- metadataLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
26
- contentLicenseId: 'https://creativecommons.org/licenses/by-sa/4.0/',
27
- createdAt: new Date('2025-01-01'),
28
- updatedAt: new Date('2025-01-02'),
29
- meta: { test: 'data' },
30
- };
31
-
32
- const result = baseEntityTransformer(entity);
33
-
34
- expect(result).toEqual({
35
- id: 'http://example.com/entity/123',
36
- name: 'Test Entity',
37
- description: 'A test entity description',
38
- entityType: 'http://pcdm.org/models#Collection',
39
- memberOf: 'http://example.com/parent',
40
- rootCollection: 'http://example.com/root',
41
- metadataLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
42
- contentLicenseId: 'https://creativecommons.org/licenses/by-sa/4.0/',
43
- });
44
- });
45
-
46
- it('should handle null memberOf and rootCollection', () => {
47
- const entity: Entity & { file?: { id: string } | null } = {
48
- id: 'http://example.com/collection',
49
- name: 'Top Collection',
50
- description: 'A top-level collection',
51
- entityType: 'http://pcdm.org/models#Collection',
52
- memberOf: null,
53
- rootCollection: null,
54
- metadataLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
55
- contentLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
56
- createdAt: new Date(),
57
- updatedAt: new Date(),
58
- meta: null,
59
- };
60
-
61
- const result = baseEntityTransformer(entity);
62
-
63
- expect(result.memberOf).toBeNull();
64
- expect(result.rootCollection).toBeNull();
65
- });
66
-
67
- it('should exclude database-specific fields', () => {
68
- const entity: Entity & { file?: { id: string } | null } = {
69
- id: 'http://example.com/entity/456',
70
- name: 'Test',
71
- description: 'Test',
72
- entityType: 'http://pcdm.org/models#Object',
73
- memberOf: null,
74
- rootCollection: null,
75
- metadataLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
76
- contentLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
77
- createdAt: new Date(),
78
- updatedAt: new Date(),
79
- meta: { storage: 'path' },
80
- };
81
-
82
- const result = baseEntityTransformer(entity);
83
-
84
- expect(result.id).toBe('http://example.com/entity/456');
85
- expect(result).not.toHaveProperty('createdAt');
86
- expect(result).not.toHaveProperty('updatedAt');
87
- expect(result).not.toHaveProperty('meta');
88
- expect(Object.keys(result)).toEqual([
89
- 'id',
90
- 'name',
91
- 'description',
92
- 'entityType',
93
- 'memberOf',
94
- 'rootCollection',
95
- 'metadataLicenseId',
96
- 'contentLicenseId',
97
- ]);
98
- });
99
-
100
- it('should handle File entity (MediaObject) with file relation', () => {
101
- const entity: Entity & { file?: { id: string } | null } = {
102
- id: 'http://example.com/file/audio.wav',
103
- name: 'Audio File',
104
- description: 'An audio recording',
105
- entityType: 'http://schema.org/MediaObject',
106
- memberOf: 'http://example.com/collection',
107
- rootCollection: 'http://example.com/collection',
108
- metadataLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
109
- contentLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
110
- createdAt: new Date(),
111
- updatedAt: new Date(),
112
- meta: null,
113
- file: { id: 'http://example.com/files/audio.wav' },
114
- };
115
-
116
- const result = baseEntityTransformer(entity);
117
-
118
- expect(result).toEqual({
119
- id: 'http://example.com/file/audio.wav',
120
- name: 'Audio File',
121
- description: 'An audio recording',
122
- entityType: 'http://schema.org/MediaObject',
123
- memberOf: 'http://example.com/collection',
124
- rootCollection: 'http://example.com/collection',
125
- metadataLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
126
- contentLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
127
- });
128
- });
129
- });
130
-
131
- describe('AllPublicAccess', () => {
132
- it('should grant access to metadata and content without authorisation URLs', () => {
133
- const baseEntity = {
134
- id: 'http://example.com/entity/123',
135
- name: 'Test Entity',
136
- description: 'A test entity',
137
- entityType: 'http://schema.org/MediaObject',
138
- memberOf: 'http://example.com/parent',
139
- rootCollection: 'http://example.com/root',
140
- metadataLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
141
- contentLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
142
- };
143
-
144
- expect(AllPublicAccess(baseEntity, context)).toEqual({ metadata: true, content: true });
145
- });
146
- });
147
-
148
- const dbEntity = (id: string, overrides: Partial<Entity> = {}): Entity => ({
149
- id,
150
- name: `Name of ${id}`,
151
- description: '',
152
- entityType: 'http://schema.org/MediaObject',
153
- memberOf: null,
154
- rootCollection: null,
155
- metadataLicenseId: 'https://example.com/licence/metadata',
156
- contentLicenseId: 'https://example.com/licence/content',
157
- createdAt: new Date(),
158
- updatedAt: new Date(),
159
- meta: null,
160
- ...overrides,
161
- });
162
-
163
- describe('authoriseEntities', () => {
164
- beforeEach(() => {
165
- mockReset(prisma);
166
- });
167
-
168
- it('should pass the access function parent ids but return the Record with parent names', async () => {
169
- prisma.entity.findMany.mockResolvedValue([dbEntity('http://example.com/collection')]);
170
- const access = vi.fn<AccessFunction>(AllPublicAccess);
171
-
172
- const [entity] = await authoriseEntities(
173
- [dbEntity('http://example.com/item', { memberOf: 'http://example.com/collection' })],
174
- prisma,
175
- access,
176
- context,
177
- );
178
-
179
- expect(access).toHaveBeenCalledWith(
180
- expect.objectContaining({ memberOf: 'http://example.com/collection' }),
181
- context,
182
- );
183
- expect(entity.memberOf).toEqual({
184
- id: 'http://example.com/collection',
185
- name: 'Name of http://example.com/collection',
186
- });
187
- expect(entity.access).toEqual({ metadata: true, content: true });
188
- });
189
-
190
- it('should keep only the access keys the library defines', async () => {
191
- const access = vi.fn(async () => ({
192
- metadata: false,
193
- content: false,
194
- metadataAuthorizationUrl: 'https://example.com/request-metadata',
195
- contentAuthorizationUrl: 'https://example.com/request-content',
196
- licence: 'secret',
197
- }));
198
-
199
- const [entity] = await authoriseEntities([dbEntity('http://example.com/item')], prisma, access, context);
200
-
201
- expect(entity.access).toEqual({
202
- metadata: false,
203
- content: false,
204
- metadataAuthorizationUrl: 'https://example.com/request-metadata',
205
- contentAuthorizationUrl: 'https://example.com/request-content',
206
- });
207
- });
208
- });
209
-
210
- describe('authoriseFiles', () => {
211
- beforeEach(() => {
212
- mockReset(prisma);
213
- });
214
-
215
- const dbFile = (id: string) => ({
216
- id,
217
- filename: 'audio.wav',
218
- mediaType: 'audio/wav',
219
- size: BigInt(2048),
220
- meta: null,
221
- createdAt: new Date(),
222
- updatedAt: new Date(),
223
- entity: dbEntity(id),
224
- });
225
-
226
- it("should attach only the Content access of each file's entity", async () => {
227
- const access: AccessFunction = (entity) =>
228
- entity.id === 'http://example.com/restricted.wav'
229
- ? {
230
- metadata: false,
231
- content: false,
232
- metadataAuthorizationUrl: 'https://example.com/request-metadata',
233
- contentAuthorizationUrl: 'https://example.com/request-content',
234
- }
235
- : { metadata: false, content: true };
236
-
237
- const files = await authoriseFiles(
238
- [dbFile('http://example.com/restricted.wav'), dbFile('http://example.com/open.wav')],
239
- access,
240
- context,
241
- );
242
-
243
- expect(files).toEqual([
244
- {
245
- id: 'http://example.com/restricted.wav',
246
- filename: 'audio.wav',
247
- mediaType: 'audio/wav',
248
- size: 2048,
249
- access: { content: false, contentAuthorizationUrl: 'https://example.com/request-content' },
250
- },
251
- {
252
- id: 'http://example.com/open.wav',
253
- filename: 'audio.wav',
254
- mediaType: 'audio/wav',
255
- size: 2048,
256
- access: { content: true },
257
- },
258
- ]);
259
- });
260
- });
261
-
262
- describe('applyTransformers', () => {
263
- it('should chain transformers in order', async () => {
264
- const result = await applyTransformers(
265
- 'a',
266
- [(item) => `${item}b`, async (item, { request }) => `${item}c${request.url}`],
267
- { ...context, request: { url: '/d' } as FastifyRequest },
268
- );
269
-
270
- expect(result).toBe('abc/d');
271
- });
272
-
273
- it('should return the item unchanged without transformers', async () => {
274
- expect(await applyTransformers('a', [], context)).toBe('a');
275
- });
276
- });
@@ -1,226 +0,0 @@
1
- import type { FastifyInstance, FastifyRequest } from 'fastify';
2
- import { describe, expect, it } from 'vitest';
3
- import { prisma } from '../test/helpers/fastify.js';
4
- import type { AccessFunction } from '../types/access.js';
5
- import type { RequestContext } from '../types/context.js';
6
- import type { EntityTransformer } from '../types/transformers.js';
7
- import {
8
- type AuthorisedEntity,
9
- applyTransformers,
10
- authoriseEntities,
11
- type BaseEntity,
12
- baseEntityTransformer,
13
- type StandardEntity,
14
- } from './default.js';
15
-
16
- describe('Entity Transformers', () => {
17
- const mockContext: RequestContext = {
18
- request: {} as FastifyRequest,
19
- fastify: {} as FastifyInstance,
20
- };
21
-
22
- const mockStandardEntity: StandardEntity = {
23
- id: 'http://example.com/entity/123',
24
- name: 'Test Entity',
25
- description: 'A test entity',
26
- entityType: 'http://schema.org/Person',
27
- memberOf: { id: 'http://example.com/collection', name: 'Test Collection' },
28
- rootCollection: { id: 'http://example.com/root', name: 'Root Collection' },
29
- metadataLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
30
- contentLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
31
- };
32
-
33
- const mockAuthorisedEntity: AuthorisedEntity = {
34
- ...mockStandardEntity,
35
- access: {
36
- metadata: true,
37
- content: true,
38
- },
39
- };
40
-
41
- describe('baseEntityTransformer', () => {
42
- it('should transform raw entity to base shape with unresolved references', () => {
43
- const rawEntity = {
44
- id: 'http://example.com/entity/123',
45
- name: 'Test Entity',
46
- description: 'A test entity',
47
- entityType: 'http://schema.org/Person',
48
-
49
- memberOf: 'http://example.com/collection',
50
- rootCollection: 'http://example.com/root',
51
- metadataLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
52
- contentLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
53
- meta: {},
54
- createdAt: new Date(),
55
- updatedAt: new Date(),
56
- };
57
-
58
- const result = baseEntityTransformer(rawEntity);
59
-
60
- // baseEntityTransformer returns BaseEntity with string references
61
- const expectedBaseEntity: BaseEntity = {
62
- id: 'http://example.com/entity/123',
63
- name: 'Test Entity',
64
- description: 'A test entity',
65
- entityType: 'http://schema.org/Person',
66
- memberOf: 'http://example.com/collection',
67
- rootCollection: 'http://example.com/root',
68
- metadataLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
69
- contentLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
70
- };
71
-
72
- expect(result).toEqual(expectedBaseEntity);
73
- });
74
- });
75
-
76
- describe('Custom transformers', () => {
77
- it('should allow custom transformer to add computed fields', () => {
78
- const customTransformer: EntityTransformer<AuthorisedEntity, { id: string; displayName: string; uri: string }> = (
79
- entity,
80
- ) => ({
81
- id: entity.id,
82
- displayName: entity.name.toUpperCase(),
83
- uri: entity.id,
84
- });
85
-
86
- const result = customTransformer(mockAuthorisedEntity, mockContext);
87
-
88
- expect(result).toEqual({
89
- id: 'http://example.com/entity/123',
90
- displayName: 'TEST ENTITY',
91
- uri: 'http://example.com/entity/123',
92
- });
93
- });
94
-
95
- it('should support async transformers for fetching related data', async () => {
96
- const asyncTransformer: EntityTransformer<
97
- AuthorisedEntity,
98
- { id: string; name: string; relatedData: string }
99
- > = async (entity) => {
100
- // Simulate async operation
101
- await new Promise((resolve) => setTimeout(resolve, 1));
102
-
103
- return {
104
- id: entity.id,
105
- name: entity.name,
106
- relatedData: 'fetched-data',
107
- };
108
- };
109
-
110
- const result = await asyncTransformer(mockAuthorisedEntity, mockContext);
111
-
112
- expect(result).toEqual({
113
- id: 'http://example.com/entity/123',
114
- name: 'Test Entity',
115
- relatedData: 'fetched-data',
116
- });
117
- });
118
-
119
- it('should allow transformer to access context', async () => {
120
- const contextAwareTransformer: EntityTransformer<
121
- AuthorisedEntity,
122
- {
123
- id: string;
124
- name: string;
125
- requestUrl: string;
126
- }
127
- > = async (entity, context) => ({
128
- id: entity.id,
129
- name: entity.name,
130
- requestUrl: context.request.url || 'unknown',
131
- });
132
-
133
- const contextWithUrl: RequestContext = {
134
- request: { url: '/test-url' } as FastifyRequest,
135
- fastify: {} as FastifyInstance,
136
- };
137
-
138
- const result = await contextAwareTransformer(mockAuthorisedEntity, contextWithUrl);
139
-
140
- expect(result).toEqual({
141
- id: 'http://example.com/entity/123',
142
- name: 'Test Entity',
143
- requestUrl: '/test-url',
144
- });
145
- });
146
-
147
- it('should support transformer pipeline', async () => {
148
- const addDisplayName: EntityTransformer<AuthorisedEntity, AuthorisedEntity & { displayName: string }> = (
149
- entity,
150
- ) => ({
151
- ...entity,
152
- displayName: `${entity.name} [${entity.entityType.split('/').pop()}]`,
153
- });
154
-
155
- const addUpperCase: EntityTransformer<
156
- AuthorisedEntity & { displayName: string },
157
- AuthorisedEntity & { displayName: string; upperName: string }
158
- > = (entity) => ({
159
- ...entity,
160
- upperName: entity.name.toUpperCase(),
161
- });
162
-
163
- // Simulate pipeline
164
- // biome-ignore lint/suspicious/noExplicitAny: fine in tests
165
- let result: any = mockAuthorisedEntity;
166
- result = await addDisplayName(result, mockContext);
167
- result = await addUpperCase(result, mockContext);
168
-
169
- expect(result).toMatchObject({
170
- id: 'http://example.com/entity/123',
171
- name: 'Test Entity',
172
- displayName: 'Test Entity [Person]',
173
- upperName: 'TEST ENTITY',
174
- });
175
- });
176
-
177
- it('should demonstrate full pipeline: base -> resolve -> access -> custom', async () => {
178
- const rawEntity = {
179
- id: 'http://example.com/entity/123',
180
- name: 'Test Entity',
181
- description: 'A test entity',
182
- entityType: 'http://schema.org/Person',
183
-
184
- memberOf: 'http://example.com/collection',
185
- rootCollection: 'http://example.com/root',
186
- metadataLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
187
- contentLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
188
- meta: {},
189
- createdAt: new Date(),
190
- updatedAt: new Date(),
191
- };
192
-
193
- const access: AccessFunction = () => ({
194
- metadata: true,
195
- content: false,
196
- contentAuthorizationUrl: 'https://example.com/auth',
197
- });
198
-
199
- const addMetadata: EntityTransformer<AuthorisedEntity, AuthorisedEntity & { processed: boolean }> = (entity) => ({
200
- ...entity,
201
- processed: true,
202
- });
203
-
204
- prisma.entity.findMany.mockResolvedValue([
205
- { ...rawEntity, id: 'http://example.com/collection', name: 'Parent Collection' },
206
- { ...rawEntity, id: 'http://example.com/root', name: 'Root Collection' },
207
- ]);
208
-
209
- const [authorised] = await authoriseEntities([rawEntity], prisma, access, mockContext);
210
- const final = await applyTransformers(authorised, [addMetadata], mockContext);
211
-
212
- expect(final).toMatchObject({
213
- id: 'http://example.com/entity/123',
214
- name: 'Test Entity',
215
- memberOf: { id: 'http://example.com/collection', name: 'Parent Collection' },
216
- rootCollection: { id: 'http://example.com/root', name: 'Root Collection' },
217
- access: {
218
- metadata: true,
219
- content: false,
220
- contentAuthorizationUrl: 'https://example.com/auth',
221
- },
222
- processed: true,
223
- });
224
- });
225
- });
226
- });
@@ -1,90 +0,0 @@
1
- import { describe, expect, it } from 'vitest';
2
- import { createForbiddenError, createInternalError, createNotFoundError } from './errors.js';
3
-
4
- describe('Error Utilities', () => {
5
- describe('createInternalError', () => {
6
- it('should create a default internal error', () => {
7
- const error = createInternalError();
8
-
9
- expect(error).toEqual({
10
- error: {
11
- code: 'INTERNAL_ERROR',
12
- message: 'Internal server error',
13
- details: undefined,
14
- },
15
- });
16
- });
17
-
18
- it('should create an internal error with custom message', () => {
19
- const customMessage = 'Database connection failed';
20
- const error = createInternalError(customMessage);
21
-
22
- expect(error).toEqual({
23
- error: {
24
- code: 'INTERNAL_ERROR',
25
- message: customMessage,
26
- details: undefined,
27
- },
28
- });
29
- });
30
- });
31
-
32
- describe('createForbiddenError', () => {
33
- it('should create a forbidden error with message and entityId', () => {
34
- const entityId = 'http://example.com/entity/123';
35
- const error = createForbiddenError('No access', entityId);
36
-
37
- expect(error).toEqual({
38
- error: {
39
- code: 'FORBIDDEN',
40
- message: 'No access',
41
- details: { entityId },
42
- },
43
- });
44
- });
45
- });
46
-
47
- describe('createNotFoundError', () => {
48
- it('should create a not found error with message', () => {
49
- const message = 'Entity not found';
50
- const error = createNotFoundError(message);
51
-
52
- expect(error).toEqual({
53
- error: {
54
- code: 'NOT_FOUND',
55
- message,
56
- details: undefined,
57
- },
58
- });
59
- });
60
-
61
- it('should create a not found error with message and entityId', () => {
62
- const message = 'Entity not found';
63
- const entityId = 'http://example.com/entity/123';
64
- const error = createNotFoundError(message, entityId);
65
-
66
- expect(error).toEqual({
67
- error: {
68
- code: 'NOT_FOUND',
69
- message,
70
- details: {
71
- entityId,
72
- },
73
- },
74
- });
75
- });
76
-
77
- it('should handle undefined entityId', () => {
78
- const message = 'Entity not found';
79
- const error = createNotFoundError(message, undefined);
80
-
81
- expect(error).toEqual({
82
- error: {
83
- code: 'NOT_FOUND',
84
- message,
85
- details: undefined,
86
- },
87
- });
88
- });
89
- });
90
- });