|
| 1 | +import { nativeToScVal } from "@stellar/stellar-base"; |
| 2 | +import { TransactionBuilder } from "@stellar/stellar-sdk"; |
| 3 | + |
| 4 | +import { SorobanSdkCore } from "../core"; |
| 5 | +import type { InvocationAfterContext, InvocationBeforeContext } from "../types"; |
| 6 | + |
| 7 | +describe("invocation middleware", () => { |
| 8 | + it("runs before/after hooks for read and simulate lifecycle", async () => { |
| 9 | + const before: InvocationBeforeContext[] = []; |
| 10 | + const after: InvocationAfterContext[] = []; |
| 11 | + |
| 12 | + const core = new SorobanSdkCore({ |
| 13 | + horizonUrl: "https://horizon-testnet.stellar.org", |
| 14 | + sorobanRpcUrl: "https://soroban-testnet.stellar.org", |
| 15 | + networkPassphrase: "Test SDF Network ; September 2015", |
| 16 | + simulationSource: |
| 17 | + "GAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAWHF", |
| 18 | + middleware: [ |
| 19 | + { |
| 20 | + before: (ctx) => before.push(ctx), |
| 21 | + after: (ctx) => after.push(ctx), |
| 22 | + }, |
| 23 | + ], |
| 24 | + }); |
| 25 | + |
| 26 | + const artifact = { |
| 27 | + contractId: "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD2KM", |
| 28 | + method: "get_event_count", |
| 29 | + args: [], |
| 30 | + }; |
| 31 | + |
| 32 | + (core as any).buildInvokeTransaction = jest.fn().mockResolvedValue({}); |
| 33 | + (core as any).retryPolicy.execute = jest.fn( |
| 34 | + async (fn: () => Promise<unknown>) => fn(), |
| 35 | + ); |
| 36 | + (core as any).rpcServer.simulateTransaction = jest.fn().mockResolvedValue({ |
| 37 | + result: { retval: nativeToScVal(12, { type: "u32" }) }, |
| 38 | + }); |
| 39 | + |
| 40 | + const value = await core.read<number>("eventManager", artifact, {}); |
| 41 | + |
| 42 | + expect(value).toBe(12); |
| 43 | + expect(before.map((ctx) => ctx.stage)).toEqual(["read", "simulate"]); |
| 44 | + expect(after.map((ctx) => ctx.stage)).toEqual(["simulate", "read"]); |
| 45 | + expect(after.every((ctx) => ctx.success)).toBe(true); |
| 46 | + }); |
| 47 | + |
| 48 | + it("runs write/send/wait hooks and captures errors", async () => { |
| 49 | + const before: InvocationBeforeContext[] = []; |
| 50 | + const after: InvocationAfterContext[] = []; |
| 51 | + |
| 52 | + const core = new SorobanSdkCore({ |
| 53 | + horizonUrl: "https://horizon-testnet.stellar.org", |
| 54 | + sorobanRpcUrl: "https://soroban-testnet.stellar.org", |
| 55 | + networkPassphrase: "Test SDF Network ; September 2015", |
| 56 | + middleware: [ |
| 57 | + { |
| 58 | + before: (ctx) => before.push(ctx), |
| 59 | + after: (ctx) => after.push(ctx), |
| 60 | + }, |
| 61 | + ], |
| 62 | + }); |
| 63 | + |
| 64 | + const artifact = { |
| 65 | + contractId: "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD2KM", |
| 66 | + method: "purchase_ticket", |
| 67 | + args: [], |
| 68 | + }; |
| 69 | + |
| 70 | + (core as any).prepareWrite = jest.fn().mockResolvedValue({ |
| 71 | + xdr: "AAAA", |
| 72 | + networkPassphrase: "Test SDF Network ; September 2015", |
| 73 | + source: "GAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAWHF", |
| 74 | + }); |
| 75 | + const fromXdrSpy = jest |
| 76 | + .spyOn(TransactionBuilder, "fromXDR") |
| 77 | + .mockReturnValue({} as never); |
| 78 | + (core as any).rpcServer.sendTransaction = jest |
| 79 | + .fn() |
| 80 | + .mockResolvedValue({ status: "PENDING", hash: "abc123" }); |
| 81 | + (core as any).retryPolicy.execute = jest.fn( |
| 82 | + async (fn: () => Promise<unknown>) => fn(), |
| 83 | + ); |
| 84 | + (core as any).waitForTransaction = jest |
| 85 | + .fn() |
| 86 | + .mockRejectedValue(new Error("boom from confirmation")); |
| 87 | + |
| 88 | + await expect( |
| 89 | + core.write("eventManager", artifact, { |
| 90 | + source: "GAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAWHF", |
| 91 | + signTransaction: async () => "AAAA", |
| 92 | + }), |
| 93 | + ).rejects.toThrow("boom from confirmation"); |
| 94 | + |
| 95 | + fromXdrSpy.mockRestore(); |
| 96 | + |
| 97 | + expect(before.map((ctx) => ctx.stage)).toEqual([ |
| 98 | + "write", |
| 99 | + "sendTransaction", |
| 100 | + "waitForTransaction", |
| 101 | + ]); |
| 102 | + const writeAfter = after.find((ctx) => ctx.stage === "write"); |
| 103 | + expect(writeAfter?.success).toBe(false); |
| 104 | + expect(writeAfter?.error).toBeDefined(); |
| 105 | + }); |
| 106 | +}); |
0 commit comments