Files
ForgeBase/Max Avatar/tests/unit/components/stores/useSessionStore.test.ts
T
williamandClaude Sonnet 5 7179743d4e feat(sessions): route chat messages through the intent-aware backend entry point
Replace the chat window's unconditional create-session-then-send flow
with a single call to the new POST /sessions/messages endpoint: a
plain "salut ça va ?" no longer starts a fake training session, since
the backend's intent router decides that now.

Add sessions.routeMessage to the IPC/preload surface and the sessions
HTTP client. useSessionStore.sendMessage only sets sessionId/status
(fetched fresh via sessions:get) when the backend actually reports
type: "learn". The stepper-window-open + status-broadcast trigger in
main/index.ts moves from sessions:create's handler to
sessions:route-message's, since session creation now happens inside
the backend's routing logic rather than via an explicit prior call.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-11-01 09:02:39 +01:00

162 lines
5.5 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from 'vitest'
import { useSessionStore } from '../../../../src/renderer/src/stores/useSessionStore'
const get = vi.fn()
const routeMessage = vi.fn()
const confirmObjective = vi.fn()
const notifySessionEnded = vi.fn()
beforeEach(() => {
vi.clearAllMocks()
;(window as unknown as { api: Record<string, unknown> }).api = {
sessions: { get, routeMessage, confirmObjective },
notifySessionEnded
}
useSessionStore.getState().restart()
})
describe('useSessionStore', () => {
it('should not create a session when the backend classifies the message as chat', async () => {
routeMessage.mockResolvedValueOnce({ type: 'chat', reply: 'Salut, comment puis-je aider ?' })
await useSessionStore.getState().sendMessage('Salut ça va ?')
expect(routeMessage).toHaveBeenCalledWith('Salut ça va ?')
expect(get).not.toHaveBeenCalled()
const state = useSessionStore.getState()
expect(state.sessionId).toBeNull()
expect(state.messages.map((m) => m.role)).toEqual(['user', 'max'])
expect(state.loading).toBe(false)
})
it('should set the session id and fetch the fresh status when the backend starts a training session', async () => {
routeMessage.mockResolvedValueOnce({
type: 'learn',
sessionId: 1,
reply: 'Quel est ton sujet ?',
ready: false
})
get.mockResolvedValueOnce({ id: 1, status: 'objective_discovery' })
await useSessionStore.getState().sendMessage('Je veux apprendre la facturation')
expect(get).toHaveBeenCalledWith(1)
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'])
})
it('should reuse the same session id on subsequent messages of an ongoing training', async () => {
routeMessage.mockResolvedValueOnce({
type: 'learn',
sessionId: 1,
reply: 'Quel est ton sujet ?',
ready: false
})
get.mockResolvedValueOnce({ id: 1, status: 'objective_discovery' })
await useSessionStore.getState().sendMessage('premier message')
routeMessage.mockResolvedValueOnce({
type: 'learn',
sessionId: 1,
reply: 'Et ton but professionnel ?',
ready: false
})
get.mockResolvedValueOnce({ id: 1, status: 'objective_discovery' })
await useSessionStore.getState().sendMessage('second message')
expect(routeMessage).toHaveBeenCalledTimes(2)
expect(useSessionStore.getState().sessionId).toBe(1)
})
it('should store the draft and ready flag once the agent is ready to confirm', async () => {
const draft = {
subject: 'Facturation freelance',
professionalGoal: 'Facturer sans erreur',
duration: '2 semaines'
}
routeMessage.mockResolvedValueOnce({
type: 'learn',
sessionId: 1,
reply: 'Voici le recap',
ready: true,
draft
})
get.mockResolvedValueOnce({ id: 1, status: 'objective_discovery' })
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 () => {
routeMessage.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 () => {
routeMessage.mockResolvedValueOnce({
type: 'learn',
sessionId: 1,
reply: 'Recap',
ready: true,
draft: { subject: 's', professionalGoal: 'g', duration: '1 semaine' }
})
get.mockResolvedValueOnce({ id: 1, status: 'objective_discovery' })
await useSessionStore.getState().sendMessage('message')
confirmObjective.mockResolvedValueOnce({})
get.mockResolvedValueOnce({ id: 1, status: 'objective_validated' })
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 () => {
routeMessage.mockResolvedValueOnce({
type: 'learn',
sessionId: 1,
reply: 'ok',
ready: false
})
get.mockResolvedValueOnce({ id: 1, status: 'objective_discovery' })
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')
})
})