diff --git a/frontend/src/api/client.ts b/frontend/src/api/client.ts index 65ef550..02b3775 100644 --- a/frontend/src/api/client.ts +++ b/frontend/src/api/client.ts @@ -5,9 +5,9 @@ import { mapJoinSessionResponse, mapMixAnswersResponse, mapScoreboardResponse, - mapStartNextRoundResponse, mapSessionDetailResponse, mapShowQuestionResponse, + mapStartNextRoundResponse, mapStartRoundResponse, mapSubmitGuessResponse, mapSubmitLieResponse @@ -21,9 +21,9 @@ import type { JoinSessionResponse, MixAnswersResponse, ScoreboardResponse, - StartNextRoundResponse, SessionDetailResponse, ShowQuestionResponse, + StartNextRoundResponse, StartRoundRequest, StartRoundResponse, SubmitGuessRequest, diff --git a/frontend/src/api/mappers.ts b/frontend/src/api/mappers.ts index 1944ec3..0cab015 100644 --- a/frontend/src/api/mappers.ts +++ b/frontend/src/api/mappers.ts @@ -60,6 +60,17 @@ function readBoolean(record: Record, key: string, path: string) return value; } +function readNullableNumber(record: Record, key: string, path: string): number | null { + const value = record[key]; + if (value === undefined || value === null) { + return null; + } + if (!isNumber(value)) { + throw new Error(`Invalid API contract: expected number|null at ${path}.${key}`); + } + return value; +} + export function mapHealthResponse(payload: unknown): HealthResponse { const root = asRecord(payload, 'health'); return { @@ -130,23 +141,25 @@ function mapSessionDetail(payload: unknown): SessionDetailResponse { }; }), guesses: guessesRaw.map((guess, index) => { - const record = asRecord(guess, `session_detail.reveal.guesses[${index}]`); - const fooledPlayerId = record.fooled_player_id; - if (fooledPlayerId !== null && !isNumber(fooledPlayerId)) { - throw new Error(`Invalid API contract: expected number|null at session_detail.reveal.guesses[${index}].fooled_player_id`); - } + const path = `session_detail.reveal.guesses[${index}]`; + const record = asRecord(guess, path); + const fooledPlayerId = readNullableNumber(record, 'fooled_player_id', path); const fooledPlayerNickname = record.fooled_player_nickname; - if (fooledPlayerNickname !== undefined && !isString(fooledPlayerNickname)) { - throw new Error(`Invalid API contract: expected string at session_detail.reveal.guesses[${index}].fooled_player_nickname`); + if (fooledPlayerId === null) { + if (fooledPlayerNickname !== undefined) { + throw new Error(`Invalid API contract: expected ${path}.fooled_player_nickname to be omitted when fooled_player_id is null`); + } + } else if (!isString(fooledPlayerNickname)) { + throw new Error(`Invalid API contract: expected string at ${path}.fooled_player_nickname when fooled_player_id is set`); } return { - player_id: readNumber(record, 'player_id', `session_detail.reveal.guesses[${index}]`), - nickname: readString(record, 'nickname', `session_detail.reveal.guesses[${index}]`), - selected_text: readString(record, 'selected_text', `session_detail.reveal.guesses[${index}]`), - is_correct: readBoolean(record, 'is_correct', `session_detail.reveal.guesses[${index}]`), + player_id: readNumber(record, 'player_id', path), + nickname: readString(record, 'nickname', path), + selected_text: readString(record, 'selected_text', path), + is_correct: readBoolean(record, 'is_correct', path), fooled_player_id: fooledPlayerId, ...(fooledPlayerNickname === undefined ? {} : { fooled_player_nickname: fooledPlayerNickname }), - created_at: readString(record, 'created_at', `session_detail.reveal.guesses[${index}]`) + created_at: readString(record, 'created_at', path) }; }) }; @@ -388,10 +401,7 @@ export function mapSubmitGuessResponse(payload: unknown): SubmitGuessResponse { const root = asRecord(payload, 'submit_guess'); const guess = asRecord(root.guess, 'submit_guess.guess'); const window = asRecord(root.window, 'submit_guess.window'); - const fooledPlayerId = guess.fooled_player_id; - if (fooledPlayerId !== null && !isNumber(fooledPlayerId)) { - throw new Error('Invalid API contract: expected number|null at submit_guess.guess.fooled_player_id'); - } + const fooledPlayerId = readNullableNumber(guess, 'fooled_player_id', 'submit_guess.guess'); return { guess: { diff --git a/frontend/tests/angular-api-client.test.ts b/frontend/tests/angular-api-client.test.ts index d0f2aef..275f463 100644 --- a/frontend/tests/angular-api-client.test.ts +++ b/frontend/tests/angular-api-client.test.ts @@ -1,6 +1,7 @@ import { describe, expect, it, vi } from 'vitest'; import { createAngularApiClient, type AngularHttpClientLike } from '../src/api/angular-client'; +import { mapSessionDetailResponse, mapSubmitGuessResponse } from '../src/api/mappers'; describe('createAngularApiClient', () => { it('reads health and session detail using Django-compatible endpoints', async () => { @@ -283,6 +284,309 @@ describe('createAngularApiClient', () => { } }); + it('normalizes omitted fooled_player_id to null in canonical reveal payloads', async () => { + const get = vi.fn(async (url: string) => { + if (url === '/lobby/sessions/ABCD12') { + return { + session: { code: 'ABCD12', status: 'reveal', host_id: 1, current_round: 1, players_count: 2 }, + players: [ + { id: 2, nickname: 'Maja', score: 10, is_connected: true }, + { id: 3, nickname: 'Bo', score: 7, is_connected: true } + ], + round_question: { + id: 77, + round_number: 1, + prompt: 'Q?', + shown_at: '2026-03-01T18:00:00Z', + answers: [{ text: 'A' }, { text: 'B' }] + }, + reveal: { + round_question_id: 77, + round_number: 1, + prompt: 'Q?', + correct_answer: 'A', + lies: [], + guesses: [ + { + player_id: 3, + nickname: 'Bo', + selected_text: 'A', + is_correct: true, + created_at: '2026-03-01T18:00:15Z' + } + ] + }, + phase_view_model: { + status: 'reveal', + round_number: 1, + players_count: 2, + constraints: { + min_players_to_start: 2, + max_players_mvp: 8, + min_players_reached: true, + max_players_allowed: true + }, + host: { + can_start_round: false, + can_show_question: false, + can_mix_answers: false, + can_calculate_scores: false, + can_reveal_scoreboard: true, + can_start_next_round: false, + can_finish_game: false + }, + player: { + can_join: true, + can_submit_lie: false, + can_submit_guess: false, + can_view_final_result: false + } + } + } as T; + } + throw { status: 404, error: { error: 'Not found' } }; + }); + + const post = vi.fn(async (url: string, body: unknown) => { + if (url === '/lobby/sessions/ABCD12/questions/77/guesses/submit') { + expect(body).toEqual({ player_id: 9, session_token: 'tok', selected_text: 'A' }); + return { + guess: { + id: 200, + player_id: 9, + round_question_id: 77, + selected_text: 'A', + is_correct: false, + created_at: '2026-03-01T16:01:00Z' + }, + window: { guess_deadline_at: '2026-03-01T16:01:30Z' } + } as T; + } + throw { status: 404, error: { error: 'Not found' } }; + }); + + const client = createAngularApiClient({ get, post } as AngularHttpClientLike); + const session = await client.getSession('abcd12'); + + expect(session.ok).toBe(true); + if (session.ok) { + expect(session.data.reveal?.guesses[0].fooled_player_id).toBeNull(); + expect(session.data.reveal?.guesses[0]).not.toHaveProperty('fooled_player_nickname'); + } + + const submitGuess = await client.submitGuess('ABCD12', 77, { + player_id: 9, + session_token: 'tok', + selected_text: 'A' + }); + expect(submitGuess.ok).toBe(true); + if (submitGuess.ok) { + expect(submitGuess.data.guess.fooled_player_id).toBeNull(); + } + }); + + it('maps omitted fooled_player_id to null in submit guess mapper payloads', () => { + const mapped = mapSubmitGuessResponse({ + guess: { + id: 200, + player_id: 9, + round_question_id: 77, + selected_text: 'A', + is_correct: false, + created_at: '2026-03-01T16:01:00Z' + }, + window: { guess_deadline_at: '2026-03-01T16:01:30Z' } + }); + + expect(mapped.guess.fooled_player_id).toBeNull(); + }); + + it('keeps fooled_player_nickname omitted when canonical reveal payload omits fooled player refs', () => { + const mapped = mapSessionDetailResponse({ + session: { code: 'ABCD12', status: 'reveal', host_id: 1, current_round: 1, players_count: 2 }, + players: [ + { id: 2, nickname: 'Maja', score: 10, is_connected: true }, + { id: 3, nickname: 'Bo', score: 7, is_connected: true } + ], + round_question: { + id: 77, + round_number: 1, + prompt: 'Q?', + shown_at: '2026-03-01T18:00:00Z', + answers: [{ text: 'A' }, { text: 'B' }] + }, + reveal: { + round_question_id: 77, + round_number: 1, + prompt: 'Q?', + correct_answer: 'A', + lies: [], + guesses: [ + { + player_id: 3, + nickname: 'Bo', + selected_text: 'A', + is_correct: true, + created_at: '2026-03-01T18:00:15Z' + } + ] + }, + phase_view_model: { + status: 'reveal', + round_number: 1, + players_count: 2, + constraints: { + min_players_to_start: 2, + max_players_mvp: 8, + min_players_reached: true, + max_players_allowed: true + }, + host: { + can_start_round: false, + can_show_question: false, + can_mix_answers: false, + can_calculate_scores: false, + can_reveal_scoreboard: true, + can_start_next_round: false, + can_finish_game: false + }, + player: { + can_join: true, + can_submit_lie: false, + can_submit_guess: false, + can_view_final_result: false + } + } + }); + + expect(mapped.reveal?.guesses[0].fooled_player_id).toBeNull(); + expect(mapped.reveal?.guesses[0]).not.toHaveProperty('fooled_player_nickname'); + }); + + it('rejects canonical reveal payloads that include fooled_player_nickname without fooled_player_id', () => { + expect(() => + mapSessionDetailResponse({ + session: { code: 'ABCD12', status: 'reveal', host_id: 1, current_round: 1, players_count: 2 }, + players: [ + { id: 2, nickname: 'Maja', score: 10, is_connected: true }, + { id: 3, nickname: 'Bo', score: 7, is_connected: true } + ], + round_question: { + id: 77, + round_number: 1, + prompt: 'Q?', + shown_at: '2026-03-01T18:00:00Z', + answers: [{ text: 'A' }, { text: 'B' }] + }, + reveal: { + round_question_id: 77, + round_number: 1, + prompt: 'Q?', + correct_answer: 'A', + lies: [], + guesses: [ + { + player_id: 3, + nickname: 'Bo', + selected_text: 'A', + is_correct: true, + fooled_player_nickname: 'Maja', + created_at: '2026-03-01T18:00:15Z' + } + ] + }, + phase_view_model: { + status: 'reveal', + round_number: 1, + players_count: 2, + constraints: { + min_players_to_start: 2, + max_players_mvp: 8, + min_players_reached: true, + max_players_allowed: true + }, + host: { + can_start_round: false, + can_show_question: false, + can_mix_answers: false, + can_calculate_scores: false, + can_reveal_scoreboard: true, + can_start_next_round: false, + can_finish_game: false + }, + player: { + can_join: true, + can_submit_lie: false, + can_submit_guess: false, + can_view_final_result: false + } + } + }) + ).toThrow('fooled_player_nickname to be omitted when fooled_player_id is null'); + }); + + it('rejects canonical reveal payloads that omit fooled_player_nickname when fooled_player_id is set', () => { + expect(() => + mapSessionDetailResponse({ + session: { code: 'ABCD12', status: 'reveal', host_id: 1, current_round: 1, players_count: 2 }, + players: [ + { id: 2, nickname: 'Maja', score: 10, is_connected: true }, + { id: 3, nickname: 'Bo', score: 7, is_connected: true } + ], + round_question: { + id: 77, + round_number: 1, + prompt: 'Q?', + shown_at: '2026-03-01T18:00:00Z', + answers: [{ text: 'A' }, { text: 'B' }] + }, + reveal: { + round_question_id: 77, + round_number: 1, + prompt: 'Q?', + correct_answer: 'A', + lies: [], + guesses: [ + { + player_id: 3, + nickname: 'Bo', + selected_text: 'B', + is_correct: false, + fooled_player_id: 2, + created_at: '2026-03-01T18:00:15Z' + } + ] + }, + phase_view_model: { + status: 'reveal', + round_number: 1, + players_count: 2, + constraints: { + min_players_to_start: 2, + max_players_mvp: 8, + min_players_reached: true, + max_players_allowed: true + }, + host: { + can_start_round: false, + can_show_question: false, + can_mix_answers: false, + can_calculate_scores: false, + can_reveal_scoreboard: true, + can_start_next_round: false, + can_finish_game: false + }, + player: { + can_join: true, + can_submit_lie: false, + can_submit_guess: false, + can_view_final_result: false + } + } + }) + ).toThrow('fooled_player_nickname when fooled_player_id is set'); + }); + it('maps host/player gameplay endpoints through typed response mappers', async () => { const get = vi.fn(async (url: string) => { if (url === '/lobby/sessions/ABCD12/scoreboard') { diff --git a/lobby/tests.py b/lobby/tests.py index 4322a32..c537595 100644 --- a/lobby/tests.py +++ b/lobby/tests.py @@ -1427,6 +1427,7 @@ class SessionDetailRoundQuestionTests(TestCase): self.assertEqual(payload["reveal"]["guesses"][1]["selected_text"], "Edison") self.assertTrue(payload["reveal"]["guesses"][1]["is_correct"]) self.assertIsNone(payload["reveal"]["guesses"][1]["fooled_player_id"]) + self.assertNotIn("fooled_player_nickname", payload["reveal"]["guesses"][1]) def test_session_detail_includes_canonical_reveal_payload_in_scoreboard_phase(self): self.session.status = GameSession.Status.SCOREBOARD