|
1 | 1 | import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; |
2 | 2 | import { describe, expect, it } from "@effect/vitest"; |
3 | 3 | import * as Effect from "effect/Effect"; |
| 4 | +import * as Fiber from "effect/Fiber"; |
4 | 5 | import * as Schema from "effect/Schema"; |
5 | 6 | import * as TestClock from "effect/testing/TestClock"; |
6 | | -import { HttpClient, HttpClientResponse } from "effect/http"; |
| 7 | +import { HttpClient, HttpClientError, HttpClientResponse } from "effect/http"; |
7 | 8 |
|
8 | 9 | import { makeCliproxyApi } from "./cliproxyApi.ts"; |
9 | 10 |
|
@@ -114,6 +115,99 @@ function fixture( |
114 | 115 | }; |
115 | 116 | } |
116 | 117 |
|
| 118 | +/** A hub API whose every request fails the way `fail` says. */ |
| 119 | +const unreachableHub = ( |
| 120 | + fail: ( |
| 121 | + request: Parameters<Parameters<typeof HttpClient.make>[0]>[0], |
| 122 | + ) => HttpClientError.HttpClientError, |
| 123 | +) => |
| 124 | + makeCliproxyApi.pipe( |
| 125 | + Effect.provideService( |
| 126 | + HttpClient.HttpClient, |
| 127 | + HttpClient.make((request) => Effect.fail(fail(request))), |
| 128 | + ), |
| 129 | + Effect.provide(NodeCrypto.layer), |
| 130 | + ); |
| 131 | + |
| 132 | +describe("CLIProxyAPI hub failures", () => { |
| 133 | + it.effect("names the TLS code when the hub's certificate is not trusted", () => |
| 134 | + Effect.gen(function* () { |
| 135 | + const api = yield* unreachableHub( |
| 136 | + (request) => |
| 137 | + new HttpClientError.HttpClientError({ |
| 138 | + reason: new HttpClientError.TransportError({ |
| 139 | + request, |
| 140 | + // Node's fetch wraps the TLS error, which carries the code, in a generic TypeError. |
| 141 | + cause: new TypeError("fetch failed", { |
| 142 | + cause: Object.assign(new Error("unable to get local issuer certificate"), { |
| 143 | + code: "UNABLE_TO_GET_ISSUER_CERT_LOCALLY", |
| 144 | + }), |
| 145 | + }), |
| 146 | + }), |
| 147 | + }), |
| 148 | + ); |
| 149 | + const error = yield* Effect.flip(api.readAccounts(config)); |
| 150 | + expect(error.detail).toContain("could not list accounts"); |
| 151 | + expect(error.detail).toContain("UNABLE_TO_GET_ISSUER_CERT_LOCALLY"); |
| 152 | + expect(error.detail).toContain("NODE_EXTRA_CA_CERTS"); |
| 153 | + }), |
| 154 | + ); |
| 155 | + |
| 156 | + it.effect("names the status, not the body, when the hub rejects the request", () => |
| 157 | + Effect.gen(function* () { |
| 158 | + const test = HttpClient.make((request) => |
| 159 | + Effect.succeed( |
| 160 | + HttpClientResponse.fromWeb( |
| 161 | + request, |
| 162 | + Response.json({ error: "management-secret is wrong" }, { status: 401 }), |
| 163 | + ), |
| 164 | + ), |
| 165 | + ); |
| 166 | + const api = yield* makeCliproxyApi.pipe( |
| 167 | + Effect.provideService(HttpClient.HttpClient, test), |
| 168 | + Effect.provide(NodeCrypto.layer), |
| 169 | + ); |
| 170 | + const error = yield* Effect.flip(api.readAccounts(config)); |
| 171 | + expect(error.detail).toContain("401"); |
| 172 | + expect(error.detail).not.toContain("management-secret"); |
| 173 | + }), |
| 174 | + ); |
| 175 | + |
| 176 | + it.effect("says when the hub does not answer in time", () => |
| 177 | + Effect.gen(function* () { |
| 178 | + const api = yield* makeCliproxyApi.pipe( |
| 179 | + Effect.provideService( |
| 180 | + HttpClient.HttpClient, |
| 181 | + HttpClient.make(() => Effect.never), |
| 182 | + ), |
| 183 | + Effect.provide(NodeCrypto.layer), |
| 184 | + ); |
| 185 | + const pending = yield* Effect.flip(api.readAccounts(config)).pipe( |
| 186 | + Effect.forkChild({ startImmediately: true }), |
| 187 | + ); |
| 188 | + yield* TestClock.adjust("15 seconds"); |
| 189 | + const error = yield* Fiber.join(pending); |
| 190 | + expect(error.detail).toContain("did not answer within 15 seconds"); |
| 191 | + }), |
| 192 | + ); |
| 193 | + |
| 194 | + it.effect("keeps a malformed account list generic", () => |
| 195 | + Effect.gen(function* () { |
| 196 | + const test = HttpClient.make((request) => |
| 197 | + Effect.succeed( |
| 198 | + HttpClientResponse.fromWeb(request, Response.json({ nope: "hub-internal" })), |
| 199 | + ), |
| 200 | + ); |
| 201 | + const api = yield* makeCliproxyApi.pipe( |
| 202 | + Effect.provideService(HttpClient.HttpClient, test), |
| 203 | + Effect.provide(NodeCrypto.layer), |
| 204 | + ); |
| 205 | + const error = yield* Effect.flip(api.readAccounts(config)); |
| 206 | + expect(error.detail).toBe("The hub could not list accounts."); |
| 207 | + }), |
| 208 | + ); |
| 209 | +}); |
| 210 | + |
117 | 211 | describe("CLIProxyAPI built-in management API", () => { |
118 | 212 | it.effect( |
119 | 213 | "reads both accounts and their earliest unexpired credits without plugin endpoints", |
|
0 commit comments