From 441fb6a81913c635e43de193ec68d49abd318eb6 Mon Sep 17 00:00:00 2001 From: rishavmehra Date: Tue, 18 Nov 2025 16:46:19 +0530 Subject: [PATCH] test: unit tests for get-account-info --- .../test/get-account-info.test.ts | 135 ++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 packages/solana-client/test/get-account-info.test.ts diff --git a/packages/solana-client/test/get-account-info.test.ts b/packages/solana-client/test/get-account-info.test.ts new file mode 100644 index 00000000..98c95665 --- /dev/null +++ b/packages/solana-client/test/get-account-info.test.ts @@ -0,0 +1,135 @@ +import { address } from '@solana/kit' +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' +import { getAccountInfo } from '../src/get-account-info.ts' +import type { SolanaClient } from '../src/solana-client.ts' + +describe('get-account-info', () => { + describe('expected behavior', () => { + it('should call rpc.getAccountInfo with the correct address', async () => { + // ARRANGE + expect.assertions(2) + const testAddress = 'So11111111111111111111111111111111111111112' + const AccountInfo = { + value: { + data: new Uint8Array([1, 2, 3]), + executable: false, + lamports: 1000000n, + owner: address('11111111111111111111111111111111'), + rentEpoch: 0n, + }, + } + const mockSend = vi.fn().mockResolvedValue(AccountInfo) + const mockGetAccountInfo = vi.fn().mockReturnValue({ send: mockSend }) + const mockClient = { + rpc: { + getAccountInfo: mockGetAccountInfo, + }, + } as unknown as SolanaClient + + // ACT + const result = await getAccountInfo(mockClient, { address: testAddress }) + + // ASSERT + expect(mockGetAccountInfo).toHaveBeenCalledWith(address(testAddress)) + expect(result).toEqual(AccountInfo) + }) + + it('should handle account with no data', async () => { + // ARRANGE + expect.assertions(1) + const testAddress = 'So11111111111111111111111111111111111111112' + const AccountInfo = { + value: null, + } + const mockSend = vi.fn().mockResolvedValue(AccountInfo) + const mockGetAccountInfo = vi.fn().mockReturnValue({ send: mockSend }) + const mockClient = { + rpc: { + getAccountInfo: mockGetAccountInfo, + }, + } as unknown as SolanaClient + + // ACT + const result = await getAccountInfo(mockClient, { address: testAddress }) + + // ASSERT + expect(result).toEqual(AccountInfo) + }) + + it('should handle account with executable program', async () => { + // ARRANGE + expect.assertions(2) + const testAddress = 'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA' + const AccountInfo = { + value: { + data: new Uint8Array([1, 2, 3, 4, 5]), + executable: true, + lamports: 5000000n, + owner: address('11111111111111111111111111111111'), + rentEpoch: 100n, + }, + } + const mockSend = vi.fn().mockResolvedValue(AccountInfo) + const mockGetAccountInfo = vi.fn().mockReturnValue({ send: mockSend }) + const mockClient = { + rpc: { + getAccountInfo: mockGetAccountInfo, + }, + } as unknown as SolanaClient + + // ACT + const result = await getAccountInfo(mockClient, { address: testAddress }) + + // ASSERT + expect(mockGetAccountInfo).toHaveBeenCalledWith(address(testAddress)) + expect(result.value?.executable).toBe(true) + }) + }) + + describe('unexpected behavior', () => { + beforeEach(() => { + vi.spyOn(console, 'log').mockImplementation(() => {}) + }) + + afterEach(() => { + vi.restoreAllMocks() + }) + + it('should throw an error when rpc call fails', async () => { + // ARRANGE + expect.assertions(1) + const testAddress = 'So11111111111111111111111111111111111111112' + const mockError = new Error('RPC request failed') + const mockSend = vi.fn().mockRejectedValue(mockError) + const mockGetAccountInfo = vi.fn().mockReturnValue({ send: mockSend }) + const mockClient = { + rpc: { + getAccountInfo: mockGetAccountInfo, + }, + } as unknown as SolanaClient + + // ACT & ASSERT + await expect(getAccountInfo(mockClient, { address: testAddress })).rejects.toThrow('RPC request failed') + }) + + it('should throw an error with invalid address format', () => { + // ARRANGE + expect.assertions(1) + const invalidAddress = 'invalid-address' + const mockSend = vi.fn() + const mockGetAccountInfo = vi.fn().mockReturnValue({ send: mockSend }) + const mockClient = { + rpc: { + getAccountInfo: mockGetAccountInfo, + }, + } as unknown as SolanaClient + + // ACT & ASSERT + expect(() => + getAccountInfo(mockClient, { + address: invalidAddress, + }), + ).toThrow() + }) + }) +})