from __future__ import annotations import unittest from scripts.audit_final_pack_metadata import ( duplicate_key_prefixes, validate_layout_required_create_inputs, validate_active_formula_retention, validate_campaign_member_lead_lookup_contract, validate_campaign_member_lead_conversion_relationships, validate_native_file_upload_field_updates, validate_quote_record_type_contract, ) class FinalPackKeyPrefixPropertyTest(unittest.TestCase): def test_distinct_declared_prefixes_satisfy_the_property(self) -> None: objects = [ {"apiName": "Account", "keyPrefix": "001"}, {"apiName": "Contact", "keyPrefix": "003"}, {"apiName": "RuntimeOnly"}, ] self.assertEqual(duplicate_key_prefixes(objects), {}) def test_shared_declared_prefix_is_a_counterexample(self) -> None: objects = [ {"apiName": "First__c", "keyPrefix": "a00"}, {"apiName": "Second__c", "keyPrefix": "a00"}, ] self.assertEqual( duplicate_key_prefixes(objects), {"a00": ["First__c", "Second__c"]}, ) class NativeFileUploadFieldUpdatesContractTest(unittest.TestCase): def test_runtime_file_upload_keeps_only_business_transition_fields(self) -> None: errors: list[str] = [] validate_native_file_upload_field_updates( "Order UploadArchivedOrder", {"Status": "Activated", "Archived__c": True}, "Activated", errors, ) self.assertEqual(errors, []) def test_compatibility_url_dual_write_is_rejected(self) -> None: errors: list[str] = [] validate_native_file_upload_field_updates( "Contract UploadArchivedContract", { "Status": "Effective", "Archived__c": True, "StandardUploadUrl__c": "{downloadUrl}", }, "Effective", errors, ) self.assertEqual( errors, [ "Contract UploadArchivedContract must not persist " "StandardUploadUrl__c for native Runtime Files uploads" ], ) def _active_formula_field() -> dict: return { "apiName": "Annual_Value__c", "type": "formula", "formulaMode": "active", "formula": "Monthly_Value__c * 12", "returnType": "currency", } def _active_projection_field() -> dict: return { "apiName": "Projected_Value__c", "type": "formula", "formulaMode": "active", "returnType": "string", "projection": { "sourceField": "Source_Value__c", "fallback": "", }, "formulaStorageMode": "virtual", } class ActiveFormulaRetentionPropertyTest(unittest.TestCase): def _validate( self, final_field: dict, *, l2_field: dict | None = None, deferred_fields=(), ) -> list[str]: l2_field = l2_field or _active_formula_field() errors: list[str] = [] validate_active_formula_retention( [ { "apiName": "Example__c", "fields": [l2_field], } ], [{"apiName": "Example__c", "fields": [final_field]}], errors, deferred_fields=deferred_fields, ) return errors def test_exact_active_formula_semantics_are_accepted(self) -> None: self.assertEqual(self._validate(_active_formula_field()), []) def test_changed_formula_text_is_a_counterexample(self) -> None: final_field = _active_formula_field() final_field["formula"] = "Monthly_Value__c * 13" self.assertEqual( self._validate(final_field), [ "Example__c.Annual_Value__c active formula formula changed: " "'Monthly_Value__c * 12' -> 'Monthly_Value__c * 13'" ], ) def test_exact_projection_semantics_are_accepted(self) -> None: projection_field = _active_projection_field() self.assertEqual( self._validate(projection_field, l2_field=projection_field), [], ) def test_changed_projection_is_a_counterexample(self) -> None: l2_field = _active_projection_field() final_field = _active_projection_field() final_field["projection"] = { "sourceField": "Different_Value__c", "fallback": "", } errors = self._validate(final_field, l2_field=l2_field) self.assertEqual(len(errors), 1, errors) self.assertIn("active formula projection changed", errors[0]) def test_declared_deferral_accepts_only_the_plain_field_shape(self) -> None: self.assertEqual( self._validate( { "apiName": "Annual_Value__c", "type": "currency", }, deferred_fields={("Example__c", "Annual_Value__c")}, ), [], ) def test_declared_deferral_rejects_retained_formula_metadata(self) -> None: errors = self._validate( { "apiName": "Annual_Value__c", "type": "currency", "formula": "Monthly_Value__c * 12", }, deferred_fields={("Example__c", "Annual_Value__c")}, ) self.assertEqual( errors, [ "Example__c.Annual_Value__c deferred active formula still carries formula" ], ) def test_stale_deferral_is_rejected(self) -> None: errors: list[str] = [] validate_active_formula_retention( [], [], errors, deferred_fields={("Example__c", "Missing__c")}, ) self.assertEqual( errors, [ "deferred active formula policy references non-active L2 field Example__c.Missing__c" ], ) def _person_fields(last_name_is_name_field: bool) -> dict: """Minimal Contact/Lead-style field map with a compound Name. When last_name_is_name_field is True the map reproduces the Contact display bug: two fields (Name and LastName) both flagged isNameField. """ last_name = {"compoundFieldName": "Name"} if last_name_is_name_field: last_name["isNameField"] = True return { "Name": {"isNameField": True, "createable": False, "updateable": False}, "Salutation": {"compoundFieldName": "Name"}, "FirstName": {"compoundFieldName": "Name"}, "LastName": last_name, "Email": {"type": "email"}, } class ContactNameFieldContractTest(unittest.TestCase): """Guards the Contact display-name contract. A Contact whose LastName is also flagged isNameField makes describe's DisplayNameField and the frontend getRecordName resolve the record's name to LastName alone (the first isNameField in field order wins), so the UI shows only the family name instead of the full First + Last name. """ def _run(self, contact_fields: dict) -> list[str]: errors: list[str] = [] validate_campaign_member_lead_lookup_contract( campaign_member_fields={ "LeadId": {"type": "reference", "referenceTo": ["Lead"]}, "ContactId": {"type": "reference", "referenceTo": ["Contact"]}, }, lead={}, lead_fields=_person_fields(last_name_is_name_field=False), contact_fields=contact_fields, list_fields_payload={ "Lead": ["Name", "Company", "Email"], "Contact": ["Name", "Email"], }, errors=errors, ) return errors def test_single_name_field_is_accepted(self) -> None: errors = self._run(_person_fields(last_name_is_name_field=False)) self.assertFalse( [e for e in errors if e.startswith("Contact")], f"unexpected Contact errors: {errors}", ) def test_duplicate_name_field_on_last_name_is_rejected(self) -> None: errors = self._run(_person_fields(last_name_is_name_field=True)) self.assertTrue( any("Contact must have exactly one name field" in e for e in errors), f"expected duplicate-name-field error, got: {errors}", ) class CampaignMemberLeadConversionContractTest(unittest.TestCase): def _fields(self) -> dict: return { field_name: { "createable": True, "updateable": False, } for field_name in ("CampaignId", "ContactId", "LeadId") } | {"Status": {"createable": True, "updateable": True}} def test_relationship_keys_are_accepted_without_write_protection(self) -> None: errors: list[str] = [] validate_campaign_member_lead_conversion_relationships( self._fields(), errors ) self.assertEqual(errors, []) def test_relationship_write_protection_is_rejected(self) -> None: fields = self._fields() for field_name in ("CampaignId", "ContactId", "LeadId"): fields[field_name]["writeProtectionPolicy"] = "immutable_after_create" errors: list[str] = [] validate_campaign_member_lead_conversion_relationships(fields, errors) self.assertEqual( errors, [ "CampaignMember.CampaignId must not block standard Lead conversion", "CampaignMember.ContactId must not block standard Lead conversion", "CampaignMember.LeadId must not block standard Lead conversion", ], ) def test_status_must_remain_updateable_and_unprotected(self) -> None: fields = self._fields() fields["Status"] = { "createable": True, "updateable": False, "writeProtectionPolicy": "immutable_after_create", } errors: list[str] = [] validate_campaign_member_lead_conversion_relationships(fields, errors) self.assertEqual( errors, [ "CampaignMember.Status must remain updateable", "CampaignMember.Status must not be write-protected", ], ) class QuoteRecordTypeContractTest(unittest.TestCase): def test_business_record_types_without_synthetic_standard_are_accepted(self) -> None: errors: list[str] = [] validate_quote_record_type_contract( {"Quote": ["Expansion", "New", "Renewal"]}, ["Order/Expansion", "Order/New", "Order/Renewal"], errors, ) self.assertEqual(errors, []) def test_synthetic_standard_is_rejected_in_payload_and_lineage(self) -> None: errors: list[str] = [] validate_quote_record_type_contract( {"Quote": ["Expansion", "New", "Renewal", "Standard"]}, ["Order/Expansion", "Quote/Standard"], errors, ) self.assertEqual( errors, [ "Quote record types must be exactly Expansion, New, and Renewal; got: " "Expansion, New, Renewal, Standard", "final pack lineage must not retain the synthetic Quote/Standard identity", ], ) class RequiredCreateLayoutContractTest(unittest.TestCase): def validate(self, fields, sections, compact=None): errors = [] validate_layout_required_create_inputs( {"apiName": "Example", "fields": fields}, {"layoutName": "Example Layout", "sections": sections, "compactLayout": compact or []}, errors, ) return errors def test_missing_required_input_is_rejected_even_when_in_compact_layout(self): errors = self.validate( [{"apiName": "RequiredValue", "type": "picklist", "required": True}], [{"fields": ["Name"]}], ["RequiredValue"], ) self.assertEqual(errors, [ "Example.RequiredValue is required on create but missing from editable sections in layout Example Layout" ]) def test_editable_required_input_satisfies_contract(self): fields = [{"apiName": "RequiredValue", "type": "string", "required": True}] for entry in ["RequiredValue", {"fieldApiName": "RequiredValue"}, {"apiName": "RequiredValue"}]: with self.subTest(entry=entry): self.assertEqual(self.validate(fields, [{"fields": [entry]}]), []) def test_server_owned_and_optional_values_do_not_require_inputs(self): fields = [ {"apiName": "Calculated", "type": "formula", "required": True}, {"apiName": "Generated", "type": "autonumber", "required": True}, {"apiName": "ServerOwned", "type": "string", "required": True, "createable": False}, {"apiName": "Defaulted", "type": "string", "required": True, "default": "New"}, {"apiName": "ZeroDefault", "type": "double", "required": True, "default": 0}, {"apiName": "FalseDefault", "type": "boolean", "required": True, "default": False}, {"apiName": "Optional", "type": "string"}, ] self.assertEqual(self.validate(fields, []), []) if __name__ == "__main__": unittest.main()