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>
131 lines
4.1 KiB
TypeScript
131 lines
4.1 KiB
TypeScript
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,
|
|
setAccessToken
|
|
}))
|
|
|
|
const { loginAndLoadKeys } = await import('../../src/main/auth/authClient')
|
|
|
|
function jsonResponse(body: unknown, ok = true, status = ok ? 200 : 400): Response {
|
|
return {
|
|
ok,
|
|
status,
|
|
json: () => Promise.resolve(body)
|
|
} as Response
|
|
}
|
|
|
|
describe('authClient.loginAndLoadKeys()', () => {
|
|
const originalFetch = global.fetch
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
})
|
|
|
|
afterEach(() => {
|
|
global.fetch = originalFetch
|
|
})
|
|
|
|
it('should log in, fetch the decrypted keys, and map them to the expected env var names', async () => {
|
|
global.fetch = vi
|
|
.fn()
|
|
.mockResolvedValueOnce(
|
|
jsonResponse({ accessToken: 'jwt-token', user: { email: 'william@example.com' } })
|
|
)
|
|
.mockResolvedValueOnce(
|
|
jsonResponse({
|
|
openrouter_api_key: 'sk-or-v1-x',
|
|
openrouter_model: 'deepseek/deepseek-chat',
|
|
elevenlabs_api_key: 'sk-el-x',
|
|
elevenlabs_voice_id: 'voice-123',
|
|
unknown_service: 'should-be-ignored'
|
|
})
|
|
)
|
|
|
|
const result = await loginAndLoadKeys('william@example.com', 'password')
|
|
|
|
expect(result).toEqual({ ok: true })
|
|
expect(setSessionValues).toHaveBeenCalledWith(
|
|
new Map([
|
|
['OPENROUTER_API_KEY', 'sk-or-v1-x'],
|
|
['OPENROUTER_MODEL', 'deepseek/deepseek-chat'],
|
|
['ELEVENLABS_API_KEY', 'sk-el-x'],
|
|
['ELEVENLABS_VOICE_ID', 'voice-123']
|
|
])
|
|
)
|
|
expect(setCurrentUserEmail).toHaveBeenCalledWith('william@example.com')
|
|
expect(setAccessToken).toHaveBeenCalledWith('jwt-token')
|
|
|
|
const calls = (global.fetch as unknown as { mock: { calls: [string, RequestInit][] } }).mock
|
|
.calls
|
|
const loginCall = calls.at(0)
|
|
const keysCall = calls.at(1)
|
|
expect(loginCall?.[0]).toContain('/auth/login')
|
|
expect(keysCall?.[0]).toContain('/auth/me/keys')
|
|
expect((keysCall?.[1]?.headers as Record<string, string>).Authorization).toBe(
|
|
'Bearer jwt-token'
|
|
)
|
|
})
|
|
|
|
it('should return the server error message when login fails', async () => {
|
|
global.fetch = vi
|
|
.fn()
|
|
.mockResolvedValueOnce(
|
|
jsonResponse({ message: 'Email ou mot de passe incorrect' }, false, 401)
|
|
)
|
|
|
|
const result = await loginAndLoadKeys('william@example.com', 'mauvais-mot-de-passe')
|
|
|
|
expect(result).toEqual({ ok: false, error: 'Email ou mot de passe incorrect' })
|
|
expect(setSessionValues).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('should report a generic error when login fails without a JSON body', async () => {
|
|
global.fetch = vi.fn().mockResolvedValueOnce({
|
|
ok: false,
|
|
status: 500,
|
|
json: () => Promise.reject(new Error('not json'))
|
|
})
|
|
|
|
const result = await loginAndLoadKeys('william@example.com', 'x')
|
|
|
|
expect(result).toEqual({ ok: false, error: 'Identifiants invalides' })
|
|
})
|
|
|
|
it('should report an error when the keys request fails after a successful login', async () => {
|
|
global.fetch = vi
|
|
.fn()
|
|
.mockResolvedValueOnce(
|
|
jsonResponse({ accessToken: 'jwt-token', user: { email: 'william@example.com' } })
|
|
)
|
|
.mockResolvedValueOnce(jsonResponse({ message: 'nope' }, false, 500))
|
|
|
|
const result = await loginAndLoadKeys('william@example.com', 'password')
|
|
|
|
expect(result).toEqual({
|
|
ok: false,
|
|
error: 'Connexion reussie mais impossible de recuperer les cles API'
|
|
})
|
|
})
|
|
|
|
it('should report a network error message when fetch throws', async () => {
|
|
global.fetch = vi.fn().mockRejectedValue(new Error('network down'))
|
|
|
|
const result = await loginAndLoadKeys('william@example.com', 'x')
|
|
|
|
expect(result).toEqual({ ok: false, error: 'network down' })
|
|
})
|
|
|
|
it('should report a generic message when a non-Error value is thrown', async () => {
|
|
global.fetch = vi.fn().mockRejectedValue('boom')
|
|
|
|
const result = await loginAndLoadKeys('william@example.com', 'x')
|
|
|
|
expect(result).toEqual({ ok: false, error: 'Erreur reseau inconnue' })
|
|
})
|
|
})
|