The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as NodeFs from "node:fs/promises"; |
| 2 | import { assert, describe, expect, it, layer } from "@alchemy.run/test"; |
| 3 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import * as FileSystem from "effect/FileSystem"; |
| 6 | import * as Path from "effect/Path"; |
| 7 | import * as Predicate from "effect/Predicate"; |
| 8 | import * as Bundle from "@/Bundle/Bundle"; |
| 9 | import { RAW_RE, rawPlugin, splitFileAndPostfix } from "@/Bundle/RawPlugin"; |
| 10 | |
| 11 | layer(NodeServices.layer)("Bundle.build with rawPlugin", (it) => { |
| 12 | it.effect( |
| 13 | "inlines a sibling file imported with ?raw", |
| 14 | () => |
| 15 | Effect.gen(function* () { |
| 16 | const fs = yield* FileSystem.FileSystem; |
| 17 | const path = yield* Path.Path; |
| 18 | const root = yield* fs.makeTempDirectory({ prefix: "alchemy-raw-bundle-" }); |
| 19 | yield* fs.writeFileString(path.join(root, "hello.txt"), "HELLO_RAW_MARKER"); |
| 20 | const entry = path.join(root, "entry.ts"); |
| 21 | yield* fs.writeFileString(entry, `import txt from "./hello.txt?raw";\nconsole.log(txt);\n`); |
| 22 | |
| 23 | const result = yield* Bundle.build({ input: entry, cwd: root }); |
| 24 | |
| 25 | const code = result.files |
| 26 | .filter((f) => typeof f.content === "string") |
| 27 | .map((f) => f.content as string) |
| 28 | .join("\n"); |
| 29 | expect(code).toContain(`"HELLO_RAW_MARKER"`); |
| 30 | // The bundle should not emit hello.txt as a separate asset. |
| 31 | expect(result.files.every((f) => !f.path.endsWith("hello.txt"))).toBe(true); |
| 32 | |
| 33 | yield* fs.remove(root, { recursive: true }); |
| 34 | }), |
| 35 | { tags: ["unit", "local"] }, |
| 36 | ); |
| 37 | |
| 38 | it.effect( |
| 39 | "resolves ?raw imports through subdirectories", |
| 40 | () => |
| 41 | Effect.gen(function* () { |
| 42 | const fs = yield* FileSystem.FileSystem; |
| 43 | const path = yield* Path.Path; |
| 44 | const root = yield* fs.makeTempDirectory({ prefix: "alchemy-raw-subdir-" }); |
| 45 | yield* fs.makeDirectory(path.join(root, "sub"), { recursive: true }); |
| 46 | yield* fs.writeFileString( |
| 47 | path.join(root, "sub", "foo.json"), |
| 48 | `{"marker":"SUBDIR_RAW_MARKER"}`, |
| 49 | ); |
| 50 | const entry = path.join(root, "entry.ts"); |
| 51 | yield* fs.writeFileString( |
| 52 | entry, |
| 53 | `import foo from "./sub/foo.json?raw";\nconsole.log(foo);\n`, |
| 54 | ); |
| 55 | |
| 56 | const result = yield* Bundle.build({ input: entry, cwd: root }); |
| 57 | |
| 58 | const code = result.files |
| 59 | .filter((f) => typeof f.content === "string") |
| 60 | .map((f) => f.content as string) |
| 61 | .join("\n"); |
| 62 | expect(code).toContain("SUBDIR_RAW_MARKER"); |
| 63 | |
| 64 | yield* fs.remove(root, { recursive: true }); |
| 65 | }), |
| 66 | { tags: ["unit", "local"] }, |
| 67 | ); |
| 68 | }); |
| 69 | |
| 70 | layer(NodeServices.layer)("rawPlugin load hook", (it) => { |
| 71 | /** |
| 72 | * Invokes a plugin's `load` hook directly with a stubbed plugin |
| 73 | * context. The handler reads through `this.fs` (rolldown's |
| 74 | * plugin-context filesystem); we stub it with `node:fs/promises` so the |
| 75 | * disk read works without booting a full rolldown build. |
| 76 | */ |
| 77 | const load = (id: string) => |
| 78 | Effect.promise(async () => { |
| 79 | const plugin = rawPlugin(); |
| 80 | assert( |
| 81 | Predicate.hasProperty(plugin, "load") && |
| 82 | Predicate.hasProperty(plugin.load, "handler") && |
| 83 | typeof plugin.load.handler === "function", |
| 84 | "plugin has no load hook", |
| 85 | ); |
| 86 | const result = await plugin.load.handler.call({ fs: NodeFs } as any, id); |
| 87 | assert(Predicate.isObject(result), "load hook returned non-object"); |
| 88 | return result; |
| 89 | }); |
| 90 | |
| 91 | it.effect( |
| 92 | "inlines a .txt file as a JSON-encoded default export", |
| 93 | () => |
| 94 | Effect.gen(function* () { |
| 95 | const fs = yield* FileSystem.FileSystem; |
| 96 | const path = yield* Path.Path; |
| 97 | const root = yield* fs.makeTempDirectory({ prefix: "alchemy-raw-load-" }); |
| 98 | const file = path.join(root, "hello.txt"); |
| 99 | yield* fs.writeFileString(file, "Hello, World!\n"); |
| 100 | |
| 101 | const result = yield* load(file); |
| 102 | |
| 103 | expect(result.code).toBe(`export default "Hello, World!\\n";`); |
| 104 | expect(result.moduleType).toBe("js"); |
| 105 | |
| 106 | yield* fs.remove(root, { recursive: true }); |
| 107 | }), |
| 108 | { tags: ["unit", "local"] }, |
| 109 | ); |
| 110 | |
| 111 | it.effect( |
| 112 | "inlines a .json file verbatim (no parsing)", |
| 113 | () => |
| 114 | Effect.gen(function* () { |
| 115 | const fs = yield* FileSystem.FileSystem; |
| 116 | const path = yield* Path.Path; |
| 117 | const root = yield* fs.makeTempDirectory({ prefix: "alchemy-raw-json-" }); |
| 118 | const file = path.join(root, "data.json"); |
| 119 | const raw = `{"a": 1, "b": "two"}`; |
| 120 | yield* fs.writeFileString(file, raw); |
| 121 | |
| 122 | const result = yield* load(`${file}?raw`); |
| 123 | |
| 124 | expect(result.code).toBe(`export default ${JSON.stringify(raw)};`); |
| 125 | |
| 126 | yield* fs.remove(root, { recursive: true }); |
| 127 | }), |
| 128 | { tags: ["unit", "local"] }, |
| 129 | ); |
| 130 | |
| 131 | it.effect( |
| 132 | "strips additional query params before reading the file", |
| 133 | () => |
| 134 | Effect.gen(function* () { |
| 135 | const fs = yield* FileSystem.FileSystem; |
| 136 | const path = yield* Path.Path; |
| 137 | const root = yield* fs.makeTempDirectory({ prefix: "alchemy-raw-q-" }); |
| 138 | const file = path.join(root, "page.html"); |
| 139 | yield* fs.writeFileString(file, "<h1>hi</h1>"); |
| 140 | |
| 141 | const result = yield* load(`${file}?raw&t=12345`); |
| 142 | |
| 143 | expect(result.code).toBe(`export default "<h1>hi</h1>";`); |
| 144 | |
| 145 | yield* fs.remove(root, { recursive: true }); |
| 146 | }), |
| 147 | { tags: ["unit", "local"] }, |
| 148 | ); |
| 149 | }); |
| 150 | |
| 151 | describe("RAW_RE", { tags: ["unit", "local"] }, () => { |
| 152 | it("matches `?raw`", () => { |
| 153 | expect(RAW_RE.test("/foo/bar.txt?raw")).toBe(true); |
| 154 | }); |
| 155 | |
| 156 | it("matches `?raw&foo`", () => { |
| 157 | expect(RAW_RE.test("/foo/bar.txt?raw&foo")).toBe(true); |
| 158 | }); |
| 159 | |
| 160 | it("matches `?other&raw`", () => { |
| 161 | expect(RAW_RE.test("/foo/bar.txt?other&raw")).toBe(true); |
| 162 | }); |
| 163 | |
| 164 | it("does NOT match ids without ?raw", () => { |
| 165 | expect(RAW_RE.test("/foo/bar.txt")).toBe(false); |
| 166 | expect(RAW_RE.test("/foo/bar.txt?url")).toBe(false); |
| 167 | expect(RAW_RE.test("/foo/bar.txt?rawish")).toBe(false); |
| 168 | }); |
| 169 | }); |
| 170 | |
| 171 | describe("splitFileAndPostfix", { tags: ["unit", "local"] }, () => { |
| 172 | it("splits at the first `?`", () => { |
| 173 | expect(splitFileAndPostfix("./foo.txt?raw")).toEqual(["./foo.txt", "?raw"]); |
| 174 | }); |
| 175 | |
| 176 | it("splits at the first `#`", () => { |
| 177 | expect(splitFileAndPostfix("./foo.txt#frag")).toEqual(["./foo.txt", "#frag"]); |
| 178 | }); |
| 179 | |
| 180 | it("returns empty postfix when no query/hash", () => { |
| 181 | expect(splitFileAndPostfix("./foo.txt")).toEqual(["./foo.txt", ""]); |
| 182 | }); |
| 183 | |
| 184 | it("splits at whichever of `?` / `#` comes first", () => { |
| 185 | expect(splitFileAndPostfix("./foo.txt#frag?raw")).toEqual(["./foo.txt", "#frag?raw"]); |
| 186 | }); |
| 187 | }); |