import json import unittest from pathlib import Path REPO_ROOT = Path(__file__).resolve().parents[1] CRM_PACK = REPO_ROOT / "salesforce-crm-standard" INSTALL_SEED_POLICY = CRM_PACK / "runbooks" / "install_seed_policy.md" FORBIDDEN_INSTALL_HOOK_TOKENS = { "BuildNextScheduleWindowKey", "ExecutePreparedScheduledDefinition", "PrepareScheduledDefinitionForWindow", "installLifecycleHooks", "runImmediatelyOnInstall", "scheduled_flow_natural_window", } FORBIDDEN_INSTALL_HOOK_COMPONENT_KINDS = { "install_lifecycle_hook", "pack_install_hook", "scheduled_flow_install_hook", } def load_json(path: Path): with path.open(encoding="utf-8") as handle: return json.load(handle) def first_party_pack_roots(): return sorted(path.parent for path in REPO_ROOT.glob("*/pack.json")) def json_scalar_entries(value, path="$"): if isinstance(value, dict): for key, child in value.items(): yield f"{path}.{key}", key yield from json_scalar_entries(child, f"{path}.{key}") elif isinstance(value, list): for index, child in enumerate(value): yield from json_scalar_entries(child, f"{path}[{index}]") elif isinstance(value, (str, int, float, bool)) or value is None: yield path, value class InstallSeedPolicyTest(unittest.TestCase): def test_repository_readme_links_install_seed_policy(self): readme = (REPO_ROOT / "README.md").read_text(encoding="utf-8") self.assertIn("Install seed intent belongs in L1 app-pack payload", readme) self.assertIn("salesforce-crm-standard/runbooks/install_seed_policy.md", readme) def test_first_party_pack_payloads_do_not_declare_install_immediate_hooks(self): offenders = [] for pack_root in first_party_pack_roots(): for payload_path in sorted(pack_root.rglob("*.json")): payload = load_json(payload_path) for json_path, scalar in json_scalar_entries(payload): scalar_text = str(scalar) for token in FORBIDDEN_INSTALL_HOOK_TOKENS: if token in scalar_text: offenders.append( ( payload_path.relative_to(REPO_ROOT).as_posix(), json_path, token, ) ) self.assertEqual(offenders, []) def test_crm_pack_does_not_declare_install_hook_components(self): manifest = load_json(CRM_PACK / "pack.json") self.assertNotIn("installLifecycleHooks", manifest) forbidden_components = [ component for component in manifest.get("components", []) if component.get("kind") in FORBIDDEN_INSTALL_HOOK_COMPONENT_KINDS ] self.assertEqual(forbidden_components, []) def test_crm_standard_flow_manifest_identity_matches_payload_filename(self): manifest = load_json(CRM_PACK / "pack.json") standard_flows = [ component for component in manifest.get("components", []) if component.get("kind") == "standard_flow" ] self.assertGreater(len(standard_flows), 0) for component in standard_flows: with self.subTest(flow=component.get("businessKey")): payload_path = CRM_PACK / component["path"] self.assertTrue(payload_path.is_file(), payload_path) self.assertEqual(payload_path.suffixes[-2:], [".flow", ".json"]) developer_name = payload_path.name.removesuffix(".flow.json") self.assertEqual(component["businessKey"], developer_name) def test_install_seed_policy_runbook_records_the_l1_contract(self): policy = INSTALL_SEED_POLICY.read_text(encoding="utf-8") self.assertIn("This pack currently declares no install-time seed hook.", policy) self.assertIn("Install seed behavior is L1 intent.", policy) self.assertIn("pack activation is the last install step that may fail", policy) self.assertIn("idempotent and fail-forward", policy) self.assertIn("ordinary scheduled Flow metadata", policy) self.assertIn("standard_flow.businessKey", policy) self.assertIn("runImmediatelyOnInstall", policy) if __name__ == "__main__": unittest.main()