mirror of
https://github.com/wharfkit/account.git
synced 2026-07-21 17:43:40 +00:00
enhacement: getting API tests passing
This commit is contained in:
@@ -51,7 +51,7 @@ build/docs: $(SRC_FILES) node_modules
|
||||
@${BIN}/typedoc --out build/docs \
|
||||
--excludeInternal --excludePrivate --excludeProtected \
|
||||
--includeVersion --hideGenerator --readme none \
|
||||
src/accounts.test.ts
|
||||
src/index.ts
|
||||
|
||||
build/pages: build/docs test/browser.html
|
||||
@mkdir -p build/pages
|
||||
|
||||
+1
-1
@@ -31,7 +31,7 @@
|
||||
"@rollup/plugin-virtual": "^3.0.1",
|
||||
"@types/chai": "^4.3.1",
|
||||
"@types/mocha": "^10.0.0",
|
||||
"@types/node": "^18.7.18",
|
||||
"@types/node": "^18.11.9",
|
||||
"@typescript-eslint/eslint-plugin": "^5.20.0",
|
||||
"@typescript-eslint/parser": "^5.20.0",
|
||||
"chai": "^4.3.4",
|
||||
|
||||
+24
-7
@@ -4,11 +4,10 @@ import typescript from '@rollup/plugin-typescript'
|
||||
|
||||
import pkg from './package.json' assert {type: 'json'}
|
||||
|
||||
const name = pkg.name
|
||||
const license = fs.readFileSync('LICENSE').toString('utf-8').trim()
|
||||
const banner = `
|
||||
/**
|
||||
* ${name} v${pkg.version}
|
||||
* Account SDK v${pkg.version}
|
||||
* ${pkg.homepage}
|
||||
*
|
||||
* @license
|
||||
@@ -18,10 +17,9 @@ const banner = `
|
||||
|
||||
const external = Object.keys(pkg.dependencies)
|
||||
|
||||
/** @type {import('rollup').RollupOptions} */
|
||||
export default [
|
||||
{
|
||||
input: 'src/accounts.test.ts',
|
||||
input: 'src/index.ts',
|
||||
output: {
|
||||
banner,
|
||||
file: pkg.main,
|
||||
@@ -30,9 +28,10 @@ export default [
|
||||
},
|
||||
plugins: [typescript({target: 'es6'})],
|
||||
external,
|
||||
onwarn,
|
||||
},
|
||||
{
|
||||
input: 'src/accounts.test.ts',
|
||||
input: 'src/index.ts',
|
||||
output: {
|
||||
banner,
|
||||
file: pkg.module,
|
||||
@@ -41,11 +40,29 @@ export default [
|
||||
},
|
||||
plugins: [typescript({target: 'es2020'})],
|
||||
external,
|
||||
onwarn,
|
||||
},
|
||||
{
|
||||
input: 'src/accounts.test.ts',
|
||||
input: 'src/index.ts',
|
||||
output: {banner, file: pkg.types, format: 'esm'},
|
||||
|
||||
onwarn,
|
||||
plugins: [dts()],
|
||||
},
|
||||
]
|
||||
|
||||
function onwarn(warning, rollupWarn) {
|
||||
if (warning.code === 'CIRCULAR_DEPENDENCY') {
|
||||
// unnecessary warning
|
||||
return
|
||||
}
|
||||
if (
|
||||
warning.code === 'UNUSED_EXTERNAL_IMPORT' &&
|
||||
warning.source === 'tslib' &&
|
||||
warning.names[0] === '__read'
|
||||
) {
|
||||
// when using ts with importHelpers: true rollup complains about this
|
||||
// seems safe to ignore since __read is not actually imported or used anywhere in the resulting bundles
|
||||
return
|
||||
}
|
||||
rollupWarn(warning)
|
||||
}
|
||||
|
||||
+8
-5
@@ -1,6 +1,6 @@
|
||||
import {Name, APIClient, API, NameType} from '@greymass/eosio'
|
||||
import { ChainId } from 'anchor-link'
|
||||
import type { ChainIdType } from 'anchor-link'
|
||||
import {ChainId, ChainName} from 'anchor-link'
|
||||
import type {ChainIdType} from 'anchor-link'
|
||||
|
||||
// import { Contract } from '@wharfkit/contract'
|
||||
|
||||
@@ -47,11 +47,11 @@ export class Account {
|
||||
}
|
||||
|
||||
get accountName(): Name {
|
||||
return this.accountName
|
||||
return this.account_name
|
||||
}
|
||||
|
||||
get chainId(): ChainId {
|
||||
return this.chainId
|
||||
return this.chain_id
|
||||
}
|
||||
|
||||
async getPermission(permissionName: NameType): Promise<Permission | undefined> {
|
||||
@@ -99,7 +99,7 @@ export class Account {
|
||||
getAccountData(): Promise<API.v1.AccountObject> {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (this.account_data && this.account_data_timestamp && this.account_data_timestamp + this.cache_duration > Date.now()) {
|
||||
resolve(this.account_data)
|
||||
return resolve(this.account_data)
|
||||
}
|
||||
|
||||
this.api_client.v1.chain.get_account(this.accountName.toString())
|
||||
@@ -109,6 +109,9 @@ export class Account {
|
||||
resolve(accountData)
|
||||
})
|
||||
.catch(error => {
|
||||
if (error.message.includes('Account not found')) {
|
||||
return reject(new Error(`Account ${this.account_name} does not exist on chain ${ChainName[this.chain_id.chainName]}.`))
|
||||
}
|
||||
reject(error)
|
||||
});
|
||||
});
|
||||
|
||||
+17
-12
@@ -1,12 +1,10 @@
|
||||
import {APIClient, Name, NameType} from '@greymass/eosio';
|
||||
import type { API } from '@greymass/eosio';
|
||||
|
||||
import { Contract } from '@wharfkit/contract'
|
||||
// import { Contract } from '@wharfkit/contract'
|
||||
|
||||
import { Account } from './accounts'
|
||||
|
||||
import type { PermissionOptions } from '../types'
|
||||
|
||||
interface PermissionData {
|
||||
parent: string
|
||||
required_auth: {
|
||||
@@ -19,6 +17,10 @@ interface PermissionData {
|
||||
}
|
||||
}
|
||||
|
||||
interface PermissionOptions {
|
||||
api_client?: APIClient
|
||||
}
|
||||
|
||||
interface PermissionType {
|
||||
parent: string
|
||||
permission: NameType
|
||||
@@ -45,12 +47,12 @@ interface PermissionType {
|
||||
export class Permission {
|
||||
permission_name: Name
|
||||
permission_data: API.v1.AccountPermission
|
||||
client: APIClient
|
||||
api_client: APIClient | undefined
|
||||
|
||||
constructor(permissionName: Name, permissionData: API.v1.AccountPermission, options?: PermissionOptions) {
|
||||
this.permission_name = permissionName
|
||||
this.permission_data = permissionData
|
||||
this.client = options?.client
|
||||
this.api_client = options?.api_client
|
||||
}
|
||||
|
||||
static from(permissionName: NameType, accountData: API.v1.AccountObject): Permission {
|
||||
@@ -63,13 +65,16 @@ export class Permission {
|
||||
return new Permission(Name.from(permissionName), permissionObject)
|
||||
}
|
||||
|
||||
static addPermission(permission: PermissionType, account: Account): Promise<void> {
|
||||
return Contract.eosio.updateauth({
|
||||
account: account.name.toString(),
|
||||
"permission": permission.name,
|
||||
"parent": permission.parent,
|
||||
"auth": permission.auth
|
||||
})
|
||||
static addPermission(permissionName: PermissionType, account: Account): Promise<void> {
|
||||
return new Promise((resolve) => {
|
||||
resolve()
|
||||
});
|
||||
// return Contract.eosio.updateauth({
|
||||
// account: account.name.toString(),
|
||||
// permission: permissionNam e,
|
||||
// parent: permission.parent,
|
||||
// auth: permission.auth
|
||||
// })
|
||||
}
|
||||
|
||||
get permissionName(): Name {
|
||||
|
||||
@@ -29,56 +29,51 @@ suite('accounts', function () {
|
||||
|
||||
suite('getAccountData', function () {
|
||||
this.slow(200)
|
||||
this.timeout(10 * 10000)
|
||||
this.timeout(5 * 1000)
|
||||
|
||||
test('returns account data', async function () {
|
||||
const account = testAccount()
|
||||
const account = testAccount()
|
||||
|
||||
assert.instanceOf(await account.getAccountData(), API.v1.AccountObject)
|
||||
})
|
||||
|
||||
test('throws error when account does not exist', async function (done) {
|
||||
const account = testAccount()
|
||||
test('throws error when account does not exist', function (done) {
|
||||
const account = nonExistentTestAccount()
|
||||
|
||||
try {
|
||||
await account.getAccountData()
|
||||
} catch (error) {
|
||||
account.getAccountData().catch((error) => {
|
||||
assert((error as Error).message, "Account does not exist")
|
||||
done()
|
||||
}
|
||||
});
|
||||
})
|
||||
})
|
||||
|
||||
suite('getPermission', function () {
|
||||
this.slow(200)
|
||||
this.timeout(10 * 10000)
|
||||
this.timeout(5 * 1000)
|
||||
|
||||
test('returns account data', async function () {
|
||||
test('returns permission object', async function () {
|
||||
const account = testAccount()
|
||||
|
||||
assert.instanceOf(await account.getPermission('active'), Permission)
|
||||
})
|
||||
|
||||
test('throws error when account does not exist', async function (done) {
|
||||
const account = testAccount()
|
||||
test('throws error when account does not exist', function (done) {
|
||||
const account = nonExistentTestAccount()
|
||||
|
||||
try {
|
||||
await account.getPermission('active')
|
||||
} catch (error) {
|
||||
assert((error as Error).message, "Account does not exist")
|
||||
account.getPermission('active').catch((error) => {
|
||||
console.log({ error: error.message })
|
||||
assert.equal((error as Error).message, "Account nonexistent does not exist on chain EOS.")
|
||||
done()
|
||||
}
|
||||
});
|
||||
})
|
||||
|
||||
test('throws error when permission does not exist', async function (done) {
|
||||
test('throws error when permission does not exist', function (done) {
|
||||
const account = testAccount()
|
||||
|
||||
try {
|
||||
await account.getPermission('nonexistent')
|
||||
} catch (error) {
|
||||
assert((error as Error).message, "Permission does not exist")
|
||||
account.getPermission('nonexistent').catch((error) => {
|
||||
assert.equal((error as Error).message, "Unknown permission nonexistent on account teamgreymass.")
|
||||
done()
|
||||
}
|
||||
});
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -87,3 +82,7 @@ suite('accounts', function () {
|
||||
function testAccount() {
|
||||
return Account.from('teamgreymass', ChainId.from(ChainName.EOS), { api_client: eosApiClient })
|
||||
}
|
||||
|
||||
function nonExistentTestAccount() {
|
||||
return Account.from('nonexistent', ChainId.from(ChainName.EOS), { api_client: eosApiClient })
|
||||
}
|
||||
+6
-5
@@ -2,13 +2,14 @@
|
||||
"extends": "../tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"isolatedModules": false,
|
||||
"resolveJsonModule": true,
|
||||
"module": "commonjs",
|
||||
"target": "es6",
|
||||
"types": ["mocha"],
|
||||
"baseUrl": ".",
|
||||
"types": ["mocha", "node"],
|
||||
"baseUrl": "..",
|
||||
"paths": {
|
||||
"$lib": ["../src"]
|
||||
},
|
||||
"$lib": ["src"],
|
||||
}
|
||||
},
|
||||
"include": ["./**/*.ts"]
|
||||
"include": ["*.ts", "**/*.ts"]
|
||||
}
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
import fetch from 'node-fetch'
|
||||
|
||||
import {APIProvider, Bytes, Checksum160, FetchProvider} from '@greymass/eosio'
|
||||
|
||||
import {join as joinPath} from 'path'
|
||||
import {promisify} from 'util'
|
||||
import {readFile as _readFile, writeFile as _writeFile} from 'fs'
|
||||
@@ -6,8 +9,6 @@ import {readFile as _readFile, writeFile as _writeFile} from 'fs'
|
||||
const readFile = promisify(_readFile)
|
||||
const writeFile = promisify(_writeFile)
|
||||
|
||||
import {APIProvider, Bytes, Checksum160, FetchProvider} from '$lib'
|
||||
|
||||
export class MockProvider implements APIProvider {
|
||||
recordProvider = new FetchProvider(this.api, {fetch})
|
||||
|
||||
|
||||
+3
-2
@@ -5,12 +5,13 @@
|
||||
"experimentalDecorators": true,
|
||||
"importHelpers": true,
|
||||
"isolatedModules": true,
|
||||
"lib": ["es2020"],
|
||||
"module": "es2020",
|
||||
"lib": ["dom", "esnext"],
|
||||
"module": "esnext",
|
||||
"moduleResolution": "node",
|
||||
"noImplicitAny": false,
|
||||
"sourceMap": true,
|
||||
"strict": true,
|
||||
"useUnknownInCatchVariables": false,
|
||||
"target": "es2020"
|
||||
},
|
||||
"include": ["src/**/*"]
|
||||
|
||||
@@ -441,11 +441,16 @@
|
||||
resolved "https://registry.yarnpkg.com/@types/mocha/-/mocha-10.0.0.tgz#3d9018c575f0e3f7386c1de80ee66cc21fbb7a52"
|
||||
integrity sha512-rADY+HtTOA52l9VZWtgQfn4p+UDVM2eDVkMZT1I6syp0YKxW2F9v+0pbRZLsvskhQv/vMb6ZfCay81GHbz5SHg==
|
||||
|
||||
"@types/node@*", "@types/node@^18.7.18":
|
||||
"@types/node@*":
|
||||
version "18.11.5"
|
||||
resolved "https://registry.yarnpkg.com/@types/node/-/node-18.11.5.tgz#1bc94cf2f9ab5fe33353bc7c79c797dcc5325bef"
|
||||
integrity sha512-3JRwhbjI+cHLAkUorhf8RnqUbFXajvzX4q6fMn5JwkgtuwfYtRQYI3u4V92vI6NJuTsbBQWWh3RZjFsuevyMGQ==
|
||||
|
||||
"@types/node@^18.11.9":
|
||||
version "18.11.9"
|
||||
resolved "https://registry.yarnpkg.com/@types/node/-/node-18.11.9.tgz#02d013de7058cea16d36168ef2fc653464cfbad4"
|
||||
integrity sha512-CRpX21/kGdzjOpFsZSkcrXMGIBWMGNIHXXBVFSH+ggkftxg+XYP20TESbh+zFvFj3EQOl5byk0HTRn1IL6hbqg==
|
||||
|
||||
"@types/resolve@1.20.2":
|
||||
version "1.20.2"
|
||||
resolved "https://registry.yarnpkg.com/@types/resolve/-/resolve-1.20.2.tgz#97d26e00cd4a0423b4af620abecf3e6f442b7975"
|
||||
|
||||
Reference in New Issue
Block a user