{
  "id": "in-rej-04",
  "title": "Same contact inbound twice stays exactly one Lead (idempotent)",
  "kind": "hubspot_sync",
  "caseTimeoutMs": 540000,
  "linkedIssues": [],
  "provenance": {
    "businessScenario": "IN-REJ-04 — the same HubSpot contact inbound twice (create then propertychange) must leave exactly one Lead in NexusCRM, no proliferation (P-IDEMPOTENT). Control field firstname (IdemFirst->IdemUpdated) proves the second inbound link fired before asserting COUNT()=1.",
    "reportedBy": "HubSpot sync spec IN-REJ-04 (hubspot-nexuscrm-sync-CASE-EXEC-SELFCONTAINED)",
    "capturedAt": "2026-06-18",
    "calibrated": "[calibrated] Reviewed against ksyun sandbox: idempotent re-inbound (create then propertychange) leaves exactly one Lead; inbound contact values sandbox-legal, COUNT()=1 invariant retained. 2026-06-26 staging run 28222667769 showed the second inbound firstname update can exceed the old 90s poll window, so the create materialization uses 180s, the second inbound assertion uses 240s, and timeout artifacts include connector-event diagnostics."
  },
  "steps": [
    {
      "id": "create-contact",
      "system": "hubspot",
      "method": "POST",
      "path": "/crm/v3/objects/contacts",
      "body": {
        "properties": {
          "email": "inb-idem-{{__runId}}@example.com",
          "firstname": "IdemFirst",
          "lastname": "Case{{__runId}}"
        }
      },
      "expectStatusRange": [200, 299],
      "assertions": [
        { "path": "response.body.id", "exists": true }
      ]
    },
    {
      "id": "wait-first-row",
      "system": "nexuscrm",
      "authRole": "admin",
      "method": "GET",
      "path": "/services/data/v63.0/query?q=SELECT Id, FirstName FROM Lead WHERE HubSpot_Id__c='{{steps.create-contact.response.body.id}}' LIMIT 1",
      "expectStatus": 200,
      "poll": { "budgetMs": 180000, "intervalMs": 6000 },
      "onTimeout": "fail",
      "diagnostics": {
        "connectorEvent": {
          "sourceObjectId": "{{steps.create-contact.response.body.id}}",
          "blockOnBacklog": true,
          "blockOnForeignClaimedBy": true,
          "foreignClaimedByPattern": "(MacBook|\\.local)"
        }
      },
      "assertions": [
        { "path": "response.body.records", "lengthAtLeast": 1 },
        { "path": "response.body.records[0].FirstName", "equals": "IdemFirst" }
      ]
    },
    {
      "id": "patch-contact",
      "system": "hubspot",
      "method": "PATCH",
      "path": "/crm/v3/objects/contacts/{{steps.create-contact.response.body.id}}",
      "body": { "properties": { "firstname": "IdemUpdated" } },
      "expectStatusRange": [200, 299],
      "assertions": [
        { "path": "response.body.id", "equals": "{{steps.create-contact.response.body.id}}" }
      ]
    },
    {
      "id": "wait-second-inbound",
      "system": "nexuscrm",
      "authRole": "admin",
      "method": "GET",
      "path": "/services/data/v63.0/query?q=SELECT Id, FirstName FROM Lead WHERE HubSpot_Id__c='{{steps.create-contact.response.body.id}}' LIMIT 1",
      "expectStatus": 200,
      "poll": { "budgetMs": 240000, "intervalMs": 6000 },
      "onTimeout": "fail",
      "diagnostics": {
        "connectorEvent": {
          "sourceObjectId": "{{steps.create-contact.response.body.id}}",
          "blockOnBacklog": true,
          "blockOnForeignClaimedBy": true,
          "foreignClaimedByPattern": "(MacBook|\\.local)"
        }
      },
      "assertions": [
        { "path": "response.body.records[0].FirstName", "equals": "IdemUpdated" }
      ]
    },
    {
      "id": "assert-count-one",
      "system": "nexuscrm",
      "authRole": "admin",
      "method": "GET",
      "path": "/services/data/v63.0/query?q=SELECT COUNT() FROM Lead WHERE HubSpot_Id__c='{{steps.create-contact.response.body.id}}'",
      "expectStatus": 200,
      "delayMs": 15000,
      "poll": { "budgetMs": 120000, "intervalMs": 6000 },
      "onTimeout": "fail",
      "assertions": [
        { "path": "response.body.totalSize", "numericEquals": 1 }
      ]
    }
  ]
}
