{
  "id": "fix-4228-ordinary-webhook-durable-admission",
  "title": "Ordinary HubSpot webhook returns 202 only with durable ingress, WorkItem execution, and Lead update",
  "kind": "hubspot_sync",
  "executionMode": "serial",
  "executionModeReason": "The case correlates one signed delivery across its ingress receipt, Connector Event, and run-scoped Lead effect without unrelated queue pressure.",
  "caseTimeoutMs": 600000,
  "linkedIssues": [4228],
  "provenance": {
    "businessScenario": "补一个hubspot integration test case到l2",
    "reportedBy": "Amber",
    "capturedAt": "2026-07-21",
    "calibrated": "A signed ordinary HubSpot lastname property-change delivery must return exactly 202, be durably correlated from _System_ConnectorIngressReceipt.EnqueuedEventIds to one immutable _System_ConnectorEvent provenance row, reach a processed _System_ConnectorWorkItem source projection for the same lane at or after that Event ordinal, and update the matching NexusCRM Lead. Event.Status is not an execution authority under source-projection coalescing. A destructive 204, missing durable ingress, missing WorkItem execution, or missing record effect fails the case. Controlled internal-unavailable 503 remains covered by the provider-neutral default-path and pressure gates."
  },
  "steps": [
    {
      "id": "create-hubspot-contact",
      "system": "hubspot",
      "method": "POST",
      "path": "/crm/v3/objects/contacts",
      "body": {
        "properties": {
          "email": "fix4228-{{__runId}}@example.com",
          "firstname": "Durable",
          "lastname": "Before4228{{__runId}}",
          "lifecyclestage": "lead"
        }
      },
      "expectStatusRange": [200, 299],
      "assertions": [
        { "path": "response.body.id", "exists": true }
      ]
    },
    {
      "id": "capture-webhook-time",
      "system": "tidb",
      "method": "GET",
      "sql": "SELECT CAST(UNIX_TIMESTAMP(UTC_TIMESTAMP(3)) * 1000 AS UNSIGNED) AS OccurredAtMillis",
      "expectStatus": 200,
      "assertions": [
        { "path": "response.body.records[0].OccurredAtMillis", "exists": true }
      ]
    },
    {
      "id": "send-signed-lastname-delta",
      "system": "connector_webhook",
      "method": "POST",
      "path": "/webhooks/hubspot-crm-connector/webhook",
      "body": [
        {
          "portalId": "{{portalId || \"51280875\"}}",
          "appId": 44166359,
          "subscriptionId": 6723583,
          "eventId": "fix-4228-durable-{{__runId}}",
          "occurredAt": "{{steps.capture-webhook-time.response.body.records[0].OccurredAtMillis}}",
          "subscriptionType": "contact.propertyChange",
          "attemptNumber": 0,
          "objectId": "{{steps.create-hubspot-contact.response.body.id}}",
          "propertyName": "lastname",
          "propertyValue": "After4228{{__runId}}",
          "changeSource": "AUTOMATION_PLATFORM"
        }
      ],
      "expectStatus": 202,
      "assertions": [
        { "path": "response.body", "exists": true }
      ]
    },
    {
      "id": "assert-durable-receipt-workitem-correlation",
      "system": "tidb",
      "method": "GET",
"sql": "SELECT R.Id AS ReceiptId, R.Status AS ReceiptStatus, JSON_LENGTH(R.EnqueuedEventIds) AS EnqueuedEventCount, E.Id AS EventId, E.Status AS EventStatus, W.WorkId, W.EventId AS WorkItemEventId, W.Generation AS WorkItemGeneration, W.State AS WorkItemState, W.LaneOrdinal AS WorkItemLaneOrdinal, S.Id AS SelectedEventId, S.EnqueueOrdinal AS SelectedEventOrdinal FROM `_System_ConnectorEvent` E JOIN `_System_ConnectorIngressReceipt` R ON JSON_CONTAINS(R.EnqueuedEventIds, JSON_QUOTE(E.Id)) = 1 JOIN `_System_ConnectorWorkItem` W ON W.InstalledPackId = E.InstalledPackId AND W.WorkType IN ('connector_event', 'workflow_action_apply') AND (NULLIF(COALESCE(W.ProjectionKey, ''), '') IS NOT NULL OR (NULLIF(COALESCE(W.ProjectionKey, ''), '') IS NULL AND W.WorkId LIKE 'csp:%' AND W.State NOT IN ('processed', 'skipped', 'dead'))) AND COALESCE(W.SourceLaneKey, '') = COALESCE(E.SourceLaneKey, '') JOIN `_System_ConnectorEvent` S ON S.Id = W.EventId 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'))) = 'fix-4228-durable-{{__runId}}' 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)))))) ORDER BY W.LaneOrdinal DESC, W.Generation DESC LIMIT 1",
      "expectStatus": 200,
      "poll": { "budgetMs": 240000, "intervalMs": 6000 },
      "onTimeout": "fail",
      "assertions": [
        { "path": "response.body.records", "lengthAtLeast": 1 },
        { "path": "response.body.records[0].ReceiptId", "exists": true },
        { "path": "response.body.records[0].ReceiptStatus", "equals": "enqueued" },
        { "path": "response.body.records[0].EnqueuedEventCount", "numericEquals": 1 },
        { "path": "response.body.records[0].EventId", "exists": true },
        { "path": "response.body.records[0].EventStatus", "exists": true },
        { "path": "response.body.records[0].WorkId", "exists": true },
        { "path": "response.body.records[0].WorkItemEventId", "exists": true },
        { "path": "response.body.records[0].WorkItemGeneration", "exists": true },
        { "path": "response.body.records[0].WorkItemState", "equals": "processed" },
        { "path": "response.body.records[0].WorkItemLaneOrdinal", "exists": true },
        { "path": "response.body.records[0].SelectedEventId", "exists": true },
        { "path": "response.body.records[0].SelectedEventOrdinal", "exists": true }
      ]
    },
    {
      "id": "assert-lead-lastname-effect",
      "system": "nexuscrm",
      "authRole": "admin",
      "method": "GET",
      "path": "/services/data/v63.0/query?q=SELECT Id,HubSpot_Id__c,LastName FROM Lead WHERE HubSpot_Id__c='{{steps.create-hubspot-contact.response.body.id}}' AND IsConverted=false LIMIT 1",
      "expectStatus": 200,
      "poll": { "budgetMs": 240000, "intervalMs": 6000 },
      "onTimeout": "fail",
      "assertions": [
        { "path": "response.body.records", "lengthAtLeast": 1 },
        { "path": "response.body.records[0].HubSpot_Id__c", "equals": "{{steps.create-hubspot-contact.response.body.id}}" },
        { "path": "response.body.records[0].LastName", "equals": "After4228{{__runId}}" }
      ],
      "diagnostics": {
        "connectorEvent": {
          "sourceObjectId": "{{steps.create-hubspot-contact.response.body.id}}",
          "eventKind": "contact.propertychange",
          "blockOnBacklog": true,
          "blockOnForeignClaimedBy": true
        }
      },
      "timeoutMs": 8000
    }
  ]
}
