/*
 * Copyright © All Contributors. See LICENSE and AUTHORS in the root directory for details.
 */

package at.bitfire.synctools.mapping.jtx.builder

import android.content.ContentValues
import android.content.Entity
import androidx.core.content.contentValuesOf
import at.bitfire.DefaultTimezoneRule
import at.bitfire.synctools.icalendar.propertyListOf
import at.bitfire.synctools.test.assertContentValuesEqual
import at.techbee.jtx.JtxContract
import net.fortuna.ical4j.model.component.VToDo
import net.fortuna.ical4j.model.property.DtStamp
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import java.time.Instant

@RunWith(RobolectricTestRunner::class)
class DtStampBuilderTest {

    @get:Rule
    val tzRule = DefaultTimezoneRule("Europe/Berlin")

    private val builder = DtStampBuilder()

    @Test
    fun `DTSTAMP is auto-generated by VToDo`() {
        val output = Entity(ContentValues())
        val task = VToDo()

        builder.build(from = task, main = task, to = output)

        // VToDo auto-generates DTSTAMP, so we just check it's not null
        assert(output.entityValues.containsKey(JtxContract.JtxICalObject.DTSTAMP))
    }

    @Test
    fun `DTSTAMP with explicit value`() {
        val output = Entity(ContentValues())
        val instant = Instant.ofEpochMilli(3_000_000L)
        val task = VToDo(propertyListOf(DtStamp(instant)))

        builder.build(from = task, main = task, to = output)

        assertContentValuesEqual(
            contentValuesOf(JtxContract.JtxICalObject.DTSTAMP to 3_000_000L),
            output.entityValues
        )
    }

}
