|
1 | | -import type { AppSpec } from '@algorandfoundation/algokit-utils/types/app-spec' |
2 | 1 | import { Bytes } from '@algorandfoundation/algorand-typescript' |
3 | 2 | import { TestExecutionContext } from '@algorandfoundation/algorand-typescript-testing' |
4 | 3 | import { Bool, interpretAsArc4 } from '@algorandfoundation/algorand-typescript/arc4' |
5 | | -import { afterEach, describe, expect, test } from 'vitest' |
| 4 | +import { afterEach, beforeAll, describe, expect } from 'vitest' |
6 | 5 | import { ABI_RETURN_VALUE_LOG_PREFIX } from '../../src/constants' |
7 | 6 | import { asUint8Array } from '../../src/util' |
8 | | -import appSpecJson from '../artifacts/arc4-primitive-ops/data/Arc4PrimitiveOpsContract.arc32.json' |
9 | | -import { getAlgorandAppClient, getAvmResult } from '../avm-invoker' |
| 7 | +import { getAvmResult } from '../avm-invoker' |
| 8 | +import { createArc4TestFixture } from '../test-fixture' |
10 | 9 |
|
11 | 10 | describe('arc4.Bool', async () => { |
12 | | - const appClient = await getAlgorandAppClient(appSpecJson as AppSpec) |
| 11 | + const [test, localnetFixture] = createArc4TestFixture('tests/artifacts/arc4-primitive-ops/data/Arc4PrimitiveOpsContract.arc56.json', { |
| 12 | + Arc4PrimitiveOpsContract: { deployParams: { createParams: { extraProgramPages: undefined } } }, |
| 13 | + }) |
13 | 14 | const ctx = new TestExecutionContext() |
14 | 15 |
|
| 16 | + beforeAll(async () => { |
| 17 | + await localnetFixture.newScope() |
| 18 | + }) |
| 19 | + |
15 | 20 | afterEach(() => { |
16 | 21 | ctx.reset() |
17 | 22 | }) |
18 | 23 |
|
19 | | - test.each([true, false])('should be able to get bytes representation of %s', async (value) => { |
20 | | - const avmResult = await getAvmResult({ appClient }, 'verify_bool_bytes', value) |
21 | | - const result = new Bool(value) |
22 | | - expect(result.bytes).toEqual(avmResult) |
23 | | - }) |
| 24 | + test.for([true, false])( |
| 25 | + 'should be able to get bytes representation of %s', |
| 26 | + async (value, { appClientArc4PrimitiveOpsContract: appClient }) => { |
| 27 | + const avmResult = await getAvmResult({ appClient }, 'verify_bool_bytes', value) |
| 28 | + const result = new Bool(value) |
| 29 | + expect(result.bytes).toEqual(avmResult) |
| 30 | + }, |
| 31 | + ) |
24 | 32 |
|
25 | | - test.each([asUint8Array(Bytes.fromHex('00')), asUint8Array(Bytes.fromHex('80'))])('create Bool from bytes', async (value) => { |
26 | | - const avmResult = await getAvmResult({ appClient }, 'verify_bool_from_bytes', value) |
27 | | - const result = interpretAsArc4<Bool>(Bytes(value)) |
28 | | - expect(result.native).toEqual(avmResult) |
29 | | - }) |
| 33 | + test.for([asUint8Array(Bytes.fromHex('00')), asUint8Array(Bytes.fromHex('80'))])( |
| 34 | + 'create Bool from bytes', |
| 35 | + async (value, { appClientArc4PrimitiveOpsContract: appClient }) => { |
| 36 | + const avmResult = await getAvmResult({ appClient }, 'verify_bool_from_bytes', value) |
| 37 | + const result = interpretAsArc4<Bool>(Bytes(value)) |
| 38 | + expect(result.native).toEqual(avmResult) |
| 39 | + }, |
| 40 | + ) |
30 | 41 |
|
31 | | - test.each([asUint8Array(Bytes.fromHex('00')), asUint8Array(Bytes.fromHex('80'))])('create Bool from log', async (value) => { |
32 | | - const paddedValue = new Uint8Array([...asUint8Array(ABI_RETURN_VALUE_LOG_PREFIX), ...value]) |
33 | | - const avmResult = await getAvmResult({ appClient }, 'verify_bool_from_log', paddedValue) |
34 | | - const result = interpretAsArc4<Bool>(Bytes(paddedValue), 'log') |
35 | | - expect(result.native).toEqual(avmResult) |
36 | | - }) |
| 42 | + test.for([asUint8Array(Bytes.fromHex('00')), asUint8Array(Bytes.fromHex('80'))])( |
| 43 | + 'create Bool from log', |
| 44 | + async (value, { appClientArc4PrimitiveOpsContract: appClient }) => { |
| 45 | + const paddedValue = new Uint8Array([...asUint8Array(ABI_RETURN_VALUE_LOG_PREFIX), ...value]) |
| 46 | + const avmResult = await getAvmResult({ appClient }, 'verify_bool_from_log', paddedValue) |
| 47 | + const result = interpretAsArc4<Bool>(Bytes(paddedValue), 'log') |
| 48 | + expect(result.native).toEqual(avmResult) |
| 49 | + }, |
| 50 | + ) |
37 | 51 |
|
38 | | - test.each([ |
| 52 | + test.for([ |
39 | 53 | [asUint8Array(Bytes.fromHex('00')), asUint8Array('')], |
40 | 54 | [asUint8Array(Bytes.fromHex('80')), asUint8Array(Bytes.fromHex('ff000102'))], |
41 | 55 | [asUint8Array(Bytes.fromHex('00')), asUint8Array(ABI_RETURN_VALUE_LOG_PREFIX.slice(0, 3))], |
42 | | - ])('should throw error when log prefix is invalid for Bool', async (value, prefix) => { |
43 | | - const paddedValue = new Uint8Array([...prefix, ...value]) |
44 | | - await expect(() => getAvmResult({ appClient }, 'verify_bool_from_log', paddedValue)).rejects.toThrowError( |
45 | | - new RegExp('(assert failed)|(extraction start \\d+ is beyond length)'), |
46 | | - ) |
47 | | - expect(() => interpretAsArc4<Bool>(Bytes(paddedValue), 'log')).toThrowError('ABI return prefix not found') |
48 | | - }) |
| 56 | + ])( |
| 57 | + 'should throw error when log prefix is invalid for Bool', |
| 58 | + async ([value, prefix], { appClientArc4PrimitiveOpsContract: appClient }) => { |
| 59 | + const paddedValue = new Uint8Array([...prefix, ...value]) |
| 60 | + await expect(() => getAvmResult({ appClient }, 'verify_bool_from_log', paddedValue)).rejects.toThrowError( |
| 61 | + new RegExp('(has valid prefix)|(extraction start \\d+ is beyond length)'), |
| 62 | + ) |
| 63 | + expect(() => interpretAsArc4<Bool>(Bytes(paddedValue), 'log')).toThrowError('ABI return prefix not found') |
| 64 | + }, |
| 65 | + ) |
49 | 66 | }) |
0 commit comments