package net.sergeych.toread.fb2

import java.io.ByteArrayOutputStream
import java.util.zip.ZipEntry
import java.util.zip.ZipOutputStream
import kotlin.test.Test
import kotlin.test.assertEquals

class Fb2JvmZipTest {
    @Test
    fun parsesDeflatedZip() {
        val zip = ByteArrayOutputStream()
        ZipOutputStream(zip).use { output ->
            output.putNextEntry(ZipEntry("deflated.fb2"))
            output.write(sampleXml.encodeToByteArray())
            output.closeEntry()
        }

        val book = Fb2Format.parse(zip.toByteArray(), "deflated.fb2.zip")

        assertEquals("Deflated Book", book.title)
        assertEquals("Zip Writer", book.authors.single().displayName)
    }

    private val sampleXml = """
        <?xml version="1.0" encoding="UTF-8"?>
        <FictionBook xmlns="http://www.gribuser.ru/xml/fictionbook/2.0">
          <description>
            <title-info>
              <genre>prose</genre>
              <author><first-name>Zip</first-name><last-name>Writer</last-name></author>
              <book-title>Deflated Book</book-title>
              <lang>en</lang>
            </title-info>
            <document-info>
              <author><nickname>Toread</nickname></author>
              <date>2026-05-12</date>
              <id>deflated</id>
              <version>1.0</version>
            </document-info>
          </description>
          <body><section><p>Compressed.</p></section></body>
        </FictionBook>
    """.trimIndent()
}
