The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 1 | import * as Fly from "@alchemy.run/fly"; |
| 2 | import { describe, expect } from "@alchemy.run/test"; |
| 3 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 4 | import * as Alchemy from "alchemy"; |
| 5 | import * as Data from "effect/Data"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as HttpClient from "effect/http/HttpClient"; |
| 8 | import type { HttpClientError } from "effect/http/HttpClientError"; |
| 9 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 10 | import type * as HttpClientResponse from "effect/http/HttpClientResponse"; |
| 11 | import { MinimumLogLevel } from "effect/References"; |
| 12 | import * as Schedule from "effect/Schedule"; |
| 13 | import BindingsApi from "./fixtures/bindings-api.ts"; |
| 14 | import { |
| 15 | BoxKey, |
| 16 | Marker, |
| 17 | PLAINTEXT, |
| 18 | PublicIp, |
| 19 | SECRET_NAME, |
| 20 | SignKey, |
| 21 | Site, |
| 22 | } from "./fixtures/bindings-shared.ts"; |
| 23 | |
| 24 | const { test, beforeAll, afterAll, deploy, destroy } = Test.make({ |
| 25 | providers: Fly.providers(), |
| 26 | }); |
| 27 | |
| 28 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 29 | |
| 30 | class NotReady extends Data.TaggedError("NotReady")<{ |
| 31 | status: number; |
| 32 | body?: unknown; |
| 33 | }> { |
| 34 | override get message() { |
| 35 | return this.body === undefined |
| 36 | ? `status ${this.status}` |
| 37 | : `status ${this.status}: ${JSON.stringify(this.body)}`; |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | const Stack = Alchemy.Stack( |
| 42 | "FlyBindingsFixture", |
| 43 | { |
| 44 | providers: Fly.providers(), |
| 45 | state: Alchemy.localState(), |
| 46 | }, |
| 47 | Effect.gen(function* () { |
| 48 | const site = yield* Site; |
| 49 | const secret = yield* Marker; |
| 50 | const box = yield* BoxKey; |
| 51 | const signing = yield* SignKey; |
| 52 | const ip = yield* PublicIp; |
| 53 | const api = yield* BindingsApi; |
| 54 | return { |
| 55 | appName: site.appName, |
| 56 | secretName: secret.name, |
| 57 | boxName: box.name, |
| 58 | signName: signing.name, |
| 59 | ip: ip.ip, |
| 60 | url: api.url ?? `https://${site.appName}.fly.dev`, |
| 61 | }; |
| 62 | }), |
| 63 | ); |
| 64 | |
| 65 | const stack = beforeAll(deploy(Stack), { timeout: 180_000 }); |
| 66 | |
| 67 | afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack), { |
| 68 | timeout: 120_000, |
| 69 | }); |
| 70 | |
| 71 | const retryTransient = { |
| 72 | while: (e: NotReady | HttpClientError) => |
| 73 | e._tag === "NotReady" && |
| 74 | (e.status === 0 || e.status === 404 || e.status === 502 || e.status === 503), |
| 75 | schedule: Schedule.spaced("2 seconds"), |
| 76 | times: 8, |
| 77 | } as const; |
| 78 | |
| 79 | /** Decode the body, turning any non-200 (or undecodable body) into `NotReady`. */ |
| 80 | const readJson = (res: HttpClientResponse.HttpClientResponse): Effect.Effect<unknown, NotReady> => |
| 81 | res.json.pipe( |
| 82 | Effect.catch(() => Effect.fail(new NotReady({ status: res.status }))), |
| 83 | Effect.flatMap((body) => |
| 84 | res.status === 200 |
| 85 | ? Effect.succeed(body) |
| 86 | : Effect.fail(new NotReady({ status: res.status, body })), |
| 87 | ), |
| 88 | ); |
| 89 | |
| 90 | const getJson = (path: string) => |
| 91 | Effect.gen(function* () { |
| 92 | const { url } = yield* stack; |
| 93 | const client = yield* HttpClient.HttpClient; |
| 94 | return yield* client.get(`${url}${path}`).pipe( |
| 95 | Effect.timeoutOrElse({ |
| 96 | duration: "8 seconds", |
| 97 | orElse: () => Effect.fail(new NotReady({ status: 0 })), |
| 98 | }), |
| 99 | Effect.flatMap(readJson), |
| 100 | Effect.retry(retryTransient), |
| 101 | Effect.timeout("45 seconds"), |
| 102 | ); |
| 103 | }); |
| 104 | |
| 105 | const postJson = (path: string, body: unknown) => |
| 106 | Effect.gen(function* () { |
| 107 | const { url } = yield* stack; |
| 108 | return yield* HttpClient.execute( |
| 109 | HttpClientRequest.post(`${url}${path}`).pipe(HttpClientRequest.bodyJsonUnsafe(body)), |
| 110 | ).pipe( |
| 111 | Effect.timeoutOrElse({ |
| 112 | duration: "8 seconds", |
| 113 | orElse: () => Effect.fail(new NotReady({ status: 0 })), |
| 114 | }), |
| 115 | Effect.flatMap(readJson), |
| 116 | Effect.retry(retryTransient), |
| 117 | Effect.timeout("45 seconds"), |
| 118 | ); |
| 119 | }); |
| 120 | |
| 121 | describe( |
| 122 | "Fly Bindings", |
| 123 | { |
| 124 | tags: [ |
| 125 | "provider:fly", |
| 126 | "provider:fly:app", |
| 127 | "provider:fly:ipassignment", |
| 128 | "provider:fly:secret", |
| 129 | "provider:fly:secretkey", |
| 130 | "provider:fly:service", |
| 131 | "provider:fly:sprite", |
| 132 | "live", |
| 133 | ], |
| 134 | }, |
| 135 | () => { |
| 136 | test( |
| 137 | "fixture is reachable over fly.dev", |
| 138 | Effect.gen(function* () { |
| 139 | const out = yield* stack; |
| 140 | expect(out.url).toContain(".fly.dev"); |
| 141 | expect(out.ip).toEqual(expect.any(String)); |
| 142 | const body = (yield* getJson("/health")) as { |
| 143 | ok: boolean; |
| 144 | appName?: string; |
| 145 | secretName?: string; |
| 146 | hasFlySecretMarkerEnv?: boolean; |
| 147 | hasToken?: boolean; |
| 148 | }; |
| 149 | expect(body.ok).toEqual(true); |
| 150 | expect(body.appName).toEqual(out.appName); |
| 151 | expect(body.secretName).toEqual(out.secretName); |
| 152 | expect(body.hasToken).toEqual(true); |
| 153 | expect(body.hasFlySecretMarkerEnv).toEqual(false); |
| 154 | }).pipe(logLevel), |
| 155 | { timeout: 120_000 }, |
| 156 | ); |
| 157 | |
| 158 | describe("Exec", () => { |
| 159 | test( |
| 160 | "executes on a Sprite from the Service", |
| 161 | Effect.gen(function* () { |
| 162 | const body = (yield* getJson("/sprite")) as { |
| 163 | stdout: string; |
| 164 | exitCode: number; |
| 165 | }; |
| 166 | expect(body.stdout.trim()).toEqual("sprite-runtime-binding"); |
| 167 | expect(body.exitCode).toEqual(0); |
| 168 | }).pipe(logLevel), |
| 169 | { timeout: 60_000 }, |
| 170 | ); |
| 171 | }); |
| 172 | |
| 173 | describe("GetSecret", () => { |
| 174 | test( |
| 175 | "reads the App secret from the Service", |
| 176 | Effect.gen(function* () { |
| 177 | const out = yield* stack; |
| 178 | const body = (yield* getJson("/secret")) as { |
| 179 | ok: boolean; |
| 180 | name: string; |
| 181 | hasValue: boolean; |
| 182 | }; |
| 183 | expect(body.ok).toEqual(true); |
| 184 | expect(body.name).toEqual(out.secretName); |
| 185 | expect(body.name).toEqual(SECRET_NAME); |
| 186 | expect(body.hasValue).toEqual(true); |
| 187 | }).pipe(logLevel), |
| 188 | { timeout: 60_000 }, |
| 189 | ); |
| 190 | }); |
| 191 | |
| 192 | describe("ListSecrets", () => { |
| 193 | test( |
| 194 | "lists secrets on the App from the Service", |
| 195 | Effect.gen(function* () { |
| 196 | const out = yield* stack; |
| 197 | const body = (yield* getJson("/secrets")) as { names: string[] }; |
| 198 | expect(body.names).toContain(out.secretName); |
| 199 | }).pipe(logLevel), |
| 200 | { timeout: 60_000 }, |
| 201 | ); |
| 202 | }); |
| 203 | |
| 204 | describe("WriteSecret", () => { |
| 205 | test( |
| 206 | "creates a secret from the Service", |
| 207 | Effect.gen(function* () { |
| 208 | const created = (yield* postJson("/secret", { |
| 209 | name: "BINDING_CREATED", |
| 210 | value: "from-fixture", |
| 211 | })) as { ok: boolean; name: string }; |
| 212 | expect(created.ok).toEqual(true); |
| 213 | const listed = (yield* getJson("/secrets")) as { names: string[] }; |
| 214 | expect(listed.names).toContain("BINDING_CREATED"); |
| 215 | }).pipe(logLevel), |
| 216 | { timeout: 60_000 }, |
| 217 | ); |
| 218 | }); |
| 219 | |
| 220 | describe("Encrypt", () => { |
| 221 | test( |
| 222 | "encrypts and decrypts on the Service", |
| 223 | Effect.gen(function* () { |
| 224 | const enc = (yield* postJson("/encrypt", { text: PLAINTEXT })) as { |
| 225 | ciphertext: string; |
| 226 | }; |
| 227 | expect(enc.ciphertext.length).toBeGreaterThan(0); |
| 228 | const dec = (yield* postJson("/decrypt", { |
| 229 | ciphertext: enc.ciphertext, |
| 230 | })) as { text: string }; |
| 231 | expect(dec.text).toEqual(PLAINTEXT); |
| 232 | }).pipe(logLevel), |
| 233 | { timeout: 60_000 }, |
| 234 | ); |
| 235 | }); |
| 236 | |
| 237 | describe("Sign", () => { |
| 238 | test( |
| 239 | "signs and verifies on the Service", |
| 240 | Effect.gen(function* () { |
| 241 | const signed = (yield* postJson("/sign", { text: PLAINTEXT })) as { |
| 242 | signature: string; |
| 243 | }; |
| 244 | expect(signed.signature.length).toBeGreaterThan(0); |
| 245 | const checked = (yield* postJson("/verify", { |
| 246 | text: PLAINTEXT, |
| 247 | signature: signed.signature, |
| 248 | })) as { valid: boolean }; |
| 249 | expect(checked.valid).toEqual(true); |
| 250 | }).pipe(logLevel), |
| 251 | { timeout: 60_000 }, |
| 252 | ); |
| 253 | }); |
| 254 | }, |
| 255 | ); |