opencode/packages/server/test/schema-error.test.ts
Aiden Cline 9f807c7777
Some checks failed
check-nix / nix-eval (push) Waiting to run
check / typecheck (push) Waiting to run
deploy-files / files-build (push) Waiting to run
deploy-files / files-deploy (push) Blocked by required conditions
deploy-site / site-build (push) Waiting to run
deploy-site / site-deploy (push) Blocked by required conditions
deploy / legacy-build (push) Waiting to run
deploy / legacy-deploy (push) Blocked by required conditions
deploy / data-build (push) Waiting to run
deploy / data-deploy (push) Blocked by required conditions
Process daemon / Miri (callback ownership) (push) Has been cancelled
Process daemon / Core (blacksmith-6vcpu-macos-15) (push) Has been cancelled
Process daemon / Core (ubuntu-22.04) (push) Has been cancelled
Process daemon / Windows (arm64) (push) Has been cancelled
Process daemon / Windows (x64) (push) Has been cancelled
feat(server): async video generation API with disk-backed media (#64140)
2026-11-16 17:14:18 -06:00

67 lines
2.3 KiB
TypeScript

import { expect } from "bun:test"
import { Generate } from "@opencode/core/generate"
import { makeLocationNode } from "@opencode/util/effect/app-node"
import { Effect, Layer } from "effect"
import { tmpdirScoped } from "../../core/test/fixture/tmpdir"
import { it } from "../../core/test/lib/effect"
import { ServerFetch } from "../src/fetch"
// The success schema declares `text` as a string, so a number fails response encoding.
const generate = makeLocationNode({
service: Generate.Service,
layer: Layer.succeed(
Generate.Service,
Generate.Service.of({
text: () => Effect.succeed(42 as unknown as string),
image: () => Effect.die("unused"),
decisions: () => Effect.die("unused"),
video: {
start: () => Effect.die("unused"),
status: () => Effect.die("unused"),
result: () => Effect.die("unused"),
},
}),
),
deps: [],
})
it.live("reports response encoding failures as server errors and request decoding failures as bad requests", () =>
Effect.gen(function* () {
const config = yield* tmpdirScoped()
const handler = yield* ServerFetch.make(
{
config: { directory: config.path, project: false },
database: { path: ":memory:" },
fs: { filewatcher: false },
models: { fetch: false },
},
{ overrides: [Generate.node.replace(generate)] },
)
const encoding = yield* request(handler, { prompt: "hello" })
expect(encoding.status).toBe(500)
expect(encoding.body).toMatchObject({
_tag: "UnknownError",
message: "Server failed to encode its response. Check server logs for details.",
ref: expect.stringMatching(/^err_/),
})
const decoding = yield* request(handler, { prompt: 1 })
expect(decoding.status).toBe(400)
expect(decoding.body).toMatchObject({ _tag: "InvalidRequestError", kind: "Payload" })
}),
)
function request(handler: (request: Request) => Promise<Response>, body: unknown) {
return Effect.promise(async () => {
const response = await handler(
new Request("http://opencode.local/api/experimental/generate", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify(body),
}),
)
const json: unknown = await response.json()
return { status: response.status, body: json }
})
}