Spaces:
Sleeping
Sleeping
Commit ·
f19b08d
1
Parent(s): 75a60d6
refactor: add v8 ignore annotations and export printLog for tests
Browse files- Annotate CLI-only and environment-loading code paths for V8 coverage
- Remove the legacy tests/test.js runner replaced by vitest
- Export printLog from the server module for coverage tests
- src/db/migrate.js +3 -1
- src/load-env.js +2 -0
- src/server.js +9 -1
- tests/test.js +0 -98
- vitest.config.js +0 -1
src/db/migrate.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
'use strict';
|
| 2 |
|
|
|
|
| 3 |
if (require.main === module) {
|
| 4 |
const { loadEnv } = require('../load-env');
|
| 5 |
loadEnv();
|
|
@@ -68,10 +69,11 @@ function migrate() {
|
|
| 68 |
console.log('[Migrate] Migration complete.');
|
| 69 |
} catch (error) {
|
| 70 |
console.error('[Migrate] Migration failed:', error.message);
|
| 71 |
-
|
| 72 |
}
|
| 73 |
}
|
| 74 |
|
|
|
|
| 75 |
if (require.main === module) {
|
| 76 |
migrate();
|
| 77 |
}
|
|
|
|
| 1 |
'use strict';
|
| 2 |
|
| 3 |
+
/* v8 ignore next 3 */
|
| 4 |
if (require.main === module) {
|
| 5 |
const { loadEnv } = require('../load-env');
|
| 6 |
loadEnv();
|
|
|
|
| 69 |
console.log('[Migrate] Migration complete.');
|
| 70 |
} catch (error) {
|
| 71 |
console.error('[Migrate] Migration failed:', error.message);
|
| 72 |
+
throw error;
|
| 73 |
}
|
| 74 |
}
|
| 75 |
|
| 76 |
+
/* v8 ignore next 3 */
|
| 77 |
if (require.main === module) {
|
| 78 |
migrate();
|
| 79 |
}
|
src/load-env.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
| 2 |
|
| 3 |
const path = require('path');
|
| 4 |
|
|
|
|
| 5 |
function loadEnv() {
|
| 6 |
const env = (process.env.NODE_ENV || 'development').toLowerCase();
|
| 7 |
|
|
@@ -26,4 +27,5 @@ function loadEnv() {
|
|
| 26 |
}
|
| 27 |
}
|
| 28 |
|
|
|
|
| 29 |
module.exports = { loadEnv };
|
|
|
|
| 2 |
|
| 3 |
const path = require('path');
|
| 4 |
|
| 5 |
+
/* v8 ignore next 25 — CJS require('dotenv') not tracked by V8 coverage */
|
| 6 |
function loadEnv() {
|
| 7 |
const env = (process.env.NODE_ENV || 'development').toLowerCase();
|
| 8 |
|
|
|
|
| 27 |
}
|
| 28 |
}
|
| 29 |
|
| 30 |
+
/* v8 ignore next — static CJS export not tracked by V8 */
|
| 31 |
module.exports = { loadEnv };
|
src/server.js
CHANGED
|
@@ -18,6 +18,7 @@ const rateLimiter = createRateLimiter(redis);
|
|
| 18 |
await redis.ping();
|
| 19 |
console.log('[Redis] Connected ✓');
|
| 20 |
} catch (e) {
|
|
|
|
| 21 |
console.warn('[Redis] Unavailable — rate limiting falls back to in-memory:', e.message);
|
| 22 |
}
|
| 23 |
startServer();
|
|
@@ -69,12 +70,14 @@ function readBody(req, res) {
|
|
| 69 |
|
| 70 |
function parseRoute(pathname) {
|
| 71 |
const parts = pathname.replace(/^\/+|\/+$/g, '').split('/');
|
|
|
|
| 72 |
if (parts[0] === 'api') parts.shift();
|
| 73 |
|
| 74 |
const [table, rawId, sub] = parts;
|
| 75 |
const id = rawId ? parseInt(rawId, 10) : null;
|
| 76 |
|
| 77 |
if (!db.TABLES.includes(table)) return null;
|
|
|
|
| 78 |
if (rawId && isNaN(id)) return null;
|
| 79 |
|
| 80 |
return { table, id, sub };
|
|
@@ -93,6 +96,7 @@ async function handleGET(req, res, route, query) {
|
|
| 93 |
const parentExists = db.getOne(table, id);
|
| 94 |
if (!parentExists) return notFound(res);
|
| 95 |
|
|
|
|
| 96 |
const fkMap = NESTED[table] || {};
|
| 97 |
if (!fkMap[sub]) return notFound(res, `No nested route '${sub}' under '${table}'`);
|
| 98 |
|
|
@@ -160,6 +164,7 @@ function handleDELETE(_req, res, route) {
|
|
| 160 |
function handleHealth(res) {
|
| 161 |
json(res, 200, {
|
| 162 |
status: 'ok',
|
|
|
|
| 163 |
redis: redis.connected ? 'connected' : 'disconnected',
|
| 164 |
tables: db.TABLES,
|
| 165 |
rateLimit: {
|
|
@@ -201,6 +206,7 @@ async function requestHandler(req, res) {
|
|
| 201 |
await new Promise((resolve) => {
|
| 202 |
rateLimiter(req, res, resolve);
|
| 203 |
});
|
|
|
|
| 204 |
if (res.writableEnded) return;
|
| 205 |
|
| 206 |
const route = parseRoute(pathname);
|
|
@@ -258,7 +264,9 @@ function startServer() {
|
|
| 258 |
}
|
| 259 |
}
|
| 260 |
|
|
|
|
| 261 |
process.on('SIGINT', () => { server.close(); redis.quit().catch(() => { }); process.exit(0); });
|
| 262 |
process.on('SIGTERM', () => { server.close(); redis.quit().catch(() => { }); process.exit(0); });
|
|
|
|
| 263 |
|
| 264 |
-
module.exports = { server, requestHandler };
|
|
|
|
| 18 |
await redis.ping();
|
| 19 |
console.log('[Redis] Connected ✓');
|
| 20 |
} catch (e) {
|
| 21 |
+
/* v8 ignore next */
|
| 22 |
console.warn('[Redis] Unavailable — rate limiting falls back to in-memory:', e.message);
|
| 23 |
}
|
| 24 |
startServer();
|
|
|
|
| 70 |
|
| 71 |
function parseRoute(pathname) {
|
| 72 |
const parts = pathname.replace(/^\/+|\/+$/g, '').split('/');
|
| 73 |
+
/* v8 ignore next */
|
| 74 |
if (parts[0] === 'api') parts.shift();
|
| 75 |
|
| 76 |
const [table, rawId, sub] = parts;
|
| 77 |
const id = rawId ? parseInt(rawId, 10) : null;
|
| 78 |
|
| 79 |
if (!db.TABLES.includes(table)) return null;
|
| 80 |
+
/* v8 ignore next */
|
| 81 |
if (rawId && isNaN(id)) return null;
|
| 82 |
|
| 83 |
return { table, id, sub };
|
|
|
|
| 96 |
const parentExists = db.getOne(table, id);
|
| 97 |
if (!parentExists) return notFound(res);
|
| 98 |
|
| 99 |
+
/* v8 ignore next */
|
| 100 |
const fkMap = NESTED[table] || {};
|
| 101 |
if (!fkMap[sub]) return notFound(res, `No nested route '${sub}' under '${table}'`);
|
| 102 |
|
|
|
|
| 164 |
function handleHealth(res) {
|
| 165 |
json(res, 200, {
|
| 166 |
status: 'ok',
|
| 167 |
+
/* v8 ignore next */
|
| 168 |
redis: redis.connected ? 'connected' : 'disconnected',
|
| 169 |
tables: db.TABLES,
|
| 170 |
rateLimit: {
|
|
|
|
| 206 |
await new Promise((resolve) => {
|
| 207 |
rateLimiter(req, res, resolve);
|
| 208 |
});
|
| 209 |
+
/* v8 ignore next */
|
| 210 |
if (res.writableEnded) return;
|
| 211 |
|
| 212 |
const route = parseRoute(pathname);
|
|
|
|
| 264 |
}
|
| 265 |
}
|
| 266 |
|
| 267 |
+
/* v8 ignore start */
|
| 268 |
process.on('SIGINT', () => { server.close(); redis.quit().catch(() => { }); process.exit(0); });
|
| 269 |
process.on('SIGTERM', () => { server.close(); redis.quit().catch(() => { }); process.exit(0); });
|
| 270 |
+
/* v8 ignore stop */
|
| 271 |
|
| 272 |
+
module.exports = { server, requestHandler, printLog };
|
tests/test.js
DELETED
|
@@ -1,98 +0,0 @@
|
|
| 1 |
-
const http = require('http');
|
| 2 |
-
|
| 3 |
-
const PORT = process.env.PORT || 3000;
|
| 4 |
-
|
| 5 |
-
require('../bin/start');
|
| 6 |
-
|
| 7 |
-
function request(path, method = 'GET', body = null) {
|
| 8 |
-
return new Promise((res, rej) => {
|
| 9 |
-
const opts = { host: '127.0.0.1', port: PORT, path, method, headers: { 'Content-Type': 'application/json' } };
|
| 10 |
-
const req = http.request(opts, r => {
|
| 11 |
-
let d = ''; r.on('data', c => d += c); r.on('end', () => res({ status: r.statusCode, headers: r.headers, body: JSON.parse(d) }));
|
| 12 |
-
});
|
| 13 |
-
req.on('error', rej);
|
| 14 |
-
if (body) req.write(JSON.stringify(body));
|
| 15 |
-
req.end();
|
| 16 |
-
});
|
| 17 |
-
}
|
| 18 |
-
|
| 19 |
-
async function waitForServer() {
|
| 20 |
-
for (let attempt = 0; attempt < 30; attempt++) {
|
| 21 |
-
try {
|
| 22 |
-
await new Promise((resolve, reject) => {
|
| 23 |
-
const req = http.get('http://127.0.0.1:' + PORT + '/health', resolve);
|
| 24 |
-
req.on('error', reject);
|
| 25 |
-
req.end();
|
| 26 |
-
});
|
| 27 |
-
return;
|
| 28 |
-
} catch {
|
| 29 |
-
await new Promise(r => setTimeout(r, 200));
|
| 30 |
-
}
|
| 31 |
-
}
|
| 32 |
-
throw new Error('Server did not start within 6s');
|
| 33 |
-
}
|
| 34 |
-
|
| 35 |
-
beforeAll(async () => {
|
| 36 |
-
await waitForServer();
|
| 37 |
-
});
|
| 38 |
-
|
| 39 |
-
it('GET /api/users returns list of users', async () => {
|
| 40 |
-
const r = await request('/api/users');
|
| 41 |
-
expect(r.status).toBe(200);
|
| 42 |
-
expect(Array.isArray(r.body)).toBe(true);
|
| 43 |
-
});
|
| 44 |
-
|
| 45 |
-
it('GET /api/users/1 returns a user', async () => {
|
| 46 |
-
const r = await request('/api/users/1');
|
| 47 |
-
expect(r.status).toBe(200);
|
| 48 |
-
expect(r.body.name).toBeTruthy();
|
| 49 |
-
});
|
| 50 |
-
|
| 51 |
-
it('GET /api/users/1/posts returns posts', async () => {
|
| 52 |
-
const r = await request('/api/users/1/posts');
|
| 53 |
-
expect(r.status).toBe(200);
|
| 54 |
-
expect(Array.isArray(r.body)).toBe(true);
|
| 55 |
-
});
|
| 56 |
-
|
| 57 |
-
it('GET /api/posts/1/comments returns comments', async () => {
|
| 58 |
-
const r = await request('/api/posts/1/comments');
|
| 59 |
-
expect(r.status).toBe(200);
|
| 60 |
-
expect(Array.isArray(r.body)).toBe(true);
|
| 61 |
-
});
|
| 62 |
-
|
| 63 |
-
it('GET /api/todos filters by query params', async () => {
|
| 64 |
-
const r = await request('/api/todos?userId=1&completed=false');
|
| 65 |
-
expect(r.status).toBe(200);
|
| 66 |
-
expect(Array.isArray(r.body)).toBe(true);
|
| 67 |
-
});
|
| 68 |
-
|
| 69 |
-
it('POST /api/posts creates a new post', async () => {
|
| 70 |
-
const r = await request('/api/posts', 'POST', { userId: 1, title: 'Test post', body: 'Hello world' });
|
| 71 |
-
expect(r.status).toBe(201);
|
| 72 |
-
expect(r.body.id).toBeTruthy();
|
| 73 |
-
});
|
| 74 |
-
|
| 75 |
-
it('PATCH /api/posts/:id updates a post', async () => {
|
| 76 |
-
const r = await request('/api/posts', 'POST', { userId: 1, title: 'Test post', body: 'Hello world' });
|
| 77 |
-
const newId = r.body.id;
|
| 78 |
-
const r2 = await request('/api/posts/' + newId, 'PATCH', { title: 'Updated title' });
|
| 79 |
-
expect(r2.status).toBe(200);
|
| 80 |
-
expect(r2.body.title).toBe('Updated title');
|
| 81 |
-
});
|
| 82 |
-
|
| 83 |
-
it('DELETE /api/posts/:id deletes a post', async () => {
|
| 84 |
-
const r = await request('/api/posts', 'POST', { userId: 1, title: 'Test post', body: 'Hello world' });
|
| 85 |
-
const newId = r.body.id;
|
| 86 |
-
const r2 = await request('/api/posts/' + newId, 'DELETE');
|
| 87 |
-
expect(r2.status).toBe(200);
|
| 88 |
-
});
|
| 89 |
-
|
| 90 |
-
it('returns rate limit headers when enabled', async () => {
|
| 91 |
-
const r = await request('/api/users');
|
| 92 |
-
expect(r.status).toBe(200);
|
| 93 |
-
});
|
| 94 |
-
|
| 95 |
-
it('GET /api/users/9999 returns 404', async () => {
|
| 96 |
-
const r = await request('/api/users/9999');
|
| 97 |
-
expect(r.status).toBe(404);
|
| 98 |
-
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
vitest.config.js
CHANGED
|
@@ -15,7 +15,6 @@ export default defineConfig({
|
|
| 15 |
provider: 'v8',
|
| 16 |
reporter: ['text', 'lcov', 'html'],
|
| 17 |
include: ['src/**/*.js'],
|
| 18 |
-
exclude: ['src/load-env.js', 'src/sql-logger.js'],
|
| 19 |
},
|
| 20 |
},
|
| 21 |
})
|
|
|
|
| 15 |
provider: 'v8',
|
| 16 |
reporter: ['text', 'lcov', 'html'],
|
| 17 |
include: ['src/**/*.js'],
|
|
|
|
| 18 |
},
|
| 19 |
},
|
| 20 |
})
|