Skip to content

Commit daa3116

Browse files
authored
refactor(server): split HttpApi exercise harness (#26385)
1 parent 9e7f7bf commit daa3116

11 files changed

Lines changed: 2083 additions & 2014 deletions

File tree

packages/opencode/script/httpapi-exercise.ts

Lines changed: 1 addition & 2014 deletions
Large diffs are not rendered by default.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import type { CallResult, JsonObject } from "./types"
2+
3+
export function parse(text: string): unknown {
4+
if (!text) return undefined
5+
try {
6+
return JSON.parse(text) as unknown
7+
} catch {
8+
return text
9+
}
10+
}
11+
12+
export function looksJson(result: CallResult) {
13+
return result.contentType.includes("application/json") || result.text.startsWith("{") || result.text.startsWith("[")
14+
}
15+
16+
export function stable(value: unknown): string {
17+
return JSON.stringify(sort(value))
18+
}
19+
20+
function sort(value: unknown): unknown {
21+
if (Array.isArray(value)) return value.map(sort)
22+
if (!value || typeof value !== "object") return value
23+
return Object.fromEntries(
24+
Object.entries(value)
25+
.sort(([left], [right]) => left.localeCompare(right))
26+
.map(([key, item]) => [key, sort(item)]),
27+
)
28+
}
29+
30+
export function array(value: unknown): asserts value is unknown[] {
31+
if (!Array.isArray(value)) throw new Error("expected array")
32+
}
33+
34+
export function object(value: unknown): asserts value is JsonObject {
35+
if (!value || typeof value !== "object" || Array.isArray(value)) throw new Error("expected object")
36+
}
37+
38+
export function boolean(value: unknown): asserts value is boolean {
39+
if (typeof value !== "boolean") throw new Error("expected boolean")
40+
}
41+
42+
export function isRecord(value: unknown): value is JsonObject {
43+
return !!value && typeof value === "object" && !Array.isArray(value)
44+
}
45+
46+
export function check(value: boolean, message: string): asserts value {
47+
if (!value) throw new Error(message)
48+
}
49+
50+
export function message(error: unknown) {
51+
if (error instanceof Error) return error.message
52+
return String(error)
53+
}
54+
55+
export function pad(value: string, size: number) {
56+
return value.length >= size ? value : value + " ".repeat(size - value.length)
57+
}
58+
59+
export function indent(value: string) {
60+
return value
61+
.split("\n")
62+
.map((line) => ` ${line}`)
63+
.join("\n")
64+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import { Flag } from "@opencode-ai/core/flag/flag"
2+
import { ConfigProvider, Effect, Layer } from "effect"
3+
import { HttpRouter } from "effect/unstable/http"
4+
import { parse } from "./assertions"
5+
import { runtime, type Runtime } from "./runtime"
6+
import type { ActiveScenario, Backend, BackendApp, CallResult, CaptureMode, SeededContext } from "./types"
7+
8+
export function call(backend: Backend, scenario: ActiveScenario, ctx: SeededContext<unknown>) {
9+
return Effect.promise(async () =>
10+
capture(await app(await runtime(), backend).request(toRequest(scenario, ctx)), scenario.capture),
11+
)
12+
}
13+
14+
const appCache: Partial<Record<Backend, BackendApp>> = {}
15+
16+
function app(modules: Runtime, backend: Backend) {
17+
Flag.OPENCODE_EXPERIMENTAL_HTTPAPI = backend === "effect"
18+
Flag.OPENCODE_SERVER_PASSWORD = undefined
19+
Flag.OPENCODE_SERVER_USERNAME = undefined
20+
if (appCache[backend]) return appCache[backend]
21+
if (backend === "legacy") {
22+
const legacy = modules.Server.Legacy().app
23+
return (appCache.legacy = {
24+
request: (input, init) => legacy.request(input, init),
25+
})
26+
}
27+
28+
const handler = HttpRouter.toWebHandler(
29+
modules.ExperimentalHttpApiServer.routes.pipe(
30+
Layer.provide(
31+
ConfigProvider.layer(
32+
ConfigProvider.fromUnknown({ OPENCODE_SERVER_PASSWORD: undefined, OPENCODE_SERVER_USERNAME: undefined }),
33+
),
34+
),
35+
),
36+
{ disableLogger: true },
37+
).handler
38+
return (appCache.effect = {
39+
request(input: string | URL | Request, init?: RequestInit) {
40+
return handler(
41+
input instanceof Request ? input : new Request(new URL(input, "http://localhost"), init),
42+
modules.ExperimentalHttpApiServer.context,
43+
)
44+
},
45+
})
46+
}
47+
48+
function toRequest(scenario: ActiveScenario, ctx: SeededContext<unknown>) {
49+
const spec = scenario.request(ctx, ctx.state)
50+
return new Request(new URL(spec.path, "http://localhost"), {
51+
method: scenario.method,
52+
headers: spec.body === undefined ? spec.headers : { "content-type": "application/json", ...spec.headers },
53+
body: spec.body === undefined ? undefined : JSON.stringify(spec.body),
54+
})
55+
}
56+
57+
async function capture(response: Response, mode: CaptureMode): Promise<CallResult> {
58+
const text = mode === "stream" ? await captureStream(response) : await response.text()
59+
return {
60+
status: response.status,
61+
contentType: response.headers.get("content-type") ?? "",
62+
text,
63+
body: parse(text),
64+
}
65+
}
66+
67+
async function captureStream(response: Response) {
68+
if (!response.body) return ""
69+
const reader = response.body.getReader()
70+
const read = reader.read().then(
71+
(result) => ({ result }),
72+
(error: unknown) => ({ error }),
73+
)
74+
const winner = await Promise.race([read, Bun.sleep(1_000).then(() => ({ timeout: true }))])
75+
if ("timeout" in winner) {
76+
await reader.cancel("timed out waiting for stream chunk").catch(() => undefined)
77+
throw new Error("timed out waiting for stream chunk")
78+
}
79+
if ("error" in winner) throw winner.error
80+
await reader.cancel().catch(() => undefined)
81+
if (winner.result.done) return ""
82+
return new TextDecoder().decode(winner.result.value)
83+
}
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
import { Effect } from "effect"
2+
import { looksJson } from "./assertions"
3+
import type {
4+
ActiveScenario,
5+
BuilderState,
6+
CallResult,
7+
Comparison,
8+
Method,
9+
ProjectOptions,
10+
ScenarioContext,
11+
SeededContext,
12+
TodoScenario,
13+
} from "./types"
14+
15+
class ScenarioBuilder<S = undefined> {
16+
private readonly state: BuilderState<S>
17+
18+
constructor(method: Method, path: string, name: string) {
19+
this.state = {
20+
method,
21+
path,
22+
name,
23+
project: { git: true },
24+
seed: () => Effect.succeed(undefined as S),
25+
request: (ctx) => ({ path, headers: ctx.headers() }),
26+
capture: "full",
27+
mutates: false,
28+
reset: true,
29+
}
30+
}
31+
32+
global() {
33+
return this.clone({ project: undefined, request: () => ({ path: this.state.path }) })
34+
}
35+
36+
inProject(project: ProjectOptions = { git: true }) {
37+
return this.clone({ project })
38+
}
39+
40+
withLlm() {
41+
return this.clone({ project: { ...(this.state.project ?? { git: true }), llm: true } })
42+
}
43+
44+
at(request: BuilderState<S>["request"]) {
45+
return this.clone({ request })
46+
}
47+
48+
mutating() {
49+
return this.clone({ mutates: true })
50+
}
51+
52+
preserveDatabase() {
53+
return this.clone({ reset: false })
54+
}
55+
56+
stream() {
57+
return this.clone({ capture: "stream" })
58+
}
59+
60+
/** Assert a non-JSON or shape-only response. */
61+
ok(status = 200, compare: Comparison = "status") {
62+
return this.done(compare, (_ctx, result) =>
63+
Effect.sync(() => {
64+
if (result.status !== status) throw new Error(`expected ${status}, got ${result.status}: ${result.text}`)
65+
}),
66+
)
67+
}
68+
69+
status(
70+
status = 200,
71+
inspect?: (ctx: SeededContext<S>, result: CallResult) => Effect.Effect<void>,
72+
compare: Comparison = "status",
73+
) {
74+
return this.done(compare, (ctx, result) =>
75+
Effect.gen(function* () {
76+
if (result.status !== status) throw new Error(`expected ${status}, got ${result.status}: ${result.text}`)
77+
if (inspect) yield* inspect(ctx, result)
78+
}),
79+
)
80+
}
81+
82+
/** Assert JSON status/content-type plus an optional synchronous body check. */
83+
json(status = 200, inspect?: (body: unknown, ctx: SeededContext<S>) => void, compare: Comparison = "json") {
84+
return this.jsonEffect(status, inspect ? (body, ctx) => Effect.sync(() => inspect(body, ctx)) : undefined, compare)
85+
}
86+
87+
/** Assert JSON status/content-type plus optional Effect assertions, e.g. DB side effects. */
88+
jsonEffect(
89+
status = 200,
90+
inspect?: (body: unknown, ctx: SeededContext<S>) => Effect.Effect<void>,
91+
compare: Comparison = "json",
92+
) {
93+
return this.done(compare, (ctx, result) =>
94+
Effect.gen(function* () {
95+
if (result.status !== status) throw new Error(`expected ${status}, got ${result.status}: ${result.text}`)
96+
if (!looksJson(result))
97+
throw new Error(`expected JSON response, got ${result.contentType || "no content-type"}`)
98+
if (inspect) yield* inspect(result.body, ctx)
99+
}),
100+
)
101+
}
102+
103+
private clone(next: Partial<BuilderState<S>>) {
104+
const builder = new ScenarioBuilder<S>(this.state.method, this.state.path, this.state.name)
105+
Object.assign(builder.state, this.state, next)
106+
return builder
107+
}
108+
109+
/**
110+
* Seed typed state before the HTTP request. The returned value becomes `ctx.state`
111+
* for `.at(...)` and assertions, giving stateful route tests type-safe setup.
112+
*/
113+
seeded<Next>(seed: (ctx: ScenarioContext) => Effect.Effect<Next>) {
114+
const builder = new ScenarioBuilder<Next>(this.state.method, this.state.path, this.state.name)
115+
Object.assign(builder.state, this.state, { seed })
116+
return builder
117+
}
118+
119+
private done(
120+
compare: Comparison,
121+
expect: (ctx: SeededContext<S>, result: CallResult) => Effect.Effect<void>,
122+
): ActiveScenario {
123+
const state = this.state
124+
return {
125+
kind: "active",
126+
method: state.method,
127+
path: state.path,
128+
name: state.name,
129+
project: state.project,
130+
seed: state.seed,
131+
request: (ctx, seeded) => state.request({ ...ctx, state: seeded as S }),
132+
expect: (ctx, seeded, result) => expect({ ...ctx, state: seeded as S }, result),
133+
compare,
134+
capture: state.capture,
135+
mutates: state.mutates,
136+
reset: state.reset,
137+
}
138+
}
139+
}
140+
141+
export const http = {
142+
get: (path: string, name: string) => new ScenarioBuilder("GET", path, name),
143+
post: (path: string, name: string) => new ScenarioBuilder("POST", path, name),
144+
put: (path: string, name: string) => new ScenarioBuilder("PUT", path, name),
145+
patch: (path: string, name: string) => new ScenarioBuilder("PATCH", path, name),
146+
delete: (path: string, name: string) => new ScenarioBuilder("DELETE", path, name),
147+
}
148+
149+
export const pending = (method: Method, path: string, name: string, reason: string): TodoScenario => ({
150+
kind: "todo",
151+
method,
152+
path,
153+
name,
154+
reason,
155+
})
156+
157+
export function route(template: string, params: Record<string, string>) {
158+
return Object.entries(params).reduce(
159+
(next, [key, value]) => next.replaceAll(`{${key}}`, value).replaceAll(`:${key}`, value),
160+
template,
161+
)
162+
}
163+
164+
export function controlledPtyInput(title: string | undefined) {
165+
return {
166+
command: "/bin/sh",
167+
args: ["-c", "sleep 30"],
168+
...(title ? { title } : {}),
169+
}
170+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { Flag } from "@opencode-ai/core/flag/flag"
2+
import { Effect } from "effect"
3+
import path from "path"
4+
5+
const preserveExerciseGlobalRoot = !!process.env.OPENCODE_HTTPAPI_EXERCISE_GLOBAL
6+
export const exerciseGlobalRoot =
7+
process.env.OPENCODE_HTTPAPI_EXERCISE_GLOBAL ??
8+
path.join(process.env.TMPDIR ?? "/tmp", `opencode-httpapi-global-${process.pid}`)
9+
process.env.XDG_DATA_HOME = path.join(exerciseGlobalRoot, "data")
10+
process.env.XDG_CONFIG_HOME = path.join(exerciseGlobalRoot, "config")
11+
process.env.XDG_STATE_HOME = path.join(exerciseGlobalRoot, "state")
12+
process.env.XDG_CACHE_HOME = path.join(exerciseGlobalRoot, "cache")
13+
process.env.OPENCODE_DISABLE_SHARE = "true"
14+
export const exerciseConfigDirectory = path.join(exerciseGlobalRoot, "config", "opencode")
15+
export const exerciseDataDirectory = path.join(exerciseGlobalRoot, "data", "opencode")
16+
17+
const preserveExerciseDatabase = !!process.env.OPENCODE_HTTPAPI_EXERCISE_DB
18+
export const exerciseDatabasePath =
19+
process.env.OPENCODE_HTTPAPI_EXERCISE_DB ??
20+
path.join(process.env.TMPDIR ?? "/tmp", `opencode-httpapi-exercise-${process.pid}.db`)
21+
process.env.OPENCODE_DB = exerciseDatabasePath
22+
Flag.OPENCODE_DB = exerciseDatabasePath
23+
24+
export const original = {
25+
OPENCODE_EXPERIMENTAL_HTTPAPI: Flag.OPENCODE_EXPERIMENTAL_HTTPAPI,
26+
OPENCODE_SERVER_PASSWORD: Flag.OPENCODE_SERVER_PASSWORD,
27+
OPENCODE_SERVER_USERNAME: Flag.OPENCODE_SERVER_USERNAME,
28+
}
29+
30+
export const cleanupExercisePaths = Effect.promise(async () => {
31+
const fs = await import("fs/promises")
32+
if (!preserveExerciseDatabase) {
33+
await Promise.all(
34+
[exerciseDatabasePath, `${exerciseDatabasePath}-wal`, `${exerciseDatabasePath}-shm`].map((file) =>
35+
fs.rm(file, { force: true }).catch(() => undefined),
36+
),
37+
)
38+
}
39+
if (!preserveExerciseGlobalRoot)
40+
await fs.rm(exerciseGlobalRoot, { recursive: true, force: true }).catch(() => undefined)
41+
})

0 commit comments

Comments
 (0)