#!/usr/bin/env python3 import importlib.util import json from pathlib import Path import subprocess import sys import tempfile import unittest SCRIPT = Path(__file__).with_name('tcs_mother_root_agent.py') ROOT = SCRIPT.parents[2] SPEC = importlib.util.spec_from_file_location('tcs_root_agent', SCRIPT) MODULE = importlib.util.module_from_spec(SPEC) SPEC.loader.exec_module(MODULE) class RootAgentTest(unittest.TestCase): def test_worktree_map_has_valid_roots(self): MODULE.validate_root(json.loads((ROOT / 'routing/tcs-mother-root-dynamic-navigation-map.json').read_text())) def test_old_and_quarantine_paths_fail(self): for value in ['../escape', 'routing/../../escape', 'WORK-工作区/guanghu-ice-heart/map.json', 'isolation/old/map.json']: with self.assertRaises(MODULE.RootError): MODULE.safe_relative(value) def test_committed_refresh_status_resolve_and_history_reject(self): with tempfile.TemporaryDirectory() as temp: base = Path(temp) output, pointer = base / 'state', base / 'TCS-ROOT.json' refresh = MODULE.refresh(ROOT, output, pointer, 'test') self.assertEqual(refresh['outcome'], 'PASS') self.assertEqual(MODULE.status(ROOT, output, pointer)['state'], 'TCS_ROOT_CURRENT_VERIFIED') self.assertEqual(MODULE.resolve(output, 'TCS-0002∞', None)['result']['object_kind'], 'GUANGHU_HUMAN_TEAM_BODY_ROOT') self.assertEqual(MODULE.resolve(output, 'TCS-0002', 'GUANGHU_HUMAN_TEAM_BODY_ROOT')['state'], 'TYPED_ALIAS_RESOLVED') self.assertEqual(MODULE.resolve(output, 'GLW-SYS-0001', None)['state'], 'HISTORY_ONLY_NO_CURRENT_ROUTE') self.assertEqual(MODULE.resolve(output, 'CH-LIGHT-ARRIVAL-SKILL-0001', 'WORLD_SKILL_CONTRIBUTION_CHANNEL')['result']['route'], 'gls/light-arrivals/SKILL-CONTRIBUTION-CHANNEL.hdlp') self.assertEqual(MODULE.resolve(output, 'SYS-GLW-LTH-SKILL-0001', None)['result']['object_kind'], 'LIGHTHOUSE_SKILL_MODULE_ZONE') again = MODULE.refresh(ROOT, output, pointer, 'test') self.assertEqual(again['state'], 'CURRENT_IDEMPOTENT') def test_unadmitted_numbered_source_drift_preserves_previous_current(self): with tempfile.TemporaryDirectory() as temp: base = Path(temp) output, pointer = base / 'state', base / 'TCS-ROOT.json' snapshot = MODULE.build(ROOT) snapshot['registered_source_hashes']['identity/fifth-domain-subject-registry.json'] = '0' * 64 previous_for_hash = dict(snapshot) previous_for_hash.pop('freshness_token') snapshot['freshness_token'] = MODULE.digest(MODULE.stable(previous_for_hash)) output.mkdir(parents=True) current = output / 'CURRENT.json' current.write_text(json.dumps(snapshot), encoding='utf-8') before = current.read_bytes() with self.assertRaisesRegex(MODULE.RootError, 'UNADMITTED_NUMBERED_SOURCE_DRIFT'): MODULE.refresh(ROOT, output, pointer, 'negative-test') self.assertEqual(current.read_bytes(), before) def test_ordinary_commit_equivalent_sources_do_not_advance_current(self): with tempfile.TemporaryDirectory() as temp: base = Path(temp) output, pointer = base / 'state', base / 'TCS-ROOT.json' first = MODULE.refresh(ROOT, output, pointer, 'test') current = json.loads((output / 'CURRENT.json').read_text()) same = MODULE.refresh(ROOT, output, pointer, 'ordinary-commit-test') self.assertIn(same['state'], ['CURRENT_IDEMPOTENT', 'CURRENT_NUMBER_NAV_UNCHANGED']) self.assertEqual(json.loads((output / 'CURRENT.json').read_text())['freshness_token'], current['freshness_token']) if __name__ == '__main__': unittest.main()