feat(sessions): keep JWT in memory and wire IPC to the sessions API
Max Avatar can now call Max API's /sessions endpoints: session.ts keeps the access token in memory after login (never persisted, cleared on logout, same lifecycle as the existing email/keys state), a new sessionsClient.ts wraps the 4 endpoints with the Bearer header, and index.ts exposes them as sessions:create/get/send-objective-message/ confirm-objective IPC handlers (input-validated, requireLoggedIn() gated) surfaced to the renderer as window.api.sessions.*. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 5
parent
ef4cd41ad4
commit
00619add9f
@@ -2,9 +2,11 @@ import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
|
||||
|
||||
const setSessionValues = vi.fn()
|
||||
const setCurrentUserEmail = vi.fn()
|
||||
const setAccessToken = vi.fn()
|
||||
vi.mock('../../src/main/auth/session', () => ({
|
||||
setSessionValues,
|
||||
setCurrentUserEmail
|
||||
setCurrentUserEmail,
|
||||
setAccessToken
|
||||
}))
|
||||
|
||||
const { loginAndLoadKeys } = await import('../../src/main/auth/authClient')
|
||||
@@ -56,6 +58,7 @@ describe('authClient.loginAndLoadKeys()', () => {
|
||||
])
|
||||
)
|
||||
expect(setCurrentUserEmail).toHaveBeenCalledWith('william@example.com')
|
||||
expect(setAccessToken).toHaveBeenCalledWith('jwt-token')
|
||||
|
||||
const calls = (global.fetch as unknown as { mock: { calls: [string, RequestInit][] } }).mock
|
||||
.calls
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
|
||||
|
||||
const getAccessToken = vi.fn<() => string | null>(() => 'jwt-token')
|
||||
vi.mock('../../src/main/auth/session', () => ({ getAccessToken }))
|
||||
|
||||
const { createSession, getSession, sendObjectiveMessage, confirmObjective, MaxApiError } =
|
||||
await import('../../src/main/api/sessionsClient')
|
||||
|
||||
function jsonResponse(body: unknown, ok = true, status = ok ? 200 : 400): Response {
|
||||
return { ok, status, json: () => Promise.resolve(body) } as Response
|
||||
}
|
||||
|
||||
describe('sessionsClient', () => {
|
||||
const originalFetch = global.fetch
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
getAccessToken.mockReturnValue('jwt-token')
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
global.fetch = originalFetch
|
||||
})
|
||||
|
||||
it('should throw MaxApiError when there is no access token', async () => {
|
||||
getAccessToken.mockReturnValue(null)
|
||||
await expect(createSession()).rejects.toThrow(MaxApiError)
|
||||
})
|
||||
|
||||
it('should create a session with the bearer token', async () => {
|
||||
global.fetch = vi
|
||||
.fn()
|
||||
.mockResolvedValueOnce(jsonResponse({ id: 1, status: 'objective_discovery' }))
|
||||
|
||||
const result = await createSession()
|
||||
|
||||
expect(result).toEqual({ id: 1, status: 'objective_discovery' })
|
||||
const calls = (global.fetch as unknown as { mock: { calls: [string, RequestInit][] } }).mock
|
||||
.calls
|
||||
const call = calls.at(0)
|
||||
expect(call?.[0]).toContain('/sessions')
|
||||
expect((call?.[1]?.headers as Record<string, string>)?.Authorization).toBe('Bearer jwt-token')
|
||||
})
|
||||
|
||||
it('should fetch a session by id', async () => {
|
||||
global.fetch = vi
|
||||
.fn()
|
||||
.mockResolvedValueOnce(jsonResponse({ id: 1, status: 'objective_discovery' }))
|
||||
|
||||
await getSession(1)
|
||||
|
||||
const calls = (global.fetch as unknown as { mock: { calls: [string][] } }).mock.calls
|
||||
expect(calls.at(0)?.[0]).toContain('/sessions/1')
|
||||
})
|
||||
|
||||
it('should send an objective message', async () => {
|
||||
global.fetch = vi
|
||||
.fn()
|
||||
.mockResolvedValueOnce(jsonResponse({ reply: 'Quel est ton sujet ?', ready: false }))
|
||||
|
||||
const result = await sendObjectiveMessage(1, 'Je veux apprendre')
|
||||
|
||||
expect(result.ready).toBe(false)
|
||||
const calls = (global.fetch as unknown as { mock: { calls: [string, RequestInit][] } }).mock
|
||||
.calls
|
||||
const body = calls.at(0)?.[1]?.body as string
|
||||
expect(JSON.parse(body)).toEqual({ message: 'Je veux apprendre' })
|
||||
})
|
||||
|
||||
it('should confirm the objective', async () => {
|
||||
global.fetch = vi.fn().mockResolvedValueOnce(
|
||||
jsonResponse({
|
||||
id: 1,
|
||||
sessionId: 1,
|
||||
subject: 'x',
|
||||
professionalGoal: 'y',
|
||||
duration: '2 semaines',
|
||||
validatedAt: '2026-01-01T00:00:00.000Z'
|
||||
})
|
||||
)
|
||||
|
||||
const result = await confirmObjective(1)
|
||||
|
||||
expect(result.sessionId).toBe(1)
|
||||
})
|
||||
|
||||
it('should throw MaxApiError with the server message on a non-ok response', async () => {
|
||||
global.fetch = vi.fn().mockResolvedValueOnce(jsonResponse({ message: 'Conflit' }, false, 409))
|
||||
|
||||
await expect(createSession()).rejects.toThrow('Conflit')
|
||||
})
|
||||
|
||||
it('should throw MaxApiError when the network call fails', async () => {
|
||||
global.fetch = vi.fn().mockRejectedValueOnce(new Error('ECONNREFUSED'))
|
||||
|
||||
await expect(createSession()).rejects.toThrow(MaxApiError)
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user