test.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import {
  2. PublicKey,
  3. Transaction,
  4. TransactionInstruction,
  5. } from '@solana/web3.js';
  6. import { start } from 'solana-bankrun';
  7. import { describe, test } from 'node:test';
  8. import { assert } from "chai";
  9. describe('hello-solana', async () => {
  10. // load program in solana-bankrun
  11. const PROGRAM_ID = PublicKey.unique();
  12. const context = await start([{ name: 'hello_solana_program', programId: PROGRAM_ID }],[]);
  13. const client = context.banksClient;
  14. const payer = context.payer;
  15. test('Say hello!', async () => {
  16. const blockhash = context.lastBlockhash;
  17. // We set up our instruction first.
  18. let ix = new TransactionInstruction({
  19. keys: [{ pubkey: payer.publicKey, isSigner: true, isWritable: true }],
  20. programId: PROGRAM_ID,
  21. data: Buffer.alloc(0), // No data
  22. });
  23. const tx = new Transaction();
  24. tx.recentBlockhash = blockhash;
  25. tx.add(ix).sign(payer);
  26. // Now we process the transaction
  27. let transaction = await client.processTransaction(tx);
  28. assert(transaction.logMessages[0].startsWith("Program " + PROGRAM_ID));
  29. assert(transaction.logMessages[1] === "Program log: Hello, Solana!");
  30. assert(transaction.logMessages[2] === "Program log: Our program's Program ID: " + PROGRAM_ID);
  31. assert(transaction.logMessages[3].startsWith("Program " + PROGRAM_ID + " consumed"));
  32. assert(transaction.logMessages[4] === "Program " + PROGRAM_ID + " success");
  33. assert(transaction.logMessages.length == 5);
  34. });
  35. });