bolt.ts 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. import { Keypair, type PublicKey } from "@solana/web3.js";
  2. import { type Position } from "../target/types/position";
  3. import { type Velocity } from "../target/types/velocity";
  4. import { type BoltComponent } from "../target/types/bolt_component";
  5. import { type SystemSimpleMovement } from "../target/types/system_simple_movement";
  6. import { type World } from "../target/types/world";
  7. import { type SystemFly } from "../target/types/system_fly";
  8. import { type SystemApplyVelocity } from "../target/types/system_apply_velocity";
  9. import { expect } from "chai";
  10. import type BN from "bn.js";
  11. import {
  12. AddEntity,
  13. createInitializeRegistryInstruction,
  14. DELEGATION_PROGRAM_ID,
  15. FindRegistryPda,
  16. InitializeComponent,
  17. InitializeNewWorld,
  18. ApplySystem,
  19. DelegateComponent,
  20. AddAuthority,
  21. RemoveAuthority,
  22. ApproveSystem,
  23. RemoveSystem,
  24. anchor,
  25. type Program,
  26. web3,
  27. } from "../clients/bolt-sdk";
  28. enum Direction {
  29. Left = "Left",
  30. Right = "Right",
  31. Up = "Up",
  32. Down = "Down",
  33. }
  34. function padCenter(value: string, width: number) {
  35. const length = value.length;
  36. if (width <= length) {
  37. return value;
  38. }
  39. const padding = (width - length) / 2;
  40. const align = width - padding;
  41. return value.padStart(align, " ").padEnd(width, " ");
  42. }
  43. function logPosition(title: string, { x, y, z }: { x: BN; y: BN; z: BN }) {
  44. console.log(" +----------------------------------+");
  45. console.log(` | ${padCenter(title, 32)} |`);
  46. console.log(" +-----------------+----------------+");
  47. console.log(` | X Position | ${String(x).padEnd(14, " ")} |`);
  48. console.log(` | Y Position | ${String(y).padEnd(14, " ")} |`);
  49. console.log(` | Z Position | ${String(z).padEnd(14, " ")} |`);
  50. console.log(" +-----------------+----------------+");
  51. }
  52. function logVelocity(
  53. title: string,
  54. { x, y, z, lastApplied }: { x: BN; y: BN; z: BN; lastApplied: BN },
  55. ) {
  56. console.log(" +----------------------------------+");
  57. console.log(` | ${padCenter(title, 32)} |`);
  58. console.log(" +-----------------+----------------+");
  59. console.log(` | X Velocity | ${String(x).padEnd(14, " ")} |`);
  60. console.log(` | Y Velocity | ${String(y).padEnd(14, " ")} |`);
  61. console.log(` | Z Velocity | ${String(z).padEnd(14, " ")} |`);
  62. console.log(` | Last Applied | ${String(lastApplied).padEnd(14, " ")} |`);
  63. console.log(" +-----------------+----------------+");
  64. }
  65. describe("bolt", () => {
  66. const provider = anchor.AnchorProvider.env();
  67. anchor.setProvider(provider);
  68. const worldProgram = anchor.workspace.World as Program<World>;
  69. const boltComponentProgram = anchor.workspace
  70. .BoltComponent as Program<BoltComponent>;
  71. const exampleComponentPosition = anchor.workspace
  72. .Position as Program<Position>;
  73. const exampleComponentVelocity = anchor.workspace
  74. .Velocity as Program<Velocity>;
  75. const exampleSystemSimpleMovement = (
  76. anchor.workspace.SystemSimpleMovement as Program<SystemSimpleMovement>
  77. ).programId;
  78. const exampleSystemFly = (anchor.workspace.SystemFly as Program<SystemFly>)
  79. .programId;
  80. const exampleSystemApplyVelocity = (
  81. anchor.workspace.SystemApplyVelocity as Program<SystemApplyVelocity>
  82. ).programId;
  83. let worldPda: PublicKey;
  84. let entity1Pda: PublicKey;
  85. let entity2Pda: PublicKey;
  86. let entity4Pda: PublicKey;
  87. let entity5Pda: PublicKey;
  88. let componentPositionEntity1Pda: PublicKey;
  89. let componentVelocityEntity1Pda: PublicKey;
  90. let componentPositionEntity4Pda: PublicKey;
  91. let componentPositionEntity5Pda: PublicKey;
  92. const secondAuthority = Keypair.generate().publicKey;
  93. it("InitializeRegistry", async () => {
  94. const registryPda = FindRegistryPda({});
  95. const initializeRegistryIx = createInitializeRegistryInstruction({
  96. registry: registryPda,
  97. payer: provider.wallet.publicKey,
  98. });
  99. const tx = new anchor.web3.Transaction().add(initializeRegistryIx);
  100. await provider.sendAndConfirm(tx);
  101. });
  102. it("InitializeNewWorld", async () => {
  103. const initializeNewWorld = await InitializeNewWorld({
  104. payer: provider.wallet.publicKey,
  105. connection: provider.connection,
  106. });
  107. const signature = await provider.sendAndConfirm(
  108. initializeNewWorld.transaction,
  109. );
  110. console.log("InitializeNewWorld signature: ", signature);
  111. worldPda = initializeNewWorld.worldPda; // Saved for later
  112. });
  113. it("Add authority", async () => {
  114. const addAuthority = await AddAuthority({
  115. authority: provider.wallet.publicKey,
  116. newAuthority: provider.wallet.publicKey,
  117. world: worldPda,
  118. connection: provider.connection,
  119. });
  120. await provider.sendAndConfirm(addAuthority.transaction, [], {
  121. skipPreflight: true,
  122. });
  123. const worldAccount = await worldProgram.account.world.fetch(worldPda);
  124. expect(
  125. worldAccount.authorities.some((auth) =>
  126. auth.equals(provider.wallet.publicKey),
  127. ),
  128. );
  129. });
  130. it("Add a second authority", async () => {
  131. const addAuthority = await AddAuthority({
  132. authority: provider.wallet.publicKey,
  133. newAuthority: secondAuthority,
  134. world: worldPda,
  135. connection: provider.connection,
  136. });
  137. const signature = await provider.sendAndConfirm(addAuthority.transaction);
  138. console.log(`Add Authority signature: ${signature}`);
  139. const worldAccount = await worldProgram.account.world.fetch(worldPda);
  140. expect(
  141. worldAccount.authorities.some((auth) => auth.equals(secondAuthority)),
  142. );
  143. });
  144. it("Remove an authority", async () => {
  145. const addAuthority = await RemoveAuthority({
  146. authority: provider.wallet.publicKey,
  147. authorityToDelete: secondAuthority,
  148. world: worldPda,
  149. connection: provider.connection,
  150. });
  151. const signature = await provider.sendAndConfirm(addAuthority.transaction);
  152. console.log(`Add Authority signature: ${signature}`);
  153. const worldAccount = await worldProgram.account.world.fetch(worldPda);
  154. expect(
  155. !worldAccount.authorities.some((auth) => auth.equals(secondAuthority)),
  156. );
  157. });
  158. it("InitializeNewWorld 2", async () => {
  159. const initializeNewWorld = await InitializeNewWorld({
  160. payer: provider.wallet.publicKey,
  161. connection: provider.connection,
  162. });
  163. await provider.sendAndConfirm(initializeNewWorld.transaction);
  164. });
  165. it("Add entity 1", async () => {
  166. const addEntity = await AddEntity({
  167. payer: provider.wallet.publicKey,
  168. world: worldPda,
  169. connection: provider.connection,
  170. });
  171. await provider.sendAndConfirm(addEntity.transaction);
  172. entity1Pda = addEntity.entityPda; // Saved for later
  173. });
  174. it("Add entity 2", async () => {
  175. const addEntity = await AddEntity({
  176. payer: provider.wallet.publicKey,
  177. world: worldPda,
  178. connection: provider.connection,
  179. });
  180. await provider.sendAndConfirm(addEntity.transaction);
  181. entity2Pda = addEntity.entityPda; // Saved for later
  182. });
  183. it("Add entity 3", async () => {
  184. const addEntity = await AddEntity({
  185. payer: provider.wallet.publicKey,
  186. world: worldPda,
  187. connection: provider.connection,
  188. });
  189. await provider.sendAndConfirm(addEntity.transaction);
  190. });
  191. it("Add entity 4 (with seed)", async () => {
  192. const addEntity = await AddEntity({
  193. payer: provider.wallet.publicKey,
  194. world: worldPda,
  195. seed: Buffer.from("extra-seed"),
  196. connection: provider.connection,
  197. });
  198. await provider.sendAndConfirm(addEntity.transaction);
  199. entity4Pda = addEntity.entityPda;
  200. });
  201. it("Add entity 5", async () => {
  202. const addEntity = await AddEntity({
  203. payer: provider.wallet.publicKey,
  204. world: worldPda,
  205. connection: provider.connection,
  206. });
  207. await provider.sendAndConfirm(addEntity.transaction);
  208. entity5Pda = addEntity.entityPda; // Saved for later
  209. });
  210. it("Initialize Original Component on Entity 1, trough the world instance", async () => {
  211. const initializeComponent = await InitializeComponent({
  212. payer: provider.wallet.publicKey,
  213. entity: entity1Pda,
  214. seed: "origin-component",
  215. componentId: boltComponentProgram.programId,
  216. });
  217. await provider.sendAndConfirm(initializeComponent.transaction);
  218. });
  219. it("Initialize Original Component on Entity 2, trough the world instance", async () => {
  220. const initializeComponent = await InitializeComponent({
  221. payer: provider.wallet.publicKey,
  222. entity: entity2Pda,
  223. seed: "origin-component",
  224. componentId: boltComponentProgram.programId,
  225. });
  226. await provider.sendAndConfirm(initializeComponent.transaction);
  227. });
  228. it("Initialize Position Component on Entity 1", async () => {
  229. const initializeComponent = await InitializeComponent({
  230. payer: provider.wallet.publicKey,
  231. entity: entity1Pda,
  232. componentId: exampleComponentPosition.programId,
  233. });
  234. await provider.sendAndConfirm(initializeComponent.transaction);
  235. componentPositionEntity1Pda = initializeComponent.componentPda; // Saved for later
  236. });
  237. it("Initialize Velocity Component on Entity 1 (with seed)", async () => {
  238. const initializeComponent = await InitializeComponent({
  239. payer: provider.wallet.publicKey,
  240. entity: entity1Pda,
  241. componentId: exampleComponentVelocity.programId,
  242. seed: "component-velocity",
  243. });
  244. await provider.sendAndConfirm(initializeComponent.transaction);
  245. componentVelocityEntity1Pda = initializeComponent.componentPda; // Saved for later
  246. });
  247. it("Initialize Position Component on Entity 2", async () => {
  248. const initializeComponent = await InitializeComponent({
  249. payer: provider.wallet.publicKey,
  250. entity: entity2Pda,
  251. componentId: exampleComponentPosition.programId,
  252. });
  253. await provider.sendAndConfirm(initializeComponent.transaction);
  254. });
  255. it("Initialize Position Component on Entity 4", async () => {
  256. const initializeComponent = await InitializeComponent({
  257. payer: provider.wallet.publicKey,
  258. entity: entity4Pda,
  259. componentId: exampleComponentPosition.programId,
  260. });
  261. await provider.sendAndConfirm(initializeComponent.transaction);
  262. componentPositionEntity4Pda = initializeComponent.componentPda; // Saved for later
  263. });
  264. it("Initialize Position Component on Entity 5 (with authority)", async () => {
  265. const initializeComponent = await InitializeComponent({
  266. payer: provider.wallet.publicKey,
  267. entity: entity5Pda,
  268. componentId: exampleComponentPosition.programId,
  269. authority: provider.wallet.publicKey,
  270. });
  271. await provider.sendAndConfirm(initializeComponent.transaction);
  272. componentPositionEntity5Pda = initializeComponent.componentPda; // Saved for later
  273. });
  274. it("Check Position on Entity 1 is default", async () => {
  275. const position = await exampleComponentPosition.account.position.fetch(
  276. componentPositionEntity1Pda,
  277. );
  278. logPosition("Default State: Entity 1", position);
  279. expect(position.x.toNumber()).to.equal(0);
  280. expect(position.y.toNumber()).to.equal(0);
  281. expect(position.z.toNumber()).to.equal(0);
  282. });
  283. it("Apply Simple Movement System (Up) on Entity 1", async () => {
  284. const applySystem = await ApplySystem({
  285. authority: provider.wallet.publicKey,
  286. systemId: exampleSystemSimpleMovement,
  287. world: worldPda,
  288. entities: [
  289. {
  290. entity: entity1Pda,
  291. components: [{ componentId: exampleComponentPosition.programId }],
  292. },
  293. ],
  294. args: {
  295. direction: Direction.Up,
  296. },
  297. });
  298. const signature = await provider.sendAndConfirm(
  299. applySystem.transaction,
  300. [],
  301. { skipPreflight: true },
  302. );
  303. console.log(`Signature: ${signature}`);
  304. const position = await exampleComponentPosition.account.position.fetch(
  305. componentPositionEntity1Pda,
  306. );
  307. logPosition("Movement System: Entity 1", position);
  308. expect(position.x.toNumber()).to.equal(0);
  309. expect(position.y.toNumber()).to.equal(1);
  310. expect(position.z.toNumber()).to.equal(0);
  311. });
  312. it("Apply Simple Movement System (Right) on Entity 1", async () => {
  313. const applySystem = await ApplySystem({
  314. authority: provider.wallet.publicKey,
  315. systemId: exampleSystemSimpleMovement,
  316. world: worldPda,
  317. entities: [
  318. {
  319. entity: entity1Pda,
  320. components: [{ componentId: exampleComponentPosition.programId }],
  321. },
  322. ],
  323. args: {
  324. direction: Direction.Right,
  325. },
  326. });
  327. await provider.sendAndConfirm(applySystem.transaction);
  328. const position = await exampleComponentPosition.account.position.fetch(
  329. componentPositionEntity1Pda,
  330. );
  331. logPosition("Movement System: Entity 1", position);
  332. expect(position.x.toNumber()).to.equal(1);
  333. expect(position.y.toNumber()).to.equal(1);
  334. expect(position.z.toNumber()).to.equal(0);
  335. });
  336. it("Apply Fly System on Entity 1", async () => {
  337. const applySystem = await ApplySystem({
  338. authority: provider.wallet.publicKey,
  339. systemId: exampleSystemFly,
  340. world: worldPda,
  341. entities: [
  342. {
  343. entity: entity1Pda,
  344. components: [{ componentId: exampleComponentPosition.programId }],
  345. },
  346. ],
  347. });
  348. await provider.sendAndConfirm(applySystem.transaction);
  349. const position = await exampleComponentPosition.account.position.fetch(
  350. componentPositionEntity1Pda,
  351. );
  352. logPosition("Fly System: Entity 1", position);
  353. expect(position.x.toNumber()).to.equal(1);
  354. expect(position.y.toNumber()).to.equal(1);
  355. expect(position.z.toNumber()).to.equal(1);
  356. });
  357. it("Apply System Velocity on Entity 1", async () => {
  358. const applySystem = await ApplySystem({
  359. authority: provider.wallet.publicKey,
  360. systemId: exampleSystemApplyVelocity,
  361. world: worldPda,
  362. entities: [
  363. {
  364. entity: entity1Pda,
  365. components: [
  366. {
  367. componentId: exampleComponentVelocity.programId,
  368. seed: "component-velocity",
  369. },
  370. { componentId: exampleComponentPosition.programId },
  371. ],
  372. },
  373. ],
  374. });
  375. await provider.sendAndConfirm(applySystem.transaction);
  376. const velocity = await exampleComponentVelocity.account.velocity.fetch(
  377. componentVelocityEntity1Pda,
  378. );
  379. logVelocity("Apply System Velocity: Entity 1", velocity);
  380. expect(velocity.x.toNumber()).to.equal(10);
  381. expect(velocity.y.toNumber()).to.equal(0);
  382. expect(velocity.z.toNumber()).to.equal(0);
  383. expect(velocity.lastApplied.toNumber()).to.not.equal(0);
  384. const position = await exampleComponentPosition.account.position.fetch(
  385. componentPositionEntity1Pda,
  386. );
  387. logPosition("Apply System Velocity: Entity 1", position);
  388. expect(position.x.toNumber()).to.greaterThan(1);
  389. expect(position.y.toNumber()).to.equal(1);
  390. expect(position.z.toNumber()).to.equal(1);
  391. });
  392. it("Apply System Velocity on Entity 1, with Clock external account", async () => {
  393. const applySystem = await ApplySystem({
  394. authority: provider.wallet.publicKey,
  395. systemId: exampleSystemApplyVelocity,
  396. world: worldPda,
  397. entities: [
  398. {
  399. entity: entity1Pda,
  400. components: [
  401. {
  402. componentId: exampleComponentVelocity.programId,
  403. seed: "component-velocity",
  404. },
  405. { componentId: exampleComponentPosition.programId },
  406. ],
  407. },
  408. ],
  409. extraAccounts: [
  410. {
  411. pubkey: new web3.PublicKey(
  412. "SysvarC1ock11111111111111111111111111111111",
  413. ),
  414. isWritable: false,
  415. isSigner: false,
  416. },
  417. ],
  418. });
  419. await provider.sendAndConfirm(applySystem.transaction);
  420. const position = await exampleComponentPosition.account.position.fetch(
  421. componentPositionEntity1Pda,
  422. );
  423. logPosition("Apply System Velocity: Entity 1", position);
  424. expect(position.x.toNumber()).to.greaterThan(1);
  425. expect(position.y.toNumber()).to.equal(1);
  426. expect(position.z.toNumber()).to.equal(300);
  427. });
  428. it("Apply Fly System on Entity 4", async () => {
  429. const applySystem = await ApplySystem({
  430. authority: provider.wallet.publicKey,
  431. systemId: exampleSystemFly,
  432. world: worldPda,
  433. entities: [
  434. {
  435. entity: entity4Pda,
  436. components: [{ componentId: exampleComponentPosition.programId }],
  437. },
  438. ],
  439. });
  440. await provider.sendAndConfirm(applySystem.transaction);
  441. const position = await exampleComponentPosition.account.position.fetch(
  442. componentPositionEntity4Pda,
  443. );
  444. logPosition("Fly System: Entity 4", position);
  445. expect(position.x.toNumber()).to.equal(0);
  446. expect(position.y.toNumber()).to.equal(0);
  447. expect(position.z.toNumber()).to.equal(1);
  448. });
  449. it("Apply Fly System on Entity 5 (should fail with wrong authority)", async () => {
  450. const positionBefore =
  451. await exampleComponentPosition.account.position.fetch(
  452. componentPositionEntity5Pda,
  453. );
  454. const applySystem = await ApplySystem({
  455. authority: provider.wallet.publicKey,
  456. systemId: exampleSystemFly,
  457. world: worldPda,
  458. entities: [
  459. {
  460. entity: entity5Pda,
  461. components: [{ componentId: exampleComponentPosition.programId }],
  462. },
  463. ],
  464. });
  465. let failed = false;
  466. try {
  467. await provider.sendAndConfirm(applySystem.transaction);
  468. } catch (error) {
  469. failed = true;
  470. // console.log("error", error);
  471. expect(error.logs.join("\n")).to.contain("Error Code: InvalidAuthority");
  472. }
  473. expect(failed).to.equal(true);
  474. const positionAfter = await exampleComponentPosition.account.position.fetch(
  475. componentPositionEntity5Pda,
  476. );
  477. expect(positionBefore.x.toNumber()).to.equal(positionAfter.x.toNumber());
  478. expect(positionBefore.y.toNumber()).to.equal(positionAfter.y.toNumber());
  479. expect(positionBefore.z.toNumber()).to.equal(positionAfter.z.toNumber());
  480. });
  481. it("Whitelist System", async () => {
  482. const approveSystem = await ApproveSystem({
  483. authority: provider.wallet.publicKey,
  484. systemToApprove: exampleSystemFly,
  485. world: worldPda,
  486. });
  487. const signature = await provider.sendAndConfirm(
  488. approveSystem.transaction,
  489. [],
  490. { skipPreflight: true },
  491. );
  492. console.log(`Whitelist 2 system approval signature: ${signature}`);
  493. // Get World and check permissionless and systems
  494. const worldAccount = await worldProgram.account.world.fetch(worldPda);
  495. expect(worldAccount.permissionless).to.equal(false);
  496. expect(worldAccount.systems.length).to.be.greaterThan(0);
  497. });
  498. it("Whitelist System 2", async () => {
  499. const approveSystem = await ApproveSystem({
  500. authority: provider.wallet.publicKey,
  501. systemToApprove: exampleSystemApplyVelocity,
  502. world: worldPda,
  503. });
  504. const signature = await provider.sendAndConfirm(
  505. approveSystem.transaction,
  506. [],
  507. { skipPreflight: true },
  508. );
  509. console.log(`Whitelist 2 system approval signature: ${signature}`);
  510. // Get World and check permissionless and systems
  511. const worldAccount = await worldProgram.account.world.fetch(worldPda);
  512. expect(worldAccount.permissionless).to.equal(false);
  513. expect(worldAccount.systems.length).to.be.greaterThan(0);
  514. });
  515. it("Apply Fly System on Entity 1", async () => {
  516. const applySystem = await ApplySystem({
  517. authority: provider.wallet.publicKey,
  518. systemId: exampleSystemFly,
  519. world: worldPda,
  520. entities: [
  521. {
  522. entity: entity1Pda,
  523. components: [{ componentId: exampleComponentPosition.programId }],
  524. },
  525. ],
  526. });
  527. await provider.sendAndConfirm(applySystem.transaction);
  528. });
  529. it("Remove System 1", async () => {
  530. const approveSystem = await RemoveSystem({
  531. authority: provider.wallet.publicKey,
  532. systemToRemove: exampleSystemFly,
  533. world: worldPda,
  534. });
  535. const signature = await provider.sendAndConfirm(
  536. approveSystem.transaction,
  537. [],
  538. { skipPreflight: true },
  539. );
  540. console.log(`Whitelist 2 system approval signature: ${signature}`);
  541. // Get World and check permissionless and systems
  542. const worldAccount = await worldProgram.account.world.fetch(worldPda);
  543. expect(worldAccount.permissionless).to.equal(false);
  544. expect(worldAccount.systems.length).to.be.greaterThan(0);
  545. });
  546. it("Apply Invalid Fly System on Entity 1", async () => {
  547. const applySystem = await ApplySystem({
  548. authority: provider.wallet.publicKey,
  549. systemId: exampleSystemFly,
  550. world: worldPda,
  551. entities: [
  552. {
  553. entity: entity1Pda,
  554. components: [{ componentId: exampleComponentPosition.programId }],
  555. },
  556. ],
  557. });
  558. let invalid = false;
  559. try {
  560. await provider.sendAndConfirm(applySystem.transaction);
  561. } catch (error) {
  562. expect(error.logs.join(" ")).to.contain("Error Code: SystemNotApproved");
  563. invalid = true;
  564. }
  565. expect(invalid).to.equal(true);
  566. });
  567. it("Check invalid component init without CPI", async () => {
  568. let invalid = false;
  569. try {
  570. await exampleComponentPosition.methods
  571. .initialize()
  572. .accounts({
  573. payer: provider.wallet.publicKey,
  574. data: componentPositionEntity5Pda,
  575. entity: entity5Pda,
  576. authority: provider.wallet.publicKey,
  577. })
  578. .rpc();
  579. } catch (error) {
  580. // console.log("error", error);
  581. expect(error.message).to.contain("Error Code: InvalidCaller");
  582. invalid = true;
  583. }
  584. expect(invalid).to.equal(true);
  585. });
  586. it("Check invalid component update without CPI", async () => {
  587. let invalid = false;
  588. try {
  589. await boltComponentProgram.methods
  590. .update(Buffer.from(""))
  591. .accounts({
  592. boltComponent: componentPositionEntity4Pda,
  593. authority: provider.wallet.publicKey,
  594. })
  595. .rpc();
  596. } catch (error) {
  597. // console.log("error", error);
  598. expect(error.message).to.contain(
  599. "bolt_component. Error Code: AccountOwnedByWrongProgram",
  600. );
  601. invalid = true;
  602. }
  603. expect(invalid).to.equal(true);
  604. });
  605. it("Check component delegation", async () => {
  606. const delegateComponent = await DelegateComponent({
  607. payer: provider.wallet.publicKey,
  608. entity: entity1Pda,
  609. componentId: exampleComponentPosition.programId,
  610. });
  611. const txSign = await provider.sendAndConfirm(
  612. delegateComponent.transaction,
  613. [],
  614. { skipPreflight: true, commitment: "confirmed" },
  615. );
  616. console.log(`Delegation signature: ${txSign}`);
  617. const acc = await provider.connection.getAccountInfo(
  618. delegateComponent.componentPda,
  619. );
  620. expect(acc?.owner.toString()).to.equal(DELEGATION_PROGRAM_ID);
  621. });
  622. });