From 19c4b7dc378aad56db6c87dcd13886ea5f19eb6a Mon Sep 17 00:00:00 2001 From: cottongin Date: Sun, 15 Mar 2026 18:53:07 -0400 Subject: [PATCH] test: regression tests for GET /api/games vote fields Made-with: Cursor --- tests/api/regression-games.test.js | 62 ++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 tests/api/regression-games.test.js diff --git a/tests/api/regression-games.test.js b/tests/api/regression-games.test.js new file mode 100644 index 0000000..1dbdd03 --- /dev/null +++ b/tests/api/regression-games.test.js @@ -0,0 +1,62 @@ +const request = require('supertest'); +const { app } = require('../../backend/server'); +const { cleanDb, seedGame } = require('../helpers/test-utils'); +const db = require('../../backend/database'); + +describe('GET /api/games (regression)', () => { + beforeEach(() => { + cleanDb(); + }); + + test('returns games with vote fields', async () => { + seedGame({ + title: 'Quiplash 3', + upvotes: 10, + downvotes: 3, + popularity_score: 7, + }); + + const res = await request(app).get('/api/games'); + + expect(res.status).toBe(200); + expect(res.body).toHaveLength(1); + expect(res.body[0]).toEqual( + expect.objectContaining({ + title: 'Quiplash 3', + upvotes: 10, + downvotes: 3, + popularity_score: 7, + }) + ); + }); + + test('GET /api/games/:id returns vote fields', async () => { + const game = seedGame({ + title: 'Drawful 2', + upvotes: 5, + downvotes: 2, + popularity_score: 3, + }); + + const res = await request(app).get(`/api/games/${game.id}`); + + expect(res.status).toBe(200); + expect(res.body.upvotes).toBe(5); + expect(res.body.downvotes).toBe(2); + expect(res.body.popularity_score).toBe(3); + }); + + test('vote aggregates update correctly after recording votes', async () => { + const game = seedGame({ title: 'Fibbage 4' }); + + db.prepare('UPDATE games SET upvotes = upvotes + 1, popularity_score = popularity_score + 1 WHERE id = ?').run(game.id); + db.prepare('UPDATE games SET upvotes = upvotes + 1, popularity_score = popularity_score + 1 WHERE id = ?').run(game.id); + db.prepare('UPDATE games SET downvotes = downvotes + 1, popularity_score = popularity_score - 1 WHERE id = ?').run(game.id); + + const res = await request(app).get(`/api/games/${game.id}`); + + expect(res.body.upvotes).toBe(2); + expect(res.body.downvotes).toBe(1); + expect(res.body.popularity_score).toBe(1); + }); +});