1import * as BunClusterHttp from "@effect/platform-bun/BunClusterHttp"
2import { assert, describe, it } from "@effect/vitest"
3import * as ShardingConfig from "effect/cluster/ShardingConfig"
4import * as ConfigProvider from "effect/ConfigProvider"
5import * as Effect from "effect/Effect"
6import * as FetchHttpClient from "effect/http/FetchHttpClient"
7import * as HttpServer from "effect/http/HttpServer"
8import * as HttpServerResponse from "effect/http/HttpServerResponse"
9import * as NetAddress from "effect/net/NetAddress"
10
11describe("BunClusterHttp", () => {
12 for (
13 const [name, values] of [
14 ["listen address overrides the runner address", {
15 host: "::1",
16 port: "0",
17 listenHost: "127.0.0.1",
18 listenPort: "0"
19 }],
20 ["runner address is used when the listen address is omitted", { host: "127.0.0.1", port: "0" }]
21 ] as const
22 ) {
23 it.effect(name, () =>
24 Effect.gen(function*() {
25 const config = yield* ShardingConfig.config.parse(ConfigProvider.fromUnknown(values))
26 yield* Effect.gen(function*() {
27 const server = yield* HttpServer.HttpServer
28 if (server.address._tag !== "InetAddressV4") {
29 return assert.fail(`expected InetAddressV4 at 127.0.0.1, got ${server.address}`)
30 }
31 assert.strictEqual(NetAddress.formatIp(server.address.address), "127.0.0.1")
32 assert.isAbove(server.address.port, 0)
33
34 yield* server.serve(Effect.succeed(HttpServerResponse.text("cluster loopback")))
35 const client = yield* HttpServer.makeTestClient.pipe(Effect.provide(FetchHttpClient.layer))
36 const response = yield* client.get("/")
37 assert.strictEqual(response.status, 200)
38 assert.strictEqual(yield* response.text, "cluster loopback")
39 }).pipe(
40 Effect.provide(BunClusterHttp.layerHttpServer),
41 Effect.provide(ShardingConfig.layer(config))
42 )
43 }))
44 }
45})