package net.sergeych.toread.storage.jdbc

import java.io.File
import java.nio.file.Files
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue

class ManagedBookObjectStoreTest {
    @Test
    fun copiesFileIntoContentAddressedObjectsDirectory() {
        val root = Files.createTempDirectory("toread-object-store-").toFile()
        try {
            val source = File(root, "source.fb2")
            source.writeText("book body")

            val store = ManagedBookObjectStore(File(root, "library"))
            val stored = store.importFile(source)

            assertEquals("f91f20e9d49a55906a28ec7c5c11cfe59d51f1244fa32e2aaf9c2e8f8ee71925", stored.objectId)
            assertEquals("objects/f9/${stored.objectId}", stored.relativePath)
            assertEquals(source.length(), stored.sizeBytes)
            assertTrue(store.exists(stored.objectId))
            assertEquals("book body", store.fileForObject(stored.objectId).readText())
        } finally {
            root.deleteRecursively()
        }
    }
}
