{
  "id": "inbound-identity-collision-single-lead",
  "title": "Inbound fails closed instead of duplicating one existing same-email Lead",
  "kind": "hubspot_sync",
  "optional": true,
  "targetedAcceptance": {
    "schemaVersion": "hubspot-targeted-acceptance/v1",
    "reason": "Signed-webhook regression for the unique same-email Lead collision shape."
  },
  "caseTimeoutMs": 300000,
  "linkedIssues": [4041],
  "provenance": {
    "businessScenario": "在hubspot integration test新增几个case，防止回归，inbound 和set campaign中重复创建lead的问题",
    "reportedBy": "Amber",
    "capturedAt": "2026-07-17",
    "calibrated": "Ordinary inbound receives a new nonblank HubSpot contact id while exactly one active same-email Lead exists without a HubSpot id. The event must fail closed with HUBSPOT_CONTACT_IDENTITY_UNRESOLVED and must not create or bind another Lead."
  },
  "steps": [
    {
      "id": "create-existing-lead",
      "system": "nexuscrm",
      "authRole": "admin",
      "method": "POST",
      "path": "/services/data/v63.0/sobjects/Lead",
      "body": {
        "LastName": "InboundSingle{{__runId}}",
        "Company": "Inbound Identity Collision",
        "Email": "inbound-single-{{__runId}}@example.com",
        "HubSpot_Sync_Enabled__c": false
      },
      "expectStatusRange": [200, 299],
      "assertions": [
        { "path": "response.body.id", "exists": true }
      ]
    },
    {
      "id": "create-hubspot-contact",
      "system": "hubspot",
      "method": "POST",
      "path": "/crm/v3/objects/contacts",
      "body": {
        "properties": {
          "email": "inbound-single-{{__runId}}@example.com",
          "firstname": "Inbound",
          "lastname": "Single{{__runId}}",
          "lifecyclestage": "lead"
        }
      },
      "expectStatusRange": [200, 299],
      "assertions": [
        { "path": "response.body.id", "exists": true }
      ]
    },
    {
      "id": "send-inbound-collision-event",
      "system": "connector_webhook",
      "method": "POST",
      "path": "/webhooks/hubspot-crm-connector/webhook",
      "body": [
        {
          "portalId": "{{portalId || \"51280875\"}}",
          "appId": 44166359,
          "subscriptionId": 6723583,
          "eventId": "inbound-single-lead-{{__runId}}",
          "occurredAt": "1782734624058",
          "subscriptionType": "contact.propertyChange",
          "attemptNumber": 0,
          "objectId": "{{steps.create-hubspot-contact.response.body.id}}",
          "propertyName": "recent_source_details",
          "propertyValue": "Inbound Single Lead {{__runId}}",
          "changeSource": "AUTOMATION_PLATFORM"
        }
      ],
      "expectStatus": 202,
      "assertions": [
        { "path": "response.body", "exists": true }
      ]
    },
    {
      "id": "assert-inbound-failed-closed",
      "system": "tidb",
      "method": "GET",
      "sql": "SELECT Id, Status, LEFT(COALESCE(LastError, ''), 500) AS LastErrorSample FROM `_System_ConnectorEvent` WHERE Namespace = 'hubspot-crm-connector' AND EventKind = 'contact.propertychange' AND SourceObjectID = '{{steps.create-hubspot-contact.response.body.id}}' AND COALESCE(JSON_UNQUOTE(JSON_EXTRACT(Payload, '$.payload.propertyValue')), JSON_UNQUOTE(JSON_EXTRACT(Payload, '$.propertyValue'))) = 'Inbound Single Lead {{__runId}}' AND Status = 'failed' AND LastError LIKE '%HUBSPOT_CONTACT_IDENTITY_UNRESOLVED%' ORDER BY CreatedDate DESC LIMIT 1",
      "expectStatus": 200,
      "poll": { "budgetMs": 180000, "intervalMs": 6000 },
      "onTimeout": "fail",
      "assertions": [
        { "path": "response.body.records", "lengthAtLeast": 1 },
        { "path": "response.body.records[0].Status", "equals": "failed" },
        { "path": "response.body.records[0].LastErrorSample", "includes": "HUBSPOT_CONTACT_IDENTITY_UNRESOLVED" }
      ]
    },
    {
      "id": "assert-no-remote-id-lead-created",
      "system": "tidb",
      "method": "GET",
      "sql": "SELECT COUNT(*) AS RemoteIdLeadCount FROM Lead WHERE HubSpot_Id__c = '{{steps.create-hubspot-contact.response.body.id}}' AND IsConverted = 0",
      "expectStatus": 200,
      "assertions": [
        { "path": "response.body.records[0].RemoteIdLeadCount", "equals": 0 }
      ]
    },
    {
      "id": "assert-one-existing-lead-only",
      "system": "tidb",
      "method": "GET",
      "sql": "SELECT COUNT(*) AS SameEmailLeadCount FROM Lead WHERE Email = 'inbound-single-{{__runId}}@example.com' AND IsConverted = 0",
      "expectStatus": 200,
      "assertions": [
        { "path": "response.body.records[0].SameEmailLeadCount", "equals": 1 }
      ]
    },
    {
      "id": "assert-existing-lead-remains-unbound",
      "system": "tidb",
      "method": "GET",
      "sql": "SELECT COUNT(*) AS UnboundLeadCount FROM Lead WHERE Id = '{{steps.create-existing-lead.response.body.id}}' AND HubSpot_Id__c IS NULL AND IsConverted = 0",
      "expectStatus": 200,
      "assertions": [
        { "path": "response.body.records[0].UnboundLeadCount", "equals": 1 }
      ]
    }
  ]
}
