Skip to content

Testing Guide

Updated: 2026-04-10

Environment Setup

Python SDK

Terminal window
cd py
# Install dependencies + dev tools (pytest, ruff, mypy, pytest-httpx)
pip install -e ".[dev]"

Create py/.env (required only for integration tests):

IMBRACE_API_KEY=api_xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
IMBRACE_BASE_URL=https://app-gatewayv2.imbrace.co
IMBRACE_ORG_ID=org_xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

TypeScript SDK

Terminal window
cd ts
npm install

Python — Running & Testing

Unit Tests (No API key needed)

Terminal window
cd py
# Run all unit tests
pytest tests/unit -v
# Run a specific file
pytest tests/unit/test_http.py -v
pytest tests/unit/resources/test_channel.py -v
# Run a specific test case
pytest tests/unit/test_http.py::test_401_raises_auth_error -v
# Run by keyword
pytest tests/unit -k "channel" -v
pytest tests/unit -k "boards" -v

Integration Tests (Requires real API key)

Integration tests perform real calls to the API Gateway.

Terminal window
cd py
# Option 1: Set key directly in command
IMBRACE_API_KEY=api_xxx pytest tests/integration -v -m integration
# Option 2: Use .env (SDK reads it automatically)
pytest tests/integration -v -m integration

Run All (Unit + Integration)

Terminal window
pytest tests/ -v

Coverage Report

Terminal window
pytest tests/unit --cov=src/imbrace --cov-report=term-missing

TypeScript — Running & Testing

Build

Terminal window
cd ts
# Single build
npm run build
# Build in watch mode (auto-rebuild on change)
npm run dev
# Clean dist/
npm run clean

Unit Tests (No API key needed)

Terminal window
cd ts
# Run all unit tests
npm test
# Run a specific file
npx vitest run tests/unit/http.test.ts
npx vitest run tests/unit/resources/contacts.test.ts
# Watch mode (auto-run on code changes)
npm run test:watch

Integration Tests (Requires real API key)

Terminal window
cd ts
IMBRACE_API_KEY=api_xxx npm run test:integration

Run All (Unit + Integration)

Terminal window
npm run test:all

Detailed Test Case Breakdown

Python — Unit Tests

tests/unit/test_auth.py — TokenManager

Verifies thread-safe token storage and deletion.

Test caseVerification
test_initial_token_noneInitial token is None
test_initial_token_setToken is stored correctly from constructor
test_set_tokenToken updates successfully
test_clear_tokenToken is deleted, get_token() returns None
test_thread_safety2 concurrent threads — no crashes, no race conditions

tests/unit/test_exceptions.py — Error classes

Error TypeOccurrence Scenario
AuthErrorServer returns 401, 403
ApiErrorServer returns other 4xx, 5xx
NetworkErrorConnection lost, timeout
Test caseVerification
test_hierarchyAll 3 are subclasses of ImbraceError
test_api_error_messagestatus_code and message follow format [404] Not Found
test_auth_error_is_catchable_as_imbrace_errorCatchable using except ImbraceError

tests/unit/test_http.py — HttpTransport

Uses pytest-httpx to simulate server — no real requests made.

Test caseScenarioVerification
test_get_successServer returns 200Response parsed correctly
test_sets_api_key_headerRequest with api_keyHeader x-api-key is present
test_sets_bearer_token_headerRequest with access tokenToken overrides api_key
test_401_raises_auth_errorServer returns 401Raises AuthError, no retry
test_403_raises_auth_errorServer returns 403Raises AuthError, no retry
test_404_raises_api_errorServer returns 404Raises ApiError(status_code=404)
test_500_retries_then_raisesServer returns 500 consecutivelyRetries twice → total 3 requests → raises ApiError
test_network_error_retries_then_raisesNetwork interruptedRetries twice → raises NetworkError

tests/unit/test_client.py — ImbraceClient

Test caseVerification
test_default_base_urlDefault URL is correct
test_custom_base_urlTrailing slash is stripped
test_reads_api_key_from_envReads IMBRACE_API_KEY from env
test_explicit_api_key_overrides_envDirect parameter takes priority over env
test_all_resources_initializedAll resources are initialized
test_set_access_tokenToken is updated correctly
test_context_managerwith block automatically calls close()

tests/unit/resources/ — Resource tests

FileEndpoint Verified
test_account.pyGET /v1/backend/account
test_agent.pyGET/POST/PATCH/DELETE /v2/backend/templates
test_ai.pyAI completion, embedding, streaming
test_auth.pyOTP signin, verify, signout
test_boards.pyBoard CRUD, items, search, export CSV
test_campaigns.pyCampaign CRUD, touchpoints list/get/create/update/delete/validate
test_channel.pyChannel list, get, delete, conv count
test_contacts.pyContacts list, search, update, notifications
test_conversations.pyViews count, create, search
test_messages.pyList, send (text/image)
test_message_suggestion.pyPOST /v1/message-suggestion — AI reply suggestions
test_organizations.pyList, pagination, auth header
test_predict.pyPOST /predict/ — ML-based scoring
test_sessions.pyList sessions, directory filter
test_settings.pyMessage templates, users, bulk invite
test_teams.pyList, my teams, add/remove users
test_workflows.pyList, tag filter, channel automation, create/update

Lint & Type Check

Python

Terminal window
cd py
# Check code style
ruff check src/ tests/
# Automatically fix fixable ruff errors
ruff check src/ tests/ --fix
# Check type annotations
mypy src/imbrace

TypeScript

Terminal window
cd ts
# Type check
npm run typecheck
# Lint
npm run lint
# Build (compile to JS)
npm run build