component.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import { Keypair } from "@solana/web3.js";
  2. import {
  3. anchor,
  4. FindEntityPda,
  5. FindComponentPda,
  6. SerializeArgs,
  7. } from "../../../clients/bolt-sdk/lib";
  8. import { assert, expect } from "chai";
  9. export function component(framework) {
  10. let entity: anchor.web3.PublicKey;
  11. let component: anchor.web3.PublicKey;
  12. describe("Component authority", () => {
  13. it("Add entity 5", async () => {
  14. const world = await framework.worldProgram.account.world.fetch(
  15. framework.worldPda,
  16. );
  17. entity = FindEntityPda({
  18. worldId: world.id,
  19. entityId: world.entities,
  20. });
  21. const instruction = await framework.worldProgram.methods
  22. .addEntity(null)
  23. .accounts({
  24. payer: framework.provider.wallet.publicKey,
  25. world: framework.worldPda,
  26. entity: entity,
  27. })
  28. .instruction();
  29. const transaction = new anchor.web3.Transaction().add(instruction);
  30. await framework.provider.sendAndConfirm(transaction);
  31. });
  32. it("Initialize position component with authority on authority test entity", async () => {
  33. const componentId = framework.exampleComponentPosition.programId;
  34. component = FindComponentPda({
  35. componentId,
  36. entity: entity,
  37. });
  38. const instruction = await framework.worldProgram.methods
  39. .initializeComponent()
  40. .accounts({
  41. payer: framework.provider.wallet.publicKey,
  42. entity: entity,
  43. data: component,
  44. componentProgram: componentId,
  45. authority: framework.provider.wallet.publicKey,
  46. })
  47. .instruction();
  48. const transaction = new anchor.web3.Transaction().add(instruction);
  49. await framework.provider.sendAndConfirm(transaction);
  50. });
  51. it("Shouldn't apply fly system on authority test entity with wrong authority", async () => {
  52. const positionBefore =
  53. await framework.exampleComponentPosition.account.position.fetch(
  54. component,
  55. );
  56. const keypair = Keypair.generate();
  57. const instruction = await framework.worldProgram.methods
  58. .apply(SerializeArgs())
  59. .accounts({
  60. authority: keypair.publicKey,
  61. boltSystem: framework.systemFly.programId,
  62. world: framework.worldPda,
  63. sessionToken: null,
  64. })
  65. .remainingAccounts([
  66. {
  67. pubkey: framework.exampleComponentPosition.programId,
  68. isSigner: false,
  69. isWritable: false,
  70. },
  71. {
  72. pubkey: component,
  73. isSigner: false,
  74. isWritable: true,
  75. },
  76. ])
  77. .instruction();
  78. const transaction = new anchor.web3.Transaction().add(instruction);
  79. try {
  80. await framework.provider.sendAndConfirm(transaction, [keypair]);
  81. assert.fail(
  82. "Shouldn't apply fly system on authority test entity with wrong authority",
  83. );
  84. } catch (error) {
  85. expect(error.logs.join("\n")).to.contain(
  86. "Error Code: InvalidAuthority",
  87. );
  88. }
  89. const positionAfter =
  90. await framework.exampleComponentPosition.account.position.fetch(
  91. component,
  92. );
  93. expect(positionBefore.x.toNumber()).to.equal(positionAfter.x.toNumber());
  94. expect(positionBefore.y.toNumber()).to.equal(positionAfter.y.toNumber());
  95. expect(positionBefore.z.toNumber()).to.equal(positionAfter.z.toNumber());
  96. });
  97. it("Apply Fly System on authority test entity should succeed with correct authority", async () => {
  98. const positionBefore =
  99. await framework.exampleComponentPosition.account.position.fetch(
  100. component,
  101. );
  102. const instruction = await framework.worldProgram.methods
  103. .apply(SerializeArgs())
  104. .accounts({
  105. authority: framework.provider.wallet.publicKey,
  106. boltSystem: framework.systemFly.programId,
  107. world: framework.worldPda,
  108. sessionToken: null,
  109. })
  110. .remainingAccounts([
  111. {
  112. pubkey: framework.exampleComponentPosition.programId,
  113. isSigner: false,
  114. isWritable: false,
  115. },
  116. {
  117. pubkey: component,
  118. isSigner: false,
  119. isWritable: true,
  120. },
  121. ])
  122. .instruction();
  123. const transaction = new anchor.web3.Transaction().add(instruction);
  124. await framework.provider.sendAndConfirm(transaction);
  125. const positionAfter =
  126. await framework.exampleComponentPosition.account.position.fetch(
  127. component,
  128. );
  129. expect(positionAfter.x.toNumber()).to.equal(positionBefore.x.toNumber());
  130. expect(positionAfter.y.toNumber()).to.equal(positionBefore.y.toNumber());
  131. expect(positionAfter.z.toNumber()).to.equal(
  132. positionBefore.z.toNumber() + 1,
  133. );
  134. });
  135. });
  136. }