test: migrate effect cmd ALS test (#27173)

This commit is contained in:
Kit Langton
2026-05-12 19:48:17 -04:00
committed by GitHub
parent 3301fad8cd
commit 72ce24c200

View File

@@ -1,8 +1,13 @@
import { afterEach, expect, test } from "bun:test" import { afterEach, expect } from "bun:test"
import { AppFileSystem } from "@opencode-ai/core/filesystem"
import { Effect } from "effect" import { Effect } from "effect"
import fs from "fs/promises" import { fileURLToPath } from "url"
import { InstanceRef } from "../../src/effect/instance-ref"
import { Instance } from "../../src/project/instance" import { Instance } from "../../src/project/instance"
import { disposeAllInstances, provideTestInstance, tmpdir } from "../fixture/fixture" import { disposeAllInstances, TestInstance } from "../fixture/fixture"
import { testEffect } from "../lib/effect"
const it = testEffect(AppFileSystem.defaultLayer)
afterEach(async () => { afterEach(async () => {
await disposeAllInstances() await disposeAllInstances()
@@ -14,35 +19,40 @@ afterEach(async () => {
// has lost the outer InstanceRef. Services that read `InstanceState.context` // has lost the outer InstanceRef. Services that read `InstanceState.context`
// then fall back to `Instance.current` ALS, which must be installed at the JS // then fall back to `Instance.current` ALS, which must be installed at the JS
// callback boundary (Node ALS persists across awaits, Effect's fiber context // callback boundary (Node ALS persists across awaits, Effect's fiber context
// does not). `provideTestInstance` mirrors effectCmd's load + ALS-restore wrap. // does not). `it.instance` provides the loaded InstanceRef; the explicit
// Instance.restore mirrors effectCmd's load + ALS-restore wrap.
// Pins effect-cmd.ts directly: the pattern test below exercises the load + // Pins effect-cmd.ts directly: the pattern test below exercises the load +
// Instance.restore + dispose triple via the shared `provideTestInstance` fixture, // Instance.restore boundary via the shared `it.instance` fixture,
// so a regression that removed `Instance.restore` from effect-cmd.ts wouldn't // so a regression that removed `Instance.restore` from effect-cmd.ts wouldn't
// fail it. This grep guards the actual production callsite. // fail it. This grep guards the actual production callsite.
test("effect-cmd.ts wraps the handler body in Instance.restore", async () => { it.live("effect-cmd.ts wraps the handler body in Instance.restore", () =>
const source = await fs.readFile(new URL("../../src/cli/effect-cmd.ts", import.meta.url), "utf8") Effect.gen(function* () {
expect(source).toContain("Instance.restore(ctx") const fs = yield* AppFileSystem.Service
}) const source = yield* fs.readFileString(fileURLToPath(new URL("../../src/cli/effect-cmd.ts", import.meta.url)))
expect(source).toContain("Instance.restore(ctx")
}),
)
test("Instance.current reachable from inner runPromise inside Effect.promise(async)", async () => { it.instance(
await using dir = await tmpdir({ git: true }) "Instance.current reachable after await inside restored Effect.promise(async)",
await provideTestInstance({ () =>
directory: dir.path, Effect.gen(function* () {
fn: () => const test = yield* TestInstance
Effect.runPromise( const ctx = yield* InstanceRef
Effect.promise(async () => { if (!ctx) throw new Error("InstanceRef not provided")
await new Promise((r) => setTimeout(r, 5))
const current = await Effect.runPromise( const current = yield* Effect.promise(() =>
Effect.sync(() => { Instance.restore(ctx, async () => {
try { await Promise.resolve()
return Instance.current try {
} catch { return Instance.current
return undefined } catch {
} return undefined
}), }
)
expect(current?.directory).toBe(dir.path)
}), }),
), )
})
}) expect(current?.directory).toBe(test.directory)
}),
{ git: true },
)