forked from eatvio/eatvio_app
155 lines
5.3 KiB
Dart
155 lines
5.3 KiB
Dart
import 'dart:convert';
|
||
import 'dart:io';
|
||
|
||
import 'package:eatvio_app/features/insights/models/insights_response_model.dart';
|
||
import 'package:flutter_test/flutter_test.dart';
|
||
|
||
Map<String, dynamic> _fixture(String name) =>
|
||
jsonDecode(File('test/fixtures/insights/$name').readAsStringSync())
|
||
as Map<String, dynamic>;
|
||
|
||
/// Контракт `GET /api/profile/insights*` (eatvio/eatvio#3017).
|
||
void main() {
|
||
group('InsightsResponseModel', () {
|
||
test('полный ответ: data с weight/bmi/measure/daily/intra', () {
|
||
final data = InsightsResponseModel.fromJson(
|
||
_fixture('insights_response.json'),
|
||
).data;
|
||
|
||
expect(data.weight.headline, '73.2');
|
||
expect(data.measure.waist, [82, 80.5]);
|
||
expect(data.daily!.kcal, hasLength(3));
|
||
expect(data.intra!.bars, hasLength(2));
|
||
});
|
||
|
||
test('daily.percent — изменение за период, а не процент от нормы', () {
|
||
final daily = InsightsResponseModel.fromJson(
|
||
_fixture('insights_response.json'),
|
||
).data.daily!;
|
||
|
||
expect(daily.percent, 5);
|
||
expect(daily.deltaText, 'Калораж вырос на 5% за период');
|
||
});
|
||
|
||
test('нет КБЖУ за период: daily == null', () {
|
||
final data = InsightsResponseModel.fromJson(
|
||
_fixture('insights_response_empty.json'),
|
||
).data;
|
||
|
||
expect(data.daily, isNull);
|
||
});
|
||
|
||
test('пустые замеры: headline "—" и текст «Недостаточно данных»', () {
|
||
final measure = InsightsResponseModel.fromJson(
|
||
_fixture('insights_response_empty.json'),
|
||
).data.measure;
|
||
|
||
expect(measure.labels, isEmpty);
|
||
expect(measure.headline, '—');
|
||
expect(measure.deltaText, 'Недостаточно данных');
|
||
});
|
||
|
||
test('пустой день: intra приходит объектом с датами, без сна', () {
|
||
final intra = InsightsResponseModel.fromJson(
|
||
_fixture('insights_response_empty.json'),
|
||
).data.intra!;
|
||
|
||
expect(intra.bars, isEmpty);
|
||
expect(intra.satiety, isEmpty);
|
||
expect(intra.mood, isEmpty);
|
||
expect(intra.wake, isNull);
|
||
expect(intra.sleep, isNull);
|
||
expect(intra.date, '2026-11-09');
|
||
expect(intra.maxDate, '2026-11-09');
|
||
expect(intra.isToday, isTrue);
|
||
});
|
||
|
||
test(
|
||
'сокращённый пример из описания бэка разбирается без подписей сна',
|
||
() {
|
||
final data = InsightsResponseModel.fromJson(
|
||
jsonDecode(
|
||
jsonEncode({
|
||
'data': {
|
||
'weight': {
|
||
'labels': ['30.10', '07.11'],
|
||
'values': [80, 78],
|
||
'last': 78,
|
||
'delta': -2,
|
||
'headline': '78',
|
||
'unit': 'кг',
|
||
'deltaText': 'Снизили на 2 кг за 3 месяца',
|
||
},
|
||
'bmi': {},
|
||
'measure': {
|
||
'labels': [],
|
||
'waist': [],
|
||
'chest': [],
|
||
'hips': [],
|
||
'headline': '—',
|
||
'deltaText': 'Недостаточно данных',
|
||
},
|
||
'daily': {
|
||
'labels': ['09.11'],
|
||
'kcal': [250],
|
||
'proteins': [10],
|
||
'fats': [5],
|
||
'carbs': [30],
|
||
'average': 250,
|
||
'percent': 0,
|
||
'headline': '250',
|
||
'deltaText': 'Калораж не изменился за период',
|
||
},
|
||
'intra': {
|
||
'bars': [
|
||
{'x': 12.5, 'p': 40, 'f': 45, 'c': 120},
|
||
],
|
||
'satiety': [
|
||
{'x': 12.5, 'y': 4},
|
||
],
|
||
'mood': [
|
||
{'x': 12.5, 'y': 3},
|
||
],
|
||
'wake': 7,
|
||
'sleep': 23,
|
||
'date': '2026-11-09',
|
||
'maxDate': '2026-11-09',
|
||
'dateLabel': 'Сегодня',
|
||
'isToday': true,
|
||
},
|
||
},
|
||
}),
|
||
)
|
||
as Map<String, dynamic>,
|
||
).data;
|
||
|
||
expect(data.weight.values, [80, 78]);
|
||
expect(data.daily!.percent, 0);
|
||
expect(data.intra!.wake, 7);
|
||
expect(data.intra!.wakeLabel, isNull);
|
||
expect(data.intra!.wakeText, isNull);
|
||
},
|
||
);
|
||
});
|
||
|
||
group('InsightsIntraResponseModel', () {
|
||
test('ответ /insights/intra: data.intra', () {
|
||
final intra = InsightsIntraResponseModel.fromJson(
|
||
_fixture('insights_intra_response.json'),
|
||
).data.intra;
|
||
|
||
expect(intra.date, '2026-11-09');
|
||
expect(intra.bars, isNotEmpty);
|
||
});
|
||
|
||
test('прошедший день: isToday == false', () {
|
||
final intra = InsightsIntraResponseModel.fromJson(
|
||
_fixture('insights_intra_response_yesterday.json'),
|
||
).data.intra;
|
||
|
||
expect(intra.isToday, isFalse);
|
||
expect(intra.dateLabel, '8 ноября');
|
||
});
|
||
});
|
||
}
|