{
  "id": "inbound-repeat-identity-idempotency",
  "title": "Distinct inbound events for one HubSpot contact converge to one Lead",
  "kind": "hubspot_sync",
  "optional": true,
  "targetedAcceptance": {
    "schemaVersion": "hubspot-targeted-acceptance/v1",
    "reason": "Signed-webhook regression for source-lane identity convergence under repeated property changes."
  },
  "executionMode": "serial",
  "executionModeReason": "The case intentionally submits two source-scoped events and measures their shared identity result without unrelated queue pressure.",
  "caseTimeoutMs": 360000,
  "linkedIssues": [4041],
  "provenance": {
    "businessScenario": "在hubspot integration test新增几个case，防止回归，inbound 和set campaign中重复创建lead的问题",
    "reportedBy": "Amber",
    "capturedAt": "2026-07-17",
    "calibrated": "Two distinct ordinary-inbound property-change Events for the same new HubSpot contact are immutable admission facts. Execution settlement is proved through exact source-projection coverage, terminal ConnectorWorkItem state, ConnectorRuntimeHandoffReceipt, and one COMPLETED RuntimePlatformCommand; Event.Status is not execution authority. After the covered generation completes there must be exactly one active Lead for both the HubSpot id and the email."
  },
  "steps": [
    {
      "id": "create-hubspot-contact",
      "system": "hubspot",
      "method": "POST",
      "path": "/crm/v3/objects/contacts",
      "body": {
        "properties": {
          "email": "inbound-repeat-{{__runId}}@example.com",
          "firstname": "Inbound",
          "lastname": "Repeat{{__runId}}",
          "lifecyclestage": "lead"
        }
      },
      "expectStatusRange": [200, 299],
      "assertions": [
        { "path": "response.body.id", "exists": true }
      ]
    },
    {
      "id": "send-two-inbound-events",
      "system": "connector_webhook",
      "method": "POST",
      "path": "/webhooks/hubspot-crm-connector/webhook",
      "body": [
        {
          "portalId": "{{portalId || \"51280875\"}}",
          "appId": 44166359,
          "subscriptionId": 6723583,
          "eventId": "inbound-repeat-a-{{__runId}}",
          "occurredAt": "1782734624060",
          "subscriptionType": "contact.propertyChange",
          "attemptNumber": 0,
          "objectId": "{{steps.create-hubspot-contact.response.body.id}}",
          "propertyName": "recent_source_details",
          "propertyValue": "Inbound Repeat A {{__runId}}",
          "changeSource": "AUTOMATION_PLATFORM"
        },
        {
          "portalId": "{{portalId || \"51280875\"}}",
          "appId": 44166359,
          "subscriptionId": 6723583,
          "eventId": "inbound-repeat-b-{{__runId}}",
          "occurredAt": "1782734624061",
          "subscriptionType": "contact.propertyChange",
          "attemptNumber": 0,
          "objectId": "{{steps.create-hubspot-contact.response.body.id}}",
          "propertyName": "recent_source_details",
          "propertyValue": "Inbound Repeat B {{__runId}}",
          "changeSource": "AUTOMATION_PLATFORM"
        }
      ],
      "expectStatus": 202,
      "assertions": [
        { "path": "response.body", "exists": true }
      ]
    },
    {
      "id": "assert-both-inbound-events-covered-and-terminal",
      "system": "tidb",
      "method": "GET",
      "sql": "SELECT COUNT(DISTINCT E.Id) AS EventCount, COUNT(DISTINCT CASE WHEN NOT EXISTS (SELECT 1 FROM `_System_ConnectorWorkItem` O JOIN `_System_ConnectorEvent` S ON S.Id = O.EventId WHERE O.WorkType IN ('connector_event', 'workflow_action_apply') AND (O.EventId = E.Id OR (O.InstalledPackId = E.InstalledPackId AND NULLIF(COALESCE(O.ProjectionKey, ''), '') IS NOT NULL AND COALESCE(O.SourceLaneKey, '') = COALESCE(E.SourceLaneKey, '') AND S.InstalledPackId = E.InstalledPackId AND LOWER(TRIM(S.EventKind)) = LOWER(TRIM(E.EventKind)) AND COALESCE(S.RouteVersion, 0) = COALESCE(E.RouteVersion, 0) AND COALESCE(S.CompanionPackInstallId, '') = COALESCE(E.CompanionPackInstallId, '') AND COALESCE(S.CompanionFlowVersionId, '') = COALESCE(E.CompanionFlowVersionId, '') AND ((E.EnqueueOrdinal <> 0 AND S.EnqueueOrdinal <> 0 AND (E.EnqueueOrdinal < S.EnqueueOrdinal OR (E.EnqueueOrdinal = S.EnqueueOrdinal AND E.Id <= S.Id))) OR ((E.EnqueueOrdinal = 0 OR S.EnqueueOrdinal = 0) AND (E.CreatedDate < S.CreatedDate OR (E.CreatedDate = S.CreatedDate AND (E.DeliverySequence < S.DeliverySequence OR (E.DeliverySequence = S.DeliverySequence AND E.Id <= S.Id))))))))) THEN E.Id END) AS UncoveredEventCount, COUNT(DISTINCT CASE WHEN W.State NOT IN ('processed', 'skipped') THEN W.WorkId END) AS NonterminalOrDeadWorkCount, COUNT(DISTINCT CASE WHEN W.State = 'processed' AND NOT EXISTS (SELECT 1 FROM `_System_ConnectorRuntimeHandoffReceipt` R JOIN `_System_RuntimePlatformCommand` C ON BINARY C.CommandId = BINARY R.CommandId WHERE R.WorkId = W.WorkId AND R.EventId = W.EventId AND C.State IN ('COMPLETED', 'FAILED', 'POISONED')) THEN W.WorkId END) AS ProcessedWithoutTerminalCommandCount, COUNT(DISTINCT CASE WHEN W.State = 'processed' AND P.State = 'COMPLETED' THEN P.Id END) AS RuntimeCompletedCount, COUNT(DISTINCT CASE WHEN P.State IN ('FAILED', 'POISONED') THEN P.Id END) AS TerminalFailureCount FROM `_System_ConnectorEvent` E LEFT 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.EventKind = 'contact.propertychange' AND E.SourceObjectID = '{{steps.create-hubspot-contact.response.body.id}}' AND COALESCE(JSON_UNQUOTE(JSON_EXTRACT(E.Payload, '$.payload.eventId')), JSON_UNQUOTE(JSON_EXTRACT(E.Payload, '$.eventId'))) IN ('inbound-repeat-a-{{__runId}}', 'inbound-repeat-b-{{__runId}}')",
      "expectStatus": 200,
      "poll": { "budgetMs": 240000, "intervalMs": 6000 },
      "onTimeout": "fail",
      "assertions": [
        { "path": "response.body.records[0].EventCount", "numericEquals": 2 },
        { "path": "response.body.records[0].UncoveredEventCount", "numericEquals": 0 },
        { "path": "response.body.records[0].NonterminalOrDeadWorkCount", "numericEquals": 0 },
        { "path": "response.body.records[0].ProcessedWithoutTerminalCommandCount", "numericEquals": 0 },
        { "path": "response.body.records[0].RuntimeCompletedCount", "numericEquals": 1 },
        { "path": "response.body.records[0].TerminalFailureCount", "numericEquals": 0 }
      ]
    },
    {
      "id": "assert-one-remote-id-lead",
      "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,
      "poll": { "budgetMs": 240000, "intervalMs": 6000 },
      "onTimeout": "fail",
      "assertions": [
        { "path": "response.body.records[0].RemoteIdLeadCount", "numericEquals": 1 }
      ]
    },
    {
      "id": "assert-one-same-email-lead",
      "system": "tidb",
      "method": "GET",
      "sql": "SELECT COUNT(*) AS SameEmailLeadCount FROM `Lead` WHERE Email = 'inbound-repeat-{{__runId}}@example.com' AND IsConverted = 0",
      "expectStatus": 200,
      "assertions": [
        { "path": "response.body.records[0].SameEmailLeadCount", "numericEquals": 1 }
      ]
    }
  ]
}
