|
| 1 | +import { beforeAll, describe, expect, it, jest } from '@jest/globals' |
| 2 | +import { ExecutableGameFunctionResponse, ExecutableGameFunctionStatus, GameWorker } from '@virtuals-protocol/game' |
| 3 | + |
| 4 | +import { GAME_WORKER_FUNCTIONS, SUCCESS_MESSAGES } from '../src/constants' |
| 5 | +import DeskPlugin from '../src/deskPlugin' |
| 6 | +import { logger } from './log' |
| 7 | + |
| 8 | +describe('DeskPlugin', () => { |
| 9 | + const DESK_EXCHANGE_PRIVATE_KEY: string = process.env.DESK_EXCHANGE_PRIVATE_KEY || '' |
| 10 | + const DESK_EXCHANGE_NETWORK: 'mainnet' | 'testnet' = process.env.DESK_EXCHANGE_NETWORK as 'mainnet' | 'testnet' |
| 11 | + let deskPlugin: DeskPlugin |
| 12 | + let _logger: (msg: string) => void |
| 13 | + |
| 14 | + // setup |
| 15 | + beforeAll(async () => { |
| 16 | + // clear caches |
| 17 | + jest.resetModules() |
| 18 | + // init desk plugin |
| 19 | + deskPlugin = new DeskPlugin({ |
| 20 | + credentials: { |
| 21 | + network: DESK_EXCHANGE_NETWORK, |
| 22 | + privateKey: DESK_EXCHANGE_PRIVATE_KEY, |
| 23 | + }, |
| 24 | + }) |
| 25 | + // apply logger |
| 26 | + _logger = logger |
| 27 | + }) |
| 28 | + |
| 29 | + // get worker |
| 30 | + describe('getWorker', () => { |
| 31 | + it('should return a GameWorker with default functions', () => { |
| 32 | + const worker: GameWorker = deskPlugin.getWorker() |
| 33 | + expect(worker).toBeDefined() |
| 34 | + expect(worker.functions).toHaveLength(GAME_WORKER_FUNCTIONS.length) |
| 35 | + expect(worker.functions.map((f: GameWorker['functions'][0]) => f.name)).toEqual(GAME_WORKER_FUNCTIONS) |
| 36 | + }) |
| 37 | + }) |
| 38 | + |
| 39 | + // get account summary |
| 40 | + describe('getAccountSummary', () => { |
| 41 | + // success case |
| 42 | + it('should return account summary', async () => { |
| 43 | + const result: ExecutableGameFunctionResponse = await deskPlugin.getAccountSummary.executable({}, _logger) |
| 44 | + expect(result).toBeDefined() |
| 45 | + expect(result.status).toBe(ExecutableGameFunctionStatus.Done) |
| 46 | + expect(result.feedback).toContain(SUCCESS_MESSAGES.ACCOUNT_SUMMARY) |
| 47 | + expect(result.feedback).toContain(SUCCESS_MESSAGES.YOUR_POSITIONS) |
| 48 | + expect(result.feedback).toContain(SUCCESS_MESSAGES.YOUR_ORDERS) |
| 49 | + expect(result.feedback).toContain(SUCCESS_MESSAGES.YOUR_COLLATERALS) |
| 50 | + }) |
| 51 | + }) |
| 52 | + |
| 53 | + // perp trade |
| 54 | + describe('perpTrade', () => { |
| 55 | + // success case (either found or not found) |
| 56 | + it('should successfully place order', async () => { |
| 57 | + const request: Partial<{ amount: string; price: string; side: string; symbol: string }> = { |
| 58 | + // NOTE: desire amount |
| 59 | + amount: 'YOUR_DESIRE_AMOUNT', |
| 60 | + // NOTE: check up price on market info |
| 61 | + price: 'YOUR_DESIRE_PRICE', |
| 62 | + // NOTE: side, ex:`Long` or `Short` |
| 63 | + side: 'YOUR_DESIRE_SIDE', |
| 64 | + // NOTE: symbol without `USD`, ex: `BTC` |
| 65 | + symbol: 'YOUR_DESIRE_SYMBOL', |
| 66 | + } |
| 67 | + const result: ExecutableGameFunctionResponse = await deskPlugin.perpTrade.executable(request, _logger) |
| 68 | + expect(result).toBeDefined() |
| 69 | + expect(result.status).toBe(ExecutableGameFunctionStatus.Done) |
| 70 | + expect(result.feedback).toContain(SUCCESS_MESSAGES.PERP_TRADE) |
| 71 | + }) |
| 72 | + }) |
| 73 | + |
| 74 | + // cancel orders |
| 75 | + describe('cancelOrders', () => { |
| 76 | + // success case (either found or not found any open orders) |
| 77 | + it('should successfully cancel open orders', async () => { |
| 78 | + const result: ExecutableGameFunctionResponse = await deskPlugin.cancelOrders.executable({}, _logger) |
| 79 | + expect(result).toBeDefined() |
| 80 | + expect(result.status).toBe(ExecutableGameFunctionStatus.Done) |
| 81 | + expect(result.feedback).toContain(SUCCESS_MESSAGES.ORDER_CANCELLED) |
| 82 | + }) |
| 83 | + }) |
| 84 | +}) |
0 commit comments