package de.softwareapp_hb.privateqrscanner.util

import de.softwareapp_hb.privateqrscanner.domain.ScanRecord
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Test

class HistoryExportFormatterTest {

    @Test
    fun formatText_empty_returnsEmptyString() {
        assertEquals("", HistoryExportFormatter.formatText(emptyList()))
    }

    @Test
    fun formatCsv_quotesAndEscapesFields() {
        val history = listOf(
            ScanRecord(
                id = 1,
                timestamp = 0L,
                type = "Te,xt",
                content = "hello \"world\""
            )
        )

        val csv = HistoryExportFormatter.formatCsv(history)

        assertTrue(csv.startsWith("timestamp,type,content\n"))
        assertTrue(csv.contains("\"Te,xt\""))
        assertTrue(csv.contains("\"hello \"\"world\"\"\""))
    }

    @Test
    fun formatJson_escapesQuotesBackslashAndNewline() {
        val history = listOf(
            ScanRecord(
                id = 7,
                timestamp = 123L,
                type = "Type\"A",
                content = "line1\\line2\nnext"
            )
        )

        val json = HistoryExportFormatter.formatJson(history)

        assertTrue(json.contains("\"id\":7"))
        assertTrue(json.contains("\"timestamp\":123"))
        assertTrue(json.contains("\"type\":\"Type\\\"A\""))
        assertTrue(json.contains("\"content\":\"line1\\\\line2\\nnext\""))
    }
}
