@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 222 lines 10 KB
Raw
1 /**
2 * The source fragments the stress suite splices into the fixture's
3 * `<<REGION>>` markers.
4 *
5 * These are the substance of the suite: each one is a real change to the
6 * RESOURCE GRAPH — a new Lambda with its own bucket, a Queue plus its
7 * consumer event source, a new Durable Object class (and therefore a class
8 * migration) grafted onto a running Worker, a replacement-forcing schema
9 * change, a second MicroVM image. Keeping them here as named constants
10 * keeps the test file about *what is asserted* rather than about TypeScript
11 * embedded in string literals.
12 *
13 * Indentation matters: `patchRegion` splices these between markers that sit
14 * at a fixed depth in the fixture, so every fragment is written at the
15 * depth its region lives at.
16 */
17
18 // ─── An entire extra Cloudflare Worker (add / rename / remove) ──────────
19
20 export const extraWorkerSource = (marker: string) =>
21 `export default {\n` +
22 ` fetch: async () => Response.json({ marker: ${JSON.stringify(marker)} }),\n` +
23 `};\n`;
24
25 export const extraWorkerDeclaration = (logicalId: string, port: "extra" | "extraAlt") =>
26 ` const extraWorker = yield* Cloudflare.Worker(${JSON.stringify(logicalId)}, {\n` +
27 ` main: "./src/extra/extra-worker.ts",\n` +
28 ` dev: { port: PORTS.${port}, strictPort: true },\n` +
29 ` });\n`;
30
31 export const extraWorkerOutput = " extraUrl: extraWorker.url,\n";
32
33 // ─── A Worker that demands a port another Worker already holds ─────────
34 // The cleanest deterministic APPLY failure available locally: the stack
35 // imports and plans fine and exactly one resource cannot reconcile.
36
37 export const portSquatterSource = `export default { fetch: async () => new Response("squatter") };\n`;
38
39 export const portSquatterDeclaration =
40 ` yield* Cloudflare.Worker("PortSquatter", {\n` +
41 ` main: "./src/extra/squatter.ts",\n` +
42 ` dev: { port: PORTS.echo, strictPort: true },\n` +
43 ` });\n`;
44
45 // ─── An entire extra AWS Lambda, with its own S3 bucket ────────────────
46
47 export const reportFunctionSource = (marker: string) =>
48 `import * as Lambda from "@alchemy.run/aws/Lambda";\n` +
49 `import * as S3 from "@alchemy.run/aws/S3";\n` +
50 `import * as Effect from "effect/Effect";\n` +
51 `import * as Layer from "effect/Layer";\n` +
52 `import * as Stream from "effect/Stream";\n` +
53 `import { HttpServerRequest } from "effect/http/HttpServerRequest";\n` +
54 `import * as HttpServerResponse from "effect/http/HttpServerResponse";\n` +
55 `\n` +
56 `/** Added to (and later removed from) the stack while dev is running. */\n` +
57 `export default class ReportFunction extends Lambda.Function<ReportFunction>()(\n` +
58 ` "ReportFunction",\n` +
59 ` { main: import.meta.url, functionUrl: true, memorySize: 256 },\n` +
60 ` Effect.gen(function* () {\n` +
61 ` const bucket = yield* S3.Bucket("ReportBucket", { forceDestroy: true });\n` +
62 ` const putObject = yield* S3.PutObject(bucket);\n` +
63 ` const getObject = yield* S3.GetObject(bucket);\n` +
64 ` return {\n` +
65 ` fetch: Effect.gen(function* () {\n` +
66 ` const request = yield* HttpServerRequest;\n` +
67 ` const url = new URL(request.originalUrl);\n` +
68 ` if (url.pathname === "/report") {\n` +
69 ` yield* putObject({ Key: "report.txt", Body: ${JSON.stringify(marker)} });\n` +
70 ` const object = yield* getObject({ Key: "report.txt" });\n` +
71 ` const text = yield* (\n` +
72 ` object.Body?.pipe(Stream.decodeText, Stream.mkString) ??\n` +
73 ` Effect.succeed("")\n` +
74 ` );\n` +
75 ` return yield* HttpServerResponse.json({ marker: ${JSON.stringify(marker)}, text });\n` +
76 ` }\n` +
77 ` return HttpServerResponse.text("not found", { status: 404 });\n` +
78 ` }).pipe(Effect.orDie),\n` +
79 ` };\n` +
80 ` }).pipe(Effect.provide(Layer.mergeAll(S3.GetObjectHttp, S3.PutObjectHttp))),\n` +
81 `) {}\n`;
82
83 export const reportFunctionImport = `import ReportFunction from "./src/extra/ReportFunction.ts";\n`;
84
85 export const reportFunctionDeclaration = ` const reportFunction = yield* ReportFunction;\n`;
86
87 export const reportFunctionOutput = " reportUrl: reportFunction.functionUrl,\n";
88
89 // ─── A Queue + its consumer + a new Durable Object class, grafted onto
90 // the already-running EchoWorker. This adds a resource, an event source,
91 // AND a class migration to a live Worker. ──────────────────────────────
92
93 export const echoQueueBindings =
94 ` const echoQueue = yield* Cloudflare.Queues.Queue("EchoQueue");\n` +
95 ` const echoQueueSend = yield* Cloudflare.Queues.WriteQueue(echoQueue);\n` +
96 ` const inbox = yield* Inbox;\n` +
97 ` yield* Cloudflare.Queues.consumeQueueMessages<{ id: string }>(\n` +
98 ` echoQueue,\n` +
99 ` (stream) =>\n` +
100 ` Stream.runForEach(stream, (message) =>\n` +
101 ` inbox.getByName("global").record(message.body.id),\n` +
102 ` ),\n` +
103 ` );\n`;
104
105 export const echoQueueRoutes =
106 ` if (url.pathname === "/queue/send") {\n` +
107 ` const id = url.searchParams.get("id") ?? "none";\n` +
108 ` yield* echoQueueSend.send({ id }).pipe(Effect.orDie);\n` +
109 ` return yield* HttpServerResponse.json({ sent: id });\n` +
110 ` }\n` +
111 ` if (url.pathname === "/queue/received") {\n` +
112 ` const ids = yield* inbox.getByName("global").list();\n` +
113 ` return yield* HttpServerResponse.json({ ids });\n` +
114 ` }\n`;
115
116 export const echoQueueLayers =
117 ` Cloudflare.Queues.WriteQueueBinding,\n` + ` Cloudflare.Queues.EventSourceLive,\n`;
118
119 export const echoInboxClass =
120 `/** Durable Object added to a LIVE worker — this carries a class migration. */\n` +
121 `export class Inbox extends Cloudflare.DurableObject<Inbox>()(\n` +
122 ` "Inbox",\n` +
123 ` Effect.succeed(\n` +
124 ` Effect.gen(function* () {\n` +
125 ` const state = yield* Cloudflare.DurableObjectState;\n` +
126 ` return {\n` +
127 ` record: Effect.fn(function* (id: string) {\n` +
128 ` const ids = (yield* state.storage.get<string[]>("ids")) ?? [];\n` +
129 ` yield* state.storage.put("ids", [...ids, id]);\n` +
130 ` }),\n` +
131 ` list: Effect.fn(function* () {\n` +
132 ` return (yield* state.storage.get<string[]>("ids")) ?? [];\n` +
133 ` }),\n` +
134 ` };\n` +
135 ` }),\n` +
136 ` ),\n` +
137 `) {}\n`;
138
139 /** `Stream` is only imported once the queue consumer needs it. */
140 export const echoStreamImport = `import * as Stream from "effect/Stream";\n`;
141
142 // ─── A second S3 bucket + its bindings on the already-running Lambda ────
143
144 export const lambdaArchiveBindings =
145 ` const archive = yield* S3.Bucket("ArchiveBucket", { forceDestroy: true });\n` +
146 ` const putArchive = yield* S3.PutObject(archive);\n` +
147 ` const getArchive = yield* S3.GetObject(archive);\n`;
148
149 export const lambdaArchiveRoutes =
150 ` if (url.pathname === "/archive") {\n` +
151 ` yield* putArchive({ Key: "archive.txt", Body: "archived" });\n` +
152 ` const object = yield* getArchive({ Key: "archive.txt" });\n` +
153 ` const text = yield* (\n` +
154 ` object.Body?.pipe(Stream.decodeText, Stream.mkString) ??\n` +
155 ` Effect.succeed("")\n` +
156 ` );\n` +
157 ` return yield* HttpServerResponse.json({ text });\n` +
158 ` }\n`;
159
160 // ─── A second MicroVM image, and the binding set to drive it ───────────
161
162 export const secondImageSource = (marker: string) =>
163 `import * as AWS from "@alchemy.run/aws";\n` +
164 `import * as Effect from "effect/Effect";\n` +
165 `import { HttpServerRequest } from "effect/http/HttpServerRequest";\n` +
166 `import * as HttpServerResponse from "effect/http/HttpServerResponse";\n` +
167 `\n` +
168 `export const WorkerImageBuildRole = AWS.IAM.Role("StressWorkerVmBuildRole");\n` +
169 `\n` +
170 `/** A SECOND MicroVM image, added to the stack while dev is running. */\n` +
171 `export class WorkerMicrovm extends AWS.Lambda.MicrovmImage<\n` +
172 ` WorkerMicrovm,\n` +
173 ` { ping: () => Effect.Effect<string> }\n` +
174 `>()("StressWorkerVm") {}\n` +
175 `\n` +
176 `export default WorkerMicrovm.make(\n` +
177 ` WorkerImageBuildRole.pipe(\n` +
178 ` Effect.map((buildRole) => ({\n` +
179 ` main: import.meta.filename,\n` +
180 ` buildRole,\n` +
181 ` runtime: "bun" as const,\n` +
182 ` resources: [{ minimumMemoryInMiB: 512 }],\n` +
183 ` cpuConfigurations: [{ architecture: "ARM_64" as const }],\n` +
184 ` })),\n` +
185 ` ),\n` +
186 ` Effect.gen(function* () {\n` +
187 ` return {\n` +
188 ` fetch: Effect.gen(function* () {\n` +
189 ` yield* HttpServerRequest;\n` +
190 ` return HttpServerResponse.text(${JSON.stringify(marker)});\n` +
191 ` }),\n` +
192 ` ping: () => Effect.succeed(${JSON.stringify(marker)}),\n` +
193 ` };\n` +
194 ` }),\n` +
195 `);\n`;
196
197 export const secondImageImport = `import WorkerImageLive from "./src/extra/WorkerImage.ts";\n`;
198
199 /** Spliced into the stack's `Layer.mergeAll`, so it ends with a comma. */
200 export const secondImageLayer = ` WorkerImageLive,\n`;
201
202 /** Bindings + a route on `MicrovmWorker` that drive the SECOND image. */
203 export const secondImageWorkerImport = `import { WorkerMicrovm } from "./extra/WorkerImage.ts";\n`;
204
205 export const secondImageBindings =
206 ` const runWorkerVm = yield* AWS.Lambda.RunMicrovm(WorkerMicrovm);\n` +
207 ` const terminateWorkerVm = yield* AWS.Lambda.TerminateMicrovm(WorkerMicrovm);\n`;
208
209 export const secondImageRoutes =
210 ` if (url.pathname === "/second") {\n` +
211 ` const vm = yield* runWorkerVm({\n` +
212 ` idlePolicy: {\n` +
213 ` maxIdleDurationSeconds: 900,\n` +
214 ` suspendedDurationSeconds: 300,\n` +
215 ` autoResumeEnabled: true,\n` +
216 ` },\n` +
217 ` });\n` +
218 ` yield* terminateWorkerVm({ microvmIdentifier: vm.microvmId }).pipe(\n` +
219 ` Effect.ignore,\n` +
220 ` );\n` +
221 ` return yield* HttpServerResponse.json({ microvmId: vm.microvmId });\n` +
222 ` }\n`;