Skip to content

Commit 379e7f3

Browse files
authored
test(httpapi): cover sdk effect routes (#24836)
1 parent 366d11e commit 379e7f3

1 file changed

Lines changed: 84 additions & 0 deletions

File tree

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import { afterEach, describe, expect, test } from "bun:test"
2+
import { Flag } from "@opencode-ai/core/flag/flag"
3+
import { createOpencodeClient } from "@opencode-ai/sdk/v2"
4+
import { ExperimentalHttpApiServer } from "../../src/server/routes/instance/httpapi/server"
5+
import path from "path"
6+
import { resetDatabase } from "../fixture/db"
7+
import { tmpdir } from "../fixture/fixture"
8+
9+
const original = Flag.OPENCODE_EXPERIMENTAL_HTTPAPI
10+
11+
function client(directory?: string) {
12+
Flag.OPENCODE_EXPERIMENTAL_HTTPAPI = true
13+
const handler = ExperimentalHttpApiServer.webHandler().handler
14+
const fetch = Object.assign(
15+
(request: RequestInfo | URL, init?: RequestInit) =>
16+
handler(new Request(request, init), ExperimentalHttpApiServer.context),
17+
{ preconnect: globalThis.fetch.preconnect },
18+
) satisfies typeof globalThis.fetch
19+
return createOpencodeClient({
20+
baseUrl: "http://localhost",
21+
directory,
22+
fetch,
23+
})
24+
}
25+
26+
async function expectStatus(result: Promise<{ response: Response }>, status: number) {
27+
expect((await result).response.status).toBe(status)
28+
}
29+
30+
afterEach(async () => {
31+
Flag.OPENCODE_EXPERIMENTAL_HTTPAPI = original
32+
await resetDatabase()
33+
})
34+
35+
describe("HttpApi SDK", () => {
36+
test("uses the generated SDK for global and control routes", async () => {
37+
const sdk = client()
38+
const health = await sdk.global.health()
39+
40+
expect(health.response.status).toBe(200)
41+
expect(health.data).toMatchObject({ healthy: true })
42+
43+
const events = await sdk.global.event({ signal: AbortSignal.timeout(1_000) })
44+
try {
45+
const first = await events.stream.next()
46+
expect(first.value).toMatchObject({ payload: { type: "server.connected" } })
47+
} finally {
48+
await events.stream.return(undefined)
49+
}
50+
51+
const log = await sdk.app.log({ service: "httpapi-sdk-test", level: "info", message: "hello" })
52+
expect(log.response.status).toBe(200)
53+
expect(log.data).toBe(true)
54+
55+
await expectStatus(sdk.auth.set({ providerID: "test" }), 400)
56+
})
57+
58+
test("uses the generated SDK for safe instance routes", async () => {
59+
await using tmp = await tmpdir({
60+
config: { formatter: false, lsp: false },
61+
init: (dir) => Bun.write(path.join(dir, "hello.txt"), "hello"),
62+
})
63+
const sdk = client(tmp.path)
64+
65+
const file = await sdk.file.read({ path: "hello.txt" })
66+
expect(file.response.status).toBe(200)
67+
expect(file.data).toMatchObject({ content: "hello" })
68+
69+
const session = await sdk.session.create({ title: "sdk" })
70+
expect(session.response.status).toBe(200)
71+
expect(session.data).toMatchObject({ title: "sdk" })
72+
73+
const listed = await sdk.session.list({ roots: true, limit: 10 })
74+
expect(listed.response.status).toBe(200)
75+
expect(listed.data?.map((item) => item.id)).toContain(session.data?.id)
76+
77+
await Promise.all([
78+
expectStatus(sdk.project.current(), 200),
79+
expectStatus(sdk.config.get(), 200),
80+
expectStatus(sdk.config.providers(), 200),
81+
expectStatus(sdk.find.files({ query: "hello", limit: 10 }), 200),
82+
])
83+
})
84+
})

0 commit comments

Comments
 (0)