Skip to content
This repository has been archived by the owner on Jun 15, 2024. It is now read-only.

Commit

Permalink
test(network): add unit tests for Network util functions
Browse files Browse the repository at this point in the history
  • Loading branch information
drichar committed Dec 7, 2023
1 parent 5c68808 commit a6feb2e
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
84 changes: 84 additions & 0 deletions __tests__/network/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { describe, expect, it } from 'bun:test'
import { NetworkId } from 'src/network/constants'
import { isAlgodConfig, isNetworkConfigMap, isValidNetworkId } from 'src/network/utils'

describe('Type Guards', () => {
describe('isValidNetworkId', () => {
it('returns true for a valid NetworkId', () => {
expect(isValidNetworkId(NetworkId.TESTNET)).toBe(true)
})

it('returns false for an invalid NetworkId', () => {
expect(isValidNetworkId('foo')).toBe(false)
})
})

describe('isAlgodConfig', () => {
it('returns true for a valid AlgodConfig', () => {
expect(
isAlgodConfig({
token: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
baseServer: 'http://localhost',
port: 1234,
headers: {
'X-Foo': 'bar'
}
})
).toBe(true)

expect(
isAlgodConfig({
token: '',
baseServer: ''
})
).toBe(true)
})

it('returns false for an invalid AlgodConfig', () => {
expect(
isAlgodConfig({
baseServer: ''
})
).toBe(false)

expect(
isAlgodConfig({
token: ''
})
).toBe(false)

expect(
isAlgodConfig({
token: '',
baseServer: '',
foo: ''
})
).toBe(false)
})
})

describe('isNetworkConfigMap', () => {
it('returns true for a valid NetworkConfigMap', () => {
const validConfigMap = {
[NetworkId.MAINNET]: {
token: '',
baseServer: ''
},
[NetworkId.TESTNET]: {
token: '',
baseServer: ''
}
}
expect(isNetworkConfigMap(validConfigMap)).toBe(true)
})

it('returns false for an invalid NetworkConfigMap', () => {
expect(
isNetworkConfigMap({
token: '',
baseServer: ''
})
).toBe(false)
})
})
})
15 changes: 15 additions & 0 deletions src/network/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,21 @@ export function isValidNetworkId(networkId: any): networkId is NetworkId {
return Object.values(NetworkId).includes(networkId)
}

export function isAlgodConfig(config: any): config is AlgodConfig {
if (typeof config !== 'object') return false

for (const key of Object.keys(config)) {
if (!['token', 'baseServer', 'port', 'headers'].includes(key)) return false
}

return (
typeof config.token === 'string' &&
typeof config.baseServer === 'string' &&
['string', 'number', 'undefined'].includes(typeof config.port) &&
['object', 'undefined'].includes(typeof config.headers)
)
}

export function isNetworkConfigMap(config: NetworkConfig): config is NetworkConfigMap {
const networkKeys = Object.values(NetworkId) as string[]
return Object.keys(config).some((key) => networkKeys.includes(key))
Expand Down

0 comments on commit a6feb2e

Please sign in to comment.