import CallableInstance from "callable-instance"; /** * Mock implementation of an RPC (Remote Procedure Call) handler for testing purposes. * Extends CallableInstance to allow the RPC instance to be called as a function * to simulate RPC method calls with mocked responses. * * @extends CallableInstance * * @example * ```typescript * const rpc = new Rpc('getUserById'); * * // Add a mocked response * rpc.addResponse({ id: 123 }, { name: 'John Doe', email: 'john@example.com' }); * * // Call the RPC method * const result = await rpc('getUserById', { id: 123 }); * console.log(result); // { name: 'John Doe', email: 'john@example.com' } * ``` */ declare class Rpc extends CallableInstance<[methodName: string, params: object], Promise> { /** @private The name of the RPC method this instance handles */ private methodName; /** @private Arguments passed during construction */ private args; /** Number of successful RPC calls */ succeeded: number; /** Number of failed RPC calls */ failed: number; /** Total number of RPC calls made */ total: number; /** Dictionary of mocked responses keyed by parameter hash */ mocks: Record; /** @private Callback function for handling RPC responses */ private callback; /** * Creates a new RPC mock instance for a specific method * * @param {string} methodName - The name of the RPC method to mock * @param {object} [args={}] - Optional arguments for the RPC method * * @example * ```typescript * const userRpc = new Rpc('getUser'); * const orderRpc = new Rpc('createOrder', { timeout: 5000 }); * ``` */ constructor(methodName: string, args?: object); /** * Handles RPC method calls by returning mocked responses or calling registered callbacks. * This method is called when the RPC instance is invoked as a function. * * @param {string} methodName - The name of the RPC method being called * @param {object} [params={}] - Parameters to pass to the RPC method * @returns {Promise} Promise resolving to the mocked response or callback result * @throws {Error} If method name doesn't match or no response is configured * * @example * ```typescript * const result = await rpc('getUserById', { id: 123 }); * ``` */ instanceMethod(methodName: string, params?: object): Promise; /** * Registers a callback function to handle RPC responses when no specific mock is found * * @param {Function} fn - Callback function to handle RPC calls * @param {boolean} fn.error - Whether an error occurred * @param {object} fn.params - Parameters passed to the RPC call * @param {Function} fn.callback - Callback to return the response * * @example * ```typescript * rpc.addCallback((error, params, callback) => { * if (params.id === 404) { * callback(new Error('User not found'), null); * } else { * callback(null, { id: params.id, name: 'Default User' }); * } * }); * ``` */ addCallback(fn: Function): void; /** * Adds a mocked response for specific parameters or as a default response. * Supports both static responses (objects) and dynamic responses (functions). * * @param {...[params: object, response: object | Function] | [response: object | Function]} args * - If two arguments: first is params object, second is response * - If one argument: argument is the default response for any params * * @throws {Error} If a mocked response already exists for the given parameters * * @example * ```typescript * // Static response for specific parameters * rpc.addResponse({ id: 123 }, { name: 'John Doe' }); * * // Dynamic response function * rpc.addResponse({ id: 456 }, (params) => ({ * name: `User ${params.id}`, * timestamp: Date.now() * })); * * // Default response for any parameters * rpc.addResponse({ name: 'Default User' }); * ``` */ addResponse(...args: [params: object, response: object | Function] | [response: object | Function]): void; /** * Removes a mocked response for specific parameters * * @param {object} [params] - Parameters to identify which response to remove. * If undefined, removes the default response. * * @example * ```typescript * // Remove response for specific parameters * rpc.removeResponse({ id: 123 }); * * // Remove default response * rpc.removeResponse(); * ``` */ removeResponse(params?: object | undefined): void; /** * Removes all mocked responses, including default responses * * @example * ```typescript * rpc.removeAllResponses(); * // Now all RPC calls will use the callback or throw errors * ``` */ removeAllResponses(): void; /** * Resets all statistics counters to zero * * @example * ```typescript * console.log(rpc.total); // 5 * rpc.resetStats(); * console.log(rpc.total); // 0 * ``` */ resetStats(): void; /** * Resets both mocked responses and statistics. * Equivalent to calling removeAllResponses() and resetStats(). * * @example * ```typescript * // Clean slate for next test * rpc.resetAll(); * ``` */ resetAll(): void; } export default Rpc;