Skip to content

Commit

Permalink
Backlog tests and coverage (#12)
Browse files Browse the repository at this point in the history
* add tests to mock-externals suite

* tests: complete custom and alternative routes coverage

* tests: lint fix

* tests: empty route coverage

* refactor mock-externals suite

* tests: coverage on fungible token balances and null token cases

* refactor mock-externals suite

* tests: remove unused structure

* remove redundant describe

* lint fix

---------

Co-authored-by: simsbluebox <[email protected]>
  • Loading branch information
ignaciopenia and simsbluebox authored Jul 23, 2024
1 parent 924cca8 commit 9c5de7d
Show file tree
Hide file tree
Showing 5 changed files with 312 additions and 97 deletions.
8 changes: 8 additions & 0 deletions test/alexSDK.mock-exceptions.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { AlexSDK, Currency } from '../src';
import * as ammRouteResolver from '../src/utils/ammRouteResolver';
import { assertNever } from '../src/utils/utils';
import { configs } from '../src/config';

const sdk = new AlexSDK();
Expand Down Expand Up @@ -37,4 +38,11 @@ describe('AlexSDK - mock exceptions', () => {
)
).rejects.toThrow('Too many AMM pools in route');
}, 10000);

it('Attempt assertNever to throw unexpected object', () => {
const unexpectedObject = '' as never;
expect(() => assertNever(unexpectedObject)).toThrowError(
'Unexpected object: ' + unexpectedObject
);
});
});
209 changes: 173 additions & 36 deletions test/alexSDK.mock-externals.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,14 @@ const tokenMappings: TokenInfo[] = [

const stxAddress = 'SM2MARAVW6BEJCD13YV2RHGYHQWT7TDDNMNRB1MVT';

describe('AlexSDK - mock externals', () => {
describe('AlexSDK - mock externals - SDK_API_HOST - BACKEND_API_HOST - STACKS_API_HOST (Internal Server Error)', () => {
beforeEach(() => {
fetchMock.get(configs.SDK_API_HOST, 500);
fetchMock.get(`${configs.BACKEND_API_HOST}/v2/public/token-prices`, 500);
fetchMock.get(
`${configs.STACKS_API_HOST}/extended/v1/address/${stxAddress}/balances`,
500
);
});
afterEach(() => {
fetchMock.restore();
Expand All @@ -36,24 +41,24 @@ describe('AlexSDK - mock externals', () => {
await expect(sdk.getLatestPrices()).rejects.toThrow(
'Failed to fetch token mappings'
);
}, 10000);
});
it('Attempt to Get Fee with incorrect Alex SDK Data', async () => {
await expect(sdk.getFeeRate(tokenAlex, Currency.STX)).rejects.toThrow(
'Failed to fetch token mappings'
);
}, 10000);
});

it('Attempt to Get Router with incorrect Alex SDK Data', async () => {
await expect(sdk.getRouter(tokenAlex, Currency.STX)).rejects.toThrow(
'Failed to fetch token mappings'
);
}, 10000);
});

it('Attempt to Get Amount with incorrect Alex SDK Data', async () => {
await expect(
sdk.getAmountTo(Currency.STX, BigInt(2) * BigInt(1e8), tokenWUSDA)
).rejects.toThrow('Failed to fetch token mappings');
}, 10000);
});

it('Attempt to Run Swap with incorrect Alex SDK Data', async () => {
await expect(
Expand All @@ -65,40 +70,110 @@ describe('AlexSDK - mock externals', () => {
BigInt(0)
)
).rejects.toThrow('Failed to fetch token mappings');
}, 10000);
});

it('Attempt to Get Latest Prices with incorrect Alex SDK Data', async () => {
await expect(sdk.getLatestPrices()).rejects.toThrow(
'Failed to fetch token mappings'
);
}, 10000);
});

it('Attempt to Get Balances with incorrect Alex SDK Data', async () => {
const stxAddress = 'SM2MARAVW6BEJCD13YV2RHGYHQWT7TDDNMNRB1MVT';
await expect(sdk.getBalances(stxAddress)).rejects.toThrow(
'Failed to fetch token mappings'
);
}, 10000);
});

it('Attempt to Fetch Swappable Currency with incorrect Alex SDK Data', async () => {
await expect(sdk.fetchSwappableCurrency()).rejects.toThrow(
'Failed to fetch token mappings'
);
}, 10000);
});
});

describe('AlexSDK - mock externals - BACKEND_API_HOST', () => {
beforeEach(() => {
fetchMock.get(`${configs.BACKEND_API_HOST}/v2/public/token-prices`, {
status: 500,
body: 'Internal Server Error',
});
it('Attempt to get token prices with incorrect data', async () => {
await expect(getPrices(tokenMappings)).rejects.toThrow(
'Failed to fetch token mappings'
);
expect(
fetchMock.calls(`${configs.BACKEND_API_HOST}/v2/public/token-prices`)
.length
).toBe(1);
});

it('Attempt to Get Balances with incorrect data', async () => {
await expect(
fetchBalanceForAccount(stxAddress, tokenMappings)
).rejects.toThrow('Unexpected');
});
});
describe('AlexSDK - mock externals - SDK_API_HOST - BACKEND_API_HOST - STACKS_API_HOST (Gateway Timeout)', () => {
beforeEach(() => {
fetchMock.get(configs.SDK_API_HOST, 504);
fetchMock.get(`${configs.BACKEND_API_HOST}/v2/public/token-prices`, 504);
fetchMock.get(
`${configs.STACKS_API_HOST}/extended/v1/address/${stxAddress}/balances`,
504
);
});
afterEach(() => {
fetchMock.restore();
});

it('Attempt to Get Latest Prices with incorrect Alex SDK Data', async () => {
await expect(sdk.getLatestPrices()).rejects.toThrow(
'Failed to fetch token mappings'
);
});
it('Attempt to Get Fee with incorrect Alex SDK Data', async () => {
await expect(sdk.getFeeRate(tokenAlex, Currency.STX)).rejects.toThrow(
'Failed to fetch token mappings'
);
});

it('Attempt to Get Router with incorrect Alex SDK Data', async () => {
await expect(sdk.getRouter(tokenAlex, Currency.STX)).rejects.toThrow(
'Failed to fetch token mappings'
);
});

it('Attempt to Get Amount with incorrect Alex SDK Data', async () => {
await expect(
sdk.getAmountTo(Currency.STX, BigInt(2) * BigInt(1e8), tokenWUSDA)
).rejects.toThrow('Failed to fetch token mappings');
});

it('Attempt to Run Swap with incorrect Alex SDK Data', async () => {
await expect(
sdk.runSwap(
configs.CONTRACT_DEPLOYER,
tokenAlex,
tokenWUSDA,
BigInt(2) * BigInt(1e8),
BigInt(0)
)
).rejects.toThrow('Failed to fetch token mappings');
});

it('Attempt to Get Latest Prices with incorrect Alex SDK Data', async () => {
await expect(sdk.getLatestPrices()).rejects.toThrow(
'Failed to fetch token mappings'
);
});

it('Attempt to Get Balances with incorrect Alex SDK Data', async () => {
const stxAddress = 'SM2MARAVW6BEJCD13YV2RHGYHQWT7TDDNMNRB1MVT';
await expect(sdk.getBalances(stxAddress)).rejects.toThrow(
'Failed to fetch token mappings'
);
});

it('Attempt to Fetch Swappable Currency with incorrect Alex SDK Data', async () => {
await expect(sdk.fetchSwappableCurrency()).rejects.toThrow(
'Failed to fetch token mappings'
);
});

it('Attempt to get token prices with incorrect data', async () => {
await expect(getPrices(tokenMappings)).rejects.toThrow(
'Failed to fetch token mappings'
Expand All @@ -107,40 +182,102 @@ describe('AlexSDK - mock externals - BACKEND_API_HOST', () => {
fetchMock.calls(`${configs.BACKEND_API_HOST}/v2/public/token-prices`)
.length
).toBe(1);
}, 10000);
});
});

describe('Transfer Factory', () => {
it('Throws error in Transfer Factory', () => {
const transfer = transferFactory(tokenMappings);
expect(() => transfer(stxAddress, tokenAlex, BigInt(1000))).toThrow(
'Token mapping not found'
);
it('Attempt to Get Balances with incorrect data', async () => {
await expect(
fetchBalanceForAccount(stxAddress, tokenMappings)
).rejects.toThrow('Unexpected');
});
});

describe('AlexSDK - mock externals - STACKS_API_HOST', () => {
describe('AlexSDK - mock externals - SDK_API_HOST - BACKEND_API_HOST - STACKS_API_HOST (Not Found)', () => {
beforeEach(() => {
fetchMock.get(configs.SDK_API_HOST, 404);
fetchMock.get(`${configs.BACKEND_API_HOST}/v2/public/token-prices`, 404);
fetchMock.get(
`${configs.STACKS_API_HOST}/extended/v1/address/${stxAddress}/balances`,
{
status: 500,
body: 'Internal Server Error',
}
404
);
});

afterEach(() => {
fetchMock.restore();
});

it('Attempt to Get Latest Prices with incorrect Alex SDK Data', async () => {
await expect(sdk.getLatestPrices()).rejects.toThrow(
'Failed to fetch token mappings'
);
});
it('Attempt to Get Fee with incorrect Alex SDK Data', async () => {
await expect(sdk.getFeeRate(tokenAlex, Currency.STX)).rejects.toThrow(
'Failed to fetch token mappings'
);
});

it('Attempt to Get Router with incorrect Alex SDK Data', async () => {
await expect(sdk.getRouter(tokenAlex, Currency.STX)).rejects.toThrow(
'Failed to fetch token mappings'
);
});

it('Attempt to Get Amount with incorrect Alex SDK Data', async () => {
await expect(
sdk.getAmountTo(Currency.STX, BigInt(2) * BigInt(1e8), tokenWUSDA)
).rejects.toThrow('Failed to fetch token mappings');
});

it('Attempt to Run Swap with incorrect Alex SDK Data', async () => {
await expect(
sdk.runSwap(
configs.CONTRACT_DEPLOYER,
tokenAlex,
tokenWUSDA,
BigInt(2) * BigInt(1e8),
BigInt(0)
)
).rejects.toThrow('Failed to fetch token mappings');
});

it('Attempt to Get Latest Prices with incorrect Alex SDK Data', async () => {
await expect(sdk.getLatestPrices()).rejects.toThrow(
'Failed to fetch token mappings'
);
});

it('Attempt to Get Balances with incorrect Alex SDK Data', async () => {
const stxAddress = 'SM2MARAVW6BEJCD13YV2RHGYHQWT7TDDNMNRB1MVT';
await expect(sdk.getBalances(stxAddress)).rejects.toThrow(
'Failed to fetch token mappings'
);
});

it('Attempt to Fetch Swappable Currency with incorrect Alex SDK Data', async () => {
await expect(sdk.fetchSwappableCurrency()).rejects.toThrow(
'Failed to fetch token mappings'
);
});

it('Attempt to get token prices with incorrect data', async () => {
await expect(getPrices(tokenMappings)).rejects.toThrow(
'Failed to fetch token mappings'
);
expect(
fetchMock.calls(`${configs.BACKEND_API_HOST}/v2/public/token-prices`)
.length
).toBe(1);
});

it('Attempt to Get Balances with incorrect data', async () => {
await expect(
fetchBalanceForAccount(stxAddress, tokenMappings)
).rejects.toThrow(
new SyntaxError(
'Unexpected token \'I\', "Internal S"... is not valid JSON'
)
).rejects.toThrow('Unexpected');
});
});
describe('Transfer Factory', () => {
it('Throws error in Transfer Factory', () => {
const transfer = transferFactory(tokenMappings);
expect(() => transfer(stxAddress, tokenAlex, BigInt(1000))).toThrow(
'Token mapping not found'
);
}, 10000);
});
});
Loading

0 comments on commit 9c5de7d

Please sign in to comment.