@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 128 lines 4.19 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { expect } from "@alchemy.run/test";
3 import * as Test from "@alchemy.run/test/Alchemy";
4 import * as Lambda from "@distilled.cloud/aws/lambda";
5 import * as Provider from "alchemy/Provider";
6 import * as Effect from "effect/Effect";
7 import * as Schedule from "effect/Schedule";
8 import { TestFunction, TestFunctionLive } from "./handler.ts";
9
10 const { test } = Test.make({ providers: AWS.providers() });
11
12 test.provider(
13 "creates permission scoped to function URL invocation",
14 (stack) =>
15 Effect.gen(function* () {
16 yield* stack.destroy();
17
18 const deployed = yield* stack.deploy(
19 Effect.gen(function* () {
20 const fn = yield* TestFunction;
21 const permission = yield* AWS.Lambda.Permission("UrlInvokeOnly", {
22 action: "lambda:InvokeFunction",
23 functionName: fn.functionName,
24 principal: "*",
25 invokedViaFunctionUrl: true,
26 });
27
28 return { fn, permission };
29 }).pipe(Effect.provide(TestFunctionLive)),
30 );
31
32 const policy = yield* getPolicyStatement(
33 deployed.fn.functionName,
34 deployed.permission.statementId,
35 );
36
37 expect(policy.Condition).toEqual({
38 Bool: {
39 "lambda:InvokedViaFunctionUrl": "true",
40 },
41 });
42
43 yield* stack.destroy();
44 yield* assertFunctionDeleted(deployed.fn.functionName);
45 }).pipe(
46 Effect.tap(() => stack.destroy()),
47 Effect.onError(() => stack.destroy().pipe(Effect.ignore)),
48 ),
49 { tags: ["provider:aws", "provider:aws:lambda", "live"], timeout: 180_000 },
50 );
51
52 test.provider(
53 "list enumerates the deployed permission",
54 (stack) =>
55 Effect.gen(function* () {
56 yield* stack.destroy();
57
58 const deployed = yield* stack.deploy(
59 Effect.gen(function* () {
60 const fn = yield* TestFunction;
61 const permission = yield* AWS.Lambda.Permission("ListPermission", {
62 action: "lambda:InvokeFunction",
63 functionName: fn.functionName,
64 principal: "*",
65 invokedViaFunctionUrl: true,
66 });
67
68 return { fn, permission };
69 }).pipe(Effect.provide(TestFunctionLive)),
70 );
71
72 const provider = yield* Provider.findProvider(AWS.Lambda.Permission);
73 const all = yield* provider.list();
74
75 expect(
76 all.some(
77 (p) =>
78 p.statementId === deployed.permission.statementId &&
79 p.functionName === deployed.fn.functionName,
80 ),
81 ).toBe(true);
82
83 yield* stack.destroy();
84 yield* assertFunctionDeleted(deployed.fn.functionName);
85 }).pipe(
86 Effect.tap(() => stack.destroy()),
87 Effect.onError(() => stack.destroy().pipe(Effect.ignore)),
88 ),
89 { tags: ["provider:aws", "provider:aws:lambda", "live"], timeout: 180_000 },
90 );
91
92 // Out-of-band proof that the trailing destroy removed the host function (and
93 // with it the permission's policy) from the cloud.
94 const assertFunctionDeleted = Effect.fn(function* (functionName: string) {
95 yield* Lambda.getFunction({ FunctionName: functionName }).pipe(
96 Effect.flatMap(() => Effect.fail(new Error(`Function ${functionName} still exists`))),
97 Effect.catchTag("ResourceNotFoundException", () => Effect.void),
98 Effect.retry({
99 schedule: Schedule.max([Schedule.exponential(500), Schedule.recurs(8)]),
100 }),
101 );
102 });
103
104 const getPolicyStatement = Effect.fn(function* (functionName: string, statementId: string) {
105 return yield* Lambda.getPolicy({ FunctionName: functionName }).pipe(
106 Effect.flatMap(({ Policy }) =>
107 Effect.try({
108 try: () => {
109 const policy = JSON.parse(Policy ?? "{}") as {
110 Statement?: Array<{
111 Sid?: string;
112 Condition?: unknown;
113 }>;
114 };
115 const statement = policy.Statement?.find((statement) => statement.Sid === statementId);
116 if (!statement) {
117 throw new Error(`Policy statement ${statementId} not found`);
118 }
119 return statement;
120 },
121 catch: (cause) => (cause instanceof Error ? cause : new Error(String(cause))),
122 }),
123 ),
124 Effect.retry({
125 schedule: Schedule.max([Schedule.exponential(500), Schedule.recurs(10)]),
126 }),
127 );
128 });