Skip to content

armgit / peach

Public Imported from GitHub
0

Peach: Local-first video metadata scraper and media manager.

test(web): core.js 的链接与路径判据改按行为验收

longmeidaoabde6a19d5b6
diff --git a/tests/test_web_js.py b/tests/test_web_js.py
index 631714b4f..983b43a31 100644
--- a/tests/test_web_js.py
+++ b/tests/test_web_js.py
@@ -258,6 +258,94 @@ class WebJsBehaviourTests(unittest.TestCase):
# ── core 里的格式化 ─────────────────────────────────────────────────────
+ # ── 链接与路径 ──────────────────────────────────────────────────────────
+
+ def test_the_link_mark_endpoint_only_ever_carries_an_id(self):
+ # 让前端把地址递给服务端去取,等于开一个任意地址抓取的口子。和
+ # `/follow-stream` 同一条规矩:服务端只认账本里已有的链接 id。
+ results = self.run_js([
+ ["core.js", "linkMarkUrl", [{"link_id": 7}]],
+ # `?? ''` 不是 `|| ''`:id 为 0 是合法的行号,不能被当成空。
+ ["core.js", "linkMarkUrl", [{"link_id": 0}]],
+ ["core.js", "linkMarkUrl", [{}]],
+ # 就算调用方把整条链接连 url 一起递进来,出去的也只有 id。
+ ["core.js", "linkMarkUrl", [{"link_id": 3, "url": "https://x.com/a"}]],
+ ])
+ self.assertEqual(results, ["/link-mark?id=7", "/link-mark?id=0",
+ "/link-mark?id=", "/link-mark?id=3"])
+ for url in results:
+ self.assertNotIn("url=", url, "外链图标端点不得接受前端给的地址")
+
+ def test_brand_marks_cover_the_host_and_its_subdomains_only(self):
+ # 416 条社媒链接里 372 条是 x.com/twitter.com,只给它一个内联标记就覆盖
+ # 89%,还省一次跨站请求。其余继续走 favicon,不为个位数的链接各配图标。
+ self.assertJsResults([
+ ("core.js", "brandIcon", ["https://x.com/remu"], "brand-x"),
+ ("core.js", "brandIcon", ["https://www.twitter.com/remu"], "brand-x"),
+ ("core.js", "brandIcon", ["https://mobile.x.com/remu"], "brand-x"),
+ # 只认后缀边界:`notx.com` 不是 `x.com` 的子域。
+ ("core.js", "brandIcon", ["https://notx.com/remu"], ""),
+ ("core.js", "brandIcon", ["https://example.com/a"], ""),
+ # 坏地址返回空串而不是抛异常——资料页的链接来自刮削,什么都可能有。
+ ("core.js", "brandIcon", ["不是网址"], ""),
+ ("core.js", "brandIcon", [None], ""),
+ ])
+
+ def test_link_host_reads_as_a_name_not_as_a_url(self):
+ # 域名当名字:厂牌页的官网链接直接显示它。`www.` 不携带信息,只占宽度。
+ self.assertJsResults([
+ ("core.js", "linkHost", ["https://www.example.com/a?b=1"], "EXAMPLE.COM"),
+ ("core.js", "linkHost", ["https://Sub.Example.co.jp/"], "SUB.EXAMPLE.CO.JP"),
+ ("core.js", "linkHost", ["不是网址"], ""),
+ ])
+
+ def test_favicon_urls_prefer_the_pinned_ones_and_fail_soft(self):
+ self.assertJsResults([
+ # 几个站点的 favicon 路径是固定住的,取不到默认那张。
+ ("core.js", "faviconUrl", ["https://kemono.cr/user/1"],
+ "https://kemono.cr/assets/favicon-CPB6l7kH.ico"),
+ ("core.js", "faviconUrl", ["https://www.kemono.cr/user/1"],
+ "https://kemono.cr/assets/favicon-CPB6l7kH.ico"),
+ ("core.js", "faviconUrl", ["https://example.com/deep/page"],
+ "https://example.com/favicon.ico"),
+ ("core.js", "faviconUrl", ["不是网址"], ""),
+ ("core.js", "faviconFallbackUrl", ["a b.com"],
+ "https://www.google.com/s2/favicons?domain=a%20b.com&sz=64"),
+ ])
+
+ def test_entity_paths_are_semantic_and_survive_slashes_in_names(self):
+ # 女优名字里有斜杠,不编码会把路径切成多段。
+ self.assertJsResults([
+ ("core.js", "entityPath", ["performer", "波多野結衣"],
+ "/performers/%E6%B3%A2%E5%A4%9A%E9%87%8E%E7%B5%90%E8%A1%A3"),
+ ("core.js", "entityPath", ["performer", "A/B"], "/performers/A%2FB"),
+ ("core.js", "entityPath", ["studio", "S1"], "/studios/S1"),
+ ("core.js", "entityPath", ["series", "X"], "/series/X"),
+ # 没登记的种类原样当路径段用,不静默变成 undefined。
+ ("core.js", "entityPath", ["新种类", "X"], "/新种类/X"),
+ ])
+
+ def test_only_the_home_and_state_routes_count_as_catalog(self):
+ # 「这条路径算不算馆藏列表」决定了状态标题、筛选栏和高亮;管理页不算。
+ self.assertJsResults([
+ ("core.js", "isCatalogPath", ["/"], True),
+ ("core.js", "isCatalogPath", ["/unseen"], True),
+ ("core.js", "isCatalogPath", ["/junk-files"], True),
+ ("core.js", "isCatalogPath", ["/stats"], False),
+ ("core.js", "isCatalogPath", ["/trash"], False),
+ # 原型链上的属性名不许算命中——这就是它用 hasOwnProperty 的原因。
+ ("core.js", "isCatalogPath", ["constructor"], False),
+ ("core.js", "isCatalogPath", ["toString"], False),
+ ])
+
+ def test_folded_names_compare_across_width_case_and_padding(self):
+ # 别名比对用它:全角、大小写、首尾空白都不该让同一个名字变成两个。
+ self.assertJsResults([
+ ("core.js", "foldName", [" Remu "], "remu"),
+ ("core.js", "foldName", ["REMU"], "remu"),
+ ("core.js", "foldName", [None], ""),
+ ])
+
def test_the_minus_one_duration_sentinel_never_becomes_a_duration(self):
"""账本里的 `-1` 是探测硬失败的哨兵,不是时长。
diff --git a/tests/test_web_ui.py b/tests/test_web_ui.py
index 7199ab8e3..09d055501 100644
--- a/tests/test_web_ui.py
+++ b/tests/test_web_ui.py
@@ -513,10 +513,11 @@ class WebUiSourceTests(unittest.TestCase):
self.app_js[anchor:anchor + 260],
"资料页外链 favicon 必须带 no-referrer")
- def test_the_link_mark_endpoint_takes_a_link_id_not_a_url(self):
+ def test_no_caller_ever_hands_the_link_mark_endpoint_a_url(self):
# 让前端把地址递给服务端去取,等于开一个任意地址抓取的口子。和 `/follow-stream`
- # 同一条规矩:服务端只取账本里已有的地址。
- self.assertPageContains("const linkMarkUrl=link=>`/link-mark?id=")
+ # 同一条规矩:服务端只取账本里已有的地址。`linkMarkUrl` 自己只吐 id 由
+ # test_web_js.test_the_link_mark_endpoint_only_ever_carries_an_id 验收;
+ # 这里守的是「没人绕过它另写一个带地址的调用」。
self.assertPageLacks("/link-mark?url=", "外链图标端点不得接受前端给的地址")
def test_social_links_show_only_the_platform_mark_not_the_handle(self):
@@ -546,7 +547,8 @@ class WebUiSourceTests(unittest.TestCase):
# favicon 是别人服务器上的一张小位图:X 直接挡掉爬取(资料页上那个空白白圆就是
# 它),取到的也多是 16×16,放进 32px 的圆里必然糊。416 条社媒链接里 372 条是
# x.com/twitter.com,只给它一个内联标记就覆盖了 89%,还省一次跨站请求。
- self.assertPageContains("const BRAND_ICONS=[[['x.com','twitter.com'],'brand-x']];")
+ # 哪些主机走内联标记(含子域、且只认后缀边界)由
+ # test_web_js.test_brand_marks_cover_the_host_and_its_subdomains_only 验收。
self.assertPageContains('<symbol id="i-brand-x"')
# 填充字形不吃通用的 stroke:currentColor;fill:none。
self.assertPageContains('.entitylinkicon.brand svg{width:15px;height:15px;fill:currentColor;stroke:none}')
@@ -1544,7 +1546,6 @@ class WebUiSourceTests(unittest.TestCase):
self.assertPageContains("'simpcity.cr':'https://simpcity.cr/data/assets/logo/favicon.png'")
self.assertPageContains("'hanime1.me':'https://vdownload.hembed.com/image/icon/tab_logo.png")
self.assertPageContains("'kemono.cr':'https://kemono.cr/assets/favicon-CPB6l7kH.ico'")
- self.assertPageContains("const faviconFallbackUrl=domain=>`https://www.google.com/s2/favicons")
self.assertPageLacks("negative_tags")
self.assertPageContains(".tastehero{margin-bottom:16px}")
self.assertPageContains(".tasteranks{display:grid;grid-template-columns:repeat(3")
@@ -2482,7 +2483,6 @@ class WebUiSourceTests(unittest.TestCase):
self.assertPageContains(".followfilters .sep{flex:none;width:1px;height:24px;background:var(--border-15)")
def test_entity_profile_uses_logo_links_without_a_redundant_back_row(self):
- self.assertPageContains("const faviconUrl=url=>")
self.assertPageContains('class="entitylinkicon"')
self.assertPageContains('class="entitylinklabel"')
# favicon 取不到就把 <img> 摘掉,露出底下的 globe 图标;这条兜底由