mirror of
https://github.com/bleackstorm/satiea.git
synced 2026-12-03 07:41:56 +01:00
feat: implement meal notification system
Add necessary permissions, services, and receivers to support scheduled meal notifications and boot completion handling. Also includes dependency updates and minor test fixes.
This commit is contained in:
1 parent
f13d46f778
commit
fe69d7621a
31 files changed
+2972
-350
No files matched your search
@@ -0,0 +1,99 @@
|
||||
package com.example
|
||||
|
||||
import com.example.data.model.FoodUnit
|
||||
import com.example.data.model.MealEntry
|
||||
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.util.Locale
|
||||
|
||||
class CsvImporterTest {
|
||||
|
||||
@Test
|
||||
fun testParseFoodItemsFormatted() {
|
||||
val raw = "Saumon grillé (150 g) ; Riz basmati (180 g) ; Pomme (1 portion)"
|
||||
val parsed = CsvImporter.parseFoodItems(raw)
|
||||
|
||||
assertEquals(3, parsed.size)
|
||||
assertEquals("Saumon grillé", parsed[0].name)
|
||||
assertEquals(150.0, parsed[0].weight ?: 0.0, 0.001)
|
||||
assertEquals(FoodUnit.G, parsed[0].unit)
|
||||
|
||||
assertEquals("Riz basmati", parsed[1].name)
|
||||
assertEquals(180.0, parsed[1].weight ?: 0.0, 0.001)
|
||||
assertEquals(FoodUnit.G, parsed[1].unit)
|
||||
|
||||
assertEquals("Pomme", parsed[2].name)
|
||||
assertEquals(1.0, parsed[2].weight ?: 0.0, 0.001)
|
||||
assertEquals(FoodUnit.PORTION, parsed[2].unit)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testParseCsvStreamStandard() {
|
||||
val csv = """
|
||||
Date,Heure,Type de repas,Faim avant (/10),Ressenti faim,Satiété après (/10),Ressenti satiété,Aliments & Poids,Note globale,Photos jointes
|
||||
08/11/2026,12:30,Déjeuner,6,"Faim présente",7,"Idéale","Riz basmati (150 g) ; Poulet (120 g)","Déjeuner au calme",0
|
||||
08/11/2026,20:00,Dîner,4,"Petite faim",6,"Légère","Soupe de légumes (250 ml)","Repas léger",0
|
||||
""".trimIndent()
|
||||
|
||||
val stream = ByteArrayInputStream(csv.toByteArray(Charsets.UTF_8))
|
||||
val result = CsvImporter.parseCsvStream(stream, emptyList()).getOrThrow()
|
||||
|
||||
assertEquals(2, result.importedMeals.size)
|
||||
assertEquals(0, result.skippedDuplicatesCount)
|
||||
assertEquals(3, result.newFoodItems.size) // Riz basmati, Poulet, Soupe de légumes
|
||||
|
||||
val firstMeal = result.importedMeals[0]
|
||||
assertEquals(MealType.LUNCH.name, firstMeal.mealType)
|
||||
assertEquals(6, firstMeal.hungerBefore)
|
||||
assertEquals(7, firstMeal.satietyAfter)
|
||||
assertEquals(2, firstMeal.parsedFoodItems().size)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testDeduplicationIgnoresExistingMeal() {
|
||||
val dateFormat = SimpleDateFormat("dd/MM/yyyy HH:mm", Locale.FRENCH)
|
||||
val timestamp = dateFormat.parse("08/11/2026 12:30")!!.time
|
||||
|
||||
val existingMeal = MealEntry(
|
||||
id = "existing_1",
|
||||
mealType = MealType.LUNCH.name,
|
||||
mealTimestamp = timestamp
|
||||
)
|
||||
|
||||
val csv = """
|
||||
Date,Heure,Type de repas,Faim avant (/10),Satiété après (/10),Aliments & Poids,Note globale
|
||||
08/11/2026,12:30,Déjeuner,6,7,"Riz basmati (150 g)","Déjeuner",0
|
||||
08/11/2026,20:00,Dîner,4,6,"Soupe (250 ml)","Repas",0
|
||||
""".trimIndent()
|
||||
|
||||
val stream = ByteArrayInputStream(csv.toByteArray(Charsets.UTF_8))
|
||||
val result = CsvImporter.parseCsvStream(stream, listOf(existingMeal)).getOrThrow()
|
||||
|
||||
// 1 duplicate skipped, 1 imported
|
||||
assertEquals(1, result.importedMeals.size)
|
||||
assertEquals(1, result.skippedDuplicatesCount)
|
||||
assertEquals(MealType.DINNER.name, result.importedMeals[0].mealType)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testFrenchSemicolonCsvDelimiter() {
|
||||
val csv = """
|
||||
Date;Heure;Type de repas;Faim avant (/10);Satiété après (/10);Aliments & Poids;Note globale
|
||||
08/11/2026;12:30;Déjeuner;7;8;Pavé de saumon (150 g);Très bon
|
||||
""".trimIndent()
|
||||
|
||||
val stream = ByteArrayInputStream(csv.toByteArray(Charsets.UTF_8))
|
||||
val result = CsvImporter.parseCsvStream(stream, emptyList()).getOrThrow()
|
||||
|
||||
assertEquals(1, result.importedMeals.size)
|
||||
assertEquals("Très bon", result.importedMeals[0].note)
|
||||
assertEquals(1, result.importedMeals[0].parsedFoodItems().size)
|
||||
assertEquals("Pavé de saumon", result.importedMeals[0].parsedFoodItems()[0].name)
|
||||
}
|
||||
}
|
||||
@@ -9,13 +9,13 @@ import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.annotation.Config
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [36])
|
||||
@Config(sdk = [34])
|
||||
class ExampleRobolectricTest {
|
||||
|
||||
@Test
|
||||
fun `read string from context`() {
|
||||
val context = ApplicationProvider.getApplicationContext<Context>()
|
||||
val appName = context.getString(R.string.app_name)
|
||||
assertEquals("My Application", appName)
|
||||
assertEquals("Satiéa", appName)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
package com.example
|
||||
|
||||
import com.example.data.model.MealEntry
|
||||
import com.example.data.model.MealFoodItem
|
||||
import com.example.data.model.MealType
|
||||
import com.example.data.model.FoodUnit
|
||||
import com.example.util.GoogleSheetsDataFormatter
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
import java.util.Calendar
|
||||
|
||||
class GoogleSheetsDataFormatterTest {
|
||||
|
||||
@Test
|
||||
fun `formatMealsToRows with empty list returns only header row`() {
|
||||
val rows = GoogleSheetsDataFormatter.formatMealsToRows(emptyList())
|
||||
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 meal1 = MealEntry(
|
||||
id = "meal-1",
|
||||
mealType = MealType.LUNCH.name,
|
||||
mealTimestamp = cal1.timeInMillis,
|
||||
hungerBefore = 3,
|
||||
satietyAfter = 7,
|
||||
note = "Repas calme et savoureux",
|
||||
foodItemsSerialized = listOf(
|
||||
MealFoodItem("Riz complet", 150.0, FoodUnit.G).serialize(),
|
||||
MealFoodItem("Saumon", 120.0, FoodUnit.G).serialize()
|
||||
),
|
||||
photosBase64 = listOf("photo1")
|
||||
)
|
||||
|
||||
val meal2 = MealEntry(
|
||||
id = "meal-2",
|
||||
mealType = MealType.DINNER.name,
|
||||
mealTimestamp = cal2.timeInMillis,
|
||||
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()
|
||||
)
|
||||
|
||||
// Pass in reverse order to test sorting
|
||||
val rows = GoogleSheetsDataFormatter.formatMealsToRows(listOf(meal1, meal2))
|
||||
|
||||
// 1 header + 2 data rows
|
||||
assertEquals(3, rows.size)
|
||||
|
||||
// Row 0: Headers
|
||||
assertEquals(10, rows[0].size)
|
||||
assertEquals("Date", rows[0][0])
|
||||
assertEquals("Photos jointes", rows[0][9])
|
||||
|
||||
// Row 1: Most recent meal (DINNER at 20:00)
|
||||
val dinnerRow = rows[1]
|
||||
assertEquals("15/03/2026", dinnerRow[0])
|
||||
assertEquals("20:00", dinnerRow[1])
|
||||
assertEquals("Dîner", dinnerRow[2])
|
||||
assertEquals(4, dinnerRow[3])
|
||||
assertEquals(8, dinnerRow[5])
|
||||
assertTrue((dinnerRow[7] as String).contains("Soupe de légumes"))
|
||||
assertEquals("Dîner léger en famille", dinnerRow[8])
|
||||
assertEquals(0, dinnerRow[9])
|
||||
|
||||
// Row 2: Earlier meal (LUNCH at 12:30)
|
||||
val lunchRow = rows[2]
|
||||
assertEquals("15/03/2026", lunchRow[0])
|
||||
assertEquals("12:30", lunchRow[1])
|
||||
assertEquals("Déjeuner", lunchRow[2])
|
||||
assertEquals(3, lunchRow[3])
|
||||
assertEquals(7, lunchRow[5])
|
||||
val lunchFoods = lunchRow[7] as String
|
||||
assertTrue(lunchFoods.contains("Riz complet"))
|
||||
assertTrue(lunchFoods.contains("Saumon"))
|
||||
assertEquals("Repas calme et savoureux", lunchRow[8])
|
||||
assertEquals(1, lunchRow[9])
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
package com.example
|
||||
|
||||
import android.content.Context
|
||||
import androidx.room.Room
|
||||
import androidx.test.core.app.ApplicationProvider
|
||||
import com.example.data.local.AppDatabase
|
||||
import com.example.data.local.LocalCustomFoodEntity
|
||||
import com.example.data.local.LocalMealDao
|
||||
import com.example.data.local.LocalMealEntity
|
||||
import com.example.data.model.FoodUnit
|
||||
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 org.junit.After
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertNotNull
|
||||
import org.junit.Assert.assertNull
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.annotation.Config
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [34])
|
||||
class LocalDatabaseTest {
|
||||
|
||||
private lateinit var db: AppDatabase
|
||||
private lateinit var dao: LocalMealDao
|
||||
|
||||
@Before
|
||||
fun setup() {
|
||||
val context = ApplicationProvider.getApplicationContext<Context>()
|
||||
db = Room.inMemoryDatabaseBuilder(context, AppDatabase::class.java)
|
||||
.allowMainThreadQueries()
|
||||
.build()
|
||||
dao = db.mealDao()
|
||||
}
|
||||
|
||||
@After
|
||||
fun teardown() {
|
||||
db.close()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testInsertAndObserveMeal() = runBlocking {
|
||||
val meal = MealEntry(
|
||||
id = "local_test_1",
|
||||
mealType = MealType.LUNCH.name,
|
||||
mealTimestamp = 1700000000000L,
|
||||
hungerBefore = 6,
|
||||
satietyAfter = 7,
|
||||
note = "Repas calme",
|
||||
foodItemsSerialized = listOf(
|
||||
MealFoodItem("Riz", 150.0, FoodUnit.G).serialize()
|
||||
)
|
||||
)
|
||||
|
||||
val entity = LocalMealEntity.fromMealEntry(meal)
|
||||
dao.insertMeal(entity)
|
||||
|
||||
val allMeals = dao.observeAllMeals().first()
|
||||
assertEquals(1, allMeals.size)
|
||||
val readMeal = allMeals[0].toMealEntry()
|
||||
assertEquals("local_test_1", readMeal.id)
|
||||
assertEquals(MealType.LUNCH.name, readMeal.mealType)
|
||||
assertEquals(6, readMeal.hungerBefore)
|
||||
assertEquals(7, readMeal.satietyAfter)
|
||||
assertEquals("Repas calme", readMeal.note)
|
||||
assertEquals(1, readMeal.parsedFoodItems().size)
|
||||
assertEquals("Riz", readMeal.parsedFoodItems()[0].name)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testDeleteMeal() = runBlocking {
|
||||
val entity = LocalMealEntity(
|
||||
id = "to_delete",
|
||||
mealType = MealType.DINNER.name,
|
||||
mealTimestamp = System.currentTimeMillis(),
|
||||
hungerBefore = 5,
|
||||
satietyAfter = 6
|
||||
)
|
||||
dao.insertMeal(entity)
|
||||
assertNotNull(dao.getMealById("to_delete"))
|
||||
|
||||
dao.deleteMealById("to_delete")
|
||||
assertNull(dao.getMealById("to_delete"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testInsertAndObserveCustomFoods() = runBlocking {
|
||||
val food1 = LocalCustomFoodEntity.create("Granola maison", FoodUnit.G)
|
||||
val food2 = LocalCustomFoodEntity.create("Smoothie mangue", FoodUnit.ML)
|
||||
|
||||
dao.insertCustomFoods(listOf(food1, food2))
|
||||
|
||||
val allFoods = dao.observeAllCustomFoods().first()
|
||||
assertEquals(2, allFoods.size)
|
||||
val names = allFoods.map { it.name }
|
||||
assertEquals(listOf("Granola maison", "Smoothie mangue"), names)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package com.example
|
||||
|
||||
import android.content.Context
|
||||
import androidx.test.core.app.ApplicationProvider
|
||||
import com.example.data.model.MealType
|
||||
import com.example.data.preferences.ReminderPreferencesManager
|
||||
import com.example.notification.MealNotificationManager
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.annotation.Config
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [34])
|
||||
class ReminderPreferencesTest {
|
||||
|
||||
private lateinit var context: Context
|
||||
private lateinit var prefs: ReminderPreferencesManager
|
||||
|
||||
@Before
|
||||
fun setup() {
|
||||
context = ApplicationProvider.getApplicationContext()
|
||||
prefs = ReminderPreferencesManager.getInstance(context)
|
||||
prefs.clearAll()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testDefaultReminderValues() {
|
||||
assertTrue(prefs.isReminderEnabled(MealType.BREAKFAST))
|
||||
assertTrue(prefs.isReminderEnabled(MealType.LUNCH))
|
||||
assertFalse(prefs.isReminderEnabled(MealType.SNACK))
|
||||
assertTrue(prefs.isReminderEnabled(MealType.DINNER))
|
||||
|
||||
val (lunchHour, lunchMinute) = prefs.getReminderTime(MealType.LUNCH)
|
||||
assertEquals(12, lunchHour)
|
||||
assertEquals(30, lunchMinute)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testUpdateAndPersistReminderValues() {
|
||||
prefs.setReminderEnabled(MealType.SNACK, true)
|
||||
assertTrue(prefs.isReminderEnabled(MealType.SNACK))
|
||||
|
||||
prefs.setReminderTime(MealType.SNACK, 16, 45)
|
||||
val (hour, minute) = prefs.getReminderTime(MealType.SNACK)
|
||||
assertEquals(16, hour)
|
||||
assertEquals(45, minute)
|
||||
|
||||
prefs.setPromptedNotificationPermission(true)
|
||||
assertTrue(prefs.hasPromptedNotificationPermission())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testCreateNotificationChannel() {
|
||||
MealNotificationManager.createNotificationChannel(context)
|
||||
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as android.app.NotificationManager
|
||||
val channel = notificationManager.getNotificationChannel(MealNotificationManager.CHANNEL_ID)
|
||||
assertEquals(MealNotificationManager.CHANNEL_NAME, channel.name)
|
||||
assertEquals(android.app.NotificationManager.IMPORTANCE_HIGH, channel.importance)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user