|
| 1 | +import { isSolanaRpcResponse, type SolanaRpcResponse } from '../rpc-api'; |
| 2 | +import type { Slot } from '../typed-numbers'; |
| 3 | + |
| 4 | +describe('isSolanaRpcResponse', () => { |
| 5 | + it('returns true for a well-formed envelope', () => { |
| 6 | + const envelope: SolanaRpcResponse<{ lamports: bigint }> = { |
| 7 | + context: { slot: 99n as Slot }, |
| 8 | + value: { lamports: 5n }, |
| 9 | + }; |
| 10 | + expect(isSolanaRpcResponse(envelope)).toBe(true); |
| 11 | + }); |
| 12 | + |
| 13 | + it('accepts envelopes whose value is `undefined` or `null`', () => { |
| 14 | + expect(isSolanaRpcResponse({ context: { slot: 7n }, value: undefined })).toBe(true); |
| 15 | + expect(isSolanaRpcResponse({ context: { slot: 8n }, value: null })).toBe(true); |
| 16 | + }); |
| 17 | + |
| 18 | + it('ignores extra fields on `context` (future-proof against new envelope fields)', () => { |
| 19 | + expect(isSolanaRpcResponse({ context: { apiVersion: '2.0', slot: 1n }, value: 42 })).toBe(true); |
| 20 | + }); |
| 21 | + |
| 22 | + it('returns false for raw notifications without an envelope', () => { |
| 23 | + expect(isSolanaRpcResponse({ parent: 9n, root: 8n, slot: 10n })).toBe(false); |
| 24 | + }); |
| 25 | + |
| 26 | + it('returns false for primitives', () => { |
| 27 | + expect(isSolanaRpcResponse(42)).toBe(false); |
| 28 | + expect(isSolanaRpcResponse('hello')).toBe(false); |
| 29 | + expect(isSolanaRpcResponse(true)).toBe(false); |
| 30 | + }); |
| 31 | + |
| 32 | + it('returns false for nullish input', () => { |
| 33 | + expect(isSolanaRpcResponse(undefined)).toBe(false); |
| 34 | + expect(isSolanaRpcResponse(null)).toBe(false); |
| 35 | + }); |
| 36 | + |
| 37 | + it('returns false when `value` is missing', () => { |
| 38 | + expect(isSolanaRpcResponse({ context: { slot: 1n } })).toBe(false); |
| 39 | + }); |
| 40 | + |
| 41 | + it('returns false when `context` is missing', () => { |
| 42 | + expect(isSolanaRpcResponse({ value: 42 })).toBe(false); |
| 43 | + }); |
| 44 | + |
| 45 | + it('returns false when `context` is not an object', () => { |
| 46 | + expect(isSolanaRpcResponse({ context: 'oops', value: 42 })).toBe(false); |
| 47 | + expect(isSolanaRpcResponse({ context: null, value: 42 })).toBe(false); |
| 48 | + }); |
| 49 | + |
| 50 | + it('returns false when `context.slot` is missing', () => { |
| 51 | + expect(isSolanaRpcResponse({ context: { apiVersion: '2.0' }, value: 42 })).toBe(false); |
| 52 | + }); |
| 53 | + |
| 54 | + it('returns false when `context.slot` is not a bigint', () => { |
| 55 | + expect(isSolanaRpcResponse({ context: { slot: 1 }, value: 42 })).toBe(false); |
| 56 | + expect(isSolanaRpcResponse({ context: { slot: '1' }, value: 42 })).toBe(false); |
| 57 | + expect(isSolanaRpcResponse({ context: { slot: null }, value: 42 })).toBe(false); |
| 58 | + }); |
| 59 | +}); |
0 commit comments