From 5d3a7dcd12cb67f1f8a54039dfedc02add52f260 Mon Sep 17 00:00:00 2001 From: Jerko J <83344666+JJ-Cro@users.noreply.github.com> Date: Thu, 19 Sep 2024 18:49:48 +0200 Subject: [PATCH] feat(): tests --- .gitignore | 1 + examples/testfile-cbexch.ts | 61 ++++++++++++++++++++++ test/CBExchangeClient/private.test.ts | 40 +++++++++----- test/CBExchangeClient/public.test.ts | 2 +- test/CBInternationalClient/private.test.ts | 2 +- test/CBPrimeClient/private.test.ts | 2 +- 6 files changed, 93 insertions(+), 15 deletions(-) create mode 100644 examples/testfile-cbexch.ts diff --git a/.gitignore b/.gitignore index 737d98a..ba4645c 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,4 @@ ts-adv-trade-test-private.ts examples/ts-app-priv.ts examples/ts-commerce.ts ts-exchange-priv.ts +testfile-cbexch.ts diff --git a/examples/testfile-cbexch.ts b/examples/testfile-cbexch.ts new file mode 100644 index 0000000..6fd78f0 --- /dev/null +++ b/examples/testfile-cbexch.ts @@ -0,0 +1,61 @@ +/* eslint-disable @typescript-eslint/no-unused-vars */ + +import { CBExchangeClient } from '../src/index.js'; + +(async () => { + //console.clear(); + + const credsViewPermission = { + key: '6e0a73cfd6e02701655d38a383dc5fcf', + secret: + 'g1uvHV/QklP+wB+cWoGXdCOYmQ5BQ1/XYMCaLDs2rH7bZ7V07X3ECroC0Zb6H5aBSa0CkMc/WMzpz5RHvqpxFQ==', + passphrase: 'gdgjgehg3i', + }; + + const client = new CBExchangeClient({ + apiKey: credsViewPermission.key, + apiSecret: credsViewPermission.secret, + apiPassphrase: credsViewPermission.passphrase, + useSandbox: true, + }); + + try { + /* const res = await client.getNewLoanPreview({ + currency: 'USDC', + native_amount: '100', + }); + console.log('req result: ', res); */ + + /* const res = await client.getProfiles({ active: true }); + console.log('req result: ', res); */ + /* const accounts = await client.getAccounts(); + console.log('req result: ', accounts); */ + const order = await client.submitOrder({ + product_id: 'BTC-GBP', + price: '1000', + side: 'buy', + type: 'limit', + size: '0.001', + }); + + console.log('order', order); + + const res = await client.getOrder({ + order_id: '2e9a5d90-84f7-4267-a6e5-722b9d43e2d0', + }); + console.log('order', res); + // console.log('res', res); + // const res = await client.cancelOrder({ order_id: 'dummyorder' }); + // console.log('cancelOrder result: ', res); + // const orderReq: SubmitAdvTradeOrderRequest = { + // product_id: 123123, + // }; + // const orderRes = await client.submitOrder({ + // product_id: 12312312, // this should throw??? + // }); + } catch (e) { + console.error('req exception: ', JSON.stringify(e, null, 2)); + } + + // +})(); diff --git a/test/CBExchangeClient/private.test.ts b/test/CBExchangeClient/private.test.ts index bcfab43..b1703d5 100644 --- a/test/CBExchangeClient/private.test.ts +++ b/test/CBExchangeClient/private.test.ts @@ -2,15 +2,16 @@ import { CBExchangeClient } from '../../src/index.js'; describe('CBExchangeClient PRIVATE', () => { const account = { - key: process.env.API_KEY_NAME, - secret: process.env.API_PRIVATE_KEY, - passphrase: process.env.API_PASSPHRASE, + key: process.env.API_KEY_NAME_EXCH, + secret: process.env.API_PRIVATE_KEY_EXCH, + passphrase: process.env.API_PASSPHRASE_EXCH, }; const rest = new CBExchangeClient({ apiKey: account.key, apiSecret: account.secret, apiPassphrase: account.passphrase, + useSandbox: true, }); it('should have credentials to test with', () => { @@ -20,7 +21,7 @@ describe('CBExchangeClient PRIVATE', () => { describe('public endpoints', () => { it('should succeed making a GET request', async () => { - const res = await rest.getProductBook({ product_id: 'BTC-USDT' }); + const res = await rest.getProductBook({ product_id: 'BTC-USD' }); //console.log(res); expect(res).toMatchObject({ bids: expect.any(Array), @@ -40,19 +41,34 @@ describe('CBExchangeClient PRIVATE', () => { expect(res).toMatchObject({ taker_fee_rate: expect.any(String), maker_fee_rate: expect.any(String), - usd_volume: expect.any(String), + usd_volume: expect.any(Object), }); }); test('with params', async () => { - const res = await rest.getNewLoanPreview({ - currency: 'USDC', - native_amount: '100', + const res = await rest.submitOrder({ + product_id: 'BTC-GBP', + price: '1000', + side: 'buy', + type: 'limit', + size: '0.001', }); - //console.log('res with params', res); + console.log('res with params', res); expect(res).toMatchObject({ - overview: expect.any(Object), - loans: expect.any(Array), + id: expect.any(String), + price: expect.any(String), + size: expect.any(String), + product_id: expect.any(String), + side: expect.any(String), + type: expect.any(String), + time_in_force: expect.any(String), + post_only: expect.any(Boolean), + created_at: expect.any(String), + fill_fees: expect.any(String), + filled_size: expect.any(String), + executed_value: expect.any(String), + status: expect.any(String), + settled: expect.any(Boolean), }); }); }); @@ -74,7 +90,7 @@ describe('CBExchangeClient PRIVATE', () => { console.log('Error, CBExchange, post req:', e); const responseBody = e?.body; expect(responseBody).toMatchObject({ - message: 'Unauthorized.', + message: 'Forbidden', }); } }); diff --git a/test/CBExchangeClient/public.test.ts b/test/CBExchangeClient/public.test.ts index 093f2aa..9136b35 100644 --- a/test/CBExchangeClient/public.test.ts +++ b/test/CBExchangeClient/public.test.ts @@ -5,7 +5,7 @@ describe('CBExchangeClient PUBLIC', () => { describe('public endpoints', () => { it('should succeed making a GET request', async () => { - const res = await rest.getProductBook({ product_id: 'BTC-USDT' }); + const res = await rest.getProductBook({ product_id: 'BTC-USD' }); //console.log(res); expect(res).toMatchObject({ bids: expect.any(Array), diff --git a/test/CBInternationalClient/private.test.ts b/test/CBInternationalClient/private.test.ts index a2a6926..eae5eb3 100644 --- a/test/CBInternationalClient/private.test.ts +++ b/test/CBInternationalClient/private.test.ts @@ -64,7 +64,7 @@ describe('CBInternationalClient PRIVATE', () => { } catch (e: any) { // These are deliberatly restricted API keys. If the response is a permission error, it confirms the sign + request was OK and permissions were denied. // console.log(`err "${expect.getState().currentTestName}"`, e?.body); - console.log('Error, CBINTX, post req:', e); + //console.log('Error, CBINTX, post req:', e); const responseBody = e?.body; expect(responseBody).toMatchObject({ title: 'Unauthorized', diff --git a/test/CBPrimeClient/private.test.ts b/test/CBPrimeClient/private.test.ts index a31f78b..dd659d9 100644 --- a/test/CBPrimeClient/private.test.ts +++ b/test/CBPrimeClient/private.test.ts @@ -56,7 +56,7 @@ describe('CBPrimeClient PRIVATE', () => { } catch (e: any) { // These are deliberatly restricted API keys. If the response is a permission error, it confirms the sign + request was OK and permissions were denied. // console.log(`err "${expect.getState().currentTestName}"`, e?.body); - console.log('Error, CBPrime, post req:', e); + //console.log('Error, CBPrime, post req:', e); const responseBody = e?.body; expect(responseBody).toMatchObject({ title: 'Unauthorized',