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.

test(web-app): cover the Cmd/Ctrl+J assistant cycle and drop its cast

Akarshan Biswasb29412720430
diff --git a/web-app/src/components/AssistantsMenu.tsx b/web-app/src/components/AssistantsMenu.tsx
index 7e81ee4b4..a05b6e345 100644
--- a/web-app/src/components/AssistantsMenu.tsx
+++ b/web-app/src/components/AssistantsMenu.tsx
@@ -5,7 +5,7 @@ type AssistantMenuProps = {
selectedAssistant: string | undefined
setSelectedAssistant: (assistant: string) => void
currentThread: Thread | undefined
- updateCurrentThreadAssistant: (assistant: Assistant) => void
+ updateCurrentThreadAssistant: (assistant: Assistant | undefined) => void
assistants: Assistant[]
}
@@ -48,7 +48,7 @@ export function AssistantsMenu({
onClick={() => {
setSelectedAssistant('')
if (currentThread) {
- updateCurrentThreadAssistant(undefined as unknown as Assistant)
+ updateCurrentThreadAssistant(undefined)
}
}}
>
diff --git a/web-app/src/containers/ChatInput.tsx b/web-app/src/containers/ChatInput.tsx
index c31a565a5..b16d21932 100644
--- a/web-app/src/containers/ChatInput.tsx
+++ b/web-app/src/containers/ChatInput.tsx
@@ -458,7 +458,7 @@ const ChatInput = memo(function ChatInput({
: selectedAssistantId
const index = assistants.findIndex((assistant) => assistant.id === activeId)
const next = assistants[index + 1]
- if (currentThread) updateCurrentThreadAssistant(next as Assistant)
+ if (currentThread) updateCurrentThreadAssistant(next)
else setSelectedAssistantId(next?.id || '')
}
useEffect(() => {
diff --git a/web-app/src/containers/SamplerPopover.tsx b/web-app/src/containers/SamplerPopover.tsx
index 91b8da864..54ab5f1c3 100644
--- a/web-app/src/containers/SamplerPopover.tsx
+++ b/web-app/src/containers/SamplerPopover.tsx
@@ -57,7 +57,7 @@ interface SamplerPopoverProps {
currentThread: Thread | undefined
selectedAssistantId: string | undefined
setSelectedAssistantId: (id: string) => void
- updateCurrentThreadAssistant: (assistant: Assistant) => void
+ updateCurrentThreadAssistant: (assistant: Assistant | undefined) => void
}
}
diff --git a/web-app/src/containers/__tests__/ChatInput.test.tsx b/web-app/src/containers/__tests__/ChatInput.test.tsx
index af8a1aa3a..961b5683c 100644
--- a/web-app/src/containers/__tests__/ChatInput.test.tsx
+++ b/web-app/src/containers/__tests__/ChatInput.test.tsx
@@ -326,6 +326,7 @@ vi.mock('@/lib/platform/utils', () => ({
import ChatInput from '../ChatInput'
import { useImageAttachmentConfig } from '@/hooks/useImageAttachmentConfig'
import { useWebSearchConfig } from '@/hooks/useWebSearchConfig'
+import { useAssistantSwitcher } from '@/hooks/useAssistantSwitcher'
// Helpers -------------------------------------------------------------------
@@ -349,6 +350,7 @@ const resetAll = () => {
clearQueueMock.mockClear()
for (const k of Object.keys(queueState)) delete queueState[k]
getCurrentThreadMock.mockReturnValue(undefined)
+ updateCurrentThreadAssistantMock.mockClear()
useImageAttachmentConfig.setState({ maxImageSizeMB: 10 })
useWebSearchConfig.setState({ webSearchEnabled: false })
}
@@ -381,6 +383,60 @@ describe('ChatInput', () => {
expect(screen.getByTestId('stub-assistants-menu')).toHaveTextContent('a2')
})
+ describe('Cmd/Ctrl+J assistant cycle', () => {
+ const cycle = () =>
+ act(() => {
+ useAssistantSwitcher.getState().cycleHandler?.()
+ })
+
+ it('cycles a1 -> a2 -> None -> a1 on a home composer', () => {
+ renderInput()
+ const shown = () => screen.getByTestId('stub-assistants-menu').textContent
+ expect(shown()).toBe('a1')
+ cycle()
+ expect(shown()).toBe('a2')
+ cycle()
+ expect(shown()).toBe('')
+ cycle()
+ expect(shown()).toBe('a1')
+ })
+
+ it('cycles a1 -> a2 -> None -> a1 on an open thread', () => {
+ const withAssistant = (id?: string) => ({
+ id: 'thread-1',
+ assistants: id ? [{ id }] : [],
+ })
+ getCurrentThreadMock.mockReturnValue(withAssistant('a1') as never)
+ // ChatInput is memoized and takes no thread prop, so a changed store
+ // value only shows after a fresh mount.
+ const remount = (v: ReturnType<typeof renderInput>) => {
+ v.unmount()
+ return renderInput()
+ }
+ let view = renderInput()
+
+ cycle()
+ expect(updateCurrentThreadAssistantMock).toHaveBeenLastCalledWith(
+ expect.objectContaining({ id: 'a2' })
+ )
+
+ getCurrentThreadMock.mockReturnValue(withAssistant('a2') as never)
+ view = remount(view)
+ cycle()
+ expect(updateCurrentThreadAssistantMock).toHaveBeenLastCalledWith(
+ undefined
+ )
+
+ getCurrentThreadMock.mockReturnValue(withAssistant() as never)
+ view = remount(view)
+ cycle()
+ expect(updateCurrentThreadAssistantMock).toHaveBeenLastCalledWith(
+ expect.objectContaining({ id: 'a1' })
+ )
+ expect(updateCurrentThreadAssistantMock).toHaveBeenCalledTimes(3)
+ })
+ })
+
it('disables the send button when prompt is empty', () => {
renderInput()
const btn = document.querySelector(
diff --git a/web-app/src/hooks/useThreads.ts b/web-app/src/hooks/useThreads.ts
index 5ccb94d96..dcc603a33 100644
--- a/web-app/src/hooks/useThreads.ts
+++ b/web-app/src/hooks/useThreads.ts
@@ -45,7 +45,7 @@ type ThreadState = {
) => Promise<Thread>
updateCurrentThreadModel: (model: ThreadModel) => void
getFilteredThreads: (searchTerm: string) => Thread[]
- updateCurrentThreadAssistant: (assistant: Assistant) => void
+ updateCurrentThreadAssistant: (assistant: Assistant | undefined) => void
/**
* Persist new sampling parameters on one thread's assistant. Resolves to the
* saved assistant, or undefined when the thread's assistant changed (identity)