world.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import { expect } from "chai";
  2. import {
  3. AddAuthority,
  4. RemoveAuthority,
  5. ApplySystem,
  6. ApproveSystem,
  7. RemoveSystem,
  8. } from "../../../clients/bolt-sdk/lib";
  9. export function world(framework) {
  10. describe("World authority", () => {
  11. it("Add authority", async () => {
  12. const addAuthority = await AddAuthority({
  13. authority: framework.provider.wallet.publicKey,
  14. newAuthority: framework.provider.wallet.publicKey,
  15. world: framework.worldPda,
  16. connection: framework.provider.connection,
  17. });
  18. await framework.provider.sendAndConfirm(addAuthority.transaction, [], {
  19. skipPreflight: true,
  20. });
  21. const worldAccount = await framework.worldProgram.account.world.fetch(
  22. framework.worldPda,
  23. );
  24. expect(
  25. worldAccount.authorities.some((auth) =>
  26. auth.equals(framework.provider.wallet.publicKey),
  27. ),
  28. );
  29. });
  30. it("Add a second authority", async () => {
  31. const addAuthority = await AddAuthority({
  32. authority: framework.provider.wallet.publicKey,
  33. newAuthority: framework.secondAuthority,
  34. world: framework.worldPda,
  35. connection: framework.provider.connection,
  36. });
  37. await framework.provider.sendAndConfirm(addAuthority.transaction);
  38. const worldAccount = await framework.worldProgram.account.world.fetch(
  39. framework.worldPda,
  40. );
  41. expect(
  42. worldAccount.authorities.some((auth) =>
  43. auth.equals(framework.secondAuthority),
  44. ),
  45. );
  46. });
  47. it("Remove an authority", async () => {
  48. const removeAuthority = await RemoveAuthority({
  49. authority: framework.provider.wallet.publicKey,
  50. authorityToDelete: framework.secondAuthority,
  51. world: framework.worldPda,
  52. connection: framework.provider.connection,
  53. });
  54. await framework.provider.sendAndConfirm(removeAuthority.transaction);
  55. const worldAccount = await framework.worldProgram.account.world.fetch(
  56. framework.worldPda,
  57. );
  58. expect(
  59. !worldAccount.authorities.some((auth) =>
  60. auth.equals(framework.secondAuthority),
  61. ),
  62. );
  63. });
  64. it("Whitelist Fly System", async () => {
  65. const approveSystem = await ApproveSystem({
  66. authority: framework.provider.wallet.publicKey,
  67. systemToApprove: framework.systemFly.programId,
  68. world: framework.worldPda,
  69. });
  70. await framework.provider.sendAndConfirm(approveSystem.transaction, [], {
  71. skipPreflight: true,
  72. });
  73. // Get World and check permissionless and systems
  74. const worldAccount = await framework.worldProgram.account.world.fetch(
  75. framework.worldPda,
  76. );
  77. expect(worldAccount.permissionless).to.equal(false);
  78. expect(worldAccount.systems.length).to.be.greaterThan(0);
  79. });
  80. it("Whitelist Apply Velocity System system", async () => {
  81. const approveSystem = await ApproveSystem({
  82. authority: framework.provider.wallet.publicKey,
  83. systemToApprove: framework.systemApplyVelocity.programId,
  84. world: framework.worldPda,
  85. });
  86. await framework.provider.sendAndConfirm(approveSystem.transaction, [], {
  87. skipPreflight: true,
  88. });
  89. // Get World and check permissionless and systems
  90. const worldAccount = await framework.worldProgram.account.world.fetch(
  91. framework.worldPda,
  92. );
  93. expect(worldAccount.permissionless).to.equal(false);
  94. expect(worldAccount.systems.length).to.be.greaterThan(0);
  95. });
  96. it("Apply Fly System on Entity 1", async () => {
  97. const applySystem = await ApplySystem({
  98. authority: framework.provider.wallet.publicKey,
  99. systemId: framework.systemFly.programId,
  100. world: framework.worldPda,
  101. entities: [
  102. {
  103. entity: framework.entity1Pda,
  104. components: [
  105. { componentId: framework.exampleComponentPosition.programId },
  106. ],
  107. },
  108. ],
  109. });
  110. await framework.provider.sendAndConfirm(applySystem.transaction);
  111. });
  112. it("Remove Fly System", async () => {
  113. const removeSystem = await RemoveSystem({
  114. authority: framework.provider.wallet.publicKey,
  115. systemToRemove: framework.systemFly.programId,
  116. world: framework.worldPda,
  117. });
  118. await framework.provider.sendAndConfirm(removeSystem.transaction, [], {
  119. skipPreflight: true,
  120. });
  121. // Get World and check permissionless and systems
  122. const worldAccount = await framework.worldProgram.account.world.fetch(
  123. framework.worldPda,
  124. );
  125. expect(worldAccount.permissionless).to.equal(false);
  126. expect(worldAccount.systems.length).to.be.greaterThan(0);
  127. });
  128. it("Apply unauthorized Fly System on Entity 1", async () => {
  129. const applySystem = await ApplySystem({
  130. authority: framework.provider.wallet.publicKey,
  131. systemId: framework.systemFly.programId,
  132. world: framework.worldPda,
  133. entities: [
  134. {
  135. entity: framework.entity1Pda,
  136. components: [
  137. { componentId: framework.exampleComponentPosition.programId },
  138. ],
  139. },
  140. ],
  141. });
  142. let invalid = false;
  143. try {
  144. await framework.provider.sendAndConfirm(applySystem.transaction);
  145. } catch (error) {
  146. expect(error.logs.join(" ")).to.contain(
  147. "Error Code: SystemNotApproved",
  148. );
  149. invalid = true;
  150. }
  151. expect(invalid).to.equal(true);
  152. });
  153. });
  154. }