The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { expect } from "bun:test"; |
| 2 | import * as Cloudflare from "@alchemy.run/cloudflare"; |
| 3 | import * as Test from "@alchemy.run/test/Bun"; |
| 4 | import * as Alchemy from "alchemy"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import * as HttpApiClient from "effect/http-api/HttpApiClient"; |
| 7 | import * as FetchHttpClient from "effect/http/FetchHttpClient"; |
| 8 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 9 | import Stack from "../alchemy.run.ts"; |
| 10 | import { ShortyApi } from "../src/ShortyApi.ts"; |
| 11 | |
| 12 | const { test, beforeAll, afterAll, deploy, destroy } = Test.make({ |
| 13 | providers: Cloudflare.providers(), |
| 14 | state: Alchemy.localState(), |
| 15 | }); |
| 16 | |
| 17 | // Deploy a full copy of the stack to stage test_$USER, and tear it down after. |
| 18 | const stack = beforeAll(deploy(Stack)); |
| 19 | afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack)); |
| 20 | |
| 21 | // The same typed client the dashboard uses, pointed at the test deployment. |
| 22 | const client = Effect.gen(function* () { |
| 23 | const { api } = yield* stack; |
| 24 | yield* Test.getWhenReady(`${api}/links`); // fresh workers.dev URLs take a moment |
| 25 | return yield* HttpApiClient.make(ShortyApi, { baseUrl: api }); |
| 26 | }); |
| 27 | |
| 28 | test( |
| 29 | "creates and reads back a link", |
| 30 | Effect.gen(function* () { |
| 31 | const shorty = yield* client; |
| 32 | const link = yield* shorty.links.create({ payload: { url: "https://effect.website" } }); |
| 33 | |
| 34 | const found = yield* shorty.links.get({ params: { code: link.code } }); |
| 35 | expect(found.url).toBe("https://effect.website"); |
| 36 | |
| 37 | const all = yield* shorty.links.list(); |
| 38 | expect(all.map((l) => l.code)).toContain(link.code); |
| 39 | }), |
| 40 | ); |
| 41 | |
| 42 | test( |
| 43 | "a missing link is a typed LinkNotFound", |
| 44 | Effect.gen(function* () { |
| 45 | const shorty = yield* client; |
| 46 | const error = yield* shorty.links.get({ params: { code: "nope" } }).pipe(Effect.flip); |
| 47 | expect(error._tag).toBe("LinkNotFound"); |
| 48 | }), |
| 49 | ); |
| 50 | |
| 51 | test( |
| 52 | "a short link redirects to its URL", |
| 53 | Effect.gen(function* () { |
| 54 | const { api } = yield* stack; |
| 55 | const shorty = yield* client; |
| 56 | const link = yield* shorty.links.create({ payload: { url: "https://alchemy.run/" } }); |
| 57 | |
| 58 | // Retries cold-start 404/5xx responses while a fresh deploy rolls out. |
| 59 | const response = yield* Test.executeWhenReady( |
| 60 | HttpClientRequest.get(`${api}/${link.code}`), |
| 61 | ).pipe(Effect.provideService(FetchHttpClient.RequestInit, { redirect: "manual" })); |
| 62 | expect(response.status).toBe(302); |
| 63 | expect(response.headers.location).toBe("https://alchemy.run/"); |
| 64 | }), |
| 65 | ); |