|
| 1 | +import * as anchor from "@coral-xyz/anchor"; |
| 2 | +import { Program } from "@coral-xyz/anchor"; |
| 3 | +import { Keypair, PublicKey } from "@solana/web3.js"; |
| 4 | +import { startAnchor } from "solana-bankrun"; |
| 5 | +import { BankrunProvider } from "anchor-bankrun"; |
| 6 | +import { assert } from "chai"; |
| 7 | +import { CloseAccountProgram } from "../target/types/close_account_program"; |
| 8 | +import IDL from "../target/idl/close_account_program.json" with { type: 'json' }; |
| 9 | + |
| 10 | +describe("create_account and close_account", () => { |
| 11 | + let provider: BankrunProvider; |
| 12 | + let program: Program<CloseAccountProgram>; |
| 13 | + let payer: Keypair; |
| 14 | + let dataAccount: PublicKey; |
| 15 | + |
| 16 | + before(async () => { |
| 17 | + const context = await startAnchor( |
| 18 | + "", |
| 19 | + [{ name: "close_account_program", programId: new PublicKey(IDL.address) }], |
| 20 | + [] |
| 21 | + ); |
| 22 | + provider = new BankrunProvider(context); |
| 23 | + anchor.setProvider(provider); |
| 24 | + // Note: we might need to cast IDL depending on your setup. |
| 25 | + program = new Program<CloseAccountProgram>(IDL as any, provider); |
| 26 | + payer = provider.wallet.payer; |
| 27 | + const [pda] = PublicKey.findProgramAddressSync( |
| 28 | + [Buffer.from("USER"), payer.publicKey.toBuffer()], |
| 29 | + program.programId |
| 30 | + ); |
| 31 | + dataAccount = pda; |
| 32 | + }); |
| 33 | + |
| 34 | + it("create account", async () => { |
| 35 | + const tx = await program.methods.createUser("test") |
| 36 | + .accounts({ |
| 37 | + user: payer.publicKey, |
| 38 | + userAccount: dataAccount, |
| 39 | + }) |
| 40 | + .signers([payer]) |
| 41 | + .rpc(); |
| 42 | + console.log("Your transaction signature", tx); |
| 43 | + assert.ok(true); |
| 44 | + |
| 45 | + const account = await program.account.userState.fetch(dataAccount); |
| 46 | + assert.equal(account.user.toBase58(), payer.publicKey.toBase58()); |
| 47 | + assert.equal(account.name, "test"); |
| 48 | + }); |
| 49 | + |
| 50 | + it("close account", async () => { |
| 51 | + const tx = await program.methods.closeUser() |
| 52 | + .accounts({ |
| 53 | + user: payer.publicKey, |
| 54 | + userAccount: dataAccount, |
| 55 | + }) |
| 56 | + .signers([payer]) |
| 57 | + .rpc(); |
| 58 | + console.log("Close account transaction signature", tx); |
| 59 | + |
| 60 | + // When an account is closed, fetching it should fail |
| 61 | + try { |
| 62 | + await program.account.userState.fetch(dataAccount); |
| 63 | + // If it succeeds, the account was not closed properly, so we fail the test |
| 64 | + assert.fail("The account should have been closed and not fetchable"); |
| 65 | + } catch (err: any) { |
| 66 | + // We expect an error because the account was closed/deleted |
| 67 | + console.log("Account successfully deleted! Fetch error:", err.message); |
| 68 | + assert.ok(true); |
| 69 | + } |
| 70 | + assert.ok(true); |
| 71 | + }); |
| 72 | +}); |
0 commit comments