+21

![opencode-agent[bot] <219766164+opencode-agent[bot]@users.noreply.github.com>](/assets/img/avatar_default.png)



![opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com>](/assets/img/avatar_default.png)



James Long
Brendan Allan
Kit Langton
opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com>
Affan Ali
affanali2k3
Frank
opencode-agent[bot] <219766164+opencode-agent[bot]@users.noreply.github.com>
𝓛𝓲𝓽𝓽𝓵𝓮 𝓕𝓻𝓪𝓷𝓴
Aiden Cline
Jay V
Dax Raad
Aarav Sareen
OpeOginni
Luke Parker
Ben Guthrie
Dax
Filip
Max Anderson
Brendan Allan
Jack
Shoubhit Dash
Dustin Deus
starptech
Aiden Cline
usrnk1
Jay
runvip
opencode
Julian Coy
Vladimir Glafirov
8c94e9005f
Co-authored-by: Brendan Allan <14191578+Brendonovich@users.noreply.github.com> Co-authored-by: Kit Langton <kit.langton@gmail.com> Co-authored-by: opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com> Co-authored-by: Affan Ali <93028901+affanali2k3@users.noreply.github.com> Co-authored-by: affanali2k3 <affanalikhanxx@gmail.com> Co-authored-by: Frank <frank@anoma.ly> Co-authored-by: opencode-agent[bot] <219766164+opencode-agent[bot]@users.noreply.github.com> Co-authored-by: 𝓛𝓲𝓽𝓽𝓵𝓮 𝓕𝓻𝓪𝓷𝓴 <little-frank@opencord.local> Co-authored-by: Aiden Cline <63023139+rekram1-node@users.noreply.github.com> Co-authored-by: Jay V <air@live.ca> Co-authored-by: Dax Raad <d@ironbay.co> Co-authored-by: Aarav Sareen <96787824+arvsrn@users.noreply.github.com> Co-authored-by: OpeOginni <107570612+OpeOginni@users.noreply.github.com> Co-authored-by: Luke Parker <10430890+Hona@users.noreply.github.com> Co-authored-by: Ben Guthrie <benjee.012@gmail.com> Co-authored-by: Dax <mail@thdxr.com> Co-authored-by: Filip <34747899+neriousy@users.noreply.github.com> Co-authored-by: Max Anderson <max.a.anderson95@gmail.com> Co-authored-by: Brendan Allan <git@brendonovich.dev> Co-authored-by: Jack <jack@anoma.ly> Co-authored-by: Shoubhit Dash <shoubhit2005@gmail.com> Co-authored-by: Dustin Deus <deusdustin@gmail.com> Co-authored-by: starptech <starptech@starptechs-MBP.fritz.box> Co-authored-by: Aiden Cline <aidenpcline@gmail.com> Co-authored-by: usrnk1 <7547651+usrnk1@users.noreply.github.com> Co-authored-by: Jay <53023+jayair@users.noreply.github.com> Co-authored-by: runvip <164729189+runvip@users.noreply.github.com> Co-authored-by: opencode <opencode@sst.dev> Co-authored-by: Julian Coy <julian@ex-machina.co> Co-authored-by: Vladimir Glafirov <vglafirov@gitlab.com>
299 lines
10 KiB
TypeScript
299 lines
10 KiB
TypeScript
import { afterEach, describe, expect } from "bun:test"
|
|
import { LayerNode } from "@opencode-ai/core/effect/layer-node"
|
|
import { Deferred, Effect, Fiber, Layer } from "effect"
|
|
import { HttpClient, HttpClientResponse } from "effect/unstable/http"
|
|
import { eq } from "drizzle-orm"
|
|
import { GlobalBus, type GlobalEvent } from "@/bus/global"
|
|
import { ExperimentalPaths } from "../../src/server/routes/instance/httpapi/groups/experimental"
|
|
import { Session } from "@/session/session"
|
|
import { SessionTable } from "@opencode-ai/core/session/sql"
|
|
import { Database } from "@opencode-ai/core/database/database"
|
|
import { AccountV2 } from "@opencode-ai/core/account"
|
|
import { AccountTable } from "@opencode-ai/core/account/sql"
|
|
import { Worktree } from "../../src/worktree"
|
|
import { resetDatabase } from "../fixture/db"
|
|
import { disposeAllInstances, TestInstance } from "../fixture/fixture"
|
|
import { testEffect } from "../lib/effect"
|
|
import { httpApiLayer, requestInDirectory } from "./httpapi-layer"
|
|
|
|
const it = testEffect(Layer.mergeAll(LayerNode.compile(LayerNode.group([Session.node, Database.node])), httpApiLayer))
|
|
const testWorktreeMutations = process.platform === "win32" ? it.instance.skip : it.instance
|
|
|
|
function request(path: string, directory: string, init: RequestInit = {}) {
|
|
return requestInDirectory(path, directory, init)
|
|
}
|
|
|
|
function createSession(input?: Session.CreateInput) {
|
|
return Session.use.create(input)
|
|
}
|
|
|
|
function json<T>(response: HttpClientResponse.HttpClientResponse) {
|
|
return response.json.pipe(Effect.map((value) => value as T))
|
|
}
|
|
|
|
function waitReady(input: { directory?: string; name?: string }) {
|
|
return Effect.gen(function* () {
|
|
const ready = yield* Deferred.make<void>()
|
|
const on = (event: GlobalEvent) => {
|
|
if (event.payload.type !== Worktree.Event.Ready.type) return
|
|
if (input.directory && event.directory !== input.directory) return
|
|
if (input.name && event.payload.properties.name !== input.name) return
|
|
Deferred.doneUnsafe(ready, Effect.void)
|
|
}
|
|
|
|
GlobalBus.on("event", on)
|
|
yield* Effect.addFinalizer(() => Effect.sync(() => GlobalBus.off("event", on)))
|
|
|
|
return yield* Deferred.await(ready).pipe(
|
|
Effect.timeoutOrElse({
|
|
duration: "10 seconds",
|
|
orElse: () => Effect.fail(new Error("timed out waiting for worktree.ready")),
|
|
}),
|
|
)
|
|
})
|
|
}
|
|
|
|
function insertAccount() {
|
|
return Effect.acquireRelease(
|
|
Effect.gen(function* () {
|
|
const { db } = yield* Database.Service
|
|
yield* db
|
|
.insert(AccountTable)
|
|
.values({
|
|
id: AccountV2.ID.make("account-test"),
|
|
email: "test@example.com",
|
|
url: "https://console.example.com",
|
|
access_token: AccountV2.AccessToken.make("access"),
|
|
refresh_token: AccountV2.RefreshToken.make("refresh"),
|
|
time_created: Date.now(),
|
|
time_updated: Date.now(),
|
|
})
|
|
.run()
|
|
.pipe(Effect.orDie)
|
|
return "account-test"
|
|
}),
|
|
(id) =>
|
|
Database.Service.use(({ db }) =>
|
|
db
|
|
.delete(AccountTable)
|
|
.where(eq(AccountTable.id, AccountV2.ID.make(id)))
|
|
.run()
|
|
.pipe(Effect.orDie),
|
|
),
|
|
)
|
|
}
|
|
|
|
function setSessionUpdated(session: Session.Info, updated: number) {
|
|
return Effect.gen(function* () {
|
|
const { db } = yield* Database.Service
|
|
yield* db
|
|
.update(SessionTable)
|
|
.set({ time_updated: updated })
|
|
.where(eq(SessionTable.id, session.id))
|
|
.run()
|
|
.pipe(Effect.orDie)
|
|
})
|
|
}
|
|
|
|
function withCreatedWorktree(
|
|
directory: string,
|
|
use: (info: Worktree.Info) => Effect.Effect<void, unknown, HttpClient.HttpClient>,
|
|
) {
|
|
const name = "api-test"
|
|
const headers = { "content-type": "application/json" }
|
|
return Effect.acquireUseRelease(
|
|
Effect.gen(function* () {
|
|
const ready = yield* waitReady({ name }).pipe(Effect.forkScoped)
|
|
const created = yield* request(ExperimentalPaths.worktree, directory, {
|
|
method: "POST",
|
|
headers,
|
|
body: JSON.stringify({ name }),
|
|
})
|
|
|
|
expect(created.status).toBe(200)
|
|
const info = yield* json<Worktree.Info>(created)
|
|
expect(info).toMatchObject({ name, branch: "opencode/api-test" })
|
|
yield* Fiber.join(ready)
|
|
return info
|
|
}),
|
|
use,
|
|
(info) =>
|
|
Effect.gen(function* () {
|
|
const removed = yield* request(ExperimentalPaths.worktree, directory, {
|
|
method: "DELETE",
|
|
headers,
|
|
body: JSON.stringify({ directory: info.directory }),
|
|
})
|
|
if (removed.status !== 200) return yield* Effect.fail(new Error(`failed to remove worktree: ${removed.status}`))
|
|
const ok = yield* json<boolean>(removed)
|
|
if (!ok) return yield* Effect.fail(new Error(`failed to remove worktree ${info.directory}`))
|
|
}),
|
|
)
|
|
}
|
|
|
|
afterEach(async () => {
|
|
await disposeAllInstances()
|
|
await resetDatabase()
|
|
})
|
|
|
|
describe("experimental HttpApi", () => {
|
|
it.instance(
|
|
"serves read-only experimental endpoints through the default server app",
|
|
() =>
|
|
Effect.gen(function* () {
|
|
const tmp = yield* TestInstance
|
|
const directory = tmp.directory
|
|
const [consoleState, consoleOrgs, toolList, toolIDs, worktrees, resources] = yield* Effect.all(
|
|
[
|
|
request(ExperimentalPaths.console, directory),
|
|
request(ExperimentalPaths.consoleOrgs, directory),
|
|
request(`${ExperimentalPaths.tool}?provider=opencode&model=gpt-5`, directory),
|
|
request(ExperimentalPaths.toolIDs, directory),
|
|
request(ExperimentalPaths.worktree, directory),
|
|
request(ExperimentalPaths.resource, directory),
|
|
],
|
|
{ concurrency: "unbounded" },
|
|
)
|
|
|
|
expect(consoleState.status).toBe(200)
|
|
expect(yield* json(consoleState)).toEqual({
|
|
consoleManagedProviders: [],
|
|
switchableOrgCount: 0,
|
|
})
|
|
|
|
expect(consoleOrgs.status).toBe(200)
|
|
expect(yield* json(consoleOrgs)).toEqual({ orgs: [] })
|
|
|
|
expect(toolList.status).toBe(200)
|
|
expect(yield* json<unknown[]>(toolList)).toContainEqual(
|
|
expect.objectContaining({
|
|
id: "bash",
|
|
description: expect.any(String),
|
|
parameters: expect.any(Object),
|
|
}),
|
|
)
|
|
|
|
expect(toolIDs.status).toBe(200)
|
|
expect(yield* json(toolIDs)).toContain("bash")
|
|
|
|
expect(worktrees.status).toBe(200)
|
|
expect(yield* json(worktrees)).toEqual([])
|
|
|
|
expect(resources.status).toBe(200)
|
|
expect(yield* json(resources)).toEqual({})
|
|
}),
|
|
{
|
|
config: {
|
|
formatter: false,
|
|
lsp: false,
|
|
mcp: {
|
|
demo: {
|
|
type: "local",
|
|
command: ["echo", "demo"],
|
|
enabled: false,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
)
|
|
|
|
it.instance("returns declared worktree errors", () =>
|
|
Effect.gen(function* () {
|
|
const tmp = yield* TestInstance
|
|
const response = yield* request(ExperimentalPaths.worktree, tmp.directory, {
|
|
method: "POST",
|
|
headers: { "content-type": "application/json" },
|
|
body: JSON.stringify({}),
|
|
})
|
|
|
|
expect(response.status).toBe(400)
|
|
expect(yield* json(response)).toEqual({
|
|
name: "WorktreeNotGitError",
|
|
data: { message: "Worktrees are only supported for git projects" },
|
|
})
|
|
}),
|
|
)
|
|
|
|
it.instance(
|
|
"serves Console org switch through the default server app",
|
|
() =>
|
|
Effect.gen(function* () {
|
|
const tmp = yield* TestInstance
|
|
const accountID = yield* insertAccount()
|
|
const switched = yield* request(ExperimentalPaths.consoleSwitch, tmp.directory, {
|
|
method: "POST",
|
|
headers: { "content-type": "application/json" },
|
|
body: JSON.stringify({ accountID, orgID: "org-test" }),
|
|
})
|
|
|
|
expect(switched.status).toBe(200)
|
|
expect(yield* json(switched)).toBe(true)
|
|
}),
|
|
{ config: { formatter: false, lsp: false } },
|
|
)
|
|
|
|
it.instance(
|
|
"serves global session list through the default server app",
|
|
() =>
|
|
Effect.gen(function* () {
|
|
const tmp = yield* TestInstance
|
|
const first = yield* createSession({ title: "page-one" })
|
|
const second = yield* createSession({ title: "page-two" })
|
|
yield* setSessionUpdated(first, 1)
|
|
yield* setSessionUpdated(second, 2)
|
|
|
|
const page = yield* request(
|
|
`${ExperimentalPaths.session}?${new URLSearchParams({ directory: tmp.directory, limit: "1" })}`,
|
|
tmp.directory,
|
|
)
|
|
expect(page.status).toBe(200)
|
|
expect(page.headers["x-next-cursor"]).toBeTruthy()
|
|
|
|
const body = yield* json<Session.GlobalInfo[]>(page)
|
|
expect(body.map((session) => session.id)).toEqual([second.id])
|
|
expect(body[0].project?.id).toBe(second.projectID)
|
|
|
|
const next = yield* request(
|
|
`${ExperimentalPaths.session}?${new URLSearchParams({
|
|
directory: tmp.directory,
|
|
limit: "10",
|
|
cursor: body[0].time.updated.toString(),
|
|
})}`,
|
|
tmp.directory,
|
|
)
|
|
expect(next.status).toBe(200)
|
|
expect((yield* json<Session.GlobalInfo[]>(next)).map((session) => session.id)).toContain(first.id)
|
|
}),
|
|
{ git: true, config: { formatter: false, lsp: false } },
|
|
)
|
|
|
|
testWorktreeMutations(
|
|
"serves worktree mutations through the default server app",
|
|
() =>
|
|
Effect.gen(function* () {
|
|
const tmp = yield* TestInstance
|
|
yield* withCreatedWorktree(tmp.directory, (info) =>
|
|
Effect.gen(function* () {
|
|
const listed = yield* request(ExperimentalPaths.worktree, tmp.directory)
|
|
expect(listed.status).toBe(200)
|
|
expect(yield* json(listed)).toContain(info.directory)
|
|
|
|
const reset = yield* request(ExperimentalPaths.worktreeReset, tmp.directory, {
|
|
method: "POST",
|
|
headers: { "content-type": "application/json" },
|
|
body: JSON.stringify({ directory: info.directory }),
|
|
})
|
|
|
|
expect(reset.status).toBe(200)
|
|
expect(yield* json(reset)).toBe(true)
|
|
}),
|
|
)
|
|
|
|
const afterRemove = yield* request(ExperimentalPaths.worktree, tmp.directory)
|
|
expect(afterRemove.status).toBe(200)
|
|
expect(yield* json(afterRemove)).toEqual([])
|
|
}),
|
|
{ git: true, config: { formatter: false, lsp: false } },
|
|
)
|
|
})
|