Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

5.2 KB
Raw Blame
import { describe, expect, it, vi } from 'vitest'
import { render, screen } from '@testing-library/react'
import '@testing-library/jest-dom'

vi.mock('@/i18n/react-i18next-compat', () => ({
  useTranslation: () => ({ t: (key: string) => key }),
}))

vi.mock('@/hooks/useToolCallRuntime', () => ({
  useToolCallRuntime: (selector: (s: unknown) => unknown) =>
    selector({ diffs: {} }),
}))

import { AgentToolWidget, TerminalWidget } from '../AgentToolWidget'

const renderRunning = (tool: string, target = '') =>
  render(
    <AgentToolWidget
      bar={{ variant: 'workspace', tool, target }}
      state="input-available"
      toolCallId="tc1"
    />
  )

describe('AgentToolWidget', () => {
  // A workspace call is not always a read; the verb is the only progress
  // signal the widget has while the call runs.
  it.each([
    ['write', 'tools:toolCall.writing'],
    ['edit', 'tools:toolCall.editing'],
    ['grep', 'tools:toolCall.searching'],
    ['ls', 'tools:toolCall.listing'],
    ['read', 'tools:toolCall.reading'],
  ])('names what %s is doing while it runs', (tool, key) => {
    renderRunning(tool, tool === 'ls' ? '' : 'a.txt')
    expect(screen.getByText(key)).toBeInTheDocument()
  })

  /// The body is the work, and a large write takes long enough that a verb
  /// says nothing about it -- so the preview takes the throbber's place.
  it('previews a write body instead of naming the verb', () => {
    render(
      <AgentToolWidget
        bar={{
          variant: 'workspace',
          tool: 'write',
          target: 'game.html',
          body: '<!doctype html>\n<html>',
        }}
        state="input-streaming"
        toolCallId="tc1"
      />
    )
    expect(screen.queryByText('tools:toolCall.writing')).not.toBeInTheDocument()
    expect(screen.getByText('<!doctype html>')).toBeInTheDocument()
    // Numbered, so a window onto a long file says where it sits.
    expect(screen.getByText('1')).toBeInTheDocument()
    expect(screen.getByText('2')).toBeInTheDocument()
  })

  /// Same reasoning as the write body: for an `edit` the arguments are the
  /// work, so they replace the throbber rather than sitting behind it.
  it('previews an edit as a diff while it streams', () => {
    render(
      <AgentToolWidget
        bar={{
          variant: 'workspace',
          tool: 'edit',
          target: 'a.ts',
          edits: [{ old_string: 'before', new_string: 'after' }],
        }}
        state="input-streaming"
        toolCallId="tc1"
      />
    )
    expect(screen.queryByText('tools:toolCall.editing')).not.toBeInTheDocument()
    expect(screen.getByText('-before')).toBeInTheDocument()
    expect(screen.getByText('+after')).toBeInTheDocument()
  })

  /// Before the body opens there is nothing to preview, and the verb is all
  /// there is to say.
  it('keeps the verb until the body starts arriving', () => {
    render(
      <AgentToolWidget
        bar={{ variant: 'workspace', tool: 'write', target: 'game.html' }}
        state="input-streaming"
        toolCallId="tc1"
      />
    )
    expect(screen.getByText('tools:toolCall.writing')).toBeInTheDocument()
  })

  /// The notice rides after the exit marker, where the body is cut; it must
  /// still reach the user, since the stale hook no longer guards this call.
  it('shows a hook config notice under a finished shell call', () => {
    render(
      <TerminalWidget
        bar={{ variant: 'terminal', command: 'ls' }}
        state="output-available"
        output={
          'ok\n[exit 0]\n[hook config: Hook pre_tool_use matcher names `bash`]'
        }
      />
    )
    expect(screen.getByText('ok')).toBeInTheDocument()
    expect(
      screen.getByText('Hook pre_tool_use matcher names `bash`')
    ).toBeInTheDocument()
    expect(screen.queryByText(/\[hook config/)).not.toBeInTheDocument()
  })

  // One `memory` tool covers reads and changes alike, so its op, not the tool
  // name, decides the verb.
  it.each([
    ['write', 'prefs', 'tools:toolCall.writing'],
    ['edit', 'prefs', 'tools:toolCall.writing'],
    ['delete', 'prefs', 'tools:toolCall.writing'],
    ['move', 'prefs', 'tools:toolCall.writing'],
    ['search', 'cuda', 'tools:toolCall.searching'],
    ['view', '', 'tools:toolCall.listing'],
    ['view', 'prefs', 'tools:toolCall.reading'],
  ])('names what memory %s (%s) is doing while it runs', (op, target, key) => {
    render(
      <AgentToolWidget
        bar={{ variant: 'workspace', tool: 'memory', target, op }}
        state="input-available"
        toolCallId="tc1"
      />
    )
    expect(screen.getByText(key)).toBeInTheDocument()
  })

  // A bare view lists the whole store; say so rather than leave an empty bar
  // that reads as though the name failed to stream.
  it('names the store on a memory view with no name', () => {
    render(
      <AgentToolWidget
        bar={{ variant: 'workspace', tool: 'memory', target: '', op: 'view' }}
        state="input-available"
        toolCallId="tc1"
      />
    )
    expect(screen.getByText('settings:cowork.memories')).toBeInTheDocument()
  })

  it('prompts for a pattern rather than a path on grep', () => {
    renderRunning('grep')
    expect(
      screen.getByText('tools:toolCall.patternPlaceholder')
    ).toBeInTheDocument()
  })
})