Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 148 additions & 0 deletions tests/custom/test_live_integration_harness.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
"""Offline regressions for the live harness; never call the live tests."""

import importlib.util
import os
import subprocess
import sys
from pathlib import Path
from types import SimpleNamespace
from zipfile import ZipFile

import httpx
import pytest

ROOT = Path(__file__).resolve().parents[2]


def load_harness(name, monkeypatch):
monkeypatch.setenv("SMALLEST_API_KEY", "test-key")
spec = importlib.util.spec_from_file_location(name, ROOT / "tests/integration" / (name + ".py"))
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module


@pytest.mark.parametrize("name", ["test_caller_context_live", "test_sdk_smoke_live"])
@pytest.mark.parametrize(
"base", ["http://127.0.0.1:9999", "https://api.india.smallest.ai", "https://api.dev.smallest.ai"]
)
def test_sdk_targets_selected_host(name, base, monkeypatch):
monkeypatch.setenv("SMALLEST_BASE", base)
module = load_harness(name, monkeypatch)
seen = []
real_client = httpx.Client

def respond(request):
seen.append(request)
return httpx.Response(200, json={"status": "success", "data": []})

monkeypatch.setattr(httpx, "Client", lambda **kwargs: real_client(**kwargs, transport=httpx.MockTransport(respond)))
module._client().atoms.phone_numbers.list()
assert len(seen) == 1
assert str(seen[0].url).startswith(base + "/atoms/v1/")
assert seen[0].headers["Authorization"] == "Bearer test-key"


def test_probe_bundle_installs_checkout_wheel(tmp_path, monkeypatch):
module = load_harness("test_caller_context_live", monkeypatch)
wheel_name = "smallestai-5.14.0-py3-none-any.whl"

def build(args, **kwargs):
assert args[:7] == [sys.executable, "-m", "pip", "wheel", "--no-deps", "--no-build-isolation", "--wheel-dir"]
assert args[-1] == str(ROOT)
(tmp_path / wheel_name).write_bytes(b"wheel built from this checkout")
return SimpleNamespace(returncode=0)

monkeypatch.setattr(module.subprocess, "run", build)
module._build_probe_package(tmp_path)
from smallestai.cli.utils import create_zip_from_directory

with ZipFile(create_zip_from_directory(tmp_path)) as bundle:
import tomli

project = tomli.loads(bundle.read("pyproject.toml").decode())
assert project["tool"]["poetry"]["dependencies"]["smallestai"] == {"path": wheel_name}
assert "requirements.txt" not in bundle.namelist()
assert bundle.read(wheel_name) == b"wheel built from this checkout"
assert bundle.read("server.py").decode() == module.PROBE_SERVER


@pytest.mark.parametrize("stage", ["success", "init", "deploy"])
def test_deploy_uses_same_host_and_no_login(stage, monkeypatch):
module = load_harness("test_caller_context_live", monkeypatch)
module.BASE = "https://api.india.smallest.ai"
monkeypatch.setenv("SMALLEST_BASE_URL", "https://unrelated.example")
monkeypatch.setattr(module, "_build_probe_package", lambda path: None)
seen = []

def run(args, **kwargs):
seen.append((args, kwargs))
assert args[:2] == [sys.executable, str(ROOT / "tests/integration/_crew_cli.py")]
assert kwargs["env"]["SMALLEST_BASE_URL"] == module.BASE
assert kwargs["env"]["SMALLEST_API_KEY"] == "test-key"
assert kwargs["env"]["PYTHONPATH"] == str(ROOT / "src")
assert "test-key" not in " ".join(args)
assert "auth" not in args
return SimpleNamespace(returncode=int(args[2] == stage), stdout="Build ID: build-1\nSUCCEEDED", stderr="failed")

monkeypatch.setattr(module.subprocess, "run", run)
if stage == "success":
assert module._deploy_probe_crew("agent-1") == "build-1"
else:
with pytest.raises(AssertionError, match=f"{stage} failed"):
module._deploy_probe_crew("agent-1")
assert not Path(seen[0][1]["cwd"]).exists()


def test_crew_runner_uses_real_cli_without_auth_files(tmp_path):
env = {
**os.environ,
"SMALLEST_API_KEY": "test-key",
"SMALLEST_BASE_URL": "https://api.example",
"PYTHONPATH": str(ROOT / "src"),
}
result = subprocess.run(
[sys.executable, str(ROOT / "tests/integration/_crew_cli.py"), "init", "--agent-id", "agent-1"],
cwd=tmp_path,
env=env,
capture_output=True,
text=True,
)
assert result.returncode == 0, result.stderr
assert "agent-1" in (tmp_path / ".smallestai/config.toml").read_text()
assert not (tmp_path / ".smallestai/credentials.json").exists()


def test_make_live_uses_selected_host(monkeypatch):
module = load_harness("test_caller_context_live", monkeypatch)
module.BASE = "http://127.0.0.1:9999"
from unittest.mock import MagicMock

response = MagicMock()
response.__enter__.return_value.status = 200

def urlopen(request, timeout):
assert request.full_url == module.BASE + "/atoms/v1/sdk/agents/agent/builds/build"
assert request.method == "PATCH"
assert request.get_header("Authorization") == "Bearer test-key"
assert request.data == b'{"isLive": true}'
return response

monkeypatch.setattr(module.urllib.request, "urlopen", urlopen)
module._set_build_live("agent", "build")


def test_wheel_failure_stops_deployment_and_cleans_up(monkeypatch):
module = load_harness("test_caller_context_live", monkeypatch)
seen = []

def fail(args, **kwargs):
seen.append(args)
raise subprocess.CalledProcessError(1, args)

monkeypatch.setattr(module.subprocess, "run", fail)
with pytest.raises(subprocess.CalledProcessError):
module._deploy_probe_crew("agent")
assert len(seen) == 1
assert seen[0][2:4] == ["pip", "wheel"]
assert not Path(seen[0][7]).exists()
65 changes: 65 additions & 0 deletions tests/integration/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# SDK live integration suite

Real calls against a real environment — the common baseline we run on every SDK
change so we know the core surface still works, plus a feature test for the thing
we just shipped. No mocks, no WireMock. Gated by env vars, so the normal unit CI
skips it.

Everything uses the **SDK** (not raw HTTP) wherever an SDK method exists. The two
spots that have no SDK method yet are flagged inline as gaps: crew code deploy
(only the `smallestai agent-crew` CLI) and making a crew build live
(`PATCH /sdk/agents/{id}/builds/{bid}`).

## Setup

```bash
export SMALLEST_API_KEY=sk_... # required
export SMALLEST_BASE=https://api.smallest.ai # optional; use https://api.dev.smallest.ai for dev
export SMALLEST_TEST_NUMBER=+91XXXXXXXXXX # a phone YOU can answer (caller-context test only)
```

## Run

```bash
# Fast, fully automated — "nothing is broken" baseline (no telephony, no human):
poetry run pytest --noconftest tests/integration/test_sdk_smoke_live.py -m integration -s

# Caller context end-to-end (PRO-3560) — you must ANSWER the call it places:
poetry run pytest --noconftest tests/integration/test_caller_context_live.py -m integration -s
# or just run it and watch the payload print:
poetry run python tests/integration/test_caller_context_live.py
```

## What each covers

- **test_sdk_smoke_live.py** — create agent → get round-trip → archive; list phone
numbers. Catches a broken client / auth / response-shape regression. Add a case
here whenever a new core surface ships.
- **test_caller_context_live.py** — the full caller-context chain on a real call:
orchestrator forwards the caller block into `system.init` → SDK exposes it as
`session.caller` / `session.initial_variables` → crew reads it. The crew emits
what it read as a structured `caller.probe` event; the test reads that event's
**payload** (raw `session.caller` data, not the spoken/transcribed line) off the
conversation and asserts `user_number` / `agent_number` / `direction` / `call_id`.

## Convention for a new SDK change

1. Smoke stays green (nothing broke).
2. Add a focused live test for the new feature next to it (mirror the pattern:
gate on env, use the SDK, assert the observable behavior, clean up after).

## Reproducible caller probe

The caller probe builds a wheel from the current checkout and includes it in the
uploaded crew bundle. A pyproject with a local wheel dependency selects the
backend build path that copies the whole bundle before installing dependencies.
The remote build installs that wheel and its declared
runtime dependencies, rather than a separately published SDK release. Before
running it, install the local build tools with `poetry run python -m pip install
pip poetry-core`. The test uses the same interpreter and checkout for crew CLI
commands, passes credentials in the environment, and never reads or updates your
saved CLI login. Temporary build files are removed on success and failure.

`SMALLEST_BASE` is used consistently for SDK, crew CLI, and raw HTTP calls,
including regional and localhost hosts. `--noconftest` avoids the global wire-test
Docker fixture; these live tests do not use WireMock.
23 changes: 23 additions & 0 deletions tests/integration/_crew_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""Run the real crew commands without reading or writing a user's saved login."""

import os
from types import SimpleNamespace

from smallestai.cli.agent_crew import initialise_agent_crew_app
from smallestai.cli.lib.atoms import AtomsAPIClient
from smallestai.cli.lib.project_config import ProjectConfig


def main():
key = os.environ["SMALLEST_API_KEY"]
base = os.environ["SMALLEST_BASE_URL"]
app = initialise_agent_crew_app(
ProjectConfig(),
SimpleNamespace(get_credentials=lambda: {"access_token": key}),
AtomsAPIClient(base_url=base),
)
app()


if __name__ == "__main__":
main()
Loading
Loading