mirror of
https://github.com/bleackstorm/satiea.git
synced 2026-12-03 07:41:56 +01:00
build: initialize project structure
Set up Android project configuration, Gradle build scripts, typography, and default resource assets.
This commit is contained in:
1 parent
0a595f619a
commit
f13d46f778
65 files changed
+6056
-11
No files matched your search
@@ -0,0 +1,21 @@
|
||||
package com.example
|
||||
|
||||
import android.content.Context
|
||||
import androidx.test.core.app.ApplicationProvider
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.annotation.Config
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [36])
|
||||
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)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.example
|
||||
|
||||
import org.junit.Assert.*
|
||||
import org.junit.Test
|
||||
|
||||
/**
|
||||
* Example local unit test, which will execute on the development machine (host).
|
||||
*
|
||||
* See [testing documentation](http://d.android.com/tools/testing).
|
||||
*/
|
||||
class ExampleUnitTest {
|
||||
@Test
|
||||
fun addition_isCorrect() {
|
||||
assertEquals(4, 2 + 2)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
package com.example.base
|
||||
|
||||
import android.content.Context
|
||||
import androidx.test.core.app.ApplicationProvider
|
||||
import com.google.firebase.FirebaseApp
|
||||
import com.google.firebase.FirebaseOptions
|
||||
import com.google.firebase.auth.FirebaseAuth
|
||||
import com.google.firebase.auth.FirebaseAuthInvalidUserException
|
||||
import com.google.firebase.firestore.FirebaseFirestore
|
||||
import com.google.firebase.firestore.FirebaseFirestoreException
|
||||
import com.google.firebase.firestore.FirebaseFirestoreSettings
|
||||
import com.google.firebase.firestore.MemoryCacheSettings
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.tasks.await
|
||||
import kotlinx.coroutines.withContext
|
||||
import kotlinx.coroutines.withTimeout
|
||||
import org.junit.After
|
||||
import org.junit.Before
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.annotation.Config
|
||||
import org.robolectric.annotation.LooperMode
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(manifest = Config.NONE)
|
||||
@LooperMode(LooperMode.Mode.INSTRUMENTATION_TEST)
|
||||
abstract class FirestoreEmulatorTestBase {
|
||||
|
||||
protected lateinit var firestore: FirebaseFirestore
|
||||
protected lateinit var auth: FirebaseAuth
|
||||
protected lateinit var databaseId: String
|
||||
|
||||
@Before
|
||||
open fun setUpFirebase() {
|
||||
val context = ApplicationProvider.getApplicationContext<Context>()
|
||||
val resId = context.resources.getIdentifier("firestore_database_id", "string", context.packageName)
|
||||
databaseId = if (resId != 0) context.getString(resId) else "demo-database"
|
||||
|
||||
val app = if (FirebaseApp.getApps(context).isEmpty()) {
|
||||
val newApp = FirebaseApp.initializeApp(
|
||||
context,
|
||||
FirebaseOptions.Builder()
|
||||
.setApplicationId("com.example.app")
|
||||
.setProjectId(PROJECT_ID)
|
||||
.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)
|
||||
auth = FirebaseAuth.getInstance(app)
|
||||
}
|
||||
|
||||
@After
|
||||
open fun tearDownFirebase() {
|
||||
auth.signOut()
|
||||
}
|
||||
|
||||
protected suspend fun signInTestUser(email: String): String = withContext(Dispatchers.IO) {
|
||||
withTimeout(AUTH_TIMEOUT_MS) {
|
||||
val result = try {
|
||||
auth.signInWithEmailAndPassword(email, DEFAULT_PASSWORD).await()
|
||||
} catch (unused: FirebaseAuthInvalidUserException) {
|
||||
auth.createUserWithEmailAndPassword(email, DEFAULT_PASSWORD).await()
|
||||
}
|
||||
checkNotNull(result.user?.uid) { "User auth failed" }
|
||||
}
|
||||
}
|
||||
|
||||
protected fun Throwable.unwrapFirestoreException(): FirebaseFirestoreException? {
|
||||
var current: Throwable? = this
|
||||
while (current != null) {
|
||||
if (current is FirebaseFirestoreException) return current
|
||||
current = current.cause
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
private companion object {
|
||||
const val EMULATOR_HOST = "127.0.0.1"
|
||||
const val FIRESTORE_PORT = 8085
|
||||
const val AUTH_PORT = 9099
|
||||
const val PROJECT_ID = "demo-no-project"
|
||||
const val DEFAULT_PASSWORD = "password123"
|
||||
const val AUTH_TIMEOUT_MS = 5000L
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,157 @@
|
||||
package com.example.data.repository
|
||||
|
||||
import com.example.base.FirestoreEmulatorTestBase
|
||||
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.google.firebase.firestore.FirebaseFirestoreException
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import kotlinx.coroutines.withTimeout
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertNotNull
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Assert.fail
|
||||
import org.junit.Test
|
||||
|
||||
class MealRepositoryRuleTest : FirestoreEmulatorTestBase() {
|
||||
|
||||
@Test
|
||||
fun addMeal_validPayloadWithFoodsAndScale10_createsDocumentAndReturnsId() = runBlocking {
|
||||
val uid = signInTestUser(ALICE_EMAIL)
|
||||
val repository = MealRepository(firestore)
|
||||
|
||||
val foodItems = listOf(
|
||||
MealFoodItem("Riz basmati", 150.0, FoodUnit.G).serialize(),
|
||||
MealFoodItem("Avocat", 1.0, FoodUnit.PORTION).serialize()
|
||||
)
|
||||
|
||||
val entry = MealEntry(
|
||||
userId = uid,
|
||||
mealType = MealType.LUNCH.name,
|
||||
mealTimestamp = System.currentTimeMillis(),
|
||||
hungerBefore = 7,
|
||||
satietyAfter = 8,
|
||||
note = "Note globale : déjeuner calme et équilibré",
|
||||
foodItemsSerialized = foodItems,
|
||||
photosBase64 = emptyList()
|
||||
)
|
||||
|
||||
val createResult = withTimeout(DEFAULT_TIMEOUT_MS) { repository.addMeal(entry) }
|
||||
assertTrue(createResult.isSuccess)
|
||||
val mealId = createResult.getOrThrow()
|
||||
assertTrue(mealId.isNotEmpty())
|
||||
|
||||
val fetched = withTimeout(DEFAULT_TIMEOUT_MS) { repository.getMealById(mealId).getOrThrow() }
|
||||
assertEquals(2, fetched.parsedFoodItems().size)
|
||||
assertEquals("Riz basmati", fetched.parsedFoodItems()[0].name)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun saveCustomFood_andObserve_enrichesCloudCatalog() = runBlocking {
|
||||
signInTestUser(ALICE_EMAIL)
|
||||
val repository = MealRepository(firestore)
|
||||
|
||||
val saveResult = withTimeout(DEFAULT_TIMEOUT_MS) {
|
||||
repository.saveCustomFood("Houmous maison", FoodUnit.G)
|
||||
}
|
||||
assertTrue(saveResult.isSuccess)
|
||||
|
||||
val customFoods = withTimeout(FLOW_TIMEOUT_MS) {
|
||||
repository.observeCustomFoods().first { list -> list.any { it.name == "Houmous maison" } }
|
||||
}
|
||||
assertTrue(customFoods.any { it.name == "Houmous maison" && it.defaultUnit == "g" })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun updateMeal_retroactiveDateAndScale10_updatesSuccessfully() = runBlocking {
|
||||
val uid = signInTestUser(ALICE_EMAIL)
|
||||
val repository = MealRepository(firestore)
|
||||
|
||||
val initialTimestamp = System.currentTimeMillis()
|
||||
val entry = MealEntry(
|
||||
userId = uid,
|
||||
mealType = MealType.DINNER.name,
|
||||
mealTimestamp = initialTimestamp,
|
||||
hungerBefore = 6,
|
||||
satietyAfter = 7,
|
||||
note = "Soupe et tartine",
|
||||
foodItemsSerialized = listOf(MealFoodItem("Soupe de légumes", 300.0, FoodUnit.ML).serialize())
|
||||
)
|
||||
|
||||
val mealId = withTimeout(DEFAULT_TIMEOUT_MS) { repository.addMeal(entry).getOrThrow() }
|
||||
val correctedTimestamp = initialTimestamp - 86_400_000L
|
||||
|
||||
val updatedEntry = entry.copy(
|
||||
id = mealId,
|
||||
mealTimestamp = correctedTimestamp,
|
||||
hungerBefore = 8,
|
||||
satietyAfter = 9,
|
||||
note = "Soupe et tartine (date corrigée)",
|
||||
foodItemsSerialized = listOf(
|
||||
MealFoodItem("Soupe de légumes", 300.0, FoodUnit.ML).serialize(),
|
||||
MealFoodItem("Pain au levain", 60.0, FoodUnit.G).serialize()
|
||||
)
|
||||
)
|
||||
|
||||
val updateResult = withTimeout(DEFAULT_TIMEOUT_MS) { repository.updateMeal(updatedEntry) }
|
||||
assertTrue(updateResult.isSuccess)
|
||||
|
||||
val fetchResult = withTimeout(DEFAULT_TIMEOUT_MS) { repository.getMealById(mealId) }
|
||||
assertTrue(fetchResult.isSuccess)
|
||||
val fetched = fetchResult.getOrThrow()
|
||||
assertEquals(correctedTimestamp, fetched.mealTimestamp)
|
||||
assertEquals(2, fetched.parsedFoodItems().size)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun getMealById_crossUserAccess_failsWithPermissionDenied() = runBlocking {
|
||||
val aliceUid = signInTestUser(ALICE_EMAIL)
|
||||
val aliceRepo = MealRepository(firestore)
|
||||
|
||||
val entry = MealEntry(
|
||||
userId = aliceUid,
|
||||
mealType = MealType.LUNCH.name,
|
||||
mealTimestamp = System.currentTimeMillis(),
|
||||
hungerBefore = 6,
|
||||
satietyAfter = 8
|
||||
)
|
||||
|
||||
val mealId = withTimeout(DEFAULT_TIMEOUT_MS) { aliceRepo.addMeal(entry).getOrThrow() }
|
||||
|
||||
signInTestUser(BOB_EMAIL)
|
||||
val bobRepo = MealRepository(firestore)
|
||||
|
||||
try {
|
||||
withTimeout(DEFAULT_TIMEOUT_MS) { bobRepo.getMealById(mealId = mealId, ownerId = aliceUid).getOrThrow() }
|
||||
fail("Expected cross-user access to fail")
|
||||
} catch (e: Exception) {
|
||||
val firestoreException = e.unwrapFirestoreException()
|
||||
assertNotNull("Expected nested FirebaseFirestoreException", firestoreException)
|
||||
assertEquals(FirebaseFirestoreException.Code.PERMISSION_DENIED, firestoreException?.code)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun observeMeals_unauthenticatedUser_failsWithPermissionDenied() = runBlocking {
|
||||
auth.signOut()
|
||||
val repository = MealRepository(firestore)
|
||||
|
||||
try {
|
||||
withTimeout(FLOW_TIMEOUT_MS) { repository.observeMeals().first() }
|
||||
fail("Expected unauthenticated query to fail")
|
||||
} catch (e: Exception) {
|
||||
assertTrue(
|
||||
e is IllegalStateException || e.unwrapFirestoreException()?.code == FirebaseFirestoreException.Code.PERMISSION_DENIED
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private companion object {
|
||||
const val ALICE_EMAIL = "alice@test.com"
|
||||
const val BOB_EMAIL = "bob@test.com"
|
||||
const val DEFAULT_TIMEOUT_MS = 5000L
|
||||
const val FLOW_TIMEOUT_MS = 4000L
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user