@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 267 lines 9.79 KB
Raw
1 import { imageSourceKind, validateImageSource } from "@alchemy.run/aws/ECR/ImageSource";
2 import {
3 buildFinalDockerfile,
4 containerEnvPreamble,
5 validateContainerImageProps,
6 } from "@alchemy.run/cloudflare/Containers/ContainerBundle";
7 import * as Dockerfile from "@alchemy.run/docker/Dockerfile";
8 import { describe, expect, it, test } from "@alchemy.run/test";
9 import * as Output from "alchemy/Output";
10 import * as Cause from "effect/Cause";
11 import * as Effect from "effect/Effect";
12 import * as Exit from "effect/Exit";
13 import * as Redacted from "effect/Redacted";
14 import * as Result from "effect/Result";
15
16 describe(
17 "Dockerfile.inline",
18 { tags: ["unit", "provider:docker", "provider:docker:dockerfile", "local"] },
19 () => {
20 test("no interpolations produce plain string content", () => {
21 const df = Dockerfile.inline`FROM oven/bun:1
22 RUN apt-get install -y ffmpeg`;
23 expect(Dockerfile.isInlineDockerfile(df)).toBe(true);
24 expect(typeof df.content).toBe("string");
25 expect(df.content).toContain("FROM oven/bun:1");
26 expect(df.content).toContain("RUN apt-get install -y ffmpeg");
27 });
28
29 test("content is deterministic across constructions", () => {
30 const a = Dockerfile.inline`FROM oven/bun:1`;
31 const b = Dockerfile.inline`FROM oven/bun:1`;
32 expect(a.content).toBe(b.content);
33 });
34
35 test("interpolations produce Output content (dependency edge)", () => {
36 const df = Dockerfile.inline`FROM ${Output.literal("oven/bun:1")}
37 RUN echo hi`;
38 expect(Dockerfile.isInlineDockerfile(df)).toBe(true);
39 expect(Output.isOutput(df.content)).toBe(true);
40 });
41
42 test("isInlineDockerfile rejects path strings", () => {
43 expect(Dockerfile.isInlineDockerfile("./Dockerfile")).toBe(false);
44 expect(Dockerfile.isInlineDockerfile(undefined)).toBe(false);
45 expect(Dockerfile.isInlineDockerfile(null)).toBe(false);
46 });
47 },
48 );
49
50 describe(
51 "AWS ImageSource composition",
52 {
53 tags: [
54 "unit",
55 "provider:aws",
56 "provider:aws:ecr",
57 "provider:docker",
58 "provider:docker:dockerfile",
59 "local",
60 ],
61 },
62 () => {
63 test("main wins as the source kind; other fields describe its environment", () => {
64 expect(imageSourceKind({ main: "./index.ts" })).toBe("main");
65 expect(imageSourceKind({ main: "./index.ts", image: "oven/bun:1" })).toBe("main");
66 expect(imageSourceKind({ image: "busybox:stable" })).toBe("image");
67 expect(imageSourceKind({ context: "./app" })).toBe("context");
68 expect(imageSourceKind({ dockerfile: Dockerfile.inline`FROM alpine` })).toBe("context");
69 expect(imageSourceKind({})).toBeUndefined();
70 });
71
72 it.effect("validateImageSource dies on exclusivity violations", () =>
73 Effect.gen(function* () {
74 const dies = (source: Parameters<typeof validateImageSource>[1]) =>
75 Effect.gen(function* () {
76 const result = yield* Effect.result(
77 validateImageSource("T", source).pipe(
78 Effect.catchDefect((defect) => Effect.fail(defect as Error)),
79 ),
80 );
81 return Result.isFailure(result);
82 });
83
84 expect(yield* dies({ image: "busybox", dockerfile: "./Dockerfile" })).toBe(true);
85 expect(yield* dies({ image: "busybox", context: "./app" })).toBe(true);
86 expect(yield* dies({ dockerfile: Dockerfile.inline`FROM alpine`, context: "./app" })).toBe(
87 true,
88 );
89 // Valid combinations pass.
90 expect(yield* dies({ main: "./i.ts", image: "oven/bun:1" })).toBe(false);
91 expect(yield* dies({ main: "./i.ts", dockerfile: Dockerfile.inline`FROM x` })).toBe(false);
92 expect(yield* dies({ context: "./app", dockerfile: "./app/Dockerfile" })).toBe(false);
93
94 // Build args and secrets need a Dockerfile build.
95 const args = { NODE_ENV: "production" };
96 const secrets = { NPM_TOKEN: Redacted.make("token") };
97 expect(yield* dies({ context: "./app", build: { args, secrets } })).toBe(false);
98 expect(yield* dies({ dockerfile: Dockerfile.inline`FROM x`, build: { args } })).toBe(false);
99 expect(yield* dies({ main: "./i.ts", build: { args } })).toBe(true);
100 expect(yield* dies({ image: "busybox", build: { secrets } })).toBe(true);
101 // A `main` program's bundler config is not a Docker build.
102 expect(yield* dies({ main: "./i.ts", build: { pure: false } })).toBe(false);
103 }),
104 );
105
106 it.effect("build args and secrets on a main or image source die with a tagged defect", () =>
107 Effect.gen(function* () {
108 const exit = yield* Effect.exit(
109 validateImageSource("Api", { image: "busybox", build: { args: { A: "1" } } }),
110 );
111 const defect = Exit.isFailure(exit) ? Cause.squash(exit.cause) : undefined;
112 expect(defect).toMatchObject({
113 _tag: "DockerfileBuildConflict",
114 id: "Api",
115 source: "image",
116 });
117 }),
118 );
119 },
120 );
121
122 /**
123 * Run an Effect that may die and report whether it did — validation
124 * helpers surface invalid props as defects (`Effect.die`), never typed
125 * errors or raw throws.
126 */
127 const dies = <A>(effect: Effect.Effect<A>) =>
128 Effect.gen(function* () {
129 const result = yield* Effect.result(
130 effect.pipe(Effect.catchDefect((defect) => Effect.fail(defect as Error))),
131 );
132 return Result.isFailure(result);
133 });
134
135 describe(
136 "Cloudflare container environment composition",
137 {
138 tags: [
139 "unit",
140 "provider:cloudflare",
141 "provider:cloudflare:container",
142 "provider:docker",
143 "provider:docker:dockerfile",
144 "local",
145 ],
146 },
147 () => {
148 it.effect("containerEnvPreamble: image ref becomes FROM line", () =>
149 Effect.gen(function* () {
150 expect(yield* containerEnvPreamble({ image: "oven/bun:1" })).toBe("FROM oven/bun:1");
151 expect(yield* containerEnvPreamble({})).toBeUndefined();
152 }),
153 );
154
155 it.effect("containerEnvPreamble: inline content used verbatim", () =>
156 Effect.gen(function* () {
157 const preamble = yield* containerEnvPreamble({
158 dockerfile: Dockerfile.inline`FROM oven/bun:1
159 RUN apt-get install -y ffmpeg`,
160 });
161 expect(preamble).toContain("RUN apt-get install -y ffmpeg");
162 }),
163 );
164
165 it.effect("containerEnvPreamble: dies on Dockerfile content passed as image", () =>
166 Effect.gen(function* () {
167 expect(yield* dies(containerEnvPreamble({ image: "FROM oven/bun:1\nRUN echo hi" }))).toBe(
168 true,
169 );
170 }),
171 );
172
173 test("buildFinalDockerfile layers the bundle on top of the preamble", () => {
174 const dockerfile = buildFinalDockerfile(
175 "FROM oven/bun:1\nRUN apt-get install -y ffmpeg",
176 "bun",
177 );
178 const lines = dockerfile.split("\n");
179 expect(lines[0]).toBe("FROM oven/bun:1");
180 expect(lines[1]).toBe("RUN apt-get install -y ffmpeg");
181 expect(dockerfile).toContain('ENTRYPOINT ["bun", "/app/index.mjs"]');
182 });
183
184 test("buildFinalDockerfile falls back to the runtime default base", () => {
185 expect(buildFinalDockerfile(undefined, "bun").split("\n")[0]).toBe("FROM oven/bun:1");
186 expect(buildFinalDockerfile(undefined, "node").split("\n")[0]).toBe("FROM node:22-slim");
187 });
188
189 it.effect("validateContainerImageProps enforces exclusivity", () =>
190 Effect.gen(function* () {
191 // main + image and main + inline dockerfile are the two environments.
192 expect(
193 yield* dies(validateContainerImageProps("Api", { main: "./i.ts", image: "oven/bun:1" })),
194 ).toBe(false);
195 expect(
196 yield* dies(
197 validateContainerImageProps("Api", {
198 main: "./i.ts",
199 dockerfile: Dockerfile.inline`FROM x`,
200 }),
201 ),
202 ).toBe(false);
203 expect(
204 yield* dies(
205 validateContainerImageProps("Api", {
206 main: "./i.ts",
207 image: "oven/bun:1",
208 dockerfile: Dockerfile.inline`FROM x`,
209 }),
210 ),
211 ).toBe(true);
212 // A path dockerfile cannot be an environment on Cloudflare.
213 expect(
214 yield* dies(
215 validateContainerImageProps("Api", { main: "./i.ts", dockerfile: "./Dockerfile" }),
216 ),
217 ).toBe(true);
218 expect(
219 yield* dies(validateContainerImageProps("Api", { main: "./i.ts", context: "./app" })),
220 ).toBe(true);
221 // Without main: image is exclusive with dockerfile/context.
222 expect(
223 yield* dies(validateContainerImageProps("Api", { image: "busybox", dockerfile: "./f" })),
224 ).toBe(true);
225 expect(
226 yield* dies(validateContainerImageProps("Api", { image: "busybox", context: "./app" })),
227 ).toBe(true);
228 // Inline content has no build context.
229 expect(
230 yield* dies(
231 validateContainerImageProps("Api", {
232 dockerfile: Dockerfile.inline`FROM x`,
233 context: "./app",
234 }),
235 ),
236 ).toBe(true);
237 // The plain external build stays valid.
238 expect(
239 yield* dies(
240 validateContainerImageProps("Api", {
241 context: "./app",
242 dockerfile: "./app/Dockerfile",
243 }),
244 ),
245 ).toBe(false);
246 // Build args and secrets need a Dockerfile build.
247 const secrets = { NPM_TOKEN: Redacted.make("token") };
248 expect(
249 yield* dies(
250 validateContainerImageProps("Api", {
251 context: "./app",
252 build: { args: { A: "1" }, secrets },
253 }),
254 ),
255 ).toBe(false);
256 expect(
257 yield* dies(validateContainerImageProps("Api", { main: "./i.ts", build: { secrets } })),
258 ).toBe(true);
259 expect(
260 yield* dies(
261 validateContainerImageProps("Api", { image: "busybox", build: { args: { A: "1" } } }),
262 ),
263 ).toBe(true);
264 }),
265 );
266 },
267 );