@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 90 lines 3.04 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
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 Effect from "effect/Effect";
6 import { AuthHttpError, edgeRetry, getJson, postJson, toCookieHeader } from "../http.ts";
7 import AuthWorker from "./fixtures/auth-worker.ts";
8
9 const { test, beforeAll, afterAll, deploy, destroy } = Test.make({
10 providers: Cloudflare.providers(),
11 });
12
13 const Stack = Alchemy.Stack(
14 "BetterAuthD1TestStack",
15 { providers: Cloudflare.providers(), state: Cloudflare.state() },
16 Effect.gen(function* () {
17 const worker = yield* AuthWorker;
18 return { url: worker.url.as<string>() };
19 }),
20 );
21
22 const stack = beforeAll(deploy(Stack));
23 afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack));
24
25 test(
26 "auto-migrated D1 worker: sign-up, sign-in, session",
27 Effect.gen(function* () {
28 const { url } = yield* stack;
29 const email = "d1-user@example.com";
30 const password = "password1234";
31
32 // Deploy already ran the schema migration Action — sign-up proves the
33 // tables exist. A leftover user from an interrupted prior run (with
34 // NO_DESTROY) is fine: fall through to sign-in. The retry also rides
35 // out workers.dev subdomain propagation (Cloudflare's 404 HTML page).
36 yield* postJson(`${url}/auth/sign-up/email`, {
37 email,
38 password,
39 name: "D1 User",
40 }).pipe(
41 Effect.filterOrFail(
42 (response) => response.status === 200 || response.body.includes("USER_ALREADY_EXISTS"),
43 (response) => new AuthHttpError({ url, ...response }),
44 ),
45 edgeRetry,
46 );
47
48 const signIn = yield* postJson(`${url}/auth/sign-in/email`, {
49 email,
50 password,
51 }).pipe(
52 Effect.filterOrFail(
53 (response) => response.status === 200,
54 (response) => new AuthHttpError({ url, ...response }),
55 ),
56 );
57 expect(signIn.setCookies.length).toBeGreaterThan(0);
58 const cookie = toCookieHeader(signIn.setCookies);
59
60 // session round-trip through the worker's getSession route
61 const me = yield* getJson<{ email: string | null }>(`${url}/me`, {
62 cookie,
63 });
64 expect(me.email).toBe(email);
65
66 // anonymous request resolves a null session, not an error
67 const anonymous = yield* getJson<{ email: string | null }>(`${url}/me`);
68 expect(anonymous.email).toBeNull();
69 }),
70 {
71 tags: ["provider:cloudflare", "provider:cloudflare:d1", "provider:cloudflare:worker", "live"],
72 timeout: 120_000,
73 },
74 );
75
76 test(
77 "second deploy is a no-op for the migration Action",
78 Effect.gen(function* () {
79 // beforeAll deployed once; deploying again must succeed quickly with
80 // the migration Action's input hash unchanged (no re-migration, no
81 // failure on existing tables).
82 const { url } = yield* deploy(Stack);
83 const me = yield* getJson<{ email: string | null }>(`${url}/me`).pipe(edgeRetry);
84 expect(me.email).toBeNull();
85 }),
86 {
87 tags: ["provider:cloudflare", "provider:cloudflare:d1", "provider:cloudflare:worker", "live"],
88 timeout: 120_000,
89 },
90 );