test(server): migrate global bus helper to Effect (#27214)
This commit is contained in:
@@ -29,6 +29,3 @@ export function waitGlobalBusEvent(input: {
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
export const waitGlobalBusEventPromise = (input: Parameters<typeof waitGlobalBusEvent>[0]) =>
|
|
||||||
Effect.runPromise(waitGlobalBusEvent(input))
|
|
||||||
|
|||||||
@@ -1,10 +1,12 @@
|
|||||||
import { afterEach, describe, expect, test } from "bun:test"
|
import { afterEach, describe, expect } from "bun:test"
|
||||||
import path from "path"
|
import path from "path"
|
||||||
import { Server } from "../../src/server/server"
|
import { Server } from "../../src/server/server"
|
||||||
import * as Log from "@opencode-ai/core/util/log"
|
import * as Log from "@opencode-ai/core/util/log"
|
||||||
|
import { Effect, Fiber } from "effect"
|
||||||
import { resetDatabase } from "../fixture/db"
|
import { resetDatabase } from "../fixture/db"
|
||||||
import { disposeAllInstances, tmpdir } from "../fixture/fixture"
|
import { disposeAllInstances, tmpdir } from "../fixture/fixture"
|
||||||
import { waitGlobalBusEventPromise } from "./global-bus"
|
import { it } from "../lib/effect"
|
||||||
|
import { waitGlobalBusEvent } from "./global-bus"
|
||||||
|
|
||||||
void Log.init({ print: false })
|
void Log.init({ print: false })
|
||||||
|
|
||||||
@@ -12,47 +14,90 @@ function app() {
|
|||||||
return Server.Default().app
|
return Server.Default().app
|
||||||
}
|
}
|
||||||
|
|
||||||
async function waitDisposed(directory: string) {
|
function waitDisposed(directory: string) {
|
||||||
await waitGlobalBusEventPromise({
|
return waitGlobalBusEvent({
|
||||||
message: "timed out waiting for instance disposal",
|
message: "timed out waiting for instance disposal",
|
||||||
predicate: (event) => event.payload.type === "server.instance.disposed" && event.directory === directory,
|
predicate: (event) => event.payload.type === "server.instance.disposed" && event.directory === directory,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const tmpdirEffect = (options: Parameters<typeof tmpdir>[0]) =>
|
||||||
|
Effect.acquireRelease(
|
||||||
|
Effect.promise(() => tmpdir(options)),
|
||||||
|
(tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]()),
|
||||||
|
)
|
||||||
|
|
||||||
afterEach(async () => {
|
afterEach(async () => {
|
||||||
await disposeAllInstances()
|
await disposeAllInstances()
|
||||||
await resetDatabase()
|
await resetDatabase()
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("config HttpApi", () => {
|
describe("config HttpApi", () => {
|
||||||
test("serves config update through the default server app", async () => {
|
it.live(
|
||||||
await using tmp = await tmpdir({ config: { formatter: false, lsp: false } })
|
"serves config update through the default server app",
|
||||||
const disposed = waitDisposed(tmp.path)
|
Effect.gen(function* () {
|
||||||
|
const tmp = yield* tmpdirEffect({ config: { formatter: false, lsp: false } })
|
||||||
|
const disposed = yield* waitDisposed(tmp.path).pipe(Effect.forkScoped)
|
||||||
|
|
||||||
const response = await app().request("/config", {
|
const response = yield* Effect.promise(() =>
|
||||||
method: "PATCH",
|
Promise.resolve(
|
||||||
headers: {
|
app().request("/config", {
|
||||||
"content-type": "application/json",
|
method: "PATCH",
|
||||||
"x-opencode-directory": tmp.path,
|
headers: {
|
||||||
},
|
"content-type": "application/json",
|
||||||
body: JSON.stringify({ username: "patched-user", formatter: false, lsp: false }),
|
"x-opencode-directory": tmp.path,
|
||||||
})
|
},
|
||||||
|
body: JSON.stringify({ username: "patched-user", formatter: false, lsp: false }),
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
expect(response.status).toBe(200)
|
expect(response.status).toBe(200)
|
||||||
expect(await response.json()).toMatchObject({ username: "patched-user", formatter: false, lsp: false })
|
expect(yield* Effect.promise(() => response.json())).toMatchObject({
|
||||||
await disposed
|
username: "patched-user",
|
||||||
expect(await Bun.file(path.join(tmp.path, "config.json")).json()).toMatchObject({
|
|
||||||
username: "patched-user",
|
|
||||||
formatter: false,
|
|
||||||
lsp: false,
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
test("serves config with active provider model status", async () => {
|
|
||||||
await using tmp = await tmpdir({
|
|
||||||
config: {
|
|
||||||
formatter: false,
|
formatter: false,
|
||||||
lsp: false,
|
lsp: false,
|
||||||
|
})
|
||||||
|
yield* Fiber.join(disposed)
|
||||||
|
expect(yield* Effect.promise(() => Bun.file(path.join(tmp.path, "config.json")).json())).toMatchObject({
|
||||||
|
username: "patched-user",
|
||||||
|
formatter: false,
|
||||||
|
lsp: false,
|
||||||
|
})
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
|
||||||
|
it.live(
|
||||||
|
"serves config with active provider model status",
|
||||||
|
Effect.gen(function* () {
|
||||||
|
const tmp = yield* tmpdirEffect({
|
||||||
|
config: {
|
||||||
|
formatter: false,
|
||||||
|
lsp: false,
|
||||||
|
provider: {
|
||||||
|
omniroute: {
|
||||||
|
models: {
|
||||||
|
"gpt-4o": {
|
||||||
|
status: "active",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
const response = yield* Effect.promise(() =>
|
||||||
|
Promise.resolve(
|
||||||
|
app().request("/config", {
|
||||||
|
headers: {
|
||||||
|
"x-opencode-directory": tmp.path,
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
expect(response.status).toBe(200)
|
||||||
|
expect(yield* Effect.promise(() => response.json())).toMatchObject({
|
||||||
provider: {
|
provider: {
|
||||||
omniroute: {
|
omniroute: {
|
||||||
models: {
|
models: {
|
||||||
@@ -62,26 +107,7 @@ describe("config HttpApi", () => {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
})
|
||||||
})
|
}),
|
||||||
|
)
|
||||||
const response = await app().request("/config", {
|
|
||||||
headers: {
|
|
||||||
"x-opencode-directory": tmp.path,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
expect(response.status).toBe(200)
|
|
||||||
expect(await response.json()).toMatchObject({
|
|
||||||
provider: {
|
|
||||||
omniroute: {
|
|
||||||
models: {
|
|
||||||
"gpt-4o": {
|
|
||||||
status: "active",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user