@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 83 lines 3.09 KB
Raw
1 import { expect } from "@alchemy.run/test";
2 import * as Data from "effect/Data";
3 import * as Effect from "effect/Effect";
4 import * as HttpBody from "effect/http/HttpBody";
5 import * as HttpClient from "effect/http/HttpClient";
6 import * as HttpClientRequest from "effect/http/HttpClientRequest";
7 import * as Schedule from "effect/Schedule";
8
9 class UnexpectedStatus extends Data.TaggedError("UnexpectedStatus")<{
10 message: string;
11 }> {}
12
13 // Bounded: rides out cold starts and (live) API-token propagation, which can
14 // take several seconds before R2 accepts the token-derived S3 credentials.
15 const retry = Schedule.max([Schedule.spaced("2 seconds"), Schedule.recurs(20)]);
16
17 const expectStatus = (request: HttpClientRequest.HttpClientRequest, status: number) =>
18 Effect.gen(function* () {
19 const client = yield* HttpClient.HttpClient;
20 const res = yield* client.execute(request);
21 const body = yield* res.text;
22 if (res.status !== status) {
23 return yield* new UnexpectedStatus({
24 message: `${request.method} ${request.url} → ${res.status}: ${body.slice(0, 500)}`,
25 });
26 }
27 return body;
28 });
29
30 const until = <A, R>(effect: Effect.Effect<A, unknown, R>) =>
31 effect.pipe(Effect.retry({ schedule: retry }), Effect.orDie);
32
33 const json = <T>(url: string) =>
34 until(
35 expectStatus(HttpClientRequest.get(url), 200).pipe(Effect.map((body) => JSON.parse(body) as T)),
36 );
37
38 /**
39 * Drive a deployed presign fixture (`routes.ts`) end to end: upload through
40 * a presigned PUT URL as an unauthenticated client, read it back through
41 * the Worker's native binding and through a presigned GET URL, and check
42 * that a pinned Content-Type is enforced.
43 */
44 export const presignRoundTrip = (workerUrl: string, key: string) =>
45 Effect.gen(function* () {
46 const { url: putUrl } = yield* json<{ url: string }>(
47 `${workerUrl}/presign-put?key=${encodeURIComponent(key)}&contentType=text/plain`,
48 );
49
50 yield* until(
51 expectStatus(
52 HttpClientRequest.put(putUrl).pipe(
53 HttpClientRequest.setBody(HttpBody.text("uploaded via presigned url", "text/plain")),
54 ),
55 200,
56 ),
57 );
58
59 const read = yield* json<{ value: string | null; contentType: string }>(
60 `${workerUrl}/read?key=${encodeURIComponent(key)}`,
61 );
62 expect(read.value).toBe("uploaded via presigned url");
63 expect(read.contentType).toBe("text/plain");
64
65 // The signed Content-Type is enforced
66 const client = yield* HttpClient.HttpClient;
67 const wrongType = yield* client.execute(
68 HttpClientRequest.put(putUrl).pipe(
69 HttpClientRequest.setBody(HttpBody.text("{}", "application/json")),
70 ),
71 );
72 expect(wrongType.status).toBe(403);
73
74 const { url: getUrl } = yield* json<{ url: string }>(
75 `${workerUrl}/presign-get?key=${encodeURIComponent(key)}&contentType=application/octet-stream`,
76 );
77 const downloaded = yield* client.get(getUrl);
78 expect(downloaded.status).toBe(200);
79 expect(downloaded.headers["content-type"]).toBe("application/octet-stream");
80 expect(yield* downloaded.text).toBe("uploaded via presigned url");
81
82 return { putUrl, getUrl };
83 });