|
@@ -0,0 +1,99 @@
|
|
|
+const anchor = require("@project-serum/anchor");
|
|
|
+const assert = require("assert");
|
|
|
+
|
|
|
+describe("chat", () => {
|
|
|
+ // Configure the client to use the local cluster.
|
|
|
+ anchor.setProvider(anchor.Provider.env());
|
|
|
+
|
|
|
+ // Program client handle.
|
|
|
+ const program = anchor.workspace.Chat;
|
|
|
+
|
|
|
+ // Chat room account.
|
|
|
+ const chatRoom = new anchor.web3.Account();
|
|
|
+
|
|
|
+ it("Creates a chat room", async () => {
|
|
|
+ // Add your test here.
|
|
|
+
|
|
|
+ await program.rpc.createChatRoom("Test Chat", {
|
|
|
+ accounts: {
|
|
|
+ chatRoom: chatRoom.publicKey,
|
|
|
+ rent: anchor.web3.SYSVAR_RENT_PUBKEY,
|
|
|
+ },
|
|
|
+ instructions: [
|
|
|
+ await program.account.chatRoom.createInstruction(chatRoom),
|
|
|
+ ],
|
|
|
+ signers: [chatRoom],
|
|
|
+ });
|
|
|
+
|
|
|
+ const chat = await program.account.chatRoom(chatRoom.publicKey);
|
|
|
+ const name = new TextDecoder("utf-8").decode(new Uint8Array(chat.name));
|
|
|
+ assert.ok(name.startsWith("Test Chat")); // [u8; 280] => trailing zeros.
|
|
|
+ assert.ok(chat.messages.length === 33607);
|
|
|
+ assert.ok(chat.head.toNumber() === 0);
|
|
|
+ assert.ok(chat.tail.toNumber() === 0);
|
|
|
+ });
|
|
|
+
|
|
|
+ it("Creates a user", async () => {
|
|
|
+ const authority = program.provider.wallet.publicKey;
|
|
|
+ await program.rpc.createUser("My User", {
|
|
|
+ accounts: {
|
|
|
+ user: await program.account.user.associatedAddress(authority),
|
|
|
+ authority,
|
|
|
+ rent: anchor.web3.SYSVAR_RENT_PUBKEY,
|
|
|
+ systemProgram: anchor.web3.SystemProgram.programId,
|
|
|
+ },
|
|
|
+ });
|
|
|
+ const account = await program.account.user.associated(authority);
|
|
|
+ assert.ok(account.name === "My User");
|
|
|
+ assert.ok(account.authority.equals(authority));
|
|
|
+ });
|
|
|
+
|
|
|
+ it("Sends messages", async () => {
|
|
|
+ const authority = program.provider.wallet.publicKey;
|
|
|
+ const user = await program.account.user.associatedAddress(authority);
|
|
|
+
|
|
|
+ // Only send a couple messages so the test doesn't take an eternity.
|
|
|
+ const numMessages = 10;
|
|
|
+
|
|
|
+ // Generate random message strings.
|
|
|
+ const messages = new Array(numMessages).fill("").map((msg) => {
|
|
|
+ return (
|
|
|
+ Math.random().toString(36).substring(2, 15) +
|
|
|
+ Math.random().toString(36).substring(2, 15)
|
|
|
+ );
|
|
|
+ });
|
|
|
+
|
|
|
+ // Send each message.
|
|
|
+ for (let k = 0; k < numMessages; k += 1) {
|
|
|
+ console.log("Sending message " + k);
|
|
|
+ await program.rpc.sendMessage(messages[k], {
|
|
|
+ accounts: {
|
|
|
+ user,
|
|
|
+ authority,
|
|
|
+ chatRoom: chatRoom.publicKey,
|
|
|
+ },
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ // Check the chat room state is as expected.
|
|
|
+ const chat = await program.account.chatRoom(chatRoom.publicKey);
|
|
|
+ const name = new TextDecoder("utf-8").decode(new Uint8Array(chat.name));
|
|
|
+ assert.ok(name.startsWith("Test Chat")); // [u8; 280] => trailing zeros.
|
|
|
+ assert.ok(chat.messages.length === 33607);
|
|
|
+ assert.ok(chat.head.toNumber() === numMessages);
|
|
|
+ assert.ok(chat.tail.toNumber() === 0);
|
|
|
+ chat.messages.forEach((msg, idx) => {
|
|
|
+ if (idx < 10) {
|
|
|
+ const data = new TextDecoder("utf-8").decode(new Uint8Array(msg.data));
|
|
|
+ console.log("Message", data);
|
|
|
+ assert.ok(msg.from.equals(user));
|
|
|
+ assert.ok(data.startsWith(messages[idx]));
|
|
|
+ } else {
|
|
|
+ assert.ok(new anchor.web3.PublicKey());
|
|
|
+ assert.ok(
|
|
|
+ JSON.stringify(msg.data) === JSON.stringify(new Array(280).fill(0))
|
|
|
+ );
|
|
|
+ }
|
|
|
+ });
|
|
|
+ });
|
|
|
+});
|