The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { expect, layer } from "@alchemy.run/test"; |
| 2 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as FileSystem from "effect/FileSystem"; |
| 5 | import * as Layer from "effect/Layer"; |
| 6 | import * as Option from "effect/Option"; |
| 7 | import * as Path from "effect/Path"; |
| 8 | import * as Stream from "effect/Stream"; |
| 9 | import { AlchemyContext } from "@/AlchemyContext"; |
| 10 | import * as Bundle from "@/Bundle/Bundle"; |
| 11 | import { ResourceContext } from "@/ResourceContext"; |
| 12 | import { StackContext } from "@/StackContext"; |
| 13 | |
| 14 | /** |
| 15 | * Creates a temporary package whose entry logs `marker` and lazily imports |
| 16 | * `lazy.ts`, so the bundle has a content-hashed chunk. The directory is |
| 17 | * removed when the test's scope closes, including on failure. |
| 18 | */ |
| 19 | const makeProject = (marker: string) => |
| 20 | Effect.gen(function* () { |
| 21 | const fs = yield* FileSystem.FileSystem; |
| 22 | const path = yield* Path.Path; |
| 23 | const root = yield* fs.makeTempDirectoryScoped({ prefix: "alchemy-bundle-output-" }); |
| 24 | const entry = path.join(root, "entry.ts"); |
| 25 | yield* fs.writeFileString( |
| 26 | entry, |
| 27 | `console.log(${JSON.stringify(marker)});\nexport const lazy = () => import("./lazy.ts");\n`, |
| 28 | ); |
| 29 | yield* fs.writeFileString(path.join(root, "lazy.ts"), `export const value = "v1";\n`); |
| 30 | return { root, entry }; |
| 31 | }); |
| 32 | |
| 33 | /** |
| 34 | * Runs `effect` the way the engine runs a provider lifecycle operation: with |
| 35 | * the resource's FQN, its stack, and a `.alchemy` directory under `root`. |
| 36 | */ |
| 37 | const inLifecycle = |
| 38 | (root: string, fqn: string) => |
| 39 | <A, E, R>(effect: Effect.Effect<A, E, R>) => |
| 40 | Effect.gen(function* () { |
| 41 | const path = yield* Path.Path; |
| 42 | return yield* effect.pipe( |
| 43 | Effect.provide([ |
| 44 | Layer.succeed(ResourceContext, { |
| 45 | logicalId: fqn.split("/").at(-1)!, |
| 46 | fqn, |
| 47 | instanceId: "00000000000000000000000000000000", |
| 48 | type: "Test.Bundle", |
| 49 | }), |
| 50 | Layer.succeed(StackContext, { |
| 51 | name: "BundleStack", |
| 52 | stage: "test", |
| 53 | resources: {}, |
| 54 | bindings: {}, |
| 55 | actions: {}, |
| 56 | }), |
| 57 | Layer.succeed(AlchemyContext, { |
| 58 | dotAlchemy: path.join(root, ".alchemy"), |
| 59 | dev: false, |
| 60 | adopt: false, |
| 61 | }), |
| 62 | ]), |
| 63 | ); |
| 64 | }); |
| 65 | |
| 66 | layer(NodeServices.layer)("Bundle output location", (it) => { |
| 67 | it.effect( |
| 68 | "build keeps the bundle in memory outside a resource lifecycle", |
| 69 | () => |
| 70 | Effect.gen(function* () { |
| 71 | const fs = yield* FileSystem.FileSystem; |
| 72 | const { root, entry } = yield* makeProject("IN_MEMORY_MARKER"); |
| 73 | |
| 74 | const result = yield* Bundle.build({ input: entry, cwd: root }); |
| 75 | |
| 76 | expect(result.files[0].content).toContain("IN_MEMORY_MARKER"); |
| 77 | // Rolldown's default `dir` would have been `<root>/dist`. |
| 78 | expect((yield* fs.readDirectory(root)).sort()).toEqual(["entry.ts", "lazy.ts"]); |
| 79 | }).pipe(Effect.scoped), |
| 80 | { tags: ["unit", "local"] }, |
| 81 | ); |
| 82 | |
| 83 | it.effect( |
| 84 | "build writes a resource's bundle to its own directory in .alchemy and clears stale chunks", |
| 85 | () => |
| 86 | Effect.gen(function* () { |
| 87 | const fs = yield* FileSystem.FileSystem; |
| 88 | const path = yield* Path.Path; |
| 89 | const { root, entry } = yield* makeProject("LIFECYCLE_MARKER"); |
| 90 | const dir = path.join(root, ".alchemy", "bundles", "BundleStack-test-Api__Handler"); |
| 91 | const chunks = fs |
| 92 | .readDirectory(dir) |
| 93 | .pipe(Effect.map((files) => files.filter((file) => file.startsWith("lazy-")))); |
| 94 | |
| 95 | const first = yield* Bundle.build({ input: entry, cwd: root }).pipe( |
| 96 | inLifecycle(root, "Api/Handler"), |
| 97 | ); |
| 98 | expect(yield* fs.readFileString(path.join(dir, first.files[0].path))).toContain( |
| 99 | "LIFECYCLE_MARKER", |
| 100 | ); |
| 101 | const [firstChunk] = yield* chunks; |
| 102 | expect(firstChunk).toBeDefined(); |
| 103 | |
| 104 | yield* fs.writeFileString(path.join(root, "lazy.ts"), `export const value = "v2";\n`); |
| 105 | yield* Bundle.build({ input: entry, cwd: root }).pipe(inLifecycle(root, "Api/Handler")); |
| 106 | |
| 107 | const secondChunks = yield* chunks; |
| 108 | expect(secondChunks).toHaveLength(1); |
| 109 | expect(secondChunks[0]).not.toBe(firstChunk); |
| 110 | expect(yield* fs.exists(path.join(root, "dist"))).toBe(false); |
| 111 | }).pipe(Effect.scoped), |
| 112 | { tags: ["unit", "local"] }, |
| 113 | ); |
| 114 | |
| 115 | it.effect( |
| 116 | "build writes to the given dir and never clears it", |
| 117 | () => |
| 118 | Effect.gen(function* () { |
| 119 | const fs = yield* FileSystem.FileSystem; |
| 120 | const path = yield* Path.Path; |
| 121 | const { root, entry } = yield* makeProject("ON_DISK_MARKER"); |
| 122 | const dir = path.join(root, "out"); |
| 123 | yield* fs.makeDirectory(dir); |
| 124 | yield* fs.writeFileString(path.join(dir, "keep.txt"), "mine"); |
| 125 | |
| 126 | const result = yield* Bundle.build( |
| 127 | { input: entry, cwd: root }, |
| 128 | { dir, entryFileNames: "index.mjs" }, |
| 129 | ).pipe(inLifecycle(root, "Handler")); |
| 130 | |
| 131 | expect(result.files[0].path).toBe("index.mjs"); |
| 132 | expect(yield* fs.readFileString(path.join(dir, "index.mjs"))).toContain("ON_DISK_MARKER"); |
| 133 | expect(yield* fs.readFileString(path.join(dir, "keep.txt"))).toBe("mine"); |
| 134 | expect(yield* fs.exists(path.join(root, ".alchemy"))).toBe(false); |
| 135 | }).pipe(Effect.scoped), |
| 136 | { tags: ["unit", "local"] }, |
| 137 | ); |
| 138 | |
| 139 | it.effect( |
| 140 | "watch keeps the bundle in memory outside a resource lifecycle", |
| 141 | () => |
| 142 | Effect.gen(function* () { |
| 143 | const fs = yield* FileSystem.FileSystem; |
| 144 | const { root, entry } = yield* makeProject("WATCH_MARKER"); |
| 145 | |
| 146 | const event = yield* firstWatchResult(Bundle.watch({ input: entry, cwd: root })); |
| 147 | |
| 148 | expect(event.output.files[0].content).toContain("WATCH_MARKER"); |
| 149 | expect((yield* fs.readDirectory(root)).sort()).toEqual(["entry.ts", "lazy.ts"]); |
| 150 | }).pipe(Effect.scoped), |
| 151 | { tags: ["unit", "local"] }, |
| 152 | ); |
| 153 | |
| 154 | it.effect( |
| 155 | "watch writes a resource's bundle to its own directory in .alchemy", |
| 156 | () => |
| 157 | Effect.gen(function* () { |
| 158 | const fs = yield* FileSystem.FileSystem; |
| 159 | const path = yield* Path.Path; |
| 160 | const { root, entry } = yield* makeProject("WATCH_LIFECYCLE_MARKER"); |
| 161 | const dir = path.join(root, ".alchemy", "bundles", "BundleStack-test-Handler"); |
| 162 | |
| 163 | const event = yield* firstWatchResult(Bundle.watch({ input: entry, cwd: root })).pipe( |
| 164 | inLifecycle(root, "Handler"), |
| 165 | ); |
| 166 | |
| 167 | expect(yield* fs.readFileString(path.join(dir, event.output.files[0].path))).toContain( |
| 168 | "WATCH_LIFECYCLE_MARKER", |
| 169 | ); |
| 170 | }).pipe(Effect.scoped), |
| 171 | { tags: ["unit", "local"] }, |
| 172 | ); |
| 173 | }); |
| 174 | |
| 175 | /** The first finished build of a watch stream; fails the test on a build error. */ |
| 176 | const firstWatchResult = (stream: Stream.Stream<Bundle.BundleWatchEvent>) => |
| 177 | stream.pipe( |
| 178 | Stream.filter((event) => event._tag !== "Start"), |
| 179 | Stream.runHead, |
| 180 | Effect.map((event) => { |
| 181 | const result = Option.getOrThrow(event); |
| 182 | if (result._tag !== "Success") throw new Error(`watch build failed: ${result.error.message}`); |
| 183 | return result; |
| 184 | }), |
| 185 | ); |