chat.js 3.2 KB

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