feat(sessions): add Zustand store for the Agent 1 pipeline state

useSessionStore is the single source of truth for session id/status,
the objective draft/confirmation flags and the chat history. Status
is never computed client-side - it always comes straight from what
window.api.sessions.* actually returned, per the PipelineStepper rule
in docs/Max_Composants_Frontend.md.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
william
2026-10-31 21:44:16 +01:00
co-authored by Claude Sonnet 5
parent 00619add9f
commit 050aad9469
2 changed files with 229 additions and 0 deletions
@@ -0,0 +1,115 @@
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()
beforeEach(() => {
vi.clearAllMocks()
;(window as unknown as { api: Record<string, unknown> }).api = {
sessions: { create, get, sendObjectiveMessage, confirmObjective }
}
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', async () => {
create.mockResolvedValueOnce({ id: 1, status: 'objective_discovery' })
sendObjectiveMessage.mockResolvedValueOnce({ reply: 'ok', ready: false })
await useSessionStore.getState().sendMessage('message')
useSessionStore.getState().restart()
const state = useSessionStore.getState()
expect(state.sessionId).toBeNull()
expect(state.messages).toEqual([])
})
})