"""Guard the revision scope, the derived rationale and the declared same-site shell rules.""" from pathlib import Path from collections import Counter import json import unittest ROOT = Path(__file__).resolve().parents[1] REVISIONS = ROOT / 'definitions/annotation-revisions' MATERIALS = ROOT / 'definitions/materials/primary-content-20261103' XUEQIU = ['article-009', 'article-044', 'article-101', 'article-102', 'article-103', 'article-104'] QUANTA = ['article-064', 'article-091'] XUEQIU_SHELL = {'首页', '下载App', '登录', '搜索', '热门话题', '热股榜', '沪深', '港股', '美股', '发布', '最热', '最新', '最早', '投诉', '热门基金', '年化', '年化(近5年)', '常见问题', '关于雪球', '投资者教育', '营业执照', '侵权投诉指引', '出版物经营许可证', 'XUEQIU.COM', '北京雪球信息科技有限公司', '更多', '全球', '收藏', '转发', '赞', '关注'} QUANTA_NAV = {'Physics', 'Mathematics', 'Biology', 'Computer Science', 'Topics', 'Archive'} def suite_dirs(): registry = json.loads((ROOT / 'definitions/registry.json').read_text()) return {row['id']: ROOT / 'definitions' / row.get('directory', row['id']) for row in registry['suites']} def cases(path): return {c['id']: c for c in json.loads(path.read_text())['cases']} def anchor_map(task): return {a['id']: a['alternatives'] for a in (task.get('acceptance_contract') or {}).get('stable_text_anchors', [])} class ReviewScopeTests(unittest.TestCase): @classmethod def setUpClass(cls): cls.rationale = json.loads((REVISIONS / 'primary-content-20261103-rationale.json').read_text()) cls.cases = {(c['suite'], c['case_id']): c for c in cls.rationale['cases']} dirs = suite_dirs() cls.parents = {s: cases(dirs[s] / 'tasks.json') for s in dirs} cls.revised = {s: cases(MATERIALS / s / 'dataset.json') for s in dirs} def test_scope_and_population(self): self.assertEqual(len(self.cases), 72) self.assertEqual(self.rationale['changed_count'], 72) self.assertEqual(Counter(s for s, _ in self.cases), { 'articles': 37, 'access-restrictions': 14, 'dynamic-content': 6, 'popular-sites': 12, 'webfetch': 3}) population = json.loads((ROOT / 'definitions/populations/primary-content-20261103.json').read_text()) self.assertLessEqual(set(self.cases), {(c['suite'], c['id']) for c in population['cases']}) def test_removed_requirements_match_the_data(self): for (suite, cid), case in self.cases.items(): before = anchor_map(self.parents[suite][cid]) after = anchor_map(self.revised[suite][cid]) expected = [{'id': i, 'text': before[i]} for i in before if i not in after] self.assertEqual(case['removed'], expected, cid) for row in case['removed']: self.assertNotIn(row['id'], after, cid) def test_binding_repairs_are_explicit(self): repairs = {c for (s, c), case in self.cases.items() if 'binding_repair' in case} self.assertEqual(repairs, {'live-041-python-programming-language', 'live-063-navigation-menu', 'live-069-navigation-menu'}) for (suite, cid), case in self.cases.items(): if cid in repairs: self.assertEqual(case['removed'], []) self.assertIn('Binding repair only', case['reason']) for region in case['binding_repair']['regions']: self.assertNotEqual(region['old_text_binding'], region['new_text_binding']) def test_every_case_has_reason_and_hash_bound_source_or_repair(self): for case in self.cases.values(): self.assertTrue(case['reason'].strip()) if 'source' in case: self.assertRegex(case['source']['sha256'], r'^[0-9a-f]{64}$') def test_full_answer_and_counterfactual_controls(self): controls = [c for c in self.cases.values() if 'full_answer_control' in c] self.assertEqual(len(controls), 66) for case in controls: self.assertTrue(case['full_answer_control']['complete_answer_passed']) self.assertFalse(case['full_answer_control']['access_denied_page_passed']) self.assertEqual(case['counterfactual_control']['results'], { 'clean_before': False, 'clean_after': True, 'shell_before': False, 'shell_after': False}) def test_authored_content_counterexamples_remain_required(self): expected = { 'article-025': ['stable-7'], 'article-095': ['stable-21'], 'article-027': ['stable-28'], 'article-028': ['stable-16'], 'article-066': ['stable-37'], 'article-067': ['stable-12'], 'article-076': ['stable-8'], 'article-093': ['stable-' + str(i) for i in range(20, 26)], 'article-009': ['stable-6'], } for cid, ids in expected.items(): self.assertLessEqual(set(ids), set(anchor_map(self.revised['articles'][cid])), cid) self.assertIn('phase1-stable-f25f4853e0d1', anchor_map(self.revised['dynamic-content']['dynamic-096-ar-real-cnn-world'])) def test_same_site_shell_rules_use_body_occurrence(self): for cid in XUEQIU + QUANTA: case = self.revised['articles'][cid] body = (case.get('ground_truth_main_content') or '').casefold() labels = {x for alternatives in anchor_map(case).values() for x in alternatives} removed = {x for row in self.cases[('articles', cid)]['removed'] for x in row['text']} shell = XUEQIU_SHELL if cid in XUEQIU else QUANTA_NAV if cid == 'article-101': # profile header and post tabs are the requested collection shell = shell - {'关注', '收藏'} for label in labels & shell: self.assertIn(label.casefold(), body, (cid, label)) if body: for label in removed: self.assertNotIn(label.casefold(), body, (cid, label)) self.assertIn('不要求提取以下界面控件或页面通用区域的文字', case['task_prompt']) quanta = {cid: {x for v in anchor_map(self.revised['articles'][cid]).values() for x in v} for cid in QUANTA} self.assertEqual(quanta['article-064'] & QUANTA_NAV, {'Mathematics'}) self.assertEqual(quanta['article-091'] & QUANTA_NAV, {'Physics', 'Mathematics', 'Topics', 'Computer Science'}) self.assertEqual(set(self.rationale['rules']), {'xueqiu-shell-v1', 'quanta-shell-v1'})