@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 240 lines 8.15 KB
Raw
1 import { describe, expect, it } from "@alchemy.run/test";
2 import * as Effect from "effect/Effect";
3 import { makeDataApiDialect, type DataApiExecutor } from "@/AuroraDataApi.ts";
4
5 interface Recorded {
6 sql: string;
7 parameters: unknown[] | undefined;
8 transactionId: string | undefined;
9 }
10
11 /** Recording mock of the Data API surface the dialect drives. */
12 const makeMock = (
13 respond: (request: Recorded) => {
14 records?: unknown[][];
15 columnMetadata?: { label?: string; typeName?: string }[];
16 numberOfRecordsUpdated?: number;
17 } = () => ({}),
18 ) => {
19 const calls: { executed: Recorded[]; begun: number; committed: string[]; rolledBack: string[] } =
20 { executed: [], begun: 0, committed: [], rolledBack: [] };
21 const executor: DataApiExecutor = {
22 execute: async (request) => {
23 const recorded: Recorded = {
24 sql: request.sql,
25 parameters: request.parameters as unknown[] | undefined,
26 transactionId: request.transactionId,
27 };
28 calls.executed.push(recorded);
29 return respond(recorded) as never;
30 },
31 begin: async () => {
32 calls.begun += 1;
33 return { transactionId: `tx-${calls.begun}` };
34 },
35 commit: async (transactionId) => {
36 calls.committed.push(transactionId);
37 return {};
38 },
39 rollback: async (transactionId) => {
40 calls.rolledBack.push(transactionId);
41 return {};
42 },
43 };
44 return { executor, calls };
45 };
46
47 const makeDb = (executor: DataApiExecutor) =>
48 Effect.gen(function* () {
49 const dialect = yield* makeDataApiDialect(executor);
50 const { Kysely } = yield* Effect.promise(() => import("kysely"));
51 return new Kysely<Record<string, Record<string, unknown>>>({ dialect });
52 });
53
54 describe(
55 "AuroraDataApi dialect",
56 { tags: ["unit", "provider:aws", "provider:aws:rdsdata", "local"] },
57 () => {
58 it.live("compiles postgres SQL with named :n parameters", () =>
59 Effect.gen(function* () {
60 const { executor, calls } = makeMock();
61 const db = yield* makeDb(executor);
62 const createdAt = new Date("2026-08-10T12:00:00.000Z");
63 yield* Effect.promise(() =>
64 db
65 .insertInto("user")
66 .values({ id: "u1", email: "a@b.co", emailVerified: false, age: 42, createdAt })
67 .execute(),
68 );
69 const call = calls.executed[0]!;
70 expect(call.sql).toContain('insert into "user"');
71 expect(call.sql).toContain(":1");
72 expect(call.sql).toContain(":5");
73 expect(call.parameters).toEqual([
74 { name: "1", value: { stringValue: "u1" } },
75 { name: "2", value: { stringValue: "a@b.co" } },
76 { name: "3", value: { booleanValue: false } },
77 { name: "4", value: { longValue: 42 } },
78 { name: "5", typeHint: "TIMESTAMP", value: { stringValue: "2026-08-10 12:00:00.000" } },
79 ]);
80 }),
81 );
82
83 it.live("maps records to rows and revives timestamps as Dates", () =>
84 Effect.gen(function* () {
85 const { executor } = makeMock(() => ({
86 columnMetadata: [
87 { label: "id", typeName: "varchar" },
88 { label: "createdAt", typeName: "timestamp" },
89 { label: "count", typeName: "int8" },
90 ],
91 records: [
92 [{ stringValue: "u1" }, { stringValue: "2026-08-10 12:00:00" }, { longValue: 7 }],
93 [{ stringValue: "u2" }, { isNull: true }, { isNull: true }],
94 ],
95 }));
96 const db = yield* makeDb(executor);
97 const rows = yield* Effect.promise(() => db.selectFrom("user").selectAll().execute());
98 expect(rows).toHaveLength(2);
99 expect(rows[0]!.id).toBe("u1");
100 expect(rows[0]!.createdAt).toBeInstanceOf(Date);
101 expect((rows[0]!.createdAt as Date).toISOString()).toBe("2026-08-10T12:00:00.000Z");
102 expect(rows[0]!.count).toBe(7);
103 expect(rows[1]!.createdAt).toBeNull();
104 }),
105 );
106
107 it.live("preserves schema search paths and other array-valued results", () =>
108 Effect.gen(function* () {
109 const { executor } = makeMock(() => ({
110 columnMetadata: [
111 { label: "schemas", typeName: "_text" },
112 { label: "flags" },
113 { label: "integers" },
114 { label: "decimals" },
115 { label: "nested" },
116 { label: "empty" },
117 ],
118 records: [
119 [
120 { arrayValue: { stringValues: ["pg_catalog", "public"] } },
121 { arrayValue: { booleanValues: [true, false] } },
122 { arrayValue: { longValues: [1, 2] } },
123 { arrayValue: { doubleValues: [1.5, 2.5] } },
124 { arrayValue: { arrayValues: [{ longValues: [1, 2] }, { longValues: [3, 4] }] } },
125 { arrayValue: { stringValues: [] } },
126 ],
127 ],
128 }));
129 const db = yield* makeDb(executor);
130 const rows = yield* Effect.promise(() => db.selectFrom("metadata").selectAll().execute());
131 expect(rows).toEqual([
132 {
133 schemas: ["pg_catalog", "public"],
134 flags: [true, false],
135 integers: [1, 2],
136 decimals: [1.5, 2.5],
137 nested: [
138 [1, 2],
139 [3, 4],
140 ],
141 empty: [],
142 },
143 ]);
144 }),
145 );
146
147 it.live("casts catalog table kinds to text during schema introspection", () =>
148 Effect.gen(function* () {
149 const { executor, calls } = makeMock(() => ({
150 columnMetadata: [
151 { label: "column" },
152 { label: "not_null" },
153 { label: "has_default" },
154 { label: "table" },
155 { label: "table_type" },
156 { label: "schema" },
157 { label: "type" },
158 { label: "type_schema" },
159 { label: "column_description" },
160 { label: "auto_incrementing" },
161 ],
162 records: [
163 [
164 { stringValue: "id" },
165 { booleanValue: true },
166 { booleanValue: false },
167 { stringValue: "user" },
168 { stringValue: "r" },
169 { stringValue: "public" },
170 { stringValue: "text" },
171 { stringValue: "pg_catalog" },
172 { isNull: true },
173 { isNull: true },
174 ],
175 ],
176 }));
177 const db = yield* makeDb(executor);
178 const tables = yield* Effect.promise(() => db.introspection.getTables());
179 expect(calls.executed[0]!.sql).toContain('cast("c"."relkind" as text) as "table_type"');
180 expect(tables).toEqual([
181 {
182 name: "user",
183 schema: "public",
184 isView: false,
185 isForeign: false,
186 columns: [
187 {
188 name: "id",
189 dataType: "text",
190 dataTypeSchema: "pg_catalog",
191 isNullable: false,
192 hasDefaultValue: false,
193 isAutoIncrementing: false,
194 comment: undefined,
195 },
196 ],
197 },
198 ]);
199 }),
200 );
201
202 it.live("threads transactions through begin/commit", () =>
203 Effect.gen(function* () {
204 const { executor, calls } = makeMock();
205 const db = yield* makeDb(executor);
206 yield* Effect.promise(() =>
207 db.transaction().execute(async (trx) => {
208 await trx.deleteFrom("session").execute();
209 await trx.deleteFrom("account").execute();
210 }),
211 );
212 expect(calls.begun).toBe(1);
213 expect(calls.executed.map((call) => call.transactionId)).toEqual(["tx-1", "tx-1"]);
214 expect(calls.committed).toEqual(["tx-1"]);
215 expect(calls.rolledBack).toEqual([]);
216 }),
217 );
218
219 it.live("rolls back failed transactions", () =>
220 Effect.gen(function* () {
221 const { executor, calls } = makeMock();
222 const db = yield* makeDb(executor);
223 const result = yield* Effect.promise(() =>
224 db
225 .transaction()
226 .execute(async () => {
227 throw new Error("boom");
228 })
229 .then(
230 () => "ok",
231 () => "failed",
232 ),
233 );
234 expect(result).toBe("failed");
235 expect(calls.rolledBack).toEqual(["tx-1"]);
236 expect(calls.committed).toEqual([]);
237 }),
238 );
239 },
240 );