diff --git a/Max Avatar/src/renderer/src/components/LoginCard.tsx b/Max Avatar/src/renderer/src/components/LoginCard.tsx new file mode 100644 index 0000000..0dc544f --- /dev/null +++ b/Max Avatar/src/renderer/src/components/LoginCard.tsx @@ -0,0 +1,142 @@ +import { useState } from 'react' +import { Bot, Eye, EyeOff } from 'lucide-react' + +export interface LoginCardProps { + onSuccess: (token: string) => void + onError?: (message: string) => void +} + +interface FieldErrors { + email?: string + password?: string +} + +/** + * Panneau de connexion flottant (docs/Max_Composants_Frontend.md section 1). Un avatar emoji est + * demande par la spec, mais ca contredit la regle globale "aucune icone emoji" du meme document et + * de Max_Design_System.md : on utilise donc l'icone Lucide `Bot` a la place. + */ +export default function LoginCard({ onSuccess, onError }: LoginCardProps): React.JSX.Element { + const [email, setEmail] = useState('') + const [password, setPassword] = useState('') + const [showPassword, setShowPassword] = useState(false) + const [fieldErrors, setFieldErrors] = useState({}) + const [submitError, setSubmitError] = useState(null) + const [loading, setLoading] = useState(false) + const [success, setSuccess] = useState(false) + + function validate(): FieldErrors { + const errors: FieldErrors = {} + if (!email.includes('@')) errors.email = 'Adresse email invalide' + if (password.length === 0) errors.password = 'Mot de passe requis' + return errors + } + + const submit = (event: React.FormEvent): void => { + event.preventDefault() + if (loading) return + + const errors = validate() + setFieldErrors(errors) + if (Object.keys(errors).length > 0) return + + setLoading(true) + setSubmitError(null) + window.api + .login(email.trim(), password) + .then((result) => { + if (result.ok) { + setSuccess(true) + // window.api.login ne renvoie pas le jeton (il reste dans le process main, voir + // session.ts) : on signale juste le succes au composant parent. + onSuccess('') + } else { + setSubmitError(result.error) + onError?.(result.error) + } + }) + .catch(() => { + const message = 'Erreur inattendue' + setSubmitError(message) + onError?.(message) + }) + .finally(() => setLoading(false)) + } + + if (success) { + return ( +
+

+ Connexion reussie. Cette fenetre se ferme automatiquement... +

+
+ ) + } + + return ( +
+
+
+ +
+ + + + + {submitError && ( +

+ {submitError} +

+ )} + + +
+
+ ) +} diff --git a/Max Avatar/src/renderer/src/components/LoginView.tsx b/Max Avatar/src/renderer/src/components/LoginView.tsx index 545f29b..1f81351 100644 --- a/Max Avatar/src/renderer/src/components/LoginView.tsx +++ b/Max Avatar/src/renderer/src/components/LoginView.tsx @@ -1,74 +1,11 @@ -import { useState } from 'react' +import LoginCard from './LoginCard' +/** + * Point d'entree de la fenetre de connexion separee (voir src/main/auth/loginWindow.ts et la + * route #login dans main.tsx). La fermeture de la fenetre et la reactivation de l'ecoute vocale + * sont deja declenchees cote main process une fois la connexion reussie (voir le handler IPC + * "auth-login" dans src/main/index.ts) : rien d'autre a faire ici que d'afficher LoginCard. + */ export default function LoginView(): React.JSX.Element { - const [email, setEmail] = useState('') - const [password, setPassword] = useState('') - const [error, setError] = useState(null) - const [submitting, setSubmitting] = useState(false) - const [success, setSuccess] = useState(false) - - const canSubmit = email.trim() !== '' && password !== '' && !submitting - - const submit = (event: React.FormEvent): void => { - event.preventDefault() - if (!canSubmit) return - setSubmitting(true) - setError(null) - window.api - .login(email.trim(), password) - .then((result) => { - if (result.ok) { - setSuccess(true) - } else { - setError(result.error) - } - }) - .catch(() => setError('Erreur inattendue')) - .finally(() => setSubmitting(false)) - } - - return ( -
-

Connexion a Max

- - {success ? ( -

- Connexion reussie. Cette fenetre se ferme automatiquement... -

- ) : ( -
- - - - - {error && ( -

- {error} -

- )} - - -
- )} -
- ) + return {}} /> } diff --git a/Max Avatar/src/renderer/src/styles/app.css b/Max Avatar/src/renderer/src/styles/app.css index a76df06..f64d0e1 100644 --- a/Max Avatar/src/renderer/src/styles/app.css +++ b/Max Avatar/src/renderer/src/styles/app.css @@ -94,11 +94,18 @@ flex-direction: column; } +.auth-panel__brand { + display: flex; + align-items: center; + gap: var(--space-2); + color: var(--accent); + margin-bottom: var(--space-5); +} + .auth-panel__title { font-size: var(--text-md); font-weight: var(--weight-semi); color: var(--text-1); - margin-bottom: var(--space-5); } .auth-panel__success { @@ -137,6 +144,39 @@ border-color: var(--border-active); } +.auth-field__password { + position: relative; + display: flex; +} + +.auth-field__password .auth-field__input { + padding-right: calc(var(--space-3) + 24px); +} + +.auth-field__toggle { + position: absolute; + right: var(--space-2); + top: 50%; + transform: translateY(-50%); + border: none; + background: none; + color: var(--text-2); + display: flex; + align-items: center; + cursor: pointer; +} + +.auth-field__toggle:hover { + color: var(--text-1); +} + +.auth-field__error { + display: block; + margin-top: var(--space-1); + font-size: var(--text-xs); + color: var(--red); +} + .auth-panel__error { margin-bottom: var(--space-4); font-size: var(--text-sm); diff --git a/Max Avatar/tests/unit/components/LoginCard.test.tsx b/Max Avatar/tests/unit/components/LoginCard.test.tsx new file mode 100644 index 0000000..607d4fd --- /dev/null +++ b/Max Avatar/tests/unit/components/LoginCard.test.tsx @@ -0,0 +1,104 @@ +import { describe, it, expect, vi, beforeEach } from 'vitest' +import { render, screen, fireEvent, waitFor } from '@testing-library/react' +import LoginCard from '../../../src/renderer/src/components/LoginCard' + +const login = vi.fn() +const onSuccess = vi.fn() +const onError = vi.fn() + +beforeEach(() => { + vi.clearAllMocks() + ;(window as unknown as { api: Record }).api = { login } +}) + +function fillAndSubmit(email: string, password: string): void { + fireEvent.change(screen.getByLabelText('Email'), { target: { value: email } }) + fireEvent.change(screen.getByLabelText('Mot de passe'), { target: { value: password } }) + fireEvent.click(screen.getByRole('button', { name: 'Se connecter' })) +} + +describe('', () => { + it('should show an inline error and not call the API when the email is invalid', async () => { + render() + + fillAndSubmit('not-an-email', 'password123') + + expect(await screen.findByText('Adresse email invalide')).toBeInTheDocument() + expect(login).not.toHaveBeenCalled() + }) + + it('should show an inline error and not call the API when the password is empty', async () => { + render() + + fillAndSubmit('william@example.com', '') + + expect(await screen.findByText('Mot de passe requis')).toBeInTheDocument() + expect(login).not.toHaveBeenCalled() + }) + + it('should toggle the password field between hidden and visible text', () => { + render() + + const passwordInput = screen.getByLabelText('Mot de passe') + expect(passwordInput.type).toBe('password') + + fireEvent.click(screen.getByRole('button', { name: 'Afficher le mot de passe' })) + expect(passwordInput.type).toBe('text') + + fireEvent.click(screen.getByRole('button', { name: 'Masquer le mot de passe' })) + expect(passwordInput.type).toBe('password') + }) + + it('should call the API and show a success message on success', async () => { + login.mockResolvedValue({ ok: true }) + render() + + fillAndSubmit('william@example.com', 'password123') + + expect(login).toHaveBeenCalledWith('william@example.com', 'password123') + await waitFor(() => expect(screen.getByText(/Connexion reussie/)).toBeInTheDocument()) + expect(onSuccess).toHaveBeenCalled() + expect(onError).not.toHaveBeenCalled() + }) + + it('should show the server error and call onError on failure', async () => { + login.mockResolvedValue({ ok: false, error: 'Email ou mot de passe incorrect' }) + render() + + fillAndSubmit('william@example.com', 'mauvais') + + await waitFor(() => + expect(screen.getByText('Email ou mot de passe incorrect')).toBeInTheDocument() + ) + expect(onError).toHaveBeenCalledWith('Email ou mot de passe incorrect') + }) + + it('should show a generic error when the IPC call itself rejects', async () => { + login.mockRejectedValue(new Error('IPC down')) + render() + + fillAndSubmit('william@example.com', 'password123') + + await waitFor(() => expect(screen.getByText('Erreur inattendue')).toBeInTheDocument()) + expect(onError).toHaveBeenCalledWith('Erreur inattendue') + }) + + it('should disable the submit button and show a loading label while submitting', async () => { + let resolveLogin: (value: { ok: true }) => void = () => {} + login.mockReturnValue( + new Promise((resolve) => { + resolveLogin = resolve + }) + ) + render() + + fillAndSubmit('william@example.com', 'password123') + + const submit = screen.getByRole('button', { name: 'Se connecter' }) + expect(submit).toBeDisabled() + expect(screen.getByText('Connexion...')).toBeInTheDocument() + + resolveLogin({ ok: true }) + await waitFor(() => expect(screen.getByText(/Connexion reussie/)).toBeInTheDocument()) + }) +}) diff --git a/Max Avatar/tests/unit/components/LoginView.test.tsx b/Max Avatar/tests/unit/components/LoginView.test.tsx index 9598148..5545100 100644 --- a/Max Avatar/tests/unit/components/LoginView.test.tsx +++ b/Max Avatar/tests/unit/components/LoginView.test.tsx @@ -1,71 +1,14 @@ -import { describe, it, expect, vi, beforeEach } from 'vitest' -import { render, screen, fireEvent, waitFor } from '@testing-library/react' +import { describe, it, expect, beforeEach, vi } from 'vitest' +import { render, screen } from '@testing-library/react' import LoginView from '../../../src/renderer/src/components/LoginView' -const login = vi.fn() - beforeEach(() => { - vi.clearAllMocks() - ;(window as unknown as { api: Record }).api = { login } + ;(window as unknown as { api: Record }).api = { login: vi.fn() } }) -describe('LoginView', () => { - it('should keep the submit button disabled until both fields are filled', () => { +describe('', () => { + it('should render the LoginCard form', () => { render() - - const submit = screen.getByRole('button', { name: 'Se connecter' }) - expect(submit).toBeDisabled() - - fireEvent.change(screen.getByLabelText('Email'), { target: { value: 'william@example.com' } }) - expect(submit).toBeDisabled() - - fireEvent.change(screen.getByLabelText('Mot de passe'), { target: { value: 'password123' } }) - expect(submit).toBeEnabled() - }) - - it('should call window.api.login with the trimmed email and show a success message', async () => { - login.mockResolvedValue({ ok: true }) - render() - - fireEvent.change(screen.getByLabelText('Email'), { - target: { value: ' william@example.com ' } - }) - fireEvent.change(screen.getByLabelText('Mot de passe'), { target: { value: 'password123' } }) - fireEvent.click(screen.getByRole('button', { name: 'Se connecter' })) - - expect(login).toHaveBeenCalledWith('william@example.com', 'password123') - await waitFor(() => expect(screen.getByText(/Connexion reussie/)).toBeInTheDocument()) - }) - - it('should display the error message returned by the API on failure', async () => { - login.mockResolvedValue({ ok: false, error: 'Email ou mot de passe incorrect' }) - render() - - fireEvent.change(screen.getByLabelText('Email'), { target: { value: 'william@example.com' } }) - fireEvent.change(screen.getByLabelText('Mot de passe'), { target: { value: 'mauvais' } }) - fireEvent.click(screen.getByRole('button', { name: 'Se connecter' })) - - await waitFor(() => - expect(screen.getByRole('alert')).toHaveTextContent('Email ou mot de passe incorrect') - ) - }) - - it('should display a generic error message when the IPC call itself rejects', async () => { - login.mockRejectedValue(new Error('IPC down')) - render() - - fireEvent.change(screen.getByLabelText('Email'), { target: { value: 'william@example.com' } }) - fireEvent.change(screen.getByLabelText('Mot de passe'), { target: { value: 'password123' } }) - fireEvent.click(screen.getByRole('button', { name: 'Se connecter' })) - - await waitFor(() => expect(screen.getByRole('alert')).toHaveTextContent('Erreur inattendue')) - }) - - it('should not submit when the fields are empty', () => { - render() - - fireEvent.click(screen.getByRole('button', { name: 'Se connecter' })) - - expect(login).not.toHaveBeenCalled() + expect(screen.getByRole('button', { name: 'Se connecter' })).toBeInTheDocument() }) })