process.test.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import { expect } from "bun:test"
  2. import { Effect } from "effect"
  3. import { HttpServer, HttpServerError, HttpServerResponse } from "effect/unstable/http"
  4. import { it } from "../../core/test/lib/effect"
  5. import { ServerProcess } from "../src/process"
  6. it.live("allows browser preflight requests without credentials", () =>
  7. Effect.gen(function* () {
  8. const server = yield* ServerProcess.start<never, never>(
  9. {
  10. hostname: "127.0.0.1",
  11. port: 0,
  12. password: "secret",
  13. app: { version: "test-version" },
  14. database: { path: ":memory:" },
  15. },
  16. undefined,
  17. (api) =>
  18. api.pipe(
  19. Effect.catchIf(
  20. (error) => error instanceof HttpServerError.HttpServerError && error.reason._tag === "RouteNotFound",
  21. () => Effect.succeed(HttpServerResponse.text("fallback")),
  22. ),
  23. ),
  24. )
  25. const response = yield* Effect.promise(() =>
  26. fetch(new URL("/api/health", HttpServer.formatAddress(server.address)), {
  27. method: "OPTIONS",
  28. headers: {
  29. origin: "http://localhost:3000",
  30. "access-control-request-method": "GET",
  31. "access-control-request-headers": "authorization",
  32. },
  33. }),
  34. )
  35. expect(response.status).toBe(204)
  36. expect(response.headers.get("access-control-allow-origin")).toBe("http://localhost:3000")
  37. expect(response.headers.get("access-control-allow-headers")).toBe("authorization")
  38. const health = yield* Effect.promise(() =>
  39. fetch(new URL("/api/health", HttpServer.formatAddress(server.address)), {
  40. headers: {
  41. authorization: `Basic ${btoa("opencode:secret")}`,
  42. origin: "http://localhost:3000",
  43. },
  44. }),
  45. )
  46. expect(health.status).toBe(200)
  47. expect(health.headers.get("access-control-allow-origin")).toBe("http://localhost:3000")
  48. expect(yield* Effect.promise(() => health.json())).toMatchObject({ version: "test-version" })
  49. const missing = yield* Effect.promise(() =>
  50. fetch(new URL("/missing", HttpServer.formatAddress(server.address)), {
  51. headers: { authorization: `Basic ${btoa("opencode:secret")}` },
  52. }),
  53. )
  54. expect(missing.status).toBe(200)
  55. expect(yield* Effect.promise(() => missing.text())).toBe("fallback")
  56. }),
  57. )