arocapi 5.0.0-beta.1 → 5.0.0-beta.10
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/LICENSE.md +1 -1
- package/README.md +37 -802
- package/dist/app.d.ts +38 -20
- package/dist/app.js +62 -40
- package/dist/app.js.map +1 -1
- package/dist/routes/capabilities.d.ts +7 -0
- package/dist/routes/capabilities.js +13 -0
- package/dist/routes/capabilities.js.map +1 -0
- package/dist/routes/entities.d.ts +5 -2
- package/dist/routes/entities.js +37 -57
- package/dist/routes/entities.js.map +1 -1
- package/dist/routes/entity.d.ts +7 -2
- package/dist/routes/entity.js +13 -29
- package/dist/routes/entity.js.map +1 -1
- package/dist/routes/file.d.ts +8 -2
- package/dist/routes/file.js +37 -72
- package/dist/routes/file.js.map +1 -1
- package/dist/routes/files.d.ts +5 -2
- package/dist/routes/files.js +24 -39
- package/dist/routes/files.js.map +1 -1
- package/dist/routes/metadata.d.ts +11 -5
- package/dist/routes/metadata.js +24 -68
- package/dist/routes/metadata.js.map +1 -1
- package/dist/routes/search.d.ts +8 -7
- package/dist/routes/search.js +90 -119
- package/dist/routes/search.js.map +1 -1
- package/dist/search/advanced.d.ts +8 -0
- package/dist/search/advanced.js +198 -0
- package/dist/search/advanced.js.map +1 -0
- package/dist/search/backend.d.ts +95 -0
- package/dist/search/backend.js +5 -0
- package/dist/search/backend.js.map +1 -0
- package/dist/search/fields.d.ts +63 -0
- package/dist/search/fields.js +203 -0
- package/dist/search/fields.js.map +1 -0
- package/dist/search/opensearch/advanced.d.ts +4 -0
- package/dist/search/opensearch/advanced.js +28 -0
- package/dist/search/opensearch/advanced.js.map +1 -0
- package/dist/search/opensearch/opensearch.d.ts +16 -0
- package/dist/search/opensearch/opensearch.js +187 -0
- package/dist/search/opensearch/opensearch.js.map +1 -0
- package/dist/search/opensearch/visibility.d.ts +3 -0
- package/dist/search/opensearch/visibility.js +27 -0
- package/dist/search/opensearch/visibility.js.map +1 -0
- package/dist/search/postgres/advanced.d.ts +7 -0
- package/dist/search/postgres/advanced.js +54 -0
- package/dist/search/postgres/advanced.js.map +1 -0
- package/dist/search/postgres/facets.d.ts +2 -0
- package/dist/search/postgres/facets.js +27 -0
- package/dist/search/postgres/facets.js.map +1 -0
- package/dist/search/postgres/geo.d.ts +6 -0
- package/dist/search/postgres/geo.js +42 -0
- package/dist/search/postgres/geo.js.map +1 -0
- package/dist/search/postgres/postgres.d.ts +19 -0
- package/dist/search/postgres/postgres.js +157 -0
- package/dist/search/postgres/postgres.js.map +1 -0
- package/dist/search/postgres/values.d.ts +8 -0
- package/dist/search/postgres/values.js +20 -0
- package/dist/search/postgres/values.js.map +1 -0
- package/dist/search/postgres/visibility.d.ts +4 -0
- package/dist/search/postgres/visibility.js +38 -0
- package/dist/search/postgres/visibility.js.map +1 -0
- package/dist/search/requestFilters.d.ts +39 -0
- package/dist/search/requestFilters.js +125 -0
- package/dist/search/requestFilters.js.map +1 -0
- package/dist/search/visibility.d.ts +51 -0
- package/dist/search/visibility.js +100 -0
- package/dist/search/visibility.js.map +1 -0
- package/dist/storage/backend.d.ts +58 -0
- package/dist/storage/backend.js +2 -0
- package/dist/storage/backend.js.map +1 -0
- package/dist/storage/deliver.d.ts +12 -0
- package/dist/storage/deliver.js +34 -0
- package/dist/storage/deliver.js.map +1 -0
- package/dist/storage/directory/directory.d.ts +6 -0
- package/dist/storage/directory/directory.js +24 -0
- package/dist/storage/directory/directory.js.map +1 -0
- package/dist/storage/entity-meta/entity-meta.d.ts +5 -0
- package/dist/storage/entity-meta/entity-meta.js +14 -0
- package/dist/storage/entity-meta/entity-meta.js.map +1 -0
- package/dist/storage/location.d.ts +19 -0
- package/dist/storage/location.js +36 -0
- package/dist/storage/location.js.map +1 -0
- package/dist/storage/ocfl/ocfl.d.ts +6 -0
- package/dist/storage/ocfl/ocfl.js +45 -0
- package/dist/storage/ocfl/ocfl.js.map +1 -0
- package/dist/storage/results.d.ts +7 -0
- package/dist/storage/results.js +22 -0
- package/dist/storage/results.js.map +1 -0
- package/dist/storage/s3/s3.d.ts +10 -0
- package/dist/storage/s3/s3.js +47 -0
- package/dist/storage/s3/s3.js.map +1 -0
- package/dist/transformers/default.d.ts +11 -22
- package/dist/transformers/default.js +41 -15
- package/dist/transformers/default.js.map +1 -1
- package/dist/types/access.d.ts +9 -0
- package/dist/types/access.js +2 -0
- package/dist/types/access.js.map +1 -0
- package/dist/types/context.d.ts +6 -0
- package/dist/types/context.js +2 -0
- package/dist/types/context.js.map +1 -0
- package/dist/types/transformers.d.ts +4 -10
- package/dist/types/visibility.d.ts +6 -0
- package/dist/types/visibility.js +2 -0
- package/dist/types/visibility.js.map +1 -0
- package/dist/utils/access.d.ts +17 -0
- package/dist/utils/access.js +41 -0
- package/dist/utils/access.js.map +1 -0
- package/dist/utils/errors.d.ts +19 -10
- package/dist/utils/errors.js +44 -8
- package/dist/utils/errors.js.map +1 -1
- package/dist/utils/headers.d.ts +1 -1
- package/dist/utils/headers.js +10 -1
- package/dist/utils/headers.js.map +1 -1
- package/dist/utils/validation.d.ts +2 -0
- package/dist/utils/validation.js +8 -0
- package/dist/utils/validation.js.map +1 -0
- package/dist/utils/visibility.d.ts +18 -0
- package/dist/utils/visibility.js +16 -0
- package/dist/utils/visibility.js.map +1 -0
- package/package.json +82 -40
- package/prisma/models/entity.prisma +9 -8
- package/prisma/models/entitySearch.prisma +21 -0
- package/prisma/models/file.prisma +3 -3
- package/src/app.ts +168 -95
- package/src/routes/capabilities.ts +22 -0
- package/src/routes/entities.ts +48 -67
- package/src/routes/entity.ts +21 -35
- package/src/routes/file.ts +53 -103
- package/src/routes/files.ts +31 -47
- package/src/routes/metadata.ts +42 -98
- package/src/routes/search.ts +131 -151
- package/src/search/advanced.ts +258 -0
- package/src/search/backend.ts +91 -0
- package/src/search/fields.ts +320 -0
- package/src/search/opensearch/advanced.ts +36 -0
- package/src/search/opensearch/opensearch.ts +290 -0
- package/src/search/opensearch/visibility.ts +38 -0
- package/src/search/postgres/advanced.ts +76 -0
- package/src/search/postgres/facets.ts +34 -0
- package/src/search/postgres/geo.ts +58 -0
- package/src/search/postgres/postgres.ts +232 -0
- package/src/search/postgres/values.ts +33 -0
- package/src/search/postgres/visibility.ts +54 -0
- package/src/search/requestFilters.ts +178 -0
- package/src/search/visibility.ts +193 -0
- package/src/storage/backend.ts +75 -0
- package/src/storage/deliver.ts +65 -0
- package/src/storage/directory/directory.ts +39 -0
- package/src/storage/entity-meta/entity-meta.ts +25 -0
- package/src/storage/location.ts +78 -0
- package/src/storage/ocfl/ocfl.ts +71 -0
- package/src/storage/results.ts +32 -0
- package/src/storage/s3/s3.ts +79 -0
- package/src/transformers/default.ts +115 -85
- package/src/types/access.ts +25 -0
- package/src/types/context.ts +15 -0
- package/src/types/transformers.ts +8 -34
- package/src/types/visibility.ts +21 -0
- package/src/utils/access.ts +80 -0
- package/src/utils/errors.ts +68 -11
- package/src/utils/headers.ts +22 -2
- package/src/utils/validation.ts +11 -0
- package/src/utils/visibility.ts +56 -0
- package/dist/app.test.d.ts +0 -1
- package/dist/app.test.js +0 -82
- package/dist/app.test.js.map +0 -1
- package/dist/index.dev.d.ts +0 -1
- package/dist/index.dev.js +0 -156
- package/dist/index.dev.js.map +0 -1
- package/dist/routes/crate.test.d.ts +0 -1
- package/dist/routes/crate.test.js +0 -342
- package/dist/routes/crate.test.js.map +0 -1
- package/dist/routes/entities.test.d.ts +0 -1
- package/dist/routes/entities.test.js +0 -273
- package/dist/routes/entities.test.js.map +0 -1
- package/dist/routes/entity.test.d.ts +0 -1
- package/dist/routes/entity.test.js +0 -130
- package/dist/routes/entity.test.js.map +0 -1
- package/dist/routes/file.test.d.ts +0 -1
- package/dist/routes/file.test.js +0 -315
- package/dist/routes/file.test.js.map +0 -1
- package/dist/routes/files.test.d.ts +0 -1
- package/dist/routes/files.test.js +0 -222
- package/dist/routes/files.test.js.map +0 -1
- package/dist/routes/search.test.d.ts +0 -1
- package/dist/routes/search.test.js +0 -780
- package/dist/routes/search.test.js.map +0 -1
- package/dist/test/helpers/fastify.d.ts +0 -9
- package/dist/test/helpers/fastify.js +0 -18
- package/dist/test/helpers/fastify.js.map +0 -1
- package/dist/test/integration.setup.d.ts +0 -29
- package/dist/test/integration.setup.js +0 -187
- package/dist/test/integration.setup.js.map +0 -1
- package/dist/test/integration.test.d.ts +0 -1
- package/dist/test/integration.test.js +0 -396
- package/dist/test/integration.test.js.map +0 -1
- package/dist/transformers/default.test.d.ts +0 -1
- package/dist/transformers/default.test.js +0 -163
- package/dist/transformers/default.test.js.map +0 -1
- package/dist/transformers/transformer.test.d.ts +0 -1
- package/dist/transformers/transformer.test.js +0 -187
- package/dist/transformers/transformer.test.js.map +0 -1
- package/dist/types/fileHandlers.d.ts +0 -41
- package/dist/types/fileHandlers.js +0 -2
- package/dist/types/fileHandlers.js.map +0 -1
- package/dist/utils/errors.test.d.ts +0 -1
- package/dist/utils/errors.test.js +0 -66
- package/dist/utils/errors.test.js.map +0 -1
- package/dist/utils/queryBuilder.d.ts +0 -35
- package/dist/utils/queryBuilder.js +0 -98
- package/dist/utils/queryBuilder.js.map +0 -1
- package/prisma.config.ts +0 -9
- package/src/app.test.ts +0 -112
- package/src/index.dev.ts +0 -200
- package/src/reset.d.ts +0 -1
- package/src/routes/__snapshots__/entities.test.ts.snap +0 -96
- package/src/routes/__snapshots__/entity.test.ts.snap +0 -74
- package/src/routes/__snapshots__/search.test.ts.snap +0 -165
- package/src/routes/crate.test.ts +0 -423
- package/src/routes/entities.test.ts +0 -320
- package/src/routes/entity.test.ts +0 -160
- package/src/routes/file.test.ts +0 -395
- package/src/routes/files.test.ts +0 -278
- package/src/routes/search.test.ts +0 -903
- package/src/test/__snapshots__/integration.test.ts.snap +0 -18
- package/src/test/helpers/fastify.ts +0 -27
- package/src/test/integration.setup.ts +0 -209
- package/src/test/integration.test.ts +0 -482
- package/src/transformers/default.test.ts +0 -184
- package/src/transformers/transformer.test.ts +0 -252
- package/src/types/fileHandlers.ts +0 -65
- package/src/utils/errors.test.ts +0 -75
- package/src/utils/queryBuilder.ts +0 -132
package/src/routes/file.test.ts
DELETED
|
@@ -1,395 +0,0 @@
|
|
|
1
|
-
import { createReadStream } from 'node:fs';
|
|
2
|
-
import { Readable } from 'node:stream';
|
|
3
|
-
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
4
|
-
import { fastify, fastifyAfter, fastifyBefore, prisma } from '../test/helpers/fastify.js';
|
|
5
|
-
import type { FileHandler, FileResult } from '../types/fileHandlers.js';
|
|
6
|
-
import type { StandardErrorResponse } from '../utils/errors.js';
|
|
7
|
-
import fileRoute from './file.js';
|
|
8
|
-
|
|
9
|
-
vi.mock('node:fs', () => ({
|
|
10
|
-
createReadStream: vi.fn(),
|
|
11
|
-
}));
|
|
12
|
-
|
|
13
|
-
describe('File Route', () => {
|
|
14
|
-
const mockFileHandler: FileHandler = {
|
|
15
|
-
get: vi.fn(),
|
|
16
|
-
head: vi.fn(),
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
beforeEach(async () => {
|
|
20
|
-
await fastifyBefore();
|
|
21
|
-
await fastify.register(fileRoute, { prisma, fileHandler: mockFileHandler });
|
|
22
|
-
vi.clearAllMocks();
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
afterEach(async () => {
|
|
26
|
-
await fastifyAfter();
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
const mockFile = {
|
|
30
|
-
id: 'http://example.com/file/test.wav',
|
|
31
|
-
filename: 'test.wav',
|
|
32
|
-
mediaType: 'audio/wav',
|
|
33
|
-
size: BigInt(1024),
|
|
34
|
-
meta: { storagePath: '/data/files/test.wav' },
|
|
35
|
-
createdAt: new Date(),
|
|
36
|
-
updatedAt: new Date(),
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
describe('GET /file/:id', () => {
|
|
40
|
-
it('should stream file content successfully', async () => {
|
|
41
|
-
prisma.file.findUnique.mockResolvedValue(mockFile);
|
|
42
|
-
|
|
43
|
-
const mockStream = Readable.from(['test file content']);
|
|
44
|
-
const mockResult: FileResult = {
|
|
45
|
-
type: 'stream',
|
|
46
|
-
stream: mockStream,
|
|
47
|
-
metadata: {
|
|
48
|
-
contentType: 'audio/wav',
|
|
49
|
-
contentLength: 17,
|
|
50
|
-
etag: '"abc123"',
|
|
51
|
-
lastModified: new Date('2025-01-01'),
|
|
52
|
-
},
|
|
53
|
-
};
|
|
54
|
-
vi.mocked(mockFileHandler.get).mockResolvedValue(mockResult);
|
|
55
|
-
|
|
56
|
-
const response = await fastify.inject({
|
|
57
|
-
method: 'GET',
|
|
58
|
-
url: `/file/${encodeURIComponent('http://example.com/file/test.wav')}`,
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
expect(response.statusCode).toBe(200);
|
|
62
|
-
expect(response.headers['content-type']).toBe('audio/wav');
|
|
63
|
-
expect(response.headers['content-length']).toBe('17');
|
|
64
|
-
expect(response.headers.etag).toBe('"abc123"');
|
|
65
|
-
expect(response.headers['content-disposition']).toBe('inline; filename="test.wav"');
|
|
66
|
-
expect(response.body).toBe('test file content');
|
|
67
|
-
|
|
68
|
-
expect(mockFileHandler.get).toHaveBeenCalledWith(
|
|
69
|
-
mockFile,
|
|
70
|
-
expect.objectContaining({
|
|
71
|
-
request: expect.any(Object),
|
|
72
|
-
fastify: expect.any(Object),
|
|
73
|
-
}),
|
|
74
|
-
);
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
it('should handle attachment disposition', async () => {
|
|
78
|
-
prisma.file.findUnique.mockResolvedValue(mockFile);
|
|
79
|
-
|
|
80
|
-
const mockStream = Readable.from(['test']);
|
|
81
|
-
const mockResult: FileResult = {
|
|
82
|
-
type: 'stream',
|
|
83
|
-
stream: mockStream,
|
|
84
|
-
metadata: {
|
|
85
|
-
contentType: 'audio/wav',
|
|
86
|
-
contentLength: 4,
|
|
87
|
-
},
|
|
88
|
-
};
|
|
89
|
-
vi.mocked(mockFileHandler.get).mockResolvedValue(mockResult);
|
|
90
|
-
|
|
91
|
-
const response = await fastify.inject({
|
|
92
|
-
method: 'GET',
|
|
93
|
-
url: `/file/${encodeURIComponent('http://example.com/file/test.wav')}?disposition=attachment`,
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
expect(response.statusCode).toBe(200);
|
|
97
|
-
expect(response.headers['content-disposition']).toBe('attachment; filename="test.wav"');
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
it('should use custom filename when provided', async () => {
|
|
101
|
-
prisma.file.findUnique.mockResolvedValue(mockFile);
|
|
102
|
-
|
|
103
|
-
const mockStream = Readable.from(['test']);
|
|
104
|
-
const mockResult: FileResult = {
|
|
105
|
-
type: 'stream',
|
|
106
|
-
stream: mockStream,
|
|
107
|
-
metadata: {
|
|
108
|
-
contentType: 'audio/wav',
|
|
109
|
-
contentLength: 4,
|
|
110
|
-
},
|
|
111
|
-
};
|
|
112
|
-
vi.mocked(mockFileHandler.get).mockResolvedValue(mockResult);
|
|
113
|
-
|
|
114
|
-
const response = await fastify.inject({
|
|
115
|
-
method: 'GET',
|
|
116
|
-
url: `/file/${encodeURIComponent('http://example.com/file/test.wav')}?filename=custom-name.wav`,
|
|
117
|
-
});
|
|
118
|
-
|
|
119
|
-
expect(response.statusCode).toBe(200);
|
|
120
|
-
expect(response.headers['content-disposition']).toBe('inline; filename="custom-name.wav"');
|
|
121
|
-
});
|
|
122
|
-
|
|
123
|
-
it('should handle redirect response with noRedirect=false (default)', async () => {
|
|
124
|
-
prisma.file.findUnique.mockResolvedValue(mockFile);
|
|
125
|
-
|
|
126
|
-
const mockResult: FileResult = {
|
|
127
|
-
type: 'redirect',
|
|
128
|
-
url: 'https://storage.example.com/files/test.wav',
|
|
129
|
-
};
|
|
130
|
-
vi.mocked(mockFileHandler.get).mockResolvedValue(mockResult);
|
|
131
|
-
|
|
132
|
-
const response = await fastify.inject({
|
|
133
|
-
method: 'GET',
|
|
134
|
-
url: `/file/${encodeURIComponent('http://example.com/file/test.wav')}`,
|
|
135
|
-
});
|
|
136
|
-
|
|
137
|
-
expect(response.statusCode).toBe(302);
|
|
138
|
-
expect(response.headers.location).toBe('https://storage.example.com/files/test.wav');
|
|
139
|
-
});
|
|
140
|
-
|
|
141
|
-
it('should handle redirect response with noRedirect=true', async () => {
|
|
142
|
-
prisma.file.findUnique.mockResolvedValue(mockFile);
|
|
143
|
-
|
|
144
|
-
const mockResult: FileResult = {
|
|
145
|
-
type: 'redirect',
|
|
146
|
-
url: 'https://storage.example.com/files/test.wav',
|
|
147
|
-
};
|
|
148
|
-
vi.mocked(mockFileHandler.get).mockResolvedValue(mockResult);
|
|
149
|
-
|
|
150
|
-
const response = await fastify.inject({
|
|
151
|
-
method: 'GET',
|
|
152
|
-
url: `/file/${encodeURIComponent('http://example.com/file/test.wav')}?noRedirect=true`,
|
|
153
|
-
});
|
|
154
|
-
|
|
155
|
-
expect(response.statusCode).toBe(200);
|
|
156
|
-
const body = JSON.parse(response.body);
|
|
157
|
-
expect(body).toEqual({ location: 'https://storage.example.com/files/test.wav' });
|
|
158
|
-
});
|
|
159
|
-
|
|
160
|
-
it('should handle file path response without nginx X-Accel-Redirect', async () => {
|
|
161
|
-
prisma.file.findUnique.mockResolvedValue(mockFile);
|
|
162
|
-
|
|
163
|
-
const mockStream = Readable.from(['file content']);
|
|
164
|
-
vi.mocked(createReadStream).mockReturnValue(mockStream as never);
|
|
165
|
-
|
|
166
|
-
const mockResult: FileResult = {
|
|
167
|
-
type: 'file',
|
|
168
|
-
path: '/data/files/test.wav',
|
|
169
|
-
metadata: {
|
|
170
|
-
contentType: 'audio/wav',
|
|
171
|
-
contentLength: 1024,
|
|
172
|
-
},
|
|
173
|
-
};
|
|
174
|
-
vi.mocked(mockFileHandler.get).mockResolvedValue(mockResult);
|
|
175
|
-
|
|
176
|
-
const response = await fastify.inject({
|
|
177
|
-
method: 'GET',
|
|
178
|
-
url: `/file/${encodeURIComponent('http://example.com/file/test.wav')}`,
|
|
179
|
-
});
|
|
180
|
-
|
|
181
|
-
expect(response.statusCode).toBe(200);
|
|
182
|
-
expect(response.headers['content-type']).toBe('audio/wav');
|
|
183
|
-
expect(response.headers['content-length']).toBe('1024');
|
|
184
|
-
expect(response.headers['x-accel-redirect']).toBeUndefined();
|
|
185
|
-
expect(createReadStream).toHaveBeenCalledWith('/data/files/test.wav');
|
|
186
|
-
});
|
|
187
|
-
|
|
188
|
-
it('should handle file path response with nginx X-Accel-Redirect', async () => {
|
|
189
|
-
prisma.file.findUnique.mockResolvedValue(mockFile);
|
|
190
|
-
|
|
191
|
-
const mockResult: FileResult = {
|
|
192
|
-
type: 'file',
|
|
193
|
-
path: '/data/files/test.wav',
|
|
194
|
-
accelPath: '/internal/files/test.wav',
|
|
195
|
-
metadata: {
|
|
196
|
-
contentType: 'audio/wav',
|
|
197
|
-
contentLength: 1024,
|
|
198
|
-
},
|
|
199
|
-
};
|
|
200
|
-
vi.mocked(mockFileHandler.get).mockResolvedValue(mockResult);
|
|
201
|
-
|
|
202
|
-
const response = await fastify.inject({
|
|
203
|
-
method: 'GET',
|
|
204
|
-
url: `/file/${encodeURIComponent('http://example.com/file/test.wav')}`,
|
|
205
|
-
});
|
|
206
|
-
|
|
207
|
-
expect(response.statusCode).toBe(200);
|
|
208
|
-
expect(response.headers['content-type']).toBe('audio/wav');
|
|
209
|
-
// Note: Content-Length is '0' because Fastify sends an empty response.
|
|
210
|
-
// In production, nginx will replace this with the actual file size when serving via X-Accel-Redirect
|
|
211
|
-
expect(response.headers['content-length']).toBe('0');
|
|
212
|
-
expect(response.headers['x-accel-redirect']).toBe('/internal/files/test.wav');
|
|
213
|
-
expect(response.body).toBe(''); // Empty body when using X-Accel-Redirect
|
|
214
|
-
});
|
|
215
|
-
|
|
216
|
-
it('should return 404 when entity not found', async () => {
|
|
217
|
-
prisma.file.findUnique.mockResolvedValue(null);
|
|
218
|
-
|
|
219
|
-
const response = await fastify.inject({
|
|
220
|
-
method: 'GET',
|
|
221
|
-
url: `/file/${encodeURIComponent('http://example.com/file/nonexistent')}`,
|
|
222
|
-
});
|
|
223
|
-
const body = JSON.parse(response.body) as { error: { code: string; message: string } };
|
|
224
|
-
|
|
225
|
-
expect(response.statusCode).toBe(404);
|
|
226
|
-
expect(body.error.code).toBe('NOT_FOUND');
|
|
227
|
-
expect(body.error.message).toBe('The requested file was not found');
|
|
228
|
-
expect(mockFileHandler.get).not.toHaveBeenCalled();
|
|
229
|
-
});
|
|
230
|
-
|
|
231
|
-
it('should return 500 when database error occurs', async () => {
|
|
232
|
-
prisma.file.findUnique.mockRejectedValue(new Error('Database connection failed'));
|
|
233
|
-
|
|
234
|
-
const response = await fastify.inject({
|
|
235
|
-
method: 'GET',
|
|
236
|
-
url: `/file/${encodeURIComponent('http://example.com/file/test.wav')}`,
|
|
237
|
-
});
|
|
238
|
-
const body = JSON.parse(response.body) as { error: { code: string } };
|
|
239
|
-
|
|
240
|
-
expect(response.statusCode).toBe(500);
|
|
241
|
-
expect(body.error.code).toBe('INTERNAL_ERROR');
|
|
242
|
-
});
|
|
243
|
-
|
|
244
|
-
it('should return 500 when fileHandler throws error', async () => {
|
|
245
|
-
prisma.file.findUnique.mockResolvedValue(mockFile);
|
|
246
|
-
vi.mocked(mockFileHandler.get).mockRejectedValue(new Error('File not found in storage'));
|
|
247
|
-
|
|
248
|
-
const response = await fastify.inject({
|
|
249
|
-
method: 'GET',
|
|
250
|
-
url: `/file/${encodeURIComponent('http://example.com/file/test.wav')}`,
|
|
251
|
-
});
|
|
252
|
-
const body = JSON.parse(response.body) as { error: { code: string } };
|
|
253
|
-
|
|
254
|
-
expect(response.statusCode).toBe(500);
|
|
255
|
-
expect(body.error.code).toBe('INTERNAL_ERROR');
|
|
256
|
-
});
|
|
257
|
-
|
|
258
|
-
it('should validate ID parameter format', async () => {
|
|
259
|
-
const response = await fastify.inject({
|
|
260
|
-
method: 'GET',
|
|
261
|
-
url: '/file/invalid-id',
|
|
262
|
-
});
|
|
263
|
-
const body = JSON.parse(response.body) as StandardErrorResponse;
|
|
264
|
-
|
|
265
|
-
expect(response.statusCode).toBe(400);
|
|
266
|
-
expect(body.error).toBe('Bad Request');
|
|
267
|
-
});
|
|
268
|
-
|
|
269
|
-
it('should validate disposition parameter', async () => {
|
|
270
|
-
prisma.file.findUnique.mockResolvedValue(mockFile);
|
|
271
|
-
|
|
272
|
-
const response = await fastify.inject({
|
|
273
|
-
method: 'GET',
|
|
274
|
-
url: `/file/${encodeURIComponent('http://example.com/file/test.wav')}?disposition=invalid`,
|
|
275
|
-
});
|
|
276
|
-
const body = JSON.parse(response.body) as StandardErrorResponse;
|
|
277
|
-
|
|
278
|
-
expect(response.statusCode).toBe(400);
|
|
279
|
-
expect(body.error).toBe('Bad Request');
|
|
280
|
-
});
|
|
281
|
-
|
|
282
|
-
it('should return 404 when get handler returns false', async () => {
|
|
283
|
-
prisma.file.findUnique.mockResolvedValue(mockFile);
|
|
284
|
-
vi.mocked(mockFileHandler.get).mockResolvedValue(false);
|
|
285
|
-
|
|
286
|
-
const response = await fastify.inject({
|
|
287
|
-
method: 'GET',
|
|
288
|
-
url: `/file/${encodeURIComponent('http://example.com/file/test.wav')}`,
|
|
289
|
-
});
|
|
290
|
-
const body = JSON.parse(response.body) as { error: { code: string; message: string } };
|
|
291
|
-
|
|
292
|
-
expect(response.statusCode).toBe(404);
|
|
293
|
-
expect(body.error.code).toBe('NOT_FOUND');
|
|
294
|
-
expect(body.error.message).toBe('The requested file could not be retrieved');
|
|
295
|
-
});
|
|
296
|
-
});
|
|
297
|
-
|
|
298
|
-
describe('HEAD /file/:id', () => {
|
|
299
|
-
it('should return headers without body using head handler', async () => {
|
|
300
|
-
prisma.file.findUnique.mockResolvedValue(mockFile);
|
|
301
|
-
|
|
302
|
-
const mockMetadata = {
|
|
303
|
-
contentType: 'audio/wav',
|
|
304
|
-
contentLength: 17,
|
|
305
|
-
etag: '"abc123"',
|
|
306
|
-
lastModified: new Date('2025-01-01'),
|
|
307
|
-
};
|
|
308
|
-
vi.mocked(mockFileHandler.head).mockResolvedValue(mockMetadata);
|
|
309
|
-
|
|
310
|
-
const response = await fastify.inject({
|
|
311
|
-
method: 'HEAD',
|
|
312
|
-
url: `/file/${encodeURIComponent('http://example.com/file/test.wav')}`,
|
|
313
|
-
});
|
|
314
|
-
|
|
315
|
-
expect(response.statusCode).toBe(200);
|
|
316
|
-
expect(response.headers['content-type']).toBe('audio/wav');
|
|
317
|
-
expect(response.headers['content-length']).toBe('17');
|
|
318
|
-
expect(response.headers.etag).toBe('"abc123"');
|
|
319
|
-
expect(response.body).toBe(''); // No body for HEAD
|
|
320
|
-
|
|
321
|
-
expect(mockFileHandler.head).toHaveBeenCalledWith(
|
|
322
|
-
mockFile,
|
|
323
|
-
expect.objectContaining({
|
|
324
|
-
request: expect.any(Object),
|
|
325
|
-
fastify: expect.any(Object),
|
|
326
|
-
}),
|
|
327
|
-
);
|
|
328
|
-
});
|
|
329
|
-
|
|
330
|
-
it('should return 404 when file not found', async () => {
|
|
331
|
-
prisma.file.findUnique.mockResolvedValue(null);
|
|
332
|
-
|
|
333
|
-
const response = await fastify.inject({
|
|
334
|
-
method: 'HEAD',
|
|
335
|
-
url: `/file/${encodeURIComponent('http://example.com/file/nonexistent')}`,
|
|
336
|
-
});
|
|
337
|
-
const body = JSON.parse(response.body) as { error: { code: string } };
|
|
338
|
-
|
|
339
|
-
expect(response.statusCode).toBe(404);
|
|
340
|
-
expect(body.error.code).toBe('NOT_FOUND');
|
|
341
|
-
expect(mockFileHandler.get).not.toHaveBeenCalled();
|
|
342
|
-
});
|
|
343
|
-
|
|
344
|
-
it('should return 500 when head handler throws error', async () => {
|
|
345
|
-
prisma.file.findUnique.mockResolvedValue(mockFile);
|
|
346
|
-
vi.mocked(mockFileHandler.head).mockRejectedValue(new Error('Failed to get metadata'));
|
|
347
|
-
|
|
348
|
-
const response = await fastify.inject({
|
|
349
|
-
method: 'HEAD',
|
|
350
|
-
url: `/file/${encodeURIComponent('http://example.com/file/test.wav')}`,
|
|
351
|
-
});
|
|
352
|
-
const body = JSON.parse(response.body) as { error: { code: string } };
|
|
353
|
-
|
|
354
|
-
expect(response.statusCode).toBe(500);
|
|
355
|
-
expect(body.error.code).toBe('INTERNAL_ERROR');
|
|
356
|
-
});
|
|
357
|
-
|
|
358
|
-
it('should return 404 when head handler returns false', async () => {
|
|
359
|
-
prisma.file.findUnique.mockResolvedValue(mockFile);
|
|
360
|
-
vi.mocked(mockFileHandler.head).mockResolvedValue(false);
|
|
361
|
-
|
|
362
|
-
const response = await fastify.inject({
|
|
363
|
-
method: 'HEAD',
|
|
364
|
-
url: `/file/${encodeURIComponent('http://example.com/file/test.wav')}`,
|
|
365
|
-
});
|
|
366
|
-
const body = JSON.parse(response.body) as { error: { code: string; message: string } };
|
|
367
|
-
|
|
368
|
-
expect(response.statusCode).toBe(404);
|
|
369
|
-
expect(body.error.code).toBe('NOT_FOUND');
|
|
370
|
-
expect(body.error.message).toBe('The requested file metadata was not found');
|
|
371
|
-
});
|
|
372
|
-
});
|
|
373
|
-
|
|
374
|
-
describe('GET /file/:id - exhaustiveness check', () => {
|
|
375
|
-
it('should handle unexpected file result type', async () => {
|
|
376
|
-
prisma.file.findUnique.mockResolvedValue(mockFile);
|
|
377
|
-
|
|
378
|
-
// Mock an invalid result type to test exhaustiveness check
|
|
379
|
-
const invalidResult = {
|
|
380
|
-
type: 'invalid',
|
|
381
|
-
metadata: { contentType: 'audio/wav', contentLength: 1024 },
|
|
382
|
-
};
|
|
383
|
-
vi.mocked(mockFileHandler.get).mockResolvedValue(invalidResult as never);
|
|
384
|
-
|
|
385
|
-
const response = await fastify.inject({
|
|
386
|
-
method: 'GET',
|
|
387
|
-
url: `/file/${encodeURIComponent('http://example.com/file/test.wav')}`,
|
|
388
|
-
});
|
|
389
|
-
const body = JSON.parse(response.body) as { error: { code: string; message: string } };
|
|
390
|
-
|
|
391
|
-
expect(response.statusCode).toBe(500);
|
|
392
|
-
expect(body.error.code).toBe('INTERNAL_ERROR');
|
|
393
|
-
});
|
|
394
|
-
});
|
|
395
|
-
});
|
package/src/routes/files.test.ts
DELETED
|
@@ -1,278 +0,0 @@
|
|
|
1
|
-
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
|
|
2
|
-
import type { FileAccessTransformer, FileTransformer } from '../app.js';
|
|
3
|
-
import { fastify, fastifyAfter, fastifyBefore, prisma } from '../test/helpers/fastify.js';
|
|
4
|
-
import { AllPublicFileAccessTransformer } from '../transformers/default.js';
|
|
5
|
-
import type { StandardErrorResponse } from '../utils/errors.js';
|
|
6
|
-
import filesRoute from './files.js';
|
|
7
|
-
|
|
8
|
-
describe('Files Route', () => {
|
|
9
|
-
beforeEach(async () => {
|
|
10
|
-
await fastifyBefore();
|
|
11
|
-
await fastify.register(filesRoute, { prisma, fileAccessTransformer: AllPublicFileAccessTransformer });
|
|
12
|
-
});
|
|
13
|
-
|
|
14
|
-
afterEach(async () => {
|
|
15
|
-
await fastifyAfter();
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
const mockFile1 = {
|
|
19
|
-
id: 'http://example.com/file1.wav',
|
|
20
|
-
filename: 'file1.wav',
|
|
21
|
-
mediaType: 'audio/wav',
|
|
22
|
-
size: BigInt(1024),
|
|
23
|
-
meta: {},
|
|
24
|
-
createdAt: new Date('2025-01-01'),
|
|
25
|
-
updatedAt: new Date('2025-01-01'),
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
const mockFile2 = {
|
|
29
|
-
id: 'http://example.com/file2.txt',
|
|
30
|
-
filename: 'file2.txt',
|
|
31
|
-
mediaType: 'text/plain',
|
|
32
|
-
size: BigInt(512),
|
|
33
|
-
meta: {},
|
|
34
|
-
createdAt: new Date('2025-01-02'),
|
|
35
|
-
updatedAt: new Date('2025-01-02'),
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
describe('GET /files', () => {
|
|
39
|
-
it('should list all files successfully', async () => {
|
|
40
|
-
prisma.file.findMany.mockResolvedValue([mockFile1, mockFile2]);
|
|
41
|
-
prisma.file.count.mockResolvedValue(2);
|
|
42
|
-
|
|
43
|
-
const response = await fastify.inject({
|
|
44
|
-
method: 'GET',
|
|
45
|
-
url: '/files',
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
expect(response.statusCode).toBe(200);
|
|
49
|
-
const body = JSON.parse(response.body) as { total: number; files: unknown[] };
|
|
50
|
-
expect(body.total).toBe(2);
|
|
51
|
-
expect(body.files).toHaveLength(2);
|
|
52
|
-
expect(body.files[0]).toMatchObject({
|
|
53
|
-
id: 'http://example.com/file1.wav',
|
|
54
|
-
filename: 'file1.wav',
|
|
55
|
-
mediaType: 'audio/wav',
|
|
56
|
-
size: 1024,
|
|
57
|
-
access: {
|
|
58
|
-
content: true,
|
|
59
|
-
},
|
|
60
|
-
});
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
it('should filter files by memberOf', async () => {
|
|
64
|
-
prisma.file.findMany.mockResolvedValue([mockFile1]);
|
|
65
|
-
prisma.file.count.mockResolvedValue(1);
|
|
66
|
-
|
|
67
|
-
const response = await fastify.inject({
|
|
68
|
-
method: 'GET',
|
|
69
|
-
url: '/files?memberOf=http://example.com/collection/1',
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
expect(response.statusCode).toBe(200);
|
|
73
|
-
const body = JSON.parse(response.body) as { total: number; files: unknown[] };
|
|
74
|
-
expect(body.total).toBe(1);
|
|
75
|
-
expect(body.files).toHaveLength(1);
|
|
76
|
-
expect(body.files[0]).toMatchObject({
|
|
77
|
-
id: 'http://example.com/file1.wav',
|
|
78
|
-
filename: 'file1.wav',
|
|
79
|
-
});
|
|
80
|
-
expect(prisma.file.findMany).toHaveBeenCalledWith(
|
|
81
|
-
expect.objectContaining({
|
|
82
|
-
where: { entity: { memberOf: 'http://example.com/collection/1' } },
|
|
83
|
-
}),
|
|
84
|
-
);
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
it('should support pagination with limit and offset', async () => {
|
|
88
|
-
prisma.file.findMany.mockResolvedValue([mockFile2]);
|
|
89
|
-
prisma.file.count.mockResolvedValue(2);
|
|
90
|
-
|
|
91
|
-
const response = await fastify.inject({
|
|
92
|
-
method: 'GET',
|
|
93
|
-
url: '/files?limit=1&offset=1',
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
expect(response.statusCode).toBe(200);
|
|
97
|
-
const body = JSON.parse(response.body) as { total: number; files: unknown[] };
|
|
98
|
-
expect(body.total).toBe(2);
|
|
99
|
-
expect(body.files).toHaveLength(1);
|
|
100
|
-
expect(prisma.file.findMany).toHaveBeenCalledWith(
|
|
101
|
-
expect.objectContaining({
|
|
102
|
-
skip: 1,
|
|
103
|
-
take: 1,
|
|
104
|
-
}),
|
|
105
|
-
);
|
|
106
|
-
});
|
|
107
|
-
|
|
108
|
-
it('should support sorting by filename ascending', async () => {
|
|
109
|
-
prisma.file.findMany.mockResolvedValue([mockFile1, mockFile2]);
|
|
110
|
-
prisma.file.count.mockResolvedValue(2);
|
|
111
|
-
|
|
112
|
-
const response = await fastify.inject({
|
|
113
|
-
method: 'GET',
|
|
114
|
-
url: '/files?sort=filename&order=asc',
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
expect(response.statusCode).toBe(200);
|
|
118
|
-
expect(prisma.file.findMany).toHaveBeenCalledWith(
|
|
119
|
-
expect.objectContaining({
|
|
120
|
-
orderBy: { filename: 'asc' },
|
|
121
|
-
}),
|
|
122
|
-
);
|
|
123
|
-
});
|
|
124
|
-
|
|
125
|
-
it('should support sorting by id directly', async () => {
|
|
126
|
-
prisma.file.findMany.mockResolvedValue([mockFile1, mockFile2]);
|
|
127
|
-
prisma.file.count.mockResolvedValue(2);
|
|
128
|
-
|
|
129
|
-
const response = await fastify.inject({
|
|
130
|
-
method: 'GET',
|
|
131
|
-
url: '/files?sort=id&order=desc',
|
|
132
|
-
});
|
|
133
|
-
|
|
134
|
-
expect(response.statusCode).toBe(200);
|
|
135
|
-
expect(prisma.file.findMany).toHaveBeenCalledWith(
|
|
136
|
-
expect.objectContaining({
|
|
137
|
-
orderBy: { id: 'desc' },
|
|
138
|
-
}),
|
|
139
|
-
);
|
|
140
|
-
});
|
|
141
|
-
|
|
142
|
-
it('should return empty list when no files found', async () => {
|
|
143
|
-
prisma.file.findMany.mockResolvedValue([]);
|
|
144
|
-
prisma.file.count.mockResolvedValue(0);
|
|
145
|
-
|
|
146
|
-
const response = await fastify.inject({
|
|
147
|
-
method: 'GET',
|
|
148
|
-
url: '/files',
|
|
149
|
-
});
|
|
150
|
-
|
|
151
|
-
expect(response.statusCode).toBe(200);
|
|
152
|
-
const body = JSON.parse(response.body) as { total: number; files: unknown[] };
|
|
153
|
-
expect(body.total).toBe(0);
|
|
154
|
-
expect(body.files).toHaveLength(0);
|
|
155
|
-
});
|
|
156
|
-
|
|
157
|
-
it('should return 500 when database error occurs', async () => {
|
|
158
|
-
prisma.file.findMany.mockRejectedValue(new Error('Database connection failed'));
|
|
159
|
-
|
|
160
|
-
const response = await fastify.inject({
|
|
161
|
-
method: 'GET',
|
|
162
|
-
url: '/files',
|
|
163
|
-
});
|
|
164
|
-
const body = JSON.parse(response.body) as { error: { code: string } };
|
|
165
|
-
|
|
166
|
-
expect(response.statusCode).toBe(500);
|
|
167
|
-
expect(body.error.code).toBe('INTERNAL_ERROR');
|
|
168
|
-
});
|
|
169
|
-
|
|
170
|
-
it('should validate limit parameter', async () => {
|
|
171
|
-
const response = await fastify.inject({
|
|
172
|
-
method: 'GET',
|
|
173
|
-
url: '/files?limit=2000',
|
|
174
|
-
});
|
|
175
|
-
const body = JSON.parse(response.body) as StandardErrorResponse;
|
|
176
|
-
|
|
177
|
-
expect(response.statusCode).toBe(400);
|
|
178
|
-
expect(body.error).toBe('Bad Request');
|
|
179
|
-
});
|
|
180
|
-
|
|
181
|
-
it('should validate memberOf parameter format', async () => {
|
|
182
|
-
const response = await fastify.inject({
|
|
183
|
-
method: 'GET',
|
|
184
|
-
url: '/files?memberOf=invalid-url',
|
|
185
|
-
});
|
|
186
|
-
const body = JSON.parse(response.body) as StandardErrorResponse;
|
|
187
|
-
|
|
188
|
-
expect(response.statusCode).toBe(400);
|
|
189
|
-
expect(body.error).toBe('Bad Request');
|
|
190
|
-
});
|
|
191
|
-
|
|
192
|
-
it('should validate sort parameter', async () => {
|
|
193
|
-
const response = await fastify.inject({
|
|
194
|
-
method: 'GET',
|
|
195
|
-
url: '/files?sort=invalid',
|
|
196
|
-
});
|
|
197
|
-
const body = JSON.parse(response.body) as StandardErrorResponse;
|
|
198
|
-
|
|
199
|
-
expect(response.statusCode).toBe(400);
|
|
200
|
-
expect(body.error).toBe('Bad Request');
|
|
201
|
-
});
|
|
202
|
-
});
|
|
203
|
-
|
|
204
|
-
describe('Custom File Transformers', () => {
|
|
205
|
-
it('should apply custom file transformers', async () => {
|
|
206
|
-
await fastifyBefore();
|
|
207
|
-
|
|
208
|
-
// Custom transformer that adds extra fields
|
|
209
|
-
const customTransformer: FileTransformer = (file) => ({
|
|
210
|
-
...file,
|
|
211
|
-
customField: 'test-value',
|
|
212
|
-
uppercaseFilename: file.filename.toUpperCase(),
|
|
213
|
-
});
|
|
214
|
-
|
|
215
|
-
await fastify.register(filesRoute, {
|
|
216
|
-
prisma,
|
|
217
|
-
fileAccessTransformer: AllPublicFileAccessTransformer,
|
|
218
|
-
fileTransformers: [customTransformer],
|
|
219
|
-
});
|
|
220
|
-
|
|
221
|
-
prisma.file.findMany.mockResolvedValue([mockFile1]);
|
|
222
|
-
prisma.file.count.mockResolvedValue(1);
|
|
223
|
-
|
|
224
|
-
const response = await fastify.inject({
|
|
225
|
-
method: 'GET',
|
|
226
|
-
url: '/files',
|
|
227
|
-
});
|
|
228
|
-
|
|
229
|
-
expect(response.statusCode).toBe(200);
|
|
230
|
-
const body = JSON.parse(response.body) as { files: unknown[] };
|
|
231
|
-
expect(body.files[0]).toMatchObject({
|
|
232
|
-
id: 'http://example.com/file1.wav',
|
|
233
|
-
filename: 'file1.wav',
|
|
234
|
-
customField: 'test-value',
|
|
235
|
-
uppercaseFilename: 'FILE1.WAV',
|
|
236
|
-
});
|
|
237
|
-
|
|
238
|
-
await fastifyAfter();
|
|
239
|
-
});
|
|
240
|
-
|
|
241
|
-
it('should apply custom file access transformer', async () => {
|
|
242
|
-
await fastifyBefore();
|
|
243
|
-
|
|
244
|
-
// Custom access transformer that restricts content access
|
|
245
|
-
const customFileAccessTransformer: FileAccessTransformer = (file) => ({
|
|
246
|
-
...file,
|
|
247
|
-
access: {
|
|
248
|
-
content: false,
|
|
249
|
-
contentAuthorizationUrl: 'https://example.com/request-access',
|
|
250
|
-
},
|
|
251
|
-
});
|
|
252
|
-
|
|
253
|
-
await fastify.register(filesRoute, {
|
|
254
|
-
prisma,
|
|
255
|
-
fileAccessTransformer: customFileAccessTransformer,
|
|
256
|
-
});
|
|
257
|
-
|
|
258
|
-
prisma.file.findMany.mockResolvedValue([mockFile1]);
|
|
259
|
-
prisma.file.count.mockResolvedValue(1);
|
|
260
|
-
|
|
261
|
-
const response = await fastify.inject({
|
|
262
|
-
method: 'GET',
|
|
263
|
-
url: '/files',
|
|
264
|
-
});
|
|
265
|
-
|
|
266
|
-
expect(response.statusCode).toBe(200);
|
|
267
|
-
const body = JSON.parse(response.body) as { files: unknown[] };
|
|
268
|
-
expect(body.files[0]).toMatchObject({
|
|
269
|
-
access: {
|
|
270
|
-
content: false,
|
|
271
|
-
contentAuthorizationUrl: 'https://example.com/request-access',
|
|
272
|
-
},
|
|
273
|
-
});
|
|
274
|
-
|
|
275
|
-
await fastifyAfter();
|
|
276
|
-
});
|
|
277
|
-
});
|
|
278
|
-
});
|