コンテンツにスキップ

Network

The observed-request model the request / event / requestSequence assertions match against.

bajutsu.evidence.network

Network observation — the exchange model and the in-process collector.

How traffic is observed (DESIGN: network): a Simulator app runs as a host process and shares the Mac's loopback, so the app POSTs each request/response it makes to a small collector bajutsu runs on 127.0.0.1:<port> (the port is injected into the app via launch env, BAJUTSU_COLLECTOR, and a per-run shared token via BAJUTSU_COLLECTOR_TOKEN — the collector accepts only POSTs bearing that token, so another local process can't inject fabricated exchanges). The collector keeps the exchanges in memory so a step's request assertion can be evaluated in real time, and dumps them to network.json as scenario evidence.

The in-app side that captures and POSTs the exchanges is a separate Swift package (BajutsuKit); this module is only the bajutsu-side receiver and data model.

NetworkExchange

Bases: BaseModel

One request/response the app reported.

Extra keys from the SDK are ignored (forward-compatible); field names accept their JSON aliases.

Collector

Bases: Protocol

The exchange source the run loop and evidence writer drive.

Independent of how it observed the traffic: the iOS NetworkCollector receives POSTs over HTTP; the web WebNetworkCollector hooks Playwright events — both satisfy this, so the pipeline stays backend-agnostic.

NetworkCollector

Receives exchanges POSTed by the app and holds them for assertion + evidence.

Thread-safe: the HTTP server runs on a background thread while the run loop reads snapshot() on the main thread. clear() between scenarios scopes the exchanges.

add(data)

Validate and store one reported exchange.

A payload that fails validation is dropped rather than raised, so an SDK change can't break the run mid-flight (forward-compatible, matching NetworkExchange's extra="ignore").

check_token(candidate)

Constant-time compare of a presented token against this run's token.

Mirrors serve's own token check; false before start() mints a token.

snapshot()

The exchanges received so far, in arrival order.

snapshot_timed()

Each exchange with its receive time (monotonic), in arrival order.

clear()

Drop all stored exchanges — called between scenarios to scope them to one run.

start(port=0)

Start the receiver on the loopback interface and begin accepting the app's POSTs.

Parameters:

Name Type Description Default
port int

TCP port to bind on 127.0.0.1; 0 requests an ephemeral port.

0

Returns:

Type Description
int

The actual bound port (resolved when port is 0), to inject into the app via

int

BAJUTSU_COLLECTOR.

stop()

Stop the receiver and release its socket. Idempotent — a no-op if never started.