Files
kilo-cv/tests/regression/api-snapshot.test.js
2026-02-23 13:48:13 +01:00

105 lines
3.3 KiB
JavaScript

import { describe, it, expect, beforeAll, afterAll } from 'vitest';
import request from 'supertest';
import express from 'express';
import cors from 'cors';
import { mkdirSync, existsSync, rmSync } from 'fs';
import { join } from 'path';
import Database from 'better-sqlite3';
const testDbPath = join(process.cwd(), 'tests', 'regression', 'api-snapshots', 'test.db');
const snapshotDir = join(process.cwd(), 'tests', 'regression', 'api-snapshots');
let app;
let db;
function setupTestDB() {
if (!existsSync(snapshotDir)) {
mkdirSync(snapshotDir, { recursive: true });
}
if (existsSync(testDbPath)) {
rmSync(testDbPath);
}
db = new Database(testDbPath);
db.exec(`
CREATE TABLE IF NOT EXISTS cv_data (
id INTEGER PRIMARY KEY CHECK (id = 1),
data TEXT NOT NULL,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
)
`);
const defaultData = {
personal: { name: 'Regression Test User', title: 'Test Engineer', email: 'regression@test.com' },
experience: [{ id: 1, role: 'QA Engineer', company: 'Test Corp', period: '2020-2024', type: 'Vollzeit', description: 'Testing', highlights: ['Automated tests'] }],
skills: { 'Testing': ['Vitest', 'Playwright'] },
education: [{ id: 1, degree: 'CS Degree', institution: 'Test University', period: '2016-2020', status: 'Completed' }],
projects: [{ id: 1, name: 'Test Project', description: 'Test framework', url: 'https://test.com', tech: ['Vitest'] }]
};
db.prepare('INSERT INTO cv_data (id, data) VALUES (1, ?)').run(JSON.stringify(defaultData));
}
function createTestApp() {
const app = express();
app.use(cors());
app.use(express.json());
app.get('/api/cv', (req, res) => {
const row = db.prepare('SELECT data FROM cv_data WHERE id = 1').get();
if (!row) return res.status(404).json({ error: 'CV data not found' });
res.json(JSON.parse(row.data));
});
return app;
}
describe('Regression: API Snapshots', () => {
beforeAll(() => {
setupTestDB();
app = createTestApp();
});
afterAll(() => {
if (db) {
db.close();
}
if (existsSync(testDbPath)) {
rmSync(testDbPath);
}
});
it('GET /api/cv response matches snapshot', async () => {
const response = await request(app).get('/api/cv');
expect(response.status).toBe(200);
expect(response.body).toMatchSnapshot('cv-data-response');
});
it('personal section matches snapshot', async () => {
const response = await request(app).get('/api/cv');
expect(response.body.personal).toMatchSnapshot('personal-section');
});
it('experience section matches snapshot', async () => {
const response = await request(app).get('/api/cv');
expect(response.body.experience).toMatchSnapshot('experience-section');
});
it('skills section matches snapshot', async () => {
const response = await request(app).get('/api/cv');
expect(response.body.skills).toMatchSnapshot('skills-section');
});
it('education section matches snapshot', async () => {
const response = await request(app).get('/api/cv');
expect(response.body.education).toMatchSnapshot('education-section');
});
it('projects section matches snapshot', async () => {
const response = await request(app).get('/api/cv');
expect(response.body.projects).toMatchSnapshot('projects-section');
});
});