Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.
test(assistant-switcher): cover cycle/wrap-around logic; i18n 'No assistant'
Akarshan Biswasd96a8cbec354
diff --git a/web-app/src/containers/AssistantSwitcher.tsx b/web-app/src/containers/AssistantSwitcher.tsxindex b07e8273d..63f85ab99 100644--- a/web-app/src/containers/AssistantSwitcher.tsx+++ b/web-app/src/containers/AssistantSwitcher.tsx@@ -14,6 +14,7 @@ import { Button } from '@/components/ui/button'import { AvatarEmoji } from '@/containers/AvatarEmoji'import { AssistantsMenu } from '@/components/AssistantsMenu'import { useAssistantSwitcher } from '@/hooks/useAssistantSwitcher'+import { useTranslation } from '@/i18n/react-i18next-compat'export interface AssistantSwitcherProps {assistants: Assistant[]@@ -37,6 +38,7 @@ export function AssistantSwitcher({setSelectedAssistantId,updateCurrentThreadAssistant,}: AssistantSwitcherProps) {+ const { t } = useTranslation()const open = useAssistantSwitcher((s) => s.open)const setOpen = useAssistantSwitcher((s) => s.setOpen)const setCycleHandler = useAssistantSwitcher((s) => s.setCycleHandler)@@ -101,7 +103,7 @@ export function AssistantSwitcher({<IconUser size={14} className="text-muted-foreground" />)}<span className="text-sm font-medium truncate max-w-32">- {activeAssistant?.name ?? 'No assistant'}+ {activeAssistant?.name ?? t('common:noAssistant')}</span><IconChevronDown size={14} className="text-muted-foreground" /></Button>diff --git a/web-app/src/containers/SamplerPopover.tsx b/web-app/src/containers/SamplerPopover.tsxindex 471e6bd86..8ebd9d7d5 100644--- a/web-app/src/containers/SamplerPopover.tsx+++ b/web-app/src/containers/SamplerPopover.tsx@@ -28,6 +28,7 @@ import {import { AssistantsMenu } from '@/components/AssistantsMenu'import { AvatarEmoji } from '@/containers/AvatarEmoji'+import { useTranslation } from '@/i18n/react-i18next-compat'import { ParametersSection } from '@/containers/ParametersSection'import { useAssistant } from '@/hooks/useAssistant'import { useModelProvider } from '@/hooks/useModelProvider'@@ -256,6 +257,7 @@ function AssistantHeader({currentAssistant,assistantSwitcher,}: AssistantHeaderProps) {+ const { t } = useTranslation()const label = (<span className="flex items-center gap-1.5 min-w-0">{currentAssistant?.avatar ? (@@ -268,7 +270,7 @@ function AssistantHeader({<IconUser size={14} className="text-muted-foreground" />)}<span className="text-sm font-medium truncate">- {currentAssistant?.name ?? 'No assistant'}+ {currentAssistant?.name ?? t('common:noAssistant')}</span></span>)diff --git a/web-app/src/containers/__tests__/AssistantSwitcher.test.tsx b/web-app/src/containers/__tests__/AssistantSwitcher.test.tsxnew file mode 100644index 000000000..26b9331af--- /dev/null+++ b/web-app/src/containers/__tests__/AssistantSwitcher.test.tsx@@ -0,0 +1,118 @@+import { describe, it, expect, vi, beforeEach } from 'vitest'+import { render, screen, cleanup } from '@testing-library/react'+import { AssistantSwitcher } from '../AssistantSwitcher'+import { useAssistantSwitcher } from '@/hooks/useAssistantSwitcher'++vi.mock('@/i18n/react-i18next-compat', () => ({+ useTranslation: () => ({ t: (k: string) => k }),+}))++const assistant = (id: string, name: string): Assistant =>+ ({ id, name, avatar: '😀' }) as unknown as Assistant++const thread = (assistantId?: string): Thread =>+ ({+ id: 't1',+ assistants: assistantId ? [{ id: assistantId } as Assistant] : [],+ }) as unknown as Thread++const cycle = () => useAssistantSwitcher.getState().cycleHandler?.()++describe('AssistantSwitcher cycle logic', () => {+ let setSelectedAssistantId: ReturnType<typeof vi.fn>+ let updateCurrentThreadAssistant: ReturnType<typeof vi.fn>++ beforeEach(() => {+ cleanup()+ setSelectedAssistantId = vi.fn()+ updateCurrentThreadAssistant = vi.fn()+ useAssistantSwitcher.setState({ open: false, cycleHandler: null })+ })++ const renderSwitcher = (overrides: Partial<{+ assistants: Assistant[]+ currentThread: Thread | undefined+ selectedAssistantId: string | undefined+ }> = {}) =>+ render(+ <AssistantSwitcher+ assistants={overrides.assistants ?? [assistant('a1', 'Alice'), assistant('a2', 'Bob')]}+ currentThread={'currentThread' in overrides ? overrides.currentThread : undefined}+ selectedAssistantId={overrides.selectedAssistantId}+ setSelectedAssistantId={setSelectedAssistantId}+ updateCurrentThreadAssistant={updateCurrentThreadAssistant}+ />+ )++ it('renders nothing with a single assistant and never registers a usable cycle', () => {+ const { container } = renderSwitcher({ assistants: [assistant('a1', 'Alice')] })+ expect(container.firstChild).toBeNull()+ cycle()+ expect(setSelectedAssistantId).not.toHaveBeenCalled()+ expect(updateCurrentThreadAssistant).not.toHaveBeenCalled()+ })++ it('advances to the next assistant off-thread', () => {+ renderSwitcher({ selectedAssistantId: 'a1' })+ cycle()+ expect(setSelectedAssistantId).toHaveBeenCalledWith('a2')+ })++ it('wraps around from the last assistant to the first off-thread', () => {+ renderSwitcher({ selectedAssistantId: 'a2' })+ cycle()+ expect(setSelectedAssistantId).toHaveBeenCalledWith('a1')+ })++ it('treats an unknown selected id as index -1 and selects the first', () => {+ renderSwitcher({ selectedAssistantId: 'missing' })+ cycle()+ // findIndex => -1, (-1 + 1) % len === 0+ expect(setSelectedAssistantId).toHaveBeenCalledWith('a1')+ })++ it('cycles the thread assistant via updateCurrentThreadAssistant inside a thread', () => {+ renderSwitcher({ currentThread: thread('a1') })+ cycle()+ expect(updateCurrentThreadAssistant).toHaveBeenCalledWith(+ expect.objectContaining({ id: 'a2' })+ )+ expect(setSelectedAssistantId).not.toHaveBeenCalled()+ })++ it('wraps around the thread assistant', () => {+ renderSwitcher({ currentThread: thread('a2') })+ cycle()+ expect(updateCurrentThreadAssistant).toHaveBeenCalledWith(+ expect.objectContaining({ id: 'a1' })+ )+ })++ it('starts from the first assistant when the thread has only a model-only assistant', () => {+ renderSwitcher({ currentThread: thread('model-only') })+ cycle()+ expect(updateCurrentThreadAssistant).toHaveBeenCalledWith(+ expect.objectContaining({ id: 'a1' })+ )+ })++ it('starts from the first assistant when the thread has no assistant', () => {+ renderSwitcher({ currentThread: thread(undefined) })+ cycle()+ expect(updateCurrentThreadAssistant).toHaveBeenCalledWith(+ expect.objectContaining({ id: 'a1' })+ )+ })++ it('unregisters the cycle handler on unmount', () => {+ const { unmount } = renderSwitcher({ selectedAssistantId: 'a1' })+ expect(useAssistantSwitcher.getState().cycleHandler).not.toBeNull()+ unmount()+ expect(useAssistantSwitcher.getState().cycleHandler).toBeNull()+ })++ it('falls back to the i18n key when no assistant is active off-thread', () => {+ renderSwitcher({ selectedAssistantId: undefined })+ expect(screen.getByText('common:noAssistant')).toBeInTheDocument()+ })+})diff --git a/web-app/src/locales/en/common.json b/web-app/src/locales/en/common.jsonindex 4d36b87a8..1a401a28e 100644--- a/web-app/src/locales/en/common.json+++ b/web-app/src/locales/en/common.json@@ -114,6 +114,7 @@"noModelsFoundFor": "No models found for \"{{searchValue}}\"","failedToLoadModels": "Failed to load models","noModels": "No models found",+ "noAssistant": "No assistant","customAvatar": "Custom avatar","editAssistant": "Edit Assistant","jan": "Jan",