forked from anomalyco/opencode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpapi-instance.test.ts
More file actions
265 lines (241 loc) · 10.3 KB
/
Copy pathhttpapi-instance.test.ts
File metadata and controls
265 lines (241 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import { PermissionV1 } from "@opencode-ai/core/v1/permission"
import { NodeHttpServer, NodeServices } from "@effect/platform-node"
import { Flag } from "@opencode-ai/core/flag/flag"
import { describe, expect } from "bun:test"
import { Config, Context, Effect, FileSystem, Layer, Path } from "effect"
import { HttpClient, HttpClientRequest, HttpRouter, HttpServer } from "effect/unstable/http"
import * as Socket from "effect/unstable/socket/Socket"
import { WorkspaceV2 } from "@opencode-ai/core/workspace"
import { ControlPaths } from "../../src/server/routes/instance/httpapi/groups/control"
import { InstancePaths } from "../../src/server/routes/instance/httpapi/groups/instance"
import { SessionPaths } from "../../src/server/routes/instance/httpapi/groups/session"
import { ProjectV2 } from "@opencode-ai/core/project"
import { QuestionID } from "../../src/question/schema"
import { HttpApiApp } from "../../src/server/routes/instance/httpapi/server"
import { HEADER as FenceHeader } from "../../src/server/shared/fence"
import { resetDatabase } from "../fixture/db"
import { tmpdirScoped } from "../fixture/fixture"
import { testEffect } from "../lib/effect"
// Flip the experimental workspaces flag so EventV2.run actually writes to
// EventSequenceTable (the source of truth the fence middleware reads). Reset
// the database around the test so per-instance state does not leak between
// runs. resetDatabase() already calls disposeAllInstances(), so we don't
// repeat it.
const testStateLayer = Layer.effectDiscard(
Effect.gen(function* () {
const originalWorkspaces = Flag.OPENCODE_EXPERIMENTAL_WORKSPACES
Flag.OPENCODE_EXPERIMENTAL_WORKSPACES = true
yield* Effect.promise(() => resetDatabase())
yield* Effect.addFinalizer(() =>
Effect.promise(async () => {
Flag.OPENCODE_EXPERIMENTAL_WORKSPACES = originalWorkspaces
await resetDatabase()
}),
)
}),
)
// Mount the production HttpApi route tree on a real Node HTTP server bound to
// 127.0.0.1:0 and a fetch-based HttpClient that prepends the server URL. This
// keeps the test wired directly through the same route layer production uses.
const servedRoutes: Layer.Layer<never, Config.ConfigError, HttpServer.HttpServer> = HttpRouter.serve(
HttpApiApp.routes,
{ disableListenLog: true, disableLogger: true },
)
const httpApiServerLayer = servedRoutes.pipe(
Layer.provide(Socket.layerWebSocketConstructorGlobal),
Layer.provideMerge(NodeHttpServer.layerTest),
Layer.provideMerge(NodeServices.layer),
)
const it = testEffect(Layer.mergeAll(testStateLayer, httpApiServerLayer))
const handlerContext = Context.empty() as Context.Context<unknown>
const directoryHeader = (dir: string) => HttpClientRequest.setHeader("x-opencode-directory", dir)
describe("instance HttpApi", () => {
it.live("serves the OpenAPI document", () =>
Effect.gen(function* () {
const response = yield* HttpClient.get("/doc")
expect(response.status).toBe(200)
expect(response.headers["content-type"]).toContain("application/json")
expect(yield* response.json).toMatchObject({
openapi: expect.any(String),
info: expect.any(Object),
paths: expect.objectContaining({
"/global/health": expect.any(Object),
"/session": expect.any(Object),
}),
})
}),
)
it.live("emits a sync fence header for fixed-workspace mutations", () =>
Effect.gen(function* () {
const originalWorkspaceID = Flag.OPENCODE_WORKSPACE_ID
Flag.OPENCODE_WORKSPACE_ID = WorkspaceV2.ID.ascending()
yield* Effect.addFinalizer(() =>
Effect.sync(() => {
Flag.OPENCODE_WORKSPACE_ID = originalWorkspaceID
}),
)
const dir = yield* tmpdirScoped({ git: true })
const response = yield* HttpClientRequest.post(SessionPaths.create).pipe(
directoryHeader(dir),
HttpClientRequest.bodyJson({ title: "fenced" }),
Effect.flatMap(HttpClient.execute),
)
expect(response.status).toBe(200)
expect(JSON.parse(response.headers[FenceHeader] ?? "{}")).not.toEqual({})
}),
)
it.live("does not emit sync fence headers for fixed-workspace reads or no-op mutations", () =>
Effect.gen(function* () {
const originalWorkspaceID = Flag.OPENCODE_WORKSPACE_ID
Flag.OPENCODE_WORKSPACE_ID = WorkspaceV2.ID.ascending()
yield* Effect.addFinalizer(() =>
Effect.sync(() => {
Flag.OPENCODE_WORKSPACE_ID = originalWorkspaceID
}),
)
const dir = yield* tmpdirScoped({ git: true })
const read = yield* HttpClientRequest.get(InstancePaths.path).pipe(directoryHeader(dir), HttpClient.execute)
const log = yield* HttpClientRequest.post(ControlPaths.log).pipe(
directoryHeader(dir),
HttpClientRequest.bodyJson({ service: "fence-test", level: "info", message: "noop" }),
Effect.flatMap(HttpClient.execute),
)
expect(read.status).toBe(200)
expect(read.headers[FenceHeader]).toBeUndefined()
expect(log.status).toBe(200)
expect(log.headers[FenceHeader]).toBeUndefined()
}),
)
it.live("rejects malformed permission and question request ids", () =>
Effect.gen(function* () {
const dir = yield* tmpdirScoped({ git: true })
const request = (path: string, init?: RequestInit) =>
Effect.promise(() =>
HttpApiApp.webHandler().handler(
new Request(`http://localhost${path}`, {
...init,
headers: { "x-opencode-directory": dir, "content-type": "application/json", ...init?.headers },
}),
handlerContext,
),
)
const [permission, questionReply, questionReject] = yield* Effect.all(
[
request("/permission/invalid-permission-id/reply", {
method: "POST",
body: JSON.stringify({ reply: "once" }),
}),
request("/question/invalid-question-id/reply", {
method: "POST",
body: JSON.stringify({ answers: [["Yes"]] }),
}),
request("/question/invalid-question-id/reject", { method: "POST" }),
],
{ concurrency: "unbounded" },
)
expect(permission.status).toBe(400)
expect(questionReply.status).toBe(400)
expect(questionReject.status).toBe(400)
}),
)
it.live("returns typed not found bodies for missing permission and question requests", () =>
Effect.gen(function* () {
const dir = yield* tmpdirScoped({ git: true })
const request = (path: string, init?: RequestInit) =>
Effect.promise(() =>
HttpApiApp.webHandler().handler(
new Request(`http://localhost${path}`, {
...init,
headers: { "x-opencode-directory": dir, "content-type": "application/json", ...init?.headers },
}),
handlerContext,
),
)
const permissionID = PermissionV1.ID.ascending()
const questionReplyID = QuestionID.ascending()
const questionRejectID = QuestionID.ascending()
const [permission, questionReply, questionReject] = yield* Effect.all(
[
request(`/permission/${permissionID}/reply`, {
method: "POST",
body: JSON.stringify({ reply: "once" }),
}),
request(`/question/${questionReplyID}/reply`, {
method: "POST",
body: JSON.stringify({ answers: [["Yes"]] }),
}),
request(`/question/${questionRejectID}/reject`, { method: "POST" }),
],
{ concurrency: "unbounded" },
)
expect(permission.status).toBe(404)
expect(yield* Effect.promise(() => permission.json())).toEqual({
_tag: "PermissionNotFoundError",
requestID: permissionID,
message: `Permission request not found: ${permissionID}`,
})
expect(questionReply.status).toBe(404)
expect(yield* Effect.promise(() => questionReply.json())).toEqual({
_tag: "QuestionNotFoundError",
requestID: questionReplyID,
message: `Question request not found: ${questionReplyID}`,
})
expect(questionReject.status).toBe(404)
expect(yield* Effect.promise(() => questionReject.json())).toEqual({
_tag: "QuestionNotFoundError",
requestID: questionRejectID,
message: `Question request not found: ${questionRejectID}`,
})
}),
)
it.live("returns typed not found bodies for missing projects", () =>
Effect.gen(function* () {
const dir = yield* tmpdirScoped({ git: true })
const projectID = ProjectV2.ID.make("project_missing")
const response = yield* Effect.promise(() =>
HttpApiApp.webHandler().handler(
new Request(`http://localhost/project/${projectID}`, {
method: "PATCH",
headers: { "x-opencode-directory": dir, "content-type": "application/json" },
body: JSON.stringify({ name: "Missing" }),
}),
handlerContext,
),
)
expect(response.status).toBe(404)
expect(yield* Effect.promise(() => response.json())).toEqual({
_tag: "ProjectNotFoundError",
projectID,
message: `Project not found: ${projectID}`,
})
}),
)
it.live("serves path and VCS read endpoints", () =>
Effect.gen(function* () {
const dir = yield* tmpdirScoped({ git: true })
const fs = yield* FileSystem.FileSystem
const path = yield* Path.Path
yield* fs.writeFileString(path.join(dir, "changed.txt"), "hello")
const [paths, vcs, diff] = yield* Effect.all(
[
HttpClientRequest.get(InstancePaths.path).pipe(directoryHeader(dir), HttpClient.execute),
HttpClientRequest.get(InstancePaths.vcs).pipe(directoryHeader(dir), HttpClient.execute),
HttpClientRequest.get(InstancePaths.vcsDiff).pipe(
HttpClientRequest.setUrlParam("mode", "git"),
directoryHeader(dir),
HttpClient.execute,
),
],
{ concurrency: "unbounded" },
)
expect(paths.status).toBe(200)
expect(yield* paths.json).toMatchObject({ directory: dir, worktree: dir })
expect(vcs.status).toBe(200)
expect(yield* vcs.json).toMatchObject({ branch: expect.any(String) })
expect(diff.status).toBe(200)
expect(yield* diff.json).toContainEqual(
expect.objectContaining({ file: "changed.txt", additions: 1, status: "added" }),
)
}),
)
})