|
| 1 | +// Mock declarations must come first |
| 2 | +vi.mock('@elizaos/core'); |
| 3 | +vi.mock('ai-agent-sdk-js'); |
| 4 | + |
| 5 | +import { vi, describe, it, expect, beforeEach } from 'vitest'; |
| 6 | +import type { IAgentRuntime, Memory, State } from '@elizaos/core'; |
| 7 | +import { generateObject } from '@elizaos/core'; |
| 8 | +import { attpsPriceQuery } from '../../src/actions/attpsPriceQuery'; |
| 9 | + |
| 10 | +describe('attpsPriceQuery', () => { |
| 11 | + const mockRuntime: IAgentRuntime = { |
| 12 | + composeState: vi.fn(), |
| 13 | + updateRecentMessageState: vi.fn(), |
| 14 | + getSetting: vi.fn() |
| 15 | + } as unknown as IAgentRuntime; |
| 16 | + |
| 17 | + const mockMessage: Memory = { |
| 18 | + userId: 'test-user', |
| 19 | + agentId: 'test-agent', |
| 20 | + roomId: 'test-room', |
| 21 | + content: { |
| 22 | + text: 'query price' |
| 23 | + } |
| 24 | + } as Memory; |
| 25 | + |
| 26 | + const mockState: State = {}; |
| 27 | + const mockCallback = vi.fn(); |
| 28 | + const mockFetch = vi.fn(); |
| 29 | + |
| 30 | + beforeEach(() => { |
| 31 | + vi.clearAllMocks(); |
| 32 | + vi.mocked(mockRuntime.composeState).mockResolvedValue(mockState); |
| 33 | + vi.mocked(mockRuntime.updateRecentMessageState).mockResolvedValue(mockState); |
| 34 | + global.fetch = mockFetch; |
| 35 | + }); |
| 36 | + |
| 37 | + describe('validate', () => { |
| 38 | + it('should always return true', async () => { |
| 39 | + const result = await attpsPriceQuery.validate(mockRuntime, mockMessage); |
| 40 | + expect(result).toBe(true); |
| 41 | + }); |
| 42 | + }); |
| 43 | + |
| 44 | + describe('handler', () => { |
| 45 | + const mockPriceQuery = { |
| 46 | + sourceAgentId: 'test-source-agent', |
| 47 | + feedId: 'test-feed' |
| 48 | + }; |
| 49 | + |
| 50 | + const mockPriceResponse = { |
| 51 | + code: 0, |
| 52 | + message: 'success', |
| 53 | + result: { |
| 54 | + askPrice: '100.50', |
| 55 | + bidPrice: '100.40', |
| 56 | + midPrice: '100.45', |
| 57 | + validTimeStamp: '1234567890' |
| 58 | + } |
| 59 | + }; |
| 60 | + |
| 61 | + it('should successfully fetch price data', async () => { |
| 62 | + // Mock generateObject to return price query params |
| 63 | + vi.mocked(generateObject).mockResolvedValueOnce({ |
| 64 | + object: mockPriceQuery |
| 65 | + }); |
| 66 | + |
| 67 | + // Mock successful API response |
| 68 | + mockFetch.mockResolvedValueOnce({ |
| 69 | + json: () => Promise.resolve(mockPriceResponse) |
| 70 | + }); |
| 71 | + |
| 72 | + const result = await attpsPriceQuery.handler( |
| 73 | + mockRuntime, |
| 74 | + mockMessage, |
| 75 | + mockState, |
| 76 | + {}, |
| 77 | + mockCallback |
| 78 | + ); |
| 79 | + |
| 80 | + expect(mockCallback).toHaveBeenCalledWith(expect.objectContaining({ |
| 81 | + text: expect.stringContaining('Ask price: 100.5') |
| 82 | + })); |
| 83 | + expect(mockFetch).toHaveBeenCalledWith( |
| 84 | + expect.stringContaining('sourceAgentId=test-source-agent') |
| 85 | + ); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should handle price query params generation failure', async () => { |
| 89 | + // Mock generateObject to throw an error |
| 90 | + vi.mocked(generateObject).mockRejectedValueOnce( |
| 91 | + new Error('Failed to generate params') |
| 92 | + ); |
| 93 | + |
| 94 | + await attpsPriceQuery.handler( |
| 95 | + mockRuntime, |
| 96 | + mockMessage, |
| 97 | + mockState, |
| 98 | + {}, |
| 99 | + mockCallback |
| 100 | + ); |
| 101 | + |
| 102 | + expect(mockCallback).toHaveBeenCalledWith({ |
| 103 | + text: expect.stringContaining('Failed to generate price query params') |
| 104 | + }); |
| 105 | + }); |
| 106 | + |
| 107 | + it('should handle API error response', async () => { |
| 108 | + // Mock generateObject to return price query params |
| 109 | + vi.mocked(generateObject).mockResolvedValueOnce({ |
| 110 | + object: mockPriceQuery |
| 111 | + }); |
| 112 | + |
| 113 | + // Mock API error response |
| 114 | + mockFetch.mockResolvedValueOnce({ |
| 115 | + json: () => Promise.resolve({ |
| 116 | + code: 1, |
| 117 | + message: 'API Error' |
| 118 | + }) |
| 119 | + }); |
| 120 | + |
| 121 | + await attpsPriceQuery.handler( |
| 122 | + mockRuntime, |
| 123 | + mockMessage, |
| 124 | + mockState, |
| 125 | + {}, |
| 126 | + mockCallback |
| 127 | + ); |
| 128 | + |
| 129 | + expect(mockCallback).toHaveBeenCalledWith({ |
| 130 | + text: 'Error fetching price data, error: API Error' |
| 131 | + }); |
| 132 | + }); |
| 133 | + |
| 134 | + it('should handle network failure', async () => { |
| 135 | + // Mock generateObject to return price query params |
| 136 | + vi.mocked(generateObject).mockResolvedValueOnce({ |
| 137 | + object: mockPriceQuery |
| 138 | + }); |
| 139 | + |
| 140 | + // Mock network failure |
| 141 | + mockFetch.mockRejectedValueOnce(new Error('Network error')); |
| 142 | + |
| 143 | + await attpsPriceQuery.handler( |
| 144 | + mockRuntime, |
| 145 | + mockMessage, |
| 146 | + mockState, |
| 147 | + {}, |
| 148 | + mockCallback |
| 149 | + ); |
| 150 | + |
| 151 | + expect(mockCallback).toHaveBeenCalledWith({ |
| 152 | + text: 'Error fetching price data, error: Network error' |
| 153 | + }); |
| 154 | + }); |
| 155 | + }); |
| 156 | + |
| 157 | + describe('metadata', () => { |
| 158 | + it('should have correct name and description', () => { |
| 159 | + expect(attpsPriceQuery.name).toBe('ATTPS_PRICE_QUERY'); |
| 160 | + expect(attpsPriceQuery.description).toContain('Call remote API to fetch price data'); |
| 161 | + }); |
| 162 | + |
| 163 | + it('should have valid examples', () => { |
| 164 | + expect(Array.isArray(attpsPriceQuery.examples)).toBe(true); |
| 165 | + expect(attpsPriceQuery.examples.length).toBeGreaterThan(0); |
| 166 | + |
| 167 | + attpsPriceQuery.examples.forEach(example => { |
| 168 | + expect(Array.isArray(example)).toBe(true); |
| 169 | + expect(example.length).toBe(2); |
| 170 | + expect(example[1].content.action).toBe('ATTPS_PRICE_QUERY'); |
| 171 | + }); |
| 172 | + }); |
| 173 | + }); |
| 174 | +}); |
0 commit comments