|
| 1 | +import { describe, expect, test } from "vitest"; |
| 2 | +import { chat } from "~encore/clients"; |
| 3 | + |
| 4 | +describe("chat", () => { |
| 5 | + test("should get back own message with correct parameters", async () => { |
| 6 | + // Connect to the chat server |
| 7 | + const stream = await chat.chat({ id: "user-id" }); |
| 8 | + |
| 9 | + // Send a message to the server |
| 10 | + await stream.send({ username: "foo", msg: "hello" }); |
| 11 | + |
| 12 | + // Receive a message from the server |
| 13 | + const { msg, userID, username } = await stream.recv(); |
| 14 | + expect(userID).toBe("user-id"); |
| 15 | + expect(msg).toBe("hello"); |
| 16 | + expect(username).toBe("foo"); |
| 17 | + }); |
| 18 | + |
| 19 | + test("should get other users message", async () => { |
| 20 | + // Connect clients to the chat server |
| 21 | + const stream1 = await chat.chat({ id: "user-1" }); |
| 22 | + const stream2 = await chat.chat({ id: "user-2" }); |
| 23 | + const stream3 = await chat.chat({ id: "user-3" }); |
| 24 | + |
| 25 | + // Send a message from one client |
| 26 | + await stream1.send({ username: "foo", msg: "hello" }); |
| 27 | + |
| 28 | + // Receive the message from the other clients |
| 29 | + const stream2Results = await stream2.recv(); |
| 30 | + const stream3Results = await stream3.recv(); |
| 31 | + |
| 32 | + expect(stream2Results.userID).toBe("user-1"); |
| 33 | + expect(stream3Results.userID).toBe("user-1"); |
| 34 | + }); |
| 35 | +}); |
0 commit comments