@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 132 lines 4.96 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { describe, expect } from "@alchemy.run/test";
3 import * as Test from "@alchemy.run/test/Alchemy";
4 import * as Core from "@alchemy.run/test/Core";
5 import * as Data from "effect/Data";
6 import * as Effect from "effect/Effect";
7 import * as HttpClient from "effect/http/HttpClient";
8 import * as HttpClientRequest from "effect/http/HttpClientRequest";
9 import * as Schedule from "effect/Schedule";
10 import VectorsTestFunctionLive, { VectorsTestFunction } from "./vectors-handler";
11
12 const testOptions = { providers: AWS.providers() };
13 const { test, beforeAll, afterAll } = Test.make(testOptions);
14 const sharedStack = Core.scratchStack(testOptions, "S3VectorsBindings");
15
16 const readinessPolicy = Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(75)]);
17
18 let baseUrl: string;
19
20 class TransientUpstream extends Data.TaggedError("TransientUpstream")<{
21 readonly status: number;
22 readonly body: string;
23 }> {}
24
25 const send = (request: HttpClientRequest.HttpClientRequest) =>
26 HttpClient.execute(request).pipe(
27 Effect.flatMap((response) =>
28 response.status >= 500
29 ? response.text.pipe(
30 Effect.flatMap((body) =>
31 Effect.fail(new TransientUpstream({ status: response.status, body })),
32 ),
33 )
34 : Effect.succeed(response),
35 ),
36 Effect.retry({
37 while: (e) => e._tag === "TransientUpstream",
38 schedule: Schedule.max([Schedule.exponential("1 second"), Schedule.recurs(5)]),
39 }),
40 );
41
42 describe(
43 "S3Vectors Bindings",
44 { tags: ["provider:aws", "provider:aws:lambda", "provider:aws:s3vectors", "live"] },
45 () => {
46 beforeAll(
47 Effect.gen(function* () {
48 yield* Effect.logInfo("S3Vectors test setup: destroying previous");
49 yield* sharedStack.destroy();
50
51 yield* Effect.logInfo("S3Vectors test setup: deploying fixture");
52 const { functionUrl } = yield* sharedStack.deploy(
53 Effect.gen(function* () {
54 return yield* VectorsTestFunction;
55 }).pipe(Effect.provide(VectorsTestFunctionLive)),
56 );
57
58 expect(functionUrl).toBeTruthy();
59 baseUrl = functionUrl!.replace(/\/+$/, "");
60
61 yield* HttpClient.get(`${baseUrl}/ping`).pipe(
62 Effect.flatMap((response) =>
63 response.status === 200
64 ? Effect.succeed(response)
65 : Effect.fail(new Error(`Function not ready: ${response.status}`)),
66 ),
67 Effect.retry({ schedule: readinessPolicy }),
68 );
69 }),
70 { timeout: 240_000 },
71 );
72
73 afterAll(sharedStack.destroy(), { timeout: 300_000 });
74
75 describe("Vectors", () => {
76 test.provider(
77 "put + query round-trips through the bound index",
78 (_stack) =>
79 Effect.gen(function* () {
80 // insert
81 const putBody = (yield* send(HttpClientRequest.get(`${baseUrl}/put`)).pipe(
82 Effect.flatMap((r) => r.json),
83 )) as { put: number };
84 expect(putBody.put).toBe(3);
85
86 // query nearest to [1,0,0,0] — expect "a" (exact) and "c" (close),
87 // retrying through data-plane eventual consistency.
88 const queryBody = yield* send(HttpClientRequest.get(`${baseUrl}/query`)).pipe(
89 Effect.flatMap((r) => r.json),
90 Effect.map((b) => b as { keys: string[]; distanceMetric: string }),
91 Effect.flatMap((b) =>
92 b.keys.includes("a")
93 ? Effect.succeed(b)
94 : Effect.fail(new Error(`query not ready: ${JSON.stringify(b)}`)),
95 ),
96 Effect.retry({
97 schedule: Schedule.max([Schedule.spaced("3 seconds"), Schedule.recurs(10)]),
98 }),
99 );
100 expect(queryBody.distanceMetric).toBe("cosine");
101 expect(queryBody.keys).toContain("a");
102 expect(queryBody.keys.length).toBe(2);
103
104 // get by key (read-only client)
105 const getBody = (yield* send(HttpClientRequest.get(`${baseUrl}/get`)).pipe(
106 Effect.flatMap((r) => r.json),
107 )) as { keys: string[] };
108 expect(getBody.keys).toContain("a");
109
110 // list keys (read-only client)
111 const listBody = (yield* send(HttpClientRequest.get(`${baseUrl}/list`)).pipe(
112 Effect.flatMap((r) => r.json),
113 )) as { keys: string[] };
114 expect(listBody.keys).toContain("a");
115
116 // delete a key (write-only client)
117 const delBody = (yield* send(HttpClientRequest.get(`${baseUrl}/delete`)).pipe(
118 Effect.flatMap((r) => r.json),
119 )) as { deleted: string };
120 expect(delBody.deleted).toBe("b");
121
122 // put + get through the composed ReadWrite client
123 const rwBody = (yield* send(HttpClientRequest.get(`${baseUrl}/rw`)).pipe(
124 Effect.flatMap((r) => r.json),
125 )) as { keys: string[] };
126 expect(rwBody.keys).toContain("rw");
127 }),
128 { timeout: 120_000 },
129 );
130 });
131 },
132 );