From 4cd08b60ff20076afbe016f3b315947f86616516 Mon Sep 17 00:00:00 2001 From: Adam Avramov Date: Thu, 16 Nov 2023 09:39:15 +0200 Subject: [PATCH] test+refactor(agent): 90% coverage --- agent/connec.test.ts | 14 ++++++-------- agent/connec.ts | 4 ++-- agent/deploy.ts | 12 +++++------- agent/stub.ts | 23 ++++++++++------------- agent/tester.ts | 1 + agent/token.test.ts | 2 +- 6 files changed, 25 insertions(+), 31 deletions(-) diff --git a/agent/connec.test.ts b/agent/connec.test.ts index bce472f350e..1e29b72fcda 100644 --- a/agent/connec.test.ts +++ b/agent/connec.test.ts @@ -41,14 +41,11 @@ export async function testHeight () { } export async function testCodes () { - const backend = new Stub.Backend() - backend.uploads.set("123", { - codeHash: "abc", - codeData: new Uint8Array() - } as any) - backend.instances.set("stub1abc", { - codeId: "123" - }) + + const backend = new Stub.Backend({}) + backend.uploads.set("123", { codeHash: "abc", codeData: new Uint8Array() } as any) + backend.instances.set("stub1abc", { codeId: "123", address: 'stub1foo' }) + const connection = new Stub.Connection({ backend }) assert.equal( await connection.getCodeId('stub1abc'), "123") @@ -56,6 +53,7 @@ export async function testCodes () { await connection.getCodeHashOfAddress('stub1abc'), "abc") assert.equal( await connection.getCodeHashOfCodeId('123'), "abc") + } export async function testAuth () { diff --git a/agent/connec.ts b/agent/connec.ts index 6371fc9dd4b..a684ef9abcf 100644 --- a/agent/connec.ts +++ b/agent/connec.ts @@ -159,7 +159,7 @@ export abstract class Connection extends Endpoint { return timed( this.doGetCodeHashOfCodeId.bind(this, codeId), ({ elapsed, result }) => this.log.debug( - `Queried in ${bold(elapsed)}: code ID ${bold(codeId)} has hash ${bold(result)}` + `Queried in ${bold(elapsed)}: code ID ${bold(codeId)} has hash:\n ${bold(result)}` ) ) } @@ -175,7 +175,7 @@ export abstract class Connection extends Endpoint { return timed( this.doGetCodeHashOfAddress.bind( this, address), ({ elapsed, result }) => this.log.debug( - `Queried in ${bold(elapsed)}: code ID ${bold(address)} has hash ${bold(result)}` + `Queried in ${bold(elapsed)}: contract ${bold(address)} has hash:\n ${bold(result)}` ) ) } diff --git a/agent/deploy.ts b/agent/deploy.ts index 52ae78c75ab..103cc9b193a 100644 --- a/agent/deploy.ts +++ b/agent/deploy.ts @@ -108,7 +108,6 @@ export class ContractCode extends Logged { } const compiled = await this.compile({ compiler, rebuild }) const uploaded = await uploader.upload(compiled, uploadOptions) - console.log({uploaded}) if (!uploaded.canInstantiate) { throw new Error("upload failed") } @@ -309,7 +308,7 @@ export class CompiledCode { } async fetch (): Promise { - const console = new Console(`compiled(${bold(this[Symbol.toStringTag])})`) + const console = new Console(`CompiledCode(${bold(this[Symbol.toStringTag])})`) if (this.codeData) { console.debug("not fetching: codeData found; unset to refetch") return this.codeData @@ -332,11 +331,10 @@ export class CompiledCode { } } else { this.codeHash = CompiledCode.toCodeHash(this.codeData) - console - .br() - .warn("TOFU: Computed code hash from fetched data:") - .warn(bold(this.codeHash)) - .warn('Pin the expected code hash by setting the codeHash property.') + console.warn( + "\n TOFU: Computed code hash from fetched data:" + + `\n ${bold(this.codeHash)}` + + '\n Pin the expected code hash by setting the codeHash property.') } return this.codeData } diff --git a/agent/stub.ts b/agent/stub.ts index 1a21b233bd2..b0e87b45f42 100644 --- a/agent/stub.ts +++ b/agent/stub.ts @@ -22,11 +22,8 @@ class StubConnection extends Connection { batch (): Batch { return new StubBatch({ connection: this }) as Batch } - get height (): Promise { - return this.doGetBlockInfo().then(({height})=>height) - } doGetHeight () { - return Promise.resolve(+ new Date()) + return this.doGetBlockInfo().then(({height})=>height) } doGetBlockInfo () { return Promise.resolve({ height: + new Date() }) @@ -100,10 +97,9 @@ class StubConnection extends Connection { doInstantiate ( codeId: CodeId, options: Parameters[1] ): Promise { - return Promise.resolve(new ContractInstance({ - address: 'stub', - label: '' - }) as ContractInstance & { address: Address }) + return Promise.resolve(new ContractInstance(this.backend.instantiate(codeId, options)) as ContractInstance & { + address: Address + }) } doExecute ( contract: { address: Address, codeHash: CodeHash }, @@ -128,7 +124,7 @@ class StubBackend extends Backend { uploads = new Map() instances = - new Map() + new Map() constructor (properties?: Partial @@ -194,11 +190,12 @@ class StubBackend extends Backend { return upload } - async instantiate (...args: unknown[]): Promise & { + instantiate (codeId: CodeId, options: unknown): Partial & { address: Address - }> { - throw new Error('not implemented') - return { address: '' } + } { + const address = `stub1${Math.floor(Math.random()*1000000)}` + this.instances.set(address, { address, codeId }) + return { address, codeId } } async execute (...args: unknown[]): Promise { diff --git a/agent/tester.ts b/agent/tester.ts index a3ae322dbb9..7f29b630011 100644 --- a/agent/tester.ts +++ b/agent/tester.ts @@ -59,6 +59,7 @@ export async function testChainSupport < console.log('Querying code upload...') equal(await bob.getCodeHashOfCodeId(uploaded.codeId), uploaded.codeHash) + rejects(()=>bob.getCodeHashOfCodeId('missing')) console.log('Instantiating code...') const label = 'my-contract-label' diff --git a/agent/token.test.ts b/agent/token.test.ts index ac75634aa9f..b968ecc3b5e 100644 --- a/agent/token.test.ts +++ b/agent/token.test.ts @@ -49,7 +49,7 @@ export async function testCoins () { export async function testFees () { // FIXME: new Fee(gas, amounts[]) - new Fee(1000, 'utest', '100000') + new Fee(1000, 'utest', '100000')[Symbol.toStringTag] } export async function testFungible () {