package com.syncflow.ui.addpair

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

/**
 * Multi-folder backup: each selected folder maps to its OWN remote subfolder. The leaf-name and
 * uniqueness helpers are what keep that safe — same-named folders from different parents must not
 * collapse into one remote dir and overwrite each other.
 */
class MultiFolderTest {

    @Test fun `leaf name from SAF tree uri`() {
        assertEquals("Camera", folderLeafName("content://com.android.externalstorage.documents/tree/primary%3ADCIM%2FCamera"))
        assertEquals("DCIM", folderLeafName("content://com.android.externalstorage.documents/tree/primary%3ADCIM"))
    }

    @Test fun `leaf name from plain filesystem path`() {
        assertEquals("Camera", folderLeafName("/storage/emulated/0/DCIM/Camera"))
        assertEquals("Pictures", folderLeafName("/storage/emulated/0/Pictures/"))
    }

    @Test fun `blank-ish paths fall back to folder`() {
        assertEquals("folder", folderLeafName(""))
        assertEquals("folder", folderLeafName("/"))
    }

    @Test fun `unique sub names disambiguate collisions`() {
        val used = mutableSetOf<String>()
        assertEquals("Camera", uniqueSubName("Camera", used))
        assertEquals("Camera_2", uniqueSubName("Camera", used))
        assertEquals("Camera_3", uniqueSubName("Camera", used))
        assertEquals("Pictures", uniqueSubName("Pictures", used))
    }

    @Test fun `unique sub name flattens slashes`() {
        val used = mutableSetOf<String>()
        assertTrue('/' !in uniqueSubName("a/b/c", used))
    }
}
