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,12 +24,12 @@ 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")
await Bun.write( yield* Effect.promise(() =>
Bun.write(
pluginFile, pluginFile,
[ [
`const MARKER = ${JSON.stringify(marker)}`, `const MARKER = ${JSON.stringify(marker)}`,
@@ -35,42 +40,48 @@ async function bootstrapFixture() {
"})", "})",
"", "",
].join("\n"), ].join("\n"),
),
) )
await Bun.write( yield* Effect.promise(() =>
Bun.write(
path.join(dir, "opencode.json"), path.join(dir, "opencode.json"),
JSON.stringify({ JSON.stringify({
$schema: "https://opencode.ai/config.json", $schema: "https://opencode.ai/config.json",
plugin: [pathToFileURL(pluginFile).href], 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) it.live("InstanceStore.provide runs InstanceBootstrap before effect", () =>
}) Effect.gen(function* () {
const tmp = yield* bootstrapFixture
const store = yield* InstanceStore.Service
test("CLI bootstrap runs InstanceBootstrap before callback", async () => { yield* store.provide({ directory: tmp.directory }, Effect.succeed("ok"))
await using tmp = await bootstrapFixture()
await cliBootstrap(tmp.path, async () => "ok") expect(existsSync(tmp.marker)).toBe(true)
}),
)
expect(existsSync(tmp.extra)).toBe(true) it.live("CLI bootstrap runs InstanceBootstrap before callback", () =>
}) Effect.gen(function* () {
const tmp = yield* bootstrapFixture
test("InstanceRuntime.reloadInstance runs InstanceBootstrap", async () => { yield* Effect.promise(() => cliBootstrap(tmp.directory, async () => "ok"))
await using tmp = await bootstrapFixture()
await InstanceRuntime.reloadInstance({ directory: tmp.path }) expect(existsSync(tmp.marker)).toBe(true)
}),
)
expect(existsSync(tmp.extra)).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)
}),
)