@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 152 lines 4.92 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 Alchemy from "alchemy";
5 import * as State from "alchemy/State";
6 import * as Effect from "effect/Effect";
7 import * as HttpClient from "effect/http/HttpClient";
8 import { MinimumLogLevel } from "effect/References";
9 import * as Schedule from "effect/Schedule";
10 import SecretsTestFunctionLive, {
11 CONFIG_SECRET_ENV_KEY,
12 LITERAL_SECRET_VALUE,
13 NUMBER_VAR_VALUE,
14 OBJECT_VAR_VALUE,
15 STRING_VAR_VALUE,
16 SecretsTestFunction,
17 } from "./fixtures/handler.ts";
18
19 /**
20 * `Config.Redacted("CONFIG_SECRET")` resolves against the active
21 * `ConfigProvider` at deploy time. The default provider reads from
22 * `process.env`, so populate it before `beforeAll(deploy(Stack))`
23 * compiles the stack.
24 */
25 const CONFIG_SECRET_VALUE = "sk-from-aws-config-source-xyz";
26 process.env[CONFIG_SECRET_ENV_KEY] = CONFIG_SECRET_VALUE;
27
28 const { test, beforeAll, afterAll, deploy, destroy } = Test.make({
29 providers: AWS.providers(),
30 });
31
32 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
33
34 const Stack = Alchemy.Stack(
35 "AlchemySecretLambdaStack",
36 {
37 providers: AWS.providers(),
38 state: State.localState(),
39 },
40 Effect.gen(function* () {
41 const fn = yield* SecretsTestFunction;
42 return {
43 url: fn.functionUrl.as<string>(),
44 };
45 }).pipe(Effect.provide(SecretsTestFunctionLive)),
46 );
47
48 // The fixture is one Lambda + Function URL, but fresh IAM/Lambda propagation
49 // can exceed 90s under the full c128 sweep. Keep the hook budget below the
50 // factory hard wall while every readiness attempt remains independently
51 // bounded below.
52 const stack = beforeAll(deploy(Stack), { timeout: 210_000 });
53 afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack), { timeout: 180_000 });
54
55 // Lambda Function URLs cold-start (DNS, IAM propagation, init) can take
56 // well over a minute on a fresh deploy under parallel load. Budget a
57 // generous retry window for the very first request, then reuse the
58 // warm URL for subsequent calls.
59 const readinessSchedule = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(20)]);
60
61 const getJson = (url: string) =>
62 HttpClient.get(url).pipe(
63 Effect.timeout("4 seconds"),
64 Effect.flatMap((res) =>
65 res.status === 200
66 ? Effect.flatMap(res.json, (body) => Effect.succeed(body))
67 : Effect.fail(new Error(`Request failed: ${res.status}`)),
68 ),
69 Effect.retry({ schedule: readinessSchedule }),
70 );
71
72 test(
73 "Config.Redacted with literal default round-trips to Lambda runtime as Redacted<string>",
74 Effect.gen(function* () {
75 const { url } = yield* stack;
76 expect(url).toBeTypeOf("string");
77
78 const baseUrl = url.replace(/\/+$/, "");
79 const body = (yield* getJson(`${baseUrl}/secret/literal`)) as {
80 isRedacted: boolean;
81 value: string;
82 };
83 expect(body).toEqual({
84 isRedacted: true,
85 value: LITERAL_SECRET_VALUE,
86 });
87 }).pipe(logLevel),
88 { tags: ["provider:aws", "provider:aws:lambda", "live"], timeout: 45_000 },
89 );
90
91 test(
92 "Config.Redacted resolved from env round-trips through Lambda env",
93 Effect.gen(function* () {
94 const { url } = yield* stack;
95 const baseUrl = url.replace(/\/+$/, "");
96
97 const body = (yield* getJson(`${baseUrl}/secret/config`)) as {
98 isRedacted: boolean;
99 value: string;
100 };
101 expect(body).toEqual({
102 isRedacted: true,
103 value: CONFIG_SECRET_VALUE,
104 });
105 }).pipe(logLevel),
106 { tags: ["provider:aws", "provider:aws:lambda", "live"], timeout: 45_000 },
107 );
108
109 test(
110 "Config.String round-trips to Lambda runtime as a string",
111 Effect.gen(function* () {
112 const { url } = yield* stack;
113 const baseUrl = url.replace(/\/+$/, "");
114
115 const body = (yield* getJson(`${baseUrl}/var/string`)) as {
116 type: string;
117 value: unknown;
118 };
119 expect(body).toEqual({ type: "string", value: STRING_VAR_VALUE });
120 }).pipe(logLevel),
121 { tags: ["provider:aws", "provider:aws:lambda", "live"], timeout: 45_000 },
122 );
123
124 test(
125 "Config.Number round-trips to Lambda runtime preserving the number type",
126 Effect.gen(function* () {
127 const { url } = yield* stack;
128 const baseUrl = url.replace(/\/+$/, "");
129
130 const body = (yield* getJson(`${baseUrl}/var/number`)) as {
131 type: string;
132 value: unknown;
133 };
134 expect(body).toEqual({ type: "number", value: NUMBER_VAR_VALUE });
135 }).pipe(logLevel),
136 { tags: ["provider:aws", "provider:aws:lambda", "live"], timeout: 45_000 },
137 );
138
139 test(
140 "Config.String with object default round-trips to Lambda runtime preserving nested shape",
141 Effect.gen(function* () {
142 const { url } = yield* stack;
143 const baseUrl = url.replace(/\/+$/, "");
144
145 const body = (yield* getJson(`${baseUrl}/var/object`)) as {
146 type: string;
147 value: unknown;
148 };
149 expect(body).toEqual({ type: "object", value: OBJECT_VAR_VALUE });
150 }).pipe(logLevel),
151 { tags: ["provider:aws", "provider:aws:lambda", "live"], timeout: 45_000 },
152 );