"""Keep entry scope counts tied to the catalog and reject retired narrative.""" import os from pathlib import Path import re import subprocess import sys import unittest ROOT = Path(__file__).resolve().parents[1] OLD = re.compile( r'14 ?个版本|14 versions|390 ?(道|题|questions)|300 道正式|300 formal|' r'90 道开发|本期目标|v0 是|v0 共|v0 出|首批|first batch|first-batch' ) class EntryNarrativeTests(unittest.TestCase): def test_entry_counts(self): sys.path.insert(0, str(ROOT)) from spec.questions.core.catalog_reader import CatalogReader reader = CatalogReader(catalog=ROOT / 'spec/questions/catalog.json', v0=ROOT / 'spec/questions/v0.json') status = reader.version_status_counts() # 计数只从阅读器取(K2) counts = reader.counts() expected = (counts['versions'], status.get('v0', 0) + status.get('planned', 0), status.get('candidate', 0), counts['versions_active'], counts['versions_paused']) for name in ('README.md', 'spec/README.md'): with self.subTest(file=name): text = (ROOT / name).read_text() match = re.search(r'(\d+) 个版本:目录在册 (\d+) 版、候选 (\d+) 版;按可用性现行 (\d+) 版、暂停 (\d+) 版', text) self.assertIsNotNone(match) self.assertEqual(tuple(map(int, match.groups())), expected) scope = re.search(r'(\d+) 个母题、(\d+) 个子题、(\d+) 个问法', text) self.assertIsNotNone(scope) self.assertEqual(tuple(map(int, scope.groups())), (counts['families'], counts['subtasks'], counts['forms'])) def test_entries_do_not_name_v0_deliverable(self): for name in ('README.md', 'AGENTS.md', 'spec/README.md'): with self.subTest(file=name): self.assertNotIn('v0', (ROOT / name).read_text().replace('v0.json', '')) def test_no_retired_narrative(self): try: files = subprocess.check_output( ['git', 'ls-files', '-z', '*.md'], cwd=ROOT, stderr=subprocess.DEVNULL).decode().split('\0') except (subprocess.CalledProcessError, FileNotFoundError): files = [] if not any(files): for directory, subdirs, names in os.walk(ROOT): subdirs[:] = [name for name in subdirs if name not in ('outputs', '.git')] files.extend(str((Path(directory) / name).relative_to(ROOT)) for name in names if name.endswith('.md')) checked, hits = 0, [] for name in files: if (not name or name.startswith('outputs/') or not (ROOT / name).exists()): continue checked += 1 for number, line in enumerate((ROOT / name).read_text().splitlines(), 1): # catalog 状态词合并后去掉(H runs/narrative-1021/plan.md 第 8 步)。 if name == 'spec/questions/system.md' and line.startswith('现行体系是'): line = line.replace('v0 出 14 个版本', '', 1) if OLD.search(line): hits.append(f'{name}:{number}: {line}') self.assertGreater(checked, 0) self.assertEqual(hits, [], '\n'.join(hits)) def test_no_outputs_directory(self): self.assertFalse((ROOT / 'outputs').exists()) def test_no_status_directory(self): self.assertFalse((ROOT / 'docs/status').exists()) if __name__ == '__main__': unittest.main()