test: verify tournaments .get() is sync + add route tests
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 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.6
parent
d9e32123fe
commit
8468c89352
@@ -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')
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user