Skip to content

armgit / orca

Public Imported from GitHub
0

Orca is the place for working with a fleet of agents (e.g. Grok Bot, Claude Code). Run any coding agent with your own subscription. Available on desktop, mobile and remote runtime.

6.3 KB
Raw Blame
import { createElement } from 'react'
import { act, create, type ReactTestRenderer } from 'react-test-renderer'
import { afterEach, beforeEach, describe, expect, it } from 'vitest'
import type { AskPrompt } from '../../../../../src/shared/native-chat/input/native-chat-ask-types'
import { useMobileNativeChatAskDismiss } from './use-mobile-native-chat-ask-dismiss'
import { mobileNativeChatPromptDismissals } from './mobile-native-chat-prompt-dismissals'

describe('useMobileNativeChatAskDismiss', () => {
  let renderer: ReactTestRenderer | null = null
  let state: ReturnType<typeof useMobileNativeChatAskDismiss> | null = null
  let renders = 0

  beforeEach(() => {
    renders = 0
    mobileNativeChatPromptDismissals.clearForTests()
  })

  afterEach(() => {
    act(() => renderer?.unmount())
    renderer = null
    state = null
  })

  function Harness({
    prompt,
    startedAt = null,
    scopeKey = 'tab-1',
    sessionKey = 'session-1',
    observing = true
  }: {
    prompt: AskPrompt | null
    startedAt?: number | null
    scopeKey?: string | null
    sessionKey?: string | null
    observing?: boolean
  }): null {
    renders += 1
    state = useMobileNativeChatAskDismiss({
      ask: prompt,
      startedAt,
      scopeKey,
      sessionKey,
      observing
    })
    return null
  }

  async function mount(props: Parameters<typeof Harness>[0]): Promise<void> {
    await act(async () => {
      renderer = create(createElement(Harness, props))
    })
  }

  async function update(props: Parameters<typeof Harness>[0]): Promise<void> {
    await act(async () => renderer?.update(createElement(Harness, props)))
  }

  const first: AskPrompt = {
    questions: [
      { question: 'same first', multiSelect: false, options: [] },
      { question: 'old second', multiSelect: false, options: [] }
    ]
  }
  const replacement: AskPrompt = {
    questions: [
      { question: 'same first', multiSelect: false, options: [] },
      { question: 'new second', multiSelect: false, options: [] }
    ]
  }

  it('shows a structurally different replacement without an intervening null', async () => {
    await mount({ prompt: first })
    act(() => state?.dismissAsk())
    expect(state?.showAsk).toBe(false)

    await update({ prompt: replacement })
    expect(state?.showAsk).toBe(true)

    await update({ prompt: first })
    expect(state?.showAsk).toBe(true)
  })

  it('shows an identical status-backed prompt again in a new wait', async () => {
    await mount({ prompt: first, startedAt: 10 })
    act(() => state?.dismissAsk())

    await update({ prompt: first, startedAt: 20 })

    expect(state?.showAsk).toBe(true)
  })

  it('accepts an answer that settles after the status handed the prompt to the transcript', async () => {
    await mount({ prompt: first, startedAt: 10 })
    const acceptedDismiss = state!.dismissAsk

    await update({ prompt: first, startedAt: null })
    act(() => acceptedDismiss())

    expect(state?.showAsk).toBe(false)
  })

  it('keeps an answered status-backed prompt hidden once only the transcript still holds it', async () => {
    await mount({ prompt: first, startedAt: 10 })
    act(() => state?.dismissAsk())

    await update({ prompt: first, startedAt: null })

    expect(state?.showAsk).toBe(false)
  })

  it('ignores an answer that settles after its prompt cleared', async () => {
    await mount({ prompt: first })
    const lateDismiss = state!.dismissAsk

    await update({ prompt: null })
    act(() => lateDismiss())
    await update({ prompt: first })

    expect(state?.showAsk).toBe(true)
  })

  it('keeps a dismissal taken on-chat across a chat→terminal→chat toggle', async () => {
    // Hidden chat derives null without observing the prompt; preserve its dismissal.
    await mount({ prompt: first })
    act(() => state?.dismissAsk())
    expect(state?.showAsk).toBe(false)

    await update({ prompt: null, observing: false })
    await update({ prompt: first, observing: true })

    expect(state?.showAsk).toBe(false)
  })

  it('keeps a dismissal across a chat→terminal→chat toggle', async () => {
    // A hidden prompt’s null proves nothing about agent state.
    await mount({ prompt: first })
    const acceptedDismiss = state!.dismissAsk

    await update({ prompt: null, observing: false })
    act(() => acceptedDismiss())
    await update({ prompt: first, observing: true })

    expect(state?.showAsk).toBe(false)
  })

  it('forgets the dismissal once the prompt clears while observable', async () => {
    await mount({ prompt: first })
    act(() => state?.dismissAsk())

    await update({ prompt: null, observing: true })
    await update({ prompt: first, observing: true })

    expect(state?.showAsk).toBe(true)
  })

  it('scopes the dismissal to the tab that showed the card', async () => {
    await mount({ prompt: first, scopeKey: 'tab-1' })
    act(() => state?.dismissAsk())

    await update({ prompt: first, scopeKey: 'tab-2' })
    expect(state?.showAsk).toBe(true)
    act(() => state?.dismissAsk())
    expect(state?.showAsk).toBe(false)

    await update({ prompt: first, scopeKey: 'tab-1' })
    expect(state?.showAsk).toBe(false)

    await update({ prompt: first, scopeKey: 'tab-2' })
    expect(state?.showAsk).toBe(false)
  })

  it('shows an identical prompt after the tab starts a new provider session', async () => {
    await mount({ prompt: first, sessionKey: 'session-1' })
    act(() => state?.dismissAsk())

    await update({ prompt: first, sessionKey: 'session-2' })

    expect(state?.showAsk).toBe(true)
  })

  it('reports nothing to show without a prompt', async () => {
    await mount({ prompt: null })
    expect(state?.showAsk).toBe(false)
  })

  it('does not re-render a scope with nothing dismissed when the prompt changes', async () => {
    await mount({ prompt: first })
    const afterMount = renders

    await update({ prompt: replacement })

    // Preserve Map identity on no-op resets; this route-level hook otherwise rerenders the entire session.
    expect(renders).toBe(afterMount + 1)
  })

  it('records a settled answer against its originating tab', async () => {
    await mount({ prompt: first, scopeKey: 'tab-1' })
    const tabOneDismiss = state!.dismissAsk

    await update({ prompt: replacement, scopeKey: 'tab-2' })
    act(() => tabOneDismiss())
    await update({ prompt: first, scopeKey: 'tab-1' })

    expect(state?.showAsk).toBe(false)
  })
})