{
  "id": "contact-lead-email-validation",
  "title": "HubSpot Contact inbound handles valid, blank, and core-invalid email values",
  "kind": "hubspot_sync",
  "executionMode": "serial",
  "executionModeReason": "HubSpot contact materialization is queue-sensitive under the full-suite webhook burst and must run without competing case traffic.",
  "caseTimeoutMs": 720000,
  "linkedIssues": [],
  "provenance": {
    "businessScenario": "A HubSpot Contact with a valid email must create a Lead with Email populated. A Contact without email must still create the HubSpot-id Lead shell without attempting to write a blank Email. A nonblank address accepted by HubSpot but rejected by the NexusCRM Lead email contract must not create a Lead or surface an uncontrolled RecordCreate validation error; it must finish with HUBSPOT_CONTACT_EMAIL_INVALID.",
    "reportedBy": "HubSpot Contact-to-Lead email validation incident 2026-07-13",
    "capturedAt": "2026-07-13",
    "calibrated": "[recalibrated 2026-08-27] ConnectorEvent and WorkItem are admission/handoff history, not the D3 application-result authority. CommandId, not the service-local ScopeDigest, correlates the Integration handoff to the Runtime-owned PlatformCommand. The temporary V2 terminal contract persists the bounded HUBSPOT_CONTACT_EMAIL_INVALID code in LastError while leaving ResultPayload empty; the oracle also requires that no malformed Lead exists and no raw provider value leaks."
  },
  "steps": [
    {
      "id": "create-valid-contact",
      "system": "hubspot",
      "method": "POST",
      "path": "/crm/v3/objects/contacts",
      "body": {
        "properties": {
          "email": "email-guard-valid-{{__runId}}@example.com",
          "firstname": "EmailGuard",
          "lastname": "Valid{{__runId}}"
        }
      },
      "expectStatusRange": [200, 299],
      "assertions": [
        { "path": "response.body.id", "exists": true }
      ]
    },
    {
      "id": "create-blank-contact",
      "system": "hubspot",
      "method": "POST",
      "path": "/crm/v3/objects/contacts",
      "body": {
        "properties": {
          "firstname": "EmailGuard",
          "lastname": "Blank{{__runId}}"
        }
      },
      "expectStatusRange": [200, 299],
      "assertions": [
        { "path": "response.body.id", "exists": true }
      ]
    },
    {
      "id": "create-core-invalid-contact",
      "system": "hubspot",
      "method": "POST",
      "path": "/crm/v3/objects/contacts",
      "body": {
        "properties": {
          "email": "email-guard-invalid-{{__runId}}!core@example.com",
          "firstname": "EmailGuard",
          "lastname": "Invalid{{__runId}}"
        }
      },
      "expectStatusRange": [200, 299],
      "assertions": [
        { "path": "response.body.id", "exists": true }
      ]
    },
    {
      "id": "assert-valid-email-lead",
      "system": "tidb",
      "method": "GET",
      "sql": "SELECT COUNT(*) AS ValidLeadCount FROM `Lead` WHERE HubSpot_Id__c = '{{steps.create-valid-contact.response.body.id}}' AND Email = 'email-guard-valid-{{__runId}}@example.com'",
      "expectStatus": 200,
      "poll": { "budgetMs": 180000, "intervalMs": 6000 },
      "onTimeout": "fail",
      "assertions": [
        { "path": "response.body.records[0].ValidLeadCount", "numericEquals": 1 }
      ]
    },
    {
      "id": "assert-blank-email-lead",
      "system": "tidb",
      "method": "GET",
      "sql": "SELECT COUNT(*) AS BlankLeadCount FROM `Lead` WHERE HubSpot_Id__c = '{{steps.create-blank-contact.response.body.id}}' AND Email IS NULL",
      "expectStatus": 200,
      "poll": { "budgetMs": 180000, "intervalMs": 6000 },
      "onTimeout": "fail",
      "assertions": [
        { "path": "response.body.records[0].BlankLeadCount", "numericEquals": 1 }
      ]
    },
    {
      "id": "assert-core-invalid-email-is-bounded-business-error",
      "system": "tidb",
      "method": "GET",
      "sql": "SELECT CASE WHEN COUNT(W.WorkId) > 0 AND SUM(CASE WHEN W.State = 'processed' AND P.State IN ('COMPLETED', 'FAILED') THEN 1 WHEN W.State = 'skipped' THEN 1 ELSE 0 END) = COUNT(W.WorkId) AND SUM(CASE WHEN P.State = 'FAILED' AND COALESCE(P.LastError, '') = 'runtime Flow command failed: HUBSPOT_CONTACT_EMAIL_INVALID' THEN 1 ELSE 0 END) > 0 AND SUM(CASE WHEN COALESCE(P.LastError, '') LIKE '%invalid email format%' THEN 1 ELSE 0 END) = 0 THEN 1 ELSE 0 END AS EmailPolicyObserved, (SELECT COUNT(*) FROM `Lead` WHERE HubSpot_Id__c = '{{steps.create-core-invalid-contact.response.body.id}}') AS MalformedLeadCount FROM `_System_ConnectorEvent` E JOIN `_System_ConnectorWorkItem` W ON W.EventId = E.Id AND W.WorkType IN ('connector_event', 'workflow_action_apply') LEFT JOIN `_System_ConnectorRuntimeHandoffReceipt` H ON H.WorkId = W.WorkId AND H.EventId = E.Id LEFT JOIN `_System_RuntimePlatformCommand` P ON BINARY P.CommandId = BINARY H.CommandId WHERE E.Namespace = 'hubspot-crm-connector' AND E.SourceObjectID = '{{steps.create-core-invalid-contact.response.body.id}}'",
      "expectStatus": 200,
      "poll": { "budgetMs": 300000, "intervalMs": 6000 },
      "onTimeout": "fail",
      "assertions": [
        { "path": "response.body.records[0].EmailPolicyObserved", "numericEquals": 1 },
        { "path": "response.body.records[0].MalformedLeadCount", "numericEquals": 0 }
      ]
    }
  ]
}
