p = 'src/pages/QuizPage.test.jsx'
s = open(p).read()

s = s.replace('''  it('opens one option\\'s reasoning on click, and closes it on the next click', async () => {
    const originalGet = api.get.getMockImplementation()
    api.get.mockImplementation(async (url, ...args) => {
      const res = await originalGet(url, ...args)
      if (url.includes('attempt_id=')) {
        res.data.questions[0] = { ...res.data.questions[0],
          option_explanations: { 'First answer': 'Because it is first.' } }
      }
      return res
    })
    await begin()
    await findStem('Full first clinical question.')
    fireEvent.keyDown(window, { key: '1' })

    const option = await waitFor(() => inCard().getByText('First answer').closest('button'))
    expect(screen.queryByText('Because it is first.')).not.toBeInTheDocument()
    await userEvent.click(option)
    expect(await screen.findByText('Because it is first.')).toBeInTheDocument()
    // Clicking it again puts it away, rather than leaving it open for good.
    await userEvent.click(option)
    await waitFor(() => expect(screen.queryByText('Because it is first.')).not.toBeInTheDocument())
  })''',
'''  it('every option opens its reasoning the moment the answer is shown', async () => {
    // No click per option. Reading why the other three are wrong is the point
    // of the screen, and it used to cost three clicks to get at — the user's
    // words: "I said options show explanations immediately. No second
    // clicking."
    const originalGet = api.get.getMockImplementation()
    api.get.mockImplementation(async (url, ...args) => {
      const res = await originalGet(url, ...args)
      if (url.includes('attempt_id=')) {
        res.data.questions[0] = { ...res.data.questions[0],
          option_explanations: { 'First answer': 'Because it is first.',
                                 'Second answer': 'Because it is not.' } }
      }
      return res
    })
    await begin()
    await findStem('Full first clinical question.')
    expect(screen.queryByText('Because it is first.')).not.toBeInTheDocument()

    fireEvent.keyDown(window, { key: '1' })
    expect(await screen.findByText('Because it is first.')).toBeInTheDocument()
    expect(screen.getByText('Because it is not.')).toBeInTheDocument()

    // And nothing puts them away again.
    await userEvent.click(inCard().getByText('First answer').closest('button'))
    expect(screen.getByText('Because it is first.')).toBeInTheDocument()
  })

  it('every option that is not the answer is marked wrong, not only the one picked', async () => {
    await begin()
    await findStem('Full first clinical question.')
    fireEvent.keyDown(window, { key: '2' })          // the wrong one
    await screen.findByText(/Full explanation, preserved without shortening/)
    expect(inCard().getByText('First answer').closest('.option-row')).toHaveClass('is-correct')
    expect(inCard().getByText('Second answer').closest('.option-row')).toHaveClass('is-wrong')
  })''')

s = s.replace('''  it('shows per-option explanations in study feedback behind a toggle', async () => {
    const originalGet = api.get.getMockImplementation()
    api.get.mockImplementation(async (url, ...args) => {
      const res = await originalGet(url, ...args)
      if (res.data?.questions && url.includes('attempt_id=')) {
        res.data.questions[0] = { ...res.data.questions[0], option_explanations: { 'First answer': 'Preferred because of this', 'Second answer': 'Distractor reason' } }
      }
      return res
    })
    await begin()
    await userEvent.click(screen.getByRole('button', { name: /A\\s*First answer/ }))
    expect(screen.queryByText('Preferred because of this')).not.toBeInTheDocument()
    await userEvent.click(screen.getByRole('button', { name: 'Show all explanations' }))
    expect(await screen.findByText('Preferred because of this')).toBeInTheDocument()
    expect(screen.getByText('Distractor reason')).toBeInTheDocument()
    // The main explanation at the end stays visible either way.
    expect(screen.getByText(/Full explanation, preserved without shortening/)).toBeInTheDocument()
    await userEvent.click(screen.getByRole('button', { name: 'Hide explanations' }))
    expect(screen.queryByText('Preferred because of this')).not.toBeInTheDocument()
  })''',
'''  it('shows every per-option explanation with the answer, and no toggle for them', async () => {
    const originalGet = api.get.getMockImplementation()
    api.get.mockImplementation(async (url, ...args) => {
      const res = await originalGet(url, ...args)
      if (res.data?.questions && url.includes('attempt_id=')) {
        res.data.questions[0] = { ...res.data.questions[0], option_explanations: { 'First answer': 'Preferred because of this', 'Second answer': 'Distractor reason' } }
      }
      return res
    })
    await begin()
    await userEvent.click(screen.getByRole('button', { name: /A\\s*First answer/ }))
    expect(await screen.findByText('Preferred because of this')).toBeInTheDocument()
    expect(screen.getByText('Distractor reason')).toBeInTheDocument()
    // The main explanation at the end is still there under them.
    expect(screen.getByText(/Full explanation, preserved without shortening/)).toBeInTheDocument()
    // Nothing to show or hide: there is nothing closed.
    expect(screen.queryByRole('button', { name: /explanations/ })).toBeNull()
  })''')
open(p, 'w').write(s)
print('written')
