|
| 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