import '../assert_dev_env.js';
/**
 * Shared WebSocket client surface for cross-backend tests.
 *
 * `WsClient` is the interface every in-process or cross-process WS test
 * driver implements — `send` / `request` / `close` / `messages` /
 * `wait_for`. Two impls today:
 *
 * - **In-process** — `create_ws_test_harness` in `../ws_round_trip.ts`.
 *   Drives `register_action_ws` against a fake Hono upgrade so the
 *   dispatcher's full path runs without the wire upgrade.
 * - **Cross-process** — `create_ws_transport` in `./ws_transport.ts`.
 *   Wraps the native `WebSocket` upgrade against a real running binary,
 *   threading the session cookie captured by `FetchTransport`.
 *
 * Wire-frame types + predicates also live here so both impls (and every
 * shared assertion helper) reference one source.
 *
 * @module
 */
import { is_jsonrpc_error_response, is_jsonrpc_notification, is_jsonrpc_response, } from '../../http/jsonrpc_helpers.js';
import { JSONRPC_VERSION } from '../../http/jsonrpc.js';
// ---------------------------------------------------------------------
// Predicates — compose with `WsClient.wait_for` and
// `messages.filter(...)`. Both in-process and cross-process tests use
// the same names against the same shapes.
// ---------------------------------------------------------------------
/** Predicate matching a JSON-RPC notification with the given method name. */
export const is_notification = (method) => (msg) => is_jsonrpc_notification(msg) && msg.method === method;
/**
 * Type-guard combinator: match a notification whose typed `params` satisfies
 * `match`. Collapses the common test pattern of casting `msg` to
 * `JsonrpcNotificationFrame<P>` in every predicate body.
 *
 * ```ts
 * const match_roster_for = (id: Uuid) =>
 *   is_notification_with<RosterChangedParams>(
 *     WORLD_METHODS.roster_changed,
 *     (params) => params.character_id === id && !params.removed,
 *   );
 * const roster = await client.wait_for(match_roster_for(char_id));
 * ```
 */
export const is_notification_with = (method, match) => (msg) => is_jsonrpc_notification(msg) &&
    msg.method === method &&
    match(msg.params);
/** Predicate matching a JSON-RPC response frame (success or error) for the given request id. */
export const is_response_for = (id) => (msg) => (is_jsonrpc_response(msg) || is_jsonrpc_error_response(msg)) && msg.id === id;
// ---------------------------------------------------------------------
// WsClient — the test-driver surface every impl implements.
// ---------------------------------------------------------------------
/**
 * Default wait-for timeout shared across in-process + cross-process
 * impls. Tunable per-call via the `timeout_ms` parameter.
 */
export const WS_CLIENT_DEFAULT_TIMEOUT_MS = 1000;
