Skip to content

armgit / ODS

Public Imported from GitHub
0

ODS V3 Pre-Release: Public testing and refinement ahead of the official V3 launch. Turn your PC, Mac, or Linux box into a private AI server.

36.7 KB
Raw Blame
import { fireEvent, screen, waitFor } from '@testing-library/react'
import { render } from '../test/test-utils'
import ODSTalk from './ODSTalk' // eslint-disable-line no-unused-vars

const response = (body, status = 200) => ({
  ok: status >= 200 && status < 300,
  status,
  json: async () => body,
})

// Build a fake fetch Response with a streaming body. ``frames`` is an array
// of JS objects; each one is encoded as a single SSE frame (data: <json>\n\n).
// If ``chunks`` is provided it's an array of frame-index arrays — each chunk
// emits those frames in one reader.read() pass, simulating partial transport.
const sseResponse = (frames, { status = 200, chunks, holdOpen = false } = {}) => {
  const encoder = new TextEncoder()
  const frameBytes = frames.map(f => encoder.encode(`data: ${JSON.stringify(f)}\n\n`))
  const concatFrames = (group) => {
    const totalLen = group.reduce((acc, i) => acc + frameBytes[i].byteLength, 0)
    const out = new Uint8Array(totalLen)
    let offset = 0
    for (const i of group) {
      out.set(frameBytes[i], offset)
      offset += frameBytes[i].byteLength
    }
    return out
  }
  const chunkGroups = chunks
    ? chunks.map(group => concatFrames(group))
    : frameBytes
  let idx = 0
  const reader = {
    read: async () => {
      if (idx >= chunkGroups.length) {
        // ``holdOpen`` keeps the reader awaiting indefinitely after the final
        // chunk — useful when a test wants to assert intermediate spinner /
        // status UI without the SPA's "stream closed → finalize bubble" path
        // wiping the state before the assertion fires.
        if (holdOpen) return new Promise(() => {})
        return { done: true, value: undefined }
      }
      const value = chunkGroups[idx++]
      return { done: false, value }
    },
  }
  return {
    ok: status >= 200 && status < 300,
    status,
    body: { getReader: () => reader },
    json: async () => ({}),
  }
}

const TALK_NOT_SUPPORTED_COPY = "This model isn't supported in ODS Talk yet. Switch to a recommended model to use ODS Talk."
const FLEET_NOTE = 'Fleet model-UI run 2026-07-16T18-10Z on windows-laptop loaded this model successfully and the runtime reported granite3.3-2b-instruct-q4, but ODS Talk returned a Hermes websocket closed error and then fetch failed during the streamed verification prompt; keep it out of agent-required release coverage until revalidated.'

describe('ODSTalk', () => {
  beforeEach(() => {
    Object.defineProperty(window, 'isSecureContext', { configurable: true, value: false })
  })

  afterEach(() => {
    vi.restoreAllMocks()
  })

  test('renders a mobile text portal and sends a message', async () => {
    const fetchMock = vi.fn(async (url, options = {}) => {
      if (url === '/api/talk/status') {
        return response({
          capabilities: {
            text_chat: true,
            tts: false,
            audio_message: false,
            live_mic_requires_secure_context: true,
          },
        })
      }
      if (url === '/api/talk/message/stream' && options.method === 'POST') {
        expect(JSON.parse(options.body)).toEqual({ text: 'What can you do?' })
        return sseResponse([
          { type: 'session', session_id: 'sid' },
          { type: 'delta', text: 'I can help' },
          { type: 'delta', text: ' from this ODS.' },
          { type: 'complete', session_id: 'sid', text: 'I can help from this ODS.', status: 'ok' },
          { type: 'done' },
        ])
      }
      throw new Error(`unexpected request: ${url}`)
    })
    vi.stubGlobal('fetch', fetchMock)

    render(<ODSTalk />)

    expect(await screen.findByText('Ready')).toBeInTheDocument()
    fireEvent.change(screen.getByPlaceholderText('Message ODS'), {
      target: { value: 'What can you do?' },
    })
    fireEvent.click(screen.getByRole('button', { name: 'Send message' }))

    expect(await screen.findByText('What can you do?')).toBeInTheDocument()
    expect(await screen.findByText('I can help from this ODS.')).toBeInTheDocument()
  })

  test('shows a neutral model notice instead of the internal fleet note and disables send', async () => {
    const fetchMock = vi.fn(async (url) => {
      if (url === '/api/talk/status') {
        return response({
          reason: TALK_NOT_SUPPORTED_COPY,
          reasonCode: 'model_not_supported',
          modelCompatibility: {
            agentViability: { status: 'not_agent_viable', reason: FLEET_NOTE, userMessage: 'Not verified for agent tasks.' },
            hermesTalk: { status: 'unsupported_until_revalidated', reason: FLEET_NOTE, userMessage: TALK_NOT_SUPPORTED_COPY },
            recommendedModel: { id: 'qwen3.5-9b-q4', name: 'Qwen 3.5 9B' },
          },
          capabilities: {
            text_chat: false,
            tts: false,
            audio_message: false,
          },
        })
      }
      throw new Error(`unexpected request: ${url}`)
    })
    vi.stubGlobal('fetch', fetchMock)

    render(<ODSTalk />)

    const notice = await screen.findByTestId('talk-model-notice')
    expect(notice).toHaveTextContent(TALK_NOT_SUPPORTED_COPY)
    expect(notice).toHaveTextContent('Recommended: Qwen 3.5 9B')
    expect(notice.className).not.toMatch(/red/)
    expect(screen.getByRole('link', { name: 'Choose a model' })).toHaveAttribute('href', '/models')
    expect(screen.getByText('Model not supported in Talk')).toBeInTheDocument()
    expect(screen.queryByText(/Fleet model-UI run/)).not.toBeInTheDocument()
    expect(document.body.textContent).not.toMatch(/revalidated|release coverage|windows-laptop/)

    fireEvent.change(screen.getByPlaceholderText('Message ODS'), {
      target: { value: 'hello' },
    })
    expect(screen.getByRole('button', { name: 'Send message' })).toBeDisabled()
    // A known compatibility state is not an outage: no status polling.
    await new Promise(resolve => setTimeout(resolve, 1200))
    expect(fetchMock).toHaveBeenCalledTimes(1)
  })

  test('never renders an older API\'s internal compatibility reason', async () => {
    const fetchMock = vi.fn(async (url) => {
      if (url === '/api/talk/status') {
        return response({
          reason: FLEET_NOTE,
          modelCompatibility: {
            hermesTalk: { status: 'unsupported_until_revalidated', reason: FLEET_NOTE },
          },
          capabilities: { text_chat: false, tts: false, audio_message: false },
        })
      }
      throw new Error(`unexpected request: ${url}`)
    })
    vi.stubGlobal('fetch', fetchMock)

    render(<ODSTalk />)

    expect(await screen.findByTestId('talk-model-notice')).toHaveTextContent(TALK_NOT_SUPPORTED_COPY)
    expect(screen.queryByText(/Recommended:/)).not.toBeInTheDocument()
    expect(screen.queryByText(/Fleet model-UI run/)).not.toBeInTheDocument()
    expect(screen.getByRole('button', { name: 'Send message' })).toBeDisabled()
  })

  const TALK_TOOLS_ADVISORY_COPY = 'This model failed the tool-call check. Talk can still chat, but actions that need tools may fail.'

  test('advises, never blocks, when the model failed the tool check', async () => {
    const fetchMock = vi.fn(async (url, options = {}) => {
      if (url === '/api/talk/status') {
        return response({
          modelCompatibility: { hermesTalkAdvisory: { code: 'tools-unavailable' } },
          capabilities: { text_chat: true, tts: false, audio_message: false },
        })
      }
      if (url === '/api/talk/message/stream' && options.method === 'POST') {
        return sseResponse([
          { type: 'delta', text: 'Still chatting.' },
          { type: 'complete', text: 'Still chatting.', status: 'ok' },
          { type: 'done' },
        ])
      }
      throw new Error(`unexpected request: ${url}`)
    })
    vi.stubGlobal('fetch', fetchMock)

    render(<ODSTalk />)

    expect(await screen.findByText('Ready')).toBeInTheDocument()
    const notice = screen.getByTestId('talk-tools-notice')
    expect(notice).toHaveTextContent(TALK_TOOLS_ADVISORY_COPY)
    expect(notice.className).not.toMatch(/red/)
    expect(screen.getByRole('link', { name: 'Choose a model' })).toHaveAttribute('href', '/models')
    expect(screen.getByRole('link', { name: 'Get help on Discord' })).toHaveAttribute('href', 'https://discord.gg/4ntNp9MAwC')
    expect(screen.queryByTestId('talk-model-notice')).not.toBeInTheDocument()
    expect(screen.queryByText('Model not supported in Talk')).not.toBeInTheDocument()

    fireEvent.change(screen.getByPlaceholderText('Message ODS'), { target: { value: 'hello' } })
    expect(screen.getByRole('button', { name: 'Send message' })).toBeEnabled()
    fireEvent.click(screen.getByRole('button', { name: 'Send message' }))
    expect(await screen.findByText('Still chatting.')).toBeInTheDocument()
    expect(fetchMock).toHaveBeenCalledWith('/api/talk/message/stream', expect.objectContaining({ method: 'POST' }))
    expect(screen.getByText('Ready')).toBeInTheDocument()
  })

  test.each([
    { code: 'something-new' },
    { code: null },
    {},
    'tools-unavailable',
    null,
  ])('ignores an unknown tools advisory: %j', async hermesTalkAdvisory => {
    vi.stubGlobal('fetch', vi.fn(async (url) => {
      if (url === '/api/talk/status') {
        return response({
          modelCompatibility: { hermesTalkAdvisory },
          capabilities: { text_chat: true, tts: false, audio_message: false },
        })
      }
      throw new Error(`unexpected request: ${url}`)
    }))

    render(<ODSTalk />)

    expect(await screen.findByText('Ready')).toBeInTheDocument()
    expect(screen.queryByTestId('talk-tools-notice')).not.toBeInTheDocument()
    fireEvent.change(screen.getByPlaceholderText('Message ODS'), { target: { value: 'hello' } })
    expect(screen.getByRole('button', { name: 'Send message' })).toBeEnabled()
  })

  test('keeps the blocking model notice unchanged when a block and the advisory arrive together', async () => {
    vi.stubGlobal('fetch', vi.fn(async (url) => {
      if (url === '/api/talk/status') {
        return response({
          reason: TALK_NOT_SUPPORTED_COPY,
          reasonCode: 'model_not_supported',
          modelCompatibility: {
            hermesTalk: { status: 'unsupported', reason: FLEET_NOTE },
            hermesTalkAdvisory: { code: 'tools-unavailable' },
          },
          capabilities: { text_chat: false, tts: false, audio_message: false },
        })
      }
      throw new Error(`unexpected request: ${url}`)
    }))

    render(<ODSTalk />)

    expect(await screen.findByTestId('talk-model-notice')).toHaveTextContent(TALK_NOT_SUPPORTED_COPY)
    expect(screen.getByText('Model not supported in Talk')).toBeInTheDocument()
    expect(screen.queryByTestId('talk-tools-notice')).not.toBeInTheDocument()
    expect(screen.queryByRole('link', { name: 'Get help on Discord' })).not.toBeInTheDocument()
    fireEvent.change(screen.getByPlaceholderText('Message ODS'), { target: { value: 'hello' } })
    expect(screen.getByRole('button', { name: 'Send message' })).toBeDisabled()
  })

  test('drops the tools advisory once a refreshed status no longer carries it', async () => {
    let advisory = true
    const fetchMock = vi.fn(async (url) => {
      if (url === '/api/talk/status') {
        return response({
          modelCompatibility: advisory ? { hermesTalkAdvisory: { code: 'tools-unavailable' } } : {},
          capabilities: { text_chat: true, tts: false, audio_message: false },
        })
      }
      throw new Error(`unexpected request: ${url}`)
    })
    vi.stubGlobal('fetch', fetchMock)

    render(<ODSTalk />)

    expect(await screen.findByTestId('talk-tools-notice')).toHaveTextContent(TALK_TOOLS_ADVISORY_COPY)
    fireEvent.change(screen.getByPlaceholderText('Message ODS'), { target: { value: 'hello' } })
    const send = screen.getByRole('button', { name: 'Send message' })
    expect(send).toBeEnabled()
    advisory = false
    fireEvent.click(screen.getByRole('button', { name: 'Refresh ODS Talk status' }))
    expect(send).toBeDisabled()
    await waitFor(() => expect(send).toBeEnabled())
    expect(fetchMock).toHaveBeenCalledTimes(2)
    expect(screen.queryByTestId('talk-tools-notice')).not.toBeInTheDocument()
  })

  test('switches to the model notice when a send is rejected for model compatibility', async () => {
    let blocked = false
    const fetchMock = vi.fn(async (url, options = {}) => {
      if (url === '/api/talk/status') {
        return response(blocked
          ? {
              reason: TALK_NOT_SUPPORTED_COPY,
              reasonCode: 'model_not_supported',
              modelCompatibility: { hermesTalk: { status: 'unsupported_until_revalidated', reason: FLEET_NOTE } },
              capabilities: { text_chat: false, tts: false, audio_message: false },
            }
          : { capabilities: { text_chat: true, tts: false, audio_message: false } })
      }
      if (url === '/api/talk/message/stream' && options.method === 'POST') {
        blocked = true
        return response({ detail: TALK_NOT_SUPPORTED_COPY }, 409)
      }
      throw new Error(`unexpected request: ${url}`)
    })
    vi.stubGlobal('fetch', fetchMock)

    render(<ODSTalk />)

    expect(await screen.findByText('Ready')).toBeInTheDocument()
    fireEvent.change(screen.getByPlaceholderText('Message ODS'), {
      target: { value: 'hello' },
    })
    fireEvent.click(screen.getByRole('button', { name: 'Send message' }))

    expect(await screen.findByTestId('talk-model-notice')).toHaveTextContent(TALK_NOT_SUPPORTED_COPY)
    expect(screen.getByText('Model not supported in Talk')).toBeInTheDocument()
    expect(screen.queryByText(/Fleet model-UI run/)).not.toBeInTheDocument()
    expect(screen.getByRole('button', { name: 'Send message' })).toBeDisabled()
  })

  test('refreshes transient offline status until text chat becomes ready', async () => {
    const fetchMock = vi.fn(async (url) => {
      if (url === '/api/talk/status') {
        const ready = fetchMock.mock.calls.length > 1
        return response({
          capabilities: {
            text_chat: ready,
            tts: false,
            audio_message: false,
          },
        })
      }
      throw new Error(`unexpected request: ${url}`)
    })
    vi.stubGlobal('fetch', fetchMock)

    render(<ODSTalk />)

    expect((await screen.findAllByText('ODS Talk text chat is not available.')).length).toBeGreaterThan(0)
    expect(await screen.findByText('Ready', {}, { timeout: 2500 })).toBeInTheDocument()
    expect(fetchMock).toHaveBeenCalledTimes(2)
  })

  test('keeps retrying when readiness takes more than one attempt', async () => {
    // The retry is a one-shot setTimeout re-armed by an effect dependency. A
    // failed attempt used to set status/retry flags to the values they already
    // held, so React bailed out of the re-render, no dependency changed, and
    // the effect never ran a second time — the page sat on 'offline' until a
    // manual reload. A backend container taking longer than one interval to
    // come up is the normal case, not an edge case.
    const fetchMock = vi.fn(async (url) => {
      if (url === '/api/talk/status') {
        const ready = fetchMock.mock.calls.length > 3
        return response({
          capabilities: { text_chat: ready, tts: false, audio_message: false },
        })
      }
      throw new Error(`unexpected request: ${url}`)
    })
    vi.stubGlobal('fetch', fetchMock)

    render(<ODSTalk />)

    expect((await screen.findAllByText('ODS Talk text chat is not available.')).length).toBeGreaterThan(0)
    expect(await screen.findByText('Ready', {}, { timeout: 8000 })).toBeInTheDocument()
    expect(fetchMock.mock.calls.length).toBeGreaterThan(3)
  })

  test('stops polling once a permanent incompatibility is reported', async () => {
    // A compatibility reason is a settled answer, not a transient outage:
    // retrying must stay off so the page does not poll forever.
    const fetchMock = vi.fn(async (url) => {
      if (url === '/api/talk/status') {
        return response({
          capabilities: { text_chat: false, tts: false, audio_message: false },
          reason: 'Model does not support tool calling.',
          reasonCode: 'model_not_supported',
        })
      }
      throw new Error(`unexpected request: ${url}`)
    })
    vi.stubGlobal('fetch', fetchMock)

    render(<ODSTalk />)

    expect((await screen.findAllByText('Model does not support tool calling.')).length).toBeGreaterThan(0)
    const seen = fetchMock.mock.calls.length
    await new Promise(resolve => setTimeout(resolve, 2500))
    expect(fetchMock.mock.calls.length).toBe(seen)
  })

  test('accumulates SSE deltas across split chunks', async () => {
    // Transport may split a single SSE frame across chunk boundaries. The
    // reader has to buffer the partial frame across reads, not drop bytes.
    const fetchMock = vi.fn(async (url, options = {}) => {
      if (url === '/api/talk/status') {
        return response({ capabilities: { text_chat: true } })
      }
      if (url === '/api/talk/message/stream' && options.method === 'POST') {
        // Five frames in three transport chunks: [session][delta delta][complete done]
        return sseResponse(
          [
            { type: 'session', session_id: 'sid' },
            { type: 'delta', text: 'Hello' },
            { type: 'delta', text: ' world' },
            { type: 'complete', session_id: 'sid', text: 'Hello world', status: 'ok' },
            { type: 'done' },
          ],
          { chunks: [[0], [1, 2], [3, 4]] },
        )
      }
      throw new Error(`unexpected request: ${url}`)
    })
    vi.stubGlobal('fetch', fetchMock)

    render(<ODSTalk />)
    expect(await screen.findByText('Ready')).toBeInTheDocument()
    fireEvent.change(screen.getByPlaceholderText('Message ODS'), {
      target: { value: 'hello' },
    })
    fireEvent.click(screen.getByRole('button', { name: 'Send message' }))

    expect(await screen.findByText('Hello world')).toBeInTheDocument()
  })

  test('renders markdown in assistant bubbles (bold/lists), not raw asterisks', async () => {
    // Hermes formats replies with markdown by default. The bubble should
    // render that as HTML so a list looks like a list, not "- one\n- two".
    const fetchMock = vi.fn(async (url, options = {}) => {
      if (url === '/api/talk/status') return response({ capabilities: { text_chat: true } })
      if (url === '/api/talk/message/stream' && options.method === 'POST') {
        return sseResponse([
          { type: 'session', session_id: 'sid' },
          { type: 'complete', session_id: 'sid', text: 'Pick **one**:\n\n- alpha\n- beta', status: 'ok' },
          { type: 'done' },
        ])
      }
      throw new Error(`unexpected request: ${url}`)
    })
    vi.stubGlobal('fetch', fetchMock)

    const { container } = render(<ODSTalk />)
    expect(await screen.findByText('Ready')).toBeInTheDocument()
    fireEvent.change(screen.getByPlaceholderText('Message ODS'), {
      target: { value: 'choose' },
    })
    fireEvent.click(screen.getByRole('button', { name: 'Send message' }))

    // Bold renders as <strong>, not as ** in the text.
    expect(await screen.findByText('one')).toBeInTheDocument()
    expect(screen.getByText('one').tagName).toBe('STRONG')
    // List items render as <li> under a <ul>.
    expect(container.querySelector('ul li')).toBeTruthy()
    expect(screen.getByText('alpha').closest('li')).toBeTruthy()
    // And the raw `**` is gone from the DOM text.
    expect(container.textContent).not.toContain('**')
  })

  test.each([
    ['what is 12 squared?', '144.', '144', ['']],
    ['what is left?', 'Steps 1 and 2 are done.\n\n3. Run the tests\n4. Open the pull request', '3', ['Run the tests', 'Open the pull request']],
  ])('keeps the ordered-list start number in the reply to %j', async (question, text, start, items) => {
    // A reply of exactly "144." is an ordered list that starts at 144 with an
    // empty item. Without its start number the bubble showed a bare "1.".
    const fetchMock = vi.fn(async (url, options = {}) => {
      if (url === '/api/talk/status') return response({ capabilities: { text_chat: true } })
      if (url === '/api/talk/message/stream' && options.method === 'POST') {
        return sseResponse([
          { type: 'session', session_id: 'sid' },
          { type: 'complete', session_id: 'sid', text, status: 'ok' },
          { type: 'done' },
        ])
      }
      throw new Error(`unexpected request: ${url}`)
    })
    vi.stubGlobal('fetch', fetchMock)

    const { container } = render(<ODSTalk />)
    expect(await screen.findByText('Ready')).toBeInTheDocument()
    fireEvent.change(screen.getByPlaceholderText('Message ODS'), {
      target: { value: question },
    })
    fireEvent.click(screen.getByRole('button', { name: 'Send message' }))

    await waitFor(() => expect(container.querySelector('ol')).toHaveAttribute('start', start))
    expect([...container.querySelectorAll('ol li')].map(item => item.textContent)).toEqual(items)
  })

  test('renders dynamic SSE status caption + detail while a tool is in flight', async () => {
    // Bridge emits `status` frames between session and the first delta —
    // the SPA should swap the default "Thinking…" caption for the friendly
    // label and show the detail string (e.g. the search query) underneath.
    // Once message.delta frames start arriving, the caption is dropped and
    // the bubble shows live text. Then on `complete`, the bubble settles.
    const fetchMock = vi.fn(async (url, options = {}) => {
      if (url === '/api/talk/status') return response({ capabilities: { text_chat: true } })
      if (url === '/api/talk/message/stream' && options.method === 'POST') {
        return sseResponse([
          { type: 'session', session_id: 'sid' },
          { type: 'status', label: 'Searching the web…', tool: 'web_search', detail: 'weather forecast Philadelphia' },
        ], { chunks: [[0, 1]], holdOpen: true })
      }
      throw new Error(`unexpected request: ${url}`)
    })
    vi.stubGlobal('fetch', fetchMock)

    render(<ODSTalk />)
    expect(await screen.findByText('Ready')).toBeInTheDocument()
    fireEvent.change(screen.getByPlaceholderText('Message ODS'), {
      target: { value: 'weather?' },
    })
    fireEvent.click(screen.getByRole('button', { name: 'Send message' }))

    // The friendly caption replaces the default "Thinking…" wholesale.
    expect(await screen.findByText('Searching the web…')).toBeInTheDocument()
    expect(screen.queryByText('Thinking…')).not.toBeInTheDocument()
    // The detail string (the query Hermes is running) renders below the caption.
    expect(screen.getByText('weather forecast Philadelphia')).toBeInTheDocument()
  })

  test('drops the status caption when message.delta frames start arriving', async () => {
    // Hermes flow on a real tool-using turn: status -> deltas -> complete.
    // Once the first delta lands the bubble must switch from "Searching…"
    // to live token rendering — otherwise the user keeps seeing the stale
    // status while the model is actually writing.
    const fetchMock = vi.fn(async (url, options = {}) => {
      if (url === '/api/talk/status') return response({ capabilities: { text_chat: true } })
      if (url === '/api/talk/message/stream' && options.method === 'POST') {
        return sseResponse([
          { type: 'session', session_id: 'sid' },
          { type: 'status', label: 'Searching the web…', tool: 'web_search', detail: 'weather' },
          { type: 'status', label: null, tool: null, detail: null },
          { type: 'delta', text: 'It is ' },
          { type: 'delta', text: '64°F.' },
          { type: 'complete', session_id: 'sid', text: 'It is 64°F.', status: 'ok' },
          { type: 'done' },
        ])
      }
      throw new Error(`unexpected request: ${url}`)
    })
    vi.stubGlobal('fetch', fetchMock)

    render(<ODSTalk />)
    expect(await screen.findByText('Ready')).toBeInTheDocument()
    fireEvent.change(screen.getByPlaceholderText('Message ODS'), {
      target: { value: 'weather?' },
    })
    fireEvent.click(screen.getByRole('button', { name: 'Send message' }))

    expect(await screen.findByText('It is 64°F.')).toBeInTheDocument()
    // The status caption is gone once content has rendered.
    expect(screen.queryByText('Searching the web…')).not.toBeInTheDocument()
    expect(screen.queryByText('Thinking…')).not.toBeInTheDocument()
  })

  test('attaching an image POSTs to /api/talk/attachment + shows preview + renders user image', async () => {
    // Stub URL.createObjectURL so the blob: URL the SPA generates for the
    // preview doesn't break jsdom (which has no blob support by default).
    const createdUrls = []
    vi.stubGlobal('URL', {
      ...globalThis.URL,
      createObjectURL: (blob) => {
        const url = `blob:test-${createdUrls.length}`
        createdUrls.push({ url, blob })
        return url
      },
      revokeObjectURL: () => {},
    })

    let attachmentRequest = null
    const fetchMock = vi.fn(async (url, options = {}) => {
      if (url === '/api/talk/status') return response({ capabilities: { text_chat: true } })
      if (url === '/api/talk/attachment' && options.method === 'POST') {
        attachmentRequest = options
        return sseResponse([
          { type: 'session', session_id: 'vision-oneshot' },
          { type: 'delta', text: 'A red square.' },
          { type: 'complete', session_id: 'vision-oneshot', text: 'A red square.', status: 'ok' },
          { type: 'done' },
        ])
      }
      throw new Error(`unexpected request: ${url}`)
    })
    vi.stubGlobal('fetch', fetchMock)

    const { container } = render(<ODSTalk />)
    expect(await screen.findByText('Ready')).toBeInTheDocument()

    // Pick a fake image file via the hidden file input.
    const fileInput = container.querySelector('input[type="file"]')
    const blob = new globalThis.Blob([new Uint8Array([137, 80, 78, 71])], { type: 'image/png' })
    const file = new globalThis.File([blob], 'photo.png', { type: 'image/png' })
    fireEvent.change(fileInput, { target: { files: [file] } })

    // Preview strip shows the filename + an X button to discard.
    expect(await screen.findByText('photo.png')).toBeInTheDocument()
    expect(screen.getByRole('button', { name: 'Remove attachment' })).toBeInTheDocument()

    // Add a caption and send.
    fireEvent.change(screen.getByPlaceholderText('Message ODS'), {
      target: { value: 'What color is this?' },
    })
    fireEvent.click(screen.getByRole('button', { name: 'Send message' }))

    // Assistant reply renders.
    expect(await screen.findByText('A red square.')).toBeInTheDocument()
    // Backend was hit on the attachment endpoint with FormData.
    expect(attachmentRequest).not.toBeNull()
    expect(attachmentRequest.body).toBeInstanceOf(globalThis.FormData)
    expect(attachmentRequest.body.get('file')).toBeTruthy()
    expect(attachmentRequest.body.get('text')).toBe('What color is this?')
    // The user's bubble shows the image inline.
    const userImg = container.querySelector('img[alt="Attached"]')
    expect(userImg).toBeTruthy()
    expect(userImg.getAttribute('src')).toMatch(/^blob:/)
  })

  test('retry keeps image attachment context after an attachment stream error', async () => {
    vi.stubGlobal('URL', {
      ...globalThis.URL,
      createObjectURL: () => 'blob:retry-image',
      revokeObjectURL: () => {},
    })

    let attachmentRequests = 0
    const fetchMock = vi.fn(async (url, options = {}) => {
      if (url === '/api/talk/status') return response({ capabilities: { text_chat: true } })
      if (url === '/api/talk/attachment' && options.method === 'POST') {
        attachmentRequests += 1
        expect(options.body).toBeInstanceOf(globalThis.FormData)
        expect(options.body.get('file')).toBeTruthy()
        expect(options.body.get('text')).toBe('What color is this?')
        if (attachmentRequests === 1) {
          return sseResponse([
            { type: 'session', session_id: 'vision-oneshot' },
            { type: 'error', status_code: 502, detail: 'Vision timed out.' },
            { type: 'done' },
          ])
        }
        return sseResponse([
          { type: 'session', session_id: 'vision-oneshot' },
          { type: 'delta', text: 'A red square.' },
          { type: 'complete', session_id: 'vision-oneshot', text: 'A red square.', status: 'ok' },
          { type: 'done' },
        ])
      }
      throw new Error(`unexpected request: ${url}`)
    })
    vi.stubGlobal('fetch', fetchMock)

    const { container } = render(<ODSTalk />)
    expect(await screen.findByText('Ready')).toBeInTheDocument()

    const fileInput = container.querySelector('input[type="file"]')
    const blob = new globalThis.Blob([new Uint8Array([137, 80, 78, 71])], { type: 'image/png' })
    const file = new globalThis.File([blob], 'photo.png', { type: 'image/png' })
    fireEvent.change(fileInput, { target: { files: [file] } })
    fireEvent.change(screen.getByPlaceholderText('Message ODS'), {
      target: { value: 'What color is this?' },
    })
    fireEvent.click(screen.getByRole('button', { name: 'Send message' }))

    expect(await screen.findByText('Vision timed out.')).toBeInTheDocument()
    fireEvent.click(screen.getByRole('button', { name: 'Retry last message' }))

    expect(await screen.findByText('A red square.')).toBeInTheDocument()
    expect(attachmentRequests).toBe(2)
  })

  test('surfaces SSE error frame as an assistant error', async () => {
    const fetchMock = vi.fn(async (url, options = {}) => {
      if (url === '/api/talk/status') return response({ capabilities: { text_chat: true } })
      if (url === '/api/talk/message/stream' && options.method === 'POST') {
        return sseResponse([
          { type: 'session', session_id: 'sid' },
          { type: 'error', status_code: 502, detail: 'Hermes did not finish the response.' },
          { type: 'done' },
        ])
      }
      throw new Error(`unexpected request: ${url}`)
    })
    vi.stubGlobal('fetch', fetchMock)

    render(<ODSTalk />)
    expect(await screen.findByText('Ready')).toBeInTheDocument()
    fireEvent.change(screen.getByPlaceholderText('Message ODS'), {
      target: { value: 'hi' },
    })
    fireEvent.click(screen.getByRole('button', { name: 'Send message' }))

    expect(await screen.findByText('Hermes did not finish the response.')).toBeInTheDocument()
  })

  test('shows an expired owner-card session state', async () => {
    const fetchMock = vi.fn(async (url) => {
      if (url === '/api/talk/status') return response({ detail: 'expired' }, 401)
      throw new Error(`unexpected request: ${url}`)
    })
    vi.stubGlobal('fetch', fetchMock)

    render(<ODSTalk />)

    expect(await screen.findByText('Session expired. Scan the owner card again.')).toBeInTheDocument()
    expect(screen.getByText(/This owner session ended/i)).toBeInTheDocument()
    expect(screen.getByPlaceholderText('Message ODS')).toBeDisabled()
  })

  test('keeps text usable and shows a clear live-mic fallback on local HTTP', async () => {
    const fetchMock = vi.fn(async (url) => {
      if (url === '/api/talk/status') {
        return response({
          capabilities: {
            text_chat: true,
            tts: true,
            audio_message: true,
            live_mic_requires_secure_context: true,
          },
        })
      }
      throw new Error(`unexpected request: ${url}`)
    })
    vi.stubGlobal('fetch', fetchMock)

    render(<ODSTalk />)

    expect(await screen.findByText('Ready')).toBeInTheDocument()
    expect(screen.getByText(/Live mic needs HTTPS/i)).toBeInTheDocument()
    // Paperclip is now the general image/file attach button (it used to be
    // mis-wired to "Attach voice message" + capture, which made iOS open
    // the video recorder).
    expect(screen.getByRole('button', { name: 'Attach image or file' })).toBeEnabled()
    expect(screen.queryByRole('button', { name: 'Record voice' })).not.toBeInTheDocument()
  })

  test('can request spoken replies without blocking text chat', async () => {
    vi.stubGlobal('Audio', class {
      addEventListener() {}
      play() { return Promise.resolve() }
    })
    vi.stubGlobal('URL', {
      createObjectURL: () => 'blob:audio',
      revokeObjectURL: vi.fn(),
    })
    const fetchMock = vi.fn(async (url, options = {}) => {
      if (url === '/api/talk/status') {
        return response({
          capabilities: { text_chat: true, tts: true, audio_message: false },
        })
      }
      if (url === '/api/talk/message/stream') {
        return sseResponse([
          { type: 'session', session_id: 'sid' },
          { type: 'delta', text: 'Spoken answer.' },
          { type: 'complete', session_id: 'sid', text: 'Spoken answer.', status: 'ok' },
          { type: 'done' },
        ])
      }
      if (url === '/api/talk/speak' && options.method === 'POST') {
        return {
          ok: true,
          status: 200,
          blob: async () => new globalThis.Blob(['audio'], { type: 'audio/mpeg' }),
        }
      }
      throw new Error(`unexpected request: ${url}`)
    })
    vi.stubGlobal('fetch', fetchMock)

    render(<ODSTalk />)
    expect(await screen.findByText('Ready')).toBeInTheDocument()
    fireEvent.click(screen.getByRole('button', { name: 'Turn spoken replies on' }))
    fireEvent.change(screen.getByPlaceholderText('Message ODS'), {
      target: { value: 'read it' },
    })
    fireEvent.click(screen.getByRole('button', { name: 'Send message' }))

    expect(await screen.findByText('Spoken answer.')).toBeInTheDocument()
    await waitFor(() => expect(fetchMock).toHaveBeenCalledWith(
      '/api/talk/speak',
      expect.objectContaining({ method: 'POST' }),
    ))
  })

  test('renders a tool approval and submits one choice-only response', async () => {
    let resolveApproval
    const approvalResponse = new Promise(resolve => {
      resolveApproval = () => resolve(response({ accepted: true, choice: 'once' }))
    })
    const fetchMock = vi.fn(async (url, options = {}) => {
      if (url === '/api/talk/status') return response({ capabilities: { text_chat: true } })
      if (url === '/api/talk/message/stream' && options.method === 'POST') {
        return sseResponse([
          { type: 'session', session_id: 'sid' },
          { type: 'delta', text: 'Let me check.' },
          {
            type: 'approval',
            command: 'python -c "print(4)"',
            description: 'Run a short calculation',
            choices: ['once', 'deny'],
          },
        ], { holdOpen: true })
      }
      if (url === '/api/talk/approval' && options.method === 'POST') return approvalResponse
      throw new Error(`unexpected request: ${url}`)
    })
    vi.stubGlobal('fetch', fetchMock)

    render(<ODSTalk />)
    expect(await screen.findByText('Ready')).toBeInTheDocument()
    fireEvent.change(screen.getByPlaceholderText('Message ODS'), { target: { value: 'calculate' } })
    fireEvent.click(screen.getByRole('button', { name: 'Send message' }))

    expect(await screen.findByText('Permission required')).toBeInTheDocument()
    expect(screen.getByText('Let me check.')).toBeInTheDocument()
    expect(screen.getByText('Run a short calculation')).toBeInTheDocument()
    expect(screen.getByText('python -c "print(4)"')).toBeInTheDocument()
    const allow = screen.getByRole('button', { name: 'Allow once' })
    const deny = screen.getByRole('button', { name: 'Deny' })

    fireEvent.click(allow)
    fireEvent.click(allow)
    await waitFor(() => {
      const calls = fetchMock.mock.calls.filter(([url]) => url === '/api/talk/approval')
      expect(calls).toHaveLength(1)
      expect(JSON.parse(calls[0][1].body)).toEqual({ choice: 'once' })
      expect(Object.keys(JSON.parse(calls[0][1].body))).toEqual(['choice'])
    })
    expect(allow).toBeDisabled()
    expect(deny).toBeDisabled()

    resolveApproval()
    await waitFor(() => expect(screen.queryByText('Permission required')).not.toBeInTheDocument())
    expect(screen.getByText('Let me check.')).toBeInTheDocument()
  })

  test('keeps approval actions retryable after a failed response', async () => {
    const fetchMock = vi.fn(async (url, options = {}) => {
      if (url === '/api/talk/status') return response({ capabilities: { text_chat: true } })
      if (url === '/api/talk/message/stream' && options.method === 'POST') {
        return sseResponse([
          { type: 'approval', command: 'echo retry', description: 'Run a command' },
        ], { holdOpen: true })
      }
      if (url === '/api/talk/approval' && options.method === 'POST') {
        return response({ detail: 'Approval response failed safely.' }, 502)
      }
      throw new Error(`unexpected request: ${url}`)
    })
    vi.stubGlobal('fetch', fetchMock)

    render(<ODSTalk />)
    expect(await screen.findByText('Ready')).toBeInTheDocument()
    fireEvent.change(screen.getByPlaceholderText('Message ODS'), { target: { value: 'run it' } })
    fireEvent.click(screen.getByRole('button', { name: 'Send message' }))
    fireEvent.click(await screen.findByRole('button', { name: 'Deny' }))

    expect(await screen.findByRole('alert')).toHaveTextContent('Approval response failed safely.')
    expect(screen.getByRole('button', { name: 'Allow once' })).toBeEnabled()
    expect(screen.getByRole('button', { name: 'Deny' })).toBeEnabled()
  })
})