Files
opencode/packages/core/test/database-path.test.ts

38 lines
1.7 KiB
TypeScript

import { describe, expect, test } from "bun:test"
import path from "path"
import { ConfigProvider, Effect, Layer } from "effect"
import { Database } from "@opencode-ai/core/database/database"
import { Global } from "@opencode-ai/core/global"
import { LayerNode } from "@opencode-ai/core/effect/layer-node"
import { tmpdir } from "./fixture/tmpdir"
const resolve = (env: Record<string, string>) =>
Effect.runPromise(Effect.provide(Database.configuredPath, ConfigProvider.layer(ConfigProvider.fromUnknown(env))))
describe("Database placement", () => {
test("resolves explicit files, relative names, and the channel default", async () => {
expect(await resolve({ OPENCODE_DB: ":memory:" })).toBe(":memory:")
expect(await resolve({ OPENCODE_DB: "/tmp/explicit.db" })).toBe("/tmp/explicit.db")
expect(await resolve({ OPENCODE_DB: "relative.db" })).toBe(path.join(Global.Path.data, "relative.db"))
expect(await resolve({ OPENCODE_DISABLE_CHANNEL_DB: "true" })).toBe(path.join(Global.Path.data, "opencode.db"))
})
test("reads placement from the active ConfigProvider when the layer is built", async () => {
await using tmp = await tmpdir()
const file = path.join(tmp.path, "config-seam.sqlite")
// The preload sets OPENCODE_DB=":memory:" in the process environment, so a
// database appearing at this path proves the layer reads through the
// replaced ConfigProvider rather than the environment snapshot.
await Effect.runPromise(
Layer.build(
LayerNode.compile(LayerNode.group([Database.node])).pipe(
Layer.provide(ConfigProvider.layer(ConfigProvider.fromUnknown({ OPENCODE_DB: file }))),
),
).pipe(Effect.scoped, Effect.asVoid),
)
expect(await Bun.file(file).exists()).toBe(true)
})
})