Skip to content

Commit ed5f01a

Browse files
committed
chore: add tests for chat-room and counter (#797)
1 parent 51d6a60 commit ed5f01a

File tree

12 files changed

+108
-7
lines changed

12 files changed

+108
-7
lines changed

docs/platforms/cloudflare-workers.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ The Cloudflare Durable Objects platform leverages Cloudflare Workers to run Acto
4848
In a separate terminal, run the auto-generated test client:
4949

5050
```sh
51-
npx tsx tests/client.ts
51+
npx tsx scripts/connect.ts
5252
# Outputs:
5353
# Event: 5
5454
# Action: 5

docs/platforms/nodejs.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import CreateActorCli from '/snippets/create-actor-cli.mdx';
3939
In a separate terminal, run the auto-generated test client:
4040

4141
```sh
42-
npx tsx tests/client.ts
42+
npx tsx scripts/connect.ts
4343
# Outputs:
4444
# Event: 5
4545
# RPC: 5

docs/platforms/rivet.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ import CreateActorCli from '/snippets/create-actor-cli.mdx';
7171
Then run the client:
7272

7373
```sh
74-
npx tsx tests/client.ts
74+
npx tsx scripts/connect.ts
7575
# Outputs:
7676
# Event: 5
7777
# Action: 5

examples/chat-room/package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,17 @@
44
"private": true,
55
"type": "module",
66
"scripts": {
7-
"check-types": "tsc --noEmit"
7+
"check-types": "tsc --noEmit",
8+
"test": "vitest run"
89
},
910
"devDependencies": {
1011
"@types/node": "^22.13.9",
1112
"@types/prompts": "^2",
1213
"actor-core": "workspace:*",
1314
"prompts": "^2.4.2",
1415
"tsx": "^3.12.7",
15-
"typescript": "^5.5.2"
16+
"typescript": "^5.5.2",
17+
"vitest": "^3.0.9"
1618
},
1719
"example": {
1820
"platforms": [
File renamed without changes.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { test, expect } from "vitest";
2+
import { setupTest } from "actor-core/test";
3+
import { app } from "../src/index";
4+
5+
test("chat room should handle messages", async () => {
6+
const { client } = await setupTest(app);
7+
8+
// Connect to chat room
9+
const chatRoom = await client.chatRoom.get();
10+
11+
// Initial history should be empty
12+
const initialMessages = await chatRoom.getHistory();
13+
expect(initialMessages).toEqual([]);
14+
15+
// Test event emission
16+
let receivedUsername = "";
17+
let receivedMessage = "";
18+
chatRoom.on("newMessage", (username: string, message: string) => {
19+
receivedUsername = username;
20+
receivedMessage = message;
21+
});
22+
23+
// Send a message
24+
const testUser = "william";
25+
const testMessage = "All the world's a stage.";
26+
await chatRoom.sendMessage(testUser, testMessage);
27+
28+
// Verify event was emitted with correct data
29+
expect(receivedUsername).toBe(testUser);
30+
expect(receivedMessage).toBe(testMessage);
31+
32+
// Verify message was stored in history
33+
const updatedMessages = await chatRoom.getHistory();
34+
expect(updatedMessages).toEqual([
35+
{ username: testUser, message: testMessage }
36+
]);
37+
38+
// Send multiple messages and verify
39+
const users = ["romeo", "juliet", "othello"];
40+
const messages = ["Wherefore art thou?", "Here I am!", "The green-eyed monster."];
41+
42+
for (let i = 0; i < users.length; i++) {
43+
await chatRoom.sendMessage(users[i], messages[i]);
44+
45+
// Verify event emission
46+
expect(receivedUsername).toBe(users[i]);
47+
expect(receivedMessage).toBe(messages[i]);
48+
}
49+
50+
// Verify all messages are in history in correct order
51+
const finalHistory = await chatRoom.getHistory();
52+
expect(finalHistory).toEqual([
53+
{ username: testUser, message: testMessage },
54+
...users.map((username, i) => ({ username, message: messages[i] }))
55+
]);
56+
});

examples/counter/package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44
"private": true,
55
"type": "module",
66
"scripts": {
7-
"check-types": "tsc --noEmit"
7+
"check-types": "tsc --noEmit",
8+
"test": "vitest run"
89
},
910
"devDependencies": {
1011
"@types/node": "^22.13.9",
1112
"actor-core": "workspace:*",
1213
"tsx": "^3.12.7",
13-
"typescript": "^5.7.3"
14+
"typescript": "^5.7.3",
15+
"vitest": "^3.0.9"
1416
},
1517
"example": {
1618
"platforms": [

examples/counter/src/counter.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ const counter = actor({
88
c.broadcast("newCount", c.state.count);
99
return c.state.count;
1010
},
11+
getCount: (c) => {
12+
return c.state.count;
13+
},
1114
},
1215
});
1316

0 commit comments

Comments
 (0)