const anchor = require("@project-serum/anchor"); const serumCmn = require("@project-serum/common"); const assert = require("assert"); describe("misc", () => { // Configure the client to use the local cluster. anchor.setProvider(anchor.Provider.env()); const program = anchor.workspace.Misc; const misc2Program = anchor.workspace.Misc2; it("Can allocate extra space for a state constructor", async () => { const tx = await program.state.rpc.new(); const addr = await program.state.address(); const state = await program.state(); const accountInfo = await program.provider.connection.getAccountInfo(addr); assert.ok(state.v.equals(Buffer.from([]))); assert.ok(accountInfo.data.length === 99); }); it("Can use u128 and i128", async () => { const data = new anchor.web3.Account(); const tx = await program.rpc.initialize( new anchor.BN(1234), new anchor.BN(22), { accounts: { data: data.publicKey, rent: anchor.web3.SYSVAR_RENT_PUBKEY, }, signers: [data], instructions: [await program.account.data.createInstruction(data)], } ); const dataAccount = await program.account.data(data.publicKey); assert.ok(dataAccount.udata.eq(new anchor.BN(1234))); assert.ok(dataAccount.idata.eq(new anchor.BN(22))); }); it("Can embed programs into genesis from the Anchor.toml", async () => { const pid = new anchor.web3.PublicKey( "FtMNMKp9DZHKWUyVAsj3Q5QV8ow4P3fUPP7ZrWEQJzKr" ); let accInfo = await anchor.getProvider().connection.getAccountInfo(pid); assert.ok(accInfo.executable); }); it("Can use the executable attribtue", async () => { await program.rpc.testExecutable({ accounts: { program: program.programId, }, }); await assert.rejects( async () => { await program.rpc.testExecutable({ accounts: { program: program.provider.wallet.publicKey, }, }); }, (err) => { return true; } ); }); it("Can CPI to state instructions", async () => { const oldData = new anchor.BN(0); await misc2Program.state.rpc.new({ accounts: { authority: program.provider.wallet.publicKey, }, }); let stateAccount = await misc2Program.state(); assert.ok(stateAccount.data.eq(oldData)); assert.ok(stateAccount.auth.equals(program.provider.wallet.publicKey)); const newData = new anchor.BN(2134); await program.rpc.testStateCpi(newData, { accounts: { authority: program.provider.wallet.publicKey, cpiState: await misc2Program.state.address(), misc2Program: misc2Program.programId, }, }); stateAccount = await misc2Program.state(); assert.ok(stateAccount.data.eq(newData)); assert.ok(stateAccount.auth.equals(program.provider.wallet.publicKey)); }); });