# Test mode

import { Mermaid } from "zudoku/mermaid";

There is no separate sandbox server. Test mode runs on the production API and is selected by the key:
requests made with your **test** partner key work on test tracking items, requests made with your
**live** key work on real ones. The two never see each other's tracking items.

## What is simulated

| | Live key | Test key |
| --- | --- | --- |
| Driver SMS / push notification | Sent | Never sent |
| Tracking requests consumed from the plan | Yes | Never |
| Driver behavior | Real driver in the app | You drive it with `POST /test/simulate` |
| Webhooks | Every minute while something changes | Only when you call `/test/simulate`, one attempt |
| Lifetime | Until `expDate` (max 10 days) | At most 24 hours: `expDate` / `schedule.end` must be within the next 24 hours (`400 EXP_DATE_INVALID` / `SCHEDULE_INVALID`), and the tracking is deleted automatically 24 hours after it was created |
| Limits | Your plan | 200 test tracking items created per key per UTC day (`429 TEST_LIMIT_REACHED`, and deleting test tracking items does not give the quota back) and 60 requests per minute per key (`429 RATE_LIMITED`). Both answers carry `Retry-After` |
| Not simulated | - | `publicLink` is always `null` and `driverFiles` always `[]`. No stop arrivals, no Smart Arrival Alerts status, no derived statuses (`notMoving`, `waiting`, `settings`) and no email updates. No text message is sent, so `driverInvite.smsStatus` stays `null`. `requestDriverDetails=true` returns a made-up driver (`Test Driver` on an iPhone), `requestPhoneSettings=true` healthy settings. History is a single page (`nextCursor` is always `null`). `TRACKING_LIMIT_REACHED` and `PAYMENT_FAILED` never occur |
| Potential Fraud Alerts | Signals from the driver's phone | A clean phone by default, raise or clear alerts with the `driver.flagged` event. Filled under the same rules as live: a PRO or VIP plan, only while the tracking is `active` or `stop` |
| [ELD and Hybrid](/eld-tracking) carrier | The carrier approves by email, the truck's ELD reports | No email and no ELD provider: only the shape of the `eld` block is checked, and you drive the carrier with the `carrier.*` events. The organization still needs ELD tracking enabled |

Apart from that, test mode behaves like live: the same endpoints, request validation, error codes,
response shapes and plan checks (for example `403 PRO_PLAN_REQUIRED` for stop `expectedArrival`), but
the plan's allowance is never checked or used. The create response carries `testMode: true`, and test
mode requires the client key (`x-api-client-key`) on create: without it the answer is
`400 ORG_IDENTIFIER_REQUIRED`, or `400 CLIENT_KEY_REQUIRED` when you send `mcNum` or `dotNum`.

The request examples in the API reference use fixed dates. With a test key, set `expDate` (or
`schedule.end`) to a time within the next 24 hours - the [Quickstart](/quickstart) shows how.

## Choosing the starting state

| Create request | Starting state |
| --- | --- |
| `driverPhone: 5555550100` | Driver already has the app: status `request` (Pending driver's approval) |
| Any other valid phone number | Pending installation: status `request` (Pending installation), `driverHasApp: false` |
| `schedule` instead of `expDate` | Status `scheduled` (the whole window must end within the next 24 hours). At `schedule.start` it becomes `request` (5555550100) or pending installation (any other valid phone) |
| `trackingSource: "eld"` | Status `requestEld` (Pending ELD admin approval), waiting for the carrier, `driverHasApp: false` |
| `trackingSource: "hybrid"` with `driverPhone: 5555550100` | Status `requestEld`, in the driver's app and waiting for the driver and the carrier |
| `trackingSource: "hybrid"` with any other valid phone | Status `requestEld`, pending installation (`driverHasApp: false`) and waiting for the carrier |
| `trackingSource` `eld` or `hybrid` with `schedule` | Status `scheduledEld`. At `schedule.start` it becomes `activeEld` when the carrier already approved, otherwise `requestEld` (a hybrid also loses its `schedule`) |

## Simulating events

```bash
curl -X POST https://api.load-market.com/v1/test/simulate \
  -H "x-api-key: YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{ "trackingId": "api-test-Zr8pL3nW6yQ1cV5kT9xB", "event": "location", "coords": { "lat": 39.781721, "lng": -89.650148, "speed": 28.6, "heading": 205, "battery": 0.82, "accuracy": 6.5, "locationSource": "gps" } }'
```

| Event | Allowed from | New status |
| --- | --- | --- |
| `driver.installed` | pending installation, or `scheduled` with another phone | `request` - the driver now has the app. A `scheduled` tracking gets `driverHasApp: true` and stays `scheduled` |
| `driver.accepted` | `request` (driver has the app, not pending installation) | `active` |
| `driver.rejected` | `request` (driver has the app, not pending installation) | `cancel` |
| `location` | `active` | `active`, with the new `driverCoords`. `coords.lat` and `coords.lng` are required. Optional, like a live driver-app fix: `speed` (m/s), `heading` (degrees, 0-360), `battery` (0-1), `accuracy` (meters) and `locationSource` (`gps` or `cell-tower`, default `gps`) |
| `driver.paused` | `active` | `stop` |
| `driver.resumed` | `stop` | `active` |
| `tracking.finished` | `active`, `stop` | `done` |
| `webhook.ping` | any | unchanged - sends the current state again |
| `driver.flagged` | any, except pending installation and `scheduled` | unchanged. Raises the [Potential Fraud Alerts](/fraud-alerts) listed in `alerts` (for example `["vpn", "ipOutsideUsCa"]`) on the simulated driver's phone, an empty list clears them. They show in `fraudAlerts` while the tracking is `active` or `stop` |

<Mermaid
  chart={`
stateDiagram-v2
    direction LR
    [*] --> pending: create (other phone)
    [*] --> request: create (5555550100)
    [*] --> scheduled: create with schedule
    scheduled --> request: schedule.start (5555550100)
    scheduled --> pending: schedule.start (other phone)
    pending --> request: driver.installed
    request --> active: driver.accepted
    request --> cancel: driver.rejected
    active --> active: location
    active --> stop: driver.paused
    stop --> active: driver.resumed
    active --> done: tracking.finished
    stop --> done: tracking.finished
`}
/>

An event that is not allowed from the current status returns `400 TEST_EVENT_INVALID`. Calling
`/test/simulate` with a live key returns `403 TEST_MODE_ONLY`.

### ELD and Hybrid test tracking items

The `carrier.*` events play the links of the carrier's approval email (Start, Stop and Cancel), and the
driver events play the hybrid's driver. On a phone test tracking the `carrier.*` events answer
`400 TEST_EVENT_INVALID`.

```bash
curl -X POST https://api.load-market.com/v1/test/simulate \
  -H "x-api-key: YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{ "trackingId": "api-test-Gn5vR8kQ2wM7xT4cL9pB", "event": "carrier.approved" }'
```

| Event | Allowed from | New status |
| --- | --- | --- |
| `carrier.approved` | `requestEld` waiting for the carrier, `scheduledEld`, or a hybrid in the driver's app the carrier has not approved (also after `carrier.rejected`) | `activeEld` - the truck's ELD starts reporting. On `scheduledEld` the approval is recorded and the status stays until `schedule.start`, then becomes `activeEld`. A hybrid the driver already moved keeps its status |
| `carrier.paused` | approved by the carrier | unchanged - the truck's ELD stops reporting |
| `carrier.resumed` | paused by the carrier | unchanged - the truck's ELD reports again |
| `carrier.rejected` | any, until the tracking is finished (not twice in a row) | ELD only: `cancelEld`. Hybrid: only the ELD part ends and the status stays, unless the driver declined too (`cancelEld`) |
| `driver.installed` | hybrid whose driver has no app | `driverHasApp: true`. A pending one moves into the driver's app with status `requestEld`, one the carrier already started keeps its status |
| `driver.accepted` | hybrid in the driver's app, `requestEld` or `activeEld` | `activeEld` - the phone reports too |
| `driver.rejected` | hybrid in the driver's app, not accepted | unchanged, or `cancelEld` when the carrier already cancelled |
| `driver.paused` | hybrid `activeEld` the driver accepted | `stopEld` - the truck's ELD keeps reporting |
| `driver.resumed` | `stopEld` | `activeEld` |
| `location` | `activeEld`, or a hybrid `stopEld` | unchanged, with the new `driverCoords`. A position from the truck's ELD (no live phone) has `battery`, `accuracy` and `locationSource` `null` |
| `tracking.finished` | `activeEld`, `stopEld` | `doneEld` |
| `webhook.ping` | any | unchanged - sends the current state again |
| `driver.flagged` | hybrid in the driver's app | unchanged, as above. Alerts show only while the driver has accepted the tracking |

<Mermaid
  chart={`
stateDiagram-v2
    direction LR
    [*] --> requestEld: create
    [*] --> scheduledEld: create with schedule
    scheduledEld --> requestEld: schedule.start, not approved
    scheduledEld --> activeEld: schedule.start, carrier.approved before
    requestEld --> activeEld: carrier.approved, or hybrid driver.accepted
    requestEld --> cancelEld: carrier.rejected (ELD only)
    activeEld --> stopEld: hybrid driver.paused
    stopEld --> activeEld: driver.resumed
    activeEld --> cancelEld: carrier.rejected (hybrid - driver declined too)
    activeEld --> doneEld: tracking.finished
    stopEld --> doneEld: tracking.finished
`}
/>

Like on live tracking items, `driverCoords` is returned only while the tracking is `active` or `stop` (ELD:
`activeEld` or `stopEld`): after `tracking.finished` it is `null` (see
[Tracking lifecycle](/tracking-lifecycle#driver-position)).

## Test webhooks

Every `/test/simulate` call sends **one** signed webhook with the new state to the webhook URL
configured on your **test** key - no retries, no batching with other tracking items. The URL is never taken
from the request. The response tells you what your endpoint did:

```json
{
  "webhook": {
    "attempted": true,
    "status": 200,
    "webhookId": "msg_2b7c1f0e-5d3a-4c8e-9b61-7a4f0d2e9c35",
    "reason": null
  }
}
```

- `attempted: false` - webhooks are not enabled or no webhook URL is configured on the test key
  (`reason` says so).
- `status` - the HTTP status your endpoint returned, or `null` when it did not answer within 7 seconds or
  could not be reached (`reason` explains).

The payload, headers and signature are identical to live webhooks, so the signature check you write
against test mode works unchanged in production. The item's `events` carries the event the simulate stands
for, with the same name as the live [event](/webhooks#events) (`driver.accepted`, `driver.paused`,
`carrier.rejected`, ...). `location`, `webhook.ping` and `driver.flagged` change only the snapshot and carry no
event. The response returns the same `events`. Stop arrivals and Smart Arrival Alerts are not simulated, so
their events (`stop.*`) come from live tracking items only. See [Webhooks](/webhooks).

## Going live

1. Replace the test key with the live key in your configuration.
2. Use real driver phone numbers - the driver receives a push notification or an SMS invitation.
3. Expect live webhooks once per minute while a tracking changes, with up to 2 retries.
