testing/rpc_round_trip.ts

Schema-driven round-trip validation for RPC endpoints.

For every RPC method, generates valid params and fires JSON-RPC requests (POST for all methods, GET for reads), validating that responses are well-formed JSON-RPC. Successful responses are validated against the method's declared output schema. DB-backed via the suite's setup_test fixture-producing callback.

Cadence: per-describe setup_test() call (see testing/round_trip.ts module docstring). The generic POST and GET cases fire one JSON-RPC envelope per method-direction against shared principals (an authed account, an admin account, the keeper). A side-effecting method called as the authed account runs on a fresh account of its own (round_trip_uses_fresh_account): the generated params of a self-targeting method act on the caller — an account_delete with no account_id deletes it, an account_session_revoke_all ends its session — and on the shared account that would leave every later call refused as unauthenticated. A 401 to any non-public call fails (assert_rpc_authenticated), so a credential lost some other way goes red rather than vacuous. The success_fixtures case also mutates — each fixture builds a row and the call revokes, deletes, or decides it — so it runs last on the describe's fixture, and each factory seeds its own rows.

view source

Declarations
#

8 declarations

assert_rpc_authenticated
#

testing/rpc_round_trip.ts view source

(method: string, status: number, body: unknown, principal: AuthPrincipal): void import {assert_rpc_authenticated} from '@fuzdev/fuz_app/testing/rpc_round_trip.js';

Guard against a refusal that says the call carried no valid credential when it carried one (is_unauthenticated_refusal). The round-trip sends every non-public method a live credential its auth admits, so such a 401 means the backend failed to authenticate it — most often because an earlier call destroyed it (a self-delete, a revoke-all), which would turn every later method's coverage into a vacuous refusal. A public method may still answer 401 (a login with a wrong password), and so may a handler refusing an authenticated call with a reason of its own (invalid_credentials).

method

the RPC method name

type string

status

the HTTP status

type number

body

the parsed response body (or raw text when not JSON)

type unknown

principal

the principal the call was sent as

returns

void

describe_rpc_round_trip_tests
#

testing/rpc_round_trip.ts view source

(options: RpcRoundTripTestOptions): void import {describe_rpc_round_trip_tests} from '@fuzdev/fuz_app/testing/rpc_round_trip.js';

Run schema-driven round-trip validation for RPC endpoints.

For each method:

  1. Generate valid params from the action's input schema
  2. Fire a POST request with JSON-RPC envelope
  3. For side_effects: false methods, also fire a GET request
  4. Validate response is well-formed JSON-RPC; successful responses are also validated against the method's declared output schema

Error responses (from missing DB state, etc.) are expected and validated as well-formed JSON-RPC errors. Successful responses are validated against action.spec.output. Two error classes fail loud instead of passing as a valid envelope: method not found (-32601) means the backend is missing a method the local surface advertises (assert_method_implemented), and a crash — HTTP 500, internal_error (-32603), or a ≥500 with no JSON-RPC envelope — means the backend failed on schema-valid input (assert_not_crash). A declared ≥500 code (service_unavailable, timeout) passes as a refusal. A method the suite can't drive generically goes in skip_methods; one whose generated params can't reach its handler gets an input_overrides entry.

options

returns

void

pick_rpc_auth
#

testing/rpc_round_trip.ts view source

(method: AppSurfaceRpcMethod, keeper: KeeperHeaderProvider, authed_account: TestAccount, admin_account: TestAccount): RpcRoundTripAuth import {pick_rpc_auth} from '@fuzdev/fuz_app/testing/rpc_round_trip.js';

Pick the principal matching an RPC method's auth requirement (pick_auth_principal, shared with the REST round-trip), and the headers carrying its credential. Accepts any KeeperHeaderProvider — both TestApp (in-process) and TestFixture (cross-backend) satisfy the shape structurally.

method

keeper

authed_account

admin_account

returns

RpcRoundTripAuth

RpcRoundTripAuth
#

testing/rpc_round_trip.ts view source

RpcRoundTripAuth import type {RpcRoundTripAuth} from '@fuzdev/fuz_app/testing/rpc_round_trip.js';

A picked principal and the headers that carry its credential.

principal

type AuthPrincipal

headers

type Record<string, string>

RpcRoundTripTestOptions
#

testing/rpc_round_trip.ts view source

RpcRoundTripTestOptions import type {RpcRoundTripTestOptions} from '@fuzdev/fuz_app/testing/rpc_round_trip.js';

setup_test

Per-test fixture-producing function (per-describe cadence).

type (): Promise<TestFixture>

returns Promise<TestFixture>

surface_source

App surface (with route + RPC endpoint specs) for RPC endpoint enumeration. Constructed in TS by the consumer; same shape for in-process and cross-process tests.

type AppSurfaceSpec

session_options

Session config — only needed to resolve factory-form rpc_endpoints against a stub AppServerContext at setup time (the actions' input schemas drive params generation; auth/dispatch run against the real backend through fixture.transport).

type SessionOptions<string>

rpc_endpoints

RPC endpoint specs — eager array or factory. The factory must return the same endpoint path + spec.method list regardless of ctx (invoked once at setup with a stub ctx; the real per-test live dispatch goes through whatever the backend was started with).

type RpcEndpointsSuiteOption

skip_methods?

Methods to skip, by name (e.g., 'zap_plan').

type string[]

input_overrides?

Override generated params for specific methods (method name → params).

type Map<string, Record<string, unknown>>

success_fixtures?

Success-case fixtures for methods whose populated success body the generic nil-id input can't reach — referential reads (*_get, *_log) whose required ids must point at existing rows. Maps method name to an async factory that receives the per-test fixture (so it can seed the referenced state — e.g. create a repo via fixture.transport + fixture.create_session_headers()) and returns the params that drive a success response.

Distinct from input_overrides, which only swaps the request params; the response may still be a valid *error* envelope (missing-row not_found), which the generic loop accepts. A success_fixtures entry asserts the response is ok and validates result against the method's output schema — so a backend that drops a field, or errors where the other backend succeeds, fails loud. This is the success-shape parity check the nil-id round-trip structurally cannot perform (it only ever sees error envelopes for referential methods).

Fired as POST. The factory runs against the shared per-describe fixture, so it must not assume a clean slate between entries (seed unique state). By default the request carries the identity the generic loop picks for the method's auth (authed / admin / keeper); a case that returns headers sends those instead — for methods whose row must belong to the caller (revoking one's own token or session, retracting one's own offer). create_spine_rpc_success_fixtures (cross_backend/rpc_success_fixtures.ts) builds the map for the standard spine methods.

type RpcSuccessFixtures

RpcSuccessFixture
#

testing/rpc_round_trip.ts view source

RpcSuccessFixture import type {RpcSuccessFixture} from '@fuzdev/fuz_app/testing/rpc_round_trip.js';

One success-fixture factory: builds the row against the per-test fixture, then returns the request.

(call)

type (fixture: TestFixture): Promise<RpcSuccessFixtureCase>

fixture

returns Promise<RpcSuccessFixtureCase>

RpcSuccessFixtureCase
#

testing/rpc_round_trip.ts view source

RpcSuccessFixtureCase import type {RpcSuccessFixtureCase} from '@fuzdev/fuz_app/testing/rpc_round_trip.js';

One success-fixture request: the params, and optionally the caller's headers.

params

Params that drive a success response.

type Record<string, unknown>

headers?

Auth headers to send in place of the identity the suite picks from the method's auth — for a row that only its owner may act on.

type Record<string, string>

RpcSuccessFixtures
#

testing/rpc_round_trip.ts view source

RpcSuccessFixtures

type Map<string, RpcSuccessFixture>

import type {RpcSuccessFixtures} from '@fuzdev/fuz_app/testing/rpc_round_trip.js';

Success-case fixtures, method name → async factory. See RpcRoundTripTestOptions.success_fixtures.

Depends on
#

Imported by
#