Skip to content

Commit

Permalink
fix: getting tests passing
Browse files Browse the repository at this point in the history
  • Loading branch information
dafuga committed Jan 20, 2025
1 parent 45d5c87 commit 3d894e8
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 57 deletions.
Binary file modified bun.lockb
Binary file not shown.
11 changes: 9 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,26 @@
"build": "vite build",
"preview": "vite preview",
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch"
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
"test": "make test"
},
"devDependencies": {
"@sveltejs/adapter-auto": "^4.0.0",
"@sveltejs/kit": "^2.0.0",
"@sveltejs/vite-plugin-svelte": "^4.0.0",
"@types/chai": "^4.3.11",
"@types/mocha": "^10.0.6",
"chai": "^4.3.10",
"mocha": "^10.2.0",
"svelte": "^5.0.0",
"svelte-check": "^4.0.0",
"ts-node": "^10.9.2",
"typescript": "^5.0.0",
"vite": "^5.4.11"
},
"dependencies": {
"@wharfkit/antelope": "^1.0.13",
"@wharfkit/common": "^1.4.2"
"@wharfkit/common": "^1.4.2",
"@wharfkit/mock-data": "^1.0.0"
}
}
2 changes: 1 addition & 1 deletion src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
// place files you want to import through the `$lib` alias in this folder.
// Delete this entire file
44 changes: 44 additions & 0 deletions src/lib/lookup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import {APIClient, API, PublicKey} from '@wharfkit/antelope'
import type {PermissionLevelType} from '@wharfkit/antelope'
import type {Chain} from './chains'

export const networkRequest = (
publicKey: PublicKey,
chain: Chain,
apiClient?: APIClient
): Promise<PermissionLevelType[]> => {
return new Promise((resolve, reject) => {
const client = apiClient || new APIClient(chain)
const timeoutId = setTimeout(() => {
reject(new Error('Request timed out.'))
}, 1000)

client.v1.chain
.get_accounts_by_authorizers({
keys: [publicKey],
})
.then((response: API.v1.AccountsByAuthorizers) => {
clearTimeout(timeoutId)
resolve(
response.accounts.map((account) => ({
actor: account.account_name,
permission: account.permission_name,
}))
)
})
.catch((error) => {
clearTimeout(timeoutId)
reject(error)
})
})
}

export const lookupNetwork = async (publicKey: PublicKey, chain: Chain, apiClient?: APIClient) => {
try {
const accounts = await networkRequest(publicKey, chain, apiClient)
return {chain, accounts}
} catch (error) {
console.warn(`Lookup error on ${chain.name}: ${error}`)
return {chain, accounts: []}
}
}
41 changes: 2 additions & 39 deletions src/routes/lookup/[publicKey]/+server.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,9 @@
import {API, APIClient, type PermissionLevelType, PublicKey} from '@wharfkit/antelope'
import {PublicKey} from '@wharfkit/antelope'
import {MAINNET_CHAINS, TESTNET_CHAINS} from '$lib/chains'
import type {Chain} from '$lib/types'
import type {RequestEvent} from '@sveltejs/kit'
import {error, json} from '@sveltejs/kit'

const networkRequest = (
publicKey: PublicKey,
chain: Chain,
apiClient?: APIClient
): Promise<PermissionLevelType[]> => {
return new Promise((resolve, reject) => {
const client = apiClient || new APIClient(chain)
client.v1.chain
.get_accounts_by_authorizers({
keys: [publicKey],
})
.then((response: API.v1.AccountsByAuthorizers) => {
resolve(
response.accounts.map((account) => ({
actor: account.account_name,
permission: account.permission_name,
}))
)
})
.catch((error) => {
reject(error)
})
setTimeout(() => {
reject('Request timed out.')
}, 1000)
})
}

const lookupNetwork = async (publicKey: PublicKey, chain: Chain, apiClient?: APIClient) => {
try {
const accounts = await networkRequest(publicKey, chain, apiClient)
return {chain, accounts}
} catch (error) {
console.warn(`Lookup error on ${chain.name}: ${error}`)
return {chain, accounts: []}
}
}
import {lookupNetwork} from '$lib/lookup'

export const GET = async ({params, url}: RequestEvent) => {
const {publicKey} = params
Expand Down
16 changes: 1 addition & 15 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {assert} from 'chai'
import {PublicKey} from '@wharfkit/antelope'
import {Chains} from '@wharfkit/common'
import {accountLookup, lookupNetwork} from '../src/index'
import {lookupNetwork} from '../src/lib/lookup'
import {makeClient} from '@wharfkit/mock-data'

const PUBLIC_KEY = 'EOS65BFgzcH8uZW837HqadGcREfNViBV6Fqc1LsmwcWdfUCayHQzf'
Expand All @@ -10,20 +10,6 @@ const BAD_PUBLIC_KEY = 'EOS6KybFkAb54UMSvHoj4BMGTKPd21GEw3HUCoB5uc1vabasasqYrV'

const mockClient = makeClient('https://jungle4.greymass.com')

describe('accountLookup', () => {
it('should handle case where bad public key is provided', async () => {
const req = new Request(`https://eosio.greymass.com/lookup/${BAD_PUBLIC_KEY}`)
const result = await accountLookup(req)
assert.equal(result.status, 400)
})

it('should handle case where no public key is provided', async () => {
const req = new Request(`https://eosio.greymass.com/lookup/`)
const result = await accountLookup(req)
assert.equal(result.status, 400)
})
})

describe('lookupNetwork', () => {
it('should handle network lookup', async () => {
const publicKey = PublicKey.from(PUBLIC_KEY)
Expand Down
30 changes: 30 additions & 0 deletions tests/mock-data/0d57e0af5e51f16e321dda5a90ff35b8673e2c1d.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"request": {
"path": "https://jungle4.greymass.com/v1/chain/get_accounts_by_authorizers",
"params": {
"method": "POST",
"body": "{\"keys\":[\"PUB_K1_65BFgzcH8uZW837HqadGcREfNViBV6Fqc1LsmwcWdfUCZhX7vh\"]}",
"headers": {}
}
},
"status": 200,
"json": {
"accounts": [
{
"account_name": "testerman123",
"permission_name": "owner",
"authorizing_key": "EOS65BFgzcH8uZW837HqadGcREfNViBV6Fqc1LsmwcWdfUCayHQzf",
"weight": 1,
"threshold": 1
},
{
"account_name": "testerman123",
"permission_name": "active",
"authorizing_key": "EOS65BFgzcH8uZW837HqadGcREfNViBV6Fqc1LsmwcWdfUCayHQzf",
"weight": 1,
"threshold": 1
}
]
},
"text": "{\"accounts\":[{\"account_name\":\"testerman123\",\"permission_name\":\"owner\",\"authorizing_key\":\"EOS65BFgzcH8uZW837HqadGcREfNViBV6Fqc1LsmwcWdfUCayHQzf\",\"weight\":1,\"threshold\":1},{\"account_name\":\"testerman123\",\"permission_name\":\"active\",\"authorizing_key\":\"EOS65BFgzcH8uZW837HqadGcREfNViBV6Fqc1LsmwcWdfUCayHQzf\",\"weight\":1,\"threshold\":1}]}"
}
15 changes: 15 additions & 0 deletions tests/mock-data/be06b2c036301d5738c7e72d682e07f0454ae35a.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"request": {
"path": "https://jungle4.greymass.com/v1/chain/get_accounts_by_authorizers",
"params": {
"method": "POST",
"body": "{\"keys\":[\"PUB_K1_8hiZaTNknE75KH2UmBNqcVFN4u3vgJ8PcytevjygaJ6aBNaT59\"]}",
"headers": {}
}
},
"status": 200,
"json": {
"accounts": []
},
"text": "{\"accounts\":[]}"
}

0 comments on commit 3d894e8

Please sign in to comment.