added tests

This commit is contained in:
Karma Riuk
2025-04-28 16:27:11 +02:00
parent ac6518754f
commit 770d9b8f64
3 changed files with 105 additions and 2 deletions

8
jest.config.js Normal file
View File

@ -0,0 +1,8 @@
export default {
transform: {},
moduleNameMapper: {
'^(\\.{1,2}/.*)\\.js$': '$1',
},
testEnvironment: 'node',
verbose: true
};

View File

@ -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"
}
}

View File

@ -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');
});
});
});