|
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 2 | +import { coinbaseCommercePlugin, createCharge } from '../src/plugins/commerce'; |
| 3 | +import { IAgentRuntime, Memory, State } from '@elizaos/core'; |
| 4 | + |
| 5 | +// Mock fetch |
| 6 | +global.fetch = vi.fn(); |
| 7 | + |
| 8 | +// Mock runtime |
| 9 | +const mockRuntime = { |
| 10 | + getSetting: vi.fn().mockReturnValue('test-api-key'), |
| 11 | + getProvider: vi.fn().mockReturnValue({ apiKey: 'test-api-key' }), |
| 12 | + character: { |
| 13 | + name: 'test-character' |
| 14 | + } |
| 15 | +}; |
| 16 | + |
| 17 | +describe('Coinbase Commerce Plugin', () => { |
| 18 | + beforeEach(() => { |
| 19 | + vi.clearAllMocks(); |
| 20 | + }); |
| 21 | + |
| 22 | + describe('createCharge', () => { |
| 23 | + it('should create a charge successfully', async () => { |
| 24 | + const mockResponse = { |
| 25 | + data: { |
| 26 | + id: 'test-charge-id', |
| 27 | + name: 'Test Charge', |
| 28 | + description: 'Test Description', |
| 29 | + pricing_type: 'fixed_price', |
| 30 | + local_price: { |
| 31 | + amount: '100', |
| 32 | + currency: 'USD' |
| 33 | + } |
| 34 | + } |
| 35 | + }; |
| 36 | + |
| 37 | + (global.fetch as any).mockResolvedValueOnce({ |
| 38 | + ok: true, |
| 39 | + json: () => Promise.resolve(mockResponse) |
| 40 | + }); |
| 41 | + |
| 42 | + const params = { |
| 43 | + name: 'Test Charge', |
| 44 | + description: 'Test Description', |
| 45 | + pricing_type: 'fixed_price', |
| 46 | + local_price: { |
| 47 | + amount: '100', |
| 48 | + currency: 'USD' |
| 49 | + } |
| 50 | + }; |
| 51 | + |
| 52 | + const result = await createCharge('test-api-key', params); |
| 53 | + expect(result).toEqual(mockResponse.data); |
| 54 | + expect(global.fetch).toHaveBeenCalledWith( |
| 55 | + 'https://api.commerce.coinbase.com/charges', |
| 56 | + { |
| 57 | + method: 'POST', |
| 58 | + headers: { |
| 59 | + 'Content-Type': 'application/json', |
| 60 | + 'X-CC-Api-Key': 'test-api-key' |
| 61 | + }, |
| 62 | + body: JSON.stringify(params) |
| 63 | + } |
| 64 | + ); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should handle errors when creating charge', async () => { |
| 68 | + (global.fetch as any).mockResolvedValueOnce({ |
| 69 | + ok: false, |
| 70 | + statusText: 'Bad Request' |
| 71 | + }); |
| 72 | + |
| 73 | + const params = { |
| 74 | + name: 'Test Charge', |
| 75 | + description: 'Test Description', |
| 76 | + pricing_type: 'fixed_price', |
| 77 | + local_price: { |
| 78 | + amount: '100', |
| 79 | + currency: 'USD' |
| 80 | + } |
| 81 | + }; |
| 82 | + |
| 83 | + await expect(createCharge('test-api-key', params)) |
| 84 | + .rejects |
| 85 | + .toThrow('Failed to create charge: Bad Request'); |
| 86 | + }); |
| 87 | + }); |
| 88 | + |
| 89 | + describe('coinbaseCommercePlugin', () => { |
| 90 | + it('should have correct plugin properties', () => { |
| 91 | + expect(coinbaseCommercePlugin.name).toBe('coinbaseCommerce'); |
| 92 | + expect(coinbaseCommercePlugin.actions).toBeDefined(); |
| 93 | + expect(Array.isArray(coinbaseCommercePlugin.actions)).toBe(true); |
| 94 | + }); |
| 95 | + |
| 96 | + it('should validate plugin actions', async () => { |
| 97 | + const mockMessage: Memory = { |
| 98 | + id: '1', |
| 99 | + user: 'test-user', |
| 100 | + content: { text: 'test message' }, |
| 101 | + timestamp: new Date(), |
| 102 | + type: 'text' |
| 103 | + }; |
| 104 | + |
| 105 | + const createChargeAction = coinbaseCommercePlugin.actions.find( |
| 106 | + action => action.name === 'CREATE_CHARGE' |
| 107 | + ); |
| 108 | + |
| 109 | + expect(createChargeAction).toBeDefined(); |
| 110 | + if (createChargeAction) { |
| 111 | + const result = await createChargeAction.validate(mockRuntime as any, mockMessage); |
| 112 | + expect(result).toBe(true); |
| 113 | + } |
| 114 | + }); |
| 115 | + }); |
| 116 | +}); |
0 commit comments