Skip to content

Commit

Permalink
Merge pull request #92 from initia-labs/feat/tests
Browse files Browse the repository at this point in the history
Feat/tests
  • Loading branch information
joon9823 authored Nov 8, 2024
2 parents 7448d8c + d39b522 commit 7e39759
Show file tree
Hide file tree
Showing 157 changed files with 1,719 additions and 850 deletions.
84 changes: 0 additions & 84 deletions src/client/LocalInitia.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/client/WebSocketClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ export class WebSocketClient extends EventEmitter {

/**
* WebSocketClient constructor
* @param URL The WebSocket endpoint URL on the Tendermint RPC server.
* @param URL the websocket endpoint URL on the Tendermint RPC server.
* Ex: ws://localhost:26657/websocket
* @param reconnectCount 0 for not to attempt reconnect, -1 for infinite, > 0 for number of times to attempt
* @param reconnectInterval retry interval in milliseconds
Expand Down
3 changes: 0 additions & 3 deletions src/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,5 @@ export * from './rest/RESTClient'
export * from './rest/Wallet'
export * from './rest/api'

// LocalInitia (REST Client)
export * from './LocalInitia'

// WebSocketClient
export * from './WebSocketClient'
12 changes: 6 additions & 6 deletions src/client/rest/APIRequester.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ describe('APIRequester', () => {
it('accept a standard URL', async () => {
mockedAxios.get.mockResolvedValueOnce({ data: null })

const request = new APIRequester('https://rest.devnet.initia.xyz')
const request = new APIRequester('https://rest.testnet.initia.xyz')
await request.get('/foo')

expect(mockedAxios.get).toHaveBeenCalledWith(
'https://rest.devnet.initia.xyz/foo',
'https://rest.testnet.initia.xyz/foo',
{
headers: new axios.AxiosHeaders(),
params: {},
Expand All @@ -28,11 +28,11 @@ describe('APIRequester', () => {
it('accept a deep URL', async () => {
mockedAxios.get.mockResolvedValueOnce({ data: null })

const request = new APIRequester('https://rest.devnet.initia.xyz/bar')
const request = new APIRequester('https://rest.testnet.initia.xyz/bar')
await request.get('/foo')

expect(mockedAxios.get).toHaveBeenCalledWith(
'https://rest.devnet.initia.xyz/bar/foo',
'https://rest.testnet.initia.xyz/bar/foo',
{
headers: new axios.AxiosHeaders(),
params: {},
Expand All @@ -43,11 +43,11 @@ describe('APIRequester', () => {
it('accept an URL with search params', async () => {
mockedAxios.get.mockResolvedValueOnce({ data: null })

const request = new APIRequester('https://rest.devnet.initia.xyz?key=123')
const request = new APIRequester('https://rest.testnet.initia.xyz?key=123')
await request.get('/foo')

expect(mockedAxios.get).toHaveBeenCalledWith(
'https://rest.devnet.initia.xyz/foo?key=123',
'https://rest.testnet.initia.xyz/foo?key=123',
{
headers: new axios.AxiosHeaders(),
params: {},
Expand Down
2 changes: 1 addition & 1 deletion src/client/rest/Wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { SignMode } from '@initia/initia.proto/cosmos/tx/signing/v1beta1/signing
export class Wallet {
private accAddress: string
/**
* @deprecated Use rest instead.
* @deprecated Use `rest` instead.
*/
public lcd: RESTClient

Expand Down
13 changes: 13 additions & 0 deletions src/client/rest/api/AuctionAPI.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { APIRequester } from '../APIRequester'
import { AuctionAPI } from './AuctionAPI'
import { AuctionParams } from '../../../core'

const c = new APIRequester('https://rest.testnet.initia.xyz')
const api = new AuctionAPI(c)

describe('AuctionAPI', () => {
it('params', async () => {
const params = await api.parameters()
expect(params).toEqual(expect.any(AuctionParams))
})
})
5 changes: 4 additions & 1 deletion src/client/rest/api/AuctionAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ import { APIParams } from '../APIRequester'
import { BaseAPI } from './BaseAPI'

export class AuctionAPI extends BaseAPI {
/**
* Query the parameters of the auction module.
*/
public async parameters(params: APIParams = {}): Promise<AuctionParams> {
return this.c
.get<{
params: AuctionParams.Data
}>(`/block-sdk/auction/v1/params`, params)
.then(({ params: d }) => AuctionParams.fromData(d))
.then((d) => AuctionParams.fromData(d.params))
}
}
23 changes: 9 additions & 14 deletions src/client/rest/api/AuthAPI.spec.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,37 @@
import { APIRequester } from '../APIRequester'
import { AuthAPI } from './AuthAPI'
import { BaseAccount } from '../../../core'
import { AuthParams, BaseAccount } from '../../../core'
import { MnemonicKey } from '../../../key'

const c = new APIRequester('https://rest.devnet.initia.xyz')
const auth = new AuthAPI(c)
const c = new APIRequester('https://rest.testnet.initia.xyz')
const api = new AuthAPI(c)

describe('AuthAPI', () => {
describe('accounts', () => {
it('account exists', async () => {
const acct = await auth.accountInfo(
const acct = await api.accountInfo(
'init1hk0asaef9nxvnj7gjwawv0zz0yd7adcysktpqu'
)

expect(acct instanceof BaseAccount).toBe(true)
})

it('invalid account', async () => {
await expect(auth.accountInfo('1234')).rejects.toThrow()
await expect(api.accountInfo('1234')).rejects.toThrow()
})

it("account doesn't exist (valid but new account)", async () => {
const mk = new MnemonicKey()
await expect(auth.accountInfo(mk.accAddress)).rejects.toThrow(
await expect(api.accountInfo(mk.accAddress)).rejects.toThrow(
'status code 404'
)
})
})

describe('parameters', () => {
it('parameters', async () => {
const param = await auth.parameters()

expect(param.max_memo_characters).toBeGreaterThanOrEqual(0)
expect(param.tx_sig_limit).toBeGreaterThanOrEqual(0)
expect(param.tx_size_cost_per_byte).toBeGreaterThanOrEqual(0)
expect(param.sig_verify_cost_ed25519).toBeGreaterThanOrEqual(0)
expect(param.sig_verify_cost_secp256k1).toBeGreaterThanOrEqual(0)
it('params', async () => {
const params = await api.parameters()
expect(params).toEqual(expect.any(AuthParams))
})
})
})
10 changes: 9 additions & 1 deletion src/client/rest/api/AuthAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { APIParams } from '../APIRequester'

export class AuthAPI extends BaseAPI {
/**
* Query the account info.
* @param address address of account to look up
*/
public async accountInfo(
Expand All @@ -22,6 +23,10 @@ export class AuthAPI extends BaseAPI {
return Account.fromData(account)
}

/**
* Query the module account info.
* @param address name of module account to look up
*/
public async moduleAccount(
name: string,
params: APIParams = {}
Expand All @@ -33,9 +38,12 @@ export class AuthAPI extends BaseAPI {
return ModuleAccount.fromData(account)
}

/**
* Query the parameters of the auth module.
*/
public async parameters(params: APIParams = {}): Promise<AuthParams> {
return this.c
.get<{ params: AuthParams.Data }>(`/cosmos/auth/v1beta1/params`, params)
.then(({ params: d }) => AuthParams.fromData(d))
.then((d) => AuthParams.fromData(d.params))
}
}
15 changes: 10 additions & 5 deletions src/client/rest/api/AuthzAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ import { APIParams, Pagination } from '../APIRequester'

export class AuthzAPI extends BaseAPI {
/**
* Get the message authorization grants for a specific granter and grantee
* Query the message authorization grants for a specific granter and grantee.
* @param granter address of granter
* @param grantee address of grantee
* @param msg_type_url type url of msg
*/
public async grants(
granter: AccAddress,
grantee: AccAddress,
msgTypeUrl?: string,
msg_type_url?: string,
params: APIParams = {}
): Promise<[AuthorizationGrant[], Pagination]> {
return this.c
Expand All @@ -19,7 +22,7 @@ export class AuthzAPI extends BaseAPI {
{
granter,
grantee,
msg_type_url: msgTypeUrl,
msg_type_url,
},
params
)
Expand All @@ -28,7 +31,8 @@ export class AuthzAPI extends BaseAPI {
}

/**
* get list of `GrantAuthorization`, granted by granter.
* Query the list of `AuthorizationGrant`, granted by granter.
* @param granter address of granter
*/
public async granter(
granter: AccAddress,
Expand All @@ -46,7 +50,8 @@ export class AuthzAPI extends BaseAPI {
}

/**
* get list of `GrantAuthorization`, by grantee.
* Query the list of `AuthorizationGrant`, by grantee.
* @param grantee address of grantee
*/
public async grantee(
grantee: AccAddress,
Expand Down
18 changes: 9 additions & 9 deletions src/client/rest/api/BankAPI.spec.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
import { APIRequester } from '../APIRequester'
import { BankAPI } from './BankAPI'
import { BankParams } from '../../../core'

const c = new APIRequester('https://rest.devnet.initia.xyz')
const bank = new BankAPI(c)
const c = new APIRequester('https://rest.testnet.initia.xyz')
const api = new BankAPI(c)

describe('BankAPI', () => {
describe('balance', () => {
it('account exists', async () => {
await bank.balance('init1wlvk4e083pd3nddlfe5quy56e68atra3gu9xfs')
await api.balance('init1wlvk4e083pd3nddlfe5quy56e68atra3gu9xfs')
})

it('invalid account', async () => {
await expect(bank.balance('1234')).rejects.toThrow()
await expect(api.balance('1234')).rejects.toThrow()
})
})

it('total supply', async () => {
const totalSupply = await bank.total()
const totalSupply = await api.total()
expect(totalSupply[0].toArray().length).toBeGreaterThan(0)
})

describe('parameters', () => {
it('parameters', async () => {
const param = await bank.parameters()

expect(param.default_send_enabled).toBeDefined()
it('params', async () => {
const params = await api.parameters()
expect(params).toEqual(expect.any(BankParams))
})
})
})
Loading

0 comments on commit 7e39759

Please sign in to comment.