|
| 1 | +import { Bytes, Uint64 } from '@algorandfoundation/algorand-typescript' |
| 2 | +import { bytesToUint8Array, TestExecutionContext } from '@algorandfoundation/algorand-typescript-testing' |
| 3 | +import { DynamicArray, UintN8 } from '@algorandfoundation/algorand-typescript/arc4' |
| 4 | +import nacl from 'tweetnacl' |
| 5 | +import { afterEach, describe, expect, it } from 'vitest' |
| 6 | +import { VotingRoundApp } from './contract.algo' |
| 7 | + |
| 8 | +describe('VotingRoundApp', () => { |
| 9 | + const ctx = new TestExecutionContext() |
| 10 | + |
| 11 | + const boostrapMinBalanceReq = Uint64(287100) |
| 12 | + const voteMinBalanceReq = Uint64(21300) |
| 13 | + const tallyBoxSize = 208 |
| 14 | + const keyPair = nacl.sign.keyPair() |
| 15 | + const voteId = ctx.any.string() |
| 16 | + |
| 17 | + const createContract = () => { |
| 18 | + const contract = ctx.contract.create(VotingRoundApp) |
| 19 | + const snapshotPublicKey = Bytes(keyPair.publicKey) |
| 20 | + const metadataIpfsCid = ctx.any.string(16) |
| 21 | + const startTime = ctx.any.uint64(Date.now() - 10_000, Date.now()) |
| 22 | + const endTime = ctx.any.uint64(Date.now() + 10_000, Date.now() + 100_000) |
| 23 | + const optionCounts = new DynamicArray<UintN8>( |
| 24 | + ...Array(13) |
| 25 | + .fill(0) |
| 26 | + .map(() => new UintN8(2)), |
| 27 | + ) |
| 28 | + const quorum = ctx.any.uint64() |
| 29 | + const nftImageUrl = ctx.any.string(64) |
| 30 | + contract.create(voteId, snapshotPublicKey, metadataIpfsCid, startTime, endTime, optionCounts, quorum, nftImageUrl) |
| 31 | + return contract |
| 32 | + } |
| 33 | + |
| 34 | + afterEach(() => { |
| 35 | + ctx.reset() |
| 36 | + }) |
| 37 | + |
| 38 | + it('shoulld be able to bootstrap', () => { |
| 39 | + const contract = createContract() |
| 40 | + const app = ctx.ledger.getApplicationForContract(contract) |
| 41 | + contract.bootstrap(ctx.any.txn.payment({ receiver: app.address, amount: boostrapMinBalanceReq })) |
| 42 | + |
| 43 | + expect(contract.isBootstrapped.value).toEqual(true) |
| 44 | + expect(contract.tallyBox.value).toEqual(Bytes.fromHex('00'.repeat(tallyBoxSize))) |
| 45 | + }) |
| 46 | + |
| 47 | + it('should be able to get pre conditions', () => { |
| 48 | + const contract = createContract() |
| 49 | + const app = ctx.ledger.getApplicationForContract(contract) |
| 50 | + contract.bootstrap(ctx.any.txn.payment({ receiver: app.address, amount: boostrapMinBalanceReq })) |
| 51 | + |
| 52 | + const account = ctx.any.account() |
| 53 | + const signature = nacl.sign.detached(bytesToUint8Array(account.bytes), keyPair.secretKey) |
| 54 | + ctx.txn.createScope([ctx.any.txn.applicationCall({ sender: account })]).execute(() => { |
| 55 | + const preconditions = contract.getPreconditions(Bytes(signature)) |
| 56 | + |
| 57 | + expect(preconditions.is_allowed_to_vote).toEqual(1) |
| 58 | + expect(preconditions.is_voting_open).toEqual(1) |
| 59 | + expect(preconditions.has_already_voted).toEqual(0) |
| 60 | + expect(preconditions.current_time).toEqual(ctx.txn.activeGroup.latestTimestamp) |
| 61 | + }) |
| 62 | + }) |
| 63 | + |
| 64 | + it('should be able to vote', () => { |
| 65 | + const contract = createContract() |
| 66 | + const app = ctx.ledger.getApplicationForContract(contract) |
| 67 | + contract.bootstrap(ctx.any.txn.payment({ receiver: app.address, amount: boostrapMinBalanceReq })) |
| 68 | + |
| 69 | + const account = ctx.any.account() |
| 70 | + const signature = nacl.sign.detached(bytesToUint8Array(account.bytes), keyPair.secretKey) |
| 71 | + const answerIds = new DynamicArray<UintN8>( |
| 72 | + ...Array(13) |
| 73 | + .fill(0) |
| 74 | + .map(() => new UintN8(Math.ceil(Math.random() * 10) % 2)), |
| 75 | + ) |
| 76 | + |
| 77 | + ctx.txn.createScope([ctx.any.txn.applicationCall({ appId: app, sender: account })]).execute(() => { |
| 78 | + contract.vote(ctx.any.txn.payment({ receiver: app.address, amount: voteMinBalanceReq }), Bytes(signature), answerIds) |
| 79 | + |
| 80 | + expect(contract.votesByAccount.get(account).bytes).toEqual(answerIds.bytes) |
| 81 | + expect(contract.voterCount.value).toEqual(13) |
| 82 | + }) |
| 83 | + }) |
| 84 | + |
| 85 | + it('should be able to close', () => { |
| 86 | + const contract = createContract() |
| 87 | + const app = ctx.ledger.getApplicationForContract(contract) |
| 88 | + contract.bootstrap(ctx.any.txn.payment({ receiver: app.address, amount: boostrapMinBalanceReq })) |
| 89 | + |
| 90 | + contract.close() |
| 91 | + |
| 92 | + expect(contract.closeTime.value).toEqual(ctx.txn.lastGroup.latestTimestamp) |
| 93 | + expect(contract.nftAsset.value.name).toEqual(`[VOTE RESULT] ${voteId}`) |
| 94 | + expect(contract.nftAsset.value.unitName).toEqual('VOTERSLT') |
| 95 | + }) |
| 96 | +}) |
0 commit comments