merge: W4 — 概览页「按路径」分组折叠(可读性)

This commit is contained in:
AskaEth committed 2026-10-04 18:09:18 +08:00
commit a6b4bae2a0
3 files changed
+562 -2

No files matched your search

@@ -0,0 +1,275 @@
package top.yeij.cyrene.domain.admin
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test
/**
* 「按路径」分组与折叠模型(C-2 概览页)。
*
* `realPerPath` 是**真实响应体**里的 `perPath`(26 条,与 `AdminDtoRealPayloadTest` 同一份样本),
* 不是编造的样例——分组规则必须在真实数据上成立才算数。
*/
class AdminPathStatsTest {
private val realPerPath = mapOf(
"/" to 2L,
"/api/v1/admin/automations" to 17L,
"/api/v1/admin/backthink" to 1L,
"/api/v1/admin/channels" to 30L,
"/api/v1/admin/iot" to 34L,
"/api/v1/admin/llm" to 18L,
"/api/v1/admin/llm/catalog" to 2L,
"/api/v1/admin/memories" to 21L,
"/api/v1/admin/observability/metrics" to 24L,
"/api/v1/admin/observability/trace" to 1L,
"/api/v1/admin/personas" to 31L,
"/api/v1/admin/plugins" to 23L,
"/api/v1/admin/plugins/installed" to 17L,
"/api/v1/admin/rag" to 17L,
"/api/v1/admin/sessions" to 30L,
"/api/v1/admin/users" to 17L,
"/api/v1/auth/login" to 5L,
"/api/v1/health" to 27L,
"/api/v1/meta/enums" to 2L,
"/api/v1/personas" to 1L,
"/api/v1/sessions" to 1L,
"/api/v1/sessions/web/messages" to 1L,
"/assets/index-DrDuaeXG.js" to 1L,
"/assets/index-SA9l26Q_.css" to 1L,
"/sw.js" to 1L,
"/ws/chat" to 1L,
)
// ------------------------------------------------------------------
// 空输入 / 单条
// ------------------------------------------------------------------
@Test
fun `empty input yields no groups and no rows`() {
assertTrue(PathStats.groupPaths(emptyMap()).isEmpty())
assertTrue(PathStats.rows(emptyList()).isEmpty())
}
@Test
fun `a single path is one group and renders as a headerless row`() {
val groups = PathStats.groupPaths(mapOf("/api/v1/health" to 7L))
assertEquals(1, groups.size)
assertEquals("/health", groups[0].prefix)
assertEquals(1, groups[0].pathCount)
assertEquals(7L, groups[0].hits)
// 只有一条路径的组不产生组头:组头与内容完全重复,只会让列表变长。
assertEquals(listOf(PathRow.Leaf(PathHit("/api/v1/health", 7L, "/api/v1/health"), false)), PathStats.rows(groups))
}
// ------------------------------------------------------------------
// 分组规则(无路径清单)
// ------------------------------------------------------------------
@Test
fun `grouping keys off the first segment below the api base, not a hardcoded route list`() {
val groups = PathStats.groupPaths(
mapOf(
"/api/v1/quantum/entangle" to 3L, // 客户端不认识的新路由:照样自动成组
"/api/v1/admin/automations" to 1L,
),
)
assertEquals(listOf("/quantum", "/admin"), groups.map { it.prefix })
}
@Test
fun `api base prefix is stripped for grouping only, the row keeps the full path`() {
val group = PathStats.groupPaths(mapOf("/api/v1/admin/llm/catalog" to 2L)).single()
assertEquals("/admin", group.prefix)
assertEquals("/api/v1/admin/llm/catalog", group.paths.single().path)
}
@Test
fun `a path that only looks like the base is not stripped`() {
// 逐段比较:`/api/v1x/...` 不是 `/api/v1` 下的路径,不能被剥成 `/x`。
assertEquals("/api", PathStats.groupPaths(mapOf("/api/v1x/foo" to 1L)).single().prefix)
// 基址本身(没有更深的一段)自成一档,而不是落进空字符串。
assertEquals("/api/v1", PathStats.groupPaths(mapOf("/api/v1" to 1L)).single().prefix)
}
@Test
fun `root and non-api paths each get their own group`() {
val groups = PathStats.groupPaths(
mapOf(
"/" to 2L,
"" to 1L,
"/assets/index-DrDuaeXG.js" to 1L,
"/assets/index-SA9l26Q_.css" to 1L,
"/sw.js" to 1L,
"/ws/chat" to 1L,
),
)
assertEquals(listOf("/", "/assets", "/sw.js", "/ws"), groups.map { it.prefix })
assertEquals(2, groups.first { it.prefix == "/assets" }.pathCount)
assertEquals(2, groups.first { it.prefix == "/" }.pathCount)
}
// ------------------------------------------------------------------
// 真实 26 条
// ------------------------------------------------------------------
@Test
fun `real payload groups into ten prefixes without losing a path or a hit`() {
val groups = PathStats.groupPaths(realPerPath)
assertEquals(
listOf("/admin", "/health", "/auth", "/", "/assets", "/meta", "/sessions", "/personas", "/sw.js", "/ws"),
groups.map { it.prefix },
)
assertEquals(26, groups.sumOf { it.pathCount })
// 与同一份真实响应里的 totalRequests 一致:分组既不吞路径也不重算次数。
assertEquals(326L, groups.sumOf { it.hits })
assertEquals(15, groups.first().pathCount)
assertEquals(283L, groups.first().hits)
}
@Test
fun `groups are ordered by hits, ties by prefix`() {
val groups = PathStats.groupPaths(realPerPath)
val hits = groups.map { it.hits }
assertEquals(hits.sortedDescending(), hits)
// 并列的 2 次:按前缀升序,保证刷新前后顺序稳定。
assertEquals(listOf("/", "/assets", "/meta", "/sessions"), groups.filter { it.hits == 2L }.map { it.prefix })
}
@Test
fun `paths inside a group are ordered by count, ties by path`() {
val admin = PathStats.groupPaths(realPerPath).first().paths
val counts = admin.map { it.count }
assertEquals(counts.sortedDescending(), counts)
assertEquals(
listOf(
"/api/v1/admin/automations",
"/api/v1/admin/plugins/installed",
"/api/v1/admin/rag",
"/api/v1/admin/users",
),
admin.filter { it.count == 17L }.map { it.path },
)
assertEquals("/api/v1/admin/iot", admin.first().path)
}
// ------------------------------------------------------------------
// 长路径的展示文本
// ------------------------------------------------------------------
@Test
fun `deep paths collapse the middle and keep the tail`() {
assertEquals("…/admin/observability/metrics", PathStats.displayPath("/api/v1/admin/observability/metrics"))
assertEquals("…/sessions/web/messages", PathStats.displayPath("/api/v1/sessions/web/messages"))
}
@Test
fun `paths within the segment budget are rendered verbatim`() {
// 不归一化:尾斜杠、文件名里的点、问号都原样留着(键名就是后端给的原串)。
assertEquals("/api/v1/auth/login", PathStats.displayPath("/api/v1/auth/login"))
assertEquals("/assets/index-DrDuaeXG.js", PathStats.displayPath("/assets/index-DrDuaeXG.js"))
assertEquals("/sw.js", PathStats.displayPath("/sw.js"))
assertEquals("/", PathStats.displayPath("/"))
assertEquals("", PathStats.displayPath(""))
}
@Test
fun `collapsing never makes two sibling paths look identical`() {
val a = PathStats.displayPath("/api/v1/admin/observability/metrics")
val b = PathStats.displayPath("/api/v1/admin/observability/trace")
assertTrue(a, a != b)
assertTrue(a, a.endsWith("/metrics"))
assertTrue(b, b.endsWith("/trace"))
}
@Test
fun `display text is wired into the grouped rows`() {
val hit = PathStats.groupPaths(realPerPath)
.flatMap { it.paths }
.first { it.path == "/api/v1/admin/observability/metrics" }
assertEquals("…/admin/observability/metrics", hit.display)
}
// ------------------------------------------------------------------
// 折叠状态
// ------------------------------------------------------------------
@Test
fun `big groups collapse by default, small ones expand`() {
val groups = PathStats.groupPaths(realPerPath)
val admin = groups.first { it.prefix == "/admin" }
assertFalse(admin.expandedByDefault)
assertTrue(groups.first { it.prefix == "/assets" }.expandedByDefault)
// 阈值边界:恰好 [PathStats.COLLAPSE_THRESHOLD] 条仍展开,多一条就收起。
val atThreshold = group("/t", (1..PathStats.COLLAPSE_THRESHOLD).map { "/api/v1/t/p$it" })
val aboveThreshold = group("/t", (1..PathStats.COLLAPSE_THRESHOLD + 1).map { "/api/v1/t/p$it" })
assertTrue(atThreshold.expandedByDefault)
assertFalse(aboveThreshold.expandedByDefault)
}
@Test
fun `default rows show only the header of a collapsed group`() {
val rows = PathStats.rows(PathStats.groupPaths(realPerPath))
// 15 条 admin 路径 + 其余散条都在,但首屏只有组头承载它们。
assertEquals(14, rows.size)
assertEquals(PathRow.Header(PathStats.groupPaths(realPerPath).first()), rows.first())
assertEquals(1, rows.count { it is PathRow.Header && it.group.prefix == "/admin" })
assertTrue(rows.none { it is PathRow.Leaf && it.hit.path.startsWith("/api/v1/admin/") })
// 小分组默认展开:组头之后紧跟它自己的子行。
val assetsHeader = rows.indexOfFirst { it is PathRow.Header && it.group.prefix == "/assets" }
assertTrue(rows[assetsHeader + 1] is PathRow.Leaf)
assertEquals(listOf(true, true), (assetsHeader + 1..assetsHeader + 2).map { (rows[it] as PathRow.Leaf).nested })
}
@Test
fun `explicit toggles win over the default in both directions`() {
val groups = PathStats.groupPaths(realPerPath)
val admin = groups.first { it.prefix == "/admin" }
val assets = groups.first { it.prefix == "/assets" }
// 收起的大组被点开:组头之后紧跟着它的 [admin.pathCount] 条子行(缩进)。
val opened = PathStats.rows(groups, mapOf("/admin" to true))
val adminHeader = opened.indexOfFirst { it is PathRow.Header && it.group.prefix == "/admin" }
val children = opened.subList(adminHeader + 1, adminHeader + 1 + admin.pathCount)
assertEquals(admin.paths.map { it.path }, children.map { (it as PathRow.Leaf).hit.path })
assertTrue(children.all { it is PathRow.Leaf && it.nested })
assertEquals("/api/v1/admin/iot", (opened[adminHeader + 1] as PathRow.Leaf).hit.path)
// 默认展开的小组被收起:只剩组头。
val closed = PathStats.rows(groups, mapOf("/assets" to false))
assertTrue(closed.none { it is PathRow.Leaf && it.hit.path.startsWith("/assets/") })
assertTrue(PathStats.expanded(assets, mapOf("/assets" to false)).not())
}
@Test
fun `rows keep the group order and never drop a path when everything is expanded`() {
val groups = PathStats.groupPaths(realPerPath)
val toggled = groups.associate { it.prefix to true }
val rows = PathStats.rows(groups, toggled)
// 只有多路径的组才有组头(单条组直接是一行路径),全部展开时行数 = 组头 + 全部路径。
val multiPathGroups = groups.filter { it.pathCount > 1 }
assertEquals(multiPathGroups.size + 26, rows.size)
assertEquals(
multiPathGroups.map { it.prefix },
rows.filterIsInstance<PathRow.Header>().map { it.group.prefix },
)
}
private fun group(prefix: String, paths: List<String>): PathGroup =
PathStats.groupPaths(paths.associateWith { 1L }).single().also { assertEquals(prefix, it.prefix) }
}