Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.
/* eslint-disable @typescript-eslint/no-explicit-any */
import { describe, it, expect, vi, beforeEach } from 'vitest'
import { render, screen } from '@testing-library/react'
import '@testing-library/jest-dom'
import React from 'react'
const h = vi.hoisted(() => ({
providers: [] as any[],
search: { threadModel: undefined as any },
setCurrentThreadId: vi.fn(),
useTools: vi.fn(),
folder: null as string | null,
providerHasRemoteApiKeys: vi.fn(() => false),
predefinedProviders: [
{ provider: 'openai' },
{ provider: 'llamacpp' },
{ provider: 'jan' },
],
}))
vi.mock('@tanstack/react-router', () => ({
createFileRoute: () => (config: any) => ({ ...config, id: '/' }),
useSearch: () => h.search,
}))
vi.mock('@/i18n/react-i18next-compat', () => ({
useTranslation: () => ({ t: (key: string) => key }),
}))
vi.mock('@/hooks/useModelProvider', () => ({
useModelProvider: () => ({ providers: h.providers }),
}))
vi.mock('@/hooks/useThreads', () => ({
useThreads: () => ({ setCurrentThreadId: h.setCurrentThreadId }),
}))
vi.mock('@/hooks/useFolderDraft', () => ({
useFolderDraft: () => ({
available: true,
folder: h.folder,
attach: vi.fn(),
detach: vi.fn(),
}),
}))
vi.mock('@/hooks/useThreadManagement', () => ({
useThreadManagement: () => ({ addFolder: vi.fn(), folders: [] }),
}))
vi.mock('@/containers/dialogs/AddProjectDialog', () => ({
default: () => null,
}))
vi.mock('@/containers/CoworkComposerMenu', () => ({
CoworkComposerMenu: () => null,
}))
vi.mock('@/containers/DraftWorkspacePill', () => ({
DraftWorkspacePill: ({ folder }: any) =>
folder ? <div data-testid="draft-pill">{folder}</div> : null,
}))
vi.mock('@/hooks/useTools', () => ({
useTools: h.useTools,
}))
vi.mock('@/containers/ChatInput', () => ({
default: ({ model, initialMessage }: any) => (
<div data-testid="chat-input" data-initial={String(initialMessage)}>
{model ? model.id : 'no-model'}
</div>
),
}))
vi.mock('@/containers/HeaderPage', () => ({
default: ({ children }: any) => <div data-testid="header-page">{children}</div>,
}))
vi.mock('@/containers/SetupScreen', () => ({
default: () => <div data-testid="setup-screen" />,
}))
vi.mock('@/lib/utils', () => ({
cn: (...c: any[]) => c.filter(Boolean).join(' '),
}))
vi.mock('@/lib/provider-api-keys', () => ({
providerHasRemoteApiKeys: (p: any) => h.providerHasRemoteApiKeys(p),
}))
vi.mock('@/constants/providers', () => ({
predefinedProviders: h.predefinedProviders,
}))
vi.mock('@/constants/routes', () => ({
route: { home: '/' },
}))
import { Route } from '../index'
const renderComponent = () => {
const Component = Route.component as React.ComponentType
return render(<Component />)
}
describe('Index route', () => {
beforeEach(() => {
vi.clearAllMocks()
h.providers = []
h.search = { threadModel: undefined }
h.folder = null
h.providerHasRemoteApiKeys.mockReturnValue(false)
})
it('validateSearch returns threadModel from search params', () => {
const tm = { id: 'm1', provider: 'p1' }
const result = (Route as any).validateSearch({ threadModel: tm })
expect(result.threadModel).toEqual(tm)
})
it('validateSearch handles missing threadModel', () => {
const result = (Route as any).validateSearch({})
expect(result.threadModel).toBeUndefined()
})
it('renders SetupScreen when no valid providers exist', () => {
h.providers = []
renderComponent()
expect(screen.getByTestId('setup-screen')).toBeInTheDocument()
expect(screen.queryByTestId('chat-input')).not.toBeInTheDocument()
})
it('renders SetupScreen when predefined provider has no api key and no models', () => {
h.providers = [{ provider: 'openai', models: [] }]
h.providerHasRemoteApiKeys.mockReturnValue(false)
renderComponent()
expect(screen.getByTestId('setup-screen')).toBeInTheDocument()
})
it('renders chat UI when predefined provider has api key', () => {
h.providers = [{ provider: 'openai', models: [] }]
h.providerHasRemoteApiKeys.mockReturnValue(true)
renderComponent()
expect(screen.getByTestId('chat-input')).toBeInTheDocument()
expect(screen.getByTestId('header-page')).toBeInTheDocument()
expect(screen.getByText('chat:description')).toBeInTheDocument()
})
it('renders chat UI when llamacpp provider has models', () => {
h.providers = [{ provider: 'llamacpp', models: [{ id: 'x' }] }]
h.providerHasRemoteApiKeys.mockReturnValue(false)
renderComponent()
expect(screen.getByTestId('chat-input')).toBeInTheDocument()
})
it('renders chat UI when jan provider has models', () => {
h.providers = [{ provider: 'jan', models: [{ id: 'j' }] }]
renderComponent()
expect(screen.getByTestId('chat-input')).toBeInTheDocument()
})
it('renders chat UI for custom provider with models, no api key required', () => {
h.providers = [{ provider: 'custom-xyz', models: [{ id: 'c' }] }]
renderComponent()
expect(screen.getByTestId('chat-input')).toBeInTheDocument()
})
it('renders SetupScreen for custom provider with no models', () => {
h.providers = [{ provider: 'custom-xyz', models: [] }]
renderComponent()
expect(screen.getByTestId('setup-screen')).toBeInTheDocument()
})
it('passes threadModel from search into ChatInput', () => {
h.providers = [{ provider: 'openai', models: [] }]
h.providerHasRemoteApiKeys.mockReturnValue(true)
h.search = { threadModel: { id: 'gpt-x', provider: 'openai' } }
renderComponent()
expect(screen.getByTestId('chat-input')).toHaveTextContent('gpt-x')
expect(screen.getByTestId('chat-input')).toHaveAttribute('data-initial', 'true')
})
it('calls setCurrentThreadId(undefined) and useTools on mount', () => {
h.providers = [{ provider: 'openai', models: [] }]
h.providerHasRemoteApiKeys.mockReturnValue(true)
renderComponent()
expect(h.setCurrentThreadId).toHaveBeenCalledWith(undefined)
expect(h.useTools).toHaveBeenCalled()
})
it('shows the folder pill only once a folder is attached', () => {
h.providers = [{ provider: 'openai', models: [] }]
h.providerHasRemoteApiKeys.mockReturnValue(true)
const { unmount } = renderComponent()
expect(screen.queryByTestId('draft-pill')).toBeNull()
unmount()
h.folder = '/home/u/project'
renderComponent()
expect(screen.getByTestId('draft-pill')).toHaveTextContent('/home/u/project')
})
})