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.

6.4 KB
Raw Blame
import {act, fireEvent, screen, waitFor} from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import {render} from '../test/test-utils'
import Pixel from './Pixel'

beforeEach(() => {
  localStorage.clear()
  vi.stubGlobal('fetch', vi.fn(async () => ({ok:true,json:async () => ({available:true,model:'pixel/default'})})))
})
afterEach(() => {vi.unstubAllGlobals();vi.restoreAllMocks()})

it('keeps one file input and draft preview across composer updates and chat switches', async () => {
  render(<Pixel/>)
  await screen.findByText('Available')
  for (const value of ['One', 'Two\nlines', 'Three', '']) {
    fireEvent.change(screen.getByPlaceholderText('Message Portal...'), {target:{value}})
    expect(screen.getAllByRole('button', {name:'Add text file',exact:true})).toHaveLength(1)
    expect(screen.getAllByRole('button', {name:'Preview draft',exact:true})).toHaveLength(1)
  }
  fireEvent(window, new Event('ods:pixel-new-task'))
  expect(screen.getAllByRole('button', {name:'Add text file',exact:true})).toHaveLength(1)
})

function pendingTurn() {
  let finish
  const firstChunk = new Promise(resolve => { finish = resolve })
  let started = false
  return {
    response: {ok:true, headers:new Map([['content-type','text/event-stream']]), body:{getReader:() => ({
      read:() => {
        if (started) return Promise.resolve({done:true})
        started = true
        return firstChunk
      },
      releaseLock:() => {},
    })}},
    finish:() => finish({done:false,value:new TextEncoder().encode(
      'data: {"choices":[{"delta":{"content":"Verified reply"}}]}\n\ndata: [DONE]\n\n',
    )}),
  }
}

// Two real turns typed key by key: a loaded full-suite run can pass 5 s.
it.each(['keyboard', 'mouse'])('allows repeated type/send turns via %s without refocusing manually', async method => {
  const user = userEvent.setup()
  const turns = [pendingTurn(), pendingTurn()]
  let turnIndex = 0
  fetch.mockImplementation(async url => url === '/api/pixel/chat/stream' ? turns[turnIndex++].response
    : {ok:true,json:async () => ({available:true,model:'pixel/default'})})
  render(<Pixel/>)
  const field = await screen.findByPlaceholderText('Message Portal...')
  await waitFor(() => expect(field).toHaveFocus())
  for (let index = 0; index < turns.length; index++) {
    await user.keyboard(`Question ${index + 1}`)
    if (method === 'keyboard') await user.keyboard('{Enter}')
    else await user.click(screen.getByRole('button', {name:'Send',exact:true}))
    // The composer stays editable and focused while Portal answers.
    await screen.findByTitle('Stop')
    expect(field).toBeEnabled()
    expect(field).toHaveFocus()
    await act(async () => { turns[index].finish() })
    await screen.findByTitle('Send')
    expect(field).toHaveFocus()
    expect(field).toHaveValue('')
  }
  const requests = fetch.mock.calls.filter(([url]) => url === '/api/pixel/chat/stream')
  expect(requests).toHaveLength(2)
  expect(JSON.parse(requests[1][1].body).messages.at(-1).content).toBe('Question 2')
}, 15000)

it('keeps a search field focused when a response finishes', async () => {
  const user = userEvent.setup()
  const turn = pendingTurn()
  fetch.mockImplementation(async url => url === '/api/pixel/chat/stream' ? turn.response
    : {ok:true,json:async () => ({available:true,model:'pixel/default'})})
  render(<><input aria-label="Workspace search"/><Pixel/></>)
  const field = await screen.findByPlaceholderText('Message Portal...')
  await user.keyboard('Question{Enter}')
  await screen.findByTitle('Stop')
  const search = screen.getByRole('textbox', {name:'Workspace search'})
  await user.type(search, 'my file')
  await act(async () => { turn.finish() })
  await screen.findByTitle('Send')
  expect(field).toBeEnabled()
  expect(search).toHaveFocus()
  expect(search).toHaveValue('my file')
})

it('keeps composer focus when a request fails so the next message can be typed', async () => {
  const user = userEvent.setup()
  let reject
  const request = new Promise((_, rejectRequest) => { reject = rejectRequest })
  fetch.mockImplementation(async url => url === '/api/pixel/chat/stream' ? request
    : {ok:true,json:async () => ({available:true,model:'pixel/default'})})
  render(<Pixel/>)
  const field = await screen.findByPlaceholderText('Message Portal...')
  await user.keyboard('Question{Enter}')
  await screen.findByTitle('Stop')
  expect(field).toBeEnabled()
  await act(async () => { reject(new Error('Network unavailable')) })
  await screen.findByTitle('Send')
  expect(field).toHaveFocus()
  await user.keyboard('Try again')
  expect(field).toHaveValue('Try again')
})

it.each(['abcd', '/agents abcd', '/AGENTES   abcd', '/goal abcd'])(
  'preserves the displayed caret when background typing into %s', async draft => {
    const user = userEvent.setup()
    render(<Pixel/>)
    const field = await screen.findByPlaceholderText('Message Portal...')
    await waitFor(() => expect(field).toHaveFocus())
    fireEvent.change(field, {target:{value:draft}})
    expect(field).toHaveValue('abcd')
    field.setSelectionRange(2, 2)
    field.blur()
    await user.keyboard('XY')
    expect(field).toHaveValue('abXYcd')
    expect(field).toHaveFocus()
    expect(field.selectionStart).toBe(4)
    if (draft.startsWith('/goal')) expect(screen.getByRole('group', {name:'Goal mode'})).toBeInTheDocument()
    else if (draft !== 'abcd') expect(screen.getByRole('group', {name:'Agent team mode'})).toBeInTheDocument()
  },
)

it('leaves keys and the draft alone while the workspace hides the composer', async () => {
  const user = userEvent.setup()
  render(<Pixel/>)
  const field = await screen.findByPlaceholderText('Message Portal...')
  await waitFor(() => expect(field).toHaveFocus())
  fireEvent.change(field, {target:{value:'Retained draft'}})
  await user.click(screen.getByRole('button', {name:'Workspace', exact:true}))
  await user.click(screen.getByRole('button', {name:'Expand workspace', exact:true}))
  expect(field.closest('.pixel-chat-preview-layout')).toHaveClass('is-workspace-expanded')
  field.blur() // jsdom does not apply the imported display:none rule.
  expect(fireEvent.keyDown(document.body, {key:' '})).toBe(true)
  await user.keyboard('abc')
  expect(field).toHaveValue('Retained draft')
  await user.click(screen.getByRole('button', {name:'Restore workspace size', exact:true}))
  expect(field.closest('.pixel-chat-preview-layout')).not.toHaveClass('is-workspace-expanded')
  expect(field).toHaveValue('Retained draft')
})