test: use Effect test helper for app runtime logger (#25049)

This commit is contained in:
Kit Langton
2026-04-30 12:52:29 -04:00
committed by GitHub
parent ce63ca4d7a
commit 92e80b4660

View File

@@ -1,12 +1,15 @@
import { expect, test } from "bun:test" import { expect } from "bun:test"
import { Context, Effect, Layer, Logger } from "effect" import { Context, Effect, Layer, Logger } from "effect"
import { CrossSpawnSpawner } from "@opencode-ai/core/cross-spawn-spawner"
import { AppRuntime } from "../../src/effect/app-runtime" import { AppRuntime } from "../../src/effect/app-runtime"
import { EffectBridge } from "@/effect/bridge" import { EffectBridge } from "@/effect/bridge"
import { InstanceRef } from "../../src/effect/instance-ref" import { InstanceRef } from "../../src/effect/instance-ref"
import * as EffectLogger from "@opencode-ai/core/effect/logger" import * as EffectLogger from "@opencode-ai/core/effect/logger"
import { makeRuntime } from "../../src/effect/run-service" import { makeRuntime } from "../../src/effect/run-service"
import { Instance } from "../../src/project/instance" import { provideInstance, tmpdirScoped } from "../fixture/fixture"
import { tmpdir } from "../fixture/fixture" import { testEffect } from "../lib/effect"
const it = testEffect(CrossSpawnSpawner.defaultLayer)
function check(loggers: ReadonlySet<Logger.Logger<unknown, any>>) { function check(loggers: ReadonlySet<Logger.Logger<unknown, any>>) {
return { return {
@@ -17,56 +20,58 @@ function check(loggers: ReadonlySet<Logger.Logger<unknown, any>>) {
} }
} }
test("makeRuntime installs EffectLogger through Observability.layer", async () => { it.live("makeRuntime installs EffectLogger through Observability.layer", () =>
class Dummy extends Context.Service<Dummy, { readonly current: () => Effect.Effect<ReturnType<typeof check>> }>()( Effect.gen(function* () {
"@test/Dummy", class Dummy extends Context.Service<Dummy, { readonly current: () => Effect.Effect<ReturnType<typeof check>> }>()(
) {} "@test/Dummy",
) {}
const layer = Layer.effect( const layer = Layer.effect(
Dummy, Dummy,
Effect.gen(function* () { Effect.gen(function* () {
return Dummy.of({ return Dummy.of({
current: () => Effect.map(Effect.service(Logger.CurrentLoggers), check), current: () => Effect.map(Effect.service(Logger.CurrentLoggers), check),
}) })
}), }),
) )
const rt = makeRuntime(Dummy, layer) const current = yield* Effect.promise(() => makeRuntime(Dummy, layer).runPromise((svc) => svc.current()))
const current = await rt.runPromise((svc) => svc.current())
expect(current.effectLogger).toBe(true) expect(current.effectLogger).toBe(true)
expect(current.defaultLogger).toBe(false) expect(current.defaultLogger).toBe(false)
}) }),
)
test("AppRuntime also installs EffectLogger through Observability.layer", async () => { it.live("AppRuntime also installs EffectLogger through Observability.layer", () =>
const current = await AppRuntime.runPromise(Effect.map(Effect.service(Logger.CurrentLoggers), check)) Effect.gen(function* () {
const current = yield* Effect.promise(() =>
AppRuntime.runPromise(Effect.map(Effect.service(Logger.CurrentLoggers), check)),
)
expect(current.effectLogger).toBe(true) expect(current.effectLogger).toBe(true)
expect(current.defaultLogger).toBe(false) expect(current.defaultLogger).toBe(false)
}) }),
)
test("AppRuntime attaches InstanceRef from ALS", async () => { it.live("AppRuntime attaches InstanceRef from ALS", () =>
await using tmp = await tmpdir({ git: true }) Effect.gen(function* () {
const dir = yield* tmpdirScoped({ git: true })
const dir = await Instance.provide({ const current = yield* Effect.promise(() =>
directory: tmp.path,
fn: () =>
AppRuntime.runPromise( AppRuntime.runPromise(
Effect.gen(function* () { Effect.gen(function* () {
return (yield* InstanceRef)?.directory return (yield* InstanceRef)?.directory
}), }),
), ),
}) ).pipe(provideInstance(dir))
expect(dir).toBe(tmp.path) expect(current).toBe(dir)
}) }),
)
test("EffectBridge preserves logger and instance context across async boundaries", async () => { it.live("EffectBridge preserves logger and instance context across async boundaries", () =>
await using tmp = await tmpdir({ git: true }) Effect.gen(function* () {
const dir = yield* tmpdirScoped({ git: true })
const result = await Instance.provide({ const result = yield* Effect.promise(() =>
directory: tmp.path,
fn: () =>
AppRuntime.runPromise( AppRuntime.runPromise(
Effect.gen(function* () { Effect.gen(function* () {
const bridge = yield* EffectBridge.make() const bridge = yield* EffectBridge.make()
@@ -84,9 +89,10 @@ test("EffectBridge preserves logger and instance context across async boundaries
) )
}), }),
), ),
}) ).pipe(provideInstance(dir))
expect(result.directory).toBe(tmp.path) expect(result.directory).toBe(dir)
expect(result.effectLogger).toBe(true) expect(result.effectLogger).toBe(true)
expect(result.defaultLogger).toBe(false) expect(result.defaultLogger).toBe(false)
}) }),
)