Skip to content

Commit a443e57

Browse files
committed
fix(httpapi): model bodyless global upgrade payload
1 parent b46cec2 commit a443e57

2 files changed

Lines changed: 65 additions & 2 deletions

File tree

packages/opencode/src/server/routes/instance/httpapi/groups/global.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { BusEvent } from "@/bus/bus-event"
33
import { SyncEvent } from "@/sync"
44
import "@/server/event"
55
import { Schema } from "effect"
6-
import { HttpApi, HttpApiEndpoint, HttpApiError, HttpApiGroup, OpenApi } from "effect/unstable/httpapi"
6+
import { HttpApi, HttpApiEndpoint, HttpApiError, HttpApiGroup, HttpApiSchema, OpenApi } from "effect/unstable/httpapi"
77
import { described } from "./metadata"
88

99
const GlobalHealth = Schema.Struct({
@@ -92,7 +92,7 @@ export const GlobalApi = HttpApi.make("global").add(
9292
}),
9393
),
9494
HttpApiEndpoint.post("upgrade", GlobalPaths.upgrade, {
95-
payload: GlobalUpgradeInput,
95+
payload: [HttpApiSchema.NoContent, GlobalUpgradeInput],
9696
success: described(GlobalUpgradeResult, "Upgrade result"),
9797
error: HttpApiError.BadRequest,
9898
}).annotateMerge(
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { NodeHttpServer } from "@effect/platform-node"
2+
import { describe, expect } from "bun:test"
3+
import { Context, Effect, Layer, Option } from "effect"
4+
import { HttpBody, HttpClient, HttpClientRequest, HttpRouter } from "effect/unstable/http"
5+
import { HttpApiBuilder } from "effect/unstable/httpapi"
6+
import { Auth } from "../../src/auth"
7+
import { Config } from "../../src/config/config"
8+
import { Installation } from "../../src/installation"
9+
import { ServerAuth } from "../../src/server/auth"
10+
import { RootHttpApi } from "../../src/server/routes/instance/httpapi/api"
11+
import { GlobalPaths } from "../../src/server/routes/instance/httpapi/groups/global"
12+
import { controlHandlers } from "../../src/server/routes/instance/httpapi/handlers/control"
13+
import { globalHandlers } from "../../src/server/routes/instance/httpapi/handlers/global"
14+
import { authorizationLayer } from "../../src/server/routes/instance/httpapi/middleware/authorization"
15+
import { schemaErrorLayer } from "../../src/server/routes/instance/httpapi/middleware/schema-error"
16+
import { testEffect } from "../lib/effect"
17+
18+
const apiLayer = HttpRouter.serve(
19+
HttpApiBuilder.layer(RootHttpApi).pipe(
20+
Layer.provide([controlHandlers, globalHandlers]),
21+
Layer.provide([authorizationLayer, schemaErrorLayer]),
22+
),
23+
{ disableListenLog: true, disableLogger: true },
24+
).pipe(
25+
Layer.provideMerge(NodeHttpServer.layerTest),
26+
Layer.provide(Layer.mock(Auth.Service)({})),
27+
Layer.provide(Layer.mock(Config.Service)({})),
28+
Layer.provide(
29+
Layer.mock(Installation.Service)({
30+
method: () => Effect.succeed("npm"),
31+
latest: () => Effect.succeed("9.9.9"),
32+
upgrade: () => Effect.void,
33+
}),
34+
),
35+
Layer.provide(ServerAuth.Config.layer({ password: Option.none(), username: "opencode" })),
36+
// Raw HttpApi routes expose an opaque handler context at the web boundary.
37+
// oxlint-disable-next-line typescript-eslint/no-unsafe-type-assertion
38+
Layer.provide(Layer.succeedContext(Context.empty() as Context.Context<unknown>)),
39+
)
40+
const it = testEffect(apiLayer)
41+
42+
describe("global HttpApi", () => {
43+
it.live("upgrades to latest when the request body is omitted", () =>
44+
Effect.gen(function* () {
45+
const response = yield* HttpClient.post(GlobalPaths.upgrade)
46+
47+
expect(response.status).toBe(200)
48+
expect(yield* response.json).toEqual({ success: true, version: "9.9.9" })
49+
}),
50+
)
51+
52+
it.live("rejects malformed upgrade payloads", () =>
53+
Effect.gen(function* () {
54+
const response = yield* HttpClientRequest.post(GlobalPaths.upgrade).pipe(
55+
HttpClientRequest.setBody(HttpBody.text("{", "application/json")),
56+
HttpClient.execute,
57+
)
58+
59+
expect(response.status).toBe(400)
60+
expect(yield* response.json).toEqual({ success: false, error: "Invalid request body" })
61+
}),
62+
)
63+
})

0 commit comments

Comments
 (0)