diff --git a/jest.config.js b/jest.config.js new file mode 100644 index 0000000..630e300 --- /dev/null +++ b/jest.config.js @@ -0,0 +1,8 @@ +export default { + transform: {}, + moduleNameMapper: { + '^(\\.{1,2}/.*)\\.js$': '$1', + }, + testEnvironment: 'node', + verbose: true +}; \ No newline at end of file diff --git a/package.json b/package.json index 221ca22..04454db 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,8 @@ "main": "src/server.js", "scripts": { "start": "node src/server.js", - "dev": "nodemon src/server.js" + "dev": "nodemon src/server.js", + "test": "node --experimental-vm-modules node_modules/jest/bin/jest.js" }, "dependencies": { "express": "^4.18.2", @@ -13,6 +14,8 @@ "dotenv": "^16.3.1" }, "devDependencies": { - "nodemon": "^3.0.2" + "nodemon": "^3.0.2", + "jest": "^29.7.0", + "supertest": "^6.3.4" } } diff --git a/src/routes/__tests__/datasets.test.js b/src/routes/__tests__/datasets.test.js new file mode 100644 index 0000000..8a75e29 --- /dev/null +++ b/src/routes/__tests__/datasets.test.js @@ -0,0 +1,92 @@ +import { jest } from '@jest/globals'; +import express from 'express'; +import request from 'supertest'; +import { join } from 'path'; +import { fileURLToPath } from 'url'; +import { dirname } from 'path'; +import datasetsRouter from '../datasets.js'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); + +// Mock the paths utility +jest.mock('../../utils/paths.js', () => ({ + getProjectPath: (path) => join(__dirname, '../../..', path) +})); + +// Create Express app for testing +const app = express(); +app.use('/datasets', datasetsRouter); + +describe('Datasets Router', () => { + // Mock environment variables + const originalEnv = process.env; + beforeEach(() => { + jest.resetModules(); + process.env = { ...originalEnv }; + process.env.DATA_DIR = './test-data'; + }); + + afterEach(() => { + process.env = originalEnv; + }); + + describe('GET /download/:dataset', () => { + it('should return 400 for invalid dataset name', async () => { + const response = await request(app) + .get('/datasets/download/invalid_dataset') + .expect(400); + + expect(response.body).toEqual({ + error: 'Invalid dataset name' + }); + }); + + it('should download comment_generation without context', async () => { + const response = await request(app) + .get('/datasets/download/comment_generation') + .expect(200); + + expect(response.headers['content-type']).toBe('application/zip'); + expect(response.headers['content-disposition']).toContain('comment_generation_no_context.zip'); + }); + + it('should download comment_generation with context', async () => { + const response = await request(app) + .get('/datasets/download/comment_generation') + .query({ withContext: true }) + .expect(200); + + expect(response.headers['content-type']).toBe('application/zip'); + expect(response.headers['content-disposition']).toContain('comment_generation_with_context.zip'); + }); + + it('should download code_refinement without context', async () => { + const response = await request(app) + .get('/datasets/download/code_refinement') + .expect(200); + + expect(response.headers['content-type']).toBe('application/zip'); + expect(response.headers['content-disposition']).toContain('code_refinement_no_context.zip'); + }); + + it('should download code_refinement with context', async () => { + const response = await request(app) + .get('/datasets/download/code_refinement') + .query({ withContext: true }) + .expect(200); + + expect(response.headers['content-type']).toBe('application/zip'); + expect(response.headers['content-disposition']).toContain('code_refinement_with_context.zip'); + }); + + it('should handle JSON boolean for withContext parameter', async () => { + const response = await request(app) + .get('/datasets/download/comment_generation') + .query({ withContext: 'true' }) + .expect(200); + + expect(response.headers['content-disposition']).toContain('comment_generation_with_context.zip'); + }); + }); +}); \ No newline at end of file