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, 04:45 UTC
| 1 | import * as NodeServices from "@effect/platform-node/NodeServices" |
| 2 | import { assert, describe, it } from "@effect/vitest" |
| 3 | import * as Effect from "effect/Effect" |
| 4 | import { |
| 5 | assertNoErrors, |
| 6 | fileLines, |
| 7 | type Generated, |
| 8 | generateIn, |
| 9 | generateTasks, |
| 10 | taskConfig, |
| 11 | warnings |
| 12 | } from "./utils/generator.ts" |
| 13 | import { fixture } from "./utils/model.ts" |
| 14 | |
| 15 | const located = (generated: Generated) => |
| 16 | generated.result.diagnostics.map(({ column, line, message, severity }) => ({ severity, line, column, message })) |
| 17 | |
| 18 | describe("Generator diagnostics", () => { |
| 19 | it.effect("unmapped custom scalars decode as Schema.Json with one warning listing each one reached", () => |
| 20 | Effect.gen(function*() { |
| 21 | const generated = yield* generateTasks({ |
| 22 | "src/a.graphql": "query A { tasks { bodyHTML } }", |
| 23 | "src/b.graphql": "query B { tasks { bodyHTML due } }" |
| 24 | }) |
| 25 | assertNoErrors(generated) |
| 26 | assert.include(fileLines(generated.file("schema/app.graphql.ts")), "export const HTML = Schema.Json") |
| 27 | assert.deepStrictEqual(warnings(generated).map((warning) => warning.message), [ |
| 28 | "Custom scalars without a mapping decode as Schema.Json: HTML. Map each one to a codec with `scalars` in the config." |
| 29 | ]) |
| 30 | }).pipe(Effect.provide(NodeServices.layer))) |
| 31 | |
| 32 | it.effect("a scalars key naming a type that isn't a scalar is a config error", () => |
| 33 | Effect.gen(function*() { |
| 34 | const error = yield* generateTasks( |
| 35 | { "src/a.graphql": "query A { tasks { id } }" }, |
| 36 | { ...taskConfig, scalars: { Task: "./scalars.ts#Task" } } |
| 37 | ).pipe(Effect.flip) |
| 38 | assert.strictEqual(error._tag, "ConfigError") |
| 39 | assert.include(error.message, "Task") |
| 40 | }).pipe(Effect.provide(NodeServices.layer))) |
| 41 | |
| 42 | it.effect("a type-system definition in a document file is a located error", () => |
| 43 | Effect.gen(function*() { |
| 44 | const generated = yield* generateTasks({ |
| 45 | "src/ops.graphql": "query A { tasks { id } }\n\ntype Extra {\n a: Int\n}\n" |
| 46 | }) |
| 47 | assert.deepStrictEqual(located(generated), [ |
| 48 | { severity: "error", line: 3, column: 1, message: `The "Extra" definition is not executable.` } |
| 49 | ]) |
| 50 | assert.deepStrictEqual(generated.result.files, []) |
| 51 | }).pipe(Effect.provide(NodeServices.layer))) |
| 52 | |
| 53 | it.effect("a definition named like a binding of the generated module is a located error", () => |
| 54 | Effect.gen(function*() { |
| 55 | const generated = yield* generateTasks({ "src/ops.graphql": "query Schema { tasks { id } }" }) |
| 56 | assert.deepStrictEqual(located(generated), [{ |
| 57 | severity: "error", |
| 58 | line: 1, |
| 59 | column: 7, |
| 60 | message: `The name "Schema" is reserved in generated code; rename this definition.` |
| 61 | }]) |
| 62 | assert.deepStrictEqual(generated.result.files, []) |
| 63 | }).pipe(Effect.provide(NodeServices.layer))) |
| 64 | |
| 65 | it.effect("a field whose type the schema never defines is an Unknown type error, not a crash", () => |
| 66 | Effect.gen(function*() { |
| 67 | const generated = yield* generateIn({ |
| 68 | "schema.graphql": "type Query { a: Missing }", |
| 69 | "src/a.graphql": "query Q { a }" |
| 70 | }, { schema: "./schema.graphql", documents: ["src/*.graphql"] }) |
| 71 | assert.deepStrictEqual(generated.result.diagnostics.map(({ message, severity }) => ({ severity, message })), [ |
| 72 | { severity: "error", message: `Unknown type "Missing".` } |
| 73 | ]) |
| 74 | assert.deepStrictEqual(generated.result.files, []) |
| 75 | }).pipe(Effect.provide(NodeServices.layer))) |
| 76 | |
| 77 | it.effect("eval and arguments, which strict mode can't bind, are reserved definition names", () => |
| 78 | Effect.gen(function*() { |
| 79 | const generated = yield* generateTasks({ |
| 80 | "src/ops.graphql": "query eval { tasks { id } }\nquery arguments { tasks { id } }" |
| 81 | }) |
| 82 | assert.deepStrictEqual(located(generated).map(({ line, message }) => ({ line, message })), [ |
| 83 | { line: 1, message: `The name "eval" is reserved in generated code; rename this definition.` }, |
| 84 | { line: 2, message: `The name "arguments" is reserved in generated code; rename this definition.` } |
| 85 | ]) |
| 86 | assert.deepStrictEqual(generated.result.files, []) |
| 87 | }).pipe(Effect.provide(NodeServices.layer))) |
| 88 | |
| 89 | it.effect("a shared module path that is also a document's output is an error, and nothing is generated", () => |
| 90 | Effect.gen(function*() { |
| 91 | const generated = yield* generateTasks({ "src/ops.graphql": "query Tasks { tasks { id } }" }, { |
| 92 | ...taskConfig, |
| 93 | shared: "./src/ops.graphql.ts" |
| 94 | }) |
| 95 | assert.strictEqual(generated.result.diagnostics.filter((d) => d.severity === "error").length, 1) |
| 96 | assert.deepStrictEqual(generated.result.files, []) |
| 97 | }).pipe(Effect.provide(NodeServices.layer))) |
| 98 | }) |
| 99 | |
| 100 | describe("Generator introspection JSON schema", () => { |
| 101 | const search = |
| 102 | `query Search($filter: SearchFilter, $limit: Int = 10) { now search(term: "x", filter: $filter, limit: $limit) }` |
| 103 | |
| 104 | it.effect("a .json schema generates the same files as its SDL, with the shared module named <schema>.graphql.ts", () => |
| 105 | Effect.gen(function*() { |
| 106 | const fromJson = yield* generateIn( |
| 107 | { "schema/modern.json": fixture("introspection/modern.json"), "src/search.graphql": search }, |
| 108 | { schema: "./schema/modern.json", documents: ["src/*.graphql"] } |
| 109 | ) |
| 110 | const fromSdl = yield* generateIn( |
| 111 | { "schema/modern.graphql": fixture("introspection/modern.graphql"), "src/search.graphql": search }, |
| 112 | { schema: "./schema/modern.graphql", documents: ["src/*.graphql"] } |
| 113 | ) |
| 114 | assertNoErrors(fromJson) |
| 115 | assert.deepStrictEqual(fromJson.paths, ["schema/modern.graphql.ts", "src/search.graphql.ts"]) |
| 116 | assert.deepStrictEqual(fromSdl.paths, fromJson.paths) |
| 117 | for (const path of fromJson.paths) { |
| 118 | assert.strictEqual(fromJson.file(path), fromSdl.file(path), path) |
| 119 | } |
| 120 | }).pipe(Effect.provide(NodeServices.layer))) |
| 121 | }) |