1- import { describe , it , afterEach , expect , vi } from "vitest" ;
1+ import { describe , it , beforeEach , expect , vi } from "vitest" ;
22import { concatUint8Arrays , stringToUint8Array } from "uint8array-extras" ;
33import * as StellarSdk from "../../../src/index.js" ;
44
55import { serverUrl } from "../../constants" ;
66
7- // `Client.from` constructs its own `Server` internally, so we cannot spy on a
8- // server instance we create here. Instead we mock the http-client factory that
9- // the internal `Server` uses, which lets the real code path
10- // (`getContractInstance`, then `getContractWasmByHash` for Wasm contracts) run
11- // against controlled JSON-RPC responses.
12- const { mockPost } = vi . hoisted ( ( ) => ( { mockPost : vi . fn ( ) } ) ) ;
13-
14- vi . mock ( "../../../src/rpc/axios.js" , async ( importActual ) => {
15- const actual =
16- await importActual < typeof import ( "../../../src/rpc/axios.js" ) > ( ) ;
17- return {
18- ...actual ,
19- createHttpClient : ( ) => ( { post : mockPost } ) ,
20- } ;
21- } ) ;
22-
23- const { xdr, hash, Contract } = StellarSdk ;
7+ const { xdr, hash, Contract, rpc } = StellarSdk ;
248const { Client } = StellarSdk . contract ;
9+ const { Server } = rpc ;
2510
2611const networkPassphrase = "Test SDF Network ; September 2015" ;
2712
@@ -65,8 +50,21 @@ function wasmWithSpec(entries: StellarSdk.xdr.ScSpecEntry[]): Uint8Array {
6550}
6651
6752describe ( "contract.Client.from" , ( ) => {
68- afterEach ( ( ) => {
69- vi . clearAllMocks ( ) ;
53+ let server : any ;
54+ let mockPost : any ;
55+
56+ // `Client.from` accepts a pre-built `Server` via `options.server`, so spying
57+ // on that instance's http client lets the real code path
58+ // (`getContractInstance`, then `getContractWasmByHash` for wasm contracts)
59+ // run against controlled JSON-RPC responses without mocking a module.
60+ beforeEach ( ( ) => {
61+ server = new Server ( serverUrl ) ;
62+ // The default throws rather than calling through: `vi.spyOn` keeps the real
63+ // implementation, so once the queued `mockResolvedValueOnce` responses run
64+ // out an unexpected call would otherwise issue a real HTTP request.
65+ mockPost = vi . spyOn ( server . httpClient , "post" ) . mockImplementation ( ( ) => {
66+ throw new Error ( "unexpected RPC call" ) ;
67+ } ) ;
7068 } ) ;
7169
7270 const contractId = "CCN57TGC6EXFCYIQJ4UCD2UDZ4C3AQCHVMK74DGZ3JYCA5HD4BY7FNPC" ;
@@ -155,10 +153,13 @@ describe("contract.Client.from", () => {
155153 contractId,
156154 networkPassphrase,
157155 rpcUrl : serverUrl ,
156+ server,
158157 } ) ;
159158
160159 expect ( client ) . toBeInstanceOf ( Client ) ;
161160 expect ( client . spec . funcs ( ) . length ) . toBeGreaterThan ( 0 ) ;
161+ // The instance lookup, then the wasm fetch.
162+ expect ( mockPost ) . toHaveBeenCalledTimes ( 2 ) ;
162163 } ) ;
163164 } ) ;
164165
@@ -191,6 +192,7 @@ describe("contract.Client.from", () => {
191192 contractId,
192193 networkPassphrase,
193194 rpcUrl : serverUrl ,
195+ server,
194196 } ) ;
195197
196198 expect ( client ) . toBeInstanceOf ( Client ) ;
@@ -204,6 +206,7 @@ describe("contract.Client.from", () => {
204206 ] ) {
205207 expect ( typeof ( client as any ) [ method ] ) . toBe ( "function" ) ;
206208 }
209+ expect ( mockPost ) . toHaveBeenCalledTimes ( 1 ) ;
207210 } ) ;
208211 } ) ;
209212} ) ;
0 commit comments