Skip to content

Commit

Permalink
test+refactor(agent): 90% coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
egasimus committed Nov 16, 2023
1 parent d44112d commit 4cd08b6
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 31 deletions.
14 changes: 6 additions & 8 deletions agent/connec.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,19 @@ 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")
assert.equal(
await connection.getCodeHashOfAddress('stub1abc'), "abc")
assert.equal(
await connection.getCodeHashOfCodeId('123'), "abc")

}

export async function testAuth () {
Expand Down
4 changes: 2 additions & 2 deletions agent/connec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)}`
)
)
}
Expand All @@ -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)}`
)
)
}
Expand Down
12 changes: 5 additions & 7 deletions agent/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down Expand Up @@ -309,7 +308,7 @@ export class CompiledCode {
}

async fetch (): Promise<Uint8Array> {
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
Expand All @@ -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
}
Expand Down
23 changes: 10 additions & 13 deletions agent/stub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,8 @@ class StubConnection extends Connection {
batch (): Batch<this> {
return new StubBatch({ connection: this }) as Batch<this>
}
get height (): Promise<number> {
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() })
Expand Down Expand Up @@ -100,10 +97,9 @@ class StubConnection extends Connection {
doInstantiate (
codeId: CodeId, options: Parameters<Connection["doInstantiate"]>[1]
): Promise<ContractInstance & { address: Address }> {
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 },
Expand All @@ -128,7 +124,7 @@ class StubBackend extends Backend {
uploads =
new Map<CodeId, {chainId: ChainId, codeId: CodeId, codeHash: CodeHash, codeData: Uint8Array}>()
instances =
new Map<Address, {codeId: CodeId}>()
new Map<Address, {codeId: CodeId, address: Address}>()

constructor (properties?: Partial<StubBackend & {
genesisAccounts: Record<string, string|number>
Expand Down Expand Up @@ -194,11 +190,12 @@ class StubBackend extends Backend {
return upload
}

async instantiate (...args: unknown[]): Promise<Partial<ContractInstance> & {
instantiate (codeId: CodeId, options: unknown): Partial<ContractInstance> & {
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<unknown> {
Expand Down
1 change: 1 addition & 0 deletions agent/tester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
2 changes: 1 addition & 1 deletion agent/token.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down

0 comments on commit 4cd08b6

Please sign in to comment.