@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.
- package/.turbo/turbo-build.log +136 -90
- package/README.md +385 -0
- package/dist/ColdStorage.cjs +60 -0
- package/dist/ColdStorage.d.cts +53 -0
- package/dist/ColdStorage.d.cts.map +1 -0
- package/dist/ColdStorage.d.mts +53 -0
- package/dist/ColdStorage.d.mts.map +1 -0
- package/dist/ColdStorage.mjs +60 -0
- package/dist/ColdStorage.mjs.map +1 -0
- package/dist/DocumentManager.cjs +263 -82
- package/dist/DocumentManager.d.cts +44 -22
- package/dist/DocumentManager.d.cts.map +1 -1
- package/dist/DocumentManager.d.mts +44 -22
- package/dist/DocumentManager.d.mts.map +1 -1
- package/dist/DocumentManager.mjs +259 -67
- package/dist/DocumentManager.mjs.map +1 -1
- package/dist/Errors.cjs +54 -0
- package/dist/Errors.d.cts +96 -0
- package/dist/Errors.d.cts.map +1 -0
- package/dist/Errors.d.mts +96 -0
- package/dist/Errors.d.mts.map +1 -0
- package/dist/Errors.mjs +48 -0
- package/dist/Errors.mjs.map +1 -0
- package/dist/HotStorage.cjs +100 -0
- package/dist/HotStorage.d.cts +70 -0
- package/dist/HotStorage.d.cts.map +1 -0
- package/dist/HotStorage.d.mts +70 -0
- package/dist/HotStorage.d.mts.map +1 -0
- package/dist/HotStorage.mjs +100 -0
- package/dist/HotStorage.mjs.map +1 -0
- package/dist/Metrics.cjs +143 -0
- package/dist/Metrics.d.cts +31 -0
- package/dist/Metrics.d.cts.map +1 -0
- package/dist/Metrics.d.mts +31 -0
- package/dist/Metrics.d.mts.map +1 -0
- package/dist/Metrics.mjs +126 -0
- package/dist/Metrics.mjs.map +1 -0
- package/dist/MimicAuthService.cjs +61 -45
- package/dist/MimicAuthService.d.cts +61 -48
- package/dist/MimicAuthService.d.cts.map +1 -1
- package/dist/MimicAuthService.d.mts +61 -48
- package/dist/MimicAuthService.d.mts.map +1 -1
- package/dist/MimicAuthService.mjs +60 -36
- package/dist/MimicAuthService.mjs.map +1 -1
- package/dist/MimicClusterServerEngine.cjs +521 -0
- package/dist/MimicClusterServerEngine.d.cts +17 -0
- package/dist/MimicClusterServerEngine.d.cts.map +1 -0
- package/dist/MimicClusterServerEngine.d.mts +17 -0
- package/dist/MimicClusterServerEngine.d.mts.map +1 -0
- package/dist/MimicClusterServerEngine.mjs +523 -0
- package/dist/MimicClusterServerEngine.mjs.map +1 -0
- package/dist/MimicServer.cjs +205 -96
- package/dist/MimicServer.d.cts +9 -110
- package/dist/MimicServer.d.cts.map +1 -1
- package/dist/MimicServer.d.mts +9 -110
- package/dist/MimicServer.d.mts.map +1 -1
- package/dist/MimicServer.mjs +206 -90
- package/dist/MimicServer.mjs.map +1 -1
- package/dist/MimicServerEngine.cjs +97 -0
- package/dist/MimicServerEngine.d.cts +78 -0
- package/dist/MimicServerEngine.d.cts.map +1 -0
- package/dist/MimicServerEngine.d.mts +78 -0
- package/dist/MimicServerEngine.d.mts.map +1 -0
- package/dist/MimicServerEngine.mjs +97 -0
- package/dist/MimicServerEngine.mjs.map +1 -0
- package/dist/PresenceManager.cjs +75 -91
- package/dist/PresenceManager.d.cts +17 -66
- package/dist/PresenceManager.d.cts.map +1 -1
- package/dist/PresenceManager.d.mts +17 -66
- package/dist/PresenceManager.d.mts.map +1 -1
- package/dist/PresenceManager.mjs +74 -78
- package/dist/PresenceManager.mjs.map +1 -1
- package/dist/Protocol.cjs +146 -0
- package/dist/Protocol.d.cts +203 -0
- package/dist/Protocol.d.cts.map +1 -0
- package/dist/Protocol.d.mts +203 -0
- package/dist/Protocol.d.mts.map +1 -0
- package/dist/Protocol.mjs +132 -0
- package/dist/Protocol.mjs.map +1 -0
- package/dist/Types.d.cts +172 -0
- package/dist/Types.d.cts.map +1 -0
- package/dist/Types.d.mts +172 -0
- package/dist/Types.d.mts.map +1 -0
- package/dist/_virtual/rolldown_runtime.cjs +1 -25
- package/dist/_virtual/rolldown_runtime.mjs +4 -1
- package/dist/index.cjs +37 -75
- package/dist/index.d.cts +13 -12
- package/dist/index.d.mts +13 -12
- package/dist/index.mjs +12 -12
- package/dist/testing/ColdStorageTestSuite.cjs +508 -0
- package/dist/testing/ColdStorageTestSuite.d.cts +36 -0
- package/dist/testing/ColdStorageTestSuite.d.cts.map +1 -0
- package/dist/testing/ColdStorageTestSuite.d.mts +36 -0
- package/dist/testing/ColdStorageTestSuite.d.mts.map +1 -0
- package/dist/testing/ColdStorageTestSuite.mjs +508 -0
- package/dist/testing/ColdStorageTestSuite.mjs.map +1 -0
- package/dist/testing/FailingStorage.cjs +135 -0
- package/dist/testing/FailingStorage.d.cts +43 -0
- package/dist/testing/FailingStorage.d.cts.map +1 -0
- package/dist/testing/FailingStorage.d.mts +43 -0
- package/dist/testing/FailingStorage.d.mts.map +1 -0
- package/dist/testing/FailingStorage.mjs +136 -0
- package/dist/testing/FailingStorage.mjs.map +1 -0
- package/dist/testing/HotStorageTestSuite.cjs +585 -0
- package/dist/testing/HotStorageTestSuite.d.cts +40 -0
- package/dist/testing/HotStorageTestSuite.d.cts.map +1 -0
- package/dist/testing/HotStorageTestSuite.d.mts +40 -0
- package/dist/testing/HotStorageTestSuite.d.mts.map +1 -0
- package/dist/testing/HotStorageTestSuite.mjs +585 -0
- package/dist/testing/HotStorageTestSuite.mjs.map +1 -0
- package/dist/testing/StorageIntegrationTestSuite.cjs +349 -0
- package/dist/testing/StorageIntegrationTestSuite.d.cts +35 -0
- package/dist/testing/StorageIntegrationTestSuite.d.cts.map +1 -0
- package/dist/testing/StorageIntegrationTestSuite.d.mts +35 -0
- package/dist/testing/StorageIntegrationTestSuite.d.mts.map +1 -0
- package/dist/testing/StorageIntegrationTestSuite.mjs +349 -0
- package/dist/testing/StorageIntegrationTestSuite.mjs.map +1 -0
- package/dist/testing/assertions.cjs +114 -0
- package/dist/testing/assertions.mjs +109 -0
- package/dist/testing/assertions.mjs.map +1 -0
- package/dist/testing/index.cjs +14 -0
- package/dist/testing/index.d.cts +6 -0
- package/dist/testing/index.d.mts +6 -0
- package/dist/testing/index.mjs +7 -0
- package/dist/testing/types.cjs +15 -0
- package/dist/testing/types.d.cts +90 -0
- package/dist/testing/types.d.cts.map +1 -0
- package/dist/testing/types.d.mts +90 -0
- package/dist/testing/types.d.mts.map +1 -0
- package/dist/testing/types.mjs +16 -0
- package/dist/testing/types.mjs.map +1 -0
- package/package.json +18 -3
- package/src/ColdStorage.ts +136 -0
- package/src/DocumentManager.ts +550 -190
- package/src/Errors.ts +114 -0
- package/src/HotStorage.ts +239 -0
- package/src/Metrics.ts +187 -0
- package/src/MimicAuthService.ts +126 -64
- package/src/MimicClusterServerEngine.ts +946 -0
- package/src/MimicServer.ts +448 -195
- package/src/MimicServerEngine.ts +276 -0
- package/src/PresenceManager.ts +169 -240
- package/src/Protocol.ts +350 -0
- package/src/Types.ts +231 -0
- package/src/index.ts +57 -23
- package/src/testing/ColdStorageTestSuite.ts +589 -0
- package/src/testing/FailingStorage.ts +286 -0
- package/src/testing/HotStorageTestSuite.ts +762 -0
- package/src/testing/StorageIntegrationTestSuite.ts +504 -0
- package/src/testing/assertions.ts +181 -0
- package/src/testing/index.ts +83 -0
- package/src/testing/types.ts +100 -0
- package/tests/ColdStorage.test.ts +24 -0
- package/tests/DocumentManager.test.ts +158 -287
- package/tests/HotStorage.test.ts +24 -0
- package/tests/MimicAuthService.test.ts +102 -134
- package/tests/MimicClusterServerEngine.test.ts +587 -0
- package/tests/MimicServer.test.ts +90 -226
- package/tests/MimicServerEngine.test.ts +521 -0
- package/tests/PresenceManager.test.ts +22 -63
- package/tests/Protocol.test.ts +190 -0
- package/tests/StorageIntegration.test.ts +259 -0
- package/tsconfig.json +1 -1
- package/tsdown.config.ts +1 -1
- package/dist/DocumentProtocol.cjs +0 -94
- package/dist/DocumentProtocol.d.cts +0 -113
- package/dist/DocumentProtocol.d.cts.map +0 -1
- package/dist/DocumentProtocol.d.mts +0 -113
- package/dist/DocumentProtocol.d.mts.map +0 -1
- package/dist/DocumentProtocol.mjs +0 -89
- package/dist/DocumentProtocol.mjs.map +0 -1
- package/dist/MimicConfig.cjs +0 -60
- package/dist/MimicConfig.d.cts +0 -141
- package/dist/MimicConfig.d.cts.map +0 -1
- package/dist/MimicConfig.d.mts +0 -141
- package/dist/MimicConfig.d.mts.map +0 -1
- package/dist/MimicConfig.mjs +0 -50
- package/dist/MimicConfig.mjs.map +0 -1
- package/dist/MimicDataStorage.cjs +0 -83
- package/dist/MimicDataStorage.d.cts +0 -113
- package/dist/MimicDataStorage.d.cts.map +0 -1
- package/dist/MimicDataStorage.d.mts +0 -113
- package/dist/MimicDataStorage.d.mts.map +0 -1
- package/dist/MimicDataStorage.mjs +0 -74
- package/dist/MimicDataStorage.mjs.map +0 -1
- package/dist/WebSocketHandler.cjs +0 -365
- package/dist/WebSocketHandler.d.cts +0 -34
- package/dist/WebSocketHandler.d.cts.map +0 -1
- package/dist/WebSocketHandler.d.mts +0 -34
- package/dist/WebSocketHandler.d.mts.map +0 -1
- package/dist/WebSocketHandler.mjs +0 -355
- package/dist/WebSocketHandler.mjs.map +0 -1
- package/dist/auth/NoAuth.cjs +0 -43
- package/dist/auth/NoAuth.d.cts +0 -22
- package/dist/auth/NoAuth.d.cts.map +0 -1
- package/dist/auth/NoAuth.d.mts +0 -22
- package/dist/auth/NoAuth.d.mts.map +0 -1
- package/dist/auth/NoAuth.mjs +0 -36
- package/dist/auth/NoAuth.mjs.map +0 -1
- package/dist/errors.cjs +0 -74
- package/dist/errors.d.cts +0 -89
- package/dist/errors.d.cts.map +0 -1
- package/dist/errors.d.mts +0 -89
- package/dist/errors.d.mts.map +0 -1
- package/dist/errors.mjs +0 -67
- package/dist/errors.mjs.map +0 -1
- package/dist/storage/InMemoryDataStorage.cjs +0 -57
- package/dist/storage/InMemoryDataStorage.d.cts +0 -19
- package/dist/storage/InMemoryDataStorage.d.cts.map +0 -1
- package/dist/storage/InMemoryDataStorage.d.mts +0 -19
- package/dist/storage/InMemoryDataStorage.d.mts.map +0 -1
- package/dist/storage/InMemoryDataStorage.mjs +0 -48
- package/dist/storage/InMemoryDataStorage.mjs.map +0 -1
- package/src/DocumentProtocol.ts +0 -112
- package/src/MimicConfig.ts +0 -211
- package/src/MimicDataStorage.ts +0 -157
- package/src/WebSocketHandler.ts +0 -735
- package/src/auth/NoAuth.ts +0 -46
- package/src/errors.ts +0 -113
- package/src/storage/InMemoryDataStorage.ts +0 -66
- package/tests/DocumentProtocol.test.ts +0 -113
- package/tests/InMemoryDataStorage.test.ts +0 -190
- package/tests/MimicConfig.test.ts +0 -290
- package/tests/MimicDataStorage.test.ts +0 -190
- package/tests/NoAuth.test.ts +0 -94
- package/tests/WebSocketHandler.test.ts +0 -321
- 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
|
+
});
|