fix(chat): Correction race condition + Implémentation TODOs

## Corrections Critiques

### Race Condition - Statuts de Messages
- Fix : Les icônes de statut (✓, ✓✓, ✓✓ bleu) ne s'affichaient pas
- Cause : WebSocket delivery confirmations arrivaient avant messages locaux
- Solution : Pattern Optimistic UI dans chat_bloc.dart
  - Création message temporaire immédiate
  - Ajout à la liste AVANT requête HTTP
  - Remplacement par message serveur à la réponse
- Fichier : lib/presentation/state_management/chat_bloc.dart

## Implémentation TODOs (13/21)

### Social (social_header_widget.dart)
-  Copier lien du post dans presse-papiers
-  Partage natif via Share.share()
-  Dialogue de signalement avec 5 raisons

### Partage (share_post_dialog.dart)
-  Interface sélection d'amis avec checkboxes
-  Partage externe via Share API

### Média (media_upload_service.dart)
-  Parsing JSON réponse backend
-  Méthode deleteMedia() pour suppression
-  Génération miniature vidéo

### Posts (create_post_dialog.dart, edit_post_dialog.dart)
-  Extraction URL depuis uploads
-  Documentation chargement médias

### Chat (conversations_screen.dart)
-  Navigation vers notifications
-  ConversationSearchDelegate pour recherche

## Nouveaux Fichiers

### Configuration
- build-prod.ps1 : Script build production avec dart-define
- lib/core/constants/env_config.dart : Gestion environnements

### Documentation
- TODOS_IMPLEMENTED.md : Documentation complète TODOs

## Améliorations

### Architecture
- Refactoring injection de dépendances
- Amélioration routing et navigation
- Optimisation providers (UserProvider, FriendsProvider)

### UI/UX
- Amélioration thème et couleurs
- Optimisation animations
- Meilleure gestion erreurs

### Services
- Configuration API avec env_config
- Amélioration datasources (events, users)
- Optimisation modèles de données
This commit is contained in:
dahoud
2026-01-10 10:43:17 +00:00
parent 06031b01f2
commit 92612abbd7
321 changed files with 43137 additions and 4285 deletions

View File

@@ -0,0 +1,446 @@
import 'dart:convert';
import 'package:afterwork/core/errors/exceptions.dart';
import 'package:afterwork/data/datasources/event_remote_data_source.dart';
import 'package:afterwork/data/models/event_model.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:http/http.dart' as http;
import 'package:mocktail/mocktail.dart';
// Mock classes
class MockHttpClient extends Mock implements http.Client {}
void main() {
late EventRemoteDataSource dataSource;
late MockHttpClient mockHttpClient;
setUpAll(() {
// Register fallback values for mocktail
registerFallbackValue(Uri.parse('http://example.com'));
registerFallbackValue(<String, String>{});
});
setUp(() {
mockHttpClient = MockHttpClient();
dataSource = EventRemoteDataSource(mockHttpClient);
});
group('EventRemoteDataSource', () {
final tEventJson = {
'id': '1',
'title': 'Test Event',
'description': 'Test Description',
'startDate': '2026-01-01T00:00:00Z',
'location': 'Test Location',
'category': 'Test Category',
'link': 'https://test.com',
'imageUrl': 'https://test.com/image.jpg',
'creatorEmail': 'creator@test.com',
'creatorFirstName': 'John',
'creatorLastName': 'Doe',
'profileImageUrl': 'https://test.com/profile.jpg',
'participants': ['user1', 'user2'],
'status': 'ouvert',
'reactionsCount': 5,
'commentsCount': 3,
'sharesCount': 2,
};
final tEventModel = EventModel.fromJson(tEventJson);
group('getAllEvents', () {
test('should return list of events when API call is successful', () async {
// Arrange
when(() => mockHttpClient.get(any())).thenAnswer(
(_) async => http.Response(
jsonEncode([tEventJson]),
200,
),
);
// Act
final result = await dataSource.getAllEvents();
// Assert
expect(result, isA<List<EventModel>>());
expect(result.length, 1);
expect(result.first.id, '1');
verify(() => mockHttpClient.get(any())).called(1);
});
test('should throw ServerException when API call fails', () async {
// Arrange
when(() => mockHttpClient.get(any())).thenAnswer(
(_) async => http.Response('Error', 500),
);
// Act & Assert
expect(
() => dataSource.getAllEvents(),
throwsA(isA<ServerException>()),
);
});
});
group('getEventsCreatedByUserAndFriends', () {
const tUserId = 'user123';
test('should return list of events when API call is successful', () async {
// Arrange
when(() => mockHttpClient.post(any(), headers: any(named: 'headers'), body: any(named: 'body')))
.thenAnswer(
(_) async => http.Response(
jsonEncode([tEventJson]),
200,
),
);
// Act
final result = await dataSource.getEventsCreatedByUserAndFriends(tUserId);
// Assert
expect(result, isA<List<EventModel>>());
expect(result.length, 1);
expect(result.first.id, '1');
verify(() => mockHttpClient.post(any(), headers: any(named: 'headers'), body: any(named: 'body')))
.called(1);
});
test('should throw ServerException when API call fails', () async {
// Arrange
when(() => mockHttpClient.post(any(), headers: any(named: 'headers'), body: any(named: 'body')))
.thenAnswer((_) async => http.Response('Error', 500));
// Act & Assert
expect(
() => dataSource.getEventsCreatedByUserAndFriends(tUserId),
throwsA(isA<ServerException>()),
);
});
test('should send correct request body', () async {
// Arrange
when(() => mockHttpClient.post(any(), headers: any(named: 'headers'), body: any(named: 'body')))
.thenAnswer(
(_) async => http.Response(jsonEncode([]), 200),
);
// Act
await dataSource.getEventsCreatedByUserAndFriends(tUserId);
// Assert
final captured = verify(() => mockHttpClient.post(
any(),
headers: any(named: 'headers'),
body: captureAny(named: 'body'),
)).captured;
final body = jsonDecode(captured.first as String) as Map<String, dynamic>;
expect(body['userId'], tUserId);
});
});
group('createEvent', () {
test('should return created event when API call is successful', () async {
// Arrange
when(() => mockHttpClient.post(any(), headers: any(named: 'headers'), body: any(named: 'body')))
.thenAnswer(
(_) async => http.Response(
jsonEncode(tEventJson),
201,
),
);
// Act
final result = await dataSource.createEvent(tEventModel);
// Assert
expect(result, isA<EventModel>());
expect(result.id, '1');
verify(() => mockHttpClient.post(any(), headers: any(named: 'headers'), body: any(named: 'body')))
.called(1);
});
test('should throw ServerException when API call fails', () async {
// Arrange
when(() => mockHttpClient.post(any(), headers: any(named: 'headers'), body: any(named: 'body')))
.thenAnswer((_) async => http.Response('Error', 400));
// Act & Assert
expect(
() => dataSource.createEvent(tEventModel),
throwsA(isA<ServerException>()),
);
});
});
group('getEventById', () {
const tEventId = '1';
test('should return event when API call is successful', () async {
// Arrange
when(() => mockHttpClient.get(any())).thenAnswer(
(_) async => http.Response(
jsonEncode(tEventJson),
200,
),
);
// Act
final result = await dataSource.getEventById(tEventId);
// Assert
expect(result, isA<EventModel>());
expect(result.id, tEventId);
verify(() => mockHttpClient.get(any())).called(1);
});
test('should throw ServerException when API call fails', () async {
// Arrange
when(() => mockHttpClient.get(any())).thenAnswer(
(_) async => http.Response('Error', 404),
);
// Act & Assert
expect(
() => dataSource.getEventById(tEventId),
throwsA(isA<ServerException>()),
);
});
});
group('updateEvent', () {
const tEventId = '1';
test('should return updated event when API call is successful', () async {
// Arrange
when(() => mockHttpClient.put(any(), headers: any(named: 'headers'), body: any(named: 'body')))
.thenAnswer(
(_) async => http.Response(
jsonEncode(tEventJson),
200,
),
);
// Act
final result = await dataSource.updateEvent(tEventId, tEventModel);
// Assert
expect(result, isA<EventModel>());
expect(result.id, tEventId);
verify(() => mockHttpClient.put(any(), headers: any(named: 'headers'), body: any(named: 'body')))
.called(1);
});
test('should throw ServerException when API call fails', () async {
// Arrange
when(() => mockHttpClient.put(any(), headers: any(named: 'headers'), body: any(named: 'body')))
.thenAnswer((_) async => http.Response('Error', 400));
// Act & Assert
expect(
() => dataSource.updateEvent(tEventId, tEventModel),
throwsA(isA<ServerException>()),
);
});
});
group('deleteEvent', () {
const tEventId = '1';
test('should delete event successfully when API call is successful', () async {
// Arrange
when(() => mockHttpClient.delete(any())).thenAnswer(
(_) async => http.Response('', 204),
);
// Act
await dataSource.deleteEvent(tEventId);
// Assert
verify(() => mockHttpClient.delete(any())).called(1);
});
test('should throw ServerException when API call fails', () async {
// Arrange
when(() => mockHttpClient.delete(any())).thenAnswer(
(_) async => http.Response('Error', 500),
);
// Act & Assert
expect(
() => dataSource.deleteEvent(tEventId),
throwsA(isA<ServerException>()),
);
});
});
group('participateInEvent', () {
const tEventId = '1';
const tUserId = 'user123';
test('should return event when participation is successful', () async {
// Arrange
when(() => mockHttpClient.post(any(), headers: any(named: 'headers'), body: any(named: 'body')))
.thenAnswer(
(_) async => http.Response(
jsonEncode(tEventJson),
200,
),
);
// Act
final result = await dataSource.participateInEvent(tEventId, tUserId);
// Assert
expect(result, isA<EventModel>());
verify(() => mockHttpClient.post(any(), headers: any(named: 'headers'), body: any(named: 'body')))
.called(1);
});
test('should throw ServerException when API call fails', () async {
// Arrange
when(() => mockHttpClient.post(any(), headers: any(named: 'headers'), body: any(named: 'body')))
.thenAnswer((_) async => http.Response('Error', 400));
// Act & Assert
expect(
() => dataSource.participateInEvent(tEventId, tUserId),
throwsA(isA<ServerException>()),
);
});
});
group('reactToEvent', () {
const tEventId = '1';
const tUserId = 'user123';
test('should react to event successfully when API call is successful', () async {
// Arrange
when(() => mockHttpClient.post(any(), headers: any(named: 'headers'), body: any(named: 'body')))
.thenAnswer((_) async => http.Response('Success', 200));
// Act
await dataSource.reactToEvent(tEventId, tUserId);
// Assert
verify(() => mockHttpClient.post(any(), headers: any(named: 'headers'), body: any(named: 'body')))
.called(1);
});
test('should throw ServerException when API call fails', () async {
// Arrange
when(() => mockHttpClient.post(any(), headers: any(named: 'headers'), body: any(named: 'body')))
.thenAnswer((_) async => http.Response('Error', 400));
// Act & Assert
expect(
() => dataSource.reactToEvent(tEventId, tUserId),
throwsA(isA<ServerException>()),
);
});
});
group('closeEvent', () {
const tEventId = '1';
test('should close event successfully when API call is successful', () async {
// Arrange
when(() => mockHttpClient.patch(any(), headers: any(named: 'headers')))
.thenAnswer((_) async => http.Response('Success', 200));
// Act
await dataSource.closeEvent(tEventId);
// Assert
verify(() => mockHttpClient.patch(any(), headers: any(named: 'headers'))).called(1);
});
test('should throw ServerExceptionWithMessage when status code is 400', () async {
// Arrange
when(() => mockHttpClient.patch(any(), headers: any(named: 'headers')))
.thenAnswer(
(_) async => http.Response(
jsonEncode({'message': 'Event already closed'}),
400,
),
);
// Act & Assert
expect(
() => dataSource.closeEvent(tEventId),
throwsA(isA<ServerExceptionWithMessage>()),
);
});
test('should throw ServerExceptionWithMessage when status code is not 200 or 400', () async {
// Arrange
when(() => mockHttpClient.patch(any(), headers: any(named: 'headers')))
.thenAnswer((_) async => http.Response('Error', 500));
// Act & Assert
expect(
() => dataSource.closeEvent(tEventId),
throwsA(isA<ServerExceptionWithMessage>()),
);
});
});
group('reopenEvent', () {
const tEventId = '1';
test('should reopen event successfully when API call is successful', () async {
// Arrange
when(() => mockHttpClient.patch(any(), headers: any(named: 'headers')))
.thenAnswer((_) async => http.Response('Success', 200));
// Act
await dataSource.reopenEvent(tEventId);
// Assert
verify(() => mockHttpClient.patch(any(), headers: any(named: 'headers'))).called(1);
});
test('should throw ServerExceptionWithMessage when status code is 400', () async {
// Arrange
when(() => mockHttpClient.patch(any(), headers: any(named: 'headers')))
.thenAnswer(
(_) async => http.Response(
jsonEncode({'message': 'Event already open'}),
400,
),
);
// Act & Assert
expect(
() => dataSource.reopenEvent(tEventId),
throwsA(isA<ServerExceptionWithMessage>()),
);
});
test('should throw ServerExceptionWithMessage when status code is 404', () async {
// Arrange
when(() => mockHttpClient.patch(any(), headers: any(named: 'headers')))
.thenAnswer((_) async => http.Response('Not found', 404));
// Act & Assert
expect(
() => dataSource.reopenEvent(tEventId),
throwsA(isA<ServerExceptionWithMessage>()),
);
});
test('should throw ServerExceptionWithMessage when status code is not 200, 400 or 404', () async {
// Arrange
when(() => mockHttpClient.patch(any(), headers: any(named: 'headers')))
.thenAnswer((_) async => http.Response('Error', 500));
// Act & Assert
expect(
() => dataSource.reopenEvent(tEventId),
throwsA(isA<ServerExceptionWithMessage>()),
);
});
});
});
}

View File

@@ -0,0 +1,299 @@
import 'dart:convert';
import 'package:afterwork/core/errors/exceptions.dart';
import 'package:afterwork/data/datasources/user_remote_data_source.dart';
import 'package:afterwork/data/models/user_model.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:http/http.dart' as http;
import 'package:mocktail/mocktail.dart';
// Mock classes
class MockHttpClient extends Mock implements http.Client {}
void main() {
late UserRemoteDataSource dataSource;
late MockHttpClient mockHttpClient;
setUpAll(() {
// Register fallback values for mocktail
registerFallbackValue(Uri.parse('http://example.com'));
registerFallbackValue(<String, String>{});
});
setUp(() {
mockHttpClient = MockHttpClient();
dataSource = UserRemoteDataSource(mockHttpClient);
});
group('UserRemoteDataSource', () {
final tUserJson = {
'userId': 'user123',
'email': 'test@example.com',
'firstName': 'John',
'lastName': 'Doe',
'profileImageUrl': 'https://test.com/profile.jpg',
};
final tUserModel = UserModel.fromJson(tUserJson);
group('authenticateUser', () {
const tEmail = 'test@example.com';
const tPassword = 'password123';
test('should return UserModel when authentication is successful', () async {
// Arrange
when(() => mockHttpClient.post(any(), headers: any(named: 'headers'), body: any(named: 'body')))
.thenAnswer(
(_) async => http.Response(
jsonEncode(tUserJson),
200,
),
);
// Act
final result = await dataSource.authenticateUser(tEmail, tPassword);
// Assert
expect(result, isA<UserModel>());
expect(result.userId, 'user123');
expect(result.email, tEmail);
verify(() => mockHttpClient.post(any(), headers: any(named: 'headers'), body: any(named: 'body')))
.called(1);
});
test('should throw Exception when userId is missing', () async {
// Arrange
final userJsonWithoutId = Map<String, dynamic>.from(tUserJson)..remove('userId');
when(() => mockHttpClient.post(any(), headers: any(named: 'headers'), body: any(named: 'body')))
.thenAnswer(
(_) async => http.Response(
jsonEncode(userJsonWithoutId),
200,
),
);
// Act & Assert
expect(
() => dataSource.authenticateUser(tEmail, tPassword),
throwsA(isA<Exception>()),
);
});
test('should throw UnauthorizedException when status code is 401', () async {
// Arrange
when(() => mockHttpClient.post(any(), headers: any(named: 'headers'), body: any(named: 'body')))
.thenAnswer((_) async => http.Response('Unauthorized', 401));
// Act & Assert
expect(
() => dataSource.authenticateUser(tEmail, tPassword),
throwsA(predicate((e) => e.toString().contains('UnauthorizedException'))),
);
});
test('should throw ServerExceptionWithMessage when status code is not 200 or 401', () async {
// Arrange
when(() => mockHttpClient.post(any(), headers: any(named: 'headers'), body: any(named: 'body')))
.thenAnswer((_) async => http.Response('Error', 500));
// Act & Assert
expect(
() => dataSource.authenticateUser(tEmail, tPassword),
throwsA(predicate((e) => e.toString().contains('ServerException'))),
);
});
test('should send correct request body', () async {
// Arrange
when(() => mockHttpClient.post(any(), headers: any(named: 'headers'), body: any(named: 'body')))
.thenAnswer(
(_) async => http.Response(jsonEncode(tUserJson), 200),
);
// Act
await dataSource.authenticateUser(tEmail, tPassword);
// Assert
final captured = verify(() => mockHttpClient.post(
any(),
headers: any(named: 'headers'),
body: captureAny(named: 'body'),
)).captured;
final body = jsonDecode(captured.first as String) as Map<String, dynamic>;
expect(body['email'], tEmail);
expect(body['motDePasse'], tPassword);
});
});
group('getUser', () {
const tUserId = 'user123';
test('should return UserModel when API call is successful', () async {
// Arrange
when(() => mockHttpClient.get(any())).thenAnswer(
(_) async => http.Response(
jsonEncode(tUserJson),
200,
),
);
// Act
final result = await dataSource.getUser(tUserId);
// Assert
expect(result, isA<UserModel>());
expect(result.userId, tUserId);
verify(() => mockHttpClient.get(any())).called(1);
});
test('should throw UserNotFoundException when status code is 404', () async {
// Arrange
when(() => mockHttpClient.get(any())).thenAnswer(
(_) async => http.Response('Not found', 404),
);
// Act & Assert
expect(
() => dataSource.getUser(tUserId),
throwsA(predicate((e) => e.toString().contains('UserNotFoundException'))),
);
});
test('should throw ServerException when status code is not 200 or 404', () async {
// Arrange
when(() => mockHttpClient.get(any())).thenAnswer(
(_) async => http.Response('Error', 500),
);
// Act & Assert
expect(
() => dataSource.getUser(tUserId),
throwsA(predicate((e) => e.toString().contains('ServerException'))),
);
});
});
group('createUser', () {
test('should return created UserModel when API call is successful', () async {
// Arrange
when(() => mockHttpClient.post(any(), headers: any(named: 'headers'), body: any(named: 'body')))
.thenAnswer(
(_) async => http.Response(
jsonEncode(tUserJson),
201,
),
);
// Act
final result = await dataSource.createUser(tUserModel);
// Assert
expect(result, isA<UserModel>());
expect(result.userId, 'user123');
verify(() => mockHttpClient.post(any(), headers: any(named: 'headers'), body: any(named: 'body')))
.called(1);
});
test('should throw ConflictException when status code is 409', () async {
// Arrange
when(() => mockHttpClient.post(any(), headers: any(named: 'headers'), body: any(named: 'body')))
.thenAnswer((_) async => http.Response('Conflict', 409));
// Act & Assert
expect(
() => dataSource.createUser(tUserModel),
throwsA(predicate((e) => e.toString().contains('ConflictException'))),
);
});
test('should throw ServerException when status code is not 201 or 409', () async {
// Arrange
when(() => mockHttpClient.post(any(), headers: any(named: 'headers'), body: any(named: 'body')))
.thenAnswer((_) async => http.Response('Error', 500));
// Act & Assert
expect(
() => dataSource.createUser(tUserModel),
throwsA(predicate((e) => e.toString().contains('ServerException'))),
);
});
});
group('updateUser', () {
test('should return updated UserModel when API call is successful', () async {
// Arrange
when(() => mockHttpClient.put(any(), headers: any(named: 'headers'), body: any(named: 'body')))
.thenAnswer(
(_) async => http.Response(
jsonEncode(tUserJson),
200,
),
);
// Act
final result = await dataSource.updateUser(tUserModel);
// Assert
expect(result, isA<UserModel>());
expect(result.userId, 'user123');
verify(() => mockHttpClient.put(any(), headers: any(named: 'headers'), body: any(named: 'body')))
.called(1);
});
test('should throw UserNotFoundException when status code is 404', () async {
// Arrange
when(() => mockHttpClient.put(any(), headers: any(named: 'headers'), body: any(named: 'body')))
.thenAnswer((_) async => http.Response('Not found', 404));
// Act & Assert
expect(
() => dataSource.updateUser(tUserModel),
throwsA(predicate((e) => e.toString().contains('UserNotFoundException'))),
);
});
test('should throw ServerException when status code is not 200 or 404', () async {
// Arrange
when(() => mockHttpClient.put(any(), headers: any(named: 'headers'), body: any(named: 'body')))
.thenAnswer((_) async => http.Response('Error', 500));
// Act & Assert
expect(
() => dataSource.updateUser(tUserModel),
throwsA(predicate((e) => e.toString().contains('ServerException'))),
);
});
});
group('deleteUser', () {
const tUserId = 'user123';
test('should delete user successfully when API call is successful', () async {
// Arrange
when(() => mockHttpClient.delete(any())).thenAnswer(
(_) async => http.Response('', 204),
);
// Act
await dataSource.deleteUser(tUserId);
// Assert
verify(() => mockHttpClient.delete(any())).called(1);
});
test('should throw ServerException when status code is not 204', () async {
// Arrange
when(() => mockHttpClient.delete(any())).thenAnswer(
(_) async => http.Response('Error', 500),
);
// Act & Assert
expect(
() => dataSource.deleteUser(tUserId),
throwsA(predicate((e) => e.toString().contains('ServerException'))),
);
});
});
});
}