chat.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. const anchor = require("@project-serum/anchor");
  2. const assert = require("assert");
  3. const { PublicKey } = anchor.web3;
  4. describe("chat", () => {
  5. // Configure the client to use the local cluster.
  6. anchor.setProvider(anchor.Provider.env());
  7. // Program client handle.
  8. const program = anchor.workspace.Chat;
  9. // Chat room account.
  10. const chatRoom = anchor.web3.Keypair.generate();
  11. it("Creates a chat room", async () => {
  12. await program.rpc.createChatRoom("Test Chat", {
  13. accounts: {
  14. chatRoom: chatRoom.publicKey,
  15. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  16. },
  17. instructions: [
  18. await program.account.chatRoom.createInstruction(chatRoom),
  19. ],
  20. signers: [chatRoom],
  21. });
  22. const chat = await program.account.chatRoom.fetch(chatRoom.publicKey);
  23. const name = new TextDecoder("utf-8").decode(new Uint8Array(chat.name));
  24. assert.ok(name.startsWith("Test Chat")); // [u8; 280] => trailing zeros.
  25. assert.ok(chat.messages.length === 33607);
  26. assert.ok(chat.head.toNumber() === 0);
  27. assert.ok(chat.tail.toNumber() === 0);
  28. });
  29. it("Creates a user", async () => {
  30. const authority = program.provider.wallet.publicKey;
  31. const [user, bump] = await PublicKey.findProgramAddress(
  32. [authority.toBuffer()],
  33. program.programId
  34. );
  35. await program.rpc.createUser("My User", bump, {
  36. accounts: {
  37. user,
  38. authority,
  39. systemProgram: anchor.web3.SystemProgram.programId,
  40. },
  41. });
  42. const account = await program.account.user.fetch(user);
  43. assert.ok(account.name === "My User");
  44. assert.ok(account.authority.equals(authority));
  45. });
  46. it("Sends messages", async () => {
  47. const authority = program.provider.wallet.publicKey;
  48. const user = (
  49. await PublicKey.findProgramAddress(
  50. [authority.toBuffer()],
  51. program.programId
  52. )
  53. )[0];
  54. // Only send a couple messages so the test doesn't take an eternity.
  55. const numMessages = 10;
  56. // Generate random message strings.
  57. const messages = new Array(numMessages).fill("").map((msg) => {
  58. return (
  59. Math.random().toString(36).substring(2, 15) +
  60. Math.random().toString(36).substring(2, 15)
  61. );
  62. });
  63. // Send each message.
  64. for (let k = 0; k < numMessages; k += 1) {
  65. console.log("Sending message " + k);
  66. await program.rpc.sendMessage(messages[k], {
  67. accounts: {
  68. user,
  69. authority,
  70. chatRoom: chatRoom.publicKey,
  71. },
  72. });
  73. }
  74. // Check the chat room state is as expected.
  75. const chat = await program.account.chatRoom.fetch(chatRoom.publicKey);
  76. const name = new TextDecoder("utf-8").decode(new Uint8Array(chat.name));
  77. assert.ok(name.startsWith("Test Chat")); // [u8; 280] => trailing zeros.
  78. assert.ok(chat.messages.length === 33607);
  79. assert.ok(chat.head.toNumber() === numMessages);
  80. assert.ok(chat.tail.toNumber() === 0);
  81. chat.messages.forEach((msg, idx) => {
  82. if (idx < 10) {
  83. const data = new TextDecoder("utf-8").decode(new Uint8Array(msg.data));
  84. console.log("Message", data);
  85. assert.ok(msg.from.equals(user));
  86. assert.ok(data.startsWith(messages[idx]));
  87. } else {
  88. assert.ok(anchor.web3.PublicKey.default);
  89. assert.ok(
  90. JSON.stringify(msg.data) === JSON.stringify(new Array(280).fill(0))
  91. );
  92. }
  93. });
  94. });
  95. });