test(gen-script): lock in to_rule() inline-# handling with regression tests
CI / lite (push) Successful in 3s

新增 2 个 test method 覆盖 b4a4769 修复的边界 case:
- test_to_rule_strips_inline_hash_comment_before_classifying_no_resolve
  锁定带 inline # 注释的条目(本次修复的真实场景)
  + 验证注释里出现 'no-resolve' / ',DIRECT' 字符串不被误处理
- test_to_rule_preserves_canonical_forms
  锁定 happy path(纯规则、带 no-resolve、带行尾空白)

为让 test_checked_in_router_projections_match_the_rule_source 通过,
同步重生成 router/direct-plain.txt 和 router/direct-rules.yaml。
这俩投影文件自 907fe9d 起就 stale 14+ 条规则(国内 LLM、UGREEN、
Steam 段全没同步),本次连同 b4a4769 的修复一起补齐。

测试结果:python3 -m unittest discover -s tests
  Ran 9 tests in 0.028s — OK
This commit is contained in:
ao gong
2026-09-05 13:40:50 +08:00
parent b4a4769dcf
commit 112374e2a1
3 changed files with 66 additions and 0 deletions
+36
View File
@@ -203,6 +203,42 @@ class ModuleSpecTests(unittest.TestCase):
"PROCESS-NAME,OneDrive.exe,DIRECT",
)
def test_to_rule_strips_inline_hash_comment_before_classifying_no_resolve(self) -> None:
# 回归测试 (b4a4769):to_rule() 在判断 entry.endswith(',no-resolve') 之前
# 没有剥掉行尾 # 注释,导致带 inline 注释的 Akamai Steam CDN 规则
# 走错分支,把注释当成 entry 的一部分拼进去,生成 mihomo 解析失败的规则。
self.assertEqual(
to_rule("IP-CIDR,162.254.192.0/18,no-resolve # Akamai Steam CDN 主段"),
"IP-CIDR,162.254.192.0/18,DIRECT,no-resolve",
)
# 注释里出现 "no-resolve" 不能误判为 suffix
self.assertEqual(
to_rule("DOMAIN-SUFFIX,example.com # 走 no-resolve 路径"),
"DOMAIN-SUFFIX,example.com,DIRECT",
)
# 注释里出现 ",DIRECT" 不能误拼到 proxy 字段
self.assertEqual(
to_rule("IP-CIDR,10.0.0.0/8 # 不是 DIRECT, 是 REJECT"),
"IP-CIDR,10.0.0.0/8,DIRECT",
)
def test_to_rule_preserves_canonical_forms(self) -> None:
# 不带注释、不带 no-resolve
self.assertEqual(
to_rule("DOMAIN-SUFFIX,bilibili.com"),
"DOMAIN-SUFFIX,bilibili.com,DIRECT",
)
# 带 no-resolve、无注释
self.assertEqual(
to_rule("IP-CIDR,127.0.0.0/8,no-resolve"),
"IP-CIDR,127.0.0.0/8,DIRECT,no-resolve",
)
# 带 no-resolve + 行尾空白
self.assertEqual(
to_rule("IP-CIDR,127.0.0.0/8,no-resolve "),
"IP-CIDR,127.0.0.0/8,DIRECT,no-resolve",
)
def test_clash_verge_script_adds_reachable_direct_dns_fallbacks(self) -> None:
script = render_script(["PROCESS-NAME,OneDrive.exe"])