@devries-cameron20 / t3code Mirror · read-only
Fork 0

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, 07:15 UTC

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