mirror of
https://github.com/anomalyco/opencode.git
synced 2026-12-01 22:13:19 +08:00
92 lines
3.3 KiB
TypeScript
92 lines
3.3 KiB
TypeScript
import { expect } from "bun:test"
|
|
import { EnvironmentHost } from "@opencode/core/environment/host"
|
|
import { EnvironmentUnavailable } from "@opencode/core/environment/unavailable"
|
|
import { Session } from "@opencode/schema/session"
|
|
import { CrossSpawnSpawner } from "@opencode/util/cross-spawn-spawner"
|
|
import { Effect, Schema } from "effect"
|
|
import { tmpdirScoped } from "../../core/test/fixture/tmpdir"
|
|
import { it } from "../../core/test/lib/effect"
|
|
import { ServerFetch } from "../src/fetch"
|
|
|
|
const SessionResponse = Schema.Struct({ data: Schema.toEncoded(Session.Info) })
|
|
|
|
it.live("a shell command that cannot start answers 503, a declared status, instead of 500", () =>
|
|
Effect.gen(function* () {
|
|
const tmp = yield* tmpdirScoped()
|
|
const { handler } = yield* ServerFetch.makeWithContext(
|
|
{
|
|
app: { version: "test" },
|
|
database: { path: ":memory:" },
|
|
fs: { filewatcher: false },
|
|
models: { fetch: false },
|
|
},
|
|
{ overrides: [CrossSpawnSpawner.node.replace(EnvironmentUnavailable.layer)] },
|
|
)
|
|
const request = (path: string, body: unknown) =>
|
|
Effect.promise(() =>
|
|
handler(
|
|
new Request(`http://opencode.local${path}`, {
|
|
method: "POST",
|
|
headers: { "content-type": "application/json" },
|
|
body: JSON.stringify(body),
|
|
}),
|
|
),
|
|
)
|
|
const created = yield* request("/api/session", { location: { directory: tmp.path } })
|
|
const id = Schema.decodeUnknownSync(SessionResponse)(yield* Effect.promise(() => created.json())).data.id
|
|
|
|
const failed = yield* request(`/api/session/${id}/shell`, { command: "echo hi" })
|
|
expect(failed.status).toBe(503)
|
|
expect(yield* Effect.promise(() => failed.json())).toMatchObject({
|
|
_tag: "ServiceUnavailableError",
|
|
service: "shell",
|
|
})
|
|
}).pipe(Effect.scoped),
|
|
)
|
|
|
|
it.live("POST /api/shell and POST /api/pty answer 503 when the host has no execution plane", () =>
|
|
Effect.gen(function* () {
|
|
const tmp = yield* tmpdirScoped()
|
|
const { handler } = yield* ServerFetch.makeWithContext(
|
|
{
|
|
app: { version: "test" },
|
|
database: { path: ":memory:" },
|
|
fs: { filewatcher: false },
|
|
models: { fetch: false },
|
|
},
|
|
{
|
|
overrides: [
|
|
CrossSpawnSpawner.node.replace(EnvironmentUnavailable.layer),
|
|
EnvironmentHost.node.replace(EnvironmentHost.unavailableLayer),
|
|
],
|
|
},
|
|
)
|
|
const post = (pathname: string, body: unknown) =>
|
|
Effect.promise(() => {
|
|
const url = new URL(pathname, "http://opencode.local")
|
|
url.searchParams.set("location[directory]", tmp.path)
|
|
return handler(
|
|
new Request(url, {
|
|
method: "POST",
|
|
headers: { "content-type": "application/json" },
|
|
body: JSON.stringify(body),
|
|
}),
|
|
)
|
|
})
|
|
|
|
const shellRes = yield* post("/api/shell", { command: "echo hi" })
|
|
expect(shellRes.status).toBe(503)
|
|
expect(yield* Effect.promise(() => shellRes.json())).toMatchObject({
|
|
_tag: "ServiceUnavailableError",
|
|
service: "shell",
|
|
})
|
|
|
|
const ptyRes = yield* post("/api/pty", {})
|
|
expect(ptyRes.status).toBe(503)
|
|
expect(yield* Effect.promise(() => ptyRes.json())).toMatchObject({
|
|
_tag: "ServiceUnavailableError",
|
|
service: "pty",
|
|
})
|
|
}).pipe(Effect.scoped),
|
|
)
|