diff --git a/src/routes/files.test.ts b/src/routes/files.test.ts index 230e090..5b45b37 100644 --- a/src/routes/files.test.ts +++ b/src/routes/files.test.ts @@ -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' } }, }), ); }); diff --git a/src/routes/files.ts b/src/routes/files.ts index 39e1720..f524690 100644 --- a/src/routes/files.ts +++ b/src/routes/files.ts @@ -37,7 +37,7 @@ const files: FastifyPluginAsync = async (fastify, opts) => { const where: NonNullable[0]>['where'] = {}; if (memberOf) { - where.entity = { id: memberOf }; + where.entity = { memberOf }; } const [dbFiles, total] = await Promise.all([ diff --git a/src/test/integration.setup.ts b/src/test/integration.setup.ts index b8bf42e..3ee71de 100644 --- a/src/test/integration.setup.ts +++ b/src/test/integration.setup.ts @@ -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: { diff --git a/src/test/integration.test.ts b/src/test/integration.test.ts index 9d92426..4a42b8c 100644 --- a/src/test/integration.test.ts +++ b/src/test/integration.test.ts @@ -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, @@ -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();