mirror of
https://github.com/bleackstorm/satiea.git
synced 2026-12-03 07:41:56 +01:00
feat: support meal photo URIs and improve UI events
- Add `photoUris` field to `MealEntry` and update Firestore rules. - Implement `UiEvent` handling in `MainActivity` for navigation/links. - Remove sensitive email data from error logs and clean up manifest.
This commit is contained in:
1 parent
f0487453c9
commit
87bbb448c2
34 files changed
+2026
-1170
No files matched your search
@@ -6,10 +6,11 @@ import com.example.data.model.MealFoodItem
|
||||
import com.example.data.model.MealType
|
||||
import com.example.util.CsvImporter
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
import java.io.ByteArrayInputStream
|
||||
import java.text.SimpleDateFormat
|
||||
import java.time.LocalDateTime
|
||||
import java.time.ZoneId
|
||||
import java.time.format.DateTimeFormatter
|
||||
import java.util.Locale
|
||||
|
||||
class CsvImporterTest {
|
||||
@@ -57,8 +58,11 @@ class CsvImporterTest {
|
||||
|
||||
@Test
|
||||
fun testDeduplicationIgnoresExistingMeal() {
|
||||
val dateFormat = SimpleDateFormat("dd/MM/yyyy HH:mm", Locale.FRENCH)
|
||||
val timestamp = dateFormat.parse("08/11/2026 12:30")!!.time
|
||||
val formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm", Locale.FRENCH)
|
||||
val timestamp = LocalDateTime.parse("08/11/2026 12:30", formatter)
|
||||
.atZone(ZoneId.systemDefault())
|
||||
.toInstant()
|
||||
.toEpochMilli()
|
||||
|
||||
val existingMeal = MealEntry(
|
||||
id = "existing_1",
|
||||
|
||||
@@ -8,32 +8,35 @@ import com.example.util.GoogleSheetsDataFormatter
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
import java.util.Calendar
|
||||
import java.time.LocalDateTime
|
||||
import java.time.ZoneId
|
||||
|
||||
class GoogleSheetsDataFormatterTest {
|
||||
|
||||
private val testZoneId = ZoneId.of("Europe/Paris")
|
||||
|
||||
@Test
|
||||
fun `formatMealsToRows with empty list returns only header row`() {
|
||||
val rows = GoogleSheetsDataFormatter.formatMealsToRows(emptyList())
|
||||
val rows = GoogleSheetsDataFormatter.formatMealsToRows(emptyList(), testZoneId)
|
||||
assertEquals(1, rows.size)
|
||||
assertEquals(GoogleSheetsDataFormatter.HEADERS, rows[0])
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `formatMealsToRows formats meal fields accurately and sorts chronologically descending`() {
|
||||
val cal1 = Calendar.getInstance().apply {
|
||||
set(2026, Calendar.MARCH, 15, 12, 30, 0)
|
||||
set(Calendar.MILLISECOND, 0)
|
||||
}
|
||||
val cal2 = Calendar.getInstance().apply {
|
||||
set(2026, Calendar.MARCH, 15, 20, 0, 0)
|
||||
set(Calendar.MILLISECOND, 0)
|
||||
}
|
||||
val time1 = LocalDateTime.of(2026, 3, 15, 12, 30, 0)
|
||||
.atZone(testZoneId)
|
||||
.toInstant()
|
||||
.toEpochMilli()
|
||||
val time2 = LocalDateTime.of(2026, 3, 15, 20, 0, 0)
|
||||
.atZone(testZoneId)
|
||||
.toInstant()
|
||||
.toEpochMilli()
|
||||
|
||||
val meal1 = MealEntry(
|
||||
id = "meal-1",
|
||||
mealType = MealType.LUNCH.name,
|
||||
mealTimestamp = cal1.timeInMillis,
|
||||
mealTimestamp = time1,
|
||||
hungerBefore = 3,
|
||||
satietyAfter = 7,
|
||||
note = "Repas calme et savoureux",
|
||||
@@ -41,24 +44,24 @@ class GoogleSheetsDataFormatterTest {
|
||||
MealFoodItem("Riz complet", 150.0, FoodUnit.G).serialize(),
|
||||
MealFoodItem("Saumon", 120.0, FoodUnit.G).serialize()
|
||||
),
|
||||
photosBase64 = listOf("photo1")
|
||||
photoUris = listOf("file:///photo1.jpg")
|
||||
)
|
||||
|
||||
val meal2 = MealEntry(
|
||||
id = "meal-2",
|
||||
mealType = MealType.DINNER.name,
|
||||
mealTimestamp = cal2.timeInMillis,
|
||||
mealTimestamp = time2,
|
||||
hungerBefore = 4,
|
||||
satietyAfter = 8,
|
||||
note = "Dîner léger en famille",
|
||||
foodItemsSerialized = listOf(
|
||||
MealFoodItem("Soupe de légumes", 250.0, FoodUnit.ML).serialize()
|
||||
),
|
||||
photosBase64 = emptyList()
|
||||
photoUris = emptyList()
|
||||
)
|
||||
|
||||
// Pass in reverse order to test sorting
|
||||
val rows = GoogleSheetsDataFormatter.formatMealsToRows(listOf(meal1, meal2))
|
||||
val rows = GoogleSheetsDataFormatter.formatMealsToRows(listOf(meal1, meal2), testZoneId)
|
||||
|
||||
// 1 header + 2 data rows
|
||||
assertEquals(3, rows.size)
|
||||
|
||||
@@ -12,7 +12,7 @@ import com.example.data.model.MealEntry
|
||||
import com.example.data.model.MealFoodItem
|
||||
import com.example.data.model.MealType
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import org.junit.After
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertNotNull
|
||||
@@ -45,7 +45,7 @@ class LocalDatabaseTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testInsertAndObserveMeal() = runBlocking {
|
||||
fun testInsertAndObserveMeal() = runTest {
|
||||
val meal = MealEntry(
|
||||
id = "local_test_1",
|
||||
mealType = MealType.LUNCH.name,
|
||||
@@ -55,9 +55,9 @@ class LocalDatabaseTest {
|
||||
note = "Repas calme",
|
||||
foodItemsSerialized = listOf(
|
||||
MealFoodItem("Riz", 150.0, FoodUnit.G).serialize()
|
||||
)
|
||||
),
|
||||
photoUris = listOf("file:///test_photo.jpg")
|
||||
)
|
||||
|
||||
val entity = LocalMealEntity.fromMealEntry(meal)
|
||||
dao.insertMeal(entity)
|
||||
|
||||
@@ -71,10 +71,11 @@ class LocalDatabaseTest {
|
||||
assertEquals("Repas calme", readMeal.note)
|
||||
assertEquals(1, readMeal.parsedFoodItems().size)
|
||||
assertEquals("Riz", readMeal.parsedFoodItems()[0].name)
|
||||
assertEquals(1, readMeal.displayPhotoUris.size)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testDeleteMeal() = runBlocking {
|
||||
fun testDeleteMeal() = runTest {
|
||||
val entity = LocalMealEntity(
|
||||
id = "to_delete",
|
||||
mealType = MealType.DINNER.name,
|
||||
@@ -90,7 +91,7 @@ class LocalDatabaseTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testInsertAndObserveCustomFoods() = runBlocking {
|
||||
fun testInsertAndObserveCustomFoods() = runTest {
|
||||
val food1 = LocalCustomFoodEntity.create("Granola maison", FoodUnit.G)
|
||||
val food2 = LocalCustomFoodEntity.create("Smoothie mangue", FoodUnit.ML)
|
||||
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
package com.example
|
||||
|
||||
import com.example.data.model.MealEntry
|
||||
import com.example.data.model.MealType
|
||||
import com.example.domain.WeeklyStatsCalculator
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Test
|
||||
import java.time.LocalDate
|
||||
import java.time.ZoneId
|
||||
|
||||
class MealWeeklyStatsTest {
|
||||
|
||||
private val testZoneId = ZoneId.of("Europe/Paris")
|
||||
|
||||
@Test
|
||||
fun `computeWeeklyStats returns 7 daily slots and correct distribution`() {
|
||||
val today = LocalDate.now(testZoneId)
|
||||
val todayMillis = today.atTime(12, 30).atZone(testZoneId).toInstant().toEpochMilli()
|
||||
val twoDaysAgoMillis = today.minusDays(2).atTime(20, 0).atZone(testZoneId).toInstant().toEpochMilli()
|
||||
|
||||
val meals = listOf(
|
||||
MealEntry(
|
||||
id = "m1",
|
||||
mealType = MealType.LUNCH.name,
|
||||
mealTimestamp = todayMillis,
|
||||
hungerBefore = 5,
|
||||
satietyAfter = 7
|
||||
),
|
||||
MealEntry(
|
||||
id = "m2",
|
||||
mealType = MealType.DINNER.name,
|
||||
mealTimestamp = twoDaysAgoMillis,
|
||||
hungerBefore = 3,
|
||||
satietyAfter = 6
|
||||
)
|
||||
)
|
||||
|
||||
val stats = WeeklyStatsCalculator.computeWeeklyStats(meals, testZoneId)
|
||||
|
||||
assertEquals(7, stats.dailyStats.size)
|
||||
assertEquals(2, stats.totalMealsLast7Days)
|
||||
assertEquals(6.5f, stats.averageSatiety, 0.01f)
|
||||
assertEquals(4.0f, stats.averageHunger, 0.01f)
|
||||
assertEquals(100, stats.optimalZonePercentage) // Both 7 and 6 are in 6..8
|
||||
assertEquals(1, stats.mealTypeDistribution[MealType.LUNCH])
|
||||
assertEquals(1, stats.mealTypeDistribution[MealType.DINNER])
|
||||
}
|
||||
}
|
||||
@@ -22,7 +22,7 @@ import org.robolectric.annotation.Config
|
||||
import org.robolectric.annotation.LooperMode
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(manifest = Config.NONE)
|
||||
@Config(manifest = Config.NONE, sdk = [34])
|
||||
@LooperMode(LooperMode.Mode.INSTRUMENTATION_TEST)
|
||||
abstract class FirestoreEmulatorTestBase {
|
||||
|
||||
@@ -37,7 +37,7 @@ abstract class FirestoreEmulatorTestBase {
|
||||
databaseId = if (resId != 0) context.getString(resId) else "demo-database"
|
||||
|
||||
val app = if (FirebaseApp.getApps(context).isEmpty()) {
|
||||
val newApp = FirebaseApp.initializeApp(
|
||||
FirebaseApp.initializeApp(
|
||||
context,
|
||||
FirebaseOptions.Builder()
|
||||
.setApplicationId("com.example.app")
|
||||
@@ -45,21 +45,22 @@ abstract class FirestoreEmulatorTestBase {
|
||||
.setApiKey("fake-api-key-for-emulator")
|
||||
.build()
|
||||
)
|
||||
val newFirestore = FirebaseFirestore.getInstance(newApp, databaseId)
|
||||
newFirestore.useEmulator(EMULATOR_HOST, FIRESTORE_PORT)
|
||||
newFirestore.firestoreSettings = FirebaseFirestoreSettings.Builder()
|
||||
.setLocalCacheSettings(MemoryCacheSettings.newBuilder().build())
|
||||
.build()
|
||||
|
||||
val newAuth = FirebaseAuth.getInstance(newApp)
|
||||
newAuth.useEmulator(EMULATOR_HOST, AUTH_PORT)
|
||||
newApp
|
||||
} else {
|
||||
FirebaseApp.getInstance()
|
||||
}
|
||||
|
||||
firestore = FirebaseFirestore.getInstance(app, databaseId)
|
||||
try {
|
||||
firestore.useEmulator(EMULATOR_HOST, FIRESTORE_PORT)
|
||||
firestore.firestoreSettings = FirebaseFirestoreSettings.Builder()
|
||||
.setLocalCacheSettings(MemoryCacheSettings.newBuilder().build())
|
||||
.build()
|
||||
} catch (_: IllegalStateException) {}
|
||||
|
||||
auth = FirebaseAuth.getInstance(app)
|
||||
try {
|
||||
auth.useEmulator(EMULATOR_HOST, AUTH_PORT)
|
||||
} catch (_: IllegalStateException) {}
|
||||
}
|
||||
|
||||
@After
|
||||
|
||||
Reference in new issue
Block a user