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.

9.9 KB
Raw Blame
import { createElement } from 'react'
import { act, create, type ReactTestRenderer } from 'react-test-renderer'
import { afterEach, describe, expect, it, vi } from 'vitest'
import { MobileNativeChatPermission } from './MobileNativeChatPermission'
import type { MobileChatPermission } from './mobile-native-chat-permission'
import {
  findAllByHostType,
  findByHostType
} from '../../../test-support/react-test-renderer-host-nodes'

vi.mock('react-native', () => ({
  Pressable: 'Pressable',
  ScrollView: 'ScrollView',
  StyleSheet: { create: (styles: unknown) => styles, hairlineWidth: 1 },
  Text: 'Text',
  View: 'View'
}))

vi.mock('lucide-react-native', () => ({
  ChevronDown: 'ChevronDown',
  ChevronUp: 'ChevronUp',
  ShieldQuestion: 'ShieldQuestion',
  X: 'X'
}))
vi.mock('../../../components/MobileMarkdown', () => ({ MobileMarkdown: 'MobileMarkdown' }))

describe('MobileNativeChatPermission', () => {
  let renderer: ReactTestRenderer | null = null

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

  it('accepts only one response when two presses land in the same render batch', async () => {
    let resolveResponse: (accepted: boolean) => void = () => {}
    const response = new Promise<boolean>((resolve) => (resolveResponse = resolve))
    const onRespond = vi.fn(() => response)
    await act(async () => {
      renderer = create(
        createElement(MobileNativeChatPermission, {
          permission: { title: 'Approve?', options: [{ label: 'Allow', send: '1' }] },
          onRespond
        })
      )
    })
    const button = findByHostType(renderer!.root, 'Pressable')

    act(() => {
      button.props.onPress()
      button.props.onPress()
    })

    expect(onRespond).toHaveBeenCalledOnce()
    await act(async () => resolveResponse(true))
  })

  it('passes the rendered prompt identity to cancel', async () => {
    const onCancel = vi.fn(async () => true)
    await act(async () => {
      renderer = create(
        createElement(MobileNativeChatPermission, {
          permission: {
            title: 'Approve?',
            prompt: { itemId: 'approval-1', expectedRevision: 4 },
            options: [{ label: 'Allow', send: '1' }]
          },
          onRespond: vi.fn(async () => true),
          onCancel
        })
      )
    })
    const cancel = renderer!.root.findByProps({ accessibilityLabel: 'Cancel' })
    await act(async () => cancel.props.onPress())
    expect(onCancel).toHaveBeenCalledWith({ itemId: 'approval-1', expectedRevision: 4 })
  })

  it('keeps oversized provider context in a bounded scroller above the actions', async () => {
    const description = `Workspace access ${'description '.repeat(400)}`
    const decisionReason = `Outside the allowed root ${'reason '.repeat(400)}`
    await act(async () => {
      renderer = create(
        createElement(MobileNativeChatPermission, {
          permission: {
            title: 'Claude wants to read secrets.txt '.repeat(400),
            description,
            decisionReason,
            options: [{ label: 'Allow', send: '1' }]
          },
          onRespond: vi.fn(async () => true)
        })
      )
    })

    const card = renderer!.root.findByProps({ testID: 'native-chat-approval-card' })
    const title = renderer!.root.findByProps({ testID: 'native-chat-approval-title' })
    const content = renderer!.root.findByProps({ testID: 'native-chat-approval-content' })
    const actions = renderer!.root.findByProps({ testID: 'native-chat-approval-actions' })
    const contentText = findAllByHostType(content, 'Text')
    const containsText = (value: string): boolean =>
      contentText.some((node) => {
        const children = Array.isArray(node.props.children)
          ? node.props.children
          : [node.props.children]
        return children.includes(value)
      })

    expect(card.props.style).toMatchObject({ flexShrink: 1, minHeight: 0 })
    expect(title.props).toMatchObject({ numberOfLines: 2, ellipsizeMode: 'tail' })
    expect(content.props.style).toMatchObject({ maxHeight: 240, minHeight: 0, flexShrink: 1 })
    expect(containsText(description)).toBe(true)
    expect(containsText(decisionReason)).toBe(true)
    expect(content.findAllByProps({ children: 'Allow' })).toHaveLength(0)
    expect(actions.findAllByProps({ children: 'Allow' })).toHaveLength(1)
    expect(actions.props.style).toMatchObject({ flexShrink: 0 })
  })

  it('names a blocked path the request does not show, but never the matched ask rule', async () => {
    const fromJournal = {
      title: 'Claude wants to run git push',
      decisionReason: 'Pushing changes the remote',
      blockedPath: 'C:\\qa\\demo\\.git\\config',
      matchedAskRule: {
        source: 'projectSettings',
        toolName: 'Bash',
        ruleContent: 'Bash(git push:*)'
      },
      detail: 'git push origin main',
      options: [{ label: 'Allow', send: '1' }]
    }
    const text = await renderContentText(fromJournal)
    expect(text).toContain('Pushing changes the remote')
    expect(text).toContain('Needs access to: ')
    expect(text).toContain('C:\\qa\\demo\\.git\\config')
    for (const internal of ['Ask rule', 'Bash(git push:*)', 'projectSettings', 'Blocked path']) {
      expect(text).not.toContain(internal)
    }
  })

  it('does not repeat a blocked path the request already shows', async () => {
    const blockedPath = 'C:\\qa\\demo\\notes.md'
    const text = await renderContentText({
      title: 'Claude wants to write notes.md',
      blockedPath,
      detail: JSON.stringify({ file_path: blockedPath, content: 'hi' }, null, 2),
      options: [{ label: 'Allow', send: '1' }]
    })
    expect(text).toContain('notes.md')
    expect(text).not.toContain('Needs access to')
  })

  async function renderContentText(permission: MobileChatPermission): Promise<string> {
    await act(async () => {
      renderer = create(
        createElement(MobileNativeChatPermission, {
          permission,
          onRespond: vi.fn(async () => true)
        })
      )
    })
    const content = renderer!.root.findByProps({ testID: 'native-chat-approval-content' })
    return findAllByHostType(content, 'Text')
      .flatMap((node) => [node.props.children].flat())
      .filter((child): child is string => typeof child === 'string')
      .join('')
  }

  it('renders a plan as markdown inside the same bounded scroller', async () => {
    const planText = '# Release plan\n\n- Run the tests'
    await act(async () => {
      renderer = create(
        createElement(MobileNativeChatPermission, {
          permission: {
            title: 'Claude wants to present its plan',
            subject: { kind: 'plan', text: planText, filePath: '/repo/PLAN.md' },
            detail: 'raw json that must not be shown',
            options: [{ label: 'Approve plan', send: '1' }]
          },
          onRespond: vi.fn(async () => true)
        })
      )
    })

    const content = renderer!.root.findByProps({ testID: 'native-chat-approval-content' })
    const actions = renderer!.root.findByProps({ testID: 'native-chat-approval-actions' })

    expect(content.props.style).toMatchObject({ maxHeight: 240, minHeight: 0, flexShrink: 1 })
    expect(findByHostType(content, 'MobileMarkdown').props.content).toBe(planText)
    const planFileShown = findAllByHostType(content, 'Text').some((node) => {
      const children = Array.isArray(node.props.children)
        ? node.props.children
        : [node.props.children]
      return children.some((child) => typeof child === 'string' && child.includes('/repo/PLAN.md'))
    })
    expect(planFileShown).toBe(true)
    expect(content.findAllByProps({ children: 'raw json that must not be shown' })).toHaveLength(0)
    expect(actions.findAllByProps({ children: 'Approve plan' })).toHaveLength(1)
  })

  describe("a subject of a kind this build cannot draw: a newer Orca's", () => {
    const NEEDS_NEWER_ORCA = 'This request needs a newer version of Orca.'

    async function renderNewer(detail: string | undefined) {
      const onRespond = vi.fn(async () => true)
      const onCancel = vi.fn(async () => true)
      await act(async () => {
        renderer = create(
          createElement(MobileNativeChatPermission, {
            permission: {
              title: 'Review proposed change',
              ...(detail ? { detail } : {}),
              subject: JSON.parse('{"kind":"diff","path":"a.ts"}'),
              prompt: { itemId: 'approval-1', expectedRevision: 2 },
              options: [
                { label: 'Approve', send: 'allow' },
                { label: 'Deny', send: 'deny' }
              ]
            },
            onRespond,
            onCancel
          })
        )
      })
      const actions = renderer!.root.findByProps({ testID: 'native-chat-approval-actions' })
      return { onRespond, onCancel, options: findAllByHostType(actions, 'Pressable') }
    }

    it('with no detail: says so, answers nothing, and only its cancel reaches the host', async () => {
      const { onRespond, onCancel, options } = await renderNewer(undefined)
      expect(
        renderer!.root.findByProps({ testID: 'native-chat-approval-needs-newer-orca' }).props
          .children
      ).toBe(NEEDS_NEWER_ORCA)
      expect(options).toHaveLength(2)
      expect(options.every((option) => option.props.disabled === true)).toBe(true)
      const cancel = renderer!.root.findByProps({ accessibilityLabel: 'Cancel' })
      expect(cancel.props.disabled).toBe(false)
      await act(async () => cancel.props.onPress())
      expect(onCancel).toHaveBeenCalledWith({ itemId: 'approval-1', expectedRevision: 2 })
      expect(onRespond).not.toHaveBeenCalled()
    })

    it('with a detail: shows it, and still approves nothing', async () => {
      const { options } = await renderNewer('# Release')
      const content = renderer!.root.findByProps({ testID: 'native-chat-approval-content' })
      expect(content.findAllByProps({ children: '# Release' })).toHaveLength(1)
      expect(options.every((option) => option.props.disabled === true)).toBe(true)
    })
  })
})