|
| 1 | +import * as StellarSdk from '@stellar/stellar-sdk'; |
| 2 | +import { readFileSync } from 'fs'; |
| 3 | +import { expect } from 'chai'; |
| 4 | +import path from 'path'; |
| 5 | +import { fileURLToPath } from 'url'; |
| 6 | +import { call_contract_function } from './test_helpers.js'; |
| 7 | + |
| 8 | +const __filename = fileURLToPath(import.meta.url); |
| 9 | +const dirname = path.dirname(__filename); |
| 10 | + |
| 11 | +describe('Counter', () => { |
| 12 | + let keypair; |
| 13 | + const server = new StellarSdk.SorobanRpc.Server( |
| 14 | + "https://soroban-testnet.stellar.org:443", |
| 15 | + ); |
| 16 | + |
| 17 | + let contractAddr; |
| 18 | + let contract; |
| 19 | + before(async () => { |
| 20 | + |
| 21 | + console.log('Setting up counter contract tests...'); |
| 22 | + |
| 23 | + // read secret from file |
| 24 | + const secret = readFileSync('alice.txt', 'utf8').trim(); |
| 25 | + keypair = StellarSdk.Keypair.fromSecret(secret); |
| 26 | + |
| 27 | + let contractIdFile = path.join(dirname, '.soroban', 'contract-ids', 'counter.txt'); |
| 28 | + // read contract address from file |
| 29 | + contractAddr = readFileSync(contractIdFile, 'utf8').trim().toString(); |
| 30 | + |
| 31 | + // load contract |
| 32 | + contract = new StellarSdk.Contract(contractAddr); |
| 33 | + |
| 34 | + // initialize the contract |
| 35 | + await call_contract_function("init", server, keypair, contract); |
| 36 | + |
| 37 | + }); |
| 38 | + |
| 39 | + it('get correct initial counter', async () => { |
| 40 | + // get the count |
| 41 | + let count = await call_contract_function("count", server, keypair, contract); |
| 42 | + expect(count.toString()).eq("10"); |
| 43 | + }); |
| 44 | + |
| 45 | + it('increment counter', async () => { |
| 46 | + // increment the counter |
| 47 | + await call_contract_function("increment", server, keypair, contract); |
| 48 | + |
| 49 | + // get the count |
| 50 | + let count = await call_contract_function("count", server, keypair, contract); |
| 51 | + expect(count.toString()).eq("11"); |
| 52 | + }); |
| 53 | +}); |
| 54 | + |
| 55 | + |
0 commit comments