refactor: Clean up code by removing unnecessary whitespace and improving readability

This commit is contained in:
Van Leemput Dayron
2025-11-05 07:55:05 +01:00
parent 9cb21c3470
commit 30dca05e15
8 changed files with 605 additions and 446 deletions

View File

@@ -4,43 +4,44 @@ void main() {
group('Image Loading Logic Tests', () {
test('should demonstrate the new flow without duplicate downloads', () {
// Simulation du nouveau flux de chargement d'images
// Scénario 1: Premier chargement (aucune image existante)
print('=== Scénario 1: Premier chargement ===');
String? existingImage = null; // Aucune image dans le Storage
String? existingImage; // Aucune image dans le Storage
if (existingImage == null) {
print('✓ Aucune image existante trouvée');
print('✓ Téléchargement d\'une nouvelle image depuis Google Places');
existingImage = 'https://storage.googleapis.com/image1.jpg';
print('✓ Image sauvée: $existingImage');
}
expect(existingImage, isNotNull);
// Scénario 2: Rechargement (image existante)
print('\n=== Scénario 2: Rechargement avec image existante ===');
String? cachedImage = existingImage; // Image déjà dans le Storage
print('✓ Image existante trouvée: $cachedImage');
print('✓ PAS de nouveau téléchargement');
print('✓ Réutilisation de l\'image existante');
expect(cachedImage, equals(existingImage));
// Scénario 3: Différente destination
print('\n=== Scénario 3: Destination différente ===');
String? differentLocationImage = null; // Pas d'image pour cette nouvelle destination
String?
differentLocationImage; // Pas d'image pour cette nouvelle destination
if (differentLocationImage == null) {
print('✓ Nouvelle destination, aucune image existante');
print('✓ Téléchargement autorisé pour cette nouvelle destination');
differentLocationImage = 'https://storage.googleapis.com/image2.jpg';
}
expect(differentLocationImage, isNotNull);
expect(differentLocationImage, isNot(equals(existingImage)));
print('\n=== Résumé ===');
print('• Image pour destination 1: $existingImage');
print('• Image pour destination 2: $differentLocationImage');
@@ -52,9 +53,15 @@ void main() {
final testCases = [
{'input': 'Paris, France', 'expected': 'paris_france'},
{'input': 'New York City', 'expected': 'new_york_city'},
{'input': 'São Paulo', 'expected': 's_o_paulo'}, // Caractères spéciaux remplacés
{
'input': 'São Paulo',
'expected': 's_o_paulo',
}, // Caractères spéciaux remplacés
{'input': 'Londres, Royaume-Uni', 'expected': 'londres_royaume_uni'},
{'input': 'Tokyo (東京)', 'expected': 'tokyo'}, // Caractères non-latins supprimés
{
'input': 'Tokyo (東京)',
'expected': 'tokyo',
}, // Caractères non-latins supprimés
];
for (final testCase in testCases) {
@@ -66,28 +73,28 @@ void main() {
test('should demonstrate memory and performance benefits', () {
// Simulation des bénéfices de performance
final oldSystem = {
'apiCalls': 4, // 4 appels à chaque chargement
'storageWrites': 4, // 4 écritures dans le Storage
'storageReads': 0, // Pas de vérification existante
'dataUsage': '4.8 MB', // 4 images × 1.2 MB chacune
'apiCalls': 4, // 4 appels à chaque chargement
'storageWrites': 4, // 4 écritures dans le Storage
'storageReads': 0, // Pas de vérification existante
'dataUsage': '4.8 MB', // 4 images × 1.2 MB chacune
};
final newSystem = {
'apiCalls': 2, // Seulement pour les nouvelles destinations
'storageWrites': 2, // Seulement pour les nouvelles images
'storageReads': 2, // Vérifications d'existence
'dataUsage': '2.4 MB', // Seulement 2 images nécessaires
'apiCalls': 2, // Seulement pour les nouvelles destinations
'storageWrites': 2, // Seulement pour les nouvelles images
'storageReads': 2, // Vérifications d'existence
'dataUsage': '2.4 MB', // Seulement 2 images nécessaires
};
print('=== Comparaison de performance ===');
print('Ancien système:');
oldSystem.forEach((key, value) => print(' $key: $value'));
print('\nNouveau système:');
newSystem.forEach((key, value) => print(' $key: $value'));
print('\nAméliorations:');
print(' • API calls: -50%');
print(' • Storage writes: -50%');
@@ -113,4 +120,4 @@ String _normalizeLocationName(String location) {
.replaceAll(RegExp(r'[^a-z0-9]'), '_')
.replaceAll(RegExp(r'_+'), '_')
.replaceAll(RegExp(r'^_|_$'), '');
}
}

View File

@@ -11,42 +11,47 @@ void main() {
{'width': 1200, 'height': 800, 'photo_reference': 'horizontal_hd'},
{'width': 600, 'height': 400, 'photo_reference': 'horizontal2'},
];
// Appliquer l'algorithme de tri de qualité
photos.sort((a, b) => _sortPhotosByQuality(a, b));
// Vérifications
expect(photos.first['photo_reference'], 'horizontal_hd');
expect(photos.first['width'], 1200);
expect(photos.first['height'], 800);
// La photo verticale devrait être parmi les dernières
final lastPhotos = photos.sublist(photos.length - 2);
expect(lastPhotos.any((p) => p['photo_reference'] == 'vertical1'), true);
print('Photos triées par qualité (meilleure en premier):');
for (var photo in photos) {
final width = photo['width'] as int;
final height = photo['height'] as int;
final ratio = width / height;
final resolution = width * height;
print('${photo['photo_reference']}: ${width}x${height} '
'(ratio: ${ratio.toStringAsFixed(2)}, résolution: $resolution)');
print(
'${photo['photo_reference']}: ${width}x$height '
'(ratio: ${ratio.toStringAsFixed(2)}, résolution: $resolution)',
);
}
});
test('should generate correct search terms for Paris', () {
final searchTerms = _generateSearchTerms('Paris');
// Vérifier que les termes spécifiques à Paris sont présents
expect(searchTerms.any((term) => term.contains('Tour Eiffel')), true);
expect(searchTerms.any((term) => term.contains('Arc de Triomphe')), true);
expect(searchTerms.any((term) => term.contains('Notre-Dame')), true);
// Vérifier que les termes génériques sont aussi présents
expect(searchTerms.any((term) => term.contains('attractions touristiques')), true);
expect(
searchTerms.any((term) => term.contains('attractions touristiques')),
true,
);
expect(searchTerms.any((term) => term.contains('landmarks')), true);
print('Termes de recherche pour Paris:');
for (var term in searchTerms) {
print('- $term');
@@ -55,12 +60,12 @@ void main() {
test('should generate correct search terms for London', () {
final searchTerms = _generateSearchTerms('London');
// Vérifier que les termes spécifiques à Londres sont présents
expect(searchTerms.any((term) => term.contains('Big Ben')), true);
expect(searchTerms.any((term) => term.contains('Tower Bridge')), true);
expect(searchTerms.any((term) => term.contains('London Eye')), true);
print('Termes de recherche pour Londres:');
for (var term in searchTerms) {
print('- $term');
@@ -69,15 +74,18 @@ void main() {
test('should handle unknown cities gracefully', () {
final searchTerms = _generateSearchTerms('Petite Ville Inconnue');
// Devrait au moins avoir des termes génériques
expect(searchTerms.isNotEmpty, true);
expect(searchTerms.any((term) => term.contains('attractions touristiques')), true);
expect(
searchTerms.any((term) => term.contains('attractions touristiques')),
true,
);
expect(searchTerms.any((term) => term.contains('landmarks')), true);
// Le terme original devrait être en dernier
expect(searchTerms.last, 'Petite Ville Inconnue');
print('Termes de recherche pour ville inconnue:');
for (var term in searchTerms) {
print('- $term');
@@ -92,39 +100,39 @@ int _sortPhotosByQuality(Map<String, dynamic> a, Map<String, dynamic> b) {
final aHeight = a['height'] as int;
final bWidth = b['width'] as int;
final bHeight = b['height'] as int;
final aRatio = aWidth / aHeight;
final bRatio = bWidth / bHeight;
// 1. Privilégier les photos horizontales (ratio > 1)
if (aRatio > 1 && bRatio <= 1) return -1;
if (bRatio > 1 && aRatio <= 1) return 1;
// 2. Si les deux sont horizontales ou les deux ne le sont pas,
// privilégier la résolution plus élevée
final aResolution = aWidth * aHeight;
final bResolution = bWidth * bHeight;
if (aResolution != bResolution) {
return bResolution.compareTo(aResolution);
}
// 3. En cas d'égalité de résolution, privilégier le meilleur ratio (plus proche de 1.5)
final idealRatio = 1.5;
final aDiff = (aRatio - idealRatio).abs();
final bDiff = (bRatio - idealRatio).abs();
return aDiff.compareTo(bDiff);
}
/// Reproduit l'algorithme de génération de termes de recherche
List<String> _generateSearchTerms(String location) {
final terms = <String>[];
// Ajouter des termes spécifiques pour les villes connues
final citySpecificTerms = _getCitySpecificTerms(location.toLowerCase());
terms.addAll(citySpecificTerms);
// Termes génériques avec attractions
terms.addAll([
'$location attractions touristiques monuments',
@@ -136,14 +144,14 @@ List<String> _generateSearchTerms(String location) {
'$location skyline',
location, // Terme original en dernier
]);
return terms;
}
/// Reproduit les termes spécifiques pour des villes connues
List<String> _getCitySpecificTerms(String location) {
final specific = <String>[];
if (location.contains('paris')) {
specific.addAll([
'Tour Eiffel Paris',
@@ -182,6 +190,6 @@ List<String> _getCitySpecificTerms(String location) {
'Tokyo Skytree',
]);
}
return specific;
}
}