@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 91 lines 2.88 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import * as Neon from "@alchemy.run/neon";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as Core from "@alchemy.run/test/Core";
6 import * as Effect from "effect/Effect";
7 import * as Layer from "effect/Layer";
8 import * as Schedule from "effect/Schedule";
9 import { AuthHttpError, getJson, postJson, toCookieHeader } from "../http.ts";
10 import AuthFunctionLive, { AuthFunction } from "./fixtures/auth-handler.ts";
11
12 const testOptions = {
13 providers: Layer.mergeAll(AWS.providers(), Neon.providers()),
14 };
15 const { test, beforeAll, afterAll } = Test.make(testOptions);
16 const sharedStack = Core.scratchStack(testOptions, "BetterAuthLambda");
17
18 let baseUrl: string;
19
20 // Lambda function URL cold-start can take well over 60s on a fresh
21 // deploy — poll readiness before the actual assertions.
22 const readinessRetry = Effect.retry({
23 schedule: Schedule.exponential("2 seconds", 1.5),
24 times: 10,
25 });
26
27 beforeAll(
28 Effect.gen(function* () {
29 yield* sharedStack.destroy();
30 const { functionUrl } = yield* sharedStack.deploy(
31 Effect.gen(function* () {
32 return yield* AuthFunction;
33 }).pipe(Effect.provide(AuthFunctionLive)),
34 );
35 expect(functionUrl).toBeTruthy();
36 baseUrl = functionUrl!.replace(/\/+$/, "");
37
38 yield* getJson<{ email: string | null }>(`${baseUrl}/me`).pipe(
39 Effect.tapError((error) => Effect.logWarning(`Lambda not ready yet: ${error.message}`)),
40 readinessRetry,
41 );
42 }),
43 { timeout: 240_000 },
44 );
45
46 afterAll.skipIf(!!process.env.NO_DESTROY)(sharedStack.destroy(), {
47 timeout: 120_000,
48 });
49
50 test(
51 "Lambda host: sign-up, sign-in, session against Neon Postgres",
52 Effect.gen(function* () {
53 const email = "lambda-user@example.com";
54 const password = "password1234";
55
56 yield* postJson(`${baseUrl}/auth/sign-up/email`, {
57 email,
58 password,
59 name: "Lambda User",
60 }).pipe(
61 Effect.filterOrFail(
62 (response) => response.status === 200 || response.body.includes("USER_ALREADY_EXISTS"),
63 (response) => new AuthHttpError({ url: baseUrl, ...response }),
64 ),
65 );
66
67 const signIn = yield* postJson(`${baseUrl}/auth/sign-in/email`, {
68 email,
69 password,
70 }).pipe(
71 Effect.filterOrFail(
72 (response) => response.status === 200,
73 (response) => new AuthHttpError({ url: baseUrl, ...response }),
74 ),
75 );
76 expect(signIn.setCookies.length).toBeGreaterThan(0);
77 const cookie = toCookieHeader(signIn.setCookies);
78
79 const me = yield* getJson<{ email: string | null }>(`${baseUrl}/me`, {
80 cookie,
81 });
82 expect(me.email).toBe(email);
83
84 const anonymous = yield* getJson<{ email: string | null }>(`${baseUrl}/me`);
85 expect(anonymous.email).toBeNull();
86 }),
87 {
88 tags: ["provider:aws", "provider:aws:lambda", "provider:neon", "provider:neon:project", "live"],
89 timeout: 120_000,
90 },
91 );