diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml index d407ef5..ae3f59e 100644 --- a/.gitea/workflows/ci.yml +++ b/.gitea/workflows/ci.yml @@ -31,3 +31,14 @@ jobs: - name: Tests run: python manage.py test lobby -v 1 + + - name: Setup Node + uses: actions/setup-node@v4 + with: + node-version: "22" + + - name: Install SPA dependencies + run: npm ci --prefix frontend/angular + + - name: SPA Angular smoke tests + run: npm --prefix frontend/angular test diff --git a/frontend/angular/src/app/api-contract-smoke.spec.ts b/frontend/angular/src/app/api-contract-smoke.spec.ts new file mode 100644 index 0000000..0d1010d --- /dev/null +++ b/frontend/angular/src/app/api-contract-smoke.spec.ts @@ -0,0 +1,110 @@ +import { describe, expect, it, vi } from 'vitest'; + +import { createAngularApiClient, type AngularHttpClientLike } from '../../../src/api/angular-client'; + +describe('SPA Angular API contract smoke (host/player foundation)', () => { + it('smoke-checks session read, join and start-round contracts', async () => { + const get = vi.fn(async (url: string) => { + if (url === '/lobby/sessions/ABCD12') { + return { + session: { code: 'ABCD12', status: 'lobby', host_id: 1, current_round: 1, players_count: 2 }, + players: [ + { id: 2, nickname: 'Maja', score: 0, is_connected: true }, + { id: 3, nickname: 'Bo', score: 0, is_connected: true } + ], + round_question: null, + phase_view_model: { + status: 'lobby', + 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: true, + can_show_question: false, + can_mix_answers: false, + can_calculate_scores: false, + can_reveal_scoreboard: false, + 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/join') { + expect(body).toEqual({ code: 'ABCD12', nickname: 'Maja' }); + return { + player: { id: 9, nickname: 'Maja', session_token: 'session-token-1', score: 0 }, + session: { code: 'ABCD12', status: 'lobby' } + } as T; + } + + if (url === '/lobby/sessions/ABCD12/rounds/start') { + expect(body).toEqual({ category_slug: 'history' }); + return { + session: { code: 'ABCD12', status: 'lie', current_round: 1 }, + round: { number: 1, category: { slug: 'history', name: 'History' } } + } 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.session.code).toBe('ABCD12'); + expect(session.data.session.host_id).toBe(1); + expect(session.data.phase_view_model.host.can_start_round).toBe(true); + expect(session.data.phase_view_model.player.can_join).toBe(true); + expect(session.data.phase_view_model.constraints.min_players_to_start).toBe(2); + } + + const join = await client.joinSession({ code: ' abcd12 ', nickname: ' Maja ' }); + expect(join.ok).toBe(true); + if (join.ok) { + expect(join.data.player.id).toBe(9); + expect(join.data.player.session_token).toBe('session-token-1'); + expect(join.data.session.code).toBe('ABCD12'); + } + + const start = await client.startRound(' abcd12 ', { category_slug: 'history' }); + expect(start.ok).toBe(true); + if (start.ok) { + expect(start.data.session.status).toBe('lie'); + expect(start.data.round.number).toBe(1); + expect(start.data.round.category.slug).toBe('history'); + } + + expect(get).toHaveBeenCalledWith('/lobby/sessions/ABCD12', { withCredentials: true }); + expect(post).toHaveBeenNthCalledWith( + 1, + '/lobby/sessions/join', + { code: 'ABCD12', nickname: 'Maja' }, + { withCredentials: true } + ); + expect(post).toHaveBeenNthCalledWith( + 2, + '/lobby/sessions/ABCD12/rounds/start', + { category_slug: 'history' }, + { withCredentials: true } + ); + }); +});