@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 416 lines 13.1 KB
Raw
1 import {
2 fromAttributeValue,
3 isMapSchemaType,
4 isNumberSetSchema,
5 isStringSetSchema,
6 toAttributeType,
7 toAttributeValue,
8 } from "@alchemy.run/aws/DynamoDB/AttributeValue";
9 import { describe, expect, test } from "@alchemy.run/test";
10 import type { AttributeValue } from "@distilled.cloud/aws/dynamodb";
11 import * as Effect from "effect/Effect";
12 import * as S from "effect/Schema";
13
14 describe(
15 "toAttributeValue",
16 { tags: ["unit", "provider:aws", "provider:aws:dynamodb", "local"] },
17 () => {
18 test("undefined -> NULL false", async () => {
19 const result = await Effect.runPromise(toAttributeValue(undefined));
20 expect(result).toEqual({ NULL: false });
21 });
22
23 test("null -> NULL true", async () => {
24 const result = await Effect.runPromise(toAttributeValue(null));
25 expect(result).toEqual({ NULL: true });
26 });
27
28 test("boolean true -> BOOL", async () => {
29 const result = await Effect.runPromise(toAttributeValue(true));
30 expect(result).toEqual({ BOOL: true });
31 });
32
33 test("boolean false -> BOOL", async () => {
34 const result = await Effect.runPromise(toAttributeValue(false));
35 expect(result).toEqual({ BOOL: false });
36 });
37
38 test("string -> S", async () => {
39 const result = await Effect.runPromise(toAttributeValue("hello"));
40 expect(result).toEqual({ S: "hello" });
41 });
42
43 test("number -> N", async () => {
44 const result = await Effect.runPromise(toAttributeValue(42));
45 expect(result).toEqual({ N: "42" });
46 });
47
48 test("float number -> N", async () => {
49 const result = await Effect.runPromise(toAttributeValue(3.14));
50 expect(result).toEqual({ N: "3.14" });
51 });
52
53 test("array -> L", async () => {
54 const result = await Effect.runPromise(toAttributeValue([1, 2, 3]));
55 expect(result).toEqual({ L: [{ N: "1" }, { N: "2" }, { N: "3" }] });
56 });
57
58 test("mixed array -> L", async () => {
59 const result = await Effect.runPromise(toAttributeValue(["hello", 42, true]));
60 expect(result).toEqual({ L: [{ S: "hello" }, { N: "42" }, { BOOL: true }] });
61 });
62
63 test("empty array -> L", async () => {
64 const result = await Effect.runPromise(toAttributeValue([]));
65 expect(result).toEqual({ L: [] });
66 });
67
68 test("object -> M", async () => {
69 const result = await Effect.runPromise(toAttributeValue({ name: "Alice", age: 30 }));
70 expect(result).toEqual({ M: { name: { S: "Alice" }, age: { N: "30" } } });
71 });
72
73 test("nested object -> M", async () => {
74 const result = await Effect.runPromise(
75 toAttributeValue({ user: { name: "Alice", details: { age: 30 } } }),
76 );
77 expect(result).toEqual({
78 M: { user: { M: { name: { S: "Alice" }, details: { M: { age: { N: "30" } } } } } },
79 });
80 });
81
82 test("empty object -> M", async () => {
83 const result = await Effect.runPromise(toAttributeValue({}));
84 expect(result).toEqual({ M: {} });
85 });
86
87 test("Set of strings -> SS", async () => {
88 const result = await Effect.runPromise(toAttributeValue(new Set(["a", "b", "c"])));
89 expect(result).toEqual({ SS: ["a", "b", "c"] });
90 });
91
92 test("Set of numbers -> NS", async () => {
93 const result = await Effect.runPromise(toAttributeValue(new Set([1, 2, 3])));
94 // NS values are returned as-is (numbers in this case)
95 expect(result.NS).toEqual(["1", "2", "3"]);
96 });
97
98 test("empty Set -> SS (empty)", async () => {
99 const result = await Effect.runPromise(toAttributeValue(new Set()));
100 expect(result).toEqual({ SS: [] });
101 });
102
103 test("Set of mixed types -> L", async () => {
104 const result = await Effect.runPromise(toAttributeValue(new Set([1, "two", true])));
105 expect(result).toEqual({ L: [{ N: "1" }, { S: "two" }, { BOOL: true }] });
106 });
107
108 test("Uint8Array -> B", async () => {
109 const uint8 = new Uint8Array([1, 2, 3, 4]);
110 const result = await Effect.runPromise(toAttributeValue(uint8));
111 expect(result).toEqual({ B: uint8 });
112 });
113
114 test("Buffer -> B", async () => {
115 const buffer = Buffer.from([1, 2, 3, 4]);
116 const result = await Effect.runPromise(toAttributeValue(buffer));
117 expect(result.B).toBeInstanceOf(Uint8Array);
118 });
119
120 test("File -> B", async () => {
121 const file = new File(["hello"], "test.txt");
122 const result = await Effect.runPromise(toAttributeValue(file));
123 expect(result.B).toBeInstanceOf(Uint8Array);
124 });
125
126 test("ArrayBuffer -> B", async () => {
127 const arrayBuffer = new ArrayBuffer(4);
128 const result = await Effect.runPromise(toAttributeValue(arrayBuffer));
129 expect(result.B).toBeInstanceOf(Uint8Array);
130 });
131
132 test("complex nested structure", async () => {
133 const result = await Effect.runPromise(
134 toAttributeValue({
135 id: "123",
136 name: "Alice",
137 age: 30,
138 active: true,
139 tags: ["developer", "typescript"],
140 metadata: { created: "2023-01-01", count: 42 },
141 scores: new Set([100, 95, 88]),
142 }),
143 );
144 expect(result).toEqual({
145 M: {
146 id: { S: "123" },
147 name: { S: "Alice" },
148 age: { N: "30" },
149 active: { BOOL: true },
150 tags: { L: [{ S: "developer" }, { S: "typescript" }] },
151 metadata: { M: { created: { S: "2023-01-01" }, count: { N: "42" } } },
152 scores: { NS: expect.any(Array) },
153 },
154 });
155 });
156
157 test("invalid type throws error", async () => {
158 const symbol = Symbol("test");
159 const result = Effect.runPromise(
160 toAttributeValue(symbol).pipe(
161 Effect.map(() => false),
162 Effect.catchTag("InvalidAttributeValue", (_error) => Effect.succeed(true)),
163 ),
164 );
165 await expect(result).resolves.toBe(true);
166 });
167 },
168 );
169
170 describe(
171 "fromAttributeValue",
172 { tags: ["unit", "provider:aws", "provider:aws:dynamodb", "local"] },
173 () => {
174 test("NULL true -> null", () => {
175 expect(fromAttributeValue({ NULL: true })).toBe(null);
176 });
177
178 test("BOOL -> boolean", () => {
179 expect(fromAttributeValue({ BOOL: true })).toBe(true);
180 expect(fromAttributeValue({ BOOL: false })).toBe(false);
181 });
182
183 test("S -> string", () => {
184 expect(fromAttributeValue({ S: "hello" })).toBe("hello");
185 });
186
187 test("N -> number", () => {
188 expect(fromAttributeValue({ N: "42" })).toBe(42);
189 expect(fromAttributeValue({ N: "3.14" })).toBe(3.14);
190 });
191
192 test("L -> array", () => {
193 const result = fromAttributeValue({ L: [{ N: "1" }, { N: "2" }, { N: "3" }] });
194 expect(result).toEqual([1, 2, 3]);
195 });
196
197 test("M -> object", () => {
198 const result = fromAttributeValue({ M: { name: { S: "Alice" }, age: { N: "30" } } });
199 expect(result).toEqual({ name: "Alice", age: 30 });
200 });
201
202 test("SS -> Set of strings", () => {
203 const result = fromAttributeValue({ SS: ["a", "b", "c"] });
204 expect(result).toEqual(new Set(["a", "b", "c"]));
205 });
206
207 test("NS -> Set of numbers", () => {
208 const result = fromAttributeValue({ NS: ["1", "2", "3"] });
209 expect(result).toEqual(new Set(["1", "2", "3"]));
210 });
211
212 test("BS -> Set of binary", () => {
213 const binary = [new Uint8Array([1, 2]), new Uint8Array([3, 4])];
214 const result = fromAttributeValue({ BS: binary });
215 expect(result).toEqual(new Set(binary));
216 });
217
218 test("nested structure", () => {
219 const result = fromAttributeValue({
220 M: {
221 id: { S: "123" },
222 name: { S: "Alice" },
223 tags: { L: [{ S: "developer" }, { S: "typescript" }] },
224 metadata: { M: { count: { N: "42" } } },
225 },
226 });
227 expect(result).toEqual({
228 id: "123",
229 name: "Alice",
230 tags: ["developer", "typescript"],
231 metadata: { count: 42 },
232 });
233 });
234
235 test("unknown attribute value throws error", () => {
236 expect(() => fromAttributeValue({} as AttributeValue)).toThrow();
237 });
238 },
239 );
240
241 describe(
242 "toAttributeType",
243 { tags: ["unit", "provider:aws", "provider:aws:dynamodb", "local"] },
244 () => {
245 test("String schema -> S", () => {
246 expect(toAttributeType(S.String)).toBe("S");
247 });
248
249 test("Number schema -> N", () => {
250 expect(toAttributeType(S.Number)).toBe("N");
251 });
252
253 test("Struct schema -> M", () => {
254 expect(toAttributeType(S.Struct({ name: S.String, age: S.Number }))).toBe("M");
255 });
256
257 test("Record schema -> M", () => {
258 expect(toAttributeType(S.Record(S.String, S.String))).toBe("M");
259 });
260
261 test("Map schema -> M", () => {
262 expect(toAttributeType(S.ReadonlyMap(S.String, S.String))).toBe("M");
263 });
264
265 test("Array/List schema -> L", () => {
266 expect(toAttributeType(S.Array(S.String))).toBe("L");
267 });
268
269 test("Set of strings -> SS", () => {
270 expect(toAttributeType(S.ReadonlySet(S.String))).toBe("SS");
271 });
272
273 test("Set of numbers -> NS", () => {
274 expect(toAttributeType(S.ReadonlySet(S.Number))).toBe("NS");
275 });
276
277 test("Class schema -> M", () => {
278 class User extends S.Class<User>("User")({ name: S.String, age: S.Number }) {}
279 expect(toAttributeType(User)).toBe("M");
280 });
281
282 test("default fallback -> S", () => {
283 expect(toAttributeType(S.Boolean)).toBe("S");
284 });
285 },
286 );
287
288 describe(
289 "isMapSchemaType",
290 { tags: ["unit", "provider:aws", "provider:aws:dynamodb", "local"] },
291 () => {
292 test("Map schema", () => {
293 expect(isMapSchemaType(S.ReadonlyMap(S.String, S.String))).toBe(true);
294 });
295
296 test("Record schema", () => {
297 expect(isMapSchemaType(S.Record(S.String, S.String))).toBe(true);
298 });
299
300 test("Struct schema", () => {
301 expect(isMapSchemaType(S.Struct({ name: S.String, age: S.Number }))).toBe(true);
302 });
303
304 test("Class schema", () => {
305 class User extends S.Class<User>("User")({ name: S.String, age: S.Number }) {}
306 expect(isMapSchemaType(User)).toBe(true);
307 });
308
309 test("not a map schema", () => {
310 expect(isMapSchemaType(S.String)).toBe(false);
311 expect(isMapSchemaType(S.Number)).toBe(false);
312 expect(isMapSchemaType(S.Array(S.String))).toBe(false);
313 });
314 },
315 );
316
317 describe(
318 "isStringSetSchema",
319 { tags: ["unit", "provider:aws", "provider:aws:dynamodb", "local"] },
320 () => {
321 test("Set of strings", () => {
322 expect(isStringSetSchema(S.ReadonlySet(S.String))).toBe(true);
323 });
324
325 test("not a string set", () => {
326 expect(isStringSetSchema(S.ReadonlySet(S.Number))).toBe(false);
327 expect(isStringSetSchema(S.String)).toBe(false);
328 expect(isStringSetSchema(S.Array(S.String))).toBe(false);
329 });
330 },
331 );
332
333 describe(
334 "isNumberSetSchema",
335 { tags: ["unit", "provider:aws", "provider:aws:dynamodb", "local"] },
336 () => {
337 test("Set of numbers", () => {
338 expect(isNumberSetSchema(S.ReadonlySet(S.Number))).toBe(true);
339 });
340
341 test("not a number set", () => {
342 expect(isNumberSetSchema(S.ReadonlySet(S.String))).toBe(false);
343 expect(isNumberSetSchema(S.Number)).toBe(false);
344 expect(isNumberSetSchema(S.Array(S.Number))).toBe(false);
345 });
346 },
347 );
348
349 describe(
350 "round-trip conversions",
351 { tags: ["unit", "provider:aws", "provider:aws:dynamodb", "local"] },
352 () => {
353 test("string", async () => {
354 const original = "hello";
355 const attr = await Effect.runPromise(toAttributeValue(original));
356 const result = fromAttributeValue(attr);
357 expect(result).toBe(original);
358 });
359
360 test("number", async () => {
361 const original = 42;
362 const attr = await Effect.runPromise(toAttributeValue(original));
363 const result = fromAttributeValue(attr);
364 expect(result).toBe(original);
365 });
366
367 test("boolean", async () => {
368 const original = true;
369 const attr = await Effect.runPromise(toAttributeValue(original));
370 const result = fromAttributeValue(attr);
371 expect(result).toBe(original);
372 });
373
374 test("null", async () => {
375 const original = null;
376 const attr = await Effect.runPromise(toAttributeValue(original));
377 const result = fromAttributeValue(attr);
378 expect(result).toBe(original);
379 });
380
381 test("array", async () => {
382 const original = [1, "two", true];
383 const attr = await Effect.runPromise(toAttributeValue(original));
384 const result = fromAttributeValue(attr);
385 expect(result).toEqual(original);
386 });
387
388 test("object", async () => {
389 const original = { name: "Alice", age: 30, active: true };
390 const attr = await Effect.runPromise(toAttributeValue(original));
391 const result = fromAttributeValue(attr);
392 expect(result).toEqual(original);
393 });
394
395 test("Set of strings", async () => {
396 const original = new Set(["a", "b", "c"]);
397 const attr = await Effect.runPromise(toAttributeValue(original));
398 const result = fromAttributeValue(attr);
399 expect(result).toEqual(original);
400 });
401
402 test("complex nested structure", async () => {
403 const original = {
404 id: "123",
405 name: "Alice",
406 age: 30,
407 active: true,
408 tags: ["developer", "typescript"],
409 metadata: { created: "2023-01-01", count: 42 },
410 };
411 const attr = await Effect.runPromise(toAttributeValue(original));
412 const result = fromAttributeValue(attr);
413 expect(result).toEqual(original);
414 });
415 },
416 );