mirror of
https://github.com/anomalyco/opencode.git
synced 2026-07-21 10:16:03 +00:00
test(opencode): remove obsolete HTTP API exerciser
This commit is contained in:
@@ -8,7 +8,6 @@
|
||||
"scripts": {
|
||||
"typecheck": "tsgo --noEmit",
|
||||
"test": "bun test --timeout 30000 --only-failures",
|
||||
"test:httpapi": "bun run script/httpapi-exercise.ts --mode coverage --fail-on-missing --fail-on-skip && bun run script/httpapi-exercise.ts --mode auth --fail-on-missing --fail-on-skip && bun run script/httpapi-exercise.ts --mode effect --fail-on-missing --fail-on-skip",
|
||||
"bench:test": "bun run script/bench-test-suite.ts",
|
||||
"profile:test": "bun run script/profile-test-files.ts",
|
||||
"build": "bun run script/build.ts",
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
await import("../test/server/httpapi-exercise/index")
|
||||
@@ -1,64 +0,0 @@
|
||||
import type { CallResult, JsonObject } from "./types"
|
||||
|
||||
export function parse(text: string): unknown {
|
||||
if (!text) return undefined
|
||||
try {
|
||||
return JSON.parse(text) as unknown
|
||||
} catch {
|
||||
return text
|
||||
}
|
||||
}
|
||||
|
||||
export function looksJson(result: CallResult) {
|
||||
return result.contentType.includes("application/json") || result.text.startsWith("{") || result.text.startsWith("[")
|
||||
}
|
||||
|
||||
export function stable(value: unknown): string {
|
||||
return JSON.stringify(sort(value))
|
||||
}
|
||||
|
||||
function sort(value: unknown): unknown {
|
||||
if (Array.isArray(value)) return value.map(sort)
|
||||
if (!value || typeof value !== "object") return value
|
||||
return Object.fromEntries(
|
||||
Object.entries(value)
|
||||
.sort(([left], [right]) => left.localeCompare(right))
|
||||
.map(([key, item]) => [key, sort(item)]),
|
||||
)
|
||||
}
|
||||
|
||||
export function array(value: unknown): asserts value is unknown[] {
|
||||
if (!Array.isArray(value)) throw new Error("expected array")
|
||||
}
|
||||
|
||||
export function object(value: unknown): asserts value is JsonObject {
|
||||
if (!value || typeof value !== "object" || Array.isArray(value)) throw new Error("expected object")
|
||||
}
|
||||
|
||||
export function boolean(value: unknown): asserts value is boolean {
|
||||
if (typeof value !== "boolean") throw new Error("expected boolean")
|
||||
}
|
||||
|
||||
export function isRecord(value: unknown): value is JsonObject {
|
||||
return !!value && typeof value === "object" && !Array.isArray(value)
|
||||
}
|
||||
|
||||
export function check(value: boolean, message: string): asserts value {
|
||||
if (!value) throw new Error(message)
|
||||
}
|
||||
|
||||
export function message(error: unknown) {
|
||||
if (error instanceof Error) return error.message
|
||||
return String(error)
|
||||
}
|
||||
|
||||
export function pad(value: string, size: number) {
|
||||
return value.length >= size ? value : value + " ".repeat(size - value.length)
|
||||
}
|
||||
|
||||
export function indent(value: string) {
|
||||
return value
|
||||
.split("\n")
|
||||
.map((line) => ` ${line}`)
|
||||
.join("\n")
|
||||
}
|
||||
@@ -1,144 +0,0 @@
|
||||
import { ConfigProvider, Effect, Layer } from "effect"
|
||||
import { HttpRouter } from "effect/unstable/http"
|
||||
import { parse } from "./assertions"
|
||||
import { runtime, type Runtime } from "./runtime"
|
||||
import type { ActiveScenario, BackendApp, CallResult, CaptureMode, SeededContext } from "./types"
|
||||
|
||||
type CallOptions = {
|
||||
auth?: {
|
||||
password?: string
|
||||
username?: string
|
||||
}
|
||||
}
|
||||
|
||||
export function call(scenario: ActiveScenario, ctx: SeededContext<unknown>) {
|
||||
return Effect.promise(async () =>
|
||||
capture(await app(await runtime(), {}).request(toRequest(scenario, ctx)), scenario.capture),
|
||||
)
|
||||
}
|
||||
|
||||
export function callAuthProbe(scenario: ActiveScenario, credentials: "missing" | "valid" = "missing") {
|
||||
return Effect.promise(async () => {
|
||||
const controller = new AbortController()
|
||||
return Promise.race([
|
||||
Promise.resolve(
|
||||
app(await runtime(), { auth: { password: "secret" } }).request(
|
||||
toAuthProbeRequest(scenario, credentials, controller.signal),
|
||||
),
|
||||
).then((response) => capture(response, scenario.capture)),
|
||||
Bun.sleep(1_000).then(() => {
|
||||
controller.abort("auth probe timed out")
|
||||
return {
|
||||
status: 0,
|
||||
contentType: "",
|
||||
text: "auth probe timed out",
|
||||
body: undefined,
|
||||
timedOut: true,
|
||||
}
|
||||
}),
|
||||
])
|
||||
})
|
||||
}
|
||||
|
||||
type CachedApp = BackendApp & { readonly dispose: () => Promise<void> }
|
||||
|
||||
const appCache: Partial<Record<string, CachedApp>> = {}
|
||||
|
||||
export async function disposeApps() {
|
||||
const apps = Object.values(appCache)
|
||||
for (const key of Object.keys(appCache)) delete appCache[key]
|
||||
await Promise.all(apps.flatMap((app) => (app === undefined ? [] : [app.dispose()])))
|
||||
}
|
||||
|
||||
function app(modules: Runtime, options: CallOptions) {
|
||||
const username = options.auth?.username
|
||||
const password = options.auth?.password
|
||||
const cacheKey = `${username ?? ""}:${password ?? ""}`
|
||||
if (appCache[cacheKey]) return appCache[cacheKey]
|
||||
|
||||
const web = HttpRouter.toWebHandler(
|
||||
modules.HttpApiApp.routes.pipe(
|
||||
Layer.provide(
|
||||
ConfigProvider.layer(
|
||||
ConfigProvider.fromUnknown({ OPENCODE_SERVER_PASSWORD: password, OPENCODE_SERVER_USERNAME: username }),
|
||||
),
|
||||
),
|
||||
),
|
||||
{ disableLogger: true, memoMap: modules.memoMap },
|
||||
)
|
||||
return (appCache[cacheKey] = {
|
||||
dispose: web.dispose,
|
||||
request(input: string | URL | Request, init?: RequestInit) {
|
||||
return web.handler(
|
||||
input instanceof Request ? input : new Request(new URL(input, "http://localhost"), init),
|
||||
modules.HttpApiApp.context,
|
||||
)
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
function toRequest(scenario: ActiveScenario, ctx: SeededContext<unknown>) {
|
||||
const spec = scenario.request(ctx)
|
||||
return new Request(new URL(spec.path, "http://localhost"), {
|
||||
method: scenario.method,
|
||||
headers: spec.body === undefined ? spec.headers : { "content-type": "application/json", ...spec.headers },
|
||||
body: spec.body === undefined ? undefined : JSON.stringify(spec.body),
|
||||
})
|
||||
}
|
||||
|
||||
function toAuthProbeRequest(scenario: ActiveScenario, credentials: "missing" | "valid", signal: AbortSignal) {
|
||||
const spec = scenario.authProbe ?? {
|
||||
path: authProbePath(scenario.path),
|
||||
body: scenario.method === "GET" ? undefined : {},
|
||||
}
|
||||
const headers = {
|
||||
...(spec.body === undefined ? {} : { "content-type": "application/json" }),
|
||||
...spec.headers,
|
||||
...(credentials === "valid" ? { authorization: basic("opencode", "secret") } : {}),
|
||||
}
|
||||
return new Request(new URL(spec.path, "http://localhost"), {
|
||||
method: scenario.method,
|
||||
headers,
|
||||
body: spec.body === undefined ? undefined : JSON.stringify(spec.body),
|
||||
signal,
|
||||
})
|
||||
}
|
||||
|
||||
function basic(username: string, password: string) {
|
||||
return `Basic ${Buffer.from(`${username}:${password}`).toString("base64")}`
|
||||
}
|
||||
|
||||
function authProbePath(path: string) {
|
||||
return path
|
||||
.replace(/\{([^}]+)\}/g, (_match, key: string) => `auth_${key}`)
|
||||
.replace(/:([^/]+)/g, (_match, key: string) => `auth_${key}`)
|
||||
}
|
||||
|
||||
async function capture(response: Response, mode: CaptureMode): Promise<CallResult> {
|
||||
const text = mode === "stream" ? await captureStream(response) : await response.text()
|
||||
return {
|
||||
status: response.status,
|
||||
contentType: response.headers.get("content-type") ?? "",
|
||||
text,
|
||||
body: parse(text),
|
||||
timedOut: false,
|
||||
}
|
||||
}
|
||||
|
||||
async function captureStream(response: Response) {
|
||||
if (!response.body) return ""
|
||||
const reader = response.body.getReader()
|
||||
const read = reader.read().then(
|
||||
(result) => ({ result }),
|
||||
(error: unknown) => ({ error }),
|
||||
)
|
||||
const winner = await Promise.race([read, Bun.sleep(1_000).then(() => ({ timeout: true }))])
|
||||
if ("timeout" in winner) {
|
||||
await reader.cancel("timed out waiting for stream chunk").catch(() => undefined)
|
||||
throw new Error("timed out waiting for stream chunk")
|
||||
}
|
||||
if ("error" in winner) throw winner.error
|
||||
await reader.cancel().catch(() => undefined)
|
||||
if (winner.result.done) return ""
|
||||
return new TextDecoder().decode(winner.result.value)
|
||||
}
|
||||
@@ -1,162 +0,0 @@
|
||||
import { Effect } from "effect"
|
||||
import { looksJson } from "./assertions"
|
||||
import type {
|
||||
ActiveScenario,
|
||||
AuthPolicy,
|
||||
BuilderState,
|
||||
CallResult,
|
||||
Method,
|
||||
ProjectOptions,
|
||||
RequestSpec,
|
||||
ScenarioContext,
|
||||
SeededContext,
|
||||
TodoScenario,
|
||||
} from "./types"
|
||||
|
||||
class ScenarioBuilder<S = undefined> {
|
||||
private readonly state: BuilderState<S>
|
||||
|
||||
constructor(method: Method, path: string, name: string, auth: AuthPolicy) {
|
||||
this.state = {
|
||||
method,
|
||||
path,
|
||||
name,
|
||||
project: { git: true },
|
||||
// oxlint-disable-next-line typescript-eslint/no-unsafe-type-assertion -- The unseeded builder state is intentionally undefined until `.seeded(...)` narrows it.
|
||||
seed: () => Effect.succeed(undefined as S),
|
||||
request: (ctx) => ({ path, headers: ctx.headers() }),
|
||||
authProbe: undefined,
|
||||
capture: "full",
|
||||
reset: true,
|
||||
auth,
|
||||
}
|
||||
}
|
||||
|
||||
global() {
|
||||
return this.clone({ project: undefined, request: () => ({ path: this.state.path }) })
|
||||
}
|
||||
|
||||
inProject(project: ProjectOptions = { git: true }) {
|
||||
return this.clone({ project })
|
||||
}
|
||||
|
||||
withLlm() {
|
||||
return this.clone({ project: { ...(this.state.project ?? { git: true }), llm: true } })
|
||||
}
|
||||
|
||||
at(request: BuilderState<S>["request"]) {
|
||||
return this.clone({ request })
|
||||
}
|
||||
|
||||
probe(authProbe: RequestSpec) {
|
||||
return this.clone({ authProbe })
|
||||
}
|
||||
|
||||
preserveState() {
|
||||
return this.clone({ reset: false })
|
||||
}
|
||||
|
||||
stream() {
|
||||
return this.clone({ capture: "stream" })
|
||||
}
|
||||
|
||||
status(status = 200, inspect?: (ctx: SeededContext<S>, result: CallResult) => Effect.Effect<void>) {
|
||||
return this.done((ctx, result) =>
|
||||
Effect.gen(function* () {
|
||||
if (result.status !== status) throw new Error(`expected ${status}, got ${result.status}: ${result.text}`)
|
||||
if (inspect) yield* inspect(ctx, result)
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
/** Assert JSON status/content-type plus an optional synchronous body check. */
|
||||
json(status = 200, inspect?: (body: unknown, ctx: SeededContext<S>) => void) {
|
||||
return this.jsonEffect(status, inspect ? (body, ctx) => Effect.sync(() => inspect(body, ctx)) : undefined)
|
||||
}
|
||||
|
||||
/** Assert JSON status/content-type plus optional Effect assertions, e.g. DB side effects. */
|
||||
jsonEffect(status = 200, inspect?: (body: unknown, ctx: SeededContext<S>) => Effect.Effect<void>) {
|
||||
return this.done((ctx, result) =>
|
||||
Effect.gen(function* () {
|
||||
if (result.status !== status) throw new Error(`expected ${status}, got ${result.status}: ${result.text}`)
|
||||
if (!looksJson(result))
|
||||
throw new Error(`expected JSON response, got ${result.contentType || "no content-type"}`)
|
||||
if (inspect) yield* inspect(result.body, ctx)
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
private clone(next: Partial<BuilderState<S>>) {
|
||||
const builder = new ScenarioBuilder<S>(this.state.method, this.state.path, this.state.name, this.state.auth)
|
||||
Object.assign(builder.state, this.state, next)
|
||||
return builder
|
||||
}
|
||||
|
||||
/**
|
||||
* Seed typed state before the HTTP request. The returned value becomes `ctx.state`
|
||||
* for `.at(...)` and assertions, giving stateful route tests type-safe setup.
|
||||
*/
|
||||
seeded<Next>(seed: (ctx: ScenarioContext) => Effect.Effect<Next>) {
|
||||
const builder = new ScenarioBuilder<Next>(this.state.method, this.state.path, this.state.name, this.state.auth)
|
||||
Object.assign(builder.state, this.state, { seed })
|
||||
return builder
|
||||
}
|
||||
|
||||
private done(expect: (ctx: SeededContext<S>, result: CallResult) => Effect.Effect<void>): ActiveScenario {
|
||||
const state = this.state
|
||||
return {
|
||||
kind: "active",
|
||||
method: state.method,
|
||||
path: state.path,
|
||||
name: state.name,
|
||||
project: state.project,
|
||||
seed: state.seed,
|
||||
authProbe: state.authProbe,
|
||||
// oxlint-disable-next-line typescript-eslint/no-unsafe-type-assertion -- `.seeded(...)` preserves the paired request/state type inside the builder.
|
||||
request: (ctx) => state.request(ctx as SeededContext<S>),
|
||||
// oxlint-disable-next-line typescript-eslint/no-unsafe-type-assertion -- `.seeded(...)` preserves the paired assertion/state type inside the builder.
|
||||
expect: (ctx, result) => expect(ctx as SeededContext<S>, result),
|
||||
capture: state.capture,
|
||||
reset: state.reset,
|
||||
auth: state.auth,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const routes = (auth: AuthPolicy) => ({
|
||||
get: (path: string, name: string) => new ScenarioBuilder("GET", path, name, auth),
|
||||
post: (path: string, name: string) => new ScenarioBuilder("POST", path, name, auth),
|
||||
put: (path: string, name: string) => new ScenarioBuilder("PUT", path, name, auth),
|
||||
patch: (path: string, name: string) => new ScenarioBuilder("PATCH", path, name, auth),
|
||||
delete: (path: string, name: string) => new ScenarioBuilder("DELETE", path, name, auth),
|
||||
})
|
||||
|
||||
export const http = {
|
||||
protected: routes("protected"),
|
||||
public: routes("public"),
|
||||
publicBypass: routes("public-bypass"),
|
||||
ticketBypass: routes("ticket-bypass"),
|
||||
}
|
||||
|
||||
export const pending = (method: Method, path: string, name: string, reason: string): TodoScenario => ({
|
||||
kind: "todo",
|
||||
method,
|
||||
path,
|
||||
name,
|
||||
reason,
|
||||
})
|
||||
|
||||
export function route(template: string, params: Record<string, string>) {
|
||||
return Object.entries(params).reduce(
|
||||
(next, [key, value]) => next.replaceAll(`{${key}}`, value).replaceAll(`:${key}`, value),
|
||||
template,
|
||||
)
|
||||
}
|
||||
|
||||
export function controlledPtyInput(title: string | undefined) {
|
||||
return {
|
||||
command: "/bin/sh",
|
||||
args: ["-c", "sleep 30"],
|
||||
...(title ? { title } : {}),
|
||||
}
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
import { Flag } from "@opencode-ai/core/flag/flag"
|
||||
import { Effect } from "effect"
|
||||
import path from "path"
|
||||
|
||||
const preserveExerciseGlobalRoot = !!process.env.OPENCODE_HTTPAPI_EXERCISE_GLOBAL
|
||||
export const exerciseGlobalRoot =
|
||||
process.env.OPENCODE_HTTPAPI_EXERCISE_GLOBAL ??
|
||||
path.join(process.env.TMPDIR ?? "/tmp", `opencode-httpapi-global-${process.pid}`)
|
||||
process.env.XDG_DATA_HOME = path.join(exerciseGlobalRoot, "data")
|
||||
process.env.XDG_CONFIG_HOME = path.join(exerciseGlobalRoot, "config")
|
||||
process.env.XDG_STATE_HOME = path.join(exerciseGlobalRoot, "state")
|
||||
process.env.XDG_CACHE_HOME = path.join(exerciseGlobalRoot, "cache")
|
||||
process.env.OPENCODE_DISABLE_SHARE = "true"
|
||||
export const exerciseConfigDirectory = path.join(exerciseGlobalRoot, "config", "opencode")
|
||||
export const exerciseDataDirectory = path.join(exerciseGlobalRoot, "data", "opencode")
|
||||
|
||||
const preserveExerciseDatabase = !!process.env.OPENCODE_HTTPAPI_EXERCISE_DB
|
||||
export const exerciseDatabasePath =
|
||||
process.env.OPENCODE_HTTPAPI_EXERCISE_DB ??
|
||||
path.join(process.env.TMPDIR ?? "/tmp", `opencode-httpapi-exercise-${process.pid}.db`)
|
||||
process.env.OPENCODE_DB = exerciseDatabasePath
|
||||
Flag.OPENCODE_DB = exerciseDatabasePath
|
||||
|
||||
export const original = {
|
||||
OPENCODE_SERVER_PASSWORD: Flag.OPENCODE_SERVER_PASSWORD,
|
||||
OPENCODE_SERVER_USERNAME: Flag.OPENCODE_SERVER_USERNAME,
|
||||
}
|
||||
|
||||
export const cleanupExercisePaths = Effect.promise(async () => {
|
||||
const fs = await import("fs/promises")
|
||||
if (!preserveExerciseDatabase) {
|
||||
await Promise.all(
|
||||
[exerciseDatabasePath, `${exerciseDatabasePath}-wal`, `${exerciseDatabasePath}-shm`].map((file) =>
|
||||
fs.rm(file, { force: true }).catch(() => undefined),
|
||||
),
|
||||
)
|
||||
}
|
||||
if (!preserveExerciseGlobalRoot)
|
||||
await fs.rm(exerciseGlobalRoot, { recursive: true, force: true }).catch(() => undefined)
|
||||
})
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,63 +0,0 @@
|
||||
import { Duration } from "effect"
|
||||
import { indent, pad } from "./assertions"
|
||||
import { routeKey } from "./routing"
|
||||
import type { Options, Result, Scenario } from "./types"
|
||||
|
||||
export const color = {
|
||||
dim: "\x1b[2m",
|
||||
green: "\x1b[32m",
|
||||
red: "\x1b[31m",
|
||||
yellow: "\x1b[33m",
|
||||
cyan: "\x1b[36m",
|
||||
reset: "\x1b[0m",
|
||||
}
|
||||
|
||||
export function printHeader(
|
||||
options: Options,
|
||||
effectRoutes: string[],
|
||||
selected: Scenario[],
|
||||
missing: string[],
|
||||
extra: Scenario[],
|
||||
paths: { database: string; global: string },
|
||||
) {
|
||||
console.log(`${color.cyan}HttpApi exerciser${color.reset}`)
|
||||
console.log(`${color.dim}db=${paths.database}${color.reset}`)
|
||||
console.log(`${color.dim}global=${paths.global}${color.reset}`)
|
||||
console.log(
|
||||
`${color.dim}mode=${options.mode} selected=${selected.length} scenarioTimeout=${Duration.format(options.scenarioTimeout)} effectRoutes=${effectRoutes.length} missing=${missing.length} extra=${extra.length}${color.reset}`,
|
||||
)
|
||||
console.log("")
|
||||
}
|
||||
|
||||
export function printResults(results: Result[], missing: string[], extra: Scenario[]) {
|
||||
for (const result of results) {
|
||||
if (result.status === "pass") {
|
||||
console.log(
|
||||
`${color.green}PASS${color.reset} ${pad(result.scenario.method, 6)} ${pad(result.scenario.path, 48)} ${result.scenario.name}`,
|
||||
)
|
||||
continue
|
||||
}
|
||||
if (result.status === "skip") {
|
||||
console.log(
|
||||
`${color.yellow}SKIP${color.reset} ${pad(result.scenario.method, 6)} ${pad(result.scenario.path, 48)} ${result.scenario.name} ${color.dim}${result.scenario.reason}${color.reset}`,
|
||||
)
|
||||
continue
|
||||
}
|
||||
console.log(
|
||||
`${color.red}FAIL${color.reset} ${pad(result.scenario.method, 6)} ${pad(result.scenario.path, 48)} ${result.scenario.name}`,
|
||||
)
|
||||
console.log(`${color.red}${indent(result.message)}${color.reset}`)
|
||||
}
|
||||
if (missing.length > 0) {
|
||||
console.log("\nMissing scenarios")
|
||||
for (const route of missing) console.log(`${color.red}MISS${color.reset} ${route}`)
|
||||
}
|
||||
if (extra.length > 0) {
|
||||
console.log("\nExtra scenarios")
|
||||
for (const scenario of extra)
|
||||
console.log(`${color.yellow}EXTRA${color.reset} ${routeKey(scenario)} ${scenario.name}`)
|
||||
}
|
||||
console.log(
|
||||
`\n${color.dim}summary pass=${results.filter((result) => result.status === "pass").length} fail=${results.filter((result) => result.status === "fail").length} skip=${results.filter((result) => result.status === "skip").length} missing=${missing.length} extra=${extra.length}${color.reset}`,
|
||||
)
|
||||
}
|
||||
@@ -1,96 +0,0 @@
|
||||
import { Duration } from "effect"
|
||||
import { OpenApiMethods, type OpenApiSpec, type Options, type Result, type Scenario } from "./types"
|
||||
|
||||
type ScenarioTimeout = `${number} ${Duration.Unit}`
|
||||
|
||||
const durationUnits = new Set<string>([
|
||||
"nano",
|
||||
"nanos",
|
||||
"micro",
|
||||
"micros",
|
||||
"milli",
|
||||
"millis",
|
||||
"second",
|
||||
"seconds",
|
||||
"minute",
|
||||
"minutes",
|
||||
"hour",
|
||||
"hours",
|
||||
"day",
|
||||
"days",
|
||||
"week",
|
||||
"weeks",
|
||||
])
|
||||
|
||||
export function routeKeys(spec: OpenApiSpec) {
|
||||
return Object.entries(spec.paths ?? {})
|
||||
.flatMap(([path, item]) =>
|
||||
OpenApiMethods.filter((method) => item[method]).map((method) => `${method.toUpperCase()} ${path}`),
|
||||
)
|
||||
.sort()
|
||||
}
|
||||
|
||||
export function routeKey(scenario: Scenario) {
|
||||
return `${scenario.method} ${scenario.path}`
|
||||
}
|
||||
|
||||
export function coverageResult(scenario: Scenario): Result {
|
||||
if (scenario.kind === "todo") return { status: "skip", scenario }
|
||||
return { status: "pass", scenario }
|
||||
}
|
||||
|
||||
export function parseOptions(args: string[]): Options {
|
||||
const mode = option(args, "--mode") ?? "effect"
|
||||
if (mode !== "effect" && mode !== "coverage" && mode !== "auth") throw new Error(`invalid --mode ${mode}`)
|
||||
return {
|
||||
mode,
|
||||
include: option(args, "--include"),
|
||||
startAt: option(args, "--start-at"),
|
||||
stopAt: option(args, "--stop-at"),
|
||||
failOnMissing: args.includes("--fail-on-missing"),
|
||||
failOnSkip: args.includes("--fail-on-skip"),
|
||||
scenarioTimeout: parseScenarioTimeout(option(args, "--scenario-timeout") ?? "30 seconds"),
|
||||
progress: args.includes("--progress"),
|
||||
trace: args.includes("--trace"),
|
||||
}
|
||||
}
|
||||
|
||||
export function matches(options: Options, scenario: Scenario) {
|
||||
if (!options.include) return true
|
||||
return (
|
||||
scenario.name.includes(options.include) ||
|
||||
scenario.path.includes(options.include) ||
|
||||
scenario.method.includes(options.include.toUpperCase())
|
||||
)
|
||||
}
|
||||
|
||||
export function selectedScenarios(options: Options, scenarios: Scenario[]) {
|
||||
const included = scenarios.filter((scenario) => matches(options, scenario))
|
||||
const start = options.startAt ? included.findIndex((scenario) => matchesName(options.startAt!, scenario)) : 0
|
||||
const end = options.stopAt
|
||||
? included.findIndex((scenario) => matchesName(options.stopAt!, scenario))
|
||||
: included.length - 1
|
||||
if (start === -1) throw new Error(`--start-at matched no scenario: ${options.startAt}`)
|
||||
if (end === -1) throw new Error(`--stop-at matched no scenario: ${options.stopAt}`)
|
||||
return included.slice(start, end + 1)
|
||||
}
|
||||
|
||||
function matchesName(value: string, scenario: Scenario) {
|
||||
return scenario.name.includes(value) || scenario.path.includes(value) || scenario.method.includes(value.toUpperCase())
|
||||
}
|
||||
|
||||
function option(args: string[], name: string) {
|
||||
const index = args.indexOf(name)
|
||||
if (index === -1) return undefined
|
||||
return args[index + 1]
|
||||
}
|
||||
|
||||
function parseScenarioTimeout(input: string) {
|
||||
if (!isScenarioTimeout(input)) throw new Error(`invalid --scenario-timeout ${input}`)
|
||||
return Duration.fromInputUnsafe(input)
|
||||
}
|
||||
|
||||
function isScenarioTimeout(input: string): input is ScenarioTimeout {
|
||||
const [amount, unit, extra] = input.trim().split(/\s+/)
|
||||
return extra === undefined && amount !== undefined && Number.isFinite(Number(amount)) && durationUnits.has(unit ?? "")
|
||||
}
|
||||
@@ -1,263 +0,0 @@
|
||||
import { Flag } from "@opencode-ai/core/flag/flag"
|
||||
import { ConfigV1 } from "@opencode-ai/core/v1/config/config"
|
||||
import { SessionV1 } from "@opencode-ai/core/v1/session"
|
||||
import { Cause, Duration, Effect, Layer, Scope } from "effect"
|
||||
import { TestLLMServer } from "../../lib/llm-server"
|
||||
import { MessageID, PartID } from "../../../src/session/schema"
|
||||
import { call, callAuthProbe, disposeApps } from "./backend"
|
||||
import { original } from "./environment"
|
||||
import { runtime } from "./runtime"
|
||||
import type { ActiveScenario, Options, ProjectOptions, Result, Scenario, ScenarioContext, SeededContext } from "./types"
|
||||
import { ProviderV2 } from "@opencode-ai/core/provider"
|
||||
import { ModelV2 } from "@opencode-ai/core/model"
|
||||
|
||||
export function runScenario(options: Options) {
|
||||
return (scenario: Scenario) => {
|
||||
if (scenario.kind === "todo") return Effect.succeed({ status: "skip", scenario } as Result)
|
||||
return runActive(options, scenario).pipe(
|
||||
Effect.timeoutOrElse({
|
||||
duration: options.scenarioTimeout,
|
||||
orElse: () => Effect.die(new Error(`scenario timed out after ${Duration.format(options.scenarioTimeout)}`)),
|
||||
}),
|
||||
Effect.as({ status: "pass", scenario } as Result),
|
||||
Effect.catchCause((cause) => Effect.succeed({ status: "fail" as const, scenario, message: Cause.pretty(cause) })),
|
||||
Effect.scoped,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
function runActive(options: Options, scenario: ActiveScenario) {
|
||||
if (options.mode === "auth") return runAuth(scenario)
|
||||
|
||||
return withContext(options, scenario, "shared", (ctx) =>
|
||||
Effect.gen(function* () {
|
||||
yield* trace(options, scenario, "request start")
|
||||
const result = yield* call(scenario, ctx)
|
||||
yield* trace(options, scenario, `response ${result.status}`)
|
||||
yield* trace(options, scenario, "expect start")
|
||||
yield* scenario.expect(ctx, result)
|
||||
yield* trace(options, scenario, "expect done")
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
function runAuth(scenario: ActiveScenario) {
|
||||
return Effect.gen(function* () {
|
||||
const result = yield* callAuthProbe(scenario, "missing")
|
||||
if (scenario.auth === "protected") {
|
||||
if (result.status !== 401) throw new Error(`auth expected 401, got ${result.status}`)
|
||||
const authed = yield* callAuthProbe(scenario, "valid")
|
||||
if (authed.status === 401) throw new Error("auth rejected valid credentials")
|
||||
return
|
||||
}
|
||||
|
||||
if (result.status === 401) throw new Error("auth expected public access, got 401")
|
||||
if (result.timedOut) throw new Error("auth expected public access, probe timed out")
|
||||
})
|
||||
}
|
||||
|
||||
function withContext<A, E>(
|
||||
options: Options,
|
||||
scenario: ActiveScenario,
|
||||
label: string,
|
||||
use: (ctx: SeededContext<unknown>) => Effect.Effect<A, E>,
|
||||
) {
|
||||
return Effect.acquireRelease(
|
||||
Effect.gen(function* () {
|
||||
yield* trace(options, scenario, `${label} context acquire start`)
|
||||
const llm = scenario.project?.llm ? yield* TestLLMServer : undefined
|
||||
const project = scenario.project
|
||||
const dir = project
|
||||
? yield* Effect.promise(async () => (await runtime()).tmpdir(projectOptions(project, llm?.url)))
|
||||
: undefined
|
||||
yield* trace(options, scenario, `${label} context acquire done`)
|
||||
return { dir, llm }
|
||||
}),
|
||||
(ctx) =>
|
||||
Effect.gen(function* () {
|
||||
yield* trace(options, scenario, `${label} tmpdir cleanup start`)
|
||||
yield* Effect.promise(async () => {
|
||||
await ctx.dir?.[Symbol.asyncDispose]()
|
||||
}).pipe(Effect.ignore)
|
||||
yield* trace(options, scenario, `${label} tmpdir cleanup done`)
|
||||
}),
|
||||
).pipe(
|
||||
Effect.flatMap((context) =>
|
||||
Effect.gen(function* () {
|
||||
yield* trace(options, scenario, `${label} runtime start`)
|
||||
const modules = yield* Effect.promise(() => runtime())
|
||||
const scope = yield* Scope.Scope
|
||||
const app = yield* Layer.buildWithMemoMap(modules.AppLayer, modules.memoMap, scope)
|
||||
yield* trace(options, scenario, `${label} runtime done`)
|
||||
const path = context.dir?.path
|
||||
const instance = path
|
||||
? yield* trace(options, scenario, `${label} instance load start`).pipe(
|
||||
Effect.andThen(
|
||||
modules.InstanceStore.Service.use((store) => store.load({ directory: path })).pipe(
|
||||
Effect.provide(app),
|
||||
Effect.catchCause((cause) =>
|
||||
Effect.sleep("100 millis").pipe(
|
||||
Effect.andThen(
|
||||
modules.InstanceStore.Service.use((store) => store.load({ directory: path })).pipe(
|
||||
Effect.provide(app),
|
||||
),
|
||||
),
|
||||
Effect.catchCause(() => Effect.failCause(cause)),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
Effect.tap(() => trace(options, scenario, `${label} instance load done`)),
|
||||
)
|
||||
: undefined
|
||||
const run = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
|
||||
effect.pipe(Effect.provideService(modules.InstanceRef, instance), Effect.provide(app))
|
||||
const directory = () => {
|
||||
if (!context.dir?.path) throw new Error("scenario needs a project directory")
|
||||
return context.dir.path
|
||||
}
|
||||
const llm = () => {
|
||||
if (!context.llm) throw new Error("scenario needs fake LLM")
|
||||
return context.llm
|
||||
}
|
||||
const base: ScenarioContext = {
|
||||
directory: context.dir?.path,
|
||||
headers: (extra) => ({
|
||||
...(context.dir?.path ? { "x-opencode-directory": context.dir.path } : {}),
|
||||
...extra,
|
||||
}),
|
||||
file: (name, content) =>
|
||||
Effect.promise(() => {
|
||||
return Bun.write(`${directory()}/${name}`, content)
|
||||
}).pipe(Effect.asVoid),
|
||||
session: (input) =>
|
||||
run(modules.Session.Service.use((svc) => svc.create({ title: input?.title, parentID: input?.parentID }))),
|
||||
sessionGet: (sessionID) =>
|
||||
run(modules.Session.Service.use((svc) => svc.get(sessionID))).pipe(
|
||||
Effect.catchCause(() => Effect.succeed(undefined)),
|
||||
),
|
||||
project: () =>
|
||||
Effect.sync(() => {
|
||||
if (!instance) throw new Error("scenario needs a project directory")
|
||||
return instance.project
|
||||
}),
|
||||
message: (sessionID, input) =>
|
||||
Effect.gen(function* () {
|
||||
const info: SessionV1.User = {
|
||||
id: MessageID.ascending(),
|
||||
sessionID,
|
||||
role: "user",
|
||||
time: { created: Date.now() },
|
||||
agent: "build",
|
||||
model: {
|
||||
providerID: ProviderV2.ID.opencode,
|
||||
modelID: ModelV2.ID.make("test"),
|
||||
},
|
||||
}
|
||||
const part: SessionV1.TextPart = {
|
||||
id: PartID.ascending(),
|
||||
sessionID,
|
||||
messageID: info.id,
|
||||
type: "text",
|
||||
text: input?.text ?? "hello",
|
||||
}
|
||||
yield* run(
|
||||
modules.Session.Service.use((svc) =>
|
||||
Effect.gen(function* () {
|
||||
yield* svc.updateMessage(info)
|
||||
yield* svc.updatePart(part)
|
||||
}),
|
||||
),
|
||||
)
|
||||
return { info, part }
|
||||
}),
|
||||
messages: (sessionID) =>
|
||||
run(modules.Session.Service.use((svc) => svc.messages({ sessionID }).pipe(Effect.orDie))),
|
||||
worktree: (input) => run(modules.Worktree.Service.use((svc) => svc.create(input).pipe(Effect.orDie))),
|
||||
worktreeRemove: (directory) =>
|
||||
run(modules.Worktree.Service.use((svc) => svc.remove({ directory })).pipe(Effect.ignore)),
|
||||
llmText: (value) => Effect.suspend(() => llm().text(value)),
|
||||
llmWait: (count) => Effect.suspend(() => llm().wait(count)),
|
||||
tuiRequest: (request) => Effect.sync(() => modules.Tui.submitTuiRequest(request)),
|
||||
}
|
||||
yield* trace(options, scenario, `${label} seed start`)
|
||||
const state = yield* scenario.seed(base)
|
||||
yield* trace(options, scenario, `${label} seed done`)
|
||||
yield* trace(options, scenario, `${label} use start`)
|
||||
const result = yield* use({ ...base, state })
|
||||
yield* trace(options, scenario, `${label} use done`)
|
||||
return result
|
||||
}).pipe(Effect.ensuring(context.llm ? context.llm.reset : Effect.void)),
|
||||
),
|
||||
Effect.ensuring(scenario.reset ? resetState : Effect.void),
|
||||
)
|
||||
}
|
||||
|
||||
function trace(options: Options, scenario: ActiveScenario, phase: string) {
|
||||
return Effect.sync(() => {
|
||||
if (!options.trace) return
|
||||
console.log(`[trace] ${scenario.name}: ${phase}`)
|
||||
})
|
||||
}
|
||||
|
||||
function projectOptions(
|
||||
project: ProjectOptions,
|
||||
llmUrl: string | undefined,
|
||||
): { git?: boolean; config?: Partial<ConfigV1.Info> } {
|
||||
if (!project.llm || !llmUrl) return { git: project.git, config: project.config }
|
||||
const fake = fakeLlmConfig(llmUrl)
|
||||
return {
|
||||
git: project.git,
|
||||
config: {
|
||||
...fake,
|
||||
...project.config,
|
||||
provider: {
|
||||
...fake.provider,
|
||||
...project.config?.provider,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
function fakeLlmConfig(url: string): Partial<ConfigV1.Info> {
|
||||
return {
|
||||
model: "test/test-model",
|
||||
small_model: "test/test-model",
|
||||
provider: {
|
||||
test: {
|
||||
name: "Test",
|
||||
id: "test",
|
||||
env: [],
|
||||
npm: "@ai-sdk/openai-compatible",
|
||||
models: {
|
||||
"test-model": {
|
||||
id: "test-model",
|
||||
name: "Test Model",
|
||||
attachment: false,
|
||||
reasoning: false,
|
||||
temperature: false,
|
||||
tool_call: true,
|
||||
release_date: "2025-01-01",
|
||||
limit: { context: 100000, output: 10000 },
|
||||
cost: { input: 0, output: 0 },
|
||||
options: {},
|
||||
},
|
||||
},
|
||||
options: {
|
||||
apiKey: "test-key",
|
||||
baseURL: url,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
const resetState = Effect.promise(async () => {
|
||||
const modules = await runtime()
|
||||
Flag.OPENCODE_SERVER_PASSWORD = original.OPENCODE_SERVER_PASSWORD
|
||||
Flag.OPENCODE_SERVER_USERNAME = original.OPENCODE_SERVER_USERNAME
|
||||
await disposeApps()
|
||||
await modules.disposeAllInstances()
|
||||
await modules.resetDatabase()
|
||||
await Bun.sleep(25)
|
||||
})
|
||||
@@ -1,46 +0,0 @@
|
||||
export type Runtime = {
|
||||
PublicApi: (typeof import("../../../src/server/routes/instance/httpapi/public"))["PublicApi"]
|
||||
HttpApiApp: (typeof import("../../../src/server/routes/instance/httpapi/server"))["HttpApiApp"]
|
||||
AppLayer: (typeof import("../../../src/effect/app-runtime"))["AppLayer"]
|
||||
memoMap: import("effect").Layer.MemoMap
|
||||
InstanceRef: (typeof import("../../../src/effect/instance-ref"))["InstanceRef"]
|
||||
InstanceStore: (typeof import("../../../src/project/instance-store"))["InstanceStore"]
|
||||
Session: (typeof import("../../../src/session/session"))["Session"]
|
||||
Worktree: (typeof import("../../../src/worktree"))["Worktree"]
|
||||
Tui: typeof import("../../../src/server/shared/tui-control")
|
||||
disposeAllInstances: (typeof import("../../fixture/fixture"))["disposeAllInstances"]
|
||||
tmpdir: (typeof import("../../fixture/fixture"))["tmpdir"]
|
||||
resetDatabase: (typeof import("../../fixture/db"))["resetDatabase"]
|
||||
}
|
||||
|
||||
let runtimePromise: Promise<Runtime> | undefined
|
||||
|
||||
export function runtime() {
|
||||
return (runtimePromise ??= (async () => {
|
||||
const publicApi = await import("../../../src/server/routes/instance/httpapi/public")
|
||||
const httpApiServer = await import("../../../src/server/routes/instance/httpapi/server")
|
||||
const appRuntime = await import("../../../src/effect/app-runtime")
|
||||
const { Layer } = await import("effect")
|
||||
const instanceRef = await import("../../../src/effect/instance-ref")
|
||||
const instanceStore = await import("../../../src/project/instance-store")
|
||||
const session = await import("../../../src/session/session")
|
||||
const worktree = await import("../../../src/worktree")
|
||||
const tui = await import("../../../src/server/shared/tui-control")
|
||||
const fixture = await import("../../fixture/fixture")
|
||||
const db = await import("../../fixture/db")
|
||||
return {
|
||||
PublicApi: publicApi.PublicApi,
|
||||
HttpApiApp: httpApiServer.HttpApiApp,
|
||||
AppLayer: appRuntime.AppLayer,
|
||||
memoMap: Layer.makeMemoMapUnsafe(),
|
||||
InstanceRef: instanceRef.InstanceRef,
|
||||
InstanceStore: instanceStore.InstanceStore,
|
||||
Session: session.Session,
|
||||
Worktree: worktree.Worktree,
|
||||
Tui: tui,
|
||||
disposeAllInstances: fixture.disposeAllInstances,
|
||||
tmpdir: fixture.tmpdir,
|
||||
resetDatabase: db.resetDatabase,
|
||||
}
|
||||
})())
|
||||
}
|
||||
@@ -1,115 +0,0 @@
|
||||
import type { Duration, Effect } from "effect"
|
||||
import { ConfigV1 } from "@opencode-ai/core/v1/config/config"
|
||||
import { SessionV1 } from "@opencode-ai/core/v1/session"
|
||||
import type { Project } from "../../../src/project/project"
|
||||
import type { Worktree } from "../../../src/worktree"
|
||||
import type { SessionID } from "../../../src/session/schema"
|
||||
|
||||
export const OpenApiMethods = ["get", "post", "put", "delete", "patch"] as const
|
||||
export const Methods = ["GET", "POST", "PUT", "DELETE", "PATCH"] as const
|
||||
|
||||
export type Method = (typeof Methods)[number]
|
||||
export type OpenApiMethod = (typeof OpenApiMethods)[number]
|
||||
export type Mode = "effect" | "coverage" | "auth"
|
||||
export type CaptureMode = "full" | "stream"
|
||||
export type AuthPolicy = "protected" | "public" | "public-bypass" | "ticket-bypass"
|
||||
export type ProjectOptions = { git?: boolean; config?: Partial<ConfigV1.Info>; llm?: boolean }
|
||||
export type OpenApiSpec = { paths?: Record<string, Partial<Record<OpenApiMethod, unknown>>> }
|
||||
export type JsonObject = Record<string, unknown>
|
||||
|
||||
export type Options = {
|
||||
mode: Mode
|
||||
include: string | undefined
|
||||
startAt: string | undefined
|
||||
stopAt: string | undefined
|
||||
failOnMissing: boolean
|
||||
failOnSkip: boolean
|
||||
scenarioTimeout: Duration.Duration
|
||||
progress: boolean
|
||||
trace: boolean
|
||||
}
|
||||
|
||||
export type RequestSpec = {
|
||||
path: string
|
||||
headers?: Record<string, string>
|
||||
body?: unknown
|
||||
}
|
||||
|
||||
export type CallResult = {
|
||||
status: number
|
||||
contentType: string
|
||||
body: unknown
|
||||
text: string
|
||||
timedOut: boolean
|
||||
}
|
||||
|
||||
export type BackendApp = {
|
||||
request(input: string | URL | Request, init?: RequestInit): Response | Promise<Response>
|
||||
}
|
||||
|
||||
/** Effect-native helpers available while setting up and asserting a scenario. */
|
||||
export type ScenarioContext = {
|
||||
directory: string | undefined
|
||||
headers: (extra?: Record<string, string>) => Record<string, string>
|
||||
file: (name: string, content: string) => Effect.Effect<void>
|
||||
session: (input?: { title?: string; parentID?: SessionID }) => Effect.Effect<SessionInfo>
|
||||
sessionGet: (sessionID: SessionID) => Effect.Effect<SessionInfo | undefined>
|
||||
project: () => Effect.Effect<Project.Info>
|
||||
message: (sessionID: SessionID, input?: { text?: string }) => Effect.Effect<MessageSeed>
|
||||
messages: (sessionID: SessionID) => Effect.Effect<SessionV1.WithParts[]>
|
||||
worktree: (input?: { name?: string }) => Effect.Effect<Worktree.Info>
|
||||
worktreeRemove: (directory: string) => Effect.Effect<void>
|
||||
llmText: (value: string) => Effect.Effect<void>
|
||||
llmWait: (count: number) => Effect.Effect<void>
|
||||
tuiRequest: (request: { path: string; body: unknown }) => Effect.Effect<void>
|
||||
}
|
||||
|
||||
/** Scenario context after `.seeded(...)`; `state` preserves the seed return type in the DSL. */
|
||||
export type SeededContext<S> = ScenarioContext & {
|
||||
state: S
|
||||
}
|
||||
|
||||
export type Scenario = ActiveScenario | TodoScenario
|
||||
export type ActiveScenario = {
|
||||
kind: "active"
|
||||
method: Method
|
||||
path: string
|
||||
name: string
|
||||
project: ProjectOptions | undefined
|
||||
seed: (ctx: ScenarioContext) => Effect.Effect<unknown>
|
||||
request: (ctx: SeededContext<unknown>) => RequestSpec
|
||||
authProbe: RequestSpec | undefined
|
||||
expect: (ctx: SeededContext<unknown>, result: CallResult) => Effect.Effect<void>
|
||||
capture: CaptureMode
|
||||
reset: boolean
|
||||
auth: AuthPolicy
|
||||
}
|
||||
|
||||
export type BuilderState<S> = {
|
||||
method: Method
|
||||
path: string
|
||||
name: string
|
||||
project: ProjectOptions | undefined
|
||||
seed: (ctx: ScenarioContext) => Effect.Effect<S>
|
||||
request: (ctx: SeededContext<S>) => RequestSpec
|
||||
authProbe: RequestSpec | undefined
|
||||
capture: CaptureMode
|
||||
reset: boolean
|
||||
auth: AuthPolicy
|
||||
}
|
||||
|
||||
export type TodoScenario = {
|
||||
kind: "todo"
|
||||
method: Method
|
||||
path: string
|
||||
name: string
|
||||
reason: string
|
||||
}
|
||||
|
||||
export type Result =
|
||||
| { status: "pass"; scenario: ActiveScenario }
|
||||
| { status: "fail"; scenario: ActiveScenario; message: string }
|
||||
| { status: "skip"; scenario: TodoScenario }
|
||||
|
||||
export type SessionInfo = { id: SessionID; title: string; parentID?: SessionID }
|
||||
export type MessageSeed = { info: SessionV1.User; part: SessionV1.TextPart }
|
||||
Reference in New Issue
Block a user