mirror of
https://github.com/bleackstorm/satiea.git
synced 2026-12-03 07:41:56 +01:00
230 lines
9.1 KiB
Kotlin
230 lines
9.1 KiB
Kotlin
package com.example
|
|
|
|
import android.content.Context
|
|
import androidx.test.core.app.ApplicationProvider
|
|
import com.example.data.model.ExpirationDefaults
|
|
import com.example.data.model.ExpirationStatus
|
|
import com.example.data.model.FoodCategory
|
|
import com.example.data.model.FoodUnit
|
|
import com.example.data.model.InventoryItem
|
|
import com.example.data.model.MealFoodItem
|
|
import com.example.data.model.MealType
|
|
import com.example.data.model.PlannedMealEntry
|
|
import com.example.data.model.StorageLocation
|
|
import com.example.data.preferences.PlanningPreferencesManager
|
|
import com.example.domain.ReceiptScannerAnalyzer
|
|
import com.example.domain.ShoppingListCalculator
|
|
import com.example.domain.WeeklyMealPredictor
|
|
import org.junit.Assert.assertEquals
|
|
import org.junit.Assert.assertNull
|
|
import org.junit.Assert.assertTrue
|
|
import org.junit.Test
|
|
import org.junit.runner.RunWith
|
|
import org.robolectric.RobolectricTestRunner
|
|
import org.robolectric.annotation.Config
|
|
import java.time.LocalDate
|
|
|
|
@RunWith(RobolectricTestRunner::class)
|
|
@Config(sdk = [36])
|
|
class ExpirationAndAdaptationTest {
|
|
|
|
private val today = LocalDate.of(2026, 11, 28)
|
|
|
|
@Test
|
|
fun inventoryItem_expirationStatusAndBadges_coverAllStates() {
|
|
val expired = InventoryItem(
|
|
id = "1",
|
|
name = "Yaourt",
|
|
quantity = 2.0,
|
|
unit = FoodUnit.PORTION,
|
|
expirationDateEpochDay = today.minusDays(2).toEpochDay()
|
|
)
|
|
val urgentToday = expired.copy(id = "2", expirationDateEpochDay = today.toEpochDay())
|
|
val urgentTomorrow = expired.copy(id = "3", expirationDateEpochDay = today.plusDays(1).toEpochDay())
|
|
val soon = expired.copy(id = "4", expirationDateEpochDay = today.plusDays(3).toEpochDay())
|
|
val fresh = expired.copy(id = "5", expirationDateEpochDay = today.plusDays(10).toEpochDay())
|
|
val noDate = expired.copy(id = "6", expirationDateEpochDay = null)
|
|
|
|
assertEquals(ExpirationStatus.EXPIRED, expired.expirationStatus(3, today))
|
|
assertEquals("Périmé (2 j)", expired.formatExpirationBadge(today))
|
|
|
|
assertEquals(ExpirationStatus.URGENT, urgentToday.expirationStatus(3, today))
|
|
assertEquals("Aujourd'hui", urgentToday.formatExpirationBadge(today))
|
|
|
|
assertEquals(ExpirationStatus.URGENT, urgentTomorrow.expirationStatus(3, today))
|
|
assertEquals("Demain", urgentTomorrow.formatExpirationBadge(today))
|
|
|
|
assertEquals(ExpirationStatus.SOON, soon.expirationStatus(3, today))
|
|
assertEquals(ExpirationStatus.FRESH, soon.expirationStatus(2, today))
|
|
assertEquals("Dans 3 j", soon.formatExpirationBadge(today))
|
|
|
|
assertEquals(ExpirationStatus.FRESH, fresh.expirationStatus(3, today))
|
|
assertEquals(ExpirationStatus.NONE, noDate.expirationStatus(3, today))
|
|
assertNull(noDate.formatExpirationBadge(today))
|
|
}
|
|
|
|
@Test
|
|
fun expirationDefaults_suggestsShelfLifeForFridgeAndNullForFreezerAndDryPantry() {
|
|
assertEquals(3L, ExpirationDefaults.suggestedShelfLifeDays(FoodCategory.PROTEIN, StorageLocation.FRIDGE))
|
|
assertEquals(7L, ExpirationDefaults.suggestedShelfLifeDays(FoodCategory.DAIRY, StorageLocation.FRIDGE))
|
|
assertEquals(3L, ExpirationDefaults.suggestedShelfLifeDays(FoodCategory.BREAD, StorageLocation.PANTRY))
|
|
assertNull(ExpirationDefaults.suggestedShelfLifeDays(FoodCategory.STARCH, StorageLocation.PANTRY))
|
|
assertNull(ExpirationDefaults.suggestedShelfLifeDays(FoodCategory.PROTEIN, StorageLocation.FREEZER))
|
|
}
|
|
|
|
@Test
|
|
fun computeStockDeductionProposals_deductsFifoAcrossMultipleBatches() {
|
|
val earlyBatch = InventoryItem(
|
|
id = "milk_early",
|
|
name = "Lait",
|
|
quantity = 300.0,
|
|
unit = FoodUnit.ML,
|
|
category = FoodCategory.DAIRY,
|
|
location = StorageLocation.FRIDGE,
|
|
expirationDateEpochDay = today.plusDays(1).toEpochDay()
|
|
)
|
|
val lateBatch = InventoryItem(
|
|
id = "milk_late",
|
|
name = "Lait",
|
|
quantity = 1000.0,
|
|
unit = FoodUnit.ML,
|
|
category = FoodCategory.DAIRY,
|
|
location = StorageLocation.FRIDGE,
|
|
expirationDateEpochDay = today.plusDays(7).toEpochDay()
|
|
)
|
|
|
|
val eaten = listOf(
|
|
MealFoodItem(name = "Lait", weight = 500.0, unit = FoodUnit.ML, category = FoodCategory.DAIRY)
|
|
)
|
|
|
|
// Pass late batch first to verify FIFO sorting
|
|
val proposals = ShoppingListCalculator.computeStockDeductionProposals(
|
|
eatenItems = eaten,
|
|
inventory = listOf(lateBatch, earlyBatch)
|
|
)
|
|
|
|
assertEquals(2, proposals.size)
|
|
assertEquals("milk_early", proposals[0].inventoryItem.id)
|
|
assertEquals(300.0, proposals[0].deductAmount, 0.01)
|
|
assertEquals(0.0, proposals[0].remainingAfter, 0.01)
|
|
|
|
assertEquals("milk_late", proposals[1].inventoryItem.id)
|
|
assertEquals(200.0, proposals[1].deductAmount, 0.01)
|
|
assertEquals(800.0, proposals[1].remainingAfter, 0.01)
|
|
}
|
|
|
|
@Test
|
|
fun receiptScanner_parsesEstimatedShelfLifeDays() {
|
|
val json = """
|
|
{
|
|
"items": [
|
|
{
|
|
"rawLabel": "FILET POUL",
|
|
"name": "Blanc de poulet",
|
|
"quantity": 400,
|
|
"unit": "g",
|
|
"category": "PROTEIN",
|
|
"location": "FRIDGE",
|
|
"estimatedShelfLifeDays": 2
|
|
},
|
|
{
|
|
"rawLabel": "PATES PENNE",
|
|
"name": "Pâtes",
|
|
"quantity": 500,
|
|
"unit": "g",
|
|
"category": "STARCH",
|
|
"location": "PANTRY",
|
|
"estimatedShelfLifeDays": 0
|
|
}
|
|
]
|
|
}
|
|
""".trimIndent()
|
|
|
|
val parsed = ReceiptScannerAnalyzer.parseReceiptJson(json, today)
|
|
assertEquals(2, parsed.size)
|
|
assertEquals(today.plusDays(2).toEpochDay(), parsed[0].expirationDateEpochDay)
|
|
assertNull(parsed[1].expirationDateEpochDay)
|
|
}
|
|
|
|
@Test
|
|
fun weeklyMealPredictor_parseAdaptationJson_handlesReplacementAndEmptySlot() {
|
|
val slot0 = today to MealType.DINNER
|
|
val slot1 = today.plusDays(1) to MealType.LUNCH
|
|
val existingDinner = PlannedMealEntry(
|
|
id = "existing_dinner_1",
|
|
dateEpochDay = today.toEpochDay(),
|
|
mealType = MealType.DINNER,
|
|
customTitle = "Pâtes au pesto",
|
|
servings = 2.0
|
|
)
|
|
|
|
val json = """
|
|
{
|
|
"adaptations": [
|
|
{
|
|
"slotIndex": 0,
|
|
"recipeId": "",
|
|
"title": "Poêlée de poulet aux courgettes",
|
|
"reason": "Sauve votre poulet avant demain",
|
|
"usedExpiringNames": ["Blanc de poulet", "Courgettes"],
|
|
"ingredients": [
|
|
{"name": "Blanc de poulet", "weight": 300, "unit": "g", "category": "PROTEIN"},
|
|
{"name": "Courgettes", "weight": 250, "unit": "g", "category": "VEGETABLE"}
|
|
]
|
|
},
|
|
{
|
|
"slotIndex": 1,
|
|
"recipeId": "",
|
|
"title": "Omelette aux épinards",
|
|
"reason": "Utilise vos épinards frais",
|
|
"usedExpiringNames": ["Épinards"],
|
|
"ingredients": [
|
|
{"name": "Épinards", "weight": 150, "unit": "g", "category": "VEGETABLE"}
|
|
]
|
|
}
|
|
]
|
|
}
|
|
""".trimIndent()
|
|
|
|
val proposals = WeeklyMealPredictor.parseAdaptationJson(
|
|
jsonString = json,
|
|
candidateSlots = listOf(slot0, slot1),
|
|
existingMealsBySlot = mapOf(slot0 to existingDinner),
|
|
recipesById = emptyMap(),
|
|
defaultServings = 2.0
|
|
)
|
|
|
|
assertEquals(2, proposals.size)
|
|
// Slot 0 replaces existing meal preserving its ID
|
|
assertEquals("existing_dinner_1", proposals[0].existingPlannedMealId)
|
|
assertEquals("Pâtes au pesto", proposals[0].existingMealTitle)
|
|
assertEquals("existing_dinner_1", proposals[0].toPlannedMealEntry().id)
|
|
assertEquals(listOf("Blanc de poulet", "Courgettes"), proposals[0].usedExpiringNames)
|
|
|
|
// Slot 1 fills empty slot
|
|
assertNull(proposals[1].existingPlannedMealId)
|
|
assertTrue(proposals[1].toPlannedMealEntry().id.isBlank())
|
|
}
|
|
|
|
@Test
|
|
fun planningPreferences_persistsAndClampsExpirationSettings() {
|
|
val context = ApplicationProvider.getApplicationContext<Context>()
|
|
val prefs = PlanningPreferencesManager(context)
|
|
|
|
prefs.setExpirationAlertDays(10)
|
|
assertEquals(7, prefs.getExpirationAlertDays())
|
|
|
|
prefs.setExpirationAlertDays(0)
|
|
assertEquals(1, prefs.getExpirationAlertDays())
|
|
|
|
prefs.setExpirationAlertDays(5)
|
|
assertEquals(5, prefs.getExpirationAlertDays())
|
|
|
|
prefs.setExpirationNotifEnabled(false)
|
|
assertEquals(false, prefs.isExpirationNotifEnabled())
|
|
|
|
prefs.setExpirationNotifTime(18, 30)
|
|
assertEquals(18 to 30, prefs.getExpirationNotifTime())
|
|
}
|
|
}
|