rivetkit 2.0.2 → 2.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (253) hide show
  1. package/dist/schemas/actor-persist/v1.ts +228 -0
  2. package/dist/schemas/client-protocol/v1.ts +429 -0
  3. package/dist/schemas/file-system-driver/v1.ts +102 -0
  4. package/dist/tsup/actor/errors.cjs +69 -0
  5. package/dist/tsup/actor/errors.cjs.map +1 -0
  6. package/dist/tsup/actor/errors.d.cts +143 -0
  7. package/dist/tsup/actor/errors.d.ts +143 -0
  8. package/dist/tsup/actor/errors.js +69 -0
  9. package/dist/tsup/actor/errors.js.map +1 -0
  10. package/dist/tsup/chunk-2CRLFV6Z.cjs +202 -0
  11. package/dist/tsup/chunk-2CRLFV6Z.cjs.map +1 -0
  12. package/dist/tsup/chunk-3H7O2A7I.js +525 -0
  13. package/dist/tsup/chunk-3H7O2A7I.js.map +1 -0
  14. package/dist/tsup/chunk-42I3OZ3Q.js +15 -0
  15. package/dist/tsup/chunk-42I3OZ3Q.js.map +1 -0
  16. package/dist/tsup/chunk-4NSUQZ2H.js +1790 -0
  17. package/dist/tsup/chunk-4NSUQZ2H.js.map +1 -0
  18. package/dist/tsup/chunk-6PDXBYI5.js +132 -0
  19. package/dist/tsup/chunk-6PDXBYI5.js.map +1 -0
  20. package/dist/tsup/chunk-6WKQDDUD.cjs +1790 -0
  21. package/dist/tsup/chunk-6WKQDDUD.cjs.map +1 -0
  22. package/dist/tsup/chunk-CTBOSFUH.cjs +116 -0
  23. package/dist/tsup/chunk-CTBOSFUH.cjs.map +1 -0
  24. package/dist/tsup/chunk-EGVZZFE2.js +2857 -0
  25. package/dist/tsup/chunk-EGVZZFE2.js.map +1 -0
  26. package/dist/tsup/chunk-FCCPJNMA.cjs +132 -0
  27. package/dist/tsup/chunk-FCCPJNMA.cjs.map +1 -0
  28. package/dist/tsup/chunk-FLMTTN27.js +244 -0
  29. package/dist/tsup/chunk-FLMTTN27.js.map +1 -0
  30. package/dist/tsup/chunk-GIR3AFFI.cjs +315 -0
  31. package/dist/tsup/chunk-GIR3AFFI.cjs.map +1 -0
  32. package/dist/tsup/chunk-INGJP237.js +315 -0
  33. package/dist/tsup/chunk-INGJP237.js.map +1 -0
  34. package/dist/tsup/chunk-KJCJLKRM.js +116 -0
  35. package/dist/tsup/chunk-KJCJLKRM.js.map +1 -0
  36. package/dist/tsup/chunk-KUPQZYUQ.cjs +15 -0
  37. package/dist/tsup/chunk-KUPQZYUQ.cjs.map +1 -0
  38. package/dist/tsup/chunk-O2MBYIXO.cjs +2857 -0
  39. package/dist/tsup/chunk-O2MBYIXO.cjs.map +1 -0
  40. package/dist/tsup/chunk-OGAPU3UG.cjs +525 -0
  41. package/dist/tsup/chunk-OGAPU3UG.cjs.map +1 -0
  42. package/dist/tsup/chunk-OV6AYD4S.js +4406 -0
  43. package/dist/tsup/chunk-OV6AYD4S.js.map +1 -0
  44. package/dist/tsup/chunk-PO4VLDWA.js +47 -0
  45. package/dist/tsup/chunk-PO4VLDWA.js.map +1 -0
  46. package/dist/tsup/chunk-R2OPSKIV.cjs +244 -0
  47. package/dist/tsup/chunk-R2OPSKIV.cjs.map +1 -0
  48. package/dist/tsup/chunk-TZJKSBUQ.cjs +47 -0
  49. package/dist/tsup/chunk-TZJKSBUQ.cjs.map +1 -0
  50. package/dist/tsup/chunk-UBUC5C3G.cjs +189 -0
  51. package/dist/tsup/chunk-UBUC5C3G.cjs.map +1 -0
  52. package/dist/tsup/chunk-UIM22YJL.cjs +4406 -0
  53. package/dist/tsup/chunk-UIM22YJL.cjs.map +1 -0
  54. package/dist/tsup/chunk-URVFQMYI.cjs +230 -0
  55. package/dist/tsup/chunk-URVFQMYI.cjs.map +1 -0
  56. package/dist/tsup/chunk-UVUPOS46.js +230 -0
  57. package/dist/tsup/chunk-UVUPOS46.js.map +1 -0
  58. package/dist/tsup/chunk-VRRHBNJC.js +189 -0
  59. package/dist/tsup/chunk-VRRHBNJC.js.map +1 -0
  60. package/dist/tsup/chunk-XFSS33EQ.js +202 -0
  61. package/dist/tsup/chunk-XFSS33EQ.js.map +1 -0
  62. package/dist/tsup/client/mod.cjs +32 -0
  63. package/dist/tsup/client/mod.cjs.map +1 -0
  64. package/dist/tsup/client/mod.d.cts +26 -0
  65. package/dist/tsup/client/mod.d.ts +26 -0
  66. package/dist/tsup/client/mod.js +32 -0
  67. package/dist/tsup/client/mod.js.map +1 -0
  68. package/dist/tsup/common/log.cjs +13 -0
  69. package/dist/tsup/common/log.cjs.map +1 -0
  70. package/dist/tsup/common/log.d.cts +20 -0
  71. package/dist/tsup/common/log.d.ts +20 -0
  72. package/dist/tsup/common/log.js +13 -0
  73. package/dist/tsup/common/log.js.map +1 -0
  74. package/dist/tsup/common/websocket.cjs +10 -0
  75. package/dist/tsup/common/websocket.cjs.map +1 -0
  76. package/dist/tsup/common/websocket.d.cts +3 -0
  77. package/dist/tsup/common/websocket.d.ts +3 -0
  78. package/dist/tsup/common/websocket.js +10 -0
  79. package/dist/tsup/common/websocket.js.map +1 -0
  80. package/dist/tsup/common-CpqORuCq.d.cts +218 -0
  81. package/dist/tsup/common-CpqORuCq.d.ts +218 -0
  82. package/dist/tsup/connection-BR_Ve4ku.d.cts +2117 -0
  83. package/dist/tsup/connection-BwUMoe6n.d.ts +2117 -0
  84. package/dist/tsup/driver-helpers/mod.cjs +33 -0
  85. package/dist/tsup/driver-helpers/mod.cjs.map +1 -0
  86. package/dist/tsup/driver-helpers/mod.d.cts +18 -0
  87. package/dist/tsup/driver-helpers/mod.d.ts +18 -0
  88. package/dist/tsup/driver-helpers/mod.js +33 -0
  89. package/dist/tsup/driver-helpers/mod.js.map +1 -0
  90. package/dist/tsup/driver-test-suite/mod.cjs +4619 -0
  91. package/dist/tsup/driver-test-suite/mod.cjs.map +1 -0
  92. package/dist/tsup/driver-test-suite/mod.d.cts +57 -0
  93. package/dist/tsup/driver-test-suite/mod.d.ts +57 -0
  94. package/dist/tsup/driver-test-suite/mod.js +4619 -0
  95. package/dist/tsup/driver-test-suite/mod.js.map +1 -0
  96. package/dist/tsup/inspector/mod.cjs +53 -0
  97. package/dist/tsup/inspector/mod.cjs.map +1 -0
  98. package/dist/tsup/inspector/mod.d.cts +408 -0
  99. package/dist/tsup/inspector/mod.d.ts +408 -0
  100. package/dist/tsup/inspector/mod.js +53 -0
  101. package/dist/tsup/inspector/mod.js.map +1 -0
  102. package/dist/tsup/mod.cjs +73 -0
  103. package/dist/tsup/mod.cjs.map +1 -0
  104. package/dist/tsup/mod.d.cts +100 -0
  105. package/dist/tsup/mod.d.ts +100 -0
  106. package/dist/tsup/mod.js +73 -0
  107. package/dist/tsup/mod.js.map +1 -0
  108. package/dist/tsup/router-endpoints-AYkXG8Tl.d.cts +66 -0
  109. package/dist/tsup/router-endpoints-DAbqVFx2.d.ts +66 -0
  110. package/dist/tsup/test/mod.cjs +21 -0
  111. package/dist/tsup/test/mod.cjs.map +1 -0
  112. package/dist/tsup/test/mod.d.cts +27 -0
  113. package/dist/tsup/test/mod.d.ts +27 -0
  114. package/dist/tsup/test/mod.js +21 -0
  115. package/dist/tsup/test/mod.js.map +1 -0
  116. package/dist/tsup/utils-CT0cv4jd.d.cts +17 -0
  117. package/dist/tsup/utils-CT0cv4jd.d.ts +17 -0
  118. package/dist/tsup/utils.cjs +26 -0
  119. package/dist/tsup/utils.cjs.map +1 -0
  120. package/dist/tsup/utils.d.cts +36 -0
  121. package/dist/tsup/utils.d.ts +36 -0
  122. package/dist/tsup/utils.js +26 -0
  123. package/dist/tsup/utils.js.map +1 -0
  124. package/package.json +208 -5
  125. package/src/actor/action.ts +182 -0
  126. package/src/actor/config.ts +765 -0
  127. package/src/actor/connection.ts +260 -0
  128. package/src/actor/context.ts +171 -0
  129. package/src/actor/database.ts +23 -0
  130. package/src/actor/definition.ts +86 -0
  131. package/src/actor/driver.ts +84 -0
  132. package/src/actor/errors.ts +360 -0
  133. package/src/actor/generic-conn-driver.ts +234 -0
  134. package/src/actor/instance.ts +1800 -0
  135. package/src/actor/log.ts +15 -0
  136. package/src/actor/mod.ts +113 -0
  137. package/src/actor/persisted.ts +42 -0
  138. package/src/actor/protocol/old.ts +281 -0
  139. package/src/actor/protocol/serde.ts +131 -0
  140. package/src/actor/router-endpoints.ts +685 -0
  141. package/src/actor/router.ts +263 -0
  142. package/src/actor/schedule.ts +17 -0
  143. package/src/actor/unstable-react.ts +110 -0
  144. package/src/actor/utils.ts +98 -0
  145. package/src/client/actor-common.ts +30 -0
  146. package/src/client/actor-conn.ts +804 -0
  147. package/src/client/actor-handle.ts +208 -0
  148. package/src/client/client.ts +623 -0
  149. package/src/client/errors.ts +41 -0
  150. package/src/client/http-client-driver.ts +326 -0
  151. package/src/client/log.ts +7 -0
  152. package/src/client/mod.ts +56 -0
  153. package/src/client/raw-utils.ts +92 -0
  154. package/src/client/test.ts +44 -0
  155. package/src/client/utils.ts +150 -0
  156. package/src/common/eventsource-interface.ts +47 -0
  157. package/src/common/eventsource.ts +80 -0
  158. package/src/common/fake-event-source.ts +266 -0
  159. package/src/common/inline-websocket-adapter2.ts +445 -0
  160. package/src/common/log-levels.ts +27 -0
  161. package/src/common/log.ts +139 -0
  162. package/src/common/logfmt.ts +228 -0
  163. package/src/common/network.ts +2 -0
  164. package/src/common/router.ts +87 -0
  165. package/src/common/utils.ts +322 -0
  166. package/src/common/versioned-data.ts +95 -0
  167. package/src/common/websocket-interface.ts +49 -0
  168. package/src/common/websocket.ts +43 -0
  169. package/src/driver-helpers/mod.ts +22 -0
  170. package/src/driver-helpers/utils.ts +17 -0
  171. package/src/driver-test-suite/log.ts +7 -0
  172. package/src/driver-test-suite/mod.ts +213 -0
  173. package/src/driver-test-suite/test-inline-client-driver.ts +402 -0
  174. package/src/driver-test-suite/tests/action-features.ts +136 -0
  175. package/src/driver-test-suite/tests/actor-auth.ts +591 -0
  176. package/src/driver-test-suite/tests/actor-conn-state.ts +249 -0
  177. package/src/driver-test-suite/tests/actor-conn.ts +349 -0
  178. package/src/driver-test-suite/tests/actor-driver.ts +25 -0
  179. package/src/driver-test-suite/tests/actor-error-handling.ts +158 -0
  180. package/src/driver-test-suite/tests/actor-handle.ts +259 -0
  181. package/src/driver-test-suite/tests/actor-inline-client.ts +152 -0
  182. package/src/driver-test-suite/tests/actor-inspector.ts +570 -0
  183. package/src/driver-test-suite/tests/actor-metadata.ts +116 -0
  184. package/src/driver-test-suite/tests/actor-onstatechange.ts +95 -0
  185. package/src/driver-test-suite/tests/actor-schedule.ts +108 -0
  186. package/src/driver-test-suite/tests/actor-sleep.ts +413 -0
  187. package/src/driver-test-suite/tests/actor-state.ts +54 -0
  188. package/src/driver-test-suite/tests/actor-vars.ts +93 -0
  189. package/src/driver-test-suite/tests/manager-driver.ts +365 -0
  190. package/src/driver-test-suite/tests/raw-http-direct-registry.ts +226 -0
  191. package/src/driver-test-suite/tests/raw-http-request-properties.ts +414 -0
  192. package/src/driver-test-suite/tests/raw-http.ts +347 -0
  193. package/src/driver-test-suite/tests/raw-websocket-direct-registry.ts +392 -0
  194. package/src/driver-test-suite/tests/raw-websocket.ts +484 -0
  195. package/src/driver-test-suite/tests/request-access.ts +244 -0
  196. package/src/driver-test-suite/utils.ts +68 -0
  197. package/src/drivers/default.ts +31 -0
  198. package/src/drivers/engine/actor-driver.ts +360 -0
  199. package/src/drivers/engine/api-endpoints.ts +128 -0
  200. package/src/drivers/engine/api-utils.ts +70 -0
  201. package/src/drivers/engine/config.ts +39 -0
  202. package/src/drivers/engine/keys.test.ts +266 -0
  203. package/src/drivers/engine/keys.ts +89 -0
  204. package/src/drivers/engine/kv.ts +3 -0
  205. package/src/drivers/engine/log.ts +7 -0
  206. package/src/drivers/engine/manager-driver.ts +391 -0
  207. package/src/drivers/engine/mod.ts +36 -0
  208. package/src/drivers/engine/ws-proxy.ts +170 -0
  209. package/src/drivers/file-system/actor.ts +91 -0
  210. package/src/drivers/file-system/global-state.ts +673 -0
  211. package/src/drivers/file-system/log.ts +7 -0
  212. package/src/drivers/file-system/manager.ts +306 -0
  213. package/src/drivers/file-system/mod.ts +48 -0
  214. package/src/drivers/file-system/utils.ts +109 -0
  215. package/src/globals.d.ts +6 -0
  216. package/src/inline-client-driver/log.ts +7 -0
  217. package/src/inline-client-driver/mod.ts +385 -0
  218. package/src/inspector/actor.ts +298 -0
  219. package/src/inspector/config.ts +83 -0
  220. package/src/inspector/log.ts +5 -0
  221. package/src/inspector/manager.ts +86 -0
  222. package/src/inspector/mod.ts +2 -0
  223. package/src/inspector/protocol/actor.ts +10 -0
  224. package/src/inspector/protocol/common.ts +196 -0
  225. package/src/inspector/protocol/manager.ts +10 -0
  226. package/src/inspector/protocol/mod.ts +2 -0
  227. package/src/inspector/utils.ts +76 -0
  228. package/src/manager/auth.ts +121 -0
  229. package/src/manager/driver.ts +80 -0
  230. package/src/manager/hono-websocket-adapter.ts +333 -0
  231. package/src/manager/log.ts +7 -0
  232. package/src/manager/mod.ts +2 -0
  233. package/src/manager/protocol/mod.ts +24 -0
  234. package/src/manager/protocol/query.ts +89 -0
  235. package/src/manager/router.ts +1792 -0
  236. package/src/mod.ts +20 -0
  237. package/src/registry/config.ts +32 -0
  238. package/src/registry/log.ts +7 -0
  239. package/src/registry/mod.ts +124 -0
  240. package/src/registry/run-config.ts +54 -0
  241. package/src/registry/serve.ts +53 -0
  242. package/src/schemas/actor-persist/mod.ts +1 -0
  243. package/src/schemas/actor-persist/versioned.ts +25 -0
  244. package/src/schemas/client-protocol/mod.ts +1 -0
  245. package/src/schemas/client-protocol/versioned.ts +63 -0
  246. package/src/schemas/file-system-driver/mod.ts +1 -0
  247. package/src/schemas/file-system-driver/versioned.ts +28 -0
  248. package/src/serde.ts +84 -0
  249. package/src/test/config.ts +16 -0
  250. package/src/test/log.ts +7 -0
  251. package/src/test/mod.ts +153 -0
  252. package/src/utils.ts +172 -0
  253. package/README.md +0 -13
@@ -0,0 +1,249 @@
1
+ import { describe, expect, test, vi } from "vitest";
2
+ import type { DriverTestConfig } from "../mod";
3
+ import { setupDriverTest } from "../utils";
4
+
5
+ export function runActorConnStateTests(driverTestConfig: DriverTestConfig) {
6
+ describe("Actor Connection State Tests", () => {
7
+ describe("Connection State Initialization", () => {
8
+ test("should retrieve connection state", async (c) => {
9
+ const { client } = await setupDriverTest(c, driverTestConfig);
10
+
11
+ // Connect to the actor
12
+ const connection = client.connStateActor.getOrCreate().connect();
13
+
14
+ // Get the connection state
15
+ const connState = await connection.getConnectionState();
16
+
17
+ // Verify the connection state structure
18
+ expect(connState.id).toBeDefined();
19
+ expect(connState.username).toBeDefined();
20
+ expect(connState.role).toBeDefined();
21
+ expect(connState.counter).toBeDefined();
22
+ expect(connState.createdAt).toBeDefined();
23
+
24
+ // Clean up
25
+ await connection.dispose();
26
+ });
27
+
28
+ test("should initialize connection state with custom parameters", async (c) => {
29
+ const { client } = await setupDriverTest(c, driverTestConfig);
30
+
31
+ // Connect with custom parameters
32
+ const connection = client.connStateActor
33
+ .getOrCreate([], {
34
+ params: {
35
+ username: "testuser",
36
+ role: "admin",
37
+ },
38
+ })
39
+ .connect();
40
+
41
+ // Get the connection state
42
+ const connState = await connection.getConnectionState();
43
+
44
+ // Verify the connection state was initialized with custom values
45
+ expect(connState.username).toBe("testuser");
46
+ expect(connState.role).toBe("admin");
47
+
48
+ // Clean up
49
+ await connection.dispose();
50
+ });
51
+ });
52
+
53
+ describe("Connection State Management", () => {
54
+ test("should maintain unique state for each connection", async (c) => {
55
+ const { client } = await setupDriverTest(c, driverTestConfig);
56
+
57
+ // Create multiple connections
58
+ const conn1 = client.connStateActor
59
+ .getOrCreate([], {
60
+ params: { username: "user1" },
61
+ })
62
+ .connect();
63
+
64
+ const conn2 = client.connStateActor
65
+ .getOrCreate([], {
66
+ params: { username: "user2" },
67
+ })
68
+ .connect();
69
+
70
+ // Update connection state for each connection
71
+ await conn1.incrementConnCounter(5);
72
+ await conn2.incrementConnCounter(10);
73
+
74
+ // Get state for each connection
75
+ const state1 = await conn1.getConnectionState();
76
+ const state2 = await conn2.getConnectionState();
77
+
78
+ // Verify states are separate
79
+ expect(state1.counter).toBe(5);
80
+ expect(state2.counter).toBe(10);
81
+ expect(state1.username).toBe("user1");
82
+ expect(state2.username).toBe("user2");
83
+
84
+ // Clean up
85
+ await conn1.dispose();
86
+ await conn2.dispose();
87
+ });
88
+
89
+ test("should track connections in shared state", async (c) => {
90
+ const { client } = await setupDriverTest(c, driverTestConfig);
91
+
92
+ // Create two connections
93
+ const handle = client.connStateActor.getOrCreate();
94
+ const conn1 = handle.connect();
95
+ const conn2 = handle.connect();
96
+
97
+ // HACK: Wait for both connections to successfully connect by waiting for a round trip RPC
98
+ await conn1.getConnectionState();
99
+ await conn2.getConnectionState();
100
+
101
+ // Get state1 for reference
102
+ const state1 = await conn1.getConnectionState();
103
+
104
+ // Get connection IDs tracked by the actor
105
+ const connectionIds = await conn1.getConnectionIds();
106
+
107
+ // There should be at least 2 connections tracked
108
+ expect(connectionIds.length).toBeGreaterThanOrEqual(2);
109
+
110
+ // Should include the ID of the first connection
111
+ expect(connectionIds).toContain(state1.id);
112
+
113
+ // Clean up
114
+ await conn1.dispose();
115
+ await conn2.dispose();
116
+ });
117
+
118
+ test("should identify different connections in the same actor", async (c) => {
119
+ const { client } = await setupDriverTest(c, driverTestConfig);
120
+
121
+ // Create two connections to the same actor
122
+ const handle = client.connStateActor.getOrCreate();
123
+ const conn1 = handle.connect();
124
+ const conn2 = handle.connect();
125
+
126
+ // HACK: Wait for both connections to successfully connect by waiting for a round trip RPC
127
+ await conn1.getConnectionState();
128
+ await conn2.getConnectionState();
129
+
130
+ // Get all connection states
131
+ const allStates = await conn1.getAllConnectionStates();
132
+
133
+ // Should have at least 2 states
134
+ expect(allStates.length).toBeGreaterThanOrEqual(2);
135
+
136
+ // IDs should be unique
137
+ const ids = allStates.map((state) => state.id);
138
+ const uniqueIds = [...new Set(ids)];
139
+ expect(uniqueIds.length).toBe(ids.length);
140
+
141
+ // Clean up
142
+ await conn1.dispose();
143
+ await conn2.dispose();
144
+ });
145
+ });
146
+
147
+ describe("Connection Lifecycle", () => {
148
+ test("should track connection and disconnection events", async (c) => {
149
+ const { client } = await setupDriverTest(c, driverTestConfig);
150
+
151
+ // Create a connection
152
+ const handle = client.connStateActor.getOrCreate();
153
+ const conn = handle.connect();
154
+
155
+ // Get the connection state
156
+ const connState = await conn.getConnectionState();
157
+
158
+ // Verify the connection is tracked
159
+ const connectionIds = await conn.getConnectionIds();
160
+ expect(connectionIds).toContain(connState.id);
161
+
162
+ // Initial disconnection count
163
+ const initialDisconnections = await conn.getDisconnectionCount();
164
+
165
+ // Dispose the connection
166
+ await conn.dispose();
167
+
168
+ // Create a new connection to check the disconnection count
169
+ const newConn = handle.connect();
170
+
171
+ // Verify disconnection was tracked
172
+ await vi.waitFor(async () => {
173
+ const newDisconnections = await newConn.getDisconnectionCount();
174
+
175
+ expect(newDisconnections).toBeGreaterThan(initialDisconnections);
176
+ });
177
+
178
+ // Clean up
179
+ await newConn.dispose();
180
+ });
181
+
182
+ test("should update connection state", async (c) => {
183
+ const { client } = await setupDriverTest(c, driverTestConfig);
184
+
185
+ // Create a connection
186
+ const conn = client.connStateActor.getOrCreate().connect();
187
+
188
+ // Get the initial state
189
+ const initialState = await conn.getConnectionState();
190
+ expect(initialState.username).toBe("anonymous");
191
+
192
+ // Update the connection state
193
+ const updatedState = await conn.updateConnection({
194
+ username: "newname",
195
+ role: "moderator",
196
+ });
197
+
198
+ // Verify the state was updated
199
+ expect(updatedState.username).toBe("newname");
200
+ expect(updatedState.role).toBe("moderator");
201
+
202
+ // Get the state again to verify persistence
203
+ const latestState = await conn.getConnectionState();
204
+ expect(latestState.username).toBe("newname");
205
+ expect(latestState.role).toBe("moderator");
206
+
207
+ // Clean up
208
+ await conn.dispose();
209
+ });
210
+ });
211
+
212
+ describe("Connection Communication", () => {
213
+ test("should send messages to specific connections", async (c) => {
214
+ const { client } = await setupDriverTest(c, driverTestConfig);
215
+
216
+ // Create two connections
217
+ const handle = client.connStateActor.getOrCreate();
218
+ const conn1 = handle.connect();
219
+ const conn2 = handle.connect();
220
+
221
+ // Get connection states
222
+ const state1 = await conn1.getConnectionState();
223
+ const state2 = await conn2.getConnectionState();
224
+
225
+ // Set up event listener on second connection
226
+ const receivedMessages: any[] = [];
227
+ conn2.on("directMessage", (data) => {
228
+ receivedMessages.push(data);
229
+ });
230
+
231
+ // Send message from first connection to second
232
+ const success = await conn1.sendToConnection(
233
+ state2.id,
234
+ "Hello from conn1",
235
+ );
236
+ expect(success).toBe(true);
237
+
238
+ // Verify message was received
239
+ expect(receivedMessages.length).toBe(1);
240
+ expect(receivedMessages[0].from).toBe(state1.id);
241
+ expect(receivedMessages[0].message).toBe("Hello from conn1");
242
+
243
+ // Clean up
244
+ await conn1.dispose();
245
+ await conn2.dispose();
246
+ });
247
+ });
248
+ });
249
+ }
@@ -0,0 +1,349 @@
1
+ import { describe, expect, test } from "vitest";
2
+ import type { DriverTestConfig } from "../mod";
3
+ import { FAKE_TIME, setupDriverTest, waitFor } from "../utils";
4
+
5
+ export function runActorConnTests(driverTestConfig: DriverTestConfig) {
6
+ describe("Actor Connection Tests", () => {
7
+ describe("Connection Methods", () => {
8
+ test("should connect using .get().connect()", async (c) => {
9
+ const { client } = await setupDriverTest(c, driverTestConfig);
10
+
11
+ // Create actor
12
+ await client.counter.create(["test-get"]);
13
+
14
+ // Get a handle and connect
15
+ const handle = client.counter.get(["test-get"]);
16
+ const connection = handle.connect();
17
+
18
+ // Verify connection by performing an action
19
+ const count = await connection.increment(5);
20
+ expect(count).toBe(5);
21
+
22
+ // Clean up
23
+ await connection.dispose();
24
+ });
25
+
26
+ test("should connect using .getForId().connect()", async (c) => {
27
+ const { client } = await setupDriverTest(c, driverTestConfig);
28
+
29
+ // Create a actor first to get its ID
30
+ const handle = client.counter.getOrCreate(["test-get-for-id"]);
31
+ await handle.increment(3);
32
+ const actorId = await handle.resolve();
33
+
34
+ // Get a new handle using the actor ID and connect
35
+ const idHandle = client.counter.getForId(actorId);
36
+ const connection = idHandle.connect();
37
+
38
+ // Verify connection works and state is preserved
39
+ const count = await connection.getCount();
40
+ expect(count).toBe(3);
41
+
42
+ // Clean up
43
+ await connection.dispose();
44
+ });
45
+
46
+ test("should connect using .getOrCreate().connect()", async (c) => {
47
+ const { client } = await setupDriverTest(c, driverTestConfig);
48
+
49
+ // Get or create actor and connect
50
+ const handle = client.counter.getOrCreate(["test-get-or-create"]);
51
+ const connection = handle.connect();
52
+
53
+ // Verify connection works
54
+ const count = await connection.increment(7);
55
+ expect(count).toBe(7);
56
+
57
+ // Clean up
58
+ await connection.dispose();
59
+ });
60
+
61
+ test("should connect using (await create()).connect()", async (c) => {
62
+ const { client } = await setupDriverTest(c, driverTestConfig);
63
+
64
+ // Create actor and connect
65
+ const handle = await client.counter.create(["test-create"]);
66
+ const connection = handle.connect();
67
+
68
+ // Verify connection works
69
+ const count = await connection.increment(9);
70
+ expect(count).toBe(9);
71
+
72
+ // Clean up
73
+ await connection.dispose();
74
+ });
75
+ });
76
+
77
+ describe("Event Communication", () => {
78
+ test("should mix RPC calls and WebSocket events", async (c) => {
79
+ const { client } = await setupDriverTest(c, driverTestConfig);
80
+
81
+ // Create actor
82
+ const handle = client.counter.getOrCreate(["test-mixed-rpc-ws"]);
83
+ const connection = handle.connect();
84
+
85
+ // Set up event listener
86
+ const receivedEvents: number[] = [];
87
+ const receivedEventsPromise = new Promise((resolve) => {
88
+ connection.on("newCount", (count: number) => {
89
+ receivedEvents.push(count);
90
+ if (
91
+ receivedEvents.includes(1) &&
92
+ receivedEvents.includes(6) &&
93
+ receivedEvents.includes(9)
94
+ )
95
+ resolve(undefined);
96
+ });
97
+ });
98
+
99
+ // Send one RPC call over the connection to ensure it's open
100
+ await connection.increment(1);
101
+
102
+ // Now use stateless RPC calls through the handle (no connection)
103
+ // These should still trigger events that the connection receives
104
+ await handle.increment(5);
105
+ await handle.increment(3);
106
+
107
+ // Wait for all events to be received
108
+ await receivedEventsPromise;
109
+
110
+ // Clean up
111
+ await connection.dispose();
112
+ });
113
+
114
+ test("should receive events via broadcast", async (c) => {
115
+ const { client } = await setupDriverTest(c, driverTestConfig);
116
+
117
+ // Create actor and connect
118
+ const handle = client.counter.getOrCreate(["test-broadcast"]);
119
+ const connection = handle.connect();
120
+
121
+ // Set up event listener
122
+ const receivedEvents: number[] = [];
123
+ connection.on("newCount", (count: number) => {
124
+ receivedEvents.push(count);
125
+ });
126
+
127
+ // Trigger broadcast events
128
+ await connection.increment(5);
129
+ await connection.increment(3);
130
+
131
+ // Verify events were received
132
+ expect(receivedEvents).toContain(5);
133
+ expect(receivedEvents).toContain(8);
134
+
135
+ // Clean up
136
+ await connection.dispose();
137
+ });
138
+
139
+ test("should handle one-time events with once()", async (c) => {
140
+ const { client } = await setupDriverTest(c, driverTestConfig);
141
+
142
+ // Create actor and connect
143
+ const handle = client.counter.getOrCreate(["test-once"]);
144
+ const connection = handle.connect();
145
+
146
+ // Set up one-time event listener
147
+ const receivedEvents: number[] = [];
148
+ connection.once("newCount", (count: number) => {
149
+ receivedEvents.push(count);
150
+ });
151
+
152
+ // Trigger multiple events, but should only receive the first one
153
+ await connection.increment(5);
154
+ await connection.increment(3);
155
+
156
+ // Verify only the first event was received
157
+ expect(receivedEvents).toEqual([5]);
158
+ expect(receivedEvents).not.toContain(8);
159
+
160
+ // Clean up
161
+ await connection.dispose();
162
+ });
163
+
164
+ test("should unsubscribe from events", async (c) => {
165
+ const { client } = await setupDriverTest(c, driverTestConfig);
166
+
167
+ // Create actor and connect
168
+ const handle = client.counter.getOrCreate(["test-unsubscribe"]);
169
+ const connection = handle.connect();
170
+
171
+ // Set up event listener with unsubscribe
172
+ const receivedEvents: number[] = [];
173
+ const unsubscribe = connection.on("newCount", (count: number) => {
174
+ receivedEvents.push(count);
175
+ });
176
+
177
+ // Trigger first event
178
+ await connection.increment(5);
179
+
180
+ // Unsubscribe
181
+ unsubscribe();
182
+
183
+ // Trigger second event, should not be received
184
+ await connection.increment(3);
185
+
186
+ // Verify only the first event was received
187
+ expect(receivedEvents).toEqual([5]);
188
+ expect(receivedEvents).not.toContain(8);
189
+
190
+ // Clean up
191
+ await connection.dispose();
192
+ });
193
+ });
194
+
195
+ describe("Connection Parameters", () => {
196
+ test("should pass connection parameters", async (c) => {
197
+ const { client } = await setupDriverTest(c, driverTestConfig);
198
+
199
+ // Create two connections with different params
200
+ const handle1 = client.counterWithParams.getOrCreate(["test-params"], {
201
+ params: { name: "user1" },
202
+ });
203
+ const handle2 = client.counterWithParams.getOrCreate(["test-params"], {
204
+ params: { name: "user2" },
205
+ });
206
+
207
+ const conn1 = handle1.connect();
208
+ const conn2 = handle2.connect();
209
+
210
+ // HACK: Call an action to wait for the connections to be established
211
+ await conn1.getInitializers();
212
+ await conn2.getInitializers();
213
+
214
+ // Get initializers to verify connection params were used
215
+ const initializers = await conn1.getInitializers();
216
+
217
+ // Verify both connection names were recorded
218
+ expect(initializers).toContain("user1");
219
+ expect(initializers).toContain("user2");
220
+
221
+ // Clean up
222
+ await conn1.dispose();
223
+ await conn2.dispose();
224
+ });
225
+ });
226
+
227
+ describe("Lifecycle Hooks", () => {
228
+ test("should trigger lifecycle hooks", async (c) => {
229
+ const { client } = await setupDriverTest(c, driverTestConfig);
230
+
231
+ // Create and connect
232
+ const connHandle = client.counterWithLifecycle.getOrCreate(
233
+ ["test-lifecycle"],
234
+ {
235
+ params: { trackLifecycle: true },
236
+ },
237
+ );
238
+ const connection = connHandle.connect();
239
+
240
+ // Verify lifecycle events were triggered
241
+ const events = await connection.getEvents();
242
+ expect(events).toEqual(["onStart", "onBeforeConnect", "onConnect"]);
243
+
244
+ // Disconnect should trigger onDisconnect
245
+ await connection.dispose();
246
+
247
+ // Reconnect to check if onDisconnect was called
248
+ const handle = client.counterWithLifecycle.getOrCreate([
249
+ "test-lifecycle",
250
+ ]);
251
+ const finalEvents = await handle.getEvents();
252
+ expect(finalEvents).toBeOneOf([
253
+ // Still active
254
+ ["onStart", "onBeforeConnect", "onConnect", "onDisconnect"],
255
+ // Went to sleep and woke back up
256
+ [
257
+ "onStart",
258
+ "onBeforeConnect",
259
+ "onConnect",
260
+ "onDisconnect",
261
+ "onStart",
262
+ ],
263
+ ]);
264
+ });
265
+ });
266
+
267
+ describe("Connection Liveness", () => {
268
+ // TODO: KIT-242
269
+ test.skip("should return correct liveness status for connections", async (c) => {
270
+ const { client } = await setupDriverTest(c, driverTestConfig);
271
+
272
+ // Create actor and connection
273
+ const handle = client.connLivenessActor.getOrCreate([
274
+ "test-liveness-status",
275
+ ]);
276
+ const connA = handle.connect();
277
+ const connB = handle.connect();
278
+
279
+ const connAId = await connA.getConnectionId();
280
+ const connBId = await connB.getConnectionId();
281
+
282
+ // Verify connection works initially
283
+ await connA.increment(5);
284
+ await connB.increment(5);
285
+
286
+ const counter = await handle.getCounter();
287
+ expect(counter).toBe(10);
288
+
289
+ const connectionsStatusBeforeKill =
290
+ await handle.getWsConnectionsLiveness();
291
+ expect(connectionsStatusBeforeKill).toHaveLength(2);
292
+ expect(connectionsStatusBeforeKill).toContainEqual(
293
+ expect.objectContaining({
294
+ id: connAId,
295
+ status: "connected",
296
+ lastSeen: FAKE_TIME.getTime(),
297
+ }),
298
+ );
299
+ expect(connectionsStatusBeforeKill).toContainEqual(
300
+ expect.objectContaining({
301
+ id: connBId,
302
+ status: "connected",
303
+ lastSeen: FAKE_TIME.getTime(),
304
+ }),
305
+ );
306
+
307
+ // Kill one connection
308
+ await handle.kill(connAId); // instead of dispose, we use kill to simulate a disconnection (e.g. network failure)
309
+ // connA.dispose();
310
+ // we killed the connection, but the actor instance does not know about it yet
311
+ // it should still be in the list of connections, but with a status of "reconnecting"
312
+ const connectionsStatusAfterKill =
313
+ await handle.getWsConnectionsLiveness();
314
+ expect(connectionsStatusAfterKill).toEqual(
315
+ expect.arrayContaining([
316
+ expect.objectContaining({
317
+ id: connAId,
318
+ status: "reconnecting",
319
+ lastSeen: FAKE_TIME.getTime(),
320
+ }),
321
+ expect.objectContaining({
322
+ id: connBId,
323
+ status: "connected",
324
+ lastSeen: FAKE_TIME.getTime(),
325
+ }),
326
+ ]),
327
+ );
328
+
329
+ // default time to wait for cleanup is 5 seconds
330
+ // check actor options
331
+ await waitFor(driverTestConfig, 5_000);
332
+
333
+ // After timeout, the killed connection should be unavailable, since the manager has cleaned it up
334
+ const connectionsStatusAfterCleanup =
335
+ await handle.getWsConnectionsLiveness();
336
+ expect(connectionsStatusAfterCleanup).not.toContainEqual(
337
+ expect.objectContaining({
338
+ id: connAId,
339
+ }),
340
+ );
341
+ expect(connectionsStatusAfterCleanup).toContainEqual(
342
+ expect.objectContaining({
343
+ id: connBId,
344
+ }),
345
+ );
346
+ });
347
+ });
348
+ });
349
+ }
@@ -0,0 +1,25 @@
1
+ import { describe } from "vitest";
2
+ import type { DriverTestConfig } from "../mod";
3
+ import { runActorScheduleTests } from "./actor-schedule";
4
+ import { runActorSleepTests } from "./actor-sleep";
5
+ import { runActorStateTests } from "./actor-state";
6
+
7
+ export function runActorDriverTests(driverTestConfig: DriverTestConfig) {
8
+ describe("Actor Driver Tests", () => {
9
+ // Run state persistence tests
10
+ runActorStateTests(driverTestConfig);
11
+
12
+ // Run scheduled alarms tests
13
+ runActorScheduleTests(driverTestConfig);
14
+ });
15
+ }
16
+
17
+ /** Actor driver tests that need to be tested for all transport mechanisms. */
18
+ export function runActorDriverTestsWithTransport(
19
+ driverTestConfig: DriverTestConfig,
20
+ ) {
21
+ describe("Actor Driver Tests", () => {
22
+ // Run actor sleep tests
23
+ runActorSleepTests(driverTestConfig);
24
+ });
25
+ }