test: verify correct/incorrect feedback in respond endpoint (BUG-1)
Tests confirm checkAnswer integration: correct answer returns correct: true, wrong answer returns correct: false. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.6
parent
004413457d
commit
5e0bc1dc00
@@ -0,0 +1,78 @@
|
||||
import { describe, it, expect, beforeEach, afterEach } from 'vitest'
|
||||
import { Hono } from 'hono'
|
||||
import { fightsRouter } from './fights.js'
|
||||
import { waitForHumanResponse, submitHumanResponse, clearAllPending } from '../engine/human-responses.js'
|
||||
import type { Challenge } from '../engine/challenges.js'
|
||||
|
||||
const app = new Hono()
|
||||
app.route('/api/fights', fightsRouter)
|
||||
|
||||
const testChallenge: Challenge = {
|
||||
type: 'speed_blitz',
|
||||
label: 'Speed Blitz',
|
||||
prompt: 'What is 2+2?',
|
||||
answers: ['4'],
|
||||
scoring: 'factual',
|
||||
timeout_ms: 8000,
|
||||
baseDamage: 20,
|
||||
choices: ['4', '3', '5'],
|
||||
}
|
||||
|
||||
afterEach(() => {
|
||||
clearAllPending()
|
||||
})
|
||||
|
||||
describe('POST /fights/:fightId/respond/:botId', () => {
|
||||
it('returns correct: true for correct answer', async () => {
|
||||
const fightId = `fight-respond-correct-${Date.now()}`
|
||||
const botId = 'bot-respond-test'
|
||||
|
||||
// Set up pending challenge
|
||||
const { promise } = waitForHumanResponse(fightId, botId, testChallenge, 1)
|
||||
|
||||
const res = await app.request(`/api/fights/${fightId}/respond/${botId}`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ answer: '4' }),
|
||||
})
|
||||
|
||||
expect(res.status).toBe(200)
|
||||
const json = await res.json() as { accepted: boolean; correct: boolean }
|
||||
expect(json.accepted).toBe(true)
|
||||
expect(json.correct).toBe(true)
|
||||
|
||||
await promise // consume
|
||||
})
|
||||
|
||||
it('returns correct: false for wrong answer', async () => {
|
||||
const fightId = `fight-respond-wrong-${Date.now()}`
|
||||
const botId = 'bot-respond-wrong'
|
||||
|
||||
const { promise } = waitForHumanResponse(fightId, botId, testChallenge, 1)
|
||||
|
||||
const res = await app.request(`/api/fights/${fightId}/respond/${botId}`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ answer: 'banana' }),
|
||||
})
|
||||
|
||||
expect(res.status).toBe(200)
|
||||
const json = await res.json() as { accepted: boolean; correct: boolean }
|
||||
expect(json.accepted).toBe(true)
|
||||
expect(json.correct).toBe(false)
|
||||
|
||||
await promise
|
||||
})
|
||||
|
||||
it('returns 404 for no pending challenge', async () => {
|
||||
const res = await app.request('/api/fights/nonexistent/respond/nonexistent', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ answer: 'test' }),
|
||||
})
|
||||
|
||||
expect(res.status).toBe(404)
|
||||
const json = await res.json() as { error: string }
|
||||
expect(json.error).toContain('No pending challenge')
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user