refactor(server): canonicalize service API (#31049)
This commit is contained in:
parent
53ff1b57c9
commit
fe0c4f8c74
388 changed files with 7075 additions and 4064 deletions
13
packages/tui/test/fixture/fixture.ts
Normal file
13
packages/tui/test/fixture/fixture.ts
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
import { mkdtemp, realpath, rm } from "node:fs/promises"
|
||||
import path from "node:path"
|
||||
import os from "node:os"
|
||||
|
||||
export async function tmpdir() {
|
||||
const directory = await realpath(await mkdtemp(path.join(os.tmpdir(), "opencode-tui-test-")))
|
||||
return {
|
||||
path: directory,
|
||||
async [Symbol.asyncDispose]() {
|
||||
await rm(directory, { recursive: true, force: true })
|
||||
},
|
||||
}
|
||||
}
|
||||
42
packages/tui/test/fixture/tui-environment.tsx
Normal file
42
packages/tui/test/fixture/tui-environment.tsx
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
/** @jsxImportSource @opentui/solid */
|
||||
import { createTuiEnvironment, TuiEnvironmentProvider, type TuiEnvironment } from "../../src/runtime"
|
||||
import type { ParentProps } from "solid-js"
|
||||
|
||||
export function TestTuiEnvironmentProvider(
|
||||
props: ParentProps<{
|
||||
cwd?: string
|
||||
directory?: string
|
||||
paths?: Partial<TuiEnvironment["paths"]>
|
||||
capabilities?: Partial<TuiEnvironment["capabilities"]>
|
||||
editor?: Partial<TuiEnvironment["editor"]>
|
||||
}>,
|
||||
) {
|
||||
return (
|
||||
<TuiEnvironmentProvider
|
||||
value={createTuiEnvironment({
|
||||
cwd: props.cwd ?? props.directory ?? "/tmp/opencode/packages/tui",
|
||||
platform: "linux",
|
||||
paths: {
|
||||
home: "/tmp/opencode/home",
|
||||
state: "/tmp/opencode/state",
|
||||
worktree: "/tmp/opencode",
|
||||
...props.paths,
|
||||
},
|
||||
capabilities: {
|
||||
mouse: true,
|
||||
copyOnSelect: true,
|
||||
terminalTitle: false,
|
||||
terminalSuspend: false,
|
||||
workspaces: false,
|
||||
showTimeToFirstDraw: false,
|
||||
...props.capabilities,
|
||||
},
|
||||
terminal: {},
|
||||
editor: { zedTerminal: false, ...props.editor },
|
||||
skipInitialLoading: false,
|
||||
})}
|
||||
>
|
||||
{props.children}
|
||||
</TuiEnvironmentProvider>
|
||||
)
|
||||
}
|
||||
36
packages/tui/test/fixture/tui-plugin.ts
Normal file
36
packages/tui/test/fixture/tui-plugin.ts
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
import type { TuiPluginApi } from "@opencode-ai/plugin/tui"
|
||||
import { RGBA } from "@opentui/core"
|
||||
import { createTuiResolvedConfig } from "./tui-runtime"
|
||||
|
||||
type Opts = {
|
||||
client?: TuiPluginApi["client"]
|
||||
keymap?: TuiPluginApi["keymap"]
|
||||
attention?: Partial<TuiPluginApi["attention"]>
|
||||
event?: TuiPluginApi["event"]
|
||||
state?: { session?: Partial<TuiPluginApi["state"]["session"]> }
|
||||
}
|
||||
|
||||
export function createTuiPluginApi(opts: Opts = {}) {
|
||||
const values = new Map<string, unknown>()
|
||||
const color = RGBA.fromInts(200, 200, 200)
|
||||
const dialog = { clear() {}, replace() {}, setSize() {}, size: "medium" as const, depth: 0, open: false }
|
||||
return {
|
||||
attention: { notify: async () => ({ ok: false, notification: false, sound: false }), ...opts.attention },
|
||||
client: opts.client,
|
||||
event: opts.event,
|
||||
keymap: opts.keymap,
|
||||
kv: {
|
||||
get(name: string, fallback?: unknown) {
|
||||
return values.has(name) ? values.get(name) : fallback
|
||||
},
|
||||
set(name: string, value: unknown) {
|
||||
values.set(name, value)
|
||||
},
|
||||
ready: true,
|
||||
},
|
||||
state: { session: { get: () => undefined, ...opts.state?.session } },
|
||||
theme: { current: new Proxy({}, { get: () => color }) },
|
||||
tuiConfig: createTuiResolvedConfig(),
|
||||
ui: { dialog },
|
||||
} as unknown as TuiPluginApi
|
||||
}
|
||||
12
packages/tui/test/fixture/tui-runtime.ts
Normal file
12
packages/tui/test/fixture/tui-runtime.ts
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
import { resolve, type Info, type Resolved } from "../../src/config"
|
||||
import { TuiKeybind } from "../../src/config/keybind"
|
||||
|
||||
type ResolvedInput = Omit<Info, "attention" | "keybinds" | "leader_timeout"> & {
|
||||
attention?: Partial<Resolved["attention"]>
|
||||
keybinds?: Partial<TuiKeybind.Keybinds>
|
||||
leader_timeout?: number
|
||||
}
|
||||
|
||||
export function createTuiResolvedConfig(input: ResolvedInput = {}) {
|
||||
return resolve(input, { terminalSuspend: process.platform !== "win32" })
|
||||
}
|
||||
58
packages/tui/test/fixture/tui-sdk.ts
Normal file
58
packages/tui/test/fixture/tui-sdk.ts
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
import type { GlobalEvent } from "@opencode-ai/sdk/v2"
|
||||
import type { EventSource } from "../../src/context/sdk"
|
||||
|
||||
export const worktree = "/tmp/opencode"
|
||||
export const directory = `${worktree}/packages/tui`
|
||||
|
||||
export function json(data: unknown, init?: ResponseInit) {
|
||||
return new Response(JSON.stringify(data), {
|
||||
...init,
|
||||
headers: { "content-type": "application/json", ...(init?.headers ?? {}) },
|
||||
})
|
||||
}
|
||||
|
||||
export function eventSource(): EventSource {
|
||||
return { subscribe: async () => () => {} }
|
||||
}
|
||||
|
||||
export function createEventSource() {
|
||||
let fn: ((event: GlobalEvent) => void) | undefined
|
||||
return {
|
||||
source: {
|
||||
subscribe: async (handler: (event: GlobalEvent) => void) => {
|
||||
fn = handler
|
||||
return () => {
|
||||
if (fn === handler) fn = undefined
|
||||
}
|
||||
},
|
||||
} satisfies EventSource,
|
||||
emit(event: GlobalEvent) {
|
||||
if (!fn) throw new Error("event source not ready")
|
||||
fn(event)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
export type FetchHandler = (url: URL) => Response | Promise<Response> | undefined
|
||||
|
||||
export function createFetch(override?: FetchHandler) {
|
||||
const session = [] as URL[]
|
||||
const fetch = (async (input: RequestInfo | URL) => {
|
||||
const url = new URL(input instanceof Request ? input.url : String(input))
|
||||
if (url.pathname === "/session") session.push(url)
|
||||
const overridden = await override?.(url)
|
||||
if (overridden) return overridden
|
||||
|
||||
if (["/agent", "/command", "/experimental/workspace", "/experimental/workspace/status", "/formatter", "/lsp"].includes(url.pathname)) return json([])
|
||||
if (["/config", "/experimental/resource", "/mcp", "/provider/auth", "/session/status"].includes(url.pathname)) return json({})
|
||||
if (url.pathname === "/config/providers") return json({ providers: {}, default: {} })
|
||||
if (url.pathname === "/experimental/console") return json({ consoleManagedProviders: [], switchableOrgCount: 0 })
|
||||
if (url.pathname === "/path") return json({ home: "", state: "", config: "", worktree, directory })
|
||||
if (url.pathname === "/project/current") return json({ id: "proj_test" })
|
||||
if (url.pathname === "/provider") return json({ all: [], default: {}, connected: [] })
|
||||
if (url.pathname === "/session") return json([])
|
||||
if (url.pathname === "/vcs") return json({ branch: "main" })
|
||||
throw new Error(`unexpected request: ${url.pathname}`)
|
||||
}) as typeof globalThis.fetch
|
||||
return { fetch, session }
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue