|
| 1 | +const { describe, it, before, after } = require('node:test'); |
| 2 | +const assert = require('assert'); |
| 3 | + |
| 4 | +const BASE = 'http://localhost:3001/api'; |
| 5 | + |
| 6 | +describe('CodeJudge API Tests', () => { |
| 7 | + let token; |
| 8 | + |
| 9 | + it('GET /health returns ok', async () => { |
| 10 | + const res = await fetch(`${BASE}/../api/health`); |
| 11 | + const data = await res.json(); |
| 12 | + assert.equal(data.status, 'ok'); |
| 13 | + }); |
| 14 | + |
| 15 | + it('GET /problems/stats returns numbers', async () => { |
| 16 | + const res = await fetch(`${BASE}/problems/stats`); |
| 17 | + const data = await res.json(); |
| 18 | + assert.ok(typeof data.total === 'number'); |
| 19 | + assert.ok(data.total >= 10000); |
| 20 | + }); |
| 21 | + |
| 22 | + it('POST /auth/login with valid credentials returns token', async () => { |
| 23 | + const res = await fetch(`${BASE}/auth/login`, { |
| 24 | + method: 'POST', |
| 25 | + headers: { 'Content-Type': 'application/json' }, |
| 26 | + body: JSON.stringify({ email: 'admin@oj.com', password: 'admin123' }), |
| 27 | + }); |
| 28 | + const data = await res.json(); |
| 29 | + assert.ok(data.token); |
| 30 | + assert.equal(data.user.role, 'admin'); |
| 31 | + token = data.token; |
| 32 | + }); |
| 33 | + |
| 34 | + it('GET /auth/profile requires auth', async () => { |
| 35 | + const res = await fetch(`${BASE}/auth/profile`); |
| 36 | + assert.equal(res.status, 401); |
| 37 | + }); |
| 38 | + |
| 39 | + it('GET /auth/profile with token returns user', async () => { |
| 40 | + const res = await fetch(`${BASE}/auth/profile`, { |
| 41 | + headers: { Authorization: `Bearer ${token}` }, |
| 42 | + }); |
| 43 | + const data = await res.json(); |
| 44 | + assert.equal(data.role, 'admin'); |
| 45 | + }); |
| 46 | + |
| 47 | + it('GET /problems returns paginated list', async () => { |
| 48 | + const res = await fetch(`${BASE}/problems?limit=5&page=1`); |
| 49 | + const data = await res.json(); |
| 50 | + assert.equal(data.problems.length, 5); |
| 51 | + assert.ok(data.total >= 10000); |
| 52 | + }); |
| 53 | + |
| 54 | + it('GET /problems/:id returns problem', async () => { |
| 55 | + const res = await fetch(`${BASE}/problems/1`); |
| 56 | + const data = await res.json(); |
| 57 | + assert.ok(data.title); |
| 58 | + }); |
| 59 | + |
| 60 | + it('GET /auth/leaderboard returns array', async () => { |
| 61 | + const res = await fetch(`${BASE}/auth/leaderboard`); |
| 62 | + const data = await res.json(); |
| 63 | + assert.ok(Array.isArray(data)); |
| 64 | + }); |
| 65 | + |
| 66 | + it('POST /submissions choice answer works', async () => { |
| 67 | + const res = await fetch(`${BASE}/submissions`, { |
| 68 | + method: 'POST', |
| 69 | + headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${token}` }, |
| 70 | + body: JSON.stringify({ problem_id: 5000, answer: 0 }), |
| 71 | + }); |
| 72 | + const data = await res.json(); |
| 73 | + assert.ok(['accepted', 'wrong_answer'].includes(data.status)); |
| 74 | + }); |
| 75 | +}); |
0 commit comments