import hashlib import json import sys import tempfile import unittest from pathlib import Path ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT)) sys.path.insert(0, str(ROOT / "src")) from platforms import joinquant_strategy, qmt_builtin_strategy from tools.bundle_platforms import bundle_all, effective_platform_config from tools.capability_probe import probe_capabilities class BundleTest(unittest.TestCase): def test_source_wrappers_match_canonical_baseline_config(self): baseline = json.loads( (ROOT / "configs/baseline.json").read_text(encoding="utf-8") ) self.assertEqual( joinquant_strategy.CONFIG, effective_platform_config(baseline, "joinquant"), ) self.assertEqual( qmt_builtin_strategy.CONFIG, effective_platform_config(baseline, "qmt_builtin"), ) def test_bundles_inline_same_core_compile_and_match_manifest(self): with tempfile.TemporaryDirectory() as temp: output = Path(temp) manifest = bundle_all(ROOT, output) manifest_file = output / "bundle_manifest.json" stored = json.loads(manifest_file.read_text(encoding="ascii")) for name, filename in { "joinquant": "joinquant_strategy.py", "qmt_builtin": "qmt_builtin_strategy.py", }.items(): artifact = output / filename payload = artifact.read_bytes() compile(payload, str(artifact), "exec") self.assertNotIn(b"from quant60.portable_core import", payload) self.assertEqual( hashlib.sha256(payload).hexdigest(), stored["artifacts"][name]["output_sha256"], ) (output / "qmt_builtin_strategy.py").read_bytes().decode("ascii") self.assertEqual( stored["artifacts"]["joinquant"]["core"], "src/quant60/portable_core.py", ) self.assertNotIn( "/Users/", json.dumps(stored, ensure_ascii=True) ) self.assertEqual( stored["portable_core_sha256"], manifest["portable_core_sha256"], ) self.assertEqual(stored["baseline_config"], "configs/baseline.json") self.assertEqual( stored["baseline_config_sha256"], hashlib.sha256( (ROOT / "configs/baseline.json").read_bytes() ).hexdigest(), ) def test_committed_dist_is_byte_for_byte_fresh(self): with tempfile.TemporaryDirectory() as temp: output = Path(temp) bundle_all(ROOT, output) for name in ( "joinquant_strategy.py", "qmt_builtin_strategy.py", "bundle_manifest.json", ): self.assertEqual( (ROOT / "dist" / name).read_bytes(), (output / name).read_bytes(), f"committed dist artifact is stale: {name}", ) def test_joinquant_bundle_ignores_host_shadowed_sum_name(self): with tempfile.TemporaryDirectory() as temp: output = Path(temp) bundle_all(ROOT, output) source = (output / "joinquant_strategy.py").read_text( encoding="utf-8" ) namespace = {"sum": lambda values: values} exec(compile(source, "joinquant_strategy.py", "exec"), namespace) weights = namespace["capped_target_weights"]( { "600000.XSHG": 3.0, "000001.XSHE": 2.0, "300750.XSHE": 1.0, }, max_weight=0.4, gross_target=0.95, ) self.assertAlmostEqual( namespace["_sum_numeric"](weights.values()), 0.95, ) quantities = namespace["target_quantities"]( weights, {symbol: 10.0 for symbol in weights}, equity=1_000_000, cash_buffer=0.02, ) self.assertTrue(all(value >= 0 for value in quantities.values())) class CapabilityProbeTest(unittest.TestCase): def test_shallow_probe_does_not_claim_external_verification(self): report = probe_capabilities(ROOT, deep=False) self.assertTrue(report["artifacts"]["portable_core"]) self.assertTrue(report["artifacts"]["colab_notebook"]) self.assertTrue(report["artifacts"]["jqdata_snapshot_adapter"]) self.assertTrue(report["artifacts"]["tushare_qlib_adapter"]) self.assertTrue(report["artifacts"]["tushare_qlib_cli"]) self.assertTrue(report["artifacts"]["qmt_shadow_planner"]) self.assertTrue(report["artifacts"]["qmt_shadow_cli"]) self.assertIn("no external platform", report["verification_boundary"]) self.assertTrue(report["matrix"]["xttrader"]["safe_default"]) self.assertIn("rejects BJ", report["matrix"]["xttrader"]["market_scope"]) self.assertTrue(report["matrix"]["tushare_local_qlib"]["safe_default"]) for probe in report["optional_runtimes"].values(): self.assertIs(probe["import_ok"], None) if __name__ == "__main__": unittest.main()