testing/cross_backend/ws_round_trip.ts

Cross-process WebSocket round-trip suite — the cross-process counterpart to the in-process testing/ws_round_trip.ts harness.

Where the in-process harness drives register_action_ws against a fake Hono upgrade (no wire), this suite performs a real WebSocket upgrade against a spawned backend via create_ws_transport (the ws npm package), so the actual upgrade handshake + per-connection auth + JSON-RPC dispatch over the socket are exercised end-to-end. It is the only coverage of the spawned binary's live WS path — the standard cross-process bundle (describe_standard_cross_process_tests) omits WS by design, so consumers call this alongside it.

Consumer-agnostic, apart from the cases rpc_path and capabilities.ws_account_actions gate. Every other case drives the heartbeat protocol action, which assert_ws_endpoints_include_protocol_actions guarantees is present on every WS endpoint — so the suite needs no knowledge of a consumer's domain WS methods. It validates the transport, not the domain.

Three cases mirror the upgrade route register_action_ws mounts (origin check → require_auth → dispatch): an authenticated upgrade round-trips heartbeat (the first case), and an anonymous upgrade and a disallowed-origin upgrade are refused (after the spine-parity cases). Per-connection auth is enforced at upgrade time (not per message), so the negative cases assert the upgrade itself rejects rather than a per-message error frame.

Spine-parity cases follow the first. Both spines admit a connection only after re-reading its credential, and requests that arrive first wait for that rather than being dropped: requests sent the moment the socket opens are all answered, and — gated on capabilities.ws_handshake_pipelining, with the ordering forced — so are frames written in the same TCP write as the upgrade request (driven through connect_raw_ws, since a client library sends nothing before its open event). heartbeat answers the parameterless shapes (absent, {}) and refuses a declared param with invalid_params, and a message over the backend's cap (max_message_bytes, default DEFAULT_WS_MAX_MESSAGE_BYTES) closes the socket with WS_CLOSE_MESSAGE_TOO_BIG and WS_CLOSE_MESSAGE_TOO_BIG_REASON.

The cases after the refused upgrades (gated on rpc_path) cover server-initiated close. Per-message dispatch never re-checks credential validity, so an open socket ends only when a revocation closes it — which every revocation does, once its transaction commits, and every close here is asserted with WS_CLOSE_SESSION_REVOKED and WS_CLOSE_SESSION_REVOKED_REASON, so a dropped connection can't pass for one. An authenticated socket is dropped when the account's sessions are revoked over the keeper's session channel (account_session_revoke_all), and when its account is deleted (account_delete, also gated on capabilities.account_lifecycle). The rest of the revocation table follows, each closing a second account's socket: account_token_revoke closes the revoked token's bearer socket and leaves a socket on the account's other token open; admin_token_revoke_all, POST /logout, POST /password, and account_purge (also gated on capabilities.account_lifecycle) close the account's bearer and session sockets — the rows create_account_revocations shares with the SSE suite; and a token the per-account cap evicts (max_tokens further mints) has its bearer socket closed. Omit rpc_path to skip them (consumers without the standard account and admin actions on their RPC endpoint).

The last cases are gated on capabilities.ws_account_actions — the backend's WS endpoint mounts the self-service account actions: a socket that revokes its own sessions *over that socket* is closed with WS_CLOSE_SESSION_REVOKED after reading the reply to its request — both spines answer the revoking request ahead of the close, so the caller is never left without an answer. Under the same gate (and rpc_path), a socket mints an API token over WS and the token's token_create audit row is read back after _testing_drain_effects — the barrier a WS mutation needs, since a WS reply can precede its audit write. The binary's create_testing_actions mount provides the drain.

Gated on capabilities.ws — backends without an end-to-end WS transport skip (the cases still surface as .skip in the report). Cross-process only: create_ws_transport needs a real bound socket, so wire it from a *.cross.test.ts file, never an in-process setup.

view source

Declarations
#

2 declarations

CrossProcessWsTestOptions
#

testing/cross_backend/ws_round_trip.ts view source

CrossProcessWsTestOptions import type {CrossProcessWsTestOptions} from '@fuzdev/fuz_app/testing/cross_backend/ws_round_trip.js';

inheritance

setup_test

Per-test fixture producer (default_cross_process_setup(handle)). The authenticated cases upgrade on the fresh-per-test keeper's session cookie (open_session_ws).

type (): Promise<TestFixture>

readonly
returns Promise<TestFixture>

capabilities

Backend capability flags; every case gates on capabilities.ws.

type BackendCapabilities

readonly

rpc_path?

RPC endpoint path (e.g. /api/rpc) used by the close-on-revoke case to fire account_session_revoke_all over the keeper's session channel. When omitted, that case is skipped — it depends on the standard account actions being mounted on the RPC endpoint.

type string

readonly

max_message_bytes?

The backend's inbound WebSocket message cap, in bytes — the oversized-message case sends a message just past it. Defaults to DEFAULT_WS_MAX_MESSAGE_BYTES; pass the consumer's value when it raises the server cap.

type number

readonly

logout_path?

REST logout route path, for the logout close case. Default /api/account/logout.

type string

readonly

password_path?

REST password-change route path, for its close case. Default /api/account/password.

type string

readonly

max_tokens?

The backend's per-account API token cap, which the token-cap close case mints past. Defaults to DEFAULT_MAX_TOKENS; null (a backend with the cap disabled) skips the case.

type number | null

readonly

ws_account_roles?

Roles each second account whose socket a revocation case watches is created with, for a backend whose WS upgrade requires a role (an admin-gated /api/ws refuses a roleless account's upgrade). Each must be admin-grantable — the fixture offers it. Default none.

type readonly string[]

readonly

describe_cross_process_ws_tests
#

testing/cross_backend/ws_round_trip.ts view source

(options: CrossProcessWsTestOptions): void import {describe_cross_process_ws_tests} from '@fuzdev/fuz_app/testing/cross_backend/ws_round_trip.js';

Register the cross-process WS round-trip suite over a real upgrade: authed heartbeat round-trip, heartbeat's parameterless-shape contract, an oversized message closing with WS_CLOSE_MESSAGE_TOO_BIG, anonymous-upgrade refusal, disallowed-origin refusal, and — when rpc_path is supplied — each listed revocation (session and token revokes, admin revoke-all, logout, password change, token-cap eviction, account delete and purge) closing the live socket, plus a self-revoking socket reading its reply before its close, and a WS mutation's audit row read back after _testing_drain_effects.

options

returns

void

Depends on
#

Imported by
#