realloc.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import * as anchor from "@coral-xyz/anchor";
  2. import { AnchorError, Program } from "@coral-xyz/anchor";
  3. import { assert } from "chai";
  4. import { Realloc } from "../target/types/realloc";
  5. describe("realloc", () => {
  6. // Configure the client to use the local cluster.
  7. anchor.setProvider(anchor.AnchorProvider.env());
  8. const program = anchor.workspace.Realloc as Program<Realloc>;
  9. const authority = (program.provider as any).wallet
  10. .payer as anchor.web3.Keypair;
  11. let sample: anchor.web3.PublicKey;
  12. before(async () => {
  13. [sample] = await anchor.web3.PublicKey.findProgramAddress(
  14. [Buffer.from("sample")],
  15. program.programId
  16. );
  17. });
  18. it("initialized", async () => {
  19. await program.methods
  20. .initialize()
  21. .accounts({ authority: authority.publicKey, sample })
  22. .rpc();
  23. const samples = await program.account.sample.all();
  24. assert.lengthOf(samples, 1);
  25. assert.lengthOf(samples[0].account.data, 1);
  26. });
  27. it("fails if delta bytes exceeds permitted limit", async () => {
  28. try {
  29. await program.methods
  30. .realloc(10250)
  31. .accounts({ authority: authority.publicKey, sample })
  32. .rpc();
  33. assert.ok(false);
  34. } catch (e) {
  35. assert.isTrue(e instanceof AnchorError);
  36. const err: AnchorError = e;
  37. const errMsg =
  38. "The account reallocation exceeds the MAX_PERMITTED_DATA_INCREASE limit";
  39. assert.strictEqual(err.error.errorMessage, errMsg);
  40. assert.strictEqual(err.error.errorCode.number, 3016);
  41. }
  42. });
  43. it("realloc additive", async () => {
  44. await program.methods
  45. .realloc(5)
  46. .accounts({ authority: authority.publicKey, sample })
  47. .rpc();
  48. const s = await program.account.sample.fetch(sample);
  49. assert.lengthOf(s.data, 5);
  50. });
  51. it("realloc substractive", async () => {
  52. await program.methods
  53. .realloc(1)
  54. .accounts({ authority: authority.publicKey, sample })
  55. .rpc();
  56. const s = await program.account.sample.fetch(sample);
  57. assert.lengthOf(s.data, 1);
  58. });
  59. it("fails with duplicate account reallocations", async () => {
  60. try {
  61. await program.methods
  62. .realloc2(1000)
  63. .accounts({
  64. authority: authority.publicKey,
  65. sample1: sample,
  66. sample2: sample,
  67. })
  68. .rpc();
  69. } catch (e) {
  70. assert.isTrue(e instanceof AnchorError);
  71. const err: AnchorError = e;
  72. const errMsg =
  73. "The account was duplicated for more than one reallocation";
  74. assert.strictEqual(err.error.errorMessage, errMsg);
  75. assert.strictEqual(err.error.errorCode.number, 3017);
  76. }
  77. });
  78. });