@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, 06:00 UTC

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