package com.github.nullptroma.wallenc.usecases

import com.github.nullptroma.wallenc.domain.datatypes.StorageSyncJournalEntry
import com.github.nullptroma.wallenc.domain.datatypes.StorageSyncJournalMerge
import com.github.nullptroma.wallenc.domain.datatypes.StorageSyncOperation
import com.github.nullptroma.wallenc.domain.datatypes.StorageSyncPaths
import com.github.nullptroma.wallenc.domain.datatypes.StorageSyncRevision
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test
import java.time.Instant

class StorageSyncJournalMergeTest {

    @Test
    fun mergeKeepsSingleEntryPerPath() {
        val older = entry(path = "/a.txt", sequence = 1L)
        val newer = entry(path = "/a.txt", sequence = 2L)
        val merged = StorageSyncJournalMerge.merge(
            mapOf("/a.txt" to older),
            mapOf("/a.txt" to newer),
        )
        assertEquals(1, merged.size)
        assertEquals(2L, merged["/a.txt"]?.revision?.sequence)
    }

    @Test
    fun isSyncableUserPathExcludesEncDirAndJournal() {
        assertFalse(StorageSyncPaths.isSyncableUserPath("/88a048ed-enc-dir/sync-journal.json"))
        assertFalse(StorageSyncPaths.isSyncableUserPath("/wallenc-yandex-system/sync-journal.json"))
        assertTrue(StorageSyncPaths.isSyncableUserPath("/wallenc-data/text-secrets.json"))
    }

    private fun entry(path: String, sequence: Long): StorageSyncJournalEntry =
        StorageSyncJournalEntry(
            path = path,
            operation = StorageSyncOperation.UPSERT,
            revision = StorageSyncRevision(
                sequence = sequence,
                actorId = "actor",
                createdAt = Instant.EPOCH,
            ),
        )
}
