arocapi 2.1.0 → 3.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.
- package/README.md +57 -9
- package/dist/app.d.ts +1 -1
- package/dist/app.js +5 -1
- package/dist/app.js.map +1 -1
- package/dist/index.dev.js +11 -1
- package/dist/index.dev.js.map +1 -1
- package/dist/routes/crate.js +5 -18
- package/dist/routes/crate.js.map +1 -1
- package/dist/routes/crate.test.js +20 -22
- package/dist/routes/crate.test.js.map +1 -1
- package/dist/routes/entities.js +12 -10
- package/dist/routes/entities.js.map +1 -1
- package/dist/routes/entities.test.js +18 -18
- package/dist/routes/entities.test.js.map +1 -1
- package/dist/routes/entity.js +3 -4
- package/dist/routes/entity.js.map +1 -1
- package/dist/routes/entity.test.js +11 -16
- package/dist/routes/entity.test.js.map +1 -1
- package/dist/routes/file.js +5 -18
- package/dist/routes/file.js.map +1 -1
- package/dist/routes/file.test.js +18 -22
- package/dist/routes/file.test.js.map +1 -1
- package/dist/routes/files.js +12 -11
- package/dist/routes/files.js.map +1 -1
- package/dist/routes/files.test.js +5 -34
- package/dist/routes/files.test.js.map +1 -1
- package/dist/routes/search.js +15 -12
- package/dist/routes/search.js.map +1 -1
- package/dist/routes/search.test.js +53 -23
- package/dist/routes/search.test.js.map +1 -1
- package/dist/test/integration.setup.d.ts +2 -4
- package/dist/test/integration.setup.js +18 -21
- package/dist/test/integration.setup.js.map +1 -1
- package/dist/transformers/default.d.ts +11 -18
- package/dist/transformers/default.js +5 -18
- package/dist/transformers/default.js.map +1 -1
- package/dist/transformers/default.test.js +6 -15
- package/dist/transformers/default.test.js.map +1 -1
- package/dist/transformers/transformer.test.js +2 -6
- package/dist/transformers/transformer.test.js.map +1 -1
- package/dist/utils/errors.d.ts +7 -1
- package/dist/utils/errors.js +2 -1
- package/dist/utils/errors.js.map +1 -1
- package/dist/utils/headers.d.ts +7 -0
- package/dist/utils/headers.js +11 -0
- package/dist/utils/headers.js.map +1 -0
- package/dist/utils/queryBuilder.d.ts +2 -2
- package/dist/utils/queryBuilder.js +2 -3
- package/dist/utils/queryBuilder.js.map +1 -1
- package/package.json +24 -23
- package/prisma/models/entity.prisma +17 -13
- package/prisma/models/file.prisma +9 -13
- package/prisma.config.ts +0 -2
- package/src/app.ts +7 -2
- package/src/index.dev.ts +12 -1
- package/src/routes/__snapshots__/search.test.ts.snap +2 -2
- package/src/routes/crate.test.ts +20 -22
- package/src/routes/crate.ts +6 -24
- package/src/routes/entities.test.ts +21 -18
- package/src/routes/entities.ts +12 -12
- package/src/routes/entity.test.ts +13 -16
- package/src/routes/entity.ts +3 -4
- package/src/routes/file.test.ts +18 -22
- package/src/routes/file.ts +6 -24
- package/src/routes/files.test.ts +5 -37
- package/src/routes/files.ts +12 -13
- package/src/routes/search.test.ts +62 -25
- package/src/routes/search.ts +19 -12
- package/src/test/integration.setup.ts +19 -21
- package/src/transformers/default.test.ts +10 -20
- package/src/transformers/default.ts +14 -27
- package/src/transformers/transformer.test.ts +4 -6
- package/src/utils/errors.ts +10 -6
- package/src/utils/headers.ts +16 -0
- package/src/utils/queryBuilder.ts +3 -5
- package/prisma/migrations/20250515034400_entities/migration.sql +0 -14
- package/prisma/migrations/20250515050009_extend_entity_fields/migration.sql +0 -7
- package/prisma/migrations/20250804225438_add_record_type/migration.sql +0 -8
- package/prisma/migrations/20250805040110_add_rocrate_field/migration.sql +0 -2
- package/prisma/migrations/20250827033956_record_type_scalar/migration.sql +0 -10
- package/prisma/migrations/20250923032753_api_refactor/migration.sql +0 -39
- package/prisma/migrations/20250923223119_entity_type_to_string/migration.sql +0 -8
- package/prisma/migrations/20251020140329_add_meta_column/migration.sql +0 -2
- package/prisma/migrations/20251027010713_add_file_table/migration.sql +0 -17
- package/prisma/migrations/20251027221138_add_file_id_to_entity/migration.sql +0 -2
- package/prisma/migrations/20251211025510_remove_rocrate_column/migration.sql +0 -8
- package/prisma/migrations/migration_lock.toml +0 -3
- package/prisma/schema.prisma +0 -17
package/src/routes/file.test.ts
CHANGED
|
@@ -27,14 +27,10 @@ describe('File Route', () => {
|
|
|
27
27
|
});
|
|
28
28
|
|
|
29
29
|
const mockFile = {
|
|
30
|
-
id:
|
|
31
|
-
fileId: 'http://example.com/file/test.wav',
|
|
30
|
+
id: 'http://example.com/file/test.wav',
|
|
32
31
|
filename: 'test.wav',
|
|
33
32
|
mediaType: 'audio/wav',
|
|
34
33
|
size: BigInt(1024),
|
|
35
|
-
memberOf: 'http://example.com/collection',
|
|
36
|
-
rootCollection: 'http://example.com/collection',
|
|
37
|
-
contentLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
|
|
38
34
|
meta: { storagePath: '/data/files/test.wav' },
|
|
39
35
|
createdAt: new Date(),
|
|
40
36
|
updatedAt: new Date(),
|
|
@@ -42,7 +38,7 @@ describe('File Route', () => {
|
|
|
42
38
|
|
|
43
39
|
describe('GET /file/:id', () => {
|
|
44
40
|
it('should stream file content successfully', async () => {
|
|
45
|
-
prisma.file.
|
|
41
|
+
prisma.file.findUnique.mockResolvedValue(mockFile);
|
|
46
42
|
|
|
47
43
|
const mockStream = Readable.from(['test file content']);
|
|
48
44
|
const mockResult: FileResult = {
|
|
@@ -79,7 +75,7 @@ describe('File Route', () => {
|
|
|
79
75
|
});
|
|
80
76
|
|
|
81
77
|
it('should handle attachment disposition', async () => {
|
|
82
|
-
prisma.file.
|
|
78
|
+
prisma.file.findUnique.mockResolvedValue(mockFile);
|
|
83
79
|
|
|
84
80
|
const mockStream = Readable.from(['test']);
|
|
85
81
|
const mockResult: FileResult = {
|
|
@@ -102,7 +98,7 @@ describe('File Route', () => {
|
|
|
102
98
|
});
|
|
103
99
|
|
|
104
100
|
it('should use custom filename when provided', async () => {
|
|
105
|
-
prisma.file.
|
|
101
|
+
prisma.file.findUnique.mockResolvedValue(mockFile);
|
|
106
102
|
|
|
107
103
|
const mockStream = Readable.from(['test']);
|
|
108
104
|
const mockResult: FileResult = {
|
|
@@ -125,7 +121,7 @@ describe('File Route', () => {
|
|
|
125
121
|
});
|
|
126
122
|
|
|
127
123
|
it('should handle redirect response with noRedirect=false (default)', async () => {
|
|
128
|
-
prisma.file.
|
|
124
|
+
prisma.file.findUnique.mockResolvedValue(mockFile);
|
|
129
125
|
|
|
130
126
|
const mockResult: FileResult = {
|
|
131
127
|
type: 'redirect',
|
|
@@ -143,7 +139,7 @@ describe('File Route', () => {
|
|
|
143
139
|
});
|
|
144
140
|
|
|
145
141
|
it('should handle redirect response with noRedirect=true', async () => {
|
|
146
|
-
prisma.file.
|
|
142
|
+
prisma.file.findUnique.mockResolvedValue(mockFile);
|
|
147
143
|
|
|
148
144
|
const mockResult: FileResult = {
|
|
149
145
|
type: 'redirect',
|
|
@@ -162,7 +158,7 @@ describe('File Route', () => {
|
|
|
162
158
|
});
|
|
163
159
|
|
|
164
160
|
it('should handle file path response without nginx X-Accel-Redirect', async () => {
|
|
165
|
-
prisma.file.
|
|
161
|
+
prisma.file.findUnique.mockResolvedValue(mockFile);
|
|
166
162
|
|
|
167
163
|
const mockStream = Readable.from(['file content']);
|
|
168
164
|
vi.mocked(createReadStream).mockReturnValue(mockStream as never);
|
|
@@ -190,7 +186,7 @@ describe('File Route', () => {
|
|
|
190
186
|
});
|
|
191
187
|
|
|
192
188
|
it('should handle file path response with nginx X-Accel-Redirect', async () => {
|
|
193
|
-
prisma.file.
|
|
189
|
+
prisma.file.findUnique.mockResolvedValue(mockFile);
|
|
194
190
|
|
|
195
191
|
const mockResult: FileResult = {
|
|
196
192
|
type: 'file',
|
|
@@ -218,7 +214,7 @@ describe('File Route', () => {
|
|
|
218
214
|
});
|
|
219
215
|
|
|
220
216
|
it('should return 404 when entity not found', async () => {
|
|
221
|
-
prisma.
|
|
217
|
+
prisma.file.findUnique.mockResolvedValue(null);
|
|
222
218
|
|
|
223
219
|
const response = await fastify.inject({
|
|
224
220
|
method: 'GET',
|
|
@@ -233,7 +229,7 @@ describe('File Route', () => {
|
|
|
233
229
|
});
|
|
234
230
|
|
|
235
231
|
it('should return 500 when database error occurs', async () => {
|
|
236
|
-
prisma.file.
|
|
232
|
+
prisma.file.findUnique.mockRejectedValue(new Error('Database connection failed'));
|
|
237
233
|
|
|
238
234
|
const response = await fastify.inject({
|
|
239
235
|
method: 'GET',
|
|
@@ -246,7 +242,7 @@ describe('File Route', () => {
|
|
|
246
242
|
});
|
|
247
243
|
|
|
248
244
|
it('should return 500 when fileHandler throws error', async () => {
|
|
249
|
-
prisma.file.
|
|
245
|
+
prisma.file.findUnique.mockResolvedValue(mockFile);
|
|
250
246
|
vi.mocked(mockFileHandler.get).mockRejectedValue(new Error('File not found in storage'));
|
|
251
247
|
|
|
252
248
|
const response = await fastify.inject({
|
|
@@ -271,7 +267,7 @@ describe('File Route', () => {
|
|
|
271
267
|
});
|
|
272
268
|
|
|
273
269
|
it('should validate disposition parameter', async () => {
|
|
274
|
-
prisma.file.
|
|
270
|
+
prisma.file.findUnique.mockResolvedValue(mockFile);
|
|
275
271
|
|
|
276
272
|
const response = await fastify.inject({
|
|
277
273
|
method: 'GET',
|
|
@@ -284,7 +280,7 @@ describe('File Route', () => {
|
|
|
284
280
|
});
|
|
285
281
|
|
|
286
282
|
it('should return 404 when get handler returns false', async () => {
|
|
287
|
-
prisma.file.
|
|
283
|
+
prisma.file.findUnique.mockResolvedValue(mockFile);
|
|
288
284
|
vi.mocked(mockFileHandler.get).mockResolvedValue(false);
|
|
289
285
|
|
|
290
286
|
const response = await fastify.inject({
|
|
@@ -301,7 +297,7 @@ describe('File Route', () => {
|
|
|
301
297
|
|
|
302
298
|
describe('HEAD /file/:id', () => {
|
|
303
299
|
it('should return headers without body using head handler', async () => {
|
|
304
|
-
prisma.file.
|
|
300
|
+
prisma.file.findUnique.mockResolvedValue(mockFile);
|
|
305
301
|
|
|
306
302
|
const mockMetadata = {
|
|
307
303
|
contentType: 'audio/wav',
|
|
@@ -332,7 +328,7 @@ describe('File Route', () => {
|
|
|
332
328
|
});
|
|
333
329
|
|
|
334
330
|
it('should return 404 when file not found', async () => {
|
|
335
|
-
prisma.file.
|
|
331
|
+
prisma.file.findUnique.mockResolvedValue(null);
|
|
336
332
|
|
|
337
333
|
const response = await fastify.inject({
|
|
338
334
|
method: 'HEAD',
|
|
@@ -346,7 +342,7 @@ describe('File Route', () => {
|
|
|
346
342
|
});
|
|
347
343
|
|
|
348
344
|
it('should return 500 when head handler throws error', async () => {
|
|
349
|
-
prisma.file.
|
|
345
|
+
prisma.file.findUnique.mockResolvedValue(mockFile);
|
|
350
346
|
vi.mocked(mockFileHandler.head).mockRejectedValue(new Error('Failed to get metadata'));
|
|
351
347
|
|
|
352
348
|
const response = await fastify.inject({
|
|
@@ -360,7 +356,7 @@ describe('File Route', () => {
|
|
|
360
356
|
});
|
|
361
357
|
|
|
362
358
|
it('should return 404 when head handler returns false', async () => {
|
|
363
|
-
prisma.file.
|
|
359
|
+
prisma.file.findUnique.mockResolvedValue(mockFile);
|
|
364
360
|
vi.mocked(mockFileHandler.head).mockResolvedValue(false);
|
|
365
361
|
|
|
366
362
|
const response = await fastify.inject({
|
|
@@ -377,7 +373,7 @@ describe('File Route', () => {
|
|
|
377
373
|
|
|
378
374
|
describe('GET /file/:id - exhaustiveness check', () => {
|
|
379
375
|
it('should handle unexpected file result type', async () => {
|
|
380
|
-
prisma.file.
|
|
376
|
+
prisma.file.findUnique.mockResolvedValue(mockFile);
|
|
381
377
|
|
|
382
378
|
// Mock an invalid result type to test exhaustiveness check
|
|
383
379
|
const invalidResult = {
|
package/src/routes/file.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { createReadStream } from 'node:fs';
|
|
2
|
-
import type { FastifyPluginAsync
|
|
2
|
+
import type { FastifyPluginAsync } from 'fastify';
|
|
3
3
|
import type { ZodTypeProvider } from 'fastify-type-provider-zod';
|
|
4
4
|
import { z } from 'zod/v4';
|
|
5
5
|
import type { FileHandler, FileMetadata } from '../types/fileHandlers.js';
|
|
6
6
|
import { createInternalError, createNotFoundError } from '../utils/errors.js';
|
|
7
|
+
import { setFileHeaders } from '../utils/headers.js';
|
|
7
8
|
|
|
8
9
|
const paramsSchema = z.object({
|
|
9
10
|
id: z.url(),
|
|
@@ -19,21 +20,6 @@ type FileRouteOptions = {
|
|
|
19
20
|
fileHandler: FileHandler;
|
|
20
21
|
};
|
|
21
22
|
|
|
22
|
-
const setFileHeaders = (
|
|
23
|
-
reply: FastifyReply,
|
|
24
|
-
metadata: { contentType: string; contentLength: number; etag?: string; lastModified?: Date },
|
|
25
|
-
) => {
|
|
26
|
-
reply.header('Content-Type', metadata.contentType);
|
|
27
|
-
reply.header('Content-Length', metadata.contentLength.toString());
|
|
28
|
-
|
|
29
|
-
if (metadata.etag) {
|
|
30
|
-
reply.header('ETag', metadata.etag);
|
|
31
|
-
}
|
|
32
|
-
if (metadata.lastModified) {
|
|
33
|
-
reply.header('Last-Modified', metadata.lastModified.toUTCString());
|
|
34
|
-
}
|
|
35
|
-
};
|
|
36
|
-
|
|
37
23
|
const file: FastifyPluginAsync<FileRouteOptions> = async (fastify, opts) => {
|
|
38
24
|
const { fileHandler } = opts;
|
|
39
25
|
|
|
@@ -48,10 +34,8 @@ const file: FastifyPluginAsync<FileRouteOptions> = async (fastify, opts) => {
|
|
|
48
34
|
const { id } = request.params;
|
|
49
35
|
|
|
50
36
|
try {
|
|
51
|
-
const file = await fastify.prisma.file.
|
|
52
|
-
where: {
|
|
53
|
-
fileId: id,
|
|
54
|
-
},
|
|
37
|
+
const file = await fastify.prisma.file.findUnique({
|
|
38
|
+
where: { id },
|
|
55
39
|
});
|
|
56
40
|
|
|
57
41
|
if (!file) {
|
|
@@ -91,10 +75,8 @@ const file: FastifyPluginAsync<FileRouteOptions> = async (fastify, opts) => {
|
|
|
91
75
|
const { id } = request.params;
|
|
92
76
|
|
|
93
77
|
try {
|
|
94
|
-
const file = await fastify.prisma.file.
|
|
95
|
-
where: {
|
|
96
|
-
fileId: id,
|
|
97
|
-
},
|
|
78
|
+
const file = await fastify.prisma.file.findUnique({
|
|
79
|
+
where: { id },
|
|
98
80
|
});
|
|
99
81
|
|
|
100
82
|
if (!file) {
|
package/src/routes/files.test.ts
CHANGED
|
@@ -16,28 +16,20 @@ describe('Files Route', () => {
|
|
|
16
16
|
});
|
|
17
17
|
|
|
18
18
|
const mockFile1 = {
|
|
19
|
-
id:
|
|
20
|
-
fileId: 'http://example.com/file1.wav',
|
|
19
|
+
id: 'http://example.com/file1.wav',
|
|
21
20
|
filename: 'file1.wav',
|
|
22
21
|
mediaType: 'audio/wav',
|
|
23
22
|
size: BigInt(1024),
|
|
24
|
-
memberOf: 'http://example.com/collection/1',
|
|
25
|
-
rootCollection: 'http://example.com/collection/1',
|
|
26
|
-
contentLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
|
|
27
23
|
meta: {},
|
|
28
24
|
createdAt: new Date('2025-01-01'),
|
|
29
25
|
updatedAt: new Date('2025-01-01'),
|
|
30
26
|
};
|
|
31
27
|
|
|
32
28
|
const mockFile2 = {
|
|
33
|
-
id:
|
|
34
|
-
fileId: 'http://example.com/file2.txt',
|
|
29
|
+
id: 'http://example.com/file2.txt',
|
|
35
30
|
filename: 'file2.txt',
|
|
36
31
|
mediaType: 'text/plain',
|
|
37
32
|
size: BigInt(512),
|
|
38
|
-
memberOf: 'http://example.com/collection/1',
|
|
39
|
-
rootCollection: 'http://example.com/collection/1',
|
|
40
|
-
contentLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
|
|
41
33
|
meta: {},
|
|
42
34
|
createdAt: new Date('2025-01-02'),
|
|
43
35
|
updatedAt: new Date('2025-01-02'),
|
|
@@ -62,14 +54,10 @@ describe('Files Route', () => {
|
|
|
62
54
|
filename: 'file1.wav',
|
|
63
55
|
mediaType: 'audio/wav',
|
|
64
56
|
size: 1024,
|
|
65
|
-
memberOf: 'http://example.com/collection/1',
|
|
66
|
-
rootCollection: 'http://example.com/collection/1',
|
|
67
|
-
contentLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
|
|
68
57
|
access: {
|
|
69
58
|
content: true,
|
|
70
59
|
},
|
|
71
60
|
});
|
|
72
|
-
expect(body.files[0]).not.toHaveProperty('metadataLicenseId');
|
|
73
61
|
});
|
|
74
62
|
|
|
75
63
|
it('should filter files by memberOf', async () => {
|
|
@@ -86,7 +74,7 @@ describe('Files Route', () => {
|
|
|
86
74
|
expect(body.total).toBe(1);
|
|
87
75
|
expect(prisma.file.findMany).toHaveBeenCalledWith(
|
|
88
76
|
expect.objectContaining({
|
|
89
|
-
where: {
|
|
77
|
+
where: { entity: { id: 'http://example.com/collection/1' } },
|
|
90
78
|
}),
|
|
91
79
|
);
|
|
92
80
|
});
|
|
@@ -129,7 +117,7 @@ describe('Files Route', () => {
|
|
|
129
117
|
);
|
|
130
118
|
});
|
|
131
119
|
|
|
132
|
-
it('should support sorting by id
|
|
120
|
+
it('should support sorting by id directly', async () => {
|
|
133
121
|
prisma.file.findMany.mockResolvedValue([mockFile1, mockFile2]);
|
|
134
122
|
prisma.file.count.mockResolvedValue(2);
|
|
135
123
|
|
|
@@ -141,7 +129,7 @@ describe('Files Route', () => {
|
|
|
141
129
|
expect(response.statusCode).toBe(200);
|
|
142
130
|
expect(prisma.file.findMany).toHaveBeenCalledWith(
|
|
143
131
|
expect.objectContaining({
|
|
144
|
-
orderBy: {
|
|
132
|
+
orderBy: { id: 'desc' },
|
|
145
133
|
}),
|
|
146
134
|
);
|
|
147
135
|
});
|
|
@@ -161,26 +149,6 @@ describe('Files Route', () => {
|
|
|
161
149
|
expect(body.files).toHaveLength(0);
|
|
162
150
|
});
|
|
163
151
|
|
|
164
|
-
it('should not include metadataLicenseId in response', async () => {
|
|
165
|
-
prisma.file.findMany.mockResolvedValue([mockFile1]);
|
|
166
|
-
prisma.file.count.mockResolvedValue(1);
|
|
167
|
-
|
|
168
|
-
const response = await fastify.inject({
|
|
169
|
-
method: 'GET',
|
|
170
|
-
url: '/files',
|
|
171
|
-
});
|
|
172
|
-
|
|
173
|
-
expect(response.statusCode).toBe(200);
|
|
174
|
-
const body = JSON.parse(response.body) as { files: Array<{ memberOf: string }> };
|
|
175
|
-
expect(body.files[0]).toMatchObject({
|
|
176
|
-
id: 'http://example.com/file1.wav',
|
|
177
|
-
filename: 'file1.wav',
|
|
178
|
-
memberOf: 'http://example.com/collection/1',
|
|
179
|
-
contentLicenseId: 'https://creativecommons.org/licenses/by/4.0/',
|
|
180
|
-
});
|
|
181
|
-
expect(body.files[0]).not.toHaveProperty('metadataLicenseId');
|
|
182
|
-
});
|
|
183
|
-
|
|
184
152
|
it('should return 500 when database error occurs', async () => {
|
|
185
153
|
prisma.file.findMany.mockRejectedValue(new Error('Database connection failed'));
|
|
186
154
|
|
package/src/routes/files.ts
CHANGED
|
@@ -35,21 +35,20 @@ const files: FastifyPluginAsync<FilesRouteOptions> = async (fastify, opts) => {
|
|
|
35
35
|
const where: NonNullable<Parameters<typeof fastify.prisma.file.findMany>[0]>['where'] = {};
|
|
36
36
|
|
|
37
37
|
if (memberOf) {
|
|
38
|
-
where.
|
|
38
|
+
where.entity = { id: memberOf };
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
-
const
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
const total = await fastify.prisma.file.count({ where });
|
|
41
|
+
const [dbFiles, total] = await Promise.all([
|
|
42
|
+
fastify.prisma.file.findMany({
|
|
43
|
+
where,
|
|
44
|
+
orderBy: {
|
|
45
|
+
[sort]: order,
|
|
46
|
+
},
|
|
47
|
+
skip: offset,
|
|
48
|
+
take: limit,
|
|
49
|
+
}),
|
|
50
|
+
fastify.prisma.file.count({ where }),
|
|
51
|
+
]);
|
|
53
52
|
|
|
54
53
|
// Apply transformers to each entity: base -> access -> additional
|
|
55
54
|
const filesWithAccess = await Promise.all(
|
|
@@ -2,6 +2,7 @@ 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', () => {
|
|
@@ -18,7 +19,7 @@ describe('Search Route', () => {
|
|
|
18
19
|
it('should perform basic search successfully', async () => {
|
|
19
20
|
const mockEntities = [
|
|
20
21
|
{
|
|
21
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
102
|
+
// Verify database was queried with correct ids
|
|
102
103
|
expect(prisma.entity.findMany).toHaveBeenCalledWith({
|
|
103
104
|
where: {
|
|
104
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
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
|
|
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
|
|
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
|
-
|
|
554
|
+
id: undefined,
|
|
516
555
|
name: 'Test Entity',
|
|
517
556
|
},
|
|
518
557
|
},
|
|
@@ -578,7 +617,7 @@ describe('Search Route', () => {
|
|
|
578
617
|
|
|
579
618
|
const mockEntities = [
|
|
580
619
|
{
|
|
581
|
-
|
|
620
|
+
id: 'http://example.com/entity/1',
|
|
582
621
|
name: 'Test Entity 1',
|
|
583
622
|
description: 'A test entity',
|
|
584
623
|
entityType: 'http://pcdm.org/models#Collection',
|
|
@@ -600,7 +639,7 @@ describe('Search Route', () => {
|
|
|
600
639
|
{
|
|
601
640
|
_score: 1.5,
|
|
602
641
|
_source: {
|
|
603
|
-
|
|
642
|
+
id: 'http://example.com/entity/1',
|
|
604
643
|
},
|
|
605
644
|
highlight: {},
|
|
606
645
|
},
|
|
@@ -632,7 +671,7 @@ describe('Search Route', () => {
|
|
|
632
671
|
it('should handle search response with no aggregations field', async () => {
|
|
633
672
|
const mockEntities = [
|
|
634
673
|
{
|
|
635
|
-
|
|
674
|
+
id: 'http://example.com/entity/1',
|
|
636
675
|
name: 'Test Entity 1',
|
|
637
676
|
description: 'A test entity',
|
|
638
677
|
entityType: 'http://pcdm.org/models#Collection',
|
|
@@ -654,7 +693,7 @@ describe('Search Route', () => {
|
|
|
654
693
|
{
|
|
655
694
|
_score: 1.5,
|
|
656
695
|
_source: {
|
|
657
|
-
|
|
696
|
+
id: 'http://example.com/entity/1',
|
|
658
697
|
},
|
|
659
698
|
highlight: {},
|
|
660
699
|
},
|
|
@@ -687,7 +726,7 @@ describe('Search Route', () => {
|
|
|
687
726
|
it('should handle search response with malformed aggregation buckets', async () => {
|
|
688
727
|
const mockEntities = [
|
|
689
728
|
{
|
|
690
|
-
|
|
729
|
+
id: 'http://example.com/entity/1',
|
|
691
730
|
name: 'Test Entity 1',
|
|
692
731
|
description: 'A test entity',
|
|
693
732
|
entityType: 'http://pcdm.org/models#Collection',
|
|
@@ -709,7 +748,7 @@ describe('Search Route', () => {
|
|
|
709
748
|
{
|
|
710
749
|
_score: 1.5,
|
|
711
750
|
_source: {
|
|
712
|
-
|
|
751
|
+
id: 'http://example.com/entity/1',
|
|
713
752
|
},
|
|
714
753
|
highlight: {},
|
|
715
754
|
},
|
|
@@ -749,7 +788,7 @@ describe('Search Route', () => {
|
|
|
749
788
|
it('should handle search response with malformed geohash aggregation buckets', async () => {
|
|
750
789
|
const mockEntities = [
|
|
751
790
|
{
|
|
752
|
-
|
|
791
|
+
id: 'http://example.com/entity/1',
|
|
753
792
|
name: 'Test Entity 1',
|
|
754
793
|
description: 'A test entity',
|
|
755
794
|
entityType: 'http://pcdm.org/models#Collection',
|
|
@@ -771,7 +810,7 @@ describe('Search Route', () => {
|
|
|
771
810
|
{
|
|
772
811
|
_score: 1.5,
|
|
773
812
|
_source: {
|
|
774
|
-
|
|
813
|
+
id: 'http://example.com/entity/1',
|
|
775
814
|
},
|
|
776
815
|
highlight: {},
|
|
777
816
|
},
|
|
@@ -808,10 +847,8 @@ describe('Search Route', () => {
|
|
|
808
847
|
it('should return null for memberOf/rootCollection when parent entity not found', async () => {
|
|
809
848
|
const mockEntities = [
|
|
810
849
|
{
|
|
811
|
-
id: 1,
|
|
812
|
-
fileId: null,
|
|
813
850
|
meta: {},
|
|
814
|
-
|
|
851
|
+
id: 'http://example.com/entity/1',
|
|
815
852
|
name: 'Test Entity 1',
|
|
816
853
|
description: 'Entity with missing parent',
|
|
817
854
|
entityType: 'http://pcdm.org/models#Object',
|
|
@@ -833,7 +870,7 @@ describe('Search Route', () => {
|
|
|
833
870
|
{
|
|
834
871
|
_score: 1.5,
|
|
835
872
|
_source: {
|
|
836
|
-
|
|
873
|
+
id: 'http://example.com/entity/1',
|
|
837
874
|
},
|
|
838
875
|
},
|
|
839
876
|
],
|