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 { describe, expect } from "bun:test"; |
| 2 | import * as AWS from "@alchemy.run/aws"; |
| 3 | import * as Test from "@alchemy.run/test/Bun"; |
| 4 | import * as Core from "@alchemy.run/test/Core"; |
| 5 | import { Credentials } from "@distilled.cloud/aws/Credentials"; |
| 6 | import * as dsql from "@distilled.cloud/aws/dsql"; |
| 7 | import * as iam from "@distilled.cloud/aws/iam"; |
| 8 | import { sign } from "@distilled.cloud/aws/SigV4"; |
| 9 | import * as Alchemy from "alchemy"; |
| 10 | import * as Effect from "effect/Effect"; |
| 11 | import * as HttpClient from "effect/http/HttpClient"; |
| 12 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 13 | import * as Redacted from "effect/Redacted"; |
| 14 | import Stack from "../alchemy.run.ts"; |
| 15 | |
| 16 | describe("Drizzle + Aurora DSQL", () => { |
| 17 | const { test, beforeAll, afterAll, deploy, destroy } = Test.make({ |
| 18 | providers: AWS.providers(), |
| 19 | state: Alchemy.localState(), |
| 20 | profile: process.env.ALCHEMY_PROFILE, |
| 21 | }); |
| 22 | let clusterId: string | undefined; |
| 23 | const stack = beforeAll( |
| 24 | Effect.gen(function* () { |
| 25 | yield* destroy(Stack); |
| 26 | const outputs = yield* deploy(Stack); |
| 27 | clusterId = outputs.clusterId; |
| 28 | yield* deploy(Stack); |
| 29 | return outputs; |
| 30 | }), |
| 31 | { timeout: 600_000 }, |
| 32 | ); |
| 33 | afterAll( |
| 34 | Effect.gen(function* () { |
| 35 | yield* destroy(Stack); |
| 36 | if (clusterId) { |
| 37 | const status = yield* Core.withProviders( |
| 38 | dsql.getCluster({ identifier: clusterId }).pipe( |
| 39 | Effect.map((cluster) => cluster.status), |
| 40 | Effect.catchTag("ResourceNotFoundException", () => Effect.succeed("NOT_FOUND")), |
| 41 | ), |
| 42 | { providers: AWS.providers() }, |
| 43 | "aws-dsql-drizzle", |
| 44 | ); |
| 45 | expect(["DELETING", "DELETED", "NOT_FOUND"]).toContain(status); |
| 46 | } |
| 47 | }), |
| 48 | { timeout: 600_000 }, |
| 49 | ); |
| 50 | |
| 51 | test.provider( |
| 52 | "signed HTTP CRUD with a non-admin database role and verified TLS", |
| 53 | () => |
| 54 | Effect.gen(function* () { |
| 55 | const { url, roleName } = yield* stack; |
| 56 | if (!url) return yield* Effect.fail(new Error("Missing Function URL")); |
| 57 | const policies = yield* iam.listRolePolicies({ RoleName: roleName }); |
| 58 | const documents = yield* Effect.forEach(policies.PolicyNames, (PolicyName) => |
| 59 | iam |
| 60 | .getRolePolicy({ RoleName: roleName, PolicyName }) |
| 61 | .pipe(Effect.map((policy) => decodeURIComponent(policy.PolicyDocument))), |
| 62 | ); |
| 63 | expect(documents.join("\n")).toContain('"dsql:DbConnect"'); |
| 64 | expect(documents.join("\n")).not.toContain("DbConnectAdmin"); |
| 65 | |
| 66 | const client = yield* HttpClient.HttpClient; |
| 67 | const getCredentials = yield* Credentials; |
| 68 | expect((yield* client.get(`${url}health`)).status).toBe(403); |
| 69 | const request = Effect.fn(function* ( |
| 70 | method: "GET" | "POST" | "PATCH" | "DELETE", |
| 71 | path: string, |
| 72 | body?: object, |
| 73 | ) { |
| 74 | const credentials = yield* getCredentials; |
| 75 | const payload = body === undefined ? undefined : JSON.stringify(body); |
| 76 | const signed = yield* sign({ |
| 77 | method, |
| 78 | url: new URL(path, url).toString(), |
| 79 | body: payload, |
| 80 | service: "lambda", |
| 81 | region: credentials.region, |
| 82 | accessKeyId: Redacted.value(credentials.accessKeyId), |
| 83 | secretAccessKey: credentials.secretAccessKey, |
| 84 | sessionToken: credentials.sessionToken, |
| 85 | }); |
| 86 | const base = HttpClientRequest.make(method)(signed.url).pipe( |
| 87 | HttpClientRequest.setHeaders(signed.headers), |
| 88 | ); |
| 89 | const response = yield* client.execute( |
| 90 | payload === undefined |
| 91 | ? base |
| 92 | : base.pipe(HttpClientRequest.bodyText(payload, "application/json")), |
| 93 | ); |
| 94 | return { status: response.status, body: yield* response.text }; |
| 95 | }); |
| 96 | const health = yield* request("GET", "/health"); |
| 97 | expect(health.status).toBe(200); |
| 98 | expect(JSON.parse(health.body).rows).toEqual([{ username: "app_user" }]); |
| 99 | const id = "aaaaaaaa-0000-4000-8000-000000000001"; |
| 100 | yield* request("DELETE", `/todos/${id}`); |
| 101 | expect((yield* request("GET", "/todos")).body).toBe("[]"); |
| 102 | const created = yield* request("POST", "/todos", { |
| 103 | id, |
| 104 | text: "Ship DSQL guide", |
| 105 | }); |
| 106 | expect(created.status).toBe(201); |
| 107 | expect(JSON.parse(created.body)[0]).toMatchObject({ |
| 108 | id, |
| 109 | text: "Ship DSQL guide", |
| 110 | done: false, |
| 111 | }); |
| 112 | const updated = yield* request("PATCH", `/todos/${id}`, { done: true }); |
| 113 | expect(updated.status).toBe(200); |
| 114 | expect(JSON.parse(updated.body)[0].done).toBe(true); |
| 115 | for (let i = 0; i < 3; i++) { |
| 116 | const listed = yield* request("GET", "/todos"); |
| 117 | expect(listed.status).toBe(200); |
| 118 | expect(JSON.parse(listed.body)).toHaveLength(1); |
| 119 | expect(JSON.parse(listed.body)[0]).toMatchObject({ |
| 120 | id, |
| 121 | text: "Ship DSQL guide", |
| 122 | done: true, |
| 123 | }); |
| 124 | } |
| 125 | expect((yield* request("POST", "/todos", { id: "invalid", text: "" })).status).toBe(400); |
| 126 | expect((yield* request("GET", "/setup")).status).toBe(404); |
| 127 | expect((yield* request("DELETE", `/todos/${id}`)).status).toBe(204); |
| 128 | expect((yield* request("GET", "/todos")).body).toBe("[]"); |
| 129 | }), |
| 130 | { timeout: 120_000 }, |
| 131 | ); |
| 132 | }); |