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.

3.7 KB
Raw Blame
import { describe, it, expect, beforeEach } from 'vitest'
import { act, renderHook } from '@testing-library/react'
import { usePrompt } from '../usePrompt'

describe('usePrompt', () => {
  beforeEach(() => {
    act(() => {
      usePrompt.setState({ prompt: '', promptHistory: [], historyIndex: -1, draftPrompt: '' })
    })
  })

  it('should set and clear prompt', () => {
    const { result } = renderHook(() => usePrompt())

    act(() => result.current.setPrompt('Hello'))
    expect(result.current.prompt).toBe('Hello')

    act(() => result.current.resetPrompt())
    expect(result.current.prompt).toBe('')
  })

  describe('prompt history', () => {
    it('should add entries, skip empty/duplicates, and cap at 100', () => {
      const { result } = renderHook(() => usePrompt())

      act(() => {
        result.current.addToHistory('')
        result.current.addToHistory('   ')
        result.current.addToHistory('first')
        result.current.addToHistory('first') // duplicate
        result.current.addToHistory('second')
      })

      expect(result.current.promptHistory).toEqual(['second', 'first'])

      act(() => {
        for (let i = 0; i < 110; i++) result.current.addToHistory(`msg ${i}`)
      })
      expect(result.current.promptHistory.length).toBe(100)
    })

    it('should navigate up/down and restore draft', () => {
      const { result } = renderHook(() => usePrompt())

      act(() => {
        result.current.addToHistory('first')
        result.current.addToHistory('second')
        result.current.setPrompt('my draft')
      })

      // Up through history
      act(() => result.current.navigateHistory('up'))
      expect(result.current.prompt).toBe('second')

      act(() => result.current.navigateHistory('up'))
      expect(result.current.prompt).toBe('first')

      // Can't go past oldest
      act(() => result.current.navigateHistory('up'))
      expect(result.current.prompt).toBe('first')

      // Down restores
      act(() => result.current.navigateHistory('down'))
      expect(result.current.prompt).toBe('second')

      act(() => result.current.navigateHistory('down'))
      expect(result.current.prompt).toBe('my draft')
      expect(result.current.historyIndex).toBe(-1)
    })

    it('should do nothing on empty history or no active browsing', () => {
      const { result } = renderHook(() => usePrompt())

      act(() => result.current.setPrompt('text'))
      act(() => result.current.navigateHistory('up'))
      expect(result.current.prompt).toBe('text')

      act(() => result.current.navigateHistory('down'))
      expect(result.current.prompt).toBe('text')
    })

    it('should reset history index when user types', () => {
      const { result } = renderHook(() => usePrompt())

      act(() => result.current.addToHistory('old'))
      act(() => result.current.navigateHistory('up'))
      expect(result.current.historyIndex).toBe(0)

      act(() => result.current.setPrompt('typing'))
      expect(result.current.historyIndex).toBe(-1)
    })
  })
})

describe('usePrompt draft persistence', () => {
  it('writes only the prompt to sessionStorage so a reload can restore it', () => {
    usePrompt.setState({ prompt: '', promptHistory: [], historyIndex: -1, draftPrompt: '' })
    act(() => usePrompt.getState().setPrompt('unsent draft'))

    const stored = JSON.parse(sessionStorage.getItem('chat-prompt-draft') ?? '{}')
    expect(stored.state).toEqual({ prompt: 'unsent draft' })
  })

  it('rehydrates the draft from sessionStorage', async () => {
    sessionStorage.setItem(
      'chat-prompt-draft',
      JSON.stringify({ state: { prompt: 'restored' }, version: 0 })
    )
    await usePrompt.persist.rehydrate()
    expect(usePrompt.getState().prompt).toBe('restored')
  })
})