Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/routes/files.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,16 @@ describe('Files Route', () => {
});

expect(response.statusCode).toBe(200);
const body = JSON.parse(response.body) as { total: number };
const body = JSON.parse(response.body) as { total: number; files: unknown[] };
expect(body.total).toBe(1);
expect(body.files).toHaveLength(1);
expect(body.files[0]).toMatchObject({
id: 'http://example.com/file1.wav',
filename: 'file1.wav',
});
expect(prisma.file.findMany).toHaveBeenCalledWith(
expect.objectContaining({
where: { entity: { id: 'http://example.com/collection/1' } },
where: { entity: { memberOf: 'http://example.com/collection/1' } },
}),
);
});
Expand Down
2 changes: 1 addition & 1 deletion src/routes/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const files: FastifyPluginAsync<FilesRouteOptions> = async (fastify, opts) => {
const where: NonNullable<Parameters<typeof prisma.file.findMany>[0]>['where'] = {};

if (memberOf) {
where.entity = { id: memberOf };
where.entity = { memberOf };
}

const [dbFiles, total] = await Promise.all([
Expand Down
25 changes: 25 additions & 0 deletions src/test/integration.setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,31 @@ export async function seedTestData() {
data: testEntities,
});

const testFiles = [
{
id: 'http://example.com/entity/4',
filename: 'test-audio.wav',
mediaType: 'audio/wav',
size: BigInt(2048),
meta: {},
createdAt: new Date(),
updatedAt: new Date(),
},
{
id: 'http://example.com/entity/5',
filename: 'collection-metadata.csv',
mediaType: 'text/csv',
size: BigInt(512),
meta: {},
createdAt: new Date(),
updatedAt: new Date(),
},
];

await prisma.file.createMany({
data: testFiles,
});

await opensearch.indices.create({
index: 'entities',
body: {
Expand Down
73 changes: 72 additions & 1 deletion src/test/integration.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it } from 'vitest';
import type { AuthorisedEntity } from '../transformers/default.js';
import type { AuthorisedEntity, AuthorisedFile } from '../transformers/default.js';
import type { StandardErrorResponse } from '../utils/errors.js';
import {
cleanupTestData,
Expand Down Expand Up @@ -235,6 +235,77 @@ describe('Integration Tests', () => {
});
});

describe('GET /files', () => {
it('should return all files', async () => {
const app = getTestApp();

const response = await app.inject({
method: 'GET',
url: '/files',
});
const body = JSON.parse(response.body) as { total: number; files: AuthorisedFile[] };

expect(response.statusCode).toBe(200);
expect(body.total).toBe(2);
expect(body.files).toHaveLength(2);
});

it('should filter files by memberOf (Object parent)', async () => {
const app = getTestApp();

const response = await app.inject({
method: 'GET',
url: '/files',
query: {
memberOf: 'http://example.com/entity/2',
},
});
const body = JSON.parse(response.body) as { total: number; files: AuthorisedFile[] };

expect(response.statusCode).toBe(200);
expect(body.total).toBe(1);
expect(body.files).toHaveLength(1);
expect(body.files[0].id).toBe('http://example.com/entity/4');
expect(body.files[0].filename).toBe('test-audio.wav');
});

it('should filter files by memberOf (Collection parent)', async () => {
const app = getTestApp();

const response = await app.inject({
method: 'GET',
url: '/files',
query: {
memberOf: 'http://example.com/entity/1',
},
});
const body = JSON.parse(response.body) as { total: number; files: AuthorisedFile[] };

expect(response.statusCode).toBe(200);
expect(body.total).toBe(1);
expect(body.files).toHaveLength(1);
expect(body.files[0].id).toBe('http://example.com/entity/5');
expect(body.files[0].filename).toBe('collection-metadata.csv');
});

it('should return empty list when memberOf matches no entity', async () => {
const app = getTestApp();

const response = await app.inject({
method: 'GET',
url: '/files',
query: {
memberOf: 'http://example.com/entity/does-not-exist',
},
});
const body = JSON.parse(response.body) as { total: number; files: AuthorisedFile[] };

expect(response.statusCode).toBe(200);
expect(body.total).toBe(0);
expect(body.files).toHaveLength(0);
});
});

describe('POST /search', () => {
it('should perform basic search', async () => {
const app = getTestApp();
Expand Down
Loading