@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 105 lines 4.19 KB
Raw
1 import { describe, expect } from "bun:test";
2 import * as AWS from "@alchemy.run/aws";
3 import * as Test from "@alchemy.run/test/Bun";
4 import { Credentials } from "@distilled.cloud/aws/Credentials";
5 import { sign } from "@distilled.cloud/aws/SigV4";
6 import * as Alchemy from "alchemy";
7 import * as Effect from "effect/Effect";
8 import * as HttpClient from "effect/http/HttpClient";
9 import * as HttpClientRequest from "effect/http/HttpClientRequest";
10 import * as Redacted from "effect/Redacted";
11 import Stack from "../alchemy.run.ts";
12
13 // Aurora provisioning/deletion is opt-in, as in the RDSData integration suite.
14 describe.skipIf(!!process.env.FAST)("Drizzle + Aurora PostgreSQL", () => {
15 const { test, beforeAll, afterAll, deploy, destroy } = Test.make({
16 providers: AWS.providers(),
17 state: Alchemy.localState(),
18 profile: process.env.ALCHEMY_PROFILE,
19 });
20 const stack = beforeAll(
21 Effect.gen(function* () {
22 yield* destroy(Stack);
23 const outputs = yield* deploy(Stack);
24 yield* deploy(Stack);
25 return outputs;
26 }),
27 { timeout: 1_500_000 },
28 );
29 afterAll(destroy(Stack), { timeout: 1_500_000 });
30
31 test.provider(
32 "CRUD, TLS, least privilege, and repeated invocations",
33 () =>
34 Effect.gen(function* () {
35 const { url } = yield* stack;
36 if (!url) return yield* Effect.fail(new Error("Missing Function URL"));
37 const client = yield* HttpClient.HttpClient;
38 const getCredentials = yield* Credentials;
39 expect((yield* client.get(`${url}health`)).status).toBe(403);
40 const request = Effect.fn(function* (
41 method: "GET" | "POST" | "PATCH" | "DELETE",
42 path: string,
43 body?: object,
44 ) {
45 const credentials = yield* getCredentials;
46 const payload = body === undefined ? undefined : JSON.stringify(body);
47 const signed = yield* sign({
48 method,
49 url: new URL(path, url).toString(),
50 body: payload,
51 service: "lambda",
52 region: credentials.region,
53 accessKeyId: Redacted.value(credentials.accessKeyId),
54 secretAccessKey: credentials.secretAccessKey,
55 sessionToken: credentials.sessionToken,
56 });
57 const base = HttpClientRequest.make(method)(signed.url).pipe(
58 HttpClientRequest.setHeaders(signed.headers),
59 );
60 const response = yield* client.execute(
61 payload === undefined
62 ? base
63 : base.pipe(HttpClientRequest.bodyText(payload, "application/json")),
64 );
65 return { statusCode: response.status, body: yield* response.text };
66 });
67 const health = yield* request("GET", "/health");
68 expect(health.statusCode).toBe(200);
69 expect(JSON.parse(health.body).rows).toEqual([
70 {
71 username: "app_iam",
72 tls: true,
73 can_insert: true,
74 can_create: false,
75 },
76 ]);
77 const id = "aaaaaaaa-0000-4000-8000-000000000001";
78 yield* request("DELETE", `/todos/${id}`);
79 expect((yield* request("GET", "/todos")).body).toBe("[]");
80 const created = yield* request("POST", "/todos", {
81 id,
82 title: "Ship Aurora guide",
83 });
84 expect(created.statusCode).toBe(201);
85 expect(JSON.parse(created.body)).toEqual([{ id, title: "Ship Aurora guide", done: false }]);
86 const updated = yield* request("PATCH", `/todos/${id}`, {
87 done: true,
88 });
89 expect(updated.statusCode).toBe(200);
90 expect(JSON.parse(updated.body)[0].done).toBe(true);
91 for (let i = 0; i < 3; i++) {
92 const listed = yield* request("GET", "/todos");
93 expect(listed.statusCode).toBe(200);
94 expect(JSON.parse(listed.body)).toEqual([{ id, title: "Ship Aurora guide", done: true }]);
95 }
96 expect((yield* request("POST", "/todos", { id: "invalid", title: "" })).statusCode).toBe(
97 400,
98 );
99 expect((yield* request("GET", "/setup")).statusCode).toBe(404);
100 expect((yield* request("DELETE", `/todos/${id}`)).statusCode).toBe(204);
101 expect((yield* request("GET", "/todos")).body).toBe("[]");
102 }),
103 { timeout: 120_000 },
104 );
105 });