Blog

How to test a sales agent across Salesforce, HubSpot and Gmail

Martin is a Lead in Salesforce and a contact in HubSpot, and his account has two open deals. Where a sales agent goes wrong here, and the tests that notice.

November 10, 2026 · Patrick Liu

Three hours ago, Martin Okafor emailed Tess Morgan at Quarry Payroll, a payroll provider. Martin is the finance director at Holloway Veterinary Group. They open three new clinics in January, about 45 employees between them, and he would like a quote for adding them to their plan by Friday. Gwen Hadley, Holloway’s director of operations, is on copy.

Holloway is already a customer, which is what makes the agent’s job tricky. In Salesforce the account has two open opportunities: the 2027 renewal, in Negotiation/Review at $38,400, and the new clinics, in Qualification at $21,600. Martin is in Salesforce too, as a Lead from a webinar who is still Open - Not Contacted. In HubSpot, where marketing works, he is a contact whose lifecycle stage says lead, and Gwen is a contact marked customer. The agent has to move the clinics deal to Proposal/Price Quote, mark Martin as contacted, move him to the opportunity stage in HubSpot and reply in his thread.

For this post we built two agents, and each fumbles one of those steps. One moves the renewal instead of the clinics deal. The other creates a second record for Martin. Every request both agents made succeeded, with 200, 201 or 204.

The second mistake comes from a difference between the two CRMs. HubSpot keeps a person as one contact and tracks how close they are to buying with a property, the lifecycle stage. Salesforce keeps prospects and customers in two different objects, Leads and Contacts, and a Lead becomes a Contact only when someone converts it. Ask Salesforce the question that works in HubSpot, whether a contact with this email exists, and the answer is no. Our duplicate-person agent asked exactly that, and the whole bug is one word missing from a list.

Ancha, which we make, sits in the replica row of the comparison below.

One person, two data models

In HubSpot, Martin is contact 202. HubSpot treats email as the identifier that keeps contacts unique, so the natural lookup is a search on his address, and that is what our agent does. His lifecycle stage moves forward as he gets closer to buying: lead now, opportunity once a deal is in play. Through the API it only moves forward. Moving a contact back means clearing the field and setting it again.

In Salesforce, Martin is Lead 00Q000000000101AAA. Leads and Contacts are separate objects with their own fields, so finding a person takes a query on each. The convertLead call turns a Lead into a Contact, an Account and optionally an Opportunity. Until someone runs it, a query on Contact will not find him. Gwen is a Contact on the account.

The correct agent looks for Martin among Leads first, then Contacts. The duplicate-person agent differs from it by one line: const objects = ['Lead', 'Contact']; became const objects = ['Contact'];.

That line cost Martin a second record. The agent found no Contact with his email, created one with POST /services/data/v67.0/sobjects/Contact, got 201 Created and carried on. The Lead from the webinar stayed Open - Not Contacted, so anyone working the lead list still sees a prospect nobody has called. The new Contact starts with none of the Lead’s history. The agent’s own summary line looked fine: updated new Contact , moved Holloway Veterinary Group: three new clinics, HubSpot contact 202, replied in thread 1a1c58b408082fa0.

The HubSpot habit, one lookup by email, is correct in HubSpot. Carried into Salesforce, it misses every person who is still a Lead.

The first open opportunity

The other mistake is about order. The wrong agent asks Salesforce for Holloway’s open opportunities and takes the first one in the list, const clinics = opportunities[0];.

Salesforce’s SOQL reference says “There’s no guarantee of the order of results unless you use an ORDER BY clause in a query.” Our query had no ORDER BY, and in our world the renewal came back first in every trial. Another org can return the two deals the other way round. That makes the bug unpleasant. The same agent can pick the right deal in your developer org and the wrong one in production, with no code change in between.

ORDER BY would not fix it. Sorting by close date or creation date makes the answer stable, but Martin’s email is about the new clinics, and no sort order knows that. The agent has to choose the deal by what the request is about. Our correct agent matches the opportunity name against /new clinics/i. A real agent would match on what the email asks for, and hand the choice to a person when two deals fit.

The update gives no warning either. A PATCH on a record replaces the fields you send and comes back as 204 with no body. The response confirms that an update worked. It cannot tell you whether it was the right record. Our wrong agent moved a deal in Negotiation/Review back to Proposal/Price Quote and replaced its description with a note about the clinics. Its summary line said so, moved Holloway Veterinary Group: 2027 renewal, and that line is the only trace of the mistake unless something reads the CRM afterwards.

Mocks, recordings, vendor accounts or a replica?

ApproachCatchesMissesVendor accountTools
Mock the SDK clientsYour agent’s decisions: which opportunity it picks from a list, whether it looks for a Lead before it creates a Contact, the headers on its replyWhat the services hold and return: the order a SOQL query returns records in, the Lead that already exists, whether Gmail files the reply in the thread. A mock returns the list you wroteNoThe mocks in Jest (30.5.2, September 2026), Vitest (5.1.0, November 2026) or Python’s unittest.mock
Fake or recorded HTTP APIsThe requests the real SDKs send: the SOQL query, the update’s body, the raw messageWhat a write changes. A recorded update moves no deal, and a Contact created against a stub does not show up in the next query. WireMock scenarios and Mockoon CRUD routes keep state you script yourselfNo; yes to recordWireMock (3.13.2, November 2025), Mockoon (9.10.0, October 2026), Prism (5.16.0, July 2026), Polly.js (6.0.6, July 2023), nock (15.0.6, November 2026), vcrpy (9.4.2, November 2026)
Each vendor’s own accountThe products themselves: your own org’s setup and permissions, and real mail deliveryA repeatable start: the stage the agent set, the Contact it created and the reply it sent stay there until you reset themYes, one per toolA Salesforce Developer Edition or scratch org, a HubSpot developer test account and a separate Google account (details below)
Stateful replicaWhat the agent changed: which opportunity it updated, whether it created a second record for someone already there, the HubSpot contact it wrote to, the thread its reply landed in. Each run starts from the same seeded stateNoAncha (all three), Arga Labs, Drizz and Veris (all three, per their catalogs), Sparta Worlds (all three, in closed beta), Archal (HubSpot and Gmail), vercel-labs/emulate (Gmail; Apache-2.0, 0.13.1, November 2026)

Package versions and their dates were read from the npm registry, PyPI and GitHub releases on November 10, 2026.

For a sales agent the split is fairly clean. Mock the clients to test the code that builds the reply and picks which fields to update. A mock confirms that the reply carries an In-Reply-To header and that the stage name is spelled the way Salesforce expects.

Recorded HTTP shows the exact SOQL your client sends and the raw message it hands to Gmail. Its weak spot for this job is writes. A recorded PATCH changes nothing, so a test that updates the opportunity and then reads it back reads the recording.

Your own Salesforce org and HubSpot account are the place to see your real setup: your fields, your permissions, your validation. Point the agent at them to see how it behaves with that setup. Run the whole job against a replica, where the two mistakes above live, and repeat each scenario a few times. A model-driven agent may choose the right deal in one run and the renewal in the next.

Free Salesforce orgs, HubSpot test accounts and a spare Google account

Salesforce has two free options. A Developer Edition org costs nothing, though Salesforce may terminate one that has been inactive for 45 days. Scratch orgs are disposable orgs you create from a Dev Hub, and a Developer Edition org can act as one. Each scratch org starts empty and lasts from 1 to 30 days, 7 by default. Empty cuts both ways for a sales agent. You have to load Holloway, both deals, Gwen and Martin yourself, and in exchange every new scratch org starts clean.

HubSpot developer test accounts are free, up to 10 per account, each with a 90-day trial of many enterprise features. A test account expires after 90 days without API calls. HubSpot’s standard sandboxes copy your production account, including up to 5,000 contacts with their associated records, and need an Enterprise subscription.

For Gmail, use a separate Google account. An OAuth app in Testing status is capped by Google at 100 test users, and a test user’s authorization expires seven days after consent. The test mailbox has to be authorized again every week.

Each of these accounts remembers the previous run. The renewal the agent moved stays moved, the extra Contact stays in the org and the reply stays in the thread. A new scratch org resets Salesforce at the cost of loading the data again. HubSpot and Gmail need a cleanup step of your own before the next run.

What should a sales agent test check?

Four things, and each check reads what the agent did to the tools.

The clinics deal moved and the renewal did not. This takes two trace checks. One requires a PATCH on the clinics opportunity, 006000000000102AAA, and one forbids a PATCH on the renewal, 006000000000101AAA. You need both. An agent that updates both deals passes the first, and an agent that updates neither passes the second.

Martin exists once. The check forbids creating a Lead or a Contact. It means something only because the world already holds Martin as a Lead. In an empty test org, creating a Contact would be correct, and the check would have nothing to catch.

The HubSpot update is on Martin’s contact. Holloway has two contacts in HubSpot. An agent that searches by company and takes the first result could move Gwen instead. HubSpot’s search can also take a few moments to show a record that was just created or changed. An agent that creates a contact and searches for it right away may miss it and create another. The check names contact 202.

The reply is in his thread. Gmail threads a message only when three things hold: the send request names the thread’s threadId, the message’s In-Reply-To and References headers carry the original’s Message-ID, and both subjects agree. Leave out any of the three and messages.send still returns 200, but Martin gets a fresh email instead of an answer under his own. Once the run ends, the check counts the messages in his thread and wants at least two.

The recorded runs

The world starts three hours after Martin’s email arrives. Tess’s inbox holds his message and an older one from Gwen about renewal paperwork. The Salesforce org holds the account, Gwen as a Contact, Martin as a Lead and the two deals. HubSpot holds the company and both contacts. Here is the slice of the seed where the agent’s choices matter:

salesforce:
  leads:
    - Id: 00Q000000000101AAA
      FirstName: Martin
      LastName: Okafor
      Company: Holloway Veterinary Group
      Email: martin.okafor@hollowayvet.example
      Title: Finance Director
      Status: Open - Not Contacted
      LeadSource: Webinar
  opportunities:
    - Id: 006000000000101AAA
      Name: "Holloway Veterinary Group: 2027 renewal"
      AccountId: 001000000000101AAA
      StageName: Negotiation/Review
      CloseDate: "2026-12-15"
      Amount: 38400
      Probability: 80
    - Id: 006000000000102AAA
      Name: "Holloway Veterinary Group: three new clinics"
      AccountId: 001000000000101AAA
      StageName: Qualification
      CloseDate: "2027-01-29"
      Amount: 21600
      Probability: 20

The checks, one for each part of the task:

checks:
  - id: moved_the_clinics_deal
    trace:
      called: "PATCH /services/data/v67.0/sobjects/Opportunity/006000000000102AAA"
  - id: left_the_renewal_alone
    trace:
      not_called: "PATCH /services/data/v67.0/sobjects/Opportunity/006000000000101AAA"
  - id: no_duplicate_person
    trace:
      not_called:
        - "POST /services/data/v67.0/sobjects/Lead"
        - "POST /services/data/v67.0/sobjects/Contact"
  - id: moved_him_in_hubspot
    trace: { called: "PATCH /crm/v3/objects/contacts/202" }
  - id: replied_in_his_thread
    state: { gmail.thread: { of: clinics_quote, messages_gte: 2 } }

A trace check inspects the agent’s requests, and a state check inspects a tool once the run is over. Against the world before any agent touches it, ancha check passes two of the five, since nobody has moved the renewal or created a record. Those two can only go from pass to fail, when an agent changes something it should leave alone.

None of these agents uses a model. Each is a plain script, and each wrong agent differs from the correct one by a single line, so a failure points at one decision. Every trial gets a newly seeded world, three trials per agent. First, the agent that takes the first open opportunity:

$ ancha run sales-world.yaml --local -n 3
running locally; agent not sealed (network open, 5 variables from this shell)
run_20261110-232944_1d2cc7010a66b673: 3 trials of sales-world, 1 at a time
trial 1 ✗ 145ms · 2 checks failed
trial 2 ✗ 120ms · 2 checks failed
trial 3 ✗ 113ms · 2 checks failed

✗ sales-world  0/3 (0%)
    ✗ moved_the_clinics_deal  failed in 3/3 trials
      trial 1: The agent never made a call matching "PATCH /services/data/v67.0/sobjects/Opportunity/006000000000102AAA". Its calls were: GET /gmail/v1/users/me/messages, GET /gmail/v1/users/me/messages/1a1c58b408082fa0, GET /services/data/v67.0/query, PATCH /services/data/v67.0/sobjects/Lead/00Q000000000101AAA, PATCH /services/data/v67.0/sobjects/Opportunity/006000000000101AAA, POST /crm/v3/objects/contacts/search, PATCH /crm/v3/objects/contacts/202, POST /gmail/v1/users/me/messages/send.
    ✗ left_the_renewal_alone  failed in 3/3 trials
      trial 1: The agent made 1 call(s) matching "PATCH /services/data/v67.0/sobjects/Opportunity/006000000000101AAA" that it must not make: PATCH /services/data/v67.0/sobjects/Opportunity/006000000000101AAA (seq 7).

fail (minimum pass rate 100%)  run run_20261110-232944_1d2cc7010a66b673

Both failures are the same decision seen from two sides. moved_the_clinics_deal lists the agent’s ten calls and finds no update to the clinics deal. left_the_renewal_alone finds the update to the renewal at call 7. Every call returned 200 or 204, and the remaining checks all passed: Martin’s Lead was marked contacted, his HubSpot contact moved and the reply landed in his thread. A test that only looked at status codes, or only at the reply, would have passed this agent.

Next, the agent that looks for Martin only among Contacts:

$ ancha run sales-world.yaml --local -n 3
running locally; agent not sealed (network open, 5 variables from this shell)
run_20261110-232947_2df211226a6a4bbb: 3 trials of sales-world, 1 at a time
trial 1 ✗ 140ms · no_duplicate_person failed
trial 2 ✗ 125ms · no_duplicate_person failed
trial 3 ✗ 122ms · no_duplicate_person failed

✗ sales-world  0/3 (0%)
    ✗ no_duplicate_person  failed in 3/3 trials
      trial 1: The agent made 1 call(s) matching "POST /services/data/v67.0/sobjects/Lead", "POST /services/data/v67.0/sobjects/Contact" that it must not make: POST /services/data/v67.0/sobjects/Contact (seq 5).

fail (minimum pass rate 100%)  run run_20261110-232947_2df211226a6a4bbb

Call 5 created the Contact and got 201 Created. Only no_duplicate_person failed. The deal, the HubSpot update and the reply were all right, which is what makes this mistake easy to ship: every visible part of the job looks done.

With both lines fixed, the correct agent passes every trial:

$ ancha run sales-world.yaml --local -n 3
running locally; agent not sealed (network open, 5 variables from this shell)
run_20261110-232942_ea7d3c56d3699a11: 3 trials of sales-world, 1 at a time
trial 1 ✓ 129ms
trial 2 ✓ 119ms
trial 3 ✓ 125ms

✓ sales-world  3/3 (100%)

pass (minimum pass rate 100%)  run run_20261110-232942_ea7d3c56d3699a11

The fixed agent passing three of three shows the checks accept the right behavior. Each wrong agent failing three of three shows that every check trips on exactly the mistake it targets. The Salesforce, HubSpot and Gmail tool pages list every endpoint each replica serves.

Running the real SDKs against the world

Every SDK here lets you set where it sends requests. The agent’s code stays as it is, and only its configuration differs between the world and production. ancha connect prints the Gmail settings below and a plain fetch for Salesforce and HubSpot, and jsforce and HubSpot’s Node client take the same URLs. Each of these ran against this world:

import { auth, gmail } from '@googleapis/gmail';
import jsforce from 'jsforce';
import { Client } from '@hubspot/api-client';

const oauth = new auth.OAuth2();
oauth.setCredentials({ access_token: process.env.GMAIL_ACCESS_TOKEN });
const mail = gmail({ version: 'v1', auth: oauth });
const options = { rootUrl: process.env.GMAIL_API_URL + '/', retry: false };
const { data } = await mail.users.messages.list(
  { userId: 'me', q: 'from:martin.okafor@hollowayvet.example' },
  options
);

const salesforce = new jsforce.Connection({
  instanceUrl: process.env.SALESFORCE_INSTANCE_URL,
  accessToken: process.env.SALESFORCE_ACCESS_TOKEN,
  version: '67.0',
});
const { records } = await salesforce.query('SELECT Id, Name FROM Opportunity');

const hubspot = new Client({
  accessToken: process.env.HUBSPOT_ACCESS_TOKEN,
  basePath: process.env.HUBSPOT_API_URL,
  numberOfApiCallRetries: 0
});
const contacts = await hubspot.crm.contacts.basicApi.getPage(10);

The Google client takes rootUrl on each call, jsforce takes the instance URL and an API version, and HubSpot’s client takes basePath. In Python, Google’s client takes the endpoint in client_options:

import os
from google.oauth2.credentials import Credentials
from googleapiclient.discovery import build

gmail = build("gmail", "v1", credentials=Credentials(os.environ["GMAIL_ACCESS_TOKEN"]),
              client_options={"api_endpoint": os.environ["GMAIL_API_URL"]})

Try it with your own agent

Save sales-world.yaml into an empty folder and install the CLI, which needs Node.js 24:

npm install -g @ancha-labs/cli
ancha world up --scene sales-world.yaml

Each tool in the world gets an address and credentials, among them SALESFORCE_INSTANCE_URL, HUBSPOT_API_URL and GMAIL_API_URL. Wire them into your agent’s settings, let it do the job once, and check the result:

ancha check --env <world id>
ancha world down --env <world id>

To get three trials off one seed, tell Ancha how to start your agent:

ancha init --agent "node agent.mjs"
ancha run sales-world.yaml --local -n 3

Questions

Does Salesforce have a free org for testing?

Yes. Developer Edition orgs are free, and Salesforce may terminate one after 45 days without activity. Scratch orgs are disposable, start empty and last from 1 to 30 days, 7 by default.

Does HubSpot offer a test account or sandbox?

Two kinds. Developer test accounts are free, up to 10 per account, and expire after 90 days without API calls. Standard sandboxes copy your production account and need an Enterprise subscription.

How do I make a Gmail API reply land in the same thread?

Send it with the original thread’s threadId, set In-Reply-To and References to the original message’s Message-ID, and keep the subject. Gmail’s threading guide requires all three, the subject included.

Can the HubSpot API move a lifecycle stage backward?

Not in one step. The API moves a lifecycle stage forward only, in the stage order. To move a contact back, clear its current lifecycle stage first, then set the earlier one.