{
  "id": "k-form-01",
  "title": "HubSpot Form submit with new email creates exactly one Lead (no Contact in NX)",
  "kind": "hubspot_sync",
  "caseTimeoutMs": 540000,
  "linkedIssues": [
    2862,
    2863,
    3398
  ],
  "provenance": {
    "businessScenario": "K-FORM-01 (P-FORM) — submit a brand-new email through the designated HubSpot Form; NexusCRM must produce exactly one Lead whose HubSpot_Id__c points at the Contact, must NOT directly create a Contact, and HubSpot_Sync_Enabled__c=true.",
    "wiring": "Self-contained via the `hubspot_forms` step system: the submit goes to the real HubSpot Forms endpoint on api.hsforms.com (POST /submissions/v3/integration/submit/{portalId}/{formGuid}, unauthenticated) — exactly what a public visitor does. The created Contact drives the same inbound -> Lead pipeline; s2..s5 then verify via api.hubapi.com (CRM) and NexusCRM REST.",
    "formContract": "Form guid 72b32b88-35d1-4e1a-a56c-34e7561363a4 ('NexusCRM Test', portal 51280875). Required fields read live from the form definition: firstname, lastname, company, email, country_picklist__sfdc_ (select; 'Japan' is a valid option). company is supplied so the inbound Lead (Company-required) materializes.",
    "reportedBy": "HubSpot sync spec K-FORM-01 (hubspot-nexuscrm-sync-CASE-EXEC-SELFCONTAINED)",
    "capturedAt": "2026-06-29",
    "calibrated": "[recalibrated 2026-08-26] Required form fields stay aligned to the live form definition. A final 60-second provider readback now requires the original firstname/lastname/company/email so a local PASS cannot hide the #3398 synthetic placeholder echo."
  },
  "steps": [
    {
      "id": "s1-form-submit",
      "system": "hubspot_forms",
      "method": "POST",
      "path": "/submissions/v3/integration/submit/{{portalId || \"51280875\"}}/{{formGuid || \"72b32b88-35d1-4e1a-a56c-34e7561363a4\"}}",
      "body": {
        "fields": [
          {
            "name": "email",
            "value": "kform01-{{__runId}}@example.com"
          },
          {
            "name": "firstname",
            "value": "NewLead"
          },
          {
            "name": "lastname",
            "value": "KForm01"
          },
          {
            "name": "company",
            "value": "IT FormCo"
          },
          {
            "name": "country_picklist__sfdc_",
            "value": "Japan"
          }
        ]
      },
      "expectStatusRange": [
        200,
        299
      ]
    },
    {
      "id": "s2-search-contact",
      "system": "hubspot",
      "method": "POST",
      "path": "/crm/v3/objects/contacts/search",
      "body": {
        "filterGroups": [
          {
            "filters": [
              {
                "propertyName": "email",
                "operator": "EQ",
                "value": "kform01-{{__runId}}@example.com"
              }
            ]
          }
        ]
      },
      "expectStatusRange": [
        200,
        299
      ],
      "poll": {
        "budgetMs": 60000,
        "intervalMs": 6000
      },
      "onTimeout": "blocked",
      "assertions": [
        {
          "path": "response.body.results[0].id",
          "exists": true
        }
      ]
    },
    {
      "id": "s3-wait-lead-row",
      "system": "nexuscrm",
      "authRole": "admin",
      "method": "GET",
      "path": "/services/data/v63.0/query?q=SELECT Id,HubSpot_Id__c,HubSpot_Sync_Enabled__c FROM Lead WHERE HubSpot_Id__c='{{steps.s2-search-contact.response.body.results[0].id}}' LIMIT 1",
      "expectStatus": 200,
      "poll": {
        "budgetMs": 200000,
        "intervalMs": 6000
      },
      "onTimeout": "blocked",
      "assertions": [
        {
          "path": "response.body.records[0].HubSpot_Id__c",
          "equals": "{{steps.s2-search-contact.response.body.results[0].id}}"
        },
        {
          "path": "response.body.records[0].HubSpot_Sync_Enabled__c",
          "equals": true
        }
      ]
    },
    {
      "id": "s4-lead-count",
      "system": "nexuscrm",
      "authRole": "admin",
      "method": "GET",
      "path": "/services/data/v63.0/query?q=SELECT COUNT() FROM Lead WHERE HubSpot_Id__c='{{steps.s2-search-contact.response.body.results[0].id}}'",
      "expectStatus": 200,
      "poll": {
        "budgetMs": 120000,
        "intervalMs": 8000
      },
      "onTimeout": "fail",
      "assertions": [
        {
          "path": "response.body.totalSize",
          "numericEquals": 1
        }
      ]
    },
    {
      "id": "s5-contact-count-zero",
      "system": "nexuscrm",
      "authRole": "admin",
      "method": "GET",
      "path": "/services/data/v63.0/query?q=SELECT COUNT() FROM Contact WHERE HubSpot_Id__c='{{steps.s2-search-contact.response.body.results[0].id}}'",
      "expectStatus": 200,
      "assertions": [
        {
          "path": "response.body.totalSize",
          "numericEquals": 0
        }
      ]
    },
    {
      "id": "s6-provider-fields-settled",
      "system": "hubspot",
      "method": "GET",
      "path": "/crm/v3/objects/contacts/{{steps.s2-search-contact.response.body.results[0].id}}?properties=firstname,lastname,company,email",
      "delayMs": 60000,
      "expectStatus": 200,
      "assertions": [
        { "path": "response.body.properties.firstname", "equals": "NewLead" },
        { "path": "response.body.properties.lastname", "equals": "KForm01" },
        { "path": "response.body.properties.company", "equals": "IT FormCo" },
        { "path": "response.body.properties.email", "equals": "kform01-{{__runId}}@example.com", "caseInsensitive": true }
      ]
    }
  ]
}
