Skip to content

Commit

Permalink
added reset balance tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksey.dybov committed Jun 7, 2024
1 parent c494bb3 commit 233f9cd
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 7 deletions.
62 changes: 62 additions & 0 deletions test/balances.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import {
Balance,
Balances,
BalanceType,
LoginPasswordAuthMethod,
QuadcodeClientSdk,
TurboOptionsDirection
} from "../src";
import {expect} from 'chai'
import {User} from "./data/types";
import {getUserByTitle} from "./utils/userUtils";
import {waitForCondition} from "./utils/waiters";

describe('Balances', () => {
let sdk: QuadcodeClientSdk;
let balances: Balances;
const user = getUserByTitle('balance_user') as User;
const wsURL = process.env.WS_URL as string;
const apiUrl = process.env.API_URL as string;

before(async () => {
sdk = await QuadcodeClientSdk.create(wsURL, 82, new LoginPasswordAuthMethod(apiUrl, user.email, user.password));
balances = await sdk.balances();
})

after(async () => {
await sdk.shutdown();
});

function getBalance(type: BalanceType) {
const balance = balances.getBalances().find(value => value.type === type);
if (!balance) throw new Error(`${type} balance not found`);
return balance;
}

async function openOption(balance: Balance, amount: number) {
const turboOptions = await sdk.turboOptions();
const turboOptionsActiveInstruments = await turboOptions.getActives()[0].instruments();
const availableForBuyAt = turboOptionsActiveInstruments.getAvailableForBuyAt(sdk.currentTime())[0];
await turboOptions.buy(availableForBuyAt, TurboOptionsDirection.Put, amount, balance)
}

it('should reset demo balance', async () => {
const balance = getBalance(BalanceType.Demo);
const balanceAmount = balance.amount;
const defaultBalanceAmount = 10000;
if (balanceAmount >= defaultBalanceAmount) {
const amount = balanceAmount - defaultBalanceAmount + 1;
await openOption(balance, amount);
expect(await waitForCondition(() => balance.amount === balanceAmount - amount, 2000)).to.be.true;
expect(balance.amount, "Balance amount should be changed").eq(balanceAmount - amount);
}
await balance.resetDemoBalance();
expect(await waitForCondition(() => balance.amount === defaultBalanceAmount, 2000)).to.be.true;
expect(balance.amount, "Resent is not working, balance wasn't changed").eq(defaultBalanceAmount);
});

it('cannot reset normal balance', async () => {
const balance = getBalance(BalanceType.Real);
await expect(balance.resetDemoBalance()).to.eventually.be.rejectedWith("Only demo balance can be reset")
});
});
2 changes: 1 addition & 1 deletion test/binary-options.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ describe('Binary-options', () => {
expect(positionsHelper.findPosition(position.id), 'Position must be present in all positions').not.to.be.undefined
await justWait(3000);
await position.sell();
await waitForCondition(() => position.status === "closed", 2000);
expect(await waitForCondition(() => position.status === "closed", 2000)).to.be.true;
expect(position.closeReason, "Invalid close reason").eq("sold");
expect(position.sellProfit, "Sell profit must be present").not.be.null;
expect(positionsHelper.findHistoryPosition(position.id), 'Position must be present in history positions').not.to.be.undefined
Expand Down
2 changes: 1 addition & 1 deletion test/blitz-options.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ describe('Blitz-options', () => {

it('should expired', async () => {
const position = await openOption();
await waitForCondition(() => position.closeReason !== undefined, 7000);
expect(await waitForCondition(() => position.closeReason !== undefined, 7000)).to.be.true;
expect(position.closeReason, 'Invalid close reason').to.be.oneOf(["win", "equal", "loose"])
expect(positionsHelper.findHistoryPosition(position.id), 'Position must be present in history positions').not.to.be.undefined
expect(positionsHelper.findPosition(position.id), 'Position must be not present in all positions').to.be.undefined
Expand Down
2 changes: 1 addition & 1 deletion test/digital-options.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ describe('Digital-options', () => {
expect(positionsHelper.findPosition(position.id), 'Position must be present in all positions').not.to.be.undefined
await justWait(3000);
await position.sell();
await waitForCondition(() => position.status === "closed", 2000);
expect(await waitForCondition(() => position.status === "closed", 2000)).to.be.true;
expect(position.status, "Invalid status").eq("closed");
expect(position.closeReason, "Close reason must be default").eq("default");
expect(positionsHelper.findHistoryPosition(position.id), 'Position must be present in history positions').not.to.be.undefined
Expand Down
2 changes: 1 addition & 1 deletion test/turbo-options.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ describe('Turbo-options', () => {
expect(positionsHelper.findPosition(position.id), 'Position must be present in all positions').not.to.be.undefined
await justWait(3000);
await position.sell();
await waitForCondition(() => position.status === "closed", 2000);
expect(await waitForCondition(() => position.status === "closed", 2000)).to.be.true;
expect(position.closeReason, "Invalid close reason").eq("sold");
expect(position.sellProfit, "Sell profit must be present").not.be.null;
expect(positionsHelper.findHistoryPosition(position.id), 'Position must be present in history positions').not.to.be.undefined
Expand Down
6 changes: 3 additions & 3 deletions test/utils/waiters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ export function justWait(timeout: number): Promise<string> {
});
}

export async function waitForCondition(condition: () => boolean, timeout: number): Promise<void> {
export async function waitForCondition(condition: () => boolean, timeout: number): Promise<boolean> {
const interval = 100;
const endTime = Date.now() + timeout;

while (Date.now() < endTime) {
if (condition()) {
return;
return true;
}
await new Promise(resolve => setTimeout(resolve, interval));
}
throw new Error("Timeout waiting for condition");
return false;
}

0 comments on commit 233f9cd

Please sign in to comment.