123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- import * as anchor from '@coral-xyz/anchor';
- import type { Program } from '@coral-xyz/anchor';
- import { Keypair } from '@solana/web3.js';
- import { assert } from 'chai';
- import type { AnchorRealloc } from '../target/types/anchor_realloc';
- describe('anchor-realloc', () => {
- // Configure the client to use the local cluster.
- const provider = anchor.AnchorProvider.env();
- anchor.setProvider(provider);
- const payer = provider.wallet as anchor.Wallet;
- const connection = provider.connection;
- const program = anchor.workspace.AnchorRealloc as Program<AnchorRealloc>;
- const messageAccount = new Keypair();
- // helper function to check the account data and message
- async function checkAccount(publicKey, expectedMessage) {
- const accountInfo = await connection.getAccountInfo(publicKey);
- const accountData = await program.account.message.fetch(publicKey);
- // 8 bytes for the discriminator,
- // 4 bytes for the length of the message,
- // and the length of the message
- assert.equal(accountInfo.data.length, 8 + 4 + expectedMessage.length);
- assert.equal(accountData.message, expectedMessage);
- console.log(`Account Data Length: ${accountInfo.data.length}`);
- console.log(`Message: ${accountData.message}`);
- }
- it('Is initialized!', async () => {
- const input = 'hello';
- await program.methods
- .initialize(input)
- .accounts({
- payer: payer.publicKey,
- messageAccount: messageAccount.publicKey,
- })
- .signers([messageAccount])
- .rpc();
- await checkAccount(messageAccount.publicKey, input);
- });
- it('Update', async () => {
- const input = 'hello world';
- await program.methods
- .update(input)
- .accounts({
- payer: payer.publicKey,
- messageAccount: messageAccount.publicKey,
- })
- .rpc();
- await checkAccount(messageAccount.publicKey, input);
- });
- it('Update', async () => {
- const input = 'hi';
- await program.methods
- .update(input)
- .accounts({
- payer: payer.publicKey,
- messageAccount: messageAccount.publicKey,
- })
- .rpc();
- await checkAccount(messageAccount.publicKey, input);
- });
- });
|