/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package mozilla.components.feature.session.bundling.db

import mozilla.components.browser.session.Session
import mozilla.components.browser.session.SessionManager
import org.json.JSONObject
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner

@RunWith(RobolectricTestRunner::class)
class BundleEntityTest {
    @Test
    fun `updateFrom updates state and time`() {
        val bundle = BundleEntity(0, "", 0, UrlList(listOf()))

        val snapshot = SessionManager.Snapshot(
            listOf(SessionManager.Snapshot.Item(session = Session("https://www.mozilla.org"))),
            selectedSessionIndex = 0)

        bundle.updateFrom(snapshot)

        assertTrue(bundle.savedAt > 0)

        assertEquals(1, bundle.urls.entries.size)
        assertEquals("https://www.mozilla.org", bundle.urls.entries[0])

        val json = JSONObject(bundle.state)

        assertEquals(1, json.get("version"))
        assertEquals(0, json.get("selectedSessionIndex"))

        val sessions = json.getJSONArray("sessionStateTuples")
        assertEquals(1, sessions.length())
    }
}
