The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { make as makeNext } from "@alchemy.run/frontend-frameworks/nextjs/node"; |
| 2 | import { make as makeVinext } from "@alchemy.run/frontend-frameworks/vinext/node"; |
| 3 | import { make as makeVite } from "@alchemy.run/frontend-frameworks/vite"; |
| 4 | import { createComputeArchive } from "@alchemy.run/prisma/ComputeArchive"; |
| 5 | import { stageWebsiteArtifact } from "@alchemy.run/prisma/Website/Artifact"; |
| 6 | import { describe, expect, it } from "@alchemy.run/test"; |
| 7 | import { findAvailablePort } from "alchemy/Util/Node"; |
| 8 | import { PlatformServices } from "alchemy/Util/PlatformServices"; |
| 9 | import * as Effect from "effect/Effect"; |
| 10 | import * as FileSystem from "effect/FileSystem"; |
| 11 | import * as FetchHttpClient from "effect/http/FetchHttpClient"; |
| 12 | import * as HttpClient from "effect/http/HttpClient"; |
| 13 | import * as Layer from "effect/Layer"; |
| 14 | import * as Path from "effect/Path"; |
| 15 | import * as ChildProcess from "effect/process/ChildProcess"; |
| 16 | import { ChildProcessSpawner } from "effect/process/ChildProcessSpawner"; |
| 17 | import * as Schedule from "effect/Schedule"; |
| 18 | |
| 19 | const services = Layer.mergeAll(PlatformServices, FetchHttpClient.layer); |
| 20 | |
| 21 | const fixture = Effect.fn(function* (name: "vite" | "next") { |
| 22 | const fs = yield* FileSystem.FileSystem; |
| 23 | const path = yield* Path.Path; |
| 24 | const fixtures = yield* path.fromFileUrl( |
| 25 | new URL("./fixtures/website-artifact/", import.meta.url), |
| 26 | ); |
| 27 | const root = yield* Effect.acquireRelease( |
| 28 | fs.makeTempDirectory({ directory: fixtures, prefix: "build-" }), |
| 29 | (directory) => fs.remove(directory, { recursive: true, force: true }).pipe(Effect.orDie), |
| 30 | ); |
| 31 | yield* fs.copy(path.join(fixtures, name), root); |
| 32 | yield* fs.writeFileString(path.join(root, ".env.private"), "MUST_NOT_SHIP=secret"); |
| 33 | yield* fs.writeFileString( |
| 34 | path.join(root, "unrelated-private-file.txt"), |
| 35 | "not a runtime dependency", |
| 36 | ); |
| 37 | return root; |
| 38 | }); |
| 39 | |
| 40 | const extract = Effect.fn(function* (archive: Uint8Array) { |
| 41 | const fs = yield* FileSystem.FileSystem; |
| 42 | const path = yield* Path.Path; |
| 43 | const root = yield* fs.makeTempDirectoryScoped({ prefix: "prisma-website-extracted-" }); |
| 44 | const archivePath = path.join(root, "artifact.tar.gz"); |
| 45 | yield* fs.writeFile(archivePath, archive); |
| 46 | const spawner = yield* ChildProcessSpawner; |
| 47 | const child = yield* spawner.spawn( |
| 48 | ChildProcess.make("tar", ["-xzf", archivePath, "-C", root], { |
| 49 | stdin: "ignore", |
| 50 | stdout: "ignore", |
| 51 | stderr: "inherit", |
| 52 | }), |
| 53 | ); |
| 54 | expect(yield* child.exitCode).toBe(0); |
| 55 | const manifest = yield* fs.readFileString(path.join(root, "compute.manifest.json")); |
| 56 | expect(JSON.parse(manifest).entrypoint).toBe("bundle/server.mjs"); |
| 57 | return path.join(root, "bundle"); |
| 58 | }); |
| 59 | |
| 60 | const serve = Effect.fn(function* (directory: string, runtimeOverride?: string) { |
| 61 | const port = yield* findAvailablePort(); |
| 62 | const runtime = runtimeOverride ?? (yield* Effect.sync(() => process.execPath)); |
| 63 | const spawner = yield* ChildProcessSpawner; |
| 64 | yield* spawner.spawn( |
| 65 | ChildProcess.make(runtime, ["server.mjs"], { |
| 66 | cwd: directory, |
| 67 | env: { PORT: String(port), HOST: "127.0.0.1", NODE_ENV: "production" }, |
| 68 | stdin: "ignore", |
| 69 | stdout: "ignore", |
| 70 | stderr: "inherit", |
| 71 | killSignal: "SIGKILL", |
| 72 | }), |
| 73 | ); |
| 74 | const url = `http://127.0.0.1:${port}`; |
| 75 | yield* HttpClient.get(`${url}/health`).pipe( |
| 76 | Effect.flatMap((response) => |
| 77 | response.status === 200 ? Effect.void : Effect.fail(new Error(`HTTP ${response.status}`)), |
| 78 | ), |
| 79 | Effect.timeout("1 second"), |
| 80 | Effect.retry({ schedule: Schedule.spaced("250 millis"), times: 10 }), |
| 81 | ); |
| 82 | return url; |
| 83 | }); |
| 84 | |
| 85 | const listFiles = Effect.fn(function* (root: string) { |
| 86 | const fs = yield* FileSystem.FileSystem; |
| 87 | return yield* fs.readDirectory(root, { recursive: true }); |
| 88 | }); |
| 89 | |
| 90 | describe.sequential( |
| 91 | "Prisma Website artifacts", |
| 92 | { tags: ["provider:prisma", "provider:prisma:website", "local"] }, |
| 93 | () => { |
| 94 | it.effect( |
| 95 | "ignores dangling optional-package resolution links while retaining the runtime fallback", |
| 96 | () => |
| 97 | Effect.scoped( |
| 98 | Effect.gen(function* () { |
| 99 | const fs = yield* FileSystem.FileSystem; |
| 100 | const path = yield* Path.Path; |
| 101 | const root = yield* fixture("vite"); |
| 102 | const dist = path.join(root, "dist"); |
| 103 | const modules = path.join(root, "node_modules"); |
| 104 | yield* fs.makeDirectory(dist); |
| 105 | yield* fs.makeDirectory(modules); |
| 106 | yield* fs.symlink( |
| 107 | path.join(root, "missing-optional-runtime"), |
| 108 | path.join(modules, "optional-runtime"), |
| 109 | ); |
| 110 | const entry = path.join(dist, "server.cjs"); |
| 111 | yield* fs.writeFileString( |
| 112 | entry, |
| 113 | 'try { module.exports = require("optional-runtime"); } catch { module.exports = require("./fallback.cjs"); }', |
| 114 | ); |
| 115 | yield* fs.writeFileString( |
| 116 | path.join(dist, "fallback.cjs"), |
| 117 | 'module.exports = "fallback";', |
| 118 | ); |
| 119 | const staged = yield* stageWebsiteArtifact({ root, distDir: dist, serverEntry: entry }); |
| 120 | const files = yield* listFiles(staged.directory); |
| 121 | expect(files.some((file) => file.endsWith("fallback.cjs"))).toBe(true); |
| 122 | expect(files.some((file) => file.includes("optional-runtime"))).toBe(false); |
| 123 | }), |
| 124 | ).pipe(Effect.provide(services)), |
| 125 | { tags: ["unit"] }, |
| 126 | ); |
| 127 | |
| 128 | it.effect( |
| 129 | "rejects a dangling symlink in the build output", |
| 130 | () => |
| 131 | Effect.scoped( |
| 132 | Effect.gen(function* () { |
| 133 | const fs = yield* FileSystem.FileSystem; |
| 134 | const path = yield* Path.Path; |
| 135 | const root = yield* fixture("vite"); |
| 136 | const dist = path.join(root, "dist"); |
| 137 | yield* fs.makeDirectory(dist); |
| 138 | yield* fs.symlink( |
| 139 | path.join(root, "missing-build-asset"), |
| 140 | path.join(dist, "required-asset"), |
| 141 | ); |
| 142 | const error = yield* stageWebsiteArtifact({ root, distDir: dist, static: {} }).pipe( |
| 143 | Effect.flip, |
| 144 | ); |
| 145 | expect(String(error)).toContain("required-asset"); |
| 146 | }), |
| 147 | ).pipe(Effect.provide(services)), |
| 148 | { tags: ["unit"] }, |
| 149 | ); |
| 150 | |
| 151 | it.effect( |
| 152 | "rejects output symlinks that would package the source tree", |
| 153 | () => |
| 154 | Effect.scoped( |
| 155 | Effect.gen(function* () { |
| 156 | const fs = yield* FileSystem.FileSystem; |
| 157 | const path = yield* Path.Path; |
| 158 | const root = yield* fixture("vite"); |
| 159 | const dist = path.join(root, "dist"); |
| 160 | yield* fs.makeDirectory(dist); |
| 161 | yield* fs.symlink(root, path.join(dist, "source")); |
| 162 | const error = yield* stageWebsiteArtifact({ root, distDir: dist, static: {} }).pipe( |
| 163 | Effect.flip, |
| 164 | ); |
| 165 | expect(String(error)).toContain("symlink escapes its selected directory"); |
| 166 | }), |
| 167 | ).pipe(Effect.provide(services)), |
| 168 | { tags: ["unit"] }, |
| 169 | ); |
| 170 | |
| 171 | it.live( |
| 172 | "builds Vite and serves the extracted artifact without the source tree", |
| 173 | () => |
| 174 | Effect.scoped( |
| 175 | Effect.gen(function* () { |
| 176 | const fs = yield* FileSystem.FileSystem; |
| 177 | const path = yield* Path.Path; |
| 178 | const root = yield* fixture("vite"); |
| 179 | const framework = yield* makeVite({ |
| 180 | root, |
| 181 | target: "@alchemy.run/frontend-frameworks/vite/node", |
| 182 | }); |
| 183 | const built = yield* framework.build({ root }); |
| 184 | const dist = built.distDirectory!; |
| 185 | yield* fs.writeFileString(path.join(dist, ".env"), "BUILD_SECRET=hidden"); |
| 186 | const staged = yield* stageWebsiteArtifact({ |
| 187 | root, |
| 188 | distDir: dist, |
| 189 | serverEntry: path.join(dist, built.serverModules![0]!.name), |
| 190 | }); |
| 191 | const archive = yield* createComputeArchive(staged); |
| 192 | const extracted = yield* extract(archive); |
| 193 | const files = yield* listFiles(extracted); |
| 194 | expect(files.some((file) => file.endsWith(".env"))).toBe(false); |
| 195 | expect(files.some((file) => file.includes("unrelated-private-file"))).toBe(false); |
| 196 | yield* fs.remove(root, { recursive: true }); |
| 197 | const url = yield* serve(extracted); |
| 198 | const html = yield* HttpClient.get(url).pipe( |
| 199 | Effect.flatMap((response) => response.text), |
| 200 | ); |
| 201 | expect(html).toContain("Website artifact Vite build"); |
| 202 | const asset = html.match(/src="([^"]+\.js)"/)?.[1]; |
| 203 | expect(asset).toBeDefined(); |
| 204 | expect( |
| 205 | yield* HttpClient.get(`${url}${asset}`).pipe( |
| 206 | Effect.flatMap((response) => response.text), |
| 207 | ), |
| 208 | ).toContain("built-by-real-vite"); |
| 209 | expect((yield* HttpClient.get(`${url}/deep/link`)).status).toBe(200); |
| 210 | }), |
| 211 | ).pipe(Effect.provide(services)), |
| 212 | { tags: ["provider:prisma:compute"], timeout: 90_000 }, |
| 213 | ); |
| 214 | |
| 215 | it.live( |
| 216 | "packages static output without copying source or installed dependencies", |
| 217 | () => |
| 218 | Effect.scoped( |
| 219 | Effect.gen(function* () { |
| 220 | const fs = yield* FileSystem.FileSystem; |
| 221 | const path = yield* Path.Path; |
| 222 | const root = yield* fixture("vite"); |
| 223 | const framework = yield* makeVite({ |
| 224 | root, |
| 225 | target: "@alchemy.run/frontend-frameworks/vite/node", |
| 226 | }); |
| 227 | const built = yield* framework.build({ root }); |
| 228 | yield* fs.writeFileString( |
| 229 | path.join(built.distDirectory!, "missing.html"), |
| 230 | "Artifact not found", |
| 231 | ); |
| 232 | const staged = yield* stageWebsiteArtifact({ |
| 233 | root, |
| 234 | distDir: built.distDirectory!, |
| 235 | static: { notFoundHandling: "404-page", errorPage: "missing.html" }, |
| 236 | }); |
| 237 | const extracted = yield* extract(yield* createComputeArchive(staged)); |
| 238 | expect( |
| 239 | (yield* listFiles(extracted)).some((file) => file.includes("node_modules")), |
| 240 | ).toBe(false); |
| 241 | yield* fs.remove(root, { recursive: true }); |
| 242 | const url = yield* serve(extracted); |
| 243 | const missing = yield* HttpClient.get(`${url}/missing`); |
| 244 | expect(missing.status).toBe(404); |
| 245 | expect(yield* missing.text).toBe("Artifact not found"); |
| 246 | }), |
| 247 | ).pipe(Effect.provide(services)), |
| 248 | { tags: ["provider:prisma:compute"], timeout: 90_000 }, |
| 249 | ); |
| 250 | |
| 251 | it.live( |
| 252 | "builds vinext and preserves vendored package paths in the extracted artifact", |
| 253 | () => |
| 254 | Effect.scoped( |
| 255 | Effect.gen(function* () { |
| 256 | const path = yield* Path.Path; |
| 257 | const root = yield* path.fromFileUrl( |
| 258 | new URL("../../../../examples/prisma-website-vinext/", import.meta.url), |
| 259 | ); |
| 260 | const framework = yield* makeVinext({ root }); |
| 261 | const built = yield* framework.build({ root }); |
| 262 | const staged = yield* stageWebsiteArtifact({ |
| 263 | root, |
| 264 | distDir: built.distDirectory, |
| 265 | serverEntry: path.join(built.distDirectory, built.serverModules[0]!.name), |
| 266 | }); |
| 267 | const extracted = yield* extract(yield* createComputeArchive(staged)); |
| 268 | const files = yield* listFiles(extracted); |
| 269 | expect( |
| 270 | files.some( |
| 271 | (file) => |
| 272 | file.includes("/deps/.pnpm/pathslash@") && file.endsWith("/dist/index.js"), |
| 273 | ), |
| 274 | ).toBe(true); |
| 275 | const url = yield* serve(extracted); |
| 276 | const home = yield* HttpClient.get(url); |
| 277 | expect(home.status).toBe(200); |
| 278 | expect(yield* home.text).toContain("Hello from vinext on Prisma!"); |
| 279 | const asset = yield* HttpClient.get(`${url}/example.json`); |
| 280 | expect(yield* asset.json).toEqual({ |
| 281 | framework: "vinext", |
| 282 | greeting: "Hello from vinext on Prisma!", |
| 283 | }); |
| 284 | const api = yield* HttpClient.get(`${url}/api/hello?name=artifact`); |
| 285 | expect(yield* api.json).toEqual({ name: "artifact", greeting: "hello" }); |
| 286 | }), |
| 287 | ).pipe(Effect.provide(services)), |
| 288 | { tags: ["provider:prisma:compute"], timeout: 120_000 }, |
| 289 | ); |
| 290 | |
| 291 | it.live( |
| 292 | "builds Next.js and traces config, runtime packages, SSR, and public assets", |
| 293 | () => |
| 294 | Effect.scoped( |
| 295 | Effect.gen(function* () { |
| 296 | const fs = yield* FileSystem.FileSystem; |
| 297 | const path = yield* Path.Path; |
| 298 | const root = yield* fixture("next"); |
| 299 | const framework = yield* makeNext({ root }); |
| 300 | const built = yield* framework.build({ root }); |
| 301 | const staged = yield* stageWebsiteArtifact({ |
| 302 | root, |
| 303 | distDir: built.distDirectory!, |
| 304 | serverEntry: path.join(root, built.serverModules![0]!.name), |
| 305 | layout: "next", |
| 306 | }); |
| 307 | const extracted = yield* extract(yield* createComputeArchive(staged)); |
| 308 | const files = yield* listFiles(extracted); |
| 309 | expect(files.some((file) => file.endsWith("site-config.mjs"))).toBe(true); |
| 310 | expect(files.some((file) => file.endsWith(".next/BUILD_ID"))).toBe(true); |
| 311 | expect(files.some((file) => file.includes(".next/cache/"))).toBe(false); |
| 312 | expect(files.some((file) => file.includes("unrelated-private-file"))).toBe(false); |
| 313 | expect(files.some((file) => file.endsWith(".env.private"))).toBe(false); |
| 314 | yield* fs.remove(root, { recursive: true }); |
| 315 | // Prisma Compute runs Bun 1.4. Bun 1.3, which this repo pins for the |
| 316 | // test runner, can't load Next 16.4's turbopack server runtime |
| 317 | // ("Expected CommonJS module to have a function wrapper"). |
| 318 | const bunVersion = yield* Effect.sync(() => process.versions.bun ?? ""); |
| 319 | const [major = 0, minor = 0] = bunVersion.split(".").map(Number); |
| 320 | const node = yield* Effect.sync(() => Bun.which("node") ?? "node"); |
| 321 | const url = yield* serve(extracted, major > 1 || minor >= 4 ? undefined : node); |
| 322 | expect( |
| 323 | yield* HttpClient.get(url).pipe(Effect.flatMap((response) => response.text)), |
| 324 | ).toContain("Next artifact config dependency"); |
| 325 | expect( |
| 326 | yield* HttpClient.get(`${url}/artifact.txt`).pipe( |
| 327 | Effect.flatMap((response) => response.text), |
| 328 | ), |
| 329 | ).toBe("Next public artifact\n"); |
| 330 | }), |
| 331 | ).pipe(Effect.provide(services)), |
| 332 | { tags: ["provider:prisma:compute"], timeout: 120_000 }, |
| 333 | ); |
| 334 | }, |
| 335 | ); |