|
| 1 | +import * as HttpApp from "@effect/platform/HttpApp" |
| 2 | +import * as HttpRouter from "@effect/platform/HttpRouter" |
| 3 | +import { describe, expect, it } from "@effect/vitest" |
| 4 | +import { Effect } from "effect" |
| 5 | +import fc from "fast-check" |
| 6 | + |
| 7 | +import { |
| 8 | + federationExchangeStatusResponse, |
| 9 | + resolveConfiguredFederationPublicOrigin |
| 10 | +} from "../src/http.js" |
| 11 | +import { clearFederationState } from "../src/services/federation.js" |
| 12 | + |
| 13 | +const envValueArbitrary = fc.option( |
| 14 | + fc.oneof( |
| 15 | + fc.string(), |
| 16 | + fc.constant(" "), |
| 17 | + fc.constant("\t\n") |
| 18 | + ), |
| 19 | + { nil: undefined } |
| 20 | +) |
| 21 | + |
| 22 | +const expectedConfiguredFederationPublicOrigin = ( |
| 23 | + federationPublicOrigin: string | undefined, |
| 24 | + apiPublicUrl: string | undefined |
| 25 | +): string | undefined => { |
| 26 | + const federation = federationPublicOrigin?.trim() |
| 27 | + if (federation !== undefined && federation.length > 0) { |
| 28 | + return federation |
| 29 | + } |
| 30 | + |
| 31 | + const api = apiPublicUrl?.trim() |
| 32 | + return api !== undefined && api.length > 0 ? api : undefined |
| 33 | +} |
| 34 | + |
| 35 | +const federationStatusHandler = HttpApp.toWebHandler( |
| 36 | + Effect.flatten( |
| 37 | + HttpRouter.toHttpApp( |
| 38 | + HttpRouter.empty.pipe( |
| 39 | + HttpRouter.get("/federation/status", federationExchangeStatusResponse()), |
| 40 | + HttpRouter.get("/federation/exchange/status", federationExchangeStatusResponse()) |
| 41 | + ) |
| 42 | + ) |
| 43 | + ) |
| 44 | +) |
| 45 | + |
| 46 | +const readFederationStatusRoute = (path: string) => |
| 47 | + Effect.gen(function*(_) { |
| 48 | + const response = yield* _( |
| 49 | + Effect.tryPromise({ |
| 50 | + try: () => |
| 51 | + federationStatusHandler( |
| 52 | + new Request(`http://127.0.0.1${path}`, { |
| 53 | + headers: { |
| 54 | + "x-forwarded-host": "public.example.test", |
| 55 | + "x-forwarded-proto": "https" |
| 56 | + } |
| 57 | + }) |
| 58 | + ), |
| 59 | + catch: (cause) => new Error(String(cause)) |
| 60 | + }) |
| 61 | + ) |
| 62 | + const body = yield* _( |
| 63 | + Effect.tryPromise({ |
| 64 | + try: () => response.text(), |
| 65 | + catch: (cause) => new Error(String(cause)) |
| 66 | + }) |
| 67 | + ) |
| 68 | + return { body, status: response.status } |
| 69 | + }) |
| 70 | + |
| 71 | +const parseJsonObject = (raw: string): object | null => { |
| 72 | + const parsed: unknown = JSON.parse(raw) |
| 73 | + return typeof parsed === "object" && parsed !== null && !Array.isArray(parsed) |
| 74 | + ? parsed |
| 75 | + : null |
| 76 | +} |
| 77 | + |
| 78 | +const readField = (value: object | null, key: string): unknown => |
| 79 | + value === null ? undefined : Reflect.get(value, key) |
| 80 | + |
| 81 | +describe("api http config", () => { |
| 82 | + it.effect("ignores empty federation public origin values", () => |
| 83 | + Effect.sync(() => { |
| 84 | + expect( |
| 85 | + resolveConfiguredFederationPublicOrigin({ |
| 86 | + DOCKER_GIT_API_PUBLIC_URL: " https://api.example.test ", |
| 87 | + DOCKER_GIT_FEDERATION_PUBLIC_ORIGIN: " " |
| 88 | + }) |
| 89 | + ).toBe("https://api.example.test") |
| 90 | + })) |
| 91 | + |
| 92 | + it.effect("prefers explicit federation public origin over api public url", () => |
| 93 | + Effect.sync(() => { |
| 94 | + expect( |
| 95 | + resolveConfiguredFederationPublicOrigin({ |
| 96 | + DOCKER_GIT_API_PUBLIC_URL: "https://api.example.test", |
| 97 | + DOCKER_GIT_FEDERATION_PUBLIC_ORIGIN: "https://federation.example.test" |
| 98 | + }) |
| 99 | + ).toBe("https://federation.example.test") |
| 100 | + })) |
| 101 | + |
| 102 | + it.effect("satisfies federation origin trim and priority invariant", () => |
| 103 | + Effect.sync(() => { |
| 104 | + fc.assert( |
| 105 | + fc.property( |
| 106 | + envValueArbitrary, |
| 107 | + envValueArbitrary, |
| 108 | + (federationPublicOrigin, apiPublicUrl) => { |
| 109 | + const result = resolveConfiguredFederationPublicOrigin({ |
| 110 | + DOCKER_GIT_API_PUBLIC_URL: apiPublicUrl, |
| 111 | + DOCKER_GIT_FEDERATION_PUBLIC_ORIGIN: federationPublicOrigin |
| 112 | + }) |
| 113 | + const expected = expectedConfiguredFederationPublicOrigin( |
| 114 | + federationPublicOrigin, |
| 115 | + apiPublicUrl |
| 116 | + ) |
| 117 | + |
| 118 | + expect(result).toBe(expected) |
| 119 | + expect(result).toBe(result?.trim()) |
| 120 | + } |
| 121 | + ) |
| 122 | + ) |
| 123 | + })) |
| 124 | + |
| 125 | + it.effect("serves equivalent federation status aliases at the HTTP layer", () => |
| 126 | + Effect.gen(function*(_) { |
| 127 | + yield* _(Effect.sync(() => clearFederationState())) |
| 128 | + |
| 129 | + const publicStatus = yield* _(readFederationStatusRoute("/federation/status")) |
| 130 | + const compatibilityStatus = yield* _(readFederationStatusRoute("/federation/exchange/status")) |
| 131 | + const payload = parseJsonObject(publicStatus.body) |
| 132 | + |
| 133 | + expect(publicStatus.status).toBe(200) |
| 134 | + expect(compatibilityStatus.status).toBe(200) |
| 135 | + expect(compatibilityStatus.body).toBe(publicStatus.body) |
| 136 | + expect(payload).not.toBeNull() |
| 137 | + expect(readField(payload, "publicActor")).toBe("https://public.example.test/federation/actor") |
| 138 | + expect(typeof readField(payload, "summary")).toBe("object") |
| 139 | + expect(Array.isArray(readField(payload, "subscriptions"))).toBe(true) |
| 140 | + expect(Array.isArray(readField(payload, "recentEvents"))).toBe(true) |
| 141 | + })) |
| 142 | +}) |
0 commit comments