From db3a0ad24ab5b199245ab2eb2a4b8d2f49bcd8e2 Mon Sep 17 00:00:00 2001
From: Him188 <Him188@mamoe.net>
Date: Sun, 19 Mar 2023 12:22:33 +0000
Subject: [PATCH] [mock] Filter files for `MockAbsoluteFolder.resolveFiles`,
 part of #2548

---
 .../absolutefile/MockAbsoluteFolder.kt         | 10 ++++++----
 mirai-core-mock/test/AbsoluteFileTest.kt       | 18 +++++++++++++++++-
 2 files changed, 23 insertions(+), 5 deletions(-)

diff --git a/mirai-core-mock/src/internal/remotefile/absolutefile/MockAbsoluteFolder.kt b/mirai-core-mock/src/internal/remotefile/absolutefile/MockAbsoluteFolder.kt
index 708032100..97c90f4b9 100644
--- a/mirai-core-mock/src/internal/remotefile/absolutefile/MockAbsoluteFolder.kt
+++ b/mirai-core-mock/src/internal/remotefile/absolutefile/MockAbsoluteFolder.kt
@@ -1,5 +1,5 @@
 /*
- * Copyright 2019-2022 Mamoe Technologies and contributors.
+ * Copyright 2019-2023 Mamoe Technologies and contributors.
  *
  * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
  * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
@@ -163,9 +163,11 @@ internal open class MockAbsoluteFolder(
         if (path.isBlank()) throw IllegalArgumentException("path cannot be blank.")
         if (!FileSystem.isLegal(path)) return emptyFlow()
         if (path[0] == '/') return files.root.resolveFiles(path.removePrefix("/"))
-        return files.fileSystem.findByPath(absolutePath.removeSuffix("/") + "/" + path.removePrefix("/")).map {
-            it.toMockAbsFile(files)
-        }.asFlow()
+        return files.fileSystem.findByPath(absolutePath.removeSuffix("/") + "/" + path.removePrefix("/"))
+            .filter { it.isFile }
+            .map {
+                it.toMockAbsFile(files)
+            }.asFlow()
     }
 
     @JavaFriendlyAPI
diff --git a/mirai-core-mock/test/AbsoluteFileTest.kt b/mirai-core-mock/test/AbsoluteFileTest.kt
index 5d3c2d7d7..6990adaaa 100644
--- a/mirai-core-mock/test/AbsoluteFileTest.kt
+++ b/mirai-core-mock/test/AbsoluteFileTest.kt
@@ -1,5 +1,5 @@
 /*
- * Copyright 2019-2022 Mamoe Technologies and contributors.
+ * Copyright 2019-2023 Mamoe Technologies and contributors.
  *
  * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
  * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
@@ -11,6 +11,7 @@ package net.mamoe.mirai.mock.test
 
 import com.google.common.jimfs.Configuration
 import com.google.common.jimfs.Jimfs
+import kotlinx.coroutines.flow.count
 import kotlinx.coroutines.flow.first
 import kotlinx.coroutines.flow.firstOrNull
 import kotlinx.coroutines.flow.toList
@@ -23,6 +24,7 @@ import net.mamoe.mirai.mock.utils.simpleMemberInfo
 import net.mamoe.mirai.utils.ExternalResource.Companion.toExternalResource
 import net.mamoe.mirai.utils.cast
 import net.mamoe.mirai.utils.md5
+import net.mamoe.mirai.utils.runBIO
 import org.junit.jupiter.api.AfterEach
 import org.junit.jupiter.api.Test
 import java.nio.file.FileSystem
@@ -129,4 +131,18 @@ internal class AbsoluteFileTest : MockBotTestBase() {
         val file = files.root.resolveFileById(absFile.id, true)!!
         assertContentEquals(bytes.md5(), file.md5)
     }
+
+    @Test
+    fun testResolveFiles() = runTest {
+        val file = runBIO {
+            kotlin.io.path.createTempFile("test", ".txt").toFile().apply {
+                writeText("test")
+                deleteOnExit()
+            }
+        }
+        file.toExternalResource().use {
+            group.files.root.uploadNewFile("/a/test.txt", it)
+        }
+        assertEquals(0, group.files.root.resolveFiles("/a").count())
+    }
 }
\ No newline at end of file