refactor(core): move database schema ownership (#29068)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -1,13 +1,11 @@
|
||||
import { afterEach, describe, expect } from "bun:test"
|
||||
import { Effect, Schema } from "effect"
|
||||
import { Effect, Layer, Queue, Schema, Stream } from "effect"
|
||||
import * as Log from "@opencode-ai/core/util/log"
|
||||
import { Bus } from "../../src/bus"
|
||||
import { Event as ServerEvent } from "../../src/server/event"
|
||||
import { Server } from "../../src/server/server"
|
||||
import { EventPaths } from "../../src/server/routes/instance/httpapi/groups/event"
|
||||
import { resetDatabase } from "../fixture/db"
|
||||
import { disposeAllInstances, TestInstance } from "../fixture/fixture"
|
||||
import { testEffectShared } from "../lib/effect"
|
||||
import { testEffect } from "../lib/effect"
|
||||
import { httpApiLayer, requestInDirectory } from "./httpapi-layer"
|
||||
|
||||
void Log.init({ print: false })
|
||||
|
||||
@@ -17,28 +15,25 @@ const EventData = Schema.Struct({
|
||||
properties: Schema.Record(Schema.String, Schema.Any),
|
||||
})
|
||||
|
||||
const readEvent = (reader: ReadableStreamDefaultReader<Uint8Array>) =>
|
||||
const readEvent = (reader: Queue.Dequeue<Uint8Array>) =>
|
||||
Effect.gen(function* () {
|
||||
const result = yield* Effect.promise(() => reader.read()).pipe(
|
||||
const value = yield* Queue.take(reader).pipe(
|
||||
Effect.timeoutOrElse({
|
||||
duration: "5 seconds",
|
||||
orElse: () => Effect.fail(new Error("timed out waiting for event")),
|
||||
}),
|
||||
)
|
||||
if (result.done || !result.value) return yield* Effect.fail(new Error("event stream closed"))
|
||||
return Schema.decodeUnknownSync(EventData)(
|
||||
JSON.parse(new TextDecoder().decode(result.value).replace(/^data: /, "")),
|
||||
)
|
||||
return Schema.decodeUnknownSync(EventData)(JSON.parse(new TextDecoder().decode(value).replace(/^data: /, "")))
|
||||
})
|
||||
|
||||
const openEventStream = (directory: string) =>
|
||||
Effect.gen(function* () {
|
||||
const response = yield* Effect.promise(async () =>
|
||||
Server.Default().app.request(EventPaths.event, { headers: { "x-opencode-directory": directory } }),
|
||||
const response = yield* requestInDirectory(EventPaths.event, directory)
|
||||
const reader = yield* Queue.unbounded<Uint8Array>()
|
||||
yield* response.stream.pipe(
|
||||
Stream.runForEach((value) => Queue.offer(reader, value)),
|
||||
Effect.forkScoped,
|
||||
)
|
||||
if (!response.body) return yield* Effect.die("missing SSE response body")
|
||||
const reader = response.body.getReader()
|
||||
yield* Effect.addFinalizer(() => Effect.promise(() => reader.cancel().catch(() => undefined)))
|
||||
return { response, reader }
|
||||
})
|
||||
|
||||
@@ -47,7 +42,7 @@ afterEach(async () => {
|
||||
await resetDatabase()
|
||||
})
|
||||
|
||||
const it = testEffectShared(Bus.defaultLayer)
|
||||
const it = testEffect(httpApiLayer)
|
||||
|
||||
describe("event HttpApi", () => {
|
||||
it.instance(
|
||||
@@ -58,10 +53,10 @@ describe("event HttpApi", () => {
|
||||
const { response, reader } = yield* openEventStream(directory)
|
||||
|
||||
expect(response.status).toBe(200)
|
||||
expect(response.headers.get("content-type")).toContain("text/event-stream")
|
||||
expect(response.headers.get("cache-control")).toBe("no-cache, no-transform")
|
||||
expect(response.headers.get("x-accel-buffering")).toBe("no")
|
||||
expect(response.headers.get("x-content-type-options")).toBe("nosniff")
|
||||
expect(response.headers["content-type"]).toContain("text/event-stream")
|
||||
expect(response.headers["cache-control"]).toBe("no-cache, no-transform")
|
||||
expect(response.headers["x-accel-buffering"]).toBe("no")
|
||||
expect(response.headers["x-content-type-options"]).toBe("nosniff")
|
||||
expect(yield* readEvent(reader)).toMatchObject({ type: "server.connected", properties: {} })
|
||||
}),
|
||||
{ git: true, config: { formatter: false, lsp: false } },
|
||||
@@ -76,8 +71,8 @@ describe("event HttpApi", () => {
|
||||
expect(yield* readEvent(reader)).toMatchObject({ type: "server.connected", properties: {} })
|
||||
|
||||
// If no second event arrives within 250ms, the stream is still open.
|
||||
const status = yield* Effect.promise(() => reader.read()).pipe(
|
||||
Effect.map((result) => (result.done ? ("closed" as const) : ("event" as const))),
|
||||
const status = yield* Queue.take(reader).pipe(
|
||||
Effect.as("event" as const),
|
||||
Effect.timeoutOrElse({ duration: "250 millis", orElse: () => Effect.succeed("open" as const) }),
|
||||
)
|
||||
expect(status).toBe("open")
|
||||
@@ -86,16 +81,18 @@ describe("event HttpApi", () => {
|
||||
)
|
||||
|
||||
it.instance(
|
||||
"delivers instance bus events after the initial event",
|
||||
"delivers instance events after the initial event",
|
||||
() =>
|
||||
Effect.gen(function* () {
|
||||
const { directory } = yield* TestInstance
|
||||
const { reader } = yield* openEventStream(directory)
|
||||
expect(yield* readEvent(reader)).toMatchObject({ type: "server.connected", properties: {} })
|
||||
|
||||
yield* Bus.use.publish(ServerEvent.Connected, {})
|
||||
expect(yield* readEvent(reader)).toMatchObject({ type: "server.connected", properties: {} })
|
||||
const created = yield* requestInDirectory("/session", directory, { method: "POST" })
|
||||
expect(created.status).toBe(200)
|
||||
expect(yield* readEvent(reader)).toMatchObject({ type: "session.created" })
|
||||
}),
|
||||
{ git: true, config: { formatter: false, lsp: false } },
|
||||
)
|
||||
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user