Files
bitget-api/test/private.write.test.ts
JJ-Cro 1e7987a400 feat(): add comprehensive tests for Bitget Private and Public REST API endpoints
- Introduced new test files for private read and write endpoints, covering account, trade, funding, and futures functionalities.
- Added public API tests for server time, ticker, order book, and market trades - Enhanced error handling in tests with specific error codes and messages.
- Updated response utility functions to improve error response structure.
- Removed outdated broker tests to streamline the test suite.
2025-03-17 13:38:04 +01:00

196 lines
5.6 KiB
TypeScript

import { RestClientV2 } from '../src/rest-client-v2';
import {
errorResponseObjectV3,
sucessEmptyResponseObject,
} from './response.util';
describe('Bitget Private REST API Write Endpoints', () => {
const API_KEY = process.env.API_KEY_COM;
const API_SECRET = process.env.API_SECRET_COM;
const API_PASSPHRASE = process.env.API_PASS_COM;
const api = new RestClientV2({
apiKey: API_KEY!,
apiSecret: API_SECRET!,
apiPass: API_PASSPHRASE!,
});
it('should have api credentials to use', () => {
expect(API_KEY).toStrictEqual(expect.any(String));
expect(API_SECRET).toStrictEqual(expect.any(String));
expect(API_PASSPHRASE).toStrictEqual(expect.any(String));
});
describe('Trade Endpoints', () => {
it('spotSubmitOrder()', async () => {
try {
const res = await api.spotSubmitOrder({
symbol: 'BTCUSDT',
side: 'buy',
orderType: 'limit',
price: '20000',
size: '0.001',
force: 'gtc',
});
expect(res).toMatchObject(sucessEmptyResponseObject());
} catch (e) {
expect(e).toMatchObject(errorResponseObjectV3('43012')); // not enough balance
}
});
it('spotCancelOrder()', async () => {
try {
const res = await api.spotCancelOrder({
symbol: 'BTCUSDT',
orderId: '123456789',
});
expect(res).toMatchObject(sucessEmptyResponseObject());
} catch (e) {
expect(e).toMatchObject(errorResponseObjectV3('43001')); //The order does not exist
}
});
it('spotBatchSubmitOrders()', async () => {
try {
const res = await api.spotBatchSubmitOrders({
symbol: 'BTCUSDT',
orderList: [
{
symbol: 'BTCUSDT',
side: 'buy',
orderType: 'limit',
price: '20000',
size: '0.001',
force: 'gtc',
},
],
});
expect(res).toMatchObject(sucessEmptyResponseObject());
} catch (e) {
expect(e).toMatchObject(errorResponseObjectV3('43001')); //The order does not exist
}
});
it('spotBatchCancelOrders()', async () => {
try {
const res = await api.spotBatchCancelOrders({
symbol: 'BTCUSDT',
orderList: [
{
symbol: 'BTCUSDT',
orderId: '123456789',
},
],
});
expect(res).toMatchObject(sucessEmptyResponseObject());
} catch (e) {
expect(e).toMatchObject(errorResponseObjectV3('43001')); //The order does not exist
}
});
});
describe('Funding Endpoints', () => {
it('spotTransfer()', async () => {
try {
const res = await api.spotTransfer({
fromType: 'spot',
toType: 'coin_futures',
amount: '0.001',
coin: 'BTC',
symbol: 'BTCUSDT',
});
expect(res).toMatchObject(sucessEmptyResponseObject());
} catch (e) {
expect(e).toMatchObject(errorResponseObjectV3('43117')); // Exceeds the maximum amount that can be transferred
}
});
it('spotWithdraw()', async () => {
try {
const res = await api.spotWithdraw({
coin: 'BTC',
address: 'test_address',
chain: 'BTC',
transferType: 'on_chain',
size: '0.001',
});
expect(res).toMatchObject(sucessEmptyResponseObject());
} catch (e) {
expect(e).toMatchObject(errorResponseObjectV3('40014')); // Incorrect permissions
}
});
});
describe('Futures Endpoints', () => {
it('futuresSubmitOrder()', async () => {
try {
const res = await api.futuresSubmitOrder({
symbol: 'BTCUSDT',
productType: 'USDT-FUTURES',
side: 'buy',
orderType: 'limit',
price: '20000',
size: '0.001',
marginMode: 'isolated',
marginCoin: 'USDT',
});
expect(res).toMatchObject(sucessEmptyResponseObject());
} catch (e) {
expect(e).toMatchObject(errorResponseObjectV3('40774')); // The order type for unilateral position must also be the unilateral position type.
}
});
it('futuresCancelOrder()', async () => {
try {
const res = await api.futuresCancelOrder({
symbol: 'BTCUSDT',
productType: 'USDT-FUTURES',
orderId: '123456789',
});
expect(res).toMatchObject(sucessEmptyResponseObject());
} catch (e) {
expect(e).toMatchObject(errorResponseObjectV3('40768')); // The order does not exist
}
});
it('futuresBatchSubmitOrders()', async () => {
try {
const res = await api.futuresBatchSubmitOrders({
symbol: 'BTCUSDT',
productType: 'USDT-FUTURES',
marginCoin: 'USDT',
marginMode: 'crossed',
orderList: [
{
side: 'buy',
orderType: 'limit',
price: '20000',
size: '0.001',
},
],
});
expect(res).toMatchObject(sucessEmptyResponseObject());
} catch (e) {
expect(e).toMatchObject(errorResponseObjectV3('43001'));
}
});
it('futuresBatchCancelOrders()', async () => {
try {
const res = await api.futuresBatchCancelOrders({
symbol: 'BTCUSDT',
productType: 'USDT-FUTURES',
orderIdList: [
{
orderId: '123456789',
},
],
});
expect(res).toMatchObject(sucessEmptyResponseObject());
} catch (e) {
expect(e).toMatchObject(errorResponseObjectV3('43001'));
}
});
});
});