package com.ponzischeme89.memby.data

import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotEquals
import org.junit.Assert.assertTrue
import org.junit.Test

/**
 * The device id is the television's identity in Emby's devices list and in
 * Settings → Devices. What these pin is that reinstalling the app reproduces it — that is
 * the whole reason it is derived rather than random — and that the platform values which
 * cannot identify a set do not silently make two televisions into one.
 */
class DeviceIdTest {

    @Test
    fun `same android id yields the same device id`() {
        assertEquals(deviceIdFor("a1b2c3d4e5f60718"), deviceIdFor("a1b2c3d4e5f60718"))
    }

    @Test
    fun `different android ids yield different device ids`() {
        assertNotEquals(deviceIdFor("a1b2c3d4e5f60718"), deviceIdFor("18f7e6f5d4c3b2a1"))
    }

    @Test
    fun `device id never carries the platform identifier`() {
        val id = deviceIdFor("a1b2c3d4e5f60718")
        assertTrue(id.startsWith("memby-"))
        assertTrue(!id.contains("a1b2c3d4e5f60718"))
    }

    @Test
    fun `surrounding whitespace does not change the identity`() {
        assertEquals(deviceIdFor("a1b2c3d4e5f60718"), deviceIdFor("  a1b2c3d4e5f60718 "))
    }

    @Test
    fun `unusable android ids fall back to a unique id`() {
        // A null, blank, short or all-zero value identifies nothing, and the id a batch of
        // early devices shared would make every one of them the same television. Two calls
        // must not agree: one duplicate entry is better than two sets sharing a session.
        for (value in listOf(null, "", "   ", "9774d56d682e549c", "0000000000000000", "abc")) {
            assertNotEquals("android id $value", deviceIdFor(value), deviceIdFor(value))
        }
    }
}
