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 { 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 * as Schedule from "effect/Schedule"; |
| 10 | import Stack from "../alchemy.run.ts"; |
| 11 | import { ShortyApi } from "../src/ShortyApi.ts"; |
| 12 | |
| 13 | const { test, beforeAll, afterAll, deploy, destroy } = Test.make({ |
| 14 | providers: Cloudflare.providers(), |
| 15 | state: Alchemy.localState(), |
| 16 | }); |
| 17 | |
| 18 | // Deploy a full copy of the stack to stage test_$USER, and tear it down after. |
| 19 | const stack = beforeAll(deploy(Stack)); |
| 20 | afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack)); |
| 21 | |
| 22 | // The same typed client the dashboard uses, pointed at the test deployment. |
| 23 | const client = Effect.gen(function* () { |
| 24 | const { api } = yield* stack; |
| 25 | yield* Test.getWhenReady(`${api}/links`); // fresh workers.dev URLs take a moment |
| 26 | return yield* HttpApiClient.make(ShortyApi, { baseUrl: api }); |
| 27 | }); |
| 28 | |
| 29 | /** |
| 30 | * Open a short link the way a browser would, without following the redirect. |
| 31 | * Retries cold-start 404/5xx responses while a fresh deploy rolls out. |
| 32 | */ |
| 33 | const click = (url: string) => |
| 34 | Test.executeWhenReady(HttpClientRequest.get(url)).pipe( |
| 35 | Effect.provideService(FetchHttpClient.RequestInit, { redirect: "manual" }), |
| 36 | ); |
| 37 | |
| 38 | test( |
| 39 | "creates and reads back a link", |
| 40 | Effect.gen(function* () { |
| 41 | const shorty = yield* client; |
| 42 | const link = yield* shorty.links.create({ payload: { url: "https://effect.website" } }); |
| 43 | |
| 44 | const found = yield* shorty.links.get({ params: { code: link.code } }); |
| 45 | expect(found.url).toBe("https://effect.website"); |
| 46 | |
| 47 | const all = yield* shorty.links.list(); |
| 48 | expect(all.map((l) => l.code)).toContain(link.code); |
| 49 | }), |
| 50 | ); |
| 51 | |
| 52 | test( |
| 53 | "a missing link is a typed LinkNotFound", |
| 54 | Effect.gen(function* () { |
| 55 | const shorty = yield* client; |
| 56 | const error = yield* shorty.links.get({ params: { code: "nope" } }).pipe(Effect.flip); |
| 57 | expect(error._tag).toBe("LinkNotFound"); |
| 58 | }), |
| 59 | ); |
| 60 | |
| 61 | test( |
| 62 | "a short link redirects to its URL", |
| 63 | Effect.gen(function* () { |
| 64 | const { api } = yield* stack; |
| 65 | const shorty = yield* client; |
| 66 | const link = yield* shorty.links.create({ payload: { url: "https://alchemy.run/" } }); |
| 67 | |
| 68 | const response = yield* click(`${api}/${link.code}`); |
| 69 | expect(response.status).toBe(302); |
| 70 | expect(response.headers.location).toBe("https://alchemy.run/"); |
| 71 | }), |
| 72 | ); |
| 73 | |
| 74 | test( |
| 75 | "clicks are pushed live to open WebSockets", |
| 76 | Effect.scoped( |
| 77 | Effect.gen(function* () { |
| 78 | const { api } = yield* stack; |
| 79 | const shorty = yield* client; |
| 80 | const link = yield* shorty.links.create({ payload: { url: "https://effect.website" } }); |
| 81 | |
| 82 | // Watch the link's room, like the dashboard does. |
| 83 | const counts: number[] = []; |
| 84 | yield* Effect.acquireRelease( |
| 85 | Effect.sync(() => { |
| 86 | const socket = new WebSocket(`${api.replace(/^http/, "ws")}/links/${link.code}/live`); |
| 87 | socket.onmessage = (event) => counts.push(JSON.parse(String(event.data)).clicks); |
| 88 | return socket; |
| 89 | }), |
| 90 | (socket) => Effect.sync(() => socket.close()), |
| 91 | ); |
| 92 | const latest = Effect.sync(() => counts.at(-1)); |
| 93 | const poll = { schedule: Schedule.spaced("500 millis"), times: 120 }; |
| 94 | |
| 95 | expect(yield* latest.pipe(Effect.repeat({ ...poll, until: (n) => n !== undefined }))).toBe(0); |
| 96 | |
| 97 | expect((yield* click(`${api}/${link.code}`)).status).toBe(302); |
| 98 | expect((yield* click(`${api}/${link.code}`)).status).toBe(302); |
| 99 | |
| 100 | expect(yield* latest.pipe(Effect.repeat({ ...poll, until: (n) => n === 2 }))).toBe(2); |
| 101 | }), |
| 102 | ), |
| 103 | ); |