+21

![opencode-agent[bot] <219766164+opencode-agent[bot]@users.noreply.github.com>](/assets/img/avatar_default.png)



![opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com>](/assets/img/avatar_default.png)



James Long
Brendan Allan
Kit Langton
opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com>
Affan Ali
affanali2k3
Frank
opencode-agent[bot] <219766164+opencode-agent[bot]@users.noreply.github.com>
𝓛𝓲𝓽𝓽𝓵𝓮 𝓕𝓻𝓪𝓷𝓴
Aiden Cline
Jay V
Dax Raad
Aarav Sareen
OpeOginni
Luke Parker
Ben Guthrie
Dax
Filip
Max Anderson
Brendan Allan
Jack
Shoubhit Dash
Dustin Deus
starptech
Aiden Cline
usrnk1
Jay
runvip
opencode
Julian Coy
Vladimir Glafirov
8c94e9005f
Co-authored-by: Brendan Allan <14191578+Brendonovich@users.noreply.github.com> Co-authored-by: Kit Langton <kit.langton@gmail.com> Co-authored-by: opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com> Co-authored-by: Affan Ali <93028901+affanali2k3@users.noreply.github.com> Co-authored-by: affanali2k3 <affanalikhanxx@gmail.com> Co-authored-by: Frank <frank@anoma.ly> Co-authored-by: opencode-agent[bot] <219766164+opencode-agent[bot]@users.noreply.github.com> Co-authored-by: 𝓛𝓲𝓽𝓽𝓵𝓮 𝓕𝓻𝓪𝓷𝓴 <little-frank@opencord.local> Co-authored-by: Aiden Cline <63023139+rekram1-node@users.noreply.github.com> Co-authored-by: Jay V <air@live.ca> Co-authored-by: Dax Raad <d@ironbay.co> Co-authored-by: Aarav Sareen <96787824+arvsrn@users.noreply.github.com> Co-authored-by: OpeOginni <107570612+OpeOginni@users.noreply.github.com> Co-authored-by: Luke Parker <10430890+Hona@users.noreply.github.com> Co-authored-by: Ben Guthrie <benjee.012@gmail.com> Co-authored-by: Dax <mail@thdxr.com> Co-authored-by: Filip <34747899+neriousy@users.noreply.github.com> Co-authored-by: Max Anderson <max.a.anderson95@gmail.com> Co-authored-by: Brendan Allan <git@brendonovich.dev> Co-authored-by: Jack <jack@anoma.ly> Co-authored-by: Shoubhit Dash <shoubhit2005@gmail.com> Co-authored-by: Dustin Deus <deusdustin@gmail.com> Co-authored-by: starptech <starptech@starptechs-MBP.fritz.box> Co-authored-by: Aiden Cline <aidenpcline@gmail.com> Co-authored-by: usrnk1 <7547651+usrnk1@users.noreply.github.com> Co-authored-by: Jay <53023+jayair@users.noreply.github.com> Co-authored-by: runvip <164729189+runvip@users.noreply.github.com> Co-authored-by: opencode <opencode@sst.dev> Co-authored-by: Julian Coy <julian@ex-machina.co> Co-authored-by: Vladimir Glafirov <vglafirov@gitlab.com>
229 lines
8.3 KiB
TypeScript
229 lines
8.3 KiB
TypeScript
import { $ } from "bun"
|
|
import { ConfigV1 } from "@opencode-ai/core/v1/config/config"
|
|
import * as fs from "fs/promises"
|
|
import os from "os"
|
|
import path from "path"
|
|
import { Effect, Context, Layer } from "effect"
|
|
import type * as PlatformError from "effect/PlatformError"
|
|
import type * as Scope from "effect/Scope"
|
|
import { CrossSpawnSpawner } from "@opencode-ai/core/cross-spawn-spawner"
|
|
import { AppNodeBuilder } from "@opencode-ai/core/effect/app-node-builder"
|
|
import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process"
|
|
import type { Config } from "@/config/config"
|
|
import { LayerNode } from "@opencode-ai/core/effect/layer-node"
|
|
import { InstanceRef } from "../../src/effect/instance-ref"
|
|
import { InstanceBootstrap } from "../../src/project/bootstrap-service"
|
|
import type { InstanceContext } from "../../src/project/instance-context"
|
|
import { InstanceRuntime } from "../../src/project/instance-runtime"
|
|
import { InstanceStore } from "../../src/project/instance-store"
|
|
import { TestLLMServer } from "../lib/llm-server"
|
|
|
|
const noopBootstrap = Layer.succeed(InstanceBootstrap.Service, InstanceBootstrap.Service.of({ run: Effect.void }))
|
|
export const testInstanceStoreLayer = LayerNode.compile(InstanceStore.node, [
|
|
[InstanceStore.bootstrapNode, noopBootstrap],
|
|
])
|
|
|
|
export async function provideTestInstance<R>(input: {
|
|
directory: string
|
|
init?: Effect.Effect<void>
|
|
fn: (ctx: InstanceContext) => R
|
|
}) {
|
|
const ctx = await InstanceRuntime.load({ directory: input.directory })
|
|
try {
|
|
if (input.init) await Effect.runPromise(input.init.pipe(Effect.provideService(InstanceRef, ctx)))
|
|
return await input.fn(ctx)
|
|
} finally {
|
|
await InstanceRuntime.disposeInstance(ctx)
|
|
}
|
|
}
|
|
|
|
export async function withTestInstance<R>(input: { directory: string; fn: (ctx: InstanceContext) => R }) {
|
|
return input.fn(await InstanceRuntime.load({ directory: input.directory }))
|
|
}
|
|
|
|
export async function reloadTestInstance(input: { directory: string }) {
|
|
return InstanceRuntime.reloadInstance(input)
|
|
}
|
|
|
|
export async function disposeAllInstances() {
|
|
await InstanceRuntime.disposeAllInstances()
|
|
}
|
|
|
|
// Strip null bytes from paths (defensive fix for CI environment issues)
|
|
function sanitizePath(p: string): string {
|
|
return p.replace(/\0/g, "")
|
|
}
|
|
|
|
function exists(dir: string) {
|
|
return fs
|
|
.stat(dir)
|
|
.then(() => true)
|
|
.catch(() => false)
|
|
}
|
|
|
|
function clean(dir: string) {
|
|
return fs.rm(dir, {
|
|
recursive: true,
|
|
force: true,
|
|
maxRetries: 5,
|
|
retryDelay: 100,
|
|
})
|
|
}
|
|
|
|
async function stop(dir: string) {
|
|
if (!(await exists(dir))) return
|
|
await $`git fsmonitor--daemon stop`.cwd(dir).quiet().nothrow()
|
|
}
|
|
|
|
type TmpDirOptions<T> = {
|
|
git?: boolean
|
|
config?: Partial<ConfigV1.Info>
|
|
init?: (dir: string) => Promise<T>
|
|
dispose?: (dir: string) => Promise<T>
|
|
}
|
|
export async function tmpdir<T>(options?: TmpDirOptions<T>) {
|
|
const dirpath = sanitizePath(path.join(os.tmpdir(), "opencode-test-" + Math.random().toString(36).slice(2)))
|
|
await fs.mkdir(dirpath, { recursive: true })
|
|
if (options?.git) {
|
|
await $`git init`.cwd(dirpath).quiet()
|
|
await $`git config core.fsmonitor false`.cwd(dirpath).quiet()
|
|
await $`git config commit.gpgsign false`.cwd(dirpath).quiet()
|
|
await $`git config user.email "test@opencode.test"`.cwd(dirpath).quiet()
|
|
await $`git config user.name "Test"`.cwd(dirpath).quiet()
|
|
await $`git commit --allow-empty -m "root commit ${dirpath}"`.cwd(dirpath).quiet()
|
|
}
|
|
if (options?.config) {
|
|
await Bun.write(
|
|
path.join(dirpath, "opencode.json"),
|
|
JSON.stringify({
|
|
$schema: "https://opencode.ai/config.json",
|
|
...options.config,
|
|
}),
|
|
)
|
|
}
|
|
const realpath = sanitizePath(await fs.realpath(dirpath))
|
|
const extra = await options?.init?.(realpath)
|
|
const result = {
|
|
[Symbol.asyncDispose]: async () => {
|
|
try {
|
|
await options?.dispose?.(realpath)
|
|
} finally {
|
|
if (options?.git) await stop(realpath).catch(() => undefined)
|
|
await clean(realpath).catch(() => undefined)
|
|
}
|
|
},
|
|
path: realpath,
|
|
extra: extra as T,
|
|
}
|
|
return result
|
|
}
|
|
|
|
/** Effectful scoped tmpdir. Cleaned up when the scope closes. Make sure these stay in sync */
|
|
export function tmpdirScoped<E = never, R = never>(options?: {
|
|
git?: boolean
|
|
config?: Partial<ConfigV1.Info> | (() => Partial<ConfigV1.Info>)
|
|
init?: (directory: string) => Effect.Effect<void, E, R>
|
|
}) {
|
|
return Effect.gen(function* () {
|
|
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner
|
|
const dirpath = sanitizePath(path.join(os.tmpdir(), "opencode-test-" + Math.random().toString(36).slice(2)))
|
|
yield* Effect.promise(() => fs.mkdir(dirpath, { recursive: true }))
|
|
const dir = sanitizePath(yield* Effect.promise(() => fs.realpath(dirpath)))
|
|
|
|
yield* Effect.addFinalizer(() =>
|
|
Effect.promise(async () => {
|
|
if (options?.git) await stop(dir).catch(() => undefined)
|
|
await clean(dir).catch(() => undefined)
|
|
}),
|
|
)
|
|
|
|
const git = (...args: string[]) =>
|
|
spawner.spawn(ChildProcess.make("git", args, { cwd: dir })).pipe(Effect.flatMap((handle) => handle.exitCode))
|
|
|
|
if (options?.git) {
|
|
yield* git("init")
|
|
yield* git("config", "core.fsmonitor", "false")
|
|
yield* git("config", "commit.gpgsign", "false")
|
|
yield* git("config", "user.email", "test@opencode.test")
|
|
yield* git("config", "user.name", "Test")
|
|
yield* git("commit", "--allow-empty", "-m", `root commit ${dir}`)
|
|
}
|
|
|
|
if (options?.config) {
|
|
const resolved = typeof options.config === "function" ? options.config() : options.config
|
|
yield* Effect.promise(() =>
|
|
fs.writeFile(
|
|
path.join(dir, "opencode.json"),
|
|
JSON.stringify({ $schema: "https://opencode.ai/config.json", ...resolved }),
|
|
),
|
|
)
|
|
}
|
|
|
|
if (options?.init) yield* options.init(dir)
|
|
|
|
return dir
|
|
})
|
|
}
|
|
|
|
export const provideInstance =
|
|
(directory: string) =>
|
|
<A, E, R>(self: Effect.Effect<A, E, R>): Effect.Effect<A, E, R | InstanceStore.Service> =>
|
|
InstanceStore.Service.use((store) => store.provide({ directory }, self))
|
|
|
|
export const provideInstanceEffect =
|
|
(directory: string) =>
|
|
<A, E, R>(self: Effect.Effect<A, E, R>): Effect.Effect<A, E, R | InstanceStore.Service> =>
|
|
InstanceStore.Service.use((store) => store.provide({ directory }, self))
|
|
|
|
export const reloadInstance = (input: InstanceStore.LoadInput) =>
|
|
InstanceStore.Service.use((store) => store.reload(input))
|
|
|
|
export const disposeAllInstancesEffect = InstanceStore.Service.use((store) => store.disposeAll())
|
|
|
|
export function provideTmpdirInstance<A, E, R>(
|
|
self: (path: string) => Effect.Effect<A, E, R>,
|
|
options?: { git?: boolean; config?: Partial<ConfigV1.Info> | (() => Partial<ConfigV1.Info>) },
|
|
) {
|
|
return Effect.gen(function* () {
|
|
const path = yield* tmpdirScoped(options)
|
|
return yield* self(path).pipe(provideInstance(path))
|
|
}).pipe(Effect.provide(testInstanceStoreLayer))
|
|
}
|
|
|
|
export class TestInstance extends Context.Service<TestInstance, { readonly directory: string }>()("@test/Instance") {}
|
|
|
|
export const requireInstance = Effect.gen(function* () {
|
|
const instance = yield* InstanceRef
|
|
if (!instance) return yield* Effect.die(new Error("missing test instance"))
|
|
return instance
|
|
})
|
|
|
|
export const withTmpdirInstance =
|
|
<E2 = never, R2 = never>(options?: {
|
|
git?: boolean
|
|
config?: Partial<ConfigV1.Info> | (() => Partial<ConfigV1.Info>)
|
|
init?: (directory: string) => Effect.Effect<void, E2, R2>
|
|
}) =>
|
|
<A, E, R>(self: Effect.Effect<A, E, R>) =>
|
|
Effect.gen(function* () {
|
|
const directory = yield* tmpdirScoped(options)
|
|
return yield* self.pipe(Effect.provideService(TestInstance, { directory }), provideInstanceEffect(directory))
|
|
}).pipe(Effect.provide(testInstanceStoreLayer), Effect.provide(AppNodeBuilder.build(CrossSpawnSpawner.node)))
|
|
|
|
export function provideTmpdirServer<A, E, R>(
|
|
self: (input: { dir: string; llm: TestLLMServer["Service"] }) => Effect.Effect<A, E, R>,
|
|
options?: { git?: boolean; config?: (url: string) => Partial<ConfigV1.Info> },
|
|
): Effect.Effect<
|
|
A,
|
|
E | PlatformError.PlatformError,
|
|
R | TestLLMServer | ChildProcessSpawner.ChildProcessSpawner | Scope.Scope
|
|
> {
|
|
return Effect.gen(function* () {
|
|
const llm = yield* TestLLMServer
|
|
return yield* provideTmpdirInstance((dir) => self({ dir, llm }), {
|
|
git: options?.git,
|
|
config: options?.config?.(llm.url),
|
|
})
|
|
})
|
|
}
|