Yokan / rebuild
Course 1 8624561

DesktopUpdateDeviceConditionsTest.kt

composeApp/src/desktopTest/kotlin/eu/friedcutlet/yokan/services/scheduling/DesktopUpdateDeviceConditionsTest.kt

test / fixture73 lines
1package eu.friedcutlet.yokan.services.scheduling23import kotlin.test.*4import kotlinx.coroutines.Dispatchers5import kotlinx.coroutines.async6import kotlinx.coroutines.cancelAndJoin7import kotlinx.coroutines.runBlocking8import kotlinx.coroutines.runInterruptible9import java.io.File10import kotlin.time.TimeSource1112class DesktopUpdateDeviceConditionsTest {13    @Test fun macReportsActiveHardwareAndPowerWithoutInferringNetworkCost() {14        val hardware = "Hardware Port: Wi-Fi\nDevice: en0\n\nHardware Port: Ethernet\nDevice: en1\n"15        val wifi = decodeMacUpdateConditions("interface: en0", hardware, "Now drawing from 'AC Power'\n")16        assertEquals(UpdateDeviceConditions(true, true, null, true), wifi)17        assertEquals(UpdateDeviceConditions(true, false, null, false), decodeMacUpdateConditions("interface: en1", hardware, "Now drawing from 'Battery Power'\n"))18        assertEquals(UpdateDeviceConditions(), decodeMacUpdateConditions(null, null, null))19        assertNull(decodeMacUpdateConditions("interface: en0", null, "invalid").wifi)20        assertNull(decodeMacUpdateConditions("interface: utun0", hardware, null).wifi, "A virtual path is not evidence of a wired connection")21    }2223    @Test fun windowsAcceptsOnlyTypedReadingsAndKeepsUnavailableFieldsUnknown() {24        assertEquals(UpdateDeviceConditions(true, false, false, true), decodeWindowsUpdateConditions(25            "{\"connected\":true,\"wifi\":false,\"unmetered\":false,\"externalPower\":true}"))26        assertEquals(UpdateDeviceConditions(connected = false), decodeWindowsUpdateConditions("{\"connected\":false}"))27        assertEquals(UpdateDeviceConditions(), decodeWindowsUpdateConditions("Access denied"))28        assertEquals(UpdateDeviceConditions(), decodeWindowsUpdateConditions("{\"connected\":\"true\"}"))29    }3031    @Test fun linuxUsesAnUpDefaultRouteIncludingIpv6AndRejectsPartialDisabledOrNonDefaultRoutes() {32        val header = "Iface Destination Gateway Flags RefCnt Use Metric Mask\n"33        val route = header + "eth0 00000000 0100000A 0003 0 0 100 00000000\n"34        assertEquals("eth0", linuxDefaultInterface(route, null, setOf("eth0")))35        assertNull(linuxDefaultInterface(route, null, emptySet()))36        assertNull(linuxDefaultInterface(header + "eth0 00000000 0100000A 0000 0 0 100 00000000", null, setOf("eth0")))37        assertNull(linuxDefaultInterface(route.replace("100 00000000", "100 00000080"), null, setOf("eth0")))38        val ipv6 = "${"0".repeat(32)} 00 ${"0".repeat(32)} 00 ${"0".repeat(32)} 00000100 00000000 00000000 00000001 wlan0"39        assertEquals("wlan0", linuxDefaultInterface(null, ipv6, setOf("wlan0")))40        assertNull(linuxDefaultInterface(null, ipv6.replace("00000001 wlan0", "00000000 wlan0"), setOf("wlan0")))41    }4243    @Test fun linuxGuessedFreeDataAndMissingPowerReadingsRemainUnknown() {44        assertEquals(true, decodeLinuxUnmetered("no\n"))45        assertEquals(false, decodeLinuxUnmetered("yes"))46        assertEquals(false, decodeLinuxUnmetered("yes (guessed)"))47        assertNull(decodeLinuxUnmetered("no (guessed)"))48        assertNull(decodeLinuxUnmetered(null))49        assertEquals(true, decodeLinuxExternalPower(listOf("Mains" to "0", "USB_PD" to "1")))50        assertEquals(false, decodeLinuxExternalPower(listOf("Mains" to "0")))51        assertNull(decodeLinuxExternalPower(listOf("Battery" to null)))52        assertNull(decodeLinuxExternalPower(listOf("Mains" to "invalid")))53    }5455    @Test fun commandReadsHaveRealRuntimeOutputBoundsAndMissingToolsDoNotBecomePositiveReadings() = runBlocking {56        val java = File(System.getProperty("java.home"), "bin/${if (System.getProperty("os.name").contains("win", true)) "java.exe" else "java"}").path57        val version = runInterruptible(Dispatchers.IO) { conditionCommand(listOf(java, "-version")) }58        assertTrue(checkNotNull(version).contains("version", true))59        assertNull(runInterruptible(Dispatchers.IO) { conditionCommand(listOf(java, "-version"), maximumBytes = 1) })60        assertNull(runInterruptible(Dispatchers.IO) { conditionCommand(listOf("yokan-condition-unavailable-test-tool")) })61        val sleep = if (System.getProperty("os.name").contains("win", true))62            listOf("powershell.exe", "-NoProfile", "-Command", "Start-Sleep -Seconds 30")63        else listOf("/bin/sh", "-c", "exec sleep 30")64        val start = TimeSource.Monotonic.markNow()65        assertNull(runInterruptible(Dispatchers.IO) { conditionCommand(sleep, timeoutMillis = 100) })66        assertTrue(start.elapsedNow().inWholeMilliseconds < 3000)67        val cancelled = async { runInterruptible(Dispatchers.IO) { conditionCommand(sleep, timeoutMillis = 30_000) } }68        kotlinx.coroutines.delay(100)69        val cancelAt = TimeSource.Monotonic.markNow()70        cancelled.cancelAndJoin()71        assertTrue(cancelAt.elapsedNow().inWholeMilliseconds < 3000, "Owner cancellation must terminate the command")72    }73}74

Search chapters, reference and source filenames.