Files
ForgeBase/Max Avatar/tests/unit/components/stores/useSessionStore.test.ts
T
williamandClaude Sonnet 5 c1440f83c0 feat(windows): route chat/stepper content to their own windows, fix status badge position
- ChatPanel now fills its own window instead of being absolutely
  positioned over the avatar; its interactive regions (messages,
  input bar, objective card) opt out of the window's drag region so
  they stay clickable/selectable while the rest of the panel can be
  used to move the window.
- New StepperView: the #stepper window's root component, relays
  "session-status-changed" from the main process into its own
  Zustand store instance (new setStatus action) since this window has
  no direct access to the chat window's state.
- App.tsx no longer renders ChatPanel/PipelineStepper at all (they
  live in separate windows now) or resizes itself - it only toggles
  the chat window via IPC and keeps a local optimistic flag for the
  context menu's checkmark, resynced if the window is closed
  externally.
- Fixed AvatarStatusIndicator's position: .avatar-status-anchor used
  to be computed from the in-window chat panel's height
  (bottom: calc(2% + 64vh + ...)), which misplaced it once that panel
  stopped living in this window. It now mirrors .thinking's simple
  fixed anchor above the avatar's head.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-31 23:04:10 +01:00

125 lines
4.8 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from 'vitest'
import { useSessionStore } from '../../../../src/renderer/src/stores/useSessionStore'
const create = vi.fn()
const get = vi.fn()
const sendObjectiveMessage = vi.fn()
const confirmObjective = vi.fn()
const notifySessionEnded = vi.fn()
beforeEach(() => {
vi.clearAllMocks()
;(window as unknown as { api: Record<string, unknown> }).api = {
sessions: { create, get, sendObjectiveMessage, confirmObjective },
notifySessionEnded
}
useSessionStore.getState().restart()
})
describe('useSessionStore', () => {
it('should create a session on the first message and send it there', async () => {
create.mockResolvedValueOnce({ id: 1, status: 'objective_discovery' })
sendObjectiveMessage.mockResolvedValueOnce({ reply: 'Quel est ton sujet ?', ready: false })
await useSessionStore.getState().sendMessage('Je veux apprendre la facturation')
expect(create).toHaveBeenCalledOnce()
expect(sendObjectiveMessage).toHaveBeenCalledWith(1, 'Je veux apprendre la facturation')
const state = useSessionStore.getState()
expect(state.sessionId).toBe(1)
expect(state.status).toBe('objective_discovery')
expect(state.messages.map((m) => m.role)).toEqual(['user', 'max'])
expect(state.loading).toBe(false)
})
it('should reuse the existing session id on subsequent messages', async () => {
create.mockResolvedValueOnce({ id: 1, status: 'objective_discovery' })
sendObjectiveMessage.mockResolvedValue({ reply: 'ok', ready: false })
await useSessionStore.getState().sendMessage('premier message')
await useSessionStore.getState().sendMessage('second message')
expect(create).toHaveBeenCalledOnce()
expect(sendObjectiveMessage).toHaveBeenCalledTimes(2)
})
it('should store the draft and ready flag once the agent is ready to confirm', async () => {
create.mockResolvedValueOnce({ id: 1, status: 'objective_discovery' })
const draft = {
subject: 'Facturation freelance',
professionalGoal: 'Facturer sans erreur',
duration: '2 semaines'
}
sendObjectiveMessage.mockResolvedValueOnce({ reply: 'Voici le recap', ready: true, draft })
await useSessionStore.getState().sendMessage('message')
const state = useSessionStore.getState()
expect(state.objectiveReady).toBe(true)
expect(state.objectiveDraft).toEqual(draft)
})
it('should set an error and stop loading when sending a message fails', async () => {
create.mockRejectedValueOnce(new Error('Max API injoignable'))
await useSessionStore.getState().sendMessage('message')
const state = useSessionStore.getState()
expect(state.error).toBe('Max API injoignable')
expect(state.loading).toBe(false)
})
it('should confirm the objective and refresh the session status', async () => {
create.mockResolvedValueOnce({ id: 1, status: 'objective_discovery' })
sendObjectiveMessage.mockResolvedValueOnce({
reply: 'Recap',
ready: true,
draft: { subject: 's', professionalGoal: 'g', duration: '1 semaine' }
})
await useSessionStore.getState().sendMessage('message')
confirmObjective.mockResolvedValueOnce({})
get.mockResolvedValueOnce({ id: 1, status: 'objective_validated' })
;(window as unknown as { api: Record<string, unknown> }).api = {
sessions: { create, get, sendObjectiveMessage, confirmObjective }
}
await useSessionStore
.getState()
.confirmObjective({ subject: 's', professionalGoal: 'g', duration: '1 semaine' })
expect(confirmObjective).toHaveBeenCalledWith(1)
const state = useSessionStore.getState()
expect(state.objectiveConfirmed).toBe(true)
expect(state.status).toBe('objective_validated')
})
it('should error out when confirming without an active session', async () => {
await useSessionStore
.getState()
.confirmObjective({ subject: 's', professionalGoal: 'g', duration: '1 semaine' })
expect(confirmObjective).not.toHaveBeenCalled()
expect(useSessionStore.getState().error).toBe('Aucune session en cours')
})
it('should reset all state on restart and notify the main process', async () => {
create.mockResolvedValueOnce({ id: 1, status: 'objective_discovery' })
sendObjectiveMessage.mockResolvedValueOnce({ reply: 'ok', ready: false })
await useSessionStore.getState().sendMessage('message')
notifySessionEnded.mockClear()
useSessionStore.getState().restart()
expect(notifySessionEnded).toHaveBeenCalledOnce()
const state = useSessionStore.getState()
expect(state.sessionId).toBeNull()
expect(state.messages).toEqual([])
})
it('should set the status directly (used by the stepper window to sync with the main process)', () => {
useSessionStore.getState().setStatus('objective_validated')
expect(useSessionStore.getState().status).toBe('objective_validated')
})
})