package com.aletheia.app.ui.reader

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

class ReaderStateCoordinatorTest {
    @Test
    fun latestPublicationWinsAcrossReaderInstances() {
        val coordinator = ReaderStateCoordinator()
        val first = coordinator.publish(7L, 0.2, "first", null, 2, 10, 1, 3)
        val second = coordinator.publish(7L, 0.8, "second", "Глава", 8, 10, 2, 3)

        assertTrue(second.revision > first.revision)
        assertEquals("second", coordinator.latest(7L)?.locator)
        assertEquals(0.8, coordinator.latest(7L)?.progress ?: 0.0, 0.0001)
    }

    @Test
    fun clearDropsOnlyRequestedBook() {
        val coordinator = ReaderStateCoordinator()
        coordinator.publish(1L, 0.1, null, null, 1, 10, 1, 1)
        coordinator.publish(2L, 0.2, null, null, 2, 10, 1, 1)

        coordinator.clear(1L)

        assertNull(coordinator.latest(1L))
        assertEquals(2L, coordinator.latest(2L)?.bookId)
    }
}
