@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 100 lines 4.46 KB
Raw
1 import { describe, expect, it } from "@alchemy.run/test";
2 import * as BunServices from "@effect/platform-bun/BunServices";
3 import { RuntimeContext } from "alchemy";
4 import * as Effect from "effect/Effect";
5 import * as FileSystem from "effect/FileSystem";
6 import * as Path from "effect/Path";
7 import { Drizzle } from "@/Drizzle.ts";
8 import { BetterAuth, Database } from "@/index.ts";
9 import * as schema from "./fixtures/drizzle-auth-schema.ts";
10
11 describe("BetterAuth (drizzle)", { tags: ["unit", "local"] }, () => {
12 it.live("uses generated Relations v2 for sign-up, sign-in, and joined sessions", () =>
13 Effect.gen(function* () {
14 const fs = yield* FileSystem.FileSystem;
15 const path = yield* Path.Path;
16 const migration = yield* fs.readFileString(
17 yield* path.fromFileUrl(new URL("./fixtures/drizzle-auth.sql", import.meta.url)),
18 );
19 const { drizzle } = yield* Effect.promise(() => import("drizzle-orm/bun-sqlite"));
20 const { Database: BunSqlite } = yield* Effect.promise(() => import("bun:sqlite"));
21 const client = yield* Effect.acquireRelease(
22 Effect.sync(() => new BunSqlite(":memory:")),
23 (client) => Effect.sync(() => client.close()),
24 );
25 yield* Effect.sync(() => client.exec(migration));
26 const db = yield* Effect.sync(() => drizzle({ client, relations: schema.authRelations }));
27 const auth = yield* BetterAuth({
28 baseURL: "http://localhost:3000",
29 secret: "test-secret-test-secret-test-secret",
30 emailAndPassword: { enabled: true },
31 advanced: { database: { joins: true } },
32 }).pipe(Effect.provide(Drizzle(db, { provider: "sqlite", schema })));
33 const signUp = yield* auth.api.signUpEmail({
34 body: { email: "drizzle@example.com", password: "password1234", name: "Drizzle User" },
35 });
36 const raw = yield* auth.auth;
37 const response = yield* Effect.promise(() =>
38 raw.api.signInEmail({
39 body: { email: "drizzle@example.com", password: "password1234" },
40 asResponse: true,
41 }),
42 );
43 expect(response.status).toBe(200);
44 const cookie = response.headers
45 .getSetCookie()
46 .map((value) => value.split(";")[0])
47 .join("; ");
48 const session = yield* auth.getSession(new Headers({ cookie }));
49 expect(session?.user.id).toBe(signUp.user.id);
50 expect(session?.user.email).toBe("drizzle@example.com");
51 expect(yield* auth.getSession(new Headers())).toBeNull();
52 }).pipe(
53 Effect.scoped,
54 Effect.provide(BunServices.layer),
55 Effect.provide(RuntimeContext.phantom),
56 ),
57 );
58
59 it.live("accepts a request-scoped Postgres client", () =>
60 Effect.gen(function* () {
61 const { drizzle } = yield* Effect.promise(() => import("drizzle-orm/node-postgres"));
62 const { Pool } = yield* Effect.promise(() => import("pg"));
63 let acquired = 0;
64 let released = 0;
65 const database = Effect.gen(function* () {
66 const client = yield* Effect.acquireRelease(
67 Effect.sync(() => {
68 acquired++;
69 return new Pool();
70 }),
71 (pool) =>
72 Effect.promise(() => pool.end()).pipe(Effect.tap(() => Effect.sync(() => released++))),
73 );
74 return yield* Effect.sync(() => drizzle({ client }));
75 });
76 const service = yield* Database.pipe(Effect.provide(Drizzle(database, { provider: "pg" })));
77 expect(acquired).toBe(0);
78 expect(typeof (yield* service.runtime.pipe(Effect.scoped))).toBe("function");
79 expect(acquired).toBe(1);
80 expect(released).toBe(1);
81 }).pipe(Effect.provide(RuntimeContext.phantom)),
82 );
83
84 it.live("wraps an existing drizzle db via the official adapter", () =>
85 Effect.gen(function* () {
86 const { drizzle } = yield* Effect.promise(() => import("drizzle-orm/bun-sqlite"));
87 const { Database: BunSqlite } = yield* Effect.promise(() => import("bun:sqlite"));
88 const db = drizzle({ client: new BunSqlite(":memory:") });
89
90 const service = yield* Database.pipe(Effect.provide(Drizzle(db, { provider: "sqlite" })));
91 // "pg" | "mysql" | "sqlite" maps onto the Database provider kinds
92 expect(service.provider).toBe("sqlite");
93 // no automatic migration support — schema is user-owned
94 expect(service.migrate).toBeUndefined();
95 // the runtime input is better-auth's adapter factory
96 const input = yield* Effect.scoped(service.runtime);
97 expect(typeof input).toBe("function");
98 }).pipe(Effect.provide(RuntimeContext.phantom)),
99 );
100 });