The best way to control Claude Code, Codex and more from any computer or phone
| 1 | /** CLI tests run scratch projects in process; the bin test uses Node to load the config. */ |
| 2 | import * as Generator from "@effect/graphql-generator/Generator" |
| 3 | import * as NodeServices from "@effect/platform-node/NodeServices" |
| 4 | import { assert, describe, it } from "@effect/vitest" |
| 5 | import * as Cause from "effect/Cause" |
| 6 | import { CliOutput } from "effect/cli" |
| 7 | import * as Effect from "effect/Effect" |
| 8 | import * as Exit from "effect/Exit" |
| 9 | import * as FileSystem from "effect/FileSystem" |
| 10 | import * as Layer from "effect/Layer" |
| 11 | import * as Option from "effect/Option" |
| 12 | import * as Path from "effect/Path" |
| 13 | import { ChildProcess, ChildProcessSpawner } from "effect/process" |
| 14 | import * as Runtime from "effect/Runtime" |
| 15 | import * as Stdio from "effect/Stdio" |
| 16 | import * as Stream from "effect/Stream" |
| 17 | import { TestConsole } from "effect/testing" |
| 18 | import { fileURLToPath } from "node:url" |
| 19 | |
| 20 | const schemaSdl = `type User { |
| 21 | id: ID! |
| 22 | name: String! |
| 23 | } |
| 24 | |
| 25 | type Query { |
| 26 | viewer: User! |
| 27 | } |
| 28 | ` |
| 29 | |
| 30 | const query = (name: string, field = "id"): string => `query ${name} {\n viewer {\n ${field}\n }\n}\n` |
| 31 | |
| 32 | const config = { |
| 33 | schema: "./schema.graphql", |
| 34 | documents: ["src/**/*.graphql"] |
| 35 | } |
| 36 | |
| 37 | const configSource = (value: unknown): string => |
| 38 | `import { defineConfig } from "@effect/graphql-generator/Config"\n\nexport default defineConfig(${ |
| 39 | JSON.stringify(value, null, 2) |
| 40 | })\n` |
| 41 | |
| 42 | const header = (sourceName: string): string => |
| 43 | `// Generated by @effect/graphql-generator from ${sourceName}. Do not edit.\n` |
| 44 | |
| 45 | const tmpRoot = fileURLToPath(new URL("./.tmp", import.meta.url)) |
| 46 | const binPath = fileURLToPath(new URL("../src/bin.ts", import.meta.url)) |
| 47 | |
| 48 | const runBin = Effect.fnUntraced(function*(cwd: string) { |
| 49 | const handle = yield* ChildProcess.make("node", [binPath], { cwd }) |
| 50 | return yield* Effect.all({ |
| 51 | exitCode: handle.exitCode, |
| 52 | stderr: Stream.mkString(Stream.decodeText(handle.stderr)) |
| 53 | }, { concurrency: "unbounded" }) |
| 54 | }) |
| 55 | |
| 56 | /** Writes `files` (relative path to contents) to a fresh scratch directory, removed with the scope. */ |
| 57 | const project = Effect.fnUntraced(function*(files: Readonly<Record<string, string>>) { |
| 58 | const fs = yield* FileSystem.FileSystem |
| 59 | const path = yield* Path.Path |
| 60 | yield* fs.makeDirectory(tmpRoot, { recursive: true }) |
| 61 | const dir = yield* fs.makeTempDirectoryScoped({ directory: tmpRoot, prefix: "cli-" }) |
| 62 | yield* write(dir, files) |
| 63 | return { |
| 64 | dir, |
| 65 | configPath: path.join(dir, "graphql.config.ts"), |
| 66 | file: (relative: string) => path.join(dir, relative) |
| 67 | } |
| 68 | }) |
| 69 | |
| 70 | const write = Effect.fnUntraced(function*(dir: string, files: Readonly<Record<string, string>>) { |
| 71 | const fs = yield* FileSystem.FileSystem |
| 72 | const path = yield* Path.Path |
| 73 | for (const [relative, body] of Object.entries(files)) { |
| 74 | const file = path.join(dir, relative) |
| 75 | yield* fs.makeDirectory(path.dirname(file), { recursive: true }) |
| 76 | yield* fs.writeFileString(file, body) |
| 77 | } |
| 78 | }) |
| 79 | |
| 80 | const baseProject = (documents: Readonly<Record<string, string>>, value: unknown = config) => |
| 81 | project({ "graphql.config.ts": configSource(value), "schema.graphql": schemaSdl, ...documents }) |
| 82 | |
| 83 | const runCli = Effect.fnUntraced(function*(args: ReadonlyArray<string>) { |
| 84 | const module = (yield* Effect.promise( |
| 85 | () => import(new URL("../src/main.ts", import.meta.url).href) |
| 86 | )) as { readonly run: Effect.Effect<void, unknown> } |
| 87 | |
| 88 | return yield* Effect.gen(function*() { |
| 89 | const exit = yield* Effect.exit(module.run) |
| 90 | const stdout = (yield* TestConsole.logLines).map(String).join("\n") |
| 91 | const stderr = (yield* TestConsole.errorLines).map(String).join("\n") |
| 92 | return { exitCode: exitCode(exit), stdout, stderr, output: `${stdout}\n${stderr}` } as const |
| 93 | }).pipe( |
| 94 | Effect.provide(Layer.mergeAll( |
| 95 | TestConsole.layer, |
| 96 | CliOutput.layer(CliOutput.defaultFormatter({ colors: false })), |
| 97 | NodeServices.layer, |
| 98 | Stdio.layerTest({ args: Effect.succeed(args) }) |
| 99 | )) |
| 100 | ) |
| 101 | }) |
| 102 | |
| 103 | /** The process exit code `NodeRuntime.runMain` would use for `exit`. */ |
| 104 | const exitCode = (exit: Exit.Exit<void, unknown>): number => |
| 105 | Exit.isSuccess(exit) ? 0 : Runtime.getErrorExitCode(Cause.squash(exit.cause)) |
| 106 | |
| 107 | const exists = Effect.fnUntraced(function*(file: string) { |
| 108 | const fs = yield* FileSystem.FileSystem |
| 109 | return yield* fs.exists(file) |
| 110 | }) |
| 111 | |
| 112 | const read = Effect.fnUntraced(function*(file: string) { |
| 113 | const fs = yield* FileSystem.FileSystem |
| 114 | return yield* fs.readFileString(file) |
| 115 | }) |
| 116 | |
| 117 | describe("graphqlgen CLI", () => { |
| 118 | it.effect("unknown flags exit 2", () => |
| 119 | Effect.gen(function*() { |
| 120 | const result = yield* runCli(["--unknown"]) |
| 121 | assert.strictEqual(result.exitCode, 2, result.output) |
| 122 | })) |
| 123 | |
| 124 | it.effect("--watch --check exits 2", () => |
| 125 | Effect.gen(function*() { |
| 126 | const result = yield* runCli(["--watch", "--check"]) |
| 127 | assert.strictEqual(result.exitCode, 2, result.output) |
| 128 | })) |
| 129 | |
| 130 | it.effect("a clean run writes the generated files, deletes stale ones and exits 0; a second run writes nothing", () => |
| 131 | Effect.gen(function*() { |
| 132 | const fs = yield* FileSystem.FileSystem |
| 133 | const p = yield* baseProject({ "src/a.graphql": query("A") }) |
| 134 | const expected = yield* Generator.generate(config, { cwd: p.dir }) |
| 135 | yield* write(p.dir, { "src/stale.graphql.ts": header("stale.graphql") }) |
| 136 | |
| 137 | const first = yield* runCli(["--config", p.configPath]) |
| 138 | assert.strictEqual(first.exitCode, 0, first.output) |
| 139 | assert.isFalse(yield* exists(p.file("src/stale.graphql.ts"))) |
| 140 | for (const file of expected.files) { |
| 141 | assert.strictEqual(yield* read(file.path), file.contents, file.path) |
| 142 | yield* fs.utimes(file.path, 0, 0) |
| 143 | } |
| 144 | |
| 145 | const second = yield* runCli(["--config", p.configPath]) |
| 146 | assert.strictEqual(second.exitCode, 0, second.output) |
| 147 | for (const file of expected.files) { |
| 148 | const info = yield* fs.stat(file.path) |
| 149 | assert.strictEqual(Option.getOrThrow(info.mtime).getTime(), 0, `${file.path} was rewritten`) |
| 150 | } |
| 151 | }).pipe(Effect.scoped, Effect.provide(NodeServices.layer))) |
| 152 | |
| 153 | it.effect("a validation error prints file:line:col with a code frame, changes nothing on disk and exits 1", () => |
| 154 | Effect.gen(function*() { |
| 155 | const p = yield* baseProject({ |
| 156 | "src/a.graphql": query("A"), |
| 157 | "src/b.graphql": query("B", "nope"), |
| 158 | "src/stale.graphql.ts": header("stale.graphql") |
| 159 | }) |
| 160 | |
| 161 | const result = yield* runCli(["--config", p.configPath]) |
| 162 | assert.strictEqual(result.exitCode, 1, result.output) |
| 163 | assert.include(result.stderr, `src/b.graphql:3:5: error: Cannot query field "nope" on type "User".`) |
| 164 | assert.include(result.stderr, "3 | nope") |
| 165 | assert.isFalse(yield* exists(p.file("src/a.graphql.ts"))) |
| 166 | assert.isFalse(yield* exists(p.file("schema.graphql.ts"))) |
| 167 | assert.isTrue(yield* exists(p.file("src/stale.graphql.ts"))) |
| 168 | }).pipe(Effect.scoped, Effect.provide(NodeServices.layer))) |
| 169 | |
| 170 | it.effect("a config that fails to decode exits 2 naming the issue path", () => |
| 171 | Effect.gen(function*() { |
| 172 | const p = yield* baseProject( |
| 173 | { "src/a.graphql": query("A") }, |
| 174 | { schema: "./schema.graphql", documents: "src/**/*.graphql" } |
| 175 | ) |
| 176 | |
| 177 | const result = yield* runCli(["--config", p.configPath]) |
| 178 | assert.strictEqual(result.exitCode, 2, result.output) |
| 179 | assert.include(result.stderr, "documents") |
| 180 | assert.isFalse(yield* exists(p.file("src/a.graphql.ts"))) |
| 181 | }).pipe(Effect.scoped, Effect.provide(NodeServices.layer))) |
| 182 | |
| 183 | it.effect("--check exits 0 when up to date, otherwise lists every create, change and delete and exits 1", () => |
| 184 | Effect.gen(function*() { |
| 185 | const fs = yield* FileSystem.FileSystem |
| 186 | const p = yield* baseProject({ "src/changed.graphql": query("Changed"), "src/removed.graphql": query("Removed") }) |
| 187 | yield* runCli(["--config", p.configPath]) |
| 188 | |
| 189 | const upToDate = yield* runCli(["--config", p.configPath, "--check"]) |
| 190 | assert.strictEqual(upToDate.exitCode, 0, upToDate.output) |
| 191 | |
| 192 | yield* write(p.dir, { "src/changed.graphql": query("Changed", "name"), "src/created.graphql": query("Created") }) |
| 193 | yield* fs.remove(p.file("src/removed.graphql")) |
| 194 | |
| 195 | const result = yield* runCli(["--config", p.configPath, "--check"]) |
| 196 | assert.strictEqual(result.exitCode, 1, result.output) |
| 197 | assert.match(result.stdout, /would create \S*src\/created\.graphql\.ts/) |
| 198 | assert.match(result.stdout, /would update \S*src\/changed\.graphql\.ts/) |
| 199 | assert.match(result.stdout, /would delete \S*src\/removed\.graphql\.ts/) |
| 200 | assert.isFalse(yield* exists(p.file("src/created.graphql.ts"))) |
| 201 | assert.isTrue(yield* exists(p.file("src/removed.graphql.ts"))) |
| 202 | }).pipe(Effect.scoped, Effect.provide(NodeServices.layer))) |
| 203 | |
| 204 | it.effect("documents skip the schema file, node_modules and dot-directories without a diagnostic", () => |
| 205 | Effect.gen(function*() { |
| 206 | const p = yield* baseProject( |
| 207 | { "src/a.graphql": query("A"), "node_modules/pkg/bad.graphql": "query {", ".cache/bad.graphql": "query {" }, |
| 208 | { schema: "./schema.graphql", documents: ["**/*.graphql"] } |
| 209 | ) |
| 210 | |
| 211 | const result = yield* runCli(["--config", p.configPath]) |
| 212 | assert.strictEqual(result.exitCode, 0, result.output) |
| 213 | assert.strictEqual(result.stderr, "") |
| 214 | assert.isTrue(yield* exists(p.file("src/a.graphql.ts"))) |
| 215 | }).pipe(Effect.scoped, Effect.provide(NodeServices.layer))) |
| 216 | |
| 217 | it.effect("documents patterns rooted in node_modules or a dot-directory match nothing; ../ roots still work", () => |
| 218 | Effect.gen(function*() { |
| 219 | const orphan = `${header("orphan.graphql")}export const Orphan = 1\n` |
| 220 | const p = yield* project({ |
| 221 | "app/graphql.config.ts": configSource({ |
| 222 | schema: "./schema.graphql", |
| 223 | documents: ["../shared/**/*.graphql", ".cache/**/*.graphql", "node_modules/pkg/**/*.graphql"] |
| 224 | }), |
| 225 | "app/schema.graphql": schemaSdl, |
| 226 | "shared/a.graphql": query("A"), |
| 227 | "app/.cache/q.graphql": query("CacheQuery"), |
| 228 | "app/.cache/orphan.graphql.ts": orphan, |
| 229 | "app/node_modules/pkg/q.graphql": query("PackageQuery"), |
| 230 | "app/node_modules/pkg/orphan.graphql.ts": orphan |
| 231 | }) |
| 232 | |
| 233 | const result = yield* runCli(["--config", p.file("app/graphql.config.ts")]) |
| 234 | assert.strictEqual(result.exitCode, 0, result.output) |
| 235 | assert.isTrue(yield* exists(p.file("shared/a.graphql.ts"))) |
| 236 | assert.isFalse(yield* exists(p.file("app/.cache/q.graphql.ts"))) |
| 237 | assert.isFalse(yield* exists(p.file("app/node_modules/pkg/q.graphql.ts"))) |
| 238 | assert.isTrue(yield* exists(p.file("app/.cache/orphan.graphql.ts"))) |
| 239 | assert.isTrue(yield* exists(p.file("app/node_modules/pkg/orphan.graphql.ts"))) |
| 240 | }).pipe(Effect.scoped, Effect.provide(NodeServices.layer))) |
| 241 | |
| 242 | it.effect("bin.ts loads graphql.config.ts from the working directory under Node", () => |
| 243 | Effect.gen(function*() { |
| 244 | const p = yield* baseProject({ "src/a.graphql": query("A") }) |
| 245 | |
| 246 | const result = yield* runBin(p.dir) |
| 247 | assert.strictEqual(result.exitCode, ChildProcessSpawner.ExitCode(0), result.stderr) |
| 248 | assert.include(yield* read(p.file("src/a.graphql.ts")), header("a.graphql")) |
| 249 | }).pipe(Effect.scoped, Effect.provide(NodeServices.layer))) |
| 250 | }) |