@voidhash/mimic-effect 0.0.4 → 0.0.6

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.
@@ -352,6 +352,21 @@ describe("DocumentManager", () => {
352
352
  );
353
353
  };
354
354
 
355
+ const makeTestLayerWithInitialFn = (
356
+ initialFn: (ctx: { documentId: string }) => Effect.Effect<{ title?: string; count?: number }>
357
+ ) => {
358
+ const configLayer = MimicConfig.layer({
359
+ schema: TestSchema,
360
+ maxTransactionHistory: 100,
361
+ initial: initialFn,
362
+ });
363
+
364
+ return DocumentManager.layer.pipe(
365
+ Layer.provide(configLayer),
366
+ Layer.provide(InMemoryDataStorage.layer)
367
+ );
368
+ };
369
+
355
370
  it("should use initial state for new documents", async () => {
356
371
  const result = await Effect.runPromise(
357
372
  Effect.gen(function* () {
@@ -397,5 +412,53 @@ describe("DocumentManager", () => {
397
412
  // count should still be 42 since we only modified title
398
413
  expect((result.state as any).count).toBe(42);
399
414
  });
415
+
416
+ it("should use initial state function with documentId for new documents", async () => {
417
+ const result = await Effect.runPromise(
418
+ Effect.gen(function* () {
419
+ const manager = yield* DocumentManager.DocumentManagerTag;
420
+ return yield* manager.getSnapshot("my-special-doc");
421
+ }).pipe(Effect.provide(makeTestLayerWithInitialFn(
422
+ ({ documentId }) => Effect.succeed({ title: `Doc: ${documentId}`, count: documentId.length })
423
+ )))
424
+ );
425
+
426
+ expect(result.type).toBe("snapshot");
427
+ expect(result.version).toBe(0);
428
+ expect(result.state).toEqual({ title: "Doc: my-special-doc", count: 14 });
429
+ });
430
+
431
+ it("should call initial function with different documentIds", async () => {
432
+ const layer = makeTestLayerWithInitialFn(
433
+ ({ documentId }) => Effect.succeed({ title: documentId, count: documentId.length })
434
+ );
435
+
436
+ const result = await Effect.runPromise(
437
+ Effect.gen(function* () {
438
+ const manager = yield* DocumentManager.DocumentManagerTag;
439
+ const snap1 = yield* manager.getSnapshot("short");
440
+ const snap2 = yield* manager.getSnapshot("longer-document-id");
441
+ return { snap1, snap2 };
442
+ }).pipe(Effect.provide(layer))
443
+ );
444
+
445
+ expect(result.snap1.state).toEqual({ title: "short", count: 5 });
446
+ expect(result.snap2.state).toEqual({ title: "longer-document-id", count: 18 });
447
+ });
448
+
449
+ it("should apply defaults to initial function result", async () => {
450
+ const result = await Effect.runPromise(
451
+ Effect.gen(function* () {
452
+ const manager = yield* DocumentManager.DocumentManagerTag;
453
+ return yield* manager.getSnapshot("test-doc");
454
+ }).pipe(Effect.provide(makeTestLayerWithInitialFn(
455
+ ({ documentId }) => Effect.succeed({ title: documentId }) // count omitted
456
+ )))
457
+ );
458
+
459
+ expect(result.type).toBe("snapshot");
460
+ // count should be 0 (default)
461
+ expect(result.state).toEqual({ title: "test-doc", count: 0 });
462
+ });
400
463
  });
401
464
  });
@@ -182,25 +182,50 @@ describe("MimicConfig", () => {
182
182
  expect(config.initial).toBeUndefined();
183
183
  });
184
184
 
185
- it("should accept initial state option", () => {
185
+ it("should accept initial state object and convert to function", async () => {
186
186
  const config = MimicConfig.make({
187
187
  schema: TestSchema,
188
188
  initial: { title: "My Document", count: 42 },
189
189
  });
190
190
 
191
- expect(config.initial).toEqual({ title: "My Document", count: 42 });
191
+ expect(typeof config.initial).toBe("function");
192
+ // The function should return the initial state when called
193
+ const result = await Effect.runPromise(config.initial!({ documentId: "test-doc" }));
194
+ expect(result).toEqual({ title: "My Document", count: 42 });
192
195
  });
193
196
 
194
- it("should apply defaults for omitted fields in initial state", () => {
197
+ it("should apply defaults for omitted fields in initial state object", async () => {
195
198
  const config = MimicConfig.make({
196
199
  schema: TestSchema,
197
200
  initial: { title: "My Document" }, // count has default of 0
198
201
  });
199
202
 
200
- expect(config.initial).toEqual({ title: "My Document", count: 0 });
203
+ const result = await Effect.runPromise(config.initial!({ documentId: "test-doc" }));
204
+ expect(result).toEqual({ title: "My Document", count: 0 });
201
205
  });
202
206
 
203
- it("should provide initial state through layer", async () => {
207
+ it("should accept initial state function", async () => {
208
+ const config = MimicConfig.make({
209
+ schema: TestSchema,
210
+ initial: ({ documentId }) => Effect.succeed({ title: `Doc ${documentId}`, count: 123 }),
211
+ });
212
+
213
+ expect(typeof config.initial).toBe("function");
214
+ const result = await Effect.runPromise(config.initial!({ documentId: "my-doc-id" }));
215
+ expect(result).toEqual({ title: "Doc my-doc-id", count: 123 });
216
+ });
217
+
218
+ it("should apply defaults to initial state function result", async () => {
219
+ const config = MimicConfig.make({
220
+ schema: TestSchema,
221
+ initial: ({ documentId }) => Effect.succeed({ title: documentId }), // count omitted
222
+ });
223
+
224
+ const result = await Effect.runPromise(config.initial!({ documentId: "test" }));
225
+ expect(result).toEqual({ title: "test", count: 0 });
226
+ });
227
+
228
+ it("should provide initial function through layer", async () => {
204
229
  const testLayer = MimicConfig.layer({
205
230
  schema: TestSchema,
206
231
  initial: { title: "From Layer", count: 100 },
@@ -209,14 +234,14 @@ describe("MimicConfig", () => {
209
234
  const result = await Effect.runPromise(
210
235
  Effect.gen(function* () {
211
236
  const config = yield* MimicConfig.MimicServerConfigTag;
212
- return config.initial;
237
+ return yield* config.initial!({ documentId: "layer-doc" });
213
238
  }).pipe(Effect.provide(testLayer))
214
239
  );
215
240
 
216
241
  expect(result).toEqual({ title: "From Layer", count: 100 });
217
242
  });
218
243
 
219
- it("should work with schema that has required fields without defaults", () => {
244
+ it("should work with schema that has required fields without defaults", async () => {
220
245
  const SchemaWithRequired = Primitive.Struct({
221
246
  name: Primitive.String().required(),
222
247
  optional: Primitive.String().default("default"),
@@ -227,10 +252,11 @@ describe("MimicConfig", () => {
227
252
  initial: { name: "Required Name" },
228
253
  });
229
254
 
230
- expect(config.initial).toEqual({ name: "Required Name", optional: "default" });
255
+ const result = await Effect.runPromise(config.initial!({ documentId: "test" }));
256
+ expect(result).toEqual({ name: "Required Name", optional: "default" });
231
257
  });
232
258
 
233
- it("should work with all options including initial", () => {
259
+ it("should work with all options including initial object", async () => {
234
260
  const config = MimicConfig.make({
235
261
  schema: TestSchema,
236
262
  maxIdleTime: "10 minutes",
@@ -241,7 +267,24 @@ describe("MimicConfig", () => {
241
267
  expect(config.schema).toBe(TestSchema);
242
268
  expect(Duration.toMillis(config.maxIdleTime)).toBe(10 * 60 * 1000);
243
269
  expect(config.maxTransactionHistory).toBe(500);
244
- expect(config.initial).toEqual({ title: "Full Options", count: 999 });
270
+ const result = await Effect.runPromise(config.initial!({ documentId: "test" }));
271
+ expect(result).toEqual({ title: "Full Options", count: 999 });
272
+ });
273
+
274
+ it("should work with initial function that uses documentId", async () => {
275
+ const config = MimicConfig.make({
276
+ schema: TestSchema,
277
+ initial: ({ documentId }) => Effect.succeed({
278
+ title: `Document: ${documentId}`,
279
+ count: documentId.length,
280
+ }),
281
+ });
282
+
283
+ const result1 = await Effect.runPromise(config.initial!({ documentId: "short" }));
284
+ expect(result1).toEqual({ title: "Document: short", count: 5 });
285
+
286
+ const result2 = await Effect.runPromise(config.initial!({ documentId: "longer-id" }));
287
+ expect(result2).toEqual({ title: "Document: longer-id", count: 9 });
245
288
  });
246
289
  });
247
290
  });