{
  "id": "opportunity-deal-company-association",
  "title": "Outbound Opportunity deal is associated to the same synced Account company",
  "kind": "hubspot_sync",
  "caseTimeoutMs": 600000,
  "linkedIssues": [],
  "provenance": {
    "businessScenario": "生成一个 HubSpot Integration Test,来防止此类问题再次发生。",
    "reportedBy": "prod HubSpot Opportunity->Deal association RCA 2026-07-01",
    "capturedAt": "2026-07-01",
    "calibrated": "[calibrated] Regression for the prod PushOpportunityToHubSpotDeal failure where an Account company durable link existed but association target validation failed closed after pack-install/profile-contract drift. The black-box oracle creates a fresh Account, waits for its HubSpot company id, then creates a native Opportunity and requires the resulting HubSpot deal to carry a v4 association to that exact company id. A missing Opportunity writeback or a deal associated to any other company is a FAIL."
  },
  "steps": [
    {
      "id": "account-record-type",
      "system": "nexuscrm",
      "authRole": "admin",
      "method": "GET",
      "path": "/services/data/v63.0/query?q=SELECT Id FROM _System_RecordType WHERE SobjectType = 'Account' AND Name = 'Customer' AND IsActive = true LIMIT 1",
      "expectStatus": 200,
      "assertions": [
        {
          "path": "response.body.totalSize",
          "atLeast": 1
        },
        {
          "path": "response.body.records[0].Id",
          "exists": true
        }
      ]
    },
    {
      "id": "create-account",
      "system": "nexuscrm",
      "authRole": "admin",
      "method": "POST",
      "path": "/services/data/v63.0/sobjects/Account",
      "body": {
        "Name": "OppAssocAcct {{__runId}}",
        "Industry": "FSI-FinTech",
        "NumberOfEmployees": 180,
        "RecordTypeId": "{{steps.account-record-type.response.body.records[0].Id}}",
        "Region__c": "NA",
        "Vertical__c": "Payment",
        "Website": "https://opp-assoc-{{__runId}}.example.com",
        "ApprovalStatus__c": "Approved",
        "HubSpot_Sync_Enabled__c": true
      },
      "expectStatusRange": [
        200,
        299
      ],
      "assertions": [
        {
          "path": "response.body.id",
          "exists": true
        }
      ]
    },
    {
      "id": "wait-account-writeback",
      "system": "nexuscrm",
      "authRole": "admin",
      "method": "GET",
      "path": "/services/data/v63.0/query?q=SELECT HubSpot_Id__c FROM Account WHERE Id='{{steps.create-account.response.body.id}}' LIMIT 1",
      "expectStatus": 200,
      "poll": {
        "budgetMs": 120000,
        "intervalMs": 6000
      },
      "onTimeout": "fail",
      "assertions": [
        {
          "path": "response.body.records[0].HubSpot_Id__c",
          "exists": true
        }
      ]
    },
    {
      "id": "create-opportunity",
      "system": "nexuscrm",
      "authRole": "admin",
      "method": "POST",
      "path": "/services/data/v63.0/sobjects/Opportunity",
      "delayMs": 15000,
      "body": {
        "Name": "OppAssocDeal {{__runId}}",
        "AccountId": "{{steps.create-account.response.body.id}}",
        "CloseDate": "2026-12-31",
        "Type": "New Subscription",
        "StageName": "Prospecting",
        "Launch_Date__c": "2026-12-31",
        "Amount": 260000,
        "ARR_Amount_USD__c": 110000,
        "HubSpot_Sync_Enabled__c": true
      },
      "expectStatusRange": [
        200,
        299
      ],
      "assertions": [
        {
          "path": "response.body.id",
          "exists": true
        }
      ]
    },
    {
      "id": "wait-opportunity-writeback",
      "system": "nexuscrm",
      "authRole": "admin",
      "method": "GET",
      "path": "/services/data/v63.0/query?q=SELECT HubSpot_Id__c FROM Opportunity WHERE Id='{{steps.create-opportunity.response.body.id}}' LIMIT 1",
      "expectStatus": 200,
      "poll": {
        "budgetMs": 180000,
        "intervalMs": 6000
      },
      "onTimeout": "fail",
      "diagnostics": {
        "connectorEvent": {
          "sourceObjectId": "{{steps.create-opportunity.response.body.id}}",
          "blockOnBacklog": true,
          "blockOnForeignClaimedBy": true
        }
      },
      "assertions": [
        {
          "path": "response.body.records[0].HubSpot_Id__c",
          "exists": true
        }
      ]
    },
    {
      "id": "assert-deal-company-association",
      "system": "hubspot",
      "method": "GET",
      "path": "/crm/v4/objects/deals/{{steps.wait-opportunity-writeback.response.body.records[0].HubSpot_Id__c}}/associations/companies",
      "expectStatus": 200,
      "poll": {
        "budgetMs": 180000,
        "intervalMs": 6000
      },
      "onTimeout": "fail",
      "assertions": [
        {
          "path": "response.body.results",
          "lengthAtLeast": 1
        },
        {
          "path": "response.body.results",
          "matches": "{{steps.wait-account-writeback.response.body.records[0].HubSpot_Id__c}}"
        }
      ]
    }
  ]
}
