export { MockCall, MockFn, assertConnectError, createFakeMethod, createFakeService, createMockDescField, createMockDescMessage, createMockDescMethod, createMockFn, createMockNext, createMockNextError, createMockNextSlow, createMockRequest, createMockStream } from '@connectum/test-fixtures'; import { DescService } from '@bufbuild/protobuf'; import * as _connectrpc_connect from '@connectrpc/connect'; import { Client, ServiceImpl, Interceptor } from '@connectrpc/connect'; import { Server, RemoteResolver, ServiceCatalog, Context } from '@connectum/core'; export { I as InMemoryMetricCollector, a as InMemorySpanCollector, N as NormalizedMetric, b as NormalizedSpan, T as TRANSPORT_METRIC_ATTRIBUTE, c as TRANSPORT_SPAN_ATTRIBUTE } from './otel-collectors-uZ4OWw4a.js'; export { FakeMethodOptions, FakeServiceOptions, MockDescFieldOptions, MockDescMessageOptions, MockDescMethodOptions, MockNextOptions, MockRequestOptions, MockStreamOptions } from '@connectum/test-fixtures/types'; import '@opentelemetry/sdk-metrics'; import '@opentelemetry/sdk-trace-base'; /** * Thin wrapper over {@link Server.localClient} for ergonomic test usage. * * This helper exists primarily for symmetry with {@link createTestServer} * and to keep tests independent of `@connectum/core` internals — they can * `import { createLocalClient } from "@connectum/testing"` and get a working * in-process client without having to know about the `Server.localClient` * method or `createLocalTransport`. * * Unlike `createTestServer`, no HTTP/2 socket is opened: the call goes * straight through the in-memory router transport. * * @module createLocalClient */ /** * Create an in-process ConnectRPC client for a service registered on the given Server. * * @param server - A server created via `createServer({...})`. Does not need to be started. * @param service - The proto service descriptor (e.g. `GreeterService`). * @returns A typed ConnectRPC `Client` that invokes handlers via the in-memory pipe. * * @example * ```typescript * import { createServer } from "@connectum/core"; * import { createLocalClient } from "@connectum/testing"; * * const server = createServer({ services: [greeterRoutes] }); * const client = createLocalClient(server, GreeterService); * const res = await client.sayHello({ name: "world" }); * ``` */ declare function createLocalClient(server: Server, service: T): Client; /** * `mockResolver` — a {@link RemoteResolver} backed by in-memory mock services. * * Use it to exercise catalog `ctx.call` / `ctx.stream` (or `server.client`) * against canned implementations without a network hop. Every mock response is * tagged with the {@link MOCK_RESPONSE_HEADER} so tests can assert the call was * served by a mock rather than a real transport. * * @module mockResolver */ /** Response header set on every mock-served response. */ declare const MOCK_RESPONSE_HEADER = "x-connectum-mock"; /** A mocked service: its proto descriptor paired with a (partial) implementation. */ interface MockService { readonly service: DescService; readonly impl: Partial>; } /** * Type-safe constructor for a {@link MockService}. Pairs a service descriptor * with handlers typed against it. * * @example * ```ts * mockService(InventoryService, { * getStock: () => create(StockSchema, { units: 7 }), * }); * ``` */ declare function mockService(service: S, impl: Partial>): MockService; /** * Build a {@link RemoteResolver} that serves the given mocks in-process. Returns * `null` for any service not in the mock set (so it composes with real * resolvers via `mapResolver`-style fallbacks). */ declare function mockResolver(mocks: readonly MockService[]): RemoteResolver; /** * `createMockContext` — a Connectum {@link Context} for unit-testing handler * `ctx.call` / `ctx.stream` logic in isolation. * * It drives the SAME dispatch path as a live request: a real `Server` is built * with the given catalog and a {@link mockResolver}, and a synthetic * `HandlerContext` is wrapped through the server's catalog dispatcher. So * resolver lookup, cascade injection, interceptor composition and error * semantics all match production — there is no parallel mock dispatch path to * drift from. * * @module mockContext */ /** Options for {@link createMockContext}. */ interface CreateMockContextOptions { /** The catalog the handler-under-test calls into. */ readonly catalog: ServiceCatalog; /** Mock implementations served via the catalog's resolver path. */ readonly mocks: readonly MockService[]; /** Optional outgoing interceptors (applied exactly as in production). */ readonly outgoingInterceptors?: readonly Interceptor[]; /** Optional inbound headers (seen by `ctx.requestHeader` + header propagation). */ readonly requestHeader?: HeadersInit; /** Optional inbound deadline in ms (drives the `ctx.timeoutMs()` cascade). */ readonly timeoutMs?: number; /** Optional header names propagated onto outgoing calls (default none). */ readonly propagateHeaders?: readonly string[]; } /** * Create a {@link Context} whose `ctx.call` / `ctx.stream` resolve against the * given mocks. Pass it as the second argument to a handler under test. * * @example * ```ts * const ctx = createMockContext({ * catalog: defineCatalog({ [InventoryService.typeName]: InventoryService }), * mocks: [mockService(InventoryService, { getStock: () => create(StockSchema, { units: 7 }) })], * }); * const res = await orderHandler(create(CreateOrderSchema, { sku: "x" }), ctx); * ``` */ declare function createMockContext(options: CreateMockContextOptions): Context; /** A running test server with transport and cleanup. */ interface TestServer { /** Pre-configured client transport connected to the test server. */ transport: _connectrpc_connect.Transport; /** Server base URL (e.g. `http://localhost:54321`). */ baseUrl: string; /** Assigned port number. */ port: number; /** Stop the server and close all connections. */ close(): Promise; } /** Options for {@link createTestServer}. */ interface CreateTestServerOptions { /** ConnectRPC service route handlers. */ services: unknown[]; /** Interceptors to apply. Default: `[]` */ interceptors?: unknown[]; /** Protocol extensions (Healthcheck, Reflection). Default: `[]` */ protocols?: unknown[]; /** Port number. Default: `0` (random available port) */ port?: number; } /** * Test server utilities for integration testing. * * Provides {@link createTestServer} to spin up a real gRPC server on a random port * and {@link withTestServer} for automatic lifecycle management. * * @module test-server */ /** * Create and start a test server on a random (or specified) port. * * Returns a {@link TestServer} with a pre-configured gRPC transport * ready for use with ConnectRPC clients. The caller is responsible * for calling {@link TestServer.close} when done. * * @param options - Server configuration (services, interceptors, protocols, port) * @returns Running test server with transport and cleanup function * * @example * ```typescript * const server = await createTestServer({ services: [myRoutes] }); * const client = createClient(MyService, server.transport); * const response = await client.myMethod({ id: "1" }); * await server.close(); * ``` */ declare function createTestServer(options: CreateTestServerOptions): Promise; /** * Run a test function with an auto-managed test server. * * Creates a test server, passes it to {@link testFn}, and guarantees * cleanup via `finally` — even if the test throws. * * @param options - Server configuration (services, interceptors, protocols, port) * @param testFn - Async function that receives the running test server * @returns The value returned by testFn * * @example * ```typescript * const result = await withTestServer({ services: [myRoutes] }, async (server) => { * const client = createClient(MyService, server.transport); * return client.myMethod({ id: "1" }); * }); * ``` */ declare function withTestServer(options: CreateTestServerOptions, testFn: (server: TestServer) => Promise): Promise; export { type CreateMockContextOptions, type CreateTestServerOptions, MOCK_RESPONSE_HEADER, type MockService, type TestServer, createLocalClient, createMockContext, createTestServer, mockResolver, mockService, withTestServer };