11import { afterEach , describe , expect , it , vi } from 'vitest' ;
22
3+ import type { SolanaRpcClient } from '../rpc/createSolanaRpcClient' ;
34import type { SolanaClientConfig } from '../types' ;
45import { createClient } from './createClient' ;
56
@@ -9,8 +10,6 @@ type Logger = ReturnType<ReturnType<typeof createLoggerMock>>;
910
1011type Helpers = ReturnType < typeof createClientHelpersMock > ;
1112
12- const createSolanaRpcMock = vi . hoisted ( ( ) => vi . fn ( ( ) => ( { rpc : true } ) ) ) ;
13- const createSolanaRpcSubscriptionsMock = vi . hoisted ( ( ) => vi . fn ( ( ) => ( { sub : true } ) ) ) ;
1413const createActionsMock = vi . hoisted ( ( ) =>
1514 vi . fn ( ( ) => ( {
1615 setCluster : vi . fn ( ) . mockResolvedValue ( undefined ) ,
@@ -37,10 +36,23 @@ const createWalletRegistryMock = vi.hoisted(() =>
3736const createLoggerMock = vi . hoisted ( ( ) => vi . fn ( ( ) => vi . fn ( ) ) ) ;
3837const formatErrorMock = vi . hoisted ( ( ) => vi . fn ( ( error : unknown ) => ( { formatted : error } ) ) ) ;
3938const nowMock = vi . hoisted ( ( ) => vi . fn ( ( ) => 111 ) ) ;
39+ const createSolanaRpcClientMock = vi . hoisted ( ( ) =>
40+ vi . fn (
41+ ( ) =>
42+ ( {
43+ commitment : 'confirmed' ,
44+ endpoint : 'https://rpc.example' ,
45+ rpc : { tag : 'rpc' } ,
46+ rpcSubscriptions : { tag : 'sub' } ,
47+ sendAndConfirmTransaction : vi . fn ( ) ,
48+ simulateTransaction : vi . fn ( ) ,
49+ websocketEndpoint : 'wss://rpc.example' ,
50+ } ) satisfies SolanaRpcClient ,
51+ ) ,
52+ ) ;
4053
41- vi . mock ( '@solana/kit' , ( ) => ( {
42- createSolanaRpc : createSolanaRpcMock ,
43- createSolanaRpcSubscriptions : createSolanaRpcSubscriptionsMock ,
54+ vi . mock ( '../rpc/createSolanaRpcClient' , ( ) => ( {
55+ createSolanaRpcClient : createSolanaRpcClientMock ,
4456} ) ) ;
4557
4658vi . mock ( '../logging/logger' , ( ) => ( {
@@ -87,8 +99,11 @@ describe('createClient', () => {
8799
88100 it ( 'instantiates the client with runtime wiring and helpers' , async ( ) => {
89101 const client = createClient ( config ) ;
90- expect ( createSolanaRpcMock ) . toHaveBeenCalledWith ( 'https://rpc.example' ) ;
91- expect ( createSolanaRpcSubscriptionsMock ) . toHaveBeenCalledWith ( 'https://rpc.example' ) ;
102+ expect ( createSolanaRpcClientMock ) . toHaveBeenCalledWith ( {
103+ commitment : 'finalized' ,
104+ endpoint : 'https://rpc.example' ,
105+ websocketEndpoint : 'https://rpc.example' ,
106+ } ) ;
92107 expect ( createWalletRegistryMock ) . toHaveBeenCalledWith ( config . walletConnectors ) ;
93108 expect ( createActionsMock ) . toHaveBeenCalled ( ) ;
94109 expect ( createWatchersMock ) . toHaveBeenCalled ( ) ;
@@ -110,6 +125,24 @@ describe('createClient', () => {
110125 expect ( client . store . getState ( ) . cluster . status ) . toEqual ( { status : 'idle' } ) ;
111126 } ) ;
112127
128+ it ( 'respects a provided rpcClient instance' , ( ) => {
129+ const rpcClient = {
130+ commitment : 'processed' ,
131+ endpoint : 'https://rpc.example' ,
132+ rpc : { tag : 'external-rpc' } ,
133+ rpcSubscriptions : { tag : 'external-sub' } ,
134+ sendAndConfirmTransaction : vi . fn ( ) ,
135+ simulateTransaction : vi . fn ( ) ,
136+ websocketEndpoint : 'wss://rpc.example' ,
137+ } satisfies SolanaRpcClient ;
138+ createClient ( {
139+ ...config ,
140+ commitment : 'processed' ,
141+ rpcClient,
142+ } ) ;
143+ expect ( createSolanaRpcClientMock ) . not . toHaveBeenCalled ( ) ;
144+ } ) ;
145+
113146 it ( 'logs errors when initial cluster setup fails' , async ( ) => {
114147 const logger = vi . fn ( ) ;
115148 createLoggerMock . mockReturnValueOnce ( logger as Logger ) ;
0 commit comments