完善哔哩哔哩双图回复回执校验
回执核对原评论线程、唯一正文及全部绑定图片顺序,补充回复总数校验与回归测试。 记录真机双图入口复查结果,更新拾见 0.1.25 使用说明和开发进度。
This commit is contained in:
@@ -243,15 +243,19 @@ internal class BilibiliComments(
|
||||
private fun UiPage.hasEditor(): Boolean = nodes.any { it.editable && it.focused }
|
||||
|
||||
private fun UiPage.rows(list: UiNode, videoKey: String): List<Row> {
|
||||
val parsed = parsedRows(list, videoKey)
|
||||
val counts = parsed.groupingBy { it.target.key }.eachCount()
|
||||
return parsed.filter { counts[it.target.key] == 1 }
|
||||
}
|
||||
|
||||
private fun UiPage.parsedRows(list: UiNode, videoKey: String): List<Row> {
|
||||
val viewport = Rect(list.bounds)
|
||||
val parsed = if (BilibiliComposeComments.isList(this, list)) {
|
||||
return if (BilibiliComposeComments.isList(this, list)) {
|
||||
BilibiliComposeComments.rows(this, list) { author, body ->
|
||||
CommentTarget(commentKey(videoKey, author, body), body, author)
|
||||
}
|
||||
} else nodes.filter { it.directParent() === list && viewport.contains(it.bounds) }
|
||||
.mapNotNull { row(it, videoKey) }
|
||||
val counts = parsed.groupingBy { it.target.key }.eachCount()
|
||||
return parsed.filter { counts[it.target.key] == 1 }
|
||||
}
|
||||
|
||||
private fun UiPage.row(container: UiNode, videoKey: String): Row? {
|
||||
@@ -328,13 +332,14 @@ internal class BilibiliComments(
|
||||
}
|
||||
|
||||
private fun UiPage.threadRows(list: UiNode, videoKey: String, target: CommentTarget): ThreadRows? {
|
||||
val rows = rows(list, videoKey)
|
||||
val rows = parsedRows(list, videoKey)
|
||||
val original = rows.firstOrNull()?.takeIf { it.target == target } ?: return null
|
||||
val children = nodes.filter { it.directParent() === list && list.bounds.contains(it.bounds) }
|
||||
val complete = if (BilibiliComposeComments.isList(this, list)) {
|
||||
BilibiliComposeComments.completeThread(this, list, rows.size - 1)
|
||||
} else !list.supports(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD) &&
|
||||
!list.supports(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD) && children.none { child ->
|
||||
!list.supports(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD) && nativeThreadTotalMatches(children, rows.size - 1) &&
|
||||
children.none { child ->
|
||||
val members = members(child)
|
||||
child.labels().any(::blocksThreadCompletion) || members.any { it.labels().any(::blocksThreadCompletion) } ||
|
||||
members.any { it.resourceId in setOf("item_include_header", "name_layout") } && row(child, videoKey) == null
|
||||
@@ -342,6 +347,13 @@ internal class BilibiliComments(
|
||||
return ThreadRows(original, rows.drop(1), complete)
|
||||
}
|
||||
|
||||
private fun UiPage.nativeThreadTotalMatches(children: List<UiNode>, nestedCount: Int): Boolean = children.all { child ->
|
||||
val totals = nodes.filter { it.directParent() === child && it.resourceId == "count_view" }
|
||||
if (totals.isEmpty() || nodes.none { it.directParent() === child && it.resourceId == "sort" }) return@all true
|
||||
val total = totals.singleOrNull() ?: return@all false
|
||||
total.labels().mapNotNull(BilibiliLabels::detailReplyTotal).distinct().singleOrNull() == nestedCount
|
||||
}
|
||||
|
||||
private fun blocksThreadCompletion(label: String): Boolean =
|
||||
label in setOf("展开更多评论", "查看更多回复", "加载中", "加载失败", "点击重试")
|
||||
|
||||
|
||||
@@ -51,7 +51,7 @@ internal object BilibiliComposeComments {
|
||||
): List<BilibiliComments.Row> {
|
||||
if (!isList(page, list)) return emptyList()
|
||||
val siblings = page.children(list)
|
||||
val parsed = siblings.mapIndexedNotNull { index, row ->
|
||||
return siblings.mapIndexedNotNull { index, row ->
|
||||
val identity = rowIdentity(page, list, row) ?: return@mapIndexedNotNull null
|
||||
val author = identity.author.text
|
||||
val body = identity.body
|
||||
@@ -74,8 +74,6 @@ internal object BilibiliComposeComments {
|
||||
photos(page, row, body.bounds, reply.label.bounds), own, threadEntry, emptyThread,
|
||||
)
|
||||
}
|
||||
val counts = parsed.groupingBy { it.target.key }.eachCount()
|
||||
return parsed.filter { counts[it.target.key] == 1 }
|
||||
}
|
||||
|
||||
/** The native count must account for every visible nested row before proving a full thread. */
|
||||
|
||||
@@ -4,10 +4,14 @@ import com.jieliu.app.task.RECHECK_WINDOW_MS
|
||||
|
||||
internal object BilibiliLabels {
|
||||
private val replyCount = Regex("(?:(?:全部|查看|展开|共)\\s*)?(\\d+)\\s*条回复(?:\\s*[>>])?")
|
||||
private val detailTotal = Regex("相关回复(?:共)?\\s*(\\d+)\\s*条")
|
||||
private val relativeTime = Regex("^(刚刚|[1-9]\\d?\\s*(?:秒|分钟)前)(?:\\s|$)")
|
||||
|
||||
fun replyCount(label: String): Int? = replyCount.matchEntire(label)?.groupValues?.get(1)?.toIntOrNull()
|
||||
|
||||
fun detailReplyTotal(label: String): Int? = detailTotal.matchEntire(label)?.groupValues?.get(1)?.toIntOrNull()
|
||||
?: replyCount(label) ?: label.toIntOrNull()?.takeIf { it >= 0 }
|
||||
|
||||
fun replyPrompt(label: String, author: String): Boolean {
|
||||
if (author.isBlank()) return false
|
||||
return listOf("回复 @", "回复@", "回复 ", "回复").any { prefix ->
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.jieliu.app.platform
|
||||
|
||||
internal object BilibiliReceiptEvidence {
|
||||
fun <Row> uniqueReply(
|
||||
expectedOriginal: CommentTarget,
|
||||
original: CommentTarget,
|
||||
threadComplete: Boolean,
|
||||
expectedText: String,
|
||||
replies: List<Row>,
|
||||
bodyOf: (Row) -> String,
|
||||
): Row? {
|
||||
if (original != expectedOriginal || !threadComplete) return null
|
||||
return replies.singleOrNull { bodyOf(it) == expectedText }
|
||||
}
|
||||
|
||||
fun matchesOrderedPhotos(expected: List<PhotoPixels>, actual: List<PhotoPixels>): Boolean =
|
||||
expected.size == actual.size && expected.indices.all { PublishedPhotoMatcher.matches(expected[it], actual[it]) }
|
||||
}
|
||||
@@ -194,7 +194,7 @@ internal class BilibiliReplies(
|
||||
if (!initial.own && inspectRole(target, null, initial.key, readReceipt) != Role.OTHER) return CommentPublishResult.UNCERTAIN
|
||||
|
||||
val current = ui.await(5_000) { readReceipt()?.takeIf { it.key == initial.key } } ?: return CommentPublishResult.UNCERTAIN
|
||||
if (!verifyImages(current.images, references) { readReceipt()?.takeIf { it.key == initial.key }?.images }) {
|
||||
if (!verifyImages(current.images, references) { readReceipt()?.takeIf { it == current }?.images }) {
|
||||
return CommentPublishResult.UNCERTAIN
|
||||
}
|
||||
if (references.isNotEmpty()) diagnostic("哔哩哔哩已按顺序核对原回复的全部图片")
|
||||
@@ -205,7 +205,9 @@ internal class BilibiliReplies(
|
||||
|
||||
private fun receiptRow(target: CommentTarget, text: String, count: Int, armedAt: Long): ReceiptRow? =
|
||||
comments.readThread(target) { page, thread ->
|
||||
val row = thread.replies.singleOrNull { it.target.text == text } ?: return@readThread null
|
||||
val row = BilibiliReceiptEvidence.uniqueReply(
|
||||
target, thread.original.target, thread.complete, text, thread.replies,
|
||||
) { it.target.text } ?: return@readThread null
|
||||
val time = row.time ?: return@readThread null
|
||||
if (!BilibiliLabels.matchesSubmissionTime(time, armedAt, System.currentTimeMillis())) return@readThread null
|
||||
val pictures = row.images?.takeIf { it.size == count } ?: return@readThread null
|
||||
@@ -248,8 +250,7 @@ internal class BilibiliReplies(
|
||||
|
||||
private fun verifyImages(snapshot: ImageView, expected: List<PhotoPixels>, readBack: () -> ImageView?): Boolean {
|
||||
val actual = ui.screenshotPhotos(snapshot.window, snapshot.regions) ?: return false
|
||||
return readBack() == snapshot && actual.size == expected.size &&
|
||||
expected.indices.all { PublishedPhotoMatcher.matches(expected[it], actual[it]) }
|
||||
return readBack() == snapshot && BilibiliReceiptEvidence.matchesOrderedPhotos(expected, actual)
|
||||
}
|
||||
|
||||
private fun imageView(page: UiPage, pictures: List<UiNode>): ImageView? {
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
package com.jieliu.app.platform
|
||||
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertNull
|
||||
import org.junit.Assert.assertSame
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
|
||||
class BilibiliReceiptEvidenceTest {
|
||||
private data class Reply(val body: String, val photos: List<PhotoPixels>)
|
||||
|
||||
private val original = CommentTarget("original", "原评论正文", "原评论作者")
|
||||
private val text = "完整的回复正文"
|
||||
private val twoPhotos = listOf(stripedPhoto(vertical = true), stripedPhoto(vertical = false))
|
||||
private val reply = Reply(text, twoPhotos)
|
||||
|
||||
@Test fun matchesExactTextAndBothImagesFromTheSameReply() {
|
||||
val selected = BilibiliReceiptEvidence.uniqueReply(
|
||||
original, original, true, text, listOf(Reply("另一条回复", emptyList()), reply), Reply::body,
|
||||
)
|
||||
|
||||
assertSame(reply, selected)
|
||||
assertTrue(BilibiliReceiptEvidence.matchesOrderedPhotos(twoPhotos, checkNotNull(selected).photos))
|
||||
}
|
||||
|
||||
@Test fun rejectsDifferentOriginalIncompleteThreadAndDuplicateBodies() {
|
||||
val differentOriginal = original.copy(key = "another-original")
|
||||
|
||||
assertNull(BilibiliReceiptEvidence.uniqueReply(original, differentOriginal, true, text, listOf(reply), Reply::body))
|
||||
assertNull(BilibiliReceiptEvidence.uniqueReply(original, original, false, text, listOf(reply), Reply::body))
|
||||
assertNull(BilibiliReceiptEvidence.uniqueReply(original, original, true, text, listOf(reply, reply), Reply::body))
|
||||
assertNull(BilibiliReceiptEvidence.uniqueReply(original, original, true, "$text!", listOf(reply), Reply::body))
|
||||
}
|
||||
|
||||
@Test fun rejectsMissingSwappedOrRepeatedImages() {
|
||||
assertFalse(BilibiliReceiptEvidence.matchesOrderedPhotos(twoPhotos, twoPhotos.take(1)))
|
||||
assertFalse(BilibiliReceiptEvidence.matchesOrderedPhotos(twoPhotos, twoPhotos.reversed()))
|
||||
assertFalse(BilibiliReceiptEvidence.matchesOrderedPhotos(twoPhotos, List(2) { twoPhotos.first() }))
|
||||
}
|
||||
|
||||
@Test fun readsExplicitThreadTotalsWithoutTreatingSortLabelsAsCounts() {
|
||||
assertEquals(2, BilibiliLabels.detailReplyTotal("相关回复共 2 条"))
|
||||
assertEquals(2, BilibiliLabels.detailReplyTotal("共2条回复"))
|
||||
assertEquals(2, BilibiliLabels.detailReplyTotal("2"))
|
||||
assertNull(BilibiliLabels.detailReplyTotal("按时间"))
|
||||
}
|
||||
|
||||
private fun stripedPhoto(vertical: Boolean): PhotoPixels {
|
||||
val size = 48
|
||||
return PhotoPixels(size, size, IntArray(size * size) { index ->
|
||||
val axis = if (vertical) index % size else index / size
|
||||
if (axis / 8 % 2 == 0) 0xffab2c1e.toInt() else 0xff2baa95.toInt()
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user