Merge branch 'main' of ssh://10.55.5.123:222/misha/eatvio_app into issue-911

This commit is contained in:
misha
2026-02-27 13:14:47 +03:00
10 changed files with 137 additions and 11 deletions

View File

@@ -1,3 +1,5 @@
import 'dart:io';
import 'package:eatvio_app/core/app_config.dart';
import 'package:eatvio_app/core/app_initializer.dart';
import 'package:eatvio_app/core/locator_service.dart';
@@ -13,7 +15,6 @@ import 'package:flutter_image_compress/flutter_image_compress.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'create_diet_cubit_test.mocks.dart';
@GenerateNiceMocks([MockSpec<CreateDietRepository>(), MockSpec<ProfileRepository>(), MockSpec<GlossaryRepository>()])
@@ -316,6 +317,88 @@ void main() {
expect(result, 'error');
});
test('updateDiet returns error and resets isDietBeingEdited when repository throws', () async {
cubit.setName('Test Diet');
cubit.setDetails('Test Description');
cubit.setDuration(0);
cubit.setDietState(DietState.active);
const portion = DailySchedule(
name: 'Apple',
portion: Portion.grams,
quantity: 100,
dailyNutrition: DailyNutrition(energy: 52.0, proteins: 0.3, fats: 0.2, carbohydrates: 14.0),
productNutritionalValueId: 1,
);
cubit.addPortion(dayIndex: 0, mealIndex: 0, portion: portion);
// No image so we reach repository.updateDiet without compression
when(dietRepository.updateDiet(
dietId: 1,
state: anyNamed('state'),
name: anyNamed('name'),
duration: anyNamed('duration'),
meatType: anyNamed('meatType'),
carbType: anyNamed('carbType'),
vegType: anyNamed('vegType'),
lightType: anyNamed('lightType'),
details: anyNamed('details'),
image: anyNamed('image'),
dailySchedule: anyNamed('dailySchedule'),
)).thenThrow(Exception('network error'));
final result = await cubit.updateDiet(1);
expect(result, 'error');
expect(cubit.state.isDietBeingEdited, false);
});
test('createDiet returns error when repository createDiet throws', () async {
cubit.setName('Test Diet');
cubit.setDetails('Test Description');
cubit.setDuration(0);
cubit.setDietState(DietState.active);
const portion = DailySchedule(
name: 'Apple',
portion: Portion.grams,
quantity: 100,
dailyNutrition: DailyNutrition(energy: 52.0, proteins: 0.3, fats: 0.2, carbohydrates: 14.0),
productNutritionalValueId: 1,
);
for (var day = 0; day < 7; day++) {
cubit.addPortion(dayIndex: day, mealIndex: 0, portion: portion);
}
final tempDir = Directory.systemTemp;
final tempFile = File('${tempDir.path}/create_diet_test_image_${DateTime.now().millisecondsSinceEpoch}.png');
final pngBytes = [
0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A,
0x00, 0x00, 0x00, 0x0D, 0x49, 0x48, 0x44, 0x52,
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
0x08, 0x02, 0x00, 0x00, 0x00, 0x90, 0x77, 0x53,
0xDE, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4E,
0x44, 0xAE, 0x42, 0x60, 0x82,
];
await tempFile.writeAsBytes(pngBytes);
cubit.setImage(XFile(tempFile.path));
when(dietRepository.createDiet(
name: anyNamed('name'),
duration: anyNamed('duration'),
state: anyNamed('state'),
meatType: anyNamed('meatType'),
carbType: anyNamed('carbType'),
vegType: anyNamed('vegType'),
lightType: anyNamed('lightType'),
details: anyNamed('details'),
image: anyNamed('image'),
dailySchedule: anyNamed('dailySchedule'),
)).thenThrow(Exception('server error'));
final result = await cubit.createDiet();
expect(result, 'error');
expect(cubit.state.isDietPublishing, false);
if (await tempFile.exists()) await tempFile.delete();
});
test('createDiet validates name length', () async {
cubit.setName('Te'); // Less than 3 characters
cubit.setDetails('Test Description');

View File

@@ -139,5 +139,34 @@ void main() {
expect(find.text('10'), findsOneWidget); // totalLikes
});
});
testWidgets('renders diet preview with isImageInBase64 false (URL image)', (tester) async {
await mockNetworkImagesFor(() async {
const testDiet = Diet(
id: 1,
slug: 'test-diet',
name: 'Test Diet URL Image',
image: 'https://example.com/diet-cover.jpg',
duration: 7,
state: DietState.active,
type: DietType(veg: true, light: false, carb: false, meat: false),
details: 'Test diet details',
dailySchedule: {},
author: Author(authorSlug: 'test-author', authorName: 'Test Author', authorProfileIcon: 'https://example.com/photo.jpg', authorIsMe: false),
totalLikes: 10,
totalViews: 100,
totalComments: 5,
wasLikedByMe: false,
isFavorite: false,
dailyNutrition: [],
);
await tester.pumpWidget(wrap(DietPreviewScreen(diet: testDiet, isImageInBase64: false)));
await tester.pumpAndSettle();
expect(find.byType(DietPreviewScreen), findsOneWidget);
expect(find.text('Test Diet URL Image'), findsOneWidget);
});
});
});
}

View File

@@ -269,11 +269,11 @@ void main() {
when(mockCreateDietRepository.updateDiet(
dietId: 1,
state: DietState.active,
)).thenAnswer((_) async => 200);
)).thenAnswer((_) async => 201);
final result = await myDietsCubit!.publishDiet(1);
expect(result, 200);
expect(result, 201);
verify(mockCreateDietRepository.updateDiet(dietId: 1, state: DietState.active)).called(1);
});