@voidhash/mimic-effect 0.0.9 → 1.0.0-beta.2

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 (227) hide show
  1. package/.turbo/turbo-build.log +136 -90
  2. package/README.md +385 -0
  3. package/dist/ColdStorage.cjs +60 -0
  4. package/dist/ColdStorage.d.cts +53 -0
  5. package/dist/ColdStorage.d.cts.map +1 -0
  6. package/dist/ColdStorage.d.mts +53 -0
  7. package/dist/ColdStorage.d.mts.map +1 -0
  8. package/dist/ColdStorage.mjs +60 -0
  9. package/dist/ColdStorage.mjs.map +1 -0
  10. package/dist/DocumentManager.cjs +263 -82
  11. package/dist/DocumentManager.d.cts +44 -22
  12. package/dist/DocumentManager.d.cts.map +1 -1
  13. package/dist/DocumentManager.d.mts +44 -22
  14. package/dist/DocumentManager.d.mts.map +1 -1
  15. package/dist/DocumentManager.mjs +259 -67
  16. package/dist/DocumentManager.mjs.map +1 -1
  17. package/dist/Errors.cjs +54 -0
  18. package/dist/Errors.d.cts +96 -0
  19. package/dist/Errors.d.cts.map +1 -0
  20. package/dist/Errors.d.mts +96 -0
  21. package/dist/Errors.d.mts.map +1 -0
  22. package/dist/Errors.mjs +48 -0
  23. package/dist/Errors.mjs.map +1 -0
  24. package/dist/HotStorage.cjs +100 -0
  25. package/dist/HotStorage.d.cts +70 -0
  26. package/dist/HotStorage.d.cts.map +1 -0
  27. package/dist/HotStorage.d.mts +70 -0
  28. package/dist/HotStorage.d.mts.map +1 -0
  29. package/dist/HotStorage.mjs +100 -0
  30. package/dist/HotStorage.mjs.map +1 -0
  31. package/dist/Metrics.cjs +143 -0
  32. package/dist/Metrics.d.cts +31 -0
  33. package/dist/Metrics.d.cts.map +1 -0
  34. package/dist/Metrics.d.mts +31 -0
  35. package/dist/Metrics.d.mts.map +1 -0
  36. package/dist/Metrics.mjs +126 -0
  37. package/dist/Metrics.mjs.map +1 -0
  38. package/dist/MimicAuthService.cjs +61 -45
  39. package/dist/MimicAuthService.d.cts +61 -48
  40. package/dist/MimicAuthService.d.cts.map +1 -1
  41. package/dist/MimicAuthService.d.mts +61 -48
  42. package/dist/MimicAuthService.d.mts.map +1 -1
  43. package/dist/MimicAuthService.mjs +60 -36
  44. package/dist/MimicAuthService.mjs.map +1 -1
  45. package/dist/MimicClusterServerEngine.cjs +521 -0
  46. package/dist/MimicClusterServerEngine.d.cts +17 -0
  47. package/dist/MimicClusterServerEngine.d.cts.map +1 -0
  48. package/dist/MimicClusterServerEngine.d.mts +17 -0
  49. package/dist/MimicClusterServerEngine.d.mts.map +1 -0
  50. package/dist/MimicClusterServerEngine.mjs +523 -0
  51. package/dist/MimicClusterServerEngine.mjs.map +1 -0
  52. package/dist/MimicServer.cjs +205 -96
  53. package/dist/MimicServer.d.cts +9 -110
  54. package/dist/MimicServer.d.cts.map +1 -1
  55. package/dist/MimicServer.d.mts +9 -110
  56. package/dist/MimicServer.d.mts.map +1 -1
  57. package/dist/MimicServer.mjs +206 -90
  58. package/dist/MimicServer.mjs.map +1 -1
  59. package/dist/MimicServerEngine.cjs +97 -0
  60. package/dist/MimicServerEngine.d.cts +78 -0
  61. package/dist/MimicServerEngine.d.cts.map +1 -0
  62. package/dist/MimicServerEngine.d.mts +78 -0
  63. package/dist/MimicServerEngine.d.mts.map +1 -0
  64. package/dist/MimicServerEngine.mjs +97 -0
  65. package/dist/MimicServerEngine.mjs.map +1 -0
  66. package/dist/PresenceManager.cjs +75 -91
  67. package/dist/PresenceManager.d.cts +17 -66
  68. package/dist/PresenceManager.d.cts.map +1 -1
  69. package/dist/PresenceManager.d.mts +17 -66
  70. package/dist/PresenceManager.d.mts.map +1 -1
  71. package/dist/PresenceManager.mjs +74 -78
  72. package/dist/PresenceManager.mjs.map +1 -1
  73. package/dist/Protocol.cjs +146 -0
  74. package/dist/Protocol.d.cts +203 -0
  75. package/dist/Protocol.d.cts.map +1 -0
  76. package/dist/Protocol.d.mts +203 -0
  77. package/dist/Protocol.d.mts.map +1 -0
  78. package/dist/Protocol.mjs +132 -0
  79. package/dist/Protocol.mjs.map +1 -0
  80. package/dist/Types.d.cts +172 -0
  81. package/dist/Types.d.cts.map +1 -0
  82. package/dist/Types.d.mts +172 -0
  83. package/dist/Types.d.mts.map +1 -0
  84. package/dist/_virtual/rolldown_runtime.cjs +1 -25
  85. package/dist/_virtual/rolldown_runtime.mjs +4 -1
  86. package/dist/index.cjs +37 -75
  87. package/dist/index.d.cts +13 -12
  88. package/dist/index.d.mts +13 -12
  89. package/dist/index.mjs +12 -12
  90. package/dist/testing/ColdStorageTestSuite.cjs +508 -0
  91. package/dist/testing/ColdStorageTestSuite.d.cts +36 -0
  92. package/dist/testing/ColdStorageTestSuite.d.cts.map +1 -0
  93. package/dist/testing/ColdStorageTestSuite.d.mts +36 -0
  94. package/dist/testing/ColdStorageTestSuite.d.mts.map +1 -0
  95. package/dist/testing/ColdStorageTestSuite.mjs +508 -0
  96. package/dist/testing/ColdStorageTestSuite.mjs.map +1 -0
  97. package/dist/testing/FailingStorage.cjs +135 -0
  98. package/dist/testing/FailingStorage.d.cts +43 -0
  99. package/dist/testing/FailingStorage.d.cts.map +1 -0
  100. package/dist/testing/FailingStorage.d.mts +43 -0
  101. package/dist/testing/FailingStorage.d.mts.map +1 -0
  102. package/dist/testing/FailingStorage.mjs +136 -0
  103. package/dist/testing/FailingStorage.mjs.map +1 -0
  104. package/dist/testing/HotStorageTestSuite.cjs +585 -0
  105. package/dist/testing/HotStorageTestSuite.d.cts +40 -0
  106. package/dist/testing/HotStorageTestSuite.d.cts.map +1 -0
  107. package/dist/testing/HotStorageTestSuite.d.mts +40 -0
  108. package/dist/testing/HotStorageTestSuite.d.mts.map +1 -0
  109. package/dist/testing/HotStorageTestSuite.mjs +585 -0
  110. package/dist/testing/HotStorageTestSuite.mjs.map +1 -0
  111. package/dist/testing/StorageIntegrationTestSuite.cjs +349 -0
  112. package/dist/testing/StorageIntegrationTestSuite.d.cts +35 -0
  113. package/dist/testing/StorageIntegrationTestSuite.d.cts.map +1 -0
  114. package/dist/testing/StorageIntegrationTestSuite.d.mts +35 -0
  115. package/dist/testing/StorageIntegrationTestSuite.d.mts.map +1 -0
  116. package/dist/testing/StorageIntegrationTestSuite.mjs +349 -0
  117. package/dist/testing/StorageIntegrationTestSuite.mjs.map +1 -0
  118. package/dist/testing/assertions.cjs +114 -0
  119. package/dist/testing/assertions.mjs +109 -0
  120. package/dist/testing/assertions.mjs.map +1 -0
  121. package/dist/testing/index.cjs +14 -0
  122. package/dist/testing/index.d.cts +6 -0
  123. package/dist/testing/index.d.mts +6 -0
  124. package/dist/testing/index.mjs +7 -0
  125. package/dist/testing/types.cjs +15 -0
  126. package/dist/testing/types.d.cts +90 -0
  127. package/dist/testing/types.d.cts.map +1 -0
  128. package/dist/testing/types.d.mts +90 -0
  129. package/dist/testing/types.d.mts.map +1 -0
  130. package/dist/testing/types.mjs +16 -0
  131. package/dist/testing/types.mjs.map +1 -0
  132. package/package.json +18 -3
  133. package/src/ColdStorage.ts +136 -0
  134. package/src/DocumentManager.ts +550 -190
  135. package/src/Errors.ts +114 -0
  136. package/src/HotStorage.ts +239 -0
  137. package/src/Metrics.ts +187 -0
  138. package/src/MimicAuthService.ts +126 -64
  139. package/src/MimicClusterServerEngine.ts +946 -0
  140. package/src/MimicServer.ts +448 -195
  141. package/src/MimicServerEngine.ts +276 -0
  142. package/src/PresenceManager.ts +169 -240
  143. package/src/Protocol.ts +350 -0
  144. package/src/Types.ts +231 -0
  145. package/src/index.ts +57 -23
  146. package/src/testing/ColdStorageTestSuite.ts +589 -0
  147. package/src/testing/FailingStorage.ts +286 -0
  148. package/src/testing/HotStorageTestSuite.ts +762 -0
  149. package/src/testing/StorageIntegrationTestSuite.ts +504 -0
  150. package/src/testing/assertions.ts +181 -0
  151. package/src/testing/index.ts +83 -0
  152. package/src/testing/types.ts +100 -0
  153. package/tests/ColdStorage.test.ts +24 -0
  154. package/tests/DocumentManager.test.ts +158 -287
  155. package/tests/HotStorage.test.ts +24 -0
  156. package/tests/MimicAuthService.test.ts +102 -134
  157. package/tests/MimicClusterServerEngine.test.ts +587 -0
  158. package/tests/MimicServer.test.ts +90 -226
  159. package/tests/MimicServerEngine.test.ts +521 -0
  160. package/tests/PresenceManager.test.ts +22 -63
  161. package/tests/Protocol.test.ts +190 -0
  162. package/tests/StorageIntegration.test.ts +259 -0
  163. package/tsconfig.json +1 -1
  164. package/tsdown.config.ts +1 -1
  165. package/dist/DocumentProtocol.cjs +0 -94
  166. package/dist/DocumentProtocol.d.cts +0 -113
  167. package/dist/DocumentProtocol.d.cts.map +0 -1
  168. package/dist/DocumentProtocol.d.mts +0 -113
  169. package/dist/DocumentProtocol.d.mts.map +0 -1
  170. package/dist/DocumentProtocol.mjs +0 -89
  171. package/dist/DocumentProtocol.mjs.map +0 -1
  172. package/dist/MimicConfig.cjs +0 -60
  173. package/dist/MimicConfig.d.cts +0 -141
  174. package/dist/MimicConfig.d.cts.map +0 -1
  175. package/dist/MimicConfig.d.mts +0 -141
  176. package/dist/MimicConfig.d.mts.map +0 -1
  177. package/dist/MimicConfig.mjs +0 -50
  178. package/dist/MimicConfig.mjs.map +0 -1
  179. package/dist/MimicDataStorage.cjs +0 -83
  180. package/dist/MimicDataStorage.d.cts +0 -113
  181. package/dist/MimicDataStorage.d.cts.map +0 -1
  182. package/dist/MimicDataStorage.d.mts +0 -113
  183. package/dist/MimicDataStorage.d.mts.map +0 -1
  184. package/dist/MimicDataStorage.mjs +0 -74
  185. package/dist/MimicDataStorage.mjs.map +0 -1
  186. package/dist/WebSocketHandler.cjs +0 -365
  187. package/dist/WebSocketHandler.d.cts +0 -34
  188. package/dist/WebSocketHandler.d.cts.map +0 -1
  189. package/dist/WebSocketHandler.d.mts +0 -34
  190. package/dist/WebSocketHandler.d.mts.map +0 -1
  191. package/dist/WebSocketHandler.mjs +0 -355
  192. package/dist/WebSocketHandler.mjs.map +0 -1
  193. package/dist/auth/NoAuth.cjs +0 -43
  194. package/dist/auth/NoAuth.d.cts +0 -22
  195. package/dist/auth/NoAuth.d.cts.map +0 -1
  196. package/dist/auth/NoAuth.d.mts +0 -22
  197. package/dist/auth/NoAuth.d.mts.map +0 -1
  198. package/dist/auth/NoAuth.mjs +0 -36
  199. package/dist/auth/NoAuth.mjs.map +0 -1
  200. package/dist/errors.cjs +0 -74
  201. package/dist/errors.d.cts +0 -89
  202. package/dist/errors.d.cts.map +0 -1
  203. package/dist/errors.d.mts +0 -89
  204. package/dist/errors.d.mts.map +0 -1
  205. package/dist/errors.mjs +0 -67
  206. package/dist/errors.mjs.map +0 -1
  207. package/dist/storage/InMemoryDataStorage.cjs +0 -57
  208. package/dist/storage/InMemoryDataStorage.d.cts +0 -19
  209. package/dist/storage/InMemoryDataStorage.d.cts.map +0 -1
  210. package/dist/storage/InMemoryDataStorage.d.mts +0 -19
  211. package/dist/storage/InMemoryDataStorage.d.mts.map +0 -1
  212. package/dist/storage/InMemoryDataStorage.mjs +0 -48
  213. package/dist/storage/InMemoryDataStorage.mjs.map +0 -1
  214. package/src/DocumentProtocol.ts +0 -112
  215. package/src/MimicConfig.ts +0 -211
  216. package/src/MimicDataStorage.ts +0 -157
  217. package/src/WebSocketHandler.ts +0 -735
  218. package/src/auth/NoAuth.ts +0 -46
  219. package/src/errors.ts +0 -113
  220. package/src/storage/InMemoryDataStorage.ts +0 -66
  221. package/tests/DocumentProtocol.test.ts +0 -113
  222. package/tests/InMemoryDataStorage.test.ts +0 -190
  223. package/tests/MimicConfig.test.ts +0 -290
  224. package/tests/MimicDataStorage.test.ts +0 -190
  225. package/tests/NoAuth.test.ts +0 -94
  226. package/tests/WebSocketHandler.test.ts +0 -321
  227. package/tests/errors.test.ts +0 -77
@@ -0,0 +1,521 @@
1
+ import { describe, it, expect } from "vitest";
2
+ import { Effect, Layer, Stream } from "effect";
3
+ import { Schema } from "effect";
4
+ import { Primitive, Presence, Document, Transaction } from "@voidhash/mimic";
5
+ import {
6
+ MimicServerEngine,
7
+ MimicServerEngineTag,
8
+ } from "../src/MimicServerEngine";
9
+ import { ColdStorage } from "../src/ColdStorage";
10
+ import { HotStorage } from "../src/HotStorage";
11
+ import { MimicAuthService } from "../src/MimicAuthService";
12
+ import * as Protocol from "../src/Protocol";
13
+
14
+ // =============================================================================
15
+ // Test Schema
16
+ // =============================================================================
17
+
18
+ const TestSchema = Primitive.Struct({
19
+ title: Primitive.String().default(""),
20
+ count: Primitive.Number().default(0),
21
+ });
22
+
23
+ const CursorPresence = Presence.make({
24
+ schema: Schema.Struct({
25
+ x: Schema.Number,
26
+ y: Schema.Number,
27
+ name: Schema.optional(Schema.String),
28
+ }),
29
+ });
30
+
31
+ // =============================================================================
32
+ // Helper Functions
33
+ // =============================================================================
34
+
35
+ /**
36
+ * Create a valid transaction using the Document API
37
+ */
38
+ const createValidTransaction = (title: string): Transaction.Transaction => {
39
+ const doc = Document.make(TestSchema);
40
+ doc.transaction((root) => {
41
+ root.title.set(title);
42
+ });
43
+ return doc.flush();
44
+ };
45
+
46
+ // =============================================================================
47
+ // Test Layer Factory
48
+ // =============================================================================
49
+
50
+ const makeTestLayer = (options?: {
51
+ withPresence?: boolean;
52
+ authPermissions?: Record<string, "read" | "write">;
53
+ initial?: { title: string; count?: number };
54
+ }) => {
55
+ const Engine = MimicServerEngine.make({
56
+ schema: TestSchema,
57
+ initial: options?.initial,
58
+ presence: options?.withPresence ? CursorPresence : undefined,
59
+ });
60
+
61
+ const authLayer = options?.authPermissions
62
+ ? MimicAuthService.Static.make({
63
+ permissions: options.authPermissions,
64
+ defaultPermission: undefined,
65
+ })
66
+ : MimicAuthService.NoAuth.make();
67
+
68
+ return Engine.pipe(
69
+ Layer.provide(ColdStorage.InMemory.make()),
70
+ Layer.provide(HotStorage.InMemory.make()),
71
+ Layer.provide(authLayer)
72
+ );
73
+ };
74
+
75
+ // =============================================================================
76
+ // MimicServerEngine Tests
77
+ // =============================================================================
78
+
79
+ describe("MimicServerEngine", () => {
80
+ describe("make", () => {
81
+ it("should create engine with minimal config", async () => {
82
+ const Engine = MimicServerEngine.make({
83
+ schema: TestSchema,
84
+ });
85
+
86
+ const layer = Engine.pipe(
87
+ Layer.provide(ColdStorage.InMemory.make()),
88
+ Layer.provide(HotStorage.InMemory.make()),
89
+ Layer.provide(MimicAuthService.NoAuth.make())
90
+ );
91
+
92
+ const result = await Effect.runPromise(
93
+ Effect.scoped(
94
+ Effect.gen(function* () {
95
+ const engine = yield* MimicServerEngineTag;
96
+ // Engine should have document management methods
97
+ return (
98
+ typeof engine.submit === "function" &&
99
+ typeof engine.getSnapshot === "function" &&
100
+ typeof engine.subscribe === "function" &&
101
+ typeof engine.touch === "function" &&
102
+ engine.config !== undefined
103
+ );
104
+ })
105
+ ).pipe(Effect.provide(layer))
106
+ );
107
+
108
+ expect(result).toBe(true);
109
+ });
110
+
111
+ it("should create engine with full config", async () => {
112
+ const Engine = MimicServerEngine.make({
113
+ schema: TestSchema,
114
+ initial: { title: "Default Title" },
115
+ maxIdleTime: "10 minutes",
116
+ maxTransactionHistory: 500,
117
+ snapshot: {
118
+ interval: "1 minute",
119
+ transactionThreshold: 50,
120
+ },
121
+ });
122
+
123
+ const layer = Engine.pipe(
124
+ Layer.provide(ColdStorage.InMemory.make()),
125
+ Layer.provide(HotStorage.InMemory.make()),
126
+ Layer.provide(MimicAuthService.NoAuth.make())
127
+ );
128
+
129
+ const result = await Effect.runPromise(
130
+ Effect.scoped(
131
+ Effect.gen(function* () {
132
+ const engine = yield* MimicServerEngineTag;
133
+ return engine.config !== undefined;
134
+ })
135
+ ).pipe(Effect.provide(layer))
136
+ );
137
+
138
+ expect(result).toBe(true);
139
+ });
140
+
141
+ it("should work with static auth", async () => {
142
+ const Engine = MimicServerEngine.make({
143
+ schema: TestSchema,
144
+ });
145
+
146
+ const layer = Engine.pipe(
147
+ Layer.provide(ColdStorage.InMemory.make()),
148
+ Layer.provide(HotStorage.InMemory.make()),
149
+ Layer.provide(
150
+ MimicAuthService.Static.make({
151
+ permissions: { admin: "write", user: "read" },
152
+ })
153
+ )
154
+ );
155
+
156
+ const result = await Effect.runPromise(
157
+ Effect.scoped(
158
+ Effect.gen(function* () {
159
+ const engine = yield* MimicServerEngineTag;
160
+ return engine.config !== undefined;
161
+ })
162
+ ).pipe(Effect.provide(layer))
163
+ );
164
+
165
+ expect(result).toBe(true);
166
+ });
167
+ });
168
+
169
+ describe("document management", () => {
170
+ it("should get snapshot for new document", async () => {
171
+ const result = await Effect.runPromise(
172
+ Effect.scoped(
173
+ Effect.gen(function* () {
174
+ const engine = yield* MimicServerEngineTag;
175
+ const snapshot = yield* engine.getSnapshot("test-doc-1");
176
+ return snapshot;
177
+ })
178
+ ).pipe(Effect.provide(makeTestLayer({ initial: { title: "Initial" } })))
179
+ );
180
+
181
+ // Note: state only includes properties that were explicitly set (not defaults)
182
+ expect(result.state).toEqual({ title: "Initial" });
183
+ expect(result.version).toBe(0);
184
+ });
185
+
186
+ it("should submit transaction and update state", async () => {
187
+ const tx = createValidTransaction("Updated Title");
188
+
189
+ const result = await Effect.runPromise(
190
+ Effect.scoped(
191
+ Effect.gen(function* () {
192
+ const engine = yield* MimicServerEngineTag;
193
+
194
+ // Submit transaction
195
+ const submitResult = yield* engine.submit("test-doc-2", tx);
196
+
197
+ // Get snapshot
198
+ const snapshot = yield* engine.getSnapshot("test-doc-2");
199
+
200
+ return { submitResult, snapshot };
201
+ })
202
+ ).pipe(Effect.provide(makeTestLayer({ initial: { title: "Initial" } })))
203
+ );
204
+
205
+ expect(result.submitResult.success).toBe(true);
206
+ if (result.submitResult.success) {
207
+ expect(result.submitResult.version).toBe(1);
208
+ }
209
+ // Note: state only includes properties that were explicitly set
210
+ expect(result.snapshot.state).toEqual({ title: "Updated Title" });
211
+ });
212
+
213
+ it("should touch document to update activity time", async () => {
214
+ const result = await Effect.runPromise(
215
+ Effect.scoped(
216
+ Effect.gen(function* () {
217
+ const engine = yield* MimicServerEngineTag;
218
+
219
+ // Get document first (creates it)
220
+ yield* engine.getSnapshot("test-doc-3");
221
+
222
+ // Touch should not throw
223
+ yield* engine.touch("test-doc-3");
224
+
225
+ return true;
226
+ })
227
+ ).pipe(Effect.provide(makeTestLayer()))
228
+ );
229
+
230
+ expect(result).toBe(true);
231
+ });
232
+ });
233
+
234
+ describe("presence management", () => {
235
+ it("should set and get presence", async () => {
236
+ const result = await Effect.runPromise(
237
+ Effect.scoped(
238
+ Effect.gen(function* () {
239
+ const engine = yield* MimicServerEngineTag;
240
+
241
+ // Set presence
242
+ yield* engine.setPresence("doc-1", "conn-1", {
243
+ data: { x: 10, y: 20 },
244
+ userId: "user-1",
245
+ });
246
+
247
+ // Get snapshot
248
+ const snapshot = yield* engine.getPresenceSnapshot("doc-1");
249
+
250
+ return snapshot;
251
+ })
252
+ ).pipe(Effect.provide(makeTestLayer({ withPresence: true })))
253
+ );
254
+
255
+ expect(result.presences["conn-1"]).toEqual({
256
+ data: { x: 10, y: 20 },
257
+ userId: "user-1",
258
+ });
259
+ });
260
+
261
+ it("should remove presence", async () => {
262
+ const result = await Effect.runPromise(
263
+ Effect.scoped(
264
+ Effect.gen(function* () {
265
+ const engine = yield* MimicServerEngineTag;
266
+
267
+ // Set presence
268
+ yield* engine.setPresence("doc-2", "conn-2", {
269
+ data: { x: 10, y: 20 },
270
+ userId: "user-1",
271
+ });
272
+
273
+ // Remove presence
274
+ yield* engine.removePresence("doc-2", "conn-2");
275
+
276
+ // Get snapshot
277
+ const snapshot = yield* engine.getPresenceSnapshot("doc-2");
278
+
279
+ return snapshot;
280
+ })
281
+ ).pipe(Effect.provide(makeTestLayer({ withPresence: true })))
282
+ );
283
+
284
+ expect(result.presences["conn-2"]).toBeUndefined();
285
+ });
286
+ });
287
+
288
+ describe("Tag", () => {
289
+ it("should have correct identifier", () => {
290
+ expect(MimicServerEngineTag.key).toBe(
291
+ "@voidhash/mimic-effect/MimicServerEngine"
292
+ );
293
+ });
294
+ });
295
+
296
+ describe("presence validation", () => {
297
+ it("should validate presence data against schema using Presence module", () => {
298
+ const validData = { x: 100, y: 200 };
299
+ const invalidData = { x: "invalid", y: 200 };
300
+
301
+ // Valid data should pass validation
302
+ const validated = Presence.validateSafe(CursorPresence, validData);
303
+ expect(validated).toEqual({ x: 100, y: 200 });
304
+
305
+ // Invalid data should return undefined
306
+ const invalidResult = Presence.validateSafe(CursorPresence, invalidData);
307
+ expect(invalidResult).toBeUndefined();
308
+ });
309
+
310
+ it("should handle optional fields in presence schema", () => {
311
+ // Without optional field
312
+ const withoutName = Presence.validateSafe(CursorPresence, {
313
+ x: 10,
314
+ y: 20,
315
+ });
316
+ expect(withoutName).toEqual({ x: 10, y: 20 });
317
+
318
+ // With optional field
319
+ const withName = Presence.validateSafe(CursorPresence, {
320
+ x: 10,
321
+ y: 20,
322
+ name: "Alice",
323
+ });
324
+ expect(withName).toEqual({ x: 10, y: 20, name: "Alice" });
325
+ });
326
+ });
327
+
328
+ describe("protocol message types", () => {
329
+ it("should create auth_result success message", () => {
330
+ const message = Protocol.authResultSuccess("user-123", "write");
331
+ expect(message).toEqual({
332
+ type: "auth_result",
333
+ success: true,
334
+ userId: "user-123",
335
+ permission: "write",
336
+ });
337
+ });
338
+
339
+ it("should create auth_result failure message", () => {
340
+ const message = Protocol.authResultFailure("Invalid token");
341
+ expect(message).toEqual({
342
+ type: "auth_result",
343
+ success: false,
344
+ error: "Invalid token",
345
+ });
346
+ });
347
+
348
+ it("should create presence_snapshot message", () => {
349
+ const message = Protocol.presenceSnapshotMessage("conn-123", {
350
+ "conn-456": { data: { x: 10, y: 20 }, userId: "user-1" },
351
+ });
352
+ expect(message).toEqual({
353
+ type: "presence_snapshot",
354
+ selfId: "conn-123",
355
+ presences: {
356
+ "conn-456": { data: { x: 10, y: 20 }, userId: "user-1" },
357
+ },
358
+ });
359
+ });
360
+
361
+ it("should create presence_update message", () => {
362
+ const message = Protocol.presenceUpdateMessage("conn-789", { x: 50, y: 75 }, "user-2");
363
+ expect(message).toEqual({
364
+ type: "presence_update",
365
+ id: "conn-789",
366
+ data: { x: 50, y: 75 },
367
+ userId: "user-2",
368
+ });
369
+ });
370
+
371
+ it("should create presence_remove message", () => {
372
+ const message = Protocol.presenceRemoveMessage("conn-disconnected");
373
+ expect(message).toEqual({
374
+ type: "presence_remove",
375
+ id: "conn-disconnected",
376
+ });
377
+ });
378
+
379
+ it("should create pong message", () => {
380
+ const message = Protocol.pong();
381
+ expect(message).toEqual({ type: "pong" });
382
+ });
383
+
384
+ it("should create snapshot message", () => {
385
+ const message = Protocol.snapshotMessage({ title: "Test", count: 5 }, 42);
386
+ expect(message).toEqual({
387
+ type: "snapshot",
388
+ state: { title: "Test", count: 5 },
389
+ version: 42,
390
+ });
391
+ });
392
+
393
+ it("should create error message", () => {
394
+ const message = Protocol.errorMessage("tx-123", "Duplicate transaction");
395
+ expect(message).toEqual({
396
+ type: "error",
397
+ transactionId: "tx-123",
398
+ reason: "Duplicate transaction",
399
+ });
400
+ });
401
+
402
+ it("should create transaction message", () => {
403
+ const tx = createValidTransaction("Test");
404
+ const message = Protocol.transactionMessage(tx, 5);
405
+ expect(message.type).toBe("transaction");
406
+ expect(message.version).toBe(5);
407
+ expect(message.transaction.id).toBe(tx.id);
408
+ });
409
+ });
410
+
411
+ describe("protocol encoding/decoding", () => {
412
+ it("should parse auth message", async () => {
413
+ const result = await Effect.runPromise(
414
+ Protocol.parseClientMessage(JSON.stringify({ type: "auth", token: "test-token" }))
415
+ );
416
+ expect(result).toEqual({ type: "auth", token: "test-token" });
417
+ });
418
+
419
+ it("should parse ping message", async () => {
420
+ const result = await Effect.runPromise(
421
+ Protocol.parseClientMessage(JSON.stringify({ type: "ping" }))
422
+ );
423
+ expect(result).toEqual({ type: "ping" });
424
+ });
425
+
426
+ it("should parse request_snapshot message", async () => {
427
+ const result = await Effect.runPromise(
428
+ Protocol.parseClientMessage(JSON.stringify({ type: "request_snapshot" }))
429
+ );
430
+ expect(result).toEqual({ type: "request_snapshot" });
431
+ });
432
+
433
+ it("should parse presence_set message", async () => {
434
+ const result = await Effect.runPromise(
435
+ Protocol.parseClientMessage(
436
+ JSON.stringify({ type: "presence_set", data: { x: 10, y: 20 } })
437
+ )
438
+ );
439
+ expect(result).toEqual({ type: "presence_set", data: { x: 10, y: 20 } });
440
+ });
441
+
442
+ it("should parse presence_clear message", async () => {
443
+ const result = await Effect.runPromise(
444
+ Protocol.parseClientMessage(JSON.stringify({ type: "presence_clear" }))
445
+ );
446
+ expect(result).toEqual({ type: "presence_clear" });
447
+ });
448
+
449
+ it("should encode and decode transaction message correctly", () => {
450
+ const tx = createValidTransaction("Hello");
451
+ const message = Protocol.transactionMessage(tx, 10);
452
+ const encoded = Protocol.encodeServerMessage(message);
453
+ const decoded = JSON.parse(encoded);
454
+
455
+ expect(decoded.type).toBe("transaction");
456
+ expect(decoded.version).toBe(10);
457
+ // Transaction should be encoded
458
+ expect(decoded.transaction).toBeDefined();
459
+ });
460
+
461
+ it("should encode snapshot message as JSON", () => {
462
+ const message = Protocol.snapshotMessage({ title: "Test" }, 5);
463
+ const encoded = Protocol.encodeServerMessage(message);
464
+ const decoded = JSON.parse(encoded);
465
+
466
+ expect(decoded).toEqual({
467
+ type: "snapshot",
468
+ state: { title: "Test" },
469
+ version: 5,
470
+ });
471
+ });
472
+ });
473
+
474
+ describe("auth integration", () => {
475
+ it("should authenticate with static auth service", async () => {
476
+ const result = await Effect.runPromise(
477
+ Effect.scoped(
478
+ Effect.gen(function* () {
479
+ const engine = yield* MimicServerEngineTag;
480
+ // Just verify the engine was created with auth layer
481
+ return engine.config !== undefined;
482
+ })
483
+ ).pipe(
484
+ Effect.provide(
485
+ makeTestLayer({
486
+ authPermissions: { admin: "write", viewer: "read" },
487
+ })
488
+ )
489
+ )
490
+ );
491
+
492
+ expect(result).toBe(true);
493
+ });
494
+
495
+ it("should create engine with presence enabled", async () => {
496
+ const result = await Effect.runPromise(
497
+ Effect.scoped(
498
+ Effect.gen(function* () {
499
+ const engine = yield* MimicServerEngineTag;
500
+ return engine.config.presence !== undefined;
501
+ })
502
+ ).pipe(Effect.provide(makeTestLayer({ withPresence: true })))
503
+ );
504
+
505
+ expect(result).toBe(true);
506
+ });
507
+
508
+ it("should create engine with initial state", async () => {
509
+ const result = await Effect.runPromise(
510
+ Effect.scoped(
511
+ Effect.gen(function* () {
512
+ const engine = yield* MimicServerEngineTag;
513
+ return engine.config.initial !== undefined;
514
+ })
515
+ ).pipe(Effect.provide(makeTestLayer({ initial: { title: "Initial" } })))
516
+ );
517
+
518
+ expect(result).toBe(true);
519
+ });
520
+ });
521
+ });