-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* tests: enhance tests for existing functions * tests: add test cases for existing functions * tests: add test cases for three new functions * tests: Introduce mock for getFeeRate function * tests: implement workaround for Jest BigInt assertion issue * tests: wip test cases getRoute, getAmountTo, runSwap * tests: mocked functions runSwap, getLatestPrices * temp commit * tests: mocked functions getBalances, fetchSwappableCurrency * tests: add additional tests for error scenarios * tests: add more coverage to runSwap SDK method * temp commit * temp commit * sync merge * code linted + documentation update * tests: SDK getWayPoints method * tests: add lint fix command * tests: add getFeeRate with custom route * test: add external error tests * fix: minor changes * conflicts resolved --------- Co-authored-by: [email protected] <[email protected]> Co-authored-by: simsbluebox <[email protected]> Co-authored-by: david weil <[email protected]>
- Loading branch information
1 parent
4c9ee30
commit fc23587
Showing
9 changed files
with
609 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
module.exports = { | ||
testEnvironment: 'node', | ||
verbose: true, | ||
maxWorkers: 1 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { AlexSDK, Currency } from '../src'; | ||
import * as ammRouteResolver from '../src/utils/ammRouteResolver'; | ||
import { configs } from '../src/config'; | ||
|
||
const sdk = new AlexSDK(); | ||
|
||
const tokenAlex = 'age000-governance-token' as Currency; | ||
const tokenWUSDA = 'token-wusda' as Currency; | ||
|
||
const dummyRoute = ['TokenA', 'TokenB', 'TokenC', 'TokenD', 'TokenE', 'TokenF']; | ||
jest.mock('../src/utils/ammRouteResolver', () => ({ | ||
resolveAmmRoute: jest.fn(async () => dummyRoute), | ||
})); | ||
|
||
describe('AlexSDK - mock exceptions', () => { | ||
it('Attempt to Get Fee Rate with more than 4 pools in route', async () => { | ||
expect(jest.isMockFunction(ammRouteResolver.resolveAmmRoute)).toBeTruthy(); | ||
await expect(sdk.getFeeRate(tokenAlex, Currency.STX)).rejects.toThrow( | ||
'Too many AMM pools in route' | ||
); | ||
}, 10000); | ||
|
||
it('Attempt to getAmountTo with more than 4 pools in route', async () => { | ||
await expect( | ||
sdk.getAmountTo(Currency.STX, BigInt(2) * BigInt(1e8), tokenWUSDA) | ||
).rejects.toThrow('Too many AMM pools in route'); | ||
}, 10000); | ||
|
||
it('Attempt to run swap with more than 4 pools in route', async () => { | ||
await expect( | ||
sdk.runSwap( | ||
configs.CONTRACT_DEPLOYER, | ||
tokenAlex, | ||
tokenWUSDA, | ||
BigInt(2) * BigInt(1e8), | ||
BigInt(0) | ||
) | ||
).rejects.toThrow('Too many AMM pools in route'); | ||
}, 10000); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
import { AlexSDK, Currency, TokenInfo } from '../src'; | ||
import fetchMock from 'fetch-mock'; | ||
import { configs } from '../src/config'; | ||
import { fetchBalanceForAccount, getPrices } from '../src/utils/fetchData'; | ||
import { transferFactory } from '../src/utils/postConditions'; | ||
|
||
const sdk = new AlexSDK(); | ||
|
||
const tokenAlex = 'age000-governance-token' as Currency; | ||
const tokenWUSDA = 'token-wusda' as Currency; | ||
|
||
const tokenMappings: TokenInfo[] = [ | ||
{ | ||
id: 'token-x' as Currency, | ||
name: 'Token x', | ||
icon: 'icon-x', | ||
wrapToken: 'wrap-token-x', | ||
wrapTokenDecimals: 8, | ||
underlyingToken: 'underlying-token-x', | ||
underlyingTokenDecimals: 8, | ||
isRebaseToken: false, | ||
}, | ||
]; | ||
|
||
const stxAddress = 'SM2MARAVW6BEJCD13YV2RHGYHQWT7TDDNMNRB1MVT'; | ||
|
||
describe('AlexSDK - mock externals', () => { | ||
beforeEach(() => { | ||
fetchMock.get(configs.SDK_API_HOST, 500); | ||
}); | ||
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' | ||
); | ||
}, 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( | ||
sdk.runSwap( | ||
configs.CONTRACT_DEPLOYER, | ||
tokenAlex, | ||
tokenWUSDA, | ||
BigInt(2) * BigInt(1e8), | ||
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', | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
fetchMock.restore(); | ||
}); | ||
|
||
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); | ||
}, 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' | ||
); | ||
}); | ||
}); | ||
|
||
describe('AlexSDK - mock externals - STACKS_API_HOST', () => { | ||
beforeEach(() => { | ||
fetchMock.get( | ||
`${configs.STACKS_API_HOST}/extended/v1/address/${stxAddress}/balances`, | ||
{ | ||
status: 500, | ||
body: 'Internal Server Error', | ||
} | ||
); | ||
}); | ||
|
||
afterEach(() => { | ||
fetchMock.restore(); | ||
}); | ||
|
||
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' | ||
) | ||
); | ||
}, 10000); | ||
}); |
Oops, something went wrong.