test: migrate instance bootstrap to Effect runner (#27144)

This commit is contained in:
Kit Langton
2026-05-12 20:34:12 +00:00
committed by GitHub
parent ca28dd02ec
commit 4cf088ae84
@@ -1,11 +1,16 @@
import { afterEach, expect, test } from "bun:test" import { afterEach, expect } from "bun:test"
import { existsSync } from "node:fs" import { existsSync } from "node:fs"
import path from "node:path" import path from "node:path"
import { pathToFileURL } from "node:url" import { pathToFileURL } from "node:url"
import { CrossSpawnSpawner } from "@opencode-ai/core/cross-spawn-spawner"
import { Effect, Layer } from "effect"
import { bootstrap as cliBootstrap } from "../../src/cli/bootstrap" import { bootstrap as cliBootstrap } from "../../src/cli/bootstrap"
import { WithInstance } from "../../src/project/with-instance" import { InstanceLayer } from "../../src/project/instance-layer"
import { InstanceRuntime } from "../../src/project/instance-runtime" import { InstanceStore } from "../../src/project/instance-store"
import { disposeAllInstances, tmpdir } from "../fixture/fixture" import { disposeAllInstances, tmpdirScoped } from "../fixture/fixture"
import { testEffect } from "../lib/effect"
const it = testEffect(Layer.mergeAll(InstanceLayer.layer, CrossSpawnSpawner.defaultLayer))
// InstanceBootstrap must run before any code touches the instance — // InstanceBootstrap must run before any code touches the instance —
// originally tracked by PRs #25389 and #25449, now a permanent // originally tracked by PRs #25389 and #25449, now a permanent
@@ -19,58 +24,64 @@ afterEach(async () => {
await disposeAllInstances() await disposeAllInstances()
}) })
async function bootstrapFixture() { const bootstrapFixture = Effect.gen(function* () {
return tmpdir({ const dir = yield* tmpdirScoped({ git: true })
init: async (dir) => { const marker = path.join(dir, "config-hook-fired")
const marker = path.join(dir, "config-hook-fired") const pluginFile = path.join(dir, "plugin.ts")
const pluginFile = path.join(dir, "plugin.ts") yield* Effect.promise(() =>
await Bun.write( Bun.write(
pluginFile, pluginFile,
[ [
`const MARKER = ${JSON.stringify(marker)}`, `const MARKER = ${JSON.stringify(marker)}`,
"export default async () => ({", "export default async () => ({",
" config: async () => {", " config: async () => {",
' await Bun.write(MARKER, "ran")', ' await Bun.write(MARKER, "ran")',
" },", " },",
"})", "})",
"", "",
].join("\n"), ].join("\n"),
) ),
await Bun.write( )
path.join(dir, "opencode.json"), yield* Effect.promise(() =>
JSON.stringify({ Bun.write(
$schema: "https://opencode.ai/config.json", path.join(dir, "opencode.json"),
plugin: [pathToFileURL(pluginFile).href], JSON.stringify({
}), $schema: "https://opencode.ai/config.json",
) plugin: [pathToFileURL(pluginFile).href],
return marker }),
}, ),
}) )
} return { directory: dir, marker }
test("WithInstance.provide runs InstanceBootstrap before fn", async () => {
await using tmp = await bootstrapFixture()
await WithInstance.provide({
directory: tmp.path,
fn: async () => "ok",
})
expect(existsSync(tmp.extra)).toBe(true)
}) })
test("CLI bootstrap runs InstanceBootstrap before callback", async () => { it.live("InstanceStore.provide runs InstanceBootstrap before effect", () =>
await using tmp = await bootstrapFixture() Effect.gen(function* () {
const tmp = yield* bootstrapFixture
const store = yield* InstanceStore.Service
await cliBootstrap(tmp.path, async () => "ok") yield* store.provide({ directory: tmp.directory }, Effect.succeed("ok"))
expect(existsSync(tmp.extra)).toBe(true) expect(existsSync(tmp.marker)).toBe(true)
}) }),
)
test("InstanceRuntime.reloadInstance runs InstanceBootstrap", async () => { it.live("CLI bootstrap runs InstanceBootstrap before callback", () =>
await using tmp = await bootstrapFixture() Effect.gen(function* () {
const tmp = yield* bootstrapFixture
await InstanceRuntime.reloadInstance({ directory: tmp.path }) yield* Effect.promise(() => cliBootstrap(tmp.directory, async () => "ok"))
expect(existsSync(tmp.extra)).toBe(true) expect(existsSync(tmp.marker)).toBe(true)
}) }),
)
it.live("InstanceStore.reload runs InstanceBootstrap", () =>
Effect.gen(function* () {
const tmp = yield* bootstrapFixture
const store = yield* InstanceStore.Service
yield* store.reload({ directory: tmp.directory })
expect(existsSync(tmp.marker)).toBe(true)
}),
)