From 8468c893526075023b99dc9ac945b7b151411954 Mon Sep 17 00:00:00 2001 From: Dorian Date: Thu, 12 Mar 2026 22:32:37 +0000 Subject: [PATCH] test: verify tournaments .get() is sync + add route tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit better-sqlite3 driver is synchronous — .get() does NOT need await. Added tests for unknown pubkey (404) and missing pubkey (400) on join. Co-Authored-By: Claude Opus 4.6 --- server/src/routes/tournaments.test.ts | 42 +++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 server/src/routes/tournaments.test.ts diff --git a/server/src/routes/tournaments.test.ts b/server/src/routes/tournaments.test.ts new file mode 100644 index 0000000..af5b9b4 --- /dev/null +++ b/server/src/routes/tournaments.test.ts @@ -0,0 +1,42 @@ +import { describe, it, expect } from 'vitest' +import { Hono } from 'hono' +import { tournamentsRouter } from './tournaments.js' + +// The router uses the global db from db/index.js +// Since better-sqlite3 is synchronous, .get() does NOT need await — verified. + +const app = new Hono() +app.route('/api/tournaments', tournamentsRouter) + +describe('tournaments router', () => { + it('returns 404 for unknown pubkey on tournament join', async () => { + // First create a tournament so the join endpoint doesn't fail on "tournament not found" + // We need to go through the actual routes using the global DB + + // Try joining a nonexistent tournament with a random pubkey + const res = await app.request('/api/tournaments/nonexistent-id/join', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ pubkey: 'deadbeef0000000000000000000000000000000000000000000000000000cafe' }), + }) + + // Tournament doesn't exist, so joinTournament throws "Tournament not found" → 400 + // But since the bot lookup happens BEFORE joinTournament, and the pubkey doesn't match + // any bot, we get 404 first + expect(res.status).toBe(404) + const json = await res.json() as { error: string } + expect(json.error).toBe('Bot not found for this pubkey') + }) + + it('returns 400 when pubkey missing from join request', async () => { + const res = await app.request('/api/tournaments/some-id/join', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({}), + }) + + expect(res.status).toBe(400) + const json = await res.json() as { error: string } + expect(json.error).toBe('pubkey required') + }) +})