瀏覽代碼

test: use Effect test helper for run-service (#25048)

Kit Langton 4 月之前
父節點
當前提交
f384675c01
共有 1 個文件被更改,包括 39 次插入36 次删除
  1. 39 36
      packages/opencode/test/effect/run-service.test.ts

+ 39 - 36
packages/opencode/test/effect/run-service.test.ts

@@ -1,46 +1,49 @@
-import { expect, test } from "bun:test"
+import { expect } from "bun:test"
 import { Effect, Layer, Context } from "effect"
 import { makeRuntime } from "../../src/effect/run-service"
+import { it } from "../lib/effect"
 
 class Shared extends Context.Service<Shared, { readonly id: number }>()("@test/Shared") {}
 
-test("makeRuntime shares dependent layers through the shared memo map", async () => {
-  let n = 0
+it.live("makeRuntime shares dependent layers through the shared memo map", () =>
+  Effect.gen(function* () {
+    let n = 0
 
-  const shared = Layer.effect(
-    Shared,
-    Effect.sync(() => {
-      n += 1
-      return Shared.of({ id: n })
-    }),
-  )
+    const shared = Layer.effect(
+      Shared,
+      Effect.sync(() => {
+        n += 1
+        return Shared.of({ id: n })
+      }),
+    )
 
-  class One extends Context.Service<One, { readonly get: () => Effect.Effect<number> }>()("@test/One") {}
-  const one = Layer.effect(
-    One,
-    Effect.gen(function* () {
-      const svc = yield* Shared
-      return One.of({
-        get: Effect.fn("One.get")(() => Effect.succeed(svc.id)),
-      })
-    }),
-  ).pipe(Layer.provide(shared))
+    class One extends Context.Service<One, { readonly get: () => Effect.Effect<number> }>()("@test/One") {}
+    const one = Layer.effect(
+      One,
+      Effect.gen(function* () {
+        const svc = yield* Shared
+        return One.of({
+          get: Effect.fn("One.get")(() => Effect.succeed(svc.id)),
+        })
+      }),
+    ).pipe(Layer.provide(shared))
 
-  class Two extends Context.Service<Two, { readonly get: () => Effect.Effect<number> }>()("@test/Two") {}
-  const two = Layer.effect(
-    Two,
-    Effect.gen(function* () {
-      const svc = yield* Shared
-      return Two.of({
-        get: Effect.fn("Two.get")(() => Effect.succeed(svc.id)),
-      })
-    }),
-  ).pipe(Layer.provide(shared))
+    class Two extends Context.Service<Two, { readonly get: () => Effect.Effect<number> }>()("@test/Two") {}
+    const two = Layer.effect(
+      Two,
+      Effect.gen(function* () {
+        const svc = yield* Shared
+        return Two.of({
+          get: Effect.fn("Two.get")(() => Effect.succeed(svc.id)),
+        })
+      }),
+    ).pipe(Layer.provide(shared))
 
-  const { runPromise: runOne } = makeRuntime(One, one)
-  const { runPromise: runTwo } = makeRuntime(Two, two)
+    const { runPromise: runOne } = makeRuntime(One, one)
+    const { runPromise: runTwo } = makeRuntime(Two, two)
 
-  expect(await runOne((svc) => svc.get())).toBe(1)
-  expect(await runTwo((svc) => svc.get())).toBe(1)
-  expect(n).toBe(1)
-})
+    expect(yield* Effect.promise(() => runOne((svc) => svc.get()))).toBe(1)
+    expect(yield* Effect.promise(() => runTwo((svc) => svc.get()))).toBe(1)
+    expect(n).toBe(1)
+  }),
+)