MCPcopy
hub / github.com/Effect-TS/effect / testsuite

Function testsuite

packages/experimental/test/PersistedCache.test.ts:25–61  ·  view source on GitHub ↗
(storeId: "memory" | "kvs", layer: Layer.Layer<Persistence.ResultPersistence, unknown>)

Source from the content-addressed store, hash-verified

23
24describe("PersistedCache", () => {
25 const testsuite = (storeId: "memory" | "kvs", layer: Layer.Layer<Persistence.ResultPersistence, unknown>) =>
26 it.scoped(storeId, () =>
27 Effect.gen(function*() {
28 const persistence = yield* Persistence.ResultPersistence
29 const store = yield* persistence.make({ storeId: "users" })
30 let invocations = 0
31 let cache = yield* PersistedCache.make({
32 storeId: "users",
33 lookup: (req: TTLRequest) =>
34 Effect.sync(() => {
35 invocations++
36 return new User({ id: req.id, name: "John" })
37 }),
38 timeToLive: (_req, _exit) => 5000
39 })
40 const user = yield* cache.get(new TTLRequest({ id: 1 }))
41 assert.deepStrictEqual(user, new User({ id: 1, name: "John" }))
42 assert.deepStrictEqual(
43 yield* store.get(new TTLRequest({ id: 1 })),
44 Option.some(Exit.succeed(new User({ id: 1, name: "John" })))
45 )
46 assert.strictEqual(invocations, 1)
47 assert.deepStrictEqual(yield* cache.get(new TTLRequest({ id: 1 })), new User({ id: 1, name: "John" }))
48 assert.strictEqual(invocations, 1)
49
50 cache = yield* PersistedCache.make({
51 storeId: "users",
52 lookup: (req: TTLRequest) =>
53 Effect.sync(() => {
54 invocations++
55 return new User({ id: req.id, name: "John" })
56 }),
57 timeToLive: (_req, _exit) => 5000
58 })
59 assert.deepStrictEqual(yield* cache.get(new TTLRequest({ id: 1 })), new User({ id: 1, name: "John" }))
60 assert.strictEqual(invocations, 1)
61 }).pipe(Effect.provide(layer)))
62
63 testsuite("memory", Persistence.layerResultMemory)
64 testsuite("kvs", Persistence.layerResultKeyValueStore.pipe(Layer.provide(KeyValueStore.layerMemory)))

Callers 1

Calls 5

syncMethod · 0.80
pipeMethod · 0.65
makeMethod · 0.65
getMethod · 0.65
provideMethod · 0.65

Tested by

no test coverage detected