@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, 04:45 UTC

main
ts 154 lines 5.97 KB
Raw
1 import * as Lambda from "@alchemy.run/aws/Lambda";
2 import * as MailManager from "@alchemy.run/aws/MailManager";
3 import * as Duration from "effect/Duration";
4 import * as Effect from "effect/Effect";
5 import { HttpServerRequest } from "effect/http/HttpServerRequest";
6 import * as HttpServerResponse from "effect/http/HttpServerResponse";
7 import * as Layer from "effect/Layer";
8 import path from "pathe";
9
10 const main = path.resolve(import.meta.dirname, "handler.ts");
11
12 export class MailManagerTestFunction extends Lambda.Function<Lambda.Function>()(
13 "MailManagerTestFunction",
14 ) {}
15
16 export default MailManagerTestFunction.make(
17 {
18 main,
19 functionUrl: true,
20 timeout: Duration.seconds(30),
21 },
22 Effect.gen(function* () {
23 // The address list the member-scoped bindings are bound to.
24 const blockList = yield* MailManager.AddressList("BindingList", {
25 tags: { fixture: "mailmanager-bindings" },
26 });
27 // The archive the search-scoped bindings are bound to. Nothing routes
28 // mail into it, so searches complete with zero rows.
29 const archive = yield* MailManager.Archive("BindingArchive", {
30 retentionPeriod: "THREE_MONTHS",
31 tags: { fixture: "mailmanager-bindings" },
32 });
33
34 const registerMember = yield* MailManager.RegisterMemberToAddressList(blockList);
35 const deregisterMember = yield* MailManager.DeregisterMemberFromAddressList(blockList);
36 const getMember = yield* MailManager.GetMemberOfAddressList(blockList);
37 const listMembers = yield* MailManager.ListMembersOfAddressList(blockList);
38 const listImportJobs = yield* MailManager.ListAddressListImportJobs(blockList);
39 const startSearch = yield* MailManager.StartArchiveSearch(archive);
40 const getSearch = yield* MailManager.GetArchiveSearch(archive);
41 const getSearchResults = yield* MailManager.GetArchiveSearchResults(archive);
42 const listSearches = yield* MailManager.ListArchiveSearches(archive);
43 const listExports = yield* MailManager.ListArchiveExports(archive);
44
45 const bound = {
46 registerMember,
47 deregisterMember,
48 getMember,
49 listMembers,
50 listImportJobs,
51 startSearch,
52 getSearch,
53 getSearchResults,
54 listSearches,
55 listExports,
56 };
57
58 return {
59 fetch: Effect.gen(function* () {
60 const request = yield* HttpServerRequest;
61 const url = new URL(request.originalUrl);
62 const pathname = url.pathname;
63
64 if (request.method === "GET" && pathname === "/bindings") {
65 return yield* HttpServerResponse.json({
66 bound: Object.keys(bound),
67 });
68 }
69
70 // Full member roundtrip: register -> get -> list -> deregister ->
71 // get (typed not-found). Addresses are Sensitive/Redacted end-to-end
72 // so the route reports booleans and counts, never the values.
73 if (request.method === "POST" && pathname === "/members/roundtrip") {
74 const address = "blocked@example.com";
75 yield* registerMember({ Address: address });
76 const member = yield* getMember({ Address: address });
77 const { Addresses } = yield* listMembers({});
78 yield* deregisterMember({ Address: address });
79 const goneAfterDeregister = yield* getMember({
80 Address: address,
81 }).pipe(
82 Effect.map(() => false),
83 Effect.catchTag("ResourceNotFoundException", () => Effect.succeed(true)),
84 );
85 return yield* HttpServerResponse.json({
86 registered: member.CreatedTimestamp !== undefined,
87 memberCount: Addresses.length,
88 goneAfterDeregister,
89 });
90 }
91
92 // Kick off an archive search over the last hour (the archive is
93 // empty; the search completes with zero rows).
94 if (request.method === "POST" && pathname === "/search/start") {
95 const { SearchId } = yield* startSearch({
96 FromTimestamp: new Date(Date.now() - 3_600_000),
97 ToTimestamp: new Date(),
98 MaxResults: 5,
99 });
100 return yield* HttpServerResponse.json({ searchId: SearchId });
101 }
102
103 if (request.method === "GET" && pathname === "/search/status") {
104 const searchId = url.searchParams.get("searchId") ?? "";
105 const search = yield* getSearch({ SearchId: searchId });
106 return yield* HttpServerResponse.json({
107 state: search.Status?.State,
108 errorMessage: search.Status?.ErrorMessage,
109 });
110 }
111
112 if (request.method === "GET" && pathname === "/search/results") {
113 const searchId = url.searchParams.get("searchId") ?? "";
114 const { Rows } = yield* getSearchResults({ SearchId: searchId });
115 return yield* HttpServerResponse.json({
116 rowCount: (Rows ?? []).length,
117 });
118 }
119
120 // Cheap enumeration reads proving the remaining grants.
121 if (request.method === "GET" && pathname === "/tasks") {
122 const { Searches } = yield* listSearches({});
123 const { Exports } = yield* listExports({});
124 const { ImportJobs } = yield* listImportJobs({});
125 return yield* HttpServerResponse.json({
126 searchCount: (Searches ?? []).length,
127 exportCount: (Exports ?? []).length,
128 importJobCount: ImportJobs.length,
129 });
130 }
131
132 return yield* HttpServerResponse.json(
133 { error: "Not found", method: request.method, pathname },
134 { status: 404 },
135 );
136 }).pipe(Effect.orDie),
137 };
138 }).pipe(
139 Effect.provide(
140 Layer.mergeAll(
141 MailManager.RegisterMemberToAddressListHttp,
142 MailManager.DeregisterMemberFromAddressListHttp,
143 MailManager.GetMemberOfAddressListHttp,
144 MailManager.ListMembersOfAddressListHttp,
145 MailManager.ListAddressListImportJobsHttp,
146 MailManager.StartArchiveSearchHttp,
147 MailManager.GetArchiveSearchHttp,
148 MailManager.GetArchiveSearchResultsHttp,
149 MailManager.ListArchiveSearchesHttp,
150 MailManager.ListArchiveExportsHttp,
151 ),
152 ),
153 ),
154 );