Yokan / rebuild
Course 1 8624561

BackupTestFixtures.kt

composeApp/src/commonTest/kotlin/eu/friedcutlet/yokan/services/backup/BackupTestFixtures.kt

test / fixture71 lines
1package eu.friedcutlet.yokan.services.backup23import eu.friedcutlet.yokan.database.InMemoryYokanDatabase4import eu.friedcutlet.yokan.database.YokanDatabase5import eu.friedcutlet.yokan.database.YokanDatabaseSnapshot6import eu.friedcutlet.yokan.services.files.LocalEntry7import eu.friedcutlet.yokan.services.files.LocalFileEntry8import eu.friedcutlet.yokan.services.files.LocalFileService9import kotlinx.serialization.json.Json1011internal open class MemoryFileService : LocalFileService {12    val files = mutableMapOf<String, ByteArray>()13    var onWrite: (() -> Unit)? = null14    override val downloadRoot: String = "/tmp/downloads"15    override val backupRoot: String = "/tmp/backups"16    override val cacheRoot: String = "/tmp/cache"17    override fun writeBytes(path: String, bytes: ByteArray) {18        onWrite?.invoke()19        files[path] = bytes20    }21    override fun readBytes(path: String): ByteArray = files[path] ?: ByteArray(0)22    override fun canonicalPath(path: String): String? = path23    override fun exists(path: String): Boolean = path in files24    override fun deleteRecursively(path: String): Boolean {25        files.keys.removeAll { it == path || it.startsWith("$path/") }26        return true27    }28    override fun directorySize(path: String): Long = files.filter { it.key.startsWith(path) }.values.sumOf { it.size.toLong() }29    override fun openLocation(path: String): Boolean = false30    override fun listLocalEntries(root: String): List<LocalEntry>? = emptyList()31    override fun listFiles(root: String): List<LocalFileEntry> = files.filter { it.key.startsWith(root) }.map {32        LocalFileEntry(it.key, it.key.substringAfterLast('/'), it.value.size.toLong(), 1000L)33    }34}3536internal class PassthroughCompression : BackupCompression {37    override fun compress(data: ByteArray, level: Int): ByteArray = data38    override fun decompress(data: ByteArray): ByteArray = data39}4041internal class FixtureCompression(42    private val gzipInput: Boolean = false,43    private val zipEntries: Map<String, ByteArray>? = null,44) : BackupCompression {45    override fun compress(data: ByteArray, level: Int): ByteArray = data46    override fun decompress(data: ByteArray): ByteArray = data47    override fun looksGzipped(data: ByteArray): Boolean = gzipInput48    override fun looksZipped(data: ByteArray): Boolean = zipEntries != null49    override suspend fun unzipEntriesAsync(data: ByteArray): Map<String, ByteArray>? = zipEntries50}5152internal class CountingDatabase(53    private val delegate: InMemoryYokanDatabase,54) : YokanDatabase by delegate {55    var atomicReplacementCalls = 05657    override suspend fun replaceAllAtomically(58        snapshot: YokanDatabaseSnapshot,59        expectedSnapshot: YokanDatabaseSnapshot?,60        removedSourceIds: Set<Long>,61    ) {62        atomicReplacementCalls++63        delegate.replaceAllAtomically(snapshot, expectedSnapshot, removedSourceIds)64    }65}6667internal val NativeBackupJson = Json { encodeDefaults = true }6869internal fun native(snapshot: BackupSnapshot): ByteArray = NativeBackupJson70    .encodeToString(BackupSnapshot.serializer(), snapshot)71    .encodeToByteArray()72

Search chapters, reference and source filenames.