BrowserUpdateDeviceConditionsTest.kt
composeApp/src/wasmJsTest/kotlin/eu/friedcutlet/yokan/services/scheduling/BrowserUpdateDeviceConditionsTest.kt
1@file:OptIn(kotlin.js.ExperimentalWasmJsInterop::class)23package eu.friedcutlet.yokan.services.scheduling45import eu.friedcutlet.yokan.database.*6import kotlinx.coroutines.test.runTest7import kotlin.js.JsAny8import kotlin.test.*910class BrowserUpdateDeviceConditionsTest {11 @Test fun browserWifiAndBatteryReadingsDoNotInferUnmeteredFromDisabledDataSaver() = runTest {12 val saved = installConditions(true, "wifi", false, 0)13 try {14 assertEquals(UpdateDeviceConditions(true, true, null, false), readUpdateDeviceConditions())15 assertEquals(1, batteryCalls())16 } finally { restoreConditions(saved) }17 }1819 @Test fun deniedBatteryAndDataSaverRemainConservativeEvenWithUserCostClassification() = runTest {20 val saved = installConditions(true, "ethernet", true, 2)21 try {22 val conditions = readUpdateDeviceConditions()23 assertEquals(UpdateDeviceConditions(true, false, false, null), conditions)24 val policy = DbAutomaticUpdateConstraints(DbUpdateNetwork.Unmetered, true, DbUnknownNetworkCost.Unmetered)25 assertEquals(setOf(UpdateDeferral.UnmeteredRequired, UpdateDeferral.PowerUnknown), evaluateUpdateConstraints(policy, conditions, true).reasons)26 } finally { restoreConditions(saved) }27 }2829 @Test fun missingBrowserApisStayUnknownAndNetworkOnlyChecksDoNotInvokeBattery() = runTest {30 val saved = installConditions(true, "unknown", false, 3)31 try {32 assertEquals(UpdateDeviceConditions(connected = true), readUpdateDeviceConditions())33 assertEquals(0, batteryCalls())34 } finally { restoreConditions(saved) }35 val pending = installConditions(false, "wifi", false, 4)36 try {37 assertEquals(UpdateDeviceConditions(false, false, false, null), readUpdateDeviceConditions(needPower = false))38 assertEquals(0, batteryCalls())39 } finally { restoreConditions(pending) }40 }4142 @Test fun stalledBatteryPromiseTimesOutWhilePreservingNetworkReadings() = runTest {43 val saved = installConditions(true, "wifi", false, 4)44 try {45 assertEquals(UpdateDeviceConditions(true, true, null, null), readUpdateDeviceConditions())46 assertEquals(1, batteryCalls())47 } finally { restoreConditions(saved) }48 }49}5051private fun installConditions(online: Boolean, type: String, saveData: Boolean, batteryKind: Int): JsAny = js("""52 (() => {53 const n = globalThis.navigator;54 const keys = ['onLine', 'connection', 'getBattery'];55 const saved = { descriptors: keys.map(key => [key, Object.getOwnPropertyDescriptor(n, key)]) };56 globalThis.__yokanConditionBatteryCalls = 0;57 Object.defineProperty(n, 'onLine', { configurable: true, value: online });58 Object.defineProperty(n, 'connection', { configurable: true, value: { type, saveData } });59 Object.defineProperty(n, 'getBattery', { configurable: true, value: batteryKind === 3 ? undefined : () => {60 globalThis.__yokanConditionBatteryCalls++;61 if (batteryKind === 2) return Promise.reject(new Error('Permission denied'));62 if (batteryKind === 4) return new Promise(() => {});63 return Promise.resolve({ charging: batteryKind === 1 });64 }});65 return saved;66 })()67""")6869private fun batteryCalls(): Int = js("globalThis.__yokanConditionBatteryCalls || 0")70private fun restoreConditions(saved: JsAny): Unit = js("""71 saved.descriptors.forEach(([key, descriptor]) => {72 if (descriptor) Object.defineProperty(globalThis.navigator, key, descriptor);73 else delete globalThis.navigator[key];74 })75""")76