Skip to content

Commit 6466890

Browse files
committedJan 26, 2025
plugin-0g: get indicative price test
1 parent 28d8aea commit 6466890

File tree

1 file changed

+196
-0
lines changed

1 file changed

+196
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
import { describe, it, expect, vi, beforeEach } from 'vitest';
2+
import { getIndicativePrice } from '../../src/actions/getIndicativePrice';
3+
import type { Memory, State, IAgentRuntime, HandlerCallback } from '@elizaos/core';
4+
import { generateObject } from '@elizaos/core';
5+
import { createClientV2 } from '@0x/swap-ts-sdk';
6+
import { EVMTokenRegistry } from '../../src/EVMtokenRegistry';
7+
import { Chains } from '../../src/types';
8+
9+
// Mock dependencies
10+
vi.mock('@elizaos/core', () => ({
11+
elizaLogger: {
12+
debug: vi.fn(),
13+
info: vi.fn(),
14+
error: vi.fn(),
15+
warn: vi.fn(),
16+
},
17+
composeContext: vi.fn(),
18+
generateObject: vi.fn().mockResolvedValue({
19+
object: {
20+
sellTokenSymbol: 'ETH',
21+
sellAmount: 1,
22+
buyTokenSymbol: 'USDC',
23+
chain: 'ethereum',
24+
},
25+
}),
26+
ModelClass: {
27+
SMALL: 'SMALL',
28+
},
29+
MemoryManager: {
30+
create: vi.fn(),
31+
},
32+
}));
33+
34+
vi.mock('@0x/swap-ts-sdk', () => ({
35+
createClientV2: vi.fn().mockReturnValue({
36+
getIndicativePrice: vi.fn().mockResolvedValue({
37+
buyAmount: '1000000000000000000',
38+
sellAmount: '1000000000000000000',
39+
estimatedPriceImpact: '0.01',
40+
grossPrice: '1',
41+
sellTokenToEthRate: '1',
42+
buyTokenToEthRate: '1',
43+
permit2: {
44+
permitData: {},
45+
},
46+
}),
47+
}),
48+
}));
49+
50+
vi.mock('../../src/EVMtokenRegistry', () => ({
51+
EVMTokenRegistry: {
52+
getInstance: vi.fn().mockReturnValue({
53+
isChainSupported: vi.fn().mockReturnValue(true),
54+
initializeChain: vi.fn().mockResolvedValue(undefined),
55+
getTokenBySymbol: vi.fn().mockImplementation((chain: string, symbol: string) => ({
56+
address: `0x${symbol}address`,
57+
decimals: 18,
58+
symbol,
59+
})),
60+
}),
61+
},
62+
}));
63+
64+
describe('GET_INDICATIVE_PRICE_0X Action', () => {
65+
const mockRuntime: Required<Pick<IAgentRuntime, 'getSetting' | 'composeState' | 'updateRecentMessageState'>> = {
66+
getSetting: vi.fn(),
67+
composeState: vi.fn(),
68+
updateRecentMessageState: vi.fn(),
69+
};
70+
71+
const mockMessage: Required<Pick<Memory, 'id' | 'content'>> = {
72+
id: 'test-message-id',
73+
content: {
74+
sellTokenSymbol: 'ETH',
75+
sellAmount: 1,
76+
buyTokenSymbol: 'USDC',
77+
chain: 'ethereum',
78+
},
79+
};
80+
81+
const mockState: Required<Pick<State, 'messages' | 'context'>> = {
82+
messages: [],
83+
context: {},
84+
};
85+
86+
const mockCallback: HandlerCallback = vi.fn();
87+
88+
beforeEach(() => {
89+
vi.clearAllMocks();
90+
91+
mockRuntime.getSetting.mockImplementation((key: string): string => {
92+
const settings: Record<string, string> = {
93+
ZERO_EX_API_KEY: 'test-api-key',
94+
};
95+
const value = settings[key];
96+
if (value === undefined) {
97+
throw new Error(`Unexpected setting key: ${key}`);
98+
}
99+
return value;
100+
});
101+
102+
mockRuntime.composeState.mockResolvedValue(mockState);
103+
mockRuntime.updateRecentMessageState.mockResolvedValue(mockState);
104+
});
105+
106+
describe('validate', () => {
107+
it('should validate successfully with API key', async () => {
108+
const result = await getIndicativePrice.validate(mockRuntime);
109+
expect(result).toBe(true);
110+
});
111+
112+
it('should fail validation without API key', async () => {
113+
mockRuntime.getSetting.mockReturnValue(undefined);
114+
const result = await getIndicativePrice.validate(mockRuntime);
115+
expect(result).toBe(false);
116+
});
117+
});
118+
119+
describe('handler', () => {
120+
it('should get indicative price successfully', async () => {
121+
const result = await getIndicativePrice.handler(
122+
mockRuntime,
123+
mockMessage,
124+
mockState,
125+
{},
126+
mockCallback
127+
);
128+
129+
expect(result).toBeDefined();
130+
expect(createClientV2).toHaveBeenCalledWith({ apiKey: 'test-api-key' });
131+
expect(mockCallback).toHaveBeenCalled();
132+
});
133+
134+
it('should handle invalid chain', async () => {
135+
vi.mocked(generateObject).mockResolvedValueOnce({
136+
object: {
137+
...mockMessage.content,
138+
chain: 'invalid-chain',
139+
},
140+
});
141+
142+
await getIndicativePrice.handler(
143+
mockRuntime,
144+
mockMessage,
145+
mockState,
146+
{},
147+
mockCallback
148+
);
149+
150+
expect(mockCallback).toHaveBeenCalledWith({
151+
text: expect.stringContaining('Unsupported chain'),
152+
});
153+
});
154+
155+
it('should handle token not found', async () => {
156+
vi.mocked(EVMTokenRegistry.getInstance).mockReturnValueOnce({
157+
isChainSupported: vi.fn().mockReturnValue(true),
158+
initializeChain: vi.fn().mockResolvedValue(undefined),
159+
getTokenBySymbol: vi.fn().mockReturnValue(null),
160+
});
161+
162+
await getIndicativePrice.handler(
163+
mockRuntime,
164+
mockMessage,
165+
mockState,
166+
{},
167+
mockCallback
168+
);
169+
170+
expect(mockCallback).toHaveBeenCalledWith({
171+
text: expect.stringContaining('not found'),
172+
});
173+
});
174+
175+
it('should handle 0x API error', async () => {
176+
vi.mocked(createClientV2).mockReturnValueOnce({
177+
getIndicativePrice: vi.fn().mockRejectedValue(new Error('API Error')),
178+
});
179+
180+
await getIndicativePrice.handler(
181+
mockRuntime,
182+
mockMessage,
183+
mockState,
184+
{},
185+
mockCallback
186+
);
187+
188+
expect(mockCallback).toHaveBeenCalledWith(expect.objectContaining({
189+
text: expect.stringContaining('Error getting price'),
190+
content: expect.objectContaining({
191+
error: expect.any(String),
192+
}),
193+
}));
194+
});
195+
});
196+
});

0 commit comments

Comments
 (0)