Spaces:
Running on Zero

readloops / frontend /src /Pilot.test.jsx
ManasT2485's picture
Deploy original ReadLoops React interface with ZeroGPU speech bridge
3e6f84d verified
Raw History Blame Contribute Delete
2.21 kB
// @vitest-environment jsdom
import React from 'react'
import { cleanup, fireEvent, render, screen, waitFor, within } from '@testing-library/react'
import { afterEach, expect, it, vi } from 'vitest'
import { App } from './App.jsx'
import { api, postJson } from './api.js'
vi.mock('./api.js', () => ({ api: vi.fn(), postJson: vi.fn() }))
vi.mock('./Session.jsx', () => ({ Session: ({ pilot }) => <p>{pilot ? 'Pilot reading opened' : 'Personal reading opened'}</p> }))
afterEach(() => { cleanup(); vi.resetAllMocks() })
it('locks the assigned language and uses the server-assigned session ID and policy', async () => {
const next = { session_id: 'assigned-id', passage_id: 'el-eval-library', stage: 'before' }
const pilot = { code: 'participant-code', language: 'el', arm: 'adaptive', completed: 0, total: 8, next, delete_by: '2027-01-01', contact: 'Organiser', notice: 'Voluntary participation.' }
api.mockImplementation(path => {
if (path === '/health') return Promise.resolve({ speech: { ready: true }, pilot })
if (path === '/benchmarks/latest') return Promise.resolve({ languages: [] })
if (path.startsWith('/policy-report')) return Promise.resolve({ arms: [] })
if (path.startsWith('/sessions?')) return Promise.resolve([])
if (path === '/sessions/assigned-id') return Promise.resolve({ id: 'assigned-id', passage: { id: next.passage_id } })
return Promise.resolve(path.includes('language=el') && path.includes('evaluation') ? [{ id: next.passage_id, title: 'Greek assessment', word_count: 50 }] : [])
})
postJson.mockResolvedValue({ id: 'assigned-id' })
render(<App />)
await screen.findByText('Greek assessment')
expect(screen.getByRole('button', { name: /English/ }).disabled).toBe(true)
expect(screen.getByRole('button', { name: 'Practice' }).disabled).toBe(true)
expect(screen.queryByLabelText('Exercise selection')).toBeNull()
fireEvent.click(within(screen.getByRole('region', { name: 'Choose a passage' })).getByRole('button', { name: 'Read', exact: true }))
await waitFor(() => expect(postJson).toHaveBeenCalledWith('/sessions', { id: 'assigned-id', passage_id: next.passage_id, mode: 'adaptive' }))
await screen.findByText('Pilot reading opened')
})