test(server): migrate project init git test to Effect runner (#27218)

This commit is contained in:
Kit Langton 2026-05-12 22:08:55 -04:00 committed by GitHub
commit 0879f5e22d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,113 +1,120 @@
import { afterEach, describe, expect, test } from "bun:test" import { afterEach, describe, expect } from "bun:test"
import { Effect } from "effect" import { AppFileSystem } from "@opencode-ai/core/filesystem"
import { Effect, Layer } from "effect"
import path from "path" import path from "path"
import { GlobalBus } from "../../src/bus/global" import { InstanceRef } from "../../src/effect/instance-ref"
import { InstanceBootstrap } from "../../src/project/bootstrap-service"
import { InstanceStore } from "../../src/project/instance-store"
import { GlobalBus, type GlobalEvent } from "../../src/bus/global"
import { Snapshot } from "../../src/snapshot" import { Snapshot } from "../../src/snapshot"
import { Server } from "../../src/server/server" import { Server } from "../../src/server/server"
import { Filesystem } from "@/util/filesystem"
import * as Log from "@opencode-ai/core/util/log" import * as Log from "@opencode-ai/core/util/log"
import { resetDatabase } from "../fixture/db" import { resetDatabase } from "../fixture/db"
import { disposeAllInstances, provideInstance, tmpdir } from "../fixture/fixture" import { disposeAllInstances, TestInstance } from "../fixture/fixture"
import { testEffect } from "../lib/effect"
void Log.init({ print: false }) void Log.init({ print: false })
afterEach(async () => { afterEach(async () => {
await disposeAllInstances()
await resetDatabase() await resetDatabase()
}) })
const disposedEvents = (seen: { directory?: string; payload: { type: string } }[], dir: string) => const noopBootstrap = Layer.succeed(InstanceBootstrap.Service, InstanceBootstrap.Service.of({ run: Effect.void }))
const testInstanceStore = InstanceStore.defaultLayer.pipe(Layer.provide(noopBootstrap))
const it = testEffect(Layer.mergeAll(AppFileSystem.defaultLayer, Snapshot.defaultLayer, testInstanceStore))
function request(directory: string, url: string, init: RequestInit = {}) {
return Effect.promise(() => {
const headers = new Headers(init.headers)
headers.set("x-opencode-directory", directory)
return Promise.resolve(Server.Default().app.request(url, { ...init, headers }))
})
}
function json<T>(response: Response) {
return Effect.promise(() => response.json() as Promise<T>)
}
function collectGlobalEvents() {
return Effect.acquireRelease(
Effect.sync(() => {
const seen: GlobalEvent[] = []
const on = (event: GlobalEvent) => {
seen.push(event)
}
GlobalBus.on("event", on)
return { seen, on }
}),
({ on }) => Effect.sync(() => GlobalBus.off("event", on)),
)
}
const disposedEvents = (seen: GlobalEvent[], dir: string) =>
seen.filter((evt) => evt.directory === dir && evt.payload.type === "server.instance.disposed").length seen.filter((evt) => evt.directory === dir && evt.payload.type === "server.instance.disposed").length
describe("project.initGit endpoint", () => { describe("project.initGit endpoint", () => {
test("initializes git and reloads immediately", async () => { it.instance("initializes git and reloads immediately", () =>
await using tmp = await tmpdir() Effect.gen(function* () {
const app = Server.Default().app const tmp = yield* TestInstance
const seen: { directory?: string; payload: { type: string } }[] = [] const fs = yield* AppFileSystem.Service
const fn = (evt: { directory?: string; payload: { type: string } }) => { const events = yield* collectGlobalEvents()
seen.push(evt)
}
GlobalBus.on("event", fn)
try { const init = yield* request(tmp.directory, "/project/git/init", {
const init = await app.request("/project/git/init", {
method: "POST", method: "POST",
headers: {
"x-opencode-directory": tmp.path,
},
}) })
const body = await init.json() const body = yield* json(init)
expect(init.status).toBe(200) expect(init.status).toBe(200)
expect(body).toMatchObject({ expect(body).toMatchObject({
id: "global", id: "global",
vcs: "git", vcs: "git",
worktree: tmp.path, worktree: tmp.directory,
}) })
// Reload behavior: bus emits exactly one server.instance.disposed for the directory. // Reload behavior: bus emits exactly one server.instance.disposed for the directory.
expect(disposedEvents(seen, tmp.path)).toBe(1) expect(disposedEvents(events.seen, tmp.directory)).toBe(1)
expect(await Filesystem.exists(path.join(tmp.path, ".git", "opencode"))).toBe(false) expect(yield* fs.exists(path.join(tmp.directory, ".git", "opencode"))).toBe(false)
const current = await app.request("/project/current", { const current = yield* request(tmp.directory, "/project/current")
headers: {
"x-opencode-directory": tmp.path,
},
})
expect(current.status).toBe(200) expect(current.status).toBe(200)
expect(await current.json()).toMatchObject({ expect(yield* json(current)).toMatchObject({
id: "global", id: "global",
vcs: "git", vcs: "git",
worktree: tmp.path, worktree: tmp.directory,
}) })
expect( const ctx = yield* InstanceStore.Service.use((store) => store.reload({ directory: tmp.directory }))
await Effect.runPromise( const tracked = yield* Snapshot.Service.use((snapshot) => snapshot.track()).pipe(
Snapshot.Service.use((svc) => svc.track()).pipe( Effect.provideService(InstanceRef, ctx),
provideInstance(tmp.path), )
Effect.provide(Snapshot.defaultLayer), expect(tracked).toBeTruthy()
), }),
), )
).toBeTruthy()
} finally {
await disposeAllInstances()
GlobalBus.off("event", fn)
}
})
test("does not reload when the project is already git", async () => { it.instance(
await using tmp = await tmpdir({ git: true }) "does not reload when the project is already git",
const app = Server.Default().app () =>
const seen: { directory?: string; payload: { type: string } }[] = [] Effect.gen(function* () {
const fn = (evt: { directory?: string; payload: { type: string } }) => { const tmp = yield* TestInstance
seen.push(evt) const events = yield* collectGlobalEvents()
}
GlobalBus.on("event", fn)
try { const init = yield* request(tmp.directory, "/project/git/init", {
const init = await app.request("/project/git/init", { method: "POST",
method: "POST", })
headers: { expect(init.status).toBe(200)
"x-opencode-directory": tmp.path, expect(yield* json(init)).toMatchObject({
}, vcs: "git",
}) worktree: tmp.directory,
expect(init.status).toBe(200) })
expect(await init.json()).toMatchObject({ expect(disposedEvents(events.seen, tmp.directory)).toBe(0)
vcs: "git",
worktree: tmp.path,
})
expect(disposedEvents(seen, tmp.path)).toBe(0)
const current = await app.request("/project/current", { const current = yield* request(tmp.directory, "/project/current")
headers: { expect(current.status).toBe(200)
"x-opencode-directory": tmp.path, expect(yield* json(current)).toMatchObject({
}, vcs: "git",
}) worktree: tmp.directory,
expect(current.status).toBe(200) })
expect(await current.json()).toMatchObject({ }),
vcs: "git", { git: true },
worktree: tmp.path, )
})
} finally {
await disposeAllInstances()
GlobalBus.off("event", fn)
}
})
}) })