|
| 1 | +// |
| 2 | +// Calendar Notifications Plus |
| 3 | +// Copyright (C) 2025 William Harris (wharris+cnplus@upscalews.com) |
| 4 | +// |
| 5 | +// This program is free software; you can redistribute it and/or modify |
| 6 | +// it under the terms of the GNU General Public License as published by |
| 7 | +// the Free Software Foundation; either version 3 of the License, or |
| 8 | +// (at your option) any later version. |
| 9 | +// |
| 10 | +// This program is distributed in the hope that it will be useful, |
| 11 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +// GNU General Public License for more details. |
| 14 | +// |
| 15 | +// You should have received a copy of the GNU General Public License |
| 16 | +// along with this program; if not, write to the Free Software Foundation, |
| 17 | +// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | +// |
| 19 | + |
| 20 | +package com.github.quarck.calnotify.eventsstorage |
| 21 | + |
| 22 | +import android.content.Context |
| 23 | +import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 24 | +import androidx.test.platform.app.InstrumentationRegistry |
| 25 | +import com.github.quarck.calnotify.logs.DevLog |
| 26 | +import expo.modules.mymodule.MyModule |
| 27 | +import org.junit.After |
| 28 | +import org.junit.Assert.* |
| 29 | +import org.junit.Before |
| 30 | +import org.junit.Test |
| 31 | +import org.junit.runner.RunWith |
| 32 | + |
| 33 | +/** |
| 34 | + * Contract tests for sync database consistency. |
| 35 | + * |
| 36 | + * These tests verify that the expo module (MyModule) and main app (EventsStorage) |
| 37 | + * agree on SharedPreferences keys and database names. This prevents the bug where |
| 38 | + * Room migration caused native code to write to "RoomEvents" while sync code |
| 39 | + * was hardcoded to read from "Events". |
| 40 | + * |
| 41 | + * The fix: Communication via SharedPreferences with matching constants. |
| 42 | + * |
| 43 | + * @see docs/dev_todo/sync_database_mismatch.md |
| 44 | + */ |
| 45 | +@RunWith(AndroidJUnit4::class) |
| 46 | +class SyncDatabaseContractTest { |
| 47 | + |
| 48 | + companion object { |
| 49 | + private const val LOG_TAG = "SyncDatabaseContractTest" |
| 50 | + } |
| 51 | + |
| 52 | + private lateinit var context: Context |
| 53 | + |
| 54 | + @Before |
| 55 | + fun setup() { |
| 56 | + context = InstrumentationRegistry.getInstrumentation().targetContext |
| 57 | + DevLog.info(LOG_TAG, "Test setup complete") |
| 58 | + } |
| 59 | + |
| 60 | + @After |
| 61 | + fun cleanup() { |
| 62 | + DevLog.info(LOG_TAG, "Test cleanup complete") |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * CRITICAL: Verifies that MyModule and EventsStorage use the same SharedPreferences file name. |
| 67 | + * If these don't match, the expo module won't read what the app writes. |
| 68 | + */ |
| 69 | + @Test |
| 70 | + fun sharedPreferencesFileNameMatches() { |
| 71 | + assertEquals( |
| 72 | + "MyModule and EventsStorage must use the same SharedPreferences file name", |
| 73 | + EventsStorage.STORAGE_PREFS_NAME, |
| 74 | + MyModule.STORAGE_PREFS_NAME |
| 75 | + ) |
| 76 | + DevLog.info(LOG_TAG, "✅ Prefs file name matches: ${EventsStorage.STORAGE_PREFS_NAME}") |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * CRITICAL: Verifies that MyModule and EventsStorage use the same key for database name. |
| 81 | + */ |
| 82 | + @Test |
| 83 | + fun databaseNamePreferenceKeyMatches() { |
| 84 | + assertEquals( |
| 85 | + "MyModule and EventsStorage must use the same key for active_db_name", |
| 86 | + EventsStorage.PREF_ACTIVE_DB_NAME, |
| 87 | + MyModule.PREF_ACTIVE_DB_NAME |
| 88 | + ) |
| 89 | + DevLog.info(LOG_TAG, "✅ DB name key matches: ${EventsStorage.PREF_ACTIVE_DB_NAME}") |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * CRITICAL: Verifies that MyModule and EventsStorage use the same key for is_using_room flag. |
| 94 | + */ |
| 95 | + @Test |
| 96 | + fun isUsingRoomPreferenceKeyMatches() { |
| 97 | + assertEquals( |
| 98 | + "MyModule and EventsStorage must use the same key for is_using_room", |
| 99 | + EventsStorage.PREF_IS_USING_ROOM, |
| 100 | + MyModule.PREF_IS_USING_ROOM |
| 101 | + ) |
| 102 | + DevLog.info(LOG_TAG, "✅ isUsingRoom key matches: ${EventsStorage.PREF_IS_USING_ROOM}") |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * CRITICAL: Verifies that MyModule's Room database name constant matches EventsDatabase. |
| 107 | + */ |
| 108 | + @Test |
| 109 | + fun roomDatabaseNameConstantMatches() { |
| 110 | + assertEquals( |
| 111 | + "MyModule.ROOM_DATABASE_NAME must match EventsDatabase.DATABASE_NAME", |
| 112 | + EventsDatabase.DATABASE_NAME, |
| 113 | + MyModule.ROOM_DATABASE_NAME |
| 114 | + ) |
| 115 | + DevLog.info(LOG_TAG, "✅ Room DB name matches: ${EventsDatabase.DATABASE_NAME}") |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * CRITICAL: Verifies that MyModule's legacy database name constant matches EventsDatabase. |
| 120 | + */ |
| 121 | + @Test |
| 122 | + fun legacyDatabaseNameConstantMatches() { |
| 123 | + assertEquals( |
| 124 | + "MyModule.LEGACY_DATABASE_NAME must match EventsDatabase.LEGACY_DATABASE_NAME", |
| 125 | + EventsDatabase.LEGACY_DATABASE_NAME, |
| 126 | + MyModule.LEGACY_DATABASE_NAME |
| 127 | + ) |
| 128 | + DevLog.info(LOG_TAG, "✅ Legacy DB name matches: ${EventsDatabase.LEGACY_DATABASE_NAME}") |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Integration test: Verifies that after EventsStorage writes to SharedPreferences, |
| 133 | + * reading with MyModule's keys returns the correct values. |
| 134 | + * |
| 135 | + * This is the actual contract - what EventsStorage writes, MyModule must be able to read. |
| 136 | + */ |
| 137 | + @Test |
| 138 | + fun myModuleCanReadWhatEventsStorageWrites() { |
| 139 | + // Create storage - this writes to SharedPreferences |
| 140 | + val storage = EventsStorage(context) |
| 141 | + |
| 142 | + // Read using MyModule's constants (simulating what the expo module does) |
| 143 | + val prefs = context.getSharedPreferences(MyModule.STORAGE_PREFS_NAME, Context.MODE_PRIVATE) |
| 144 | + val dbNameFromPrefs = prefs.getString(MyModule.PREF_ACTIVE_DB_NAME, MyModule.LEGACY_DATABASE_NAME) |
| 145 | + val isRoomFromPrefs = prefs.getBoolean(MyModule.PREF_IS_USING_ROOM, false) |
| 146 | + |
| 147 | + // Verify they match what EventsStorage reports |
| 148 | + val expectedDbName = if (storage.isUsingRoom) { |
| 149 | + EventsDatabase.DATABASE_NAME |
| 150 | + } else { |
| 151 | + EventsDatabase.LEGACY_DATABASE_NAME |
| 152 | + } |
| 153 | + |
| 154 | + assertEquals( |
| 155 | + "DB name read via MyModule's keys should match EventsStorage state", |
| 156 | + expectedDbName, |
| 157 | + dbNameFromPrefs |
| 158 | + ) |
| 159 | + |
| 160 | + assertEquals( |
| 161 | + "isUsingRoom read via MyModule's keys should match EventsStorage state", |
| 162 | + storage.isUsingRoom, |
| 163 | + isRoomFromPrefs |
| 164 | + ) |
| 165 | + |
| 166 | + DevLog.info(LOG_TAG, "✅ MyModule can read EventsStorage's values: dbName=$dbNameFromPrefs, isRoom=$isRoomFromPrefs") |
| 167 | + |
| 168 | + storage.close() |
| 169 | + } |
| 170 | + |
| 171 | + /** |
| 172 | + * Default test: Verifies that when SharedPreferences is empty (before EventsStorage |
| 173 | + * has been created), MyModule defaults to Room database (the primary implementation). |
| 174 | + * |
| 175 | + * Room is the primary implementation; legacy is only used if Room migration fails. |
| 176 | + */ |
| 177 | + @Test |
| 178 | + fun defaultsToRoomWhenPrefsEmpty() { |
| 179 | + // Clear the SharedPreferences to simulate fresh state |
| 180 | + val prefs = context.getSharedPreferences(MyModule.STORAGE_PREFS_NAME, Context.MODE_PRIVATE) |
| 181 | + prefs.edit().clear().commit() |
| 182 | + |
| 183 | + // Read with defaults (simulating what MyModule.getActiveEventsDbName does) |
| 184 | + // These defaults must match what's in MyModule.kt |
| 185 | + val dbNameFromPrefs = prefs.getString(MyModule.PREF_ACTIVE_DB_NAME, MyModule.ROOM_DATABASE_NAME) |
| 186 | + ?: MyModule.ROOM_DATABASE_NAME |
| 187 | + val isRoomFromPrefs = prefs.getBoolean(MyModule.PREF_IS_USING_ROOM, true) |
| 188 | + |
| 189 | + // Should default to Room (primary implementation) |
| 190 | + assertEquals( |
| 191 | + "When prefs are empty, should default to Room database name", |
| 192 | + MyModule.ROOM_DATABASE_NAME, |
| 193 | + dbNameFromPrefs |
| 194 | + ) |
| 195 | + |
| 196 | + assertEquals( |
| 197 | + "When prefs are empty, isUsingRoom should default to true", |
| 198 | + true, |
| 199 | + isRoomFromPrefs |
| 200 | + ) |
| 201 | + |
| 202 | + DevLog.info(LOG_TAG, "✅ Defaults to Room: dbName=$dbNameFromPrefs, isRoom=$isRoomFromPrefs") |
| 203 | + |
| 204 | + // Now create storage to restore proper state for other tests |
| 205 | + val storage = EventsStorage(context) |
| 206 | + storage.close() |
| 207 | + } |
| 208 | + |
| 209 | + /** |
| 210 | + * Verifies that MyModule's default values point to Room (the primary implementation). |
| 211 | + * Legacy is only used as fallback when Room migration explicitly fails. |
| 212 | + */ |
| 213 | + @Test |
| 214 | + fun defaultValuesPointToRoom() { |
| 215 | + // MyModule's Room database name should be correct |
| 216 | + assertEquals( |
| 217 | + "MyModule.ROOM_DATABASE_NAME should be 'RoomEvents'", |
| 218 | + "RoomEvents", |
| 219 | + MyModule.ROOM_DATABASE_NAME |
| 220 | + ) |
| 221 | + |
| 222 | + // MyModule's legacy database name should also be correct (for fallback) |
| 223 | + assertEquals( |
| 224 | + "MyModule.LEGACY_DATABASE_NAME should be 'Events'", |
| 225 | + "Events", |
| 226 | + MyModule.LEGACY_DATABASE_NAME |
| 227 | + ) |
| 228 | + |
| 229 | + // The Room database file path should be resolvable |
| 230 | + val roomDbPath = context.getDatabasePath(MyModule.ROOM_DATABASE_NAME) |
| 231 | + assertNotNull("Room database path should be resolvable", roomDbPath) |
| 232 | + |
| 233 | + DevLog.info(LOG_TAG, "✅ Default values correct: room=${MyModule.ROOM_DATABASE_NAME}, legacy=${MyModule.LEGACY_DATABASE_NAME}") |
| 234 | + } |
| 235 | +} |
0 commit comments