@file:OptIn(kotlin.js.ExperimentalWasmJsInterop::class) package eu.friedcutlet.yokan.services.files import eu.friedcutlet.yokan.domain.ProfileId import eu.friedcutlet.yokan.app.profiles.deletePlatformProfileStorage import eu.friedcutlet.yokan.data.DefaultYokanRepository import eu.friedcutlet.yokan.data.SettingsStore import eu.friedcutlet.yokan.database.* import eu.friedcutlet.yokan.services.library.DefaultLocalLibraryService import eu.friedcutlet.yokan.services.library.LocalMediaImporter import kotlin.js.JsAny import kotlinx.coroutines.test.runTest import kotlin.test.* class BrowserLocalLibraryImportVisibilityTest { @Test fun parentFolderScansIgnoreCommittedFilesUntilTheWholeImportIsPublished() = runTest { val id = ProfileId.random() val files = createPlatformLocalFileService(id) val db = InMemoryYokanDatabase(emptySnapshot()) val settings = SettingsStore(DefaultYokanRepository(db), backgroundScope) {} settings.updateDb { it.copy(namedLocalFolders = listOf(DbLocalFolder("Everything", files.downloadRoot))) } val scanner = DefaultLocalLibraryService(db, files, createPlatformLocalArchiveService(id)) val source = browserMediaSelection(twoEpisodes()) try { val second = source.files[1] val selection = LocalMediaSelection(listOf(source.files[0], object : LocalMediaInput { override val relativePath = second.relativePath override val sizeBytes = second.sizeBytes override suspend fun readChunks(consume: suspend (ByteArray) -> Unit) { // The first file has committed to actual OPFS and is visible in its inventory. assertEquals(0, scanner.scan().total) assertTrue(db.mangas().isEmpty()) second.readChunks(consume) } })) LocalMediaImporter(files, settings).import(selection, DbItemType.Anime) assertEquals(1, scanner.scan().total) val title = db.mangas().single() assertEquals(DbItemType.Anime, title.itemType) assertEquals(2, db.chapters(title.id).size) assertEquals(0, scanner.scan().added) assertEquals(listOf(title.id), db.mangas().map { it.id }) } finally { source.close(); files.close(); deletePlatformProfileStorage(id, emptyList()) } } } private fun twoEpisodes(): JsAny = js("""(() => { const files = [1, 2].map(number => { const file = new File([new Uint8Array([number])], number + '.mp4'); Object.defineProperty(file, 'webkitRelativePath', { value: 'Show/' + number + '.mp4' }); return file; }); return globalThis.yokanBrowserFiles.selectedMedia(files); })()""")