|
1 | | -import type { Program } from '@coral-xyz/anchor'; |
2 | | -import * as anchor from '@coral-xyz/anchor'; |
3 | | -import { Keypair } from '@solana/web3.js'; |
4 | | -import { assert } from 'chai'; |
5 | | -import type { AnchorRealloc } from '../target/types/anchor_realloc'; |
| 1 | +import type { Program } from "@coral-xyz/anchor"; |
| 2 | +import * as anchor from "@coral-xyz/anchor"; |
| 3 | +import { Keypair } from "@solana/web3.js"; |
| 4 | +import { assert } from "chai"; |
| 5 | +import type { AnchorRealloc } from "../target/types/anchor_realloc.ts"; |
6 | 6 |
|
7 | | -describe('anchor-realloc', () => { |
8 | | - // Configure the client to use the local cluster. |
9 | | - const provider = anchor.AnchorProvider.env(); |
10 | | - anchor.setProvider(provider); |
11 | | - const payer = provider.wallet as anchor.Wallet; |
12 | | - const connection = provider.connection; |
| 7 | +describe("Anchor: realloc", () => { |
| 8 | + // Configure the client to use the local cluster. |
| 9 | + const provider = anchor.AnchorProvider.env(); |
| 10 | + anchor.setProvider(provider); |
| 11 | + const payer = provider.wallet as anchor.Wallet; |
| 12 | + const connection = provider.connection; |
13 | 13 |
|
14 | | - const program = anchor.workspace.AnchorRealloc as Program<AnchorRealloc>; |
| 14 | + const program = anchor.workspace.AnchorRealloc as Program<AnchorRealloc>; |
15 | 15 |
|
16 | | - const messageAccount = new Keypair(); |
| 16 | + const messageAccount = new Keypair(); |
17 | 17 |
|
18 | | - // helper function to check the account data and message |
19 | | - async function checkAccount(publicKey, expectedMessage) { |
20 | | - const accountInfo = await connection.getAccountInfo(publicKey); |
21 | | - const accountData = await program.account.message.fetch(publicKey); |
| 18 | + // helper function to check the account data and message |
| 19 | + async function checkAccount(publicKey, expectedMessage) { |
| 20 | + const accountInfo = await connection.getAccountInfo(publicKey); |
| 21 | + const accountData = await program.account.message.fetch(publicKey); |
22 | 22 |
|
23 | | - // 8 bytes for the discriminator, |
24 | | - // 4 bytes for the length of the message, |
25 | | - // and the length of the message |
26 | | - assert.equal(accountInfo.data.length, 8 + 4 + expectedMessage.length); |
27 | | - assert.equal(accountData.message, expectedMessage); |
| 23 | + // 8 bytes for the discriminator, |
| 24 | + // 4 bytes for the length of the message, |
| 25 | + // and the length of the message |
| 26 | + assert.equal(accountInfo.data.length, 8 + 4 + expectedMessage.length); |
| 27 | + assert.equal(accountData.message, expectedMessage); |
| 28 | + } |
28 | 29 |
|
29 | | - console.log(`Account Data Length: ${accountInfo.data.length}`); |
30 | | - console.log(`Message: ${accountData.message}`); |
31 | | - } |
| 30 | + it("Is initialized!", async () => { |
| 31 | + const input = "hello"; |
32 | 32 |
|
33 | | - it('Is initialized!', async () => { |
34 | | - const input = 'hello'; |
| 33 | + await program.methods |
| 34 | + .initialize(input) |
| 35 | + .accounts({ |
| 36 | + payer: payer.publicKey, |
| 37 | + messageAccount: messageAccount.publicKey, |
| 38 | + }) |
| 39 | + .signers([messageAccount]) |
| 40 | + .rpc(); |
35 | 41 |
|
36 | | - await program.methods |
37 | | - .initialize(input) |
38 | | - .accounts({ |
39 | | - payer: payer.publicKey, |
40 | | - messageAccount: messageAccount.publicKey, |
41 | | - }) |
42 | | - .signers([messageAccount]) |
43 | | - .rpc(); |
| 42 | + await checkAccount(messageAccount.publicKey, input); |
| 43 | + }); |
44 | 44 |
|
45 | | - await checkAccount(messageAccount.publicKey, input); |
46 | | - }); |
| 45 | + it("Update", async () => { |
| 46 | + const input = "hello world"; |
47 | 47 |
|
48 | | - it('Update', async () => { |
49 | | - const input = 'hello world'; |
| 48 | + await program.methods |
| 49 | + .update(input) |
| 50 | + .accounts({ |
| 51 | + payer: payer.publicKey, |
| 52 | + messageAccount: messageAccount.publicKey, |
| 53 | + }) |
| 54 | + .rpc(); |
50 | 55 |
|
51 | | - await program.methods |
52 | | - .update(input) |
53 | | - .accounts({ |
54 | | - payer: payer.publicKey, |
55 | | - messageAccount: messageAccount.publicKey, |
56 | | - }) |
57 | | - .rpc(); |
| 56 | + await checkAccount(messageAccount.publicKey, input); |
| 57 | + }); |
58 | 58 |
|
59 | | - await checkAccount(messageAccount.publicKey, input); |
60 | | - }); |
| 59 | + it("Again update", async () => { |
| 60 | + const input = "hi"; |
61 | 61 |
|
62 | | - it('Update', async () => { |
63 | | - const input = 'hi'; |
| 62 | + await program.methods |
| 63 | + .update(input) |
| 64 | + .accounts({ |
| 65 | + payer: payer.publicKey, |
| 66 | + messageAccount: messageAccount.publicKey, |
| 67 | + }) |
| 68 | + .rpc(); |
64 | 69 |
|
65 | | - await program.methods |
66 | | - .update(input) |
67 | | - .accounts({ |
68 | | - payer: payer.publicKey, |
69 | | - messageAccount: messageAccount.publicKey, |
70 | | - }) |
71 | | - .rpc(); |
72 | | - |
73 | | - await checkAccount(messageAccount.publicKey, input); |
74 | | - }); |
| 70 | + await checkAccount(messageAccount.publicKey, input); |
| 71 | + }); |
75 | 72 | }); |
0 commit comments