The best way to control Claude Code, Codex and more from any computer or phone

main
ts 217 lines 6.91 KB
Raw
1 /**
2 * Helpers for the schema model tests: reading fixtures through either reader,
3 * a structural model comparison that names the first differing type, and
4 * builders for expected models.
5 */
6 import * as IntrospectionReader from "@effect/graphql-generator/internal/IntrospectionReader"
7 import type * as Model from "@effect/graphql-generator/internal/SchemaModel"
8 import * as SdlReader from "@effect/graphql-generator/internal/SdlReader"
9 import { assert } from "@effect/vitest"
10 import * as Result from "effect/Result"
11 import { readFileSync } from "node:fs"
12 import { formatDiagnostic, parseOrThrow, source } from "./ast.ts"
13
14 export const fixture = (file: string): string => readFileSync(new URL(`../fixtures/${file}`, import.meta.url), "utf8")
15
16 export const readSdl = (body: string, path = "schema.graphql"): Model.Schema => {
17 const result = SdlReader.read(source(body, path), parseOrThrow(body, path))
18 if (Result.isFailure(result)) {
19 throw new Error(`unexpected diagnostic: ${formatDiagnostic(result.failure)}`)
20 }
21 return result.success
22 }
23
24 export const readIntrospection = (body: string, path = "schema.json"): Model.Schema => {
25 const result = IntrospectionReader.read(source(body, path))
26 if (Result.isFailure(result)) {
27 throw new Error(`unexpected diagnostic: ${formatDiagnostic(result.failure)}`)
28 }
29 return result.success
30 }
31
32 /** Maps become plain objects (so entry order is ignored) and `undefined` members are dropped. */
33 const normalize = (value: unknown): unknown => {
34 if (value instanceof Map) {
35 const out: Record<string, unknown> = {}
36 for (const [key, child] of value) out[String(key)] = normalize(child)
37 return out
38 }
39 if (Array.isArray(value)) return value.map(normalize)
40 if (value !== null && typeof value === "object") {
41 const out: Record<string, unknown> = {}
42 for (const [key, child] of Object.entries(value)) {
43 if (child !== undefined) out[key] = normalize(child)
44 }
45 return out
46 }
47 return value
48 }
49
50 const sortedKeys = (a: ReadonlyMap<string, unknown>, b: ReadonlyMap<string, unknown>): ReadonlyArray<string> =>
51 Array.from(new Set([...a.keys(), ...b.keys()])).sort()
52
53 /**
54 * Compares two models structurally. Types are compared one at a time in name
55 * order, so a failure names the first differing type instead of dumping both
56 * schemas.
57 */
58
59 export const assertModelsEqual = (actual: Model.Schema, expected: Model.Schema): void => {
60 assert.deepStrictEqual(
61 normalize({
62 queryType: actual.queryType,
63 mutationType: actual.mutationType,
64 subscriptionType: actual.subscriptionType
65 }),
66 normalize({
67 queryType: expected.queryType,
68 mutationType: expected.mutationType,
69 subscriptionType: expected.subscriptionType
70 }),
71 "models differ in their root operation types"
72 )
73 for (const name of sortedKeys(actual.types, expected.types)) {
74 assert.deepStrictEqual(
75 normalize(actual.types.get(name)),
76 normalize(expected.types.get(name)),
77 `models differ at type "${name}"`
78 )
79 }
80 }
81
82 /** Compares one named type of `model` with `expected`. */
83 export const assertType = (model: Model.Schema, expected: Model.NamedType): void => {
84 assert.deepStrictEqual(
85 normalize(model.types.get(expected.name)),
86 normalize(expected),
87 `type "${expected.name}" differs`
88 )
89 }
90
91 // Expected-model builders. Optional members default to absent.
92
93 export const named = (name: string): Model.NamedTypeRef => ({ _tag: "NamedTypeRef", name })
94
95 export const list = (ofType: Model.TypeRef): Model.ListTypeRef => ({ _tag: "ListTypeRef", ofType })
96
97 export const nonNull = (ofType: Model.NamedTypeRef | Model.ListTypeRef): Model.NonNullTypeRef => ({
98 _tag: "NonNullTypeRef",
99 ofType
100 })
101
102 export const scalarType = (name: string, options: { readonly description?: string } = {}): Model.ScalarType => ({
103 _tag: "ScalarType",
104 name,
105 description: options.description
106 })
107
108 const builtInScalars: ReadonlyArray<Model.ScalarType> = ["String", "Int", "Float", "Boolean", "ID"].map((
109 name
110 ) => scalarType(name))
111
112 export const objectType = (
113 name: string,
114 fields: ReadonlyArray<Model.Field>,
115 options: { readonly description?: string; readonly interfaces?: ReadonlyArray<string> } = {}
116 ): Model.ObjectType => ({
117 _tag: "ObjectType",
118 name,
119 description: options.description,
120 interfaces: options.interfaces ?? [],
121 fields
122 })
123
124 export const interfaceType = (
125 name: string,
126 fields: ReadonlyArray<Model.Field>,
127 options: {
128 readonly description?: string
129 readonly interfaces?: ReadonlyArray<string>
130 readonly possibleTypes?: ReadonlyArray<string>
131 } = {}
132 ): Model.InterfaceType => ({
133 _tag: "InterfaceType",
134 name,
135 description: options.description,
136 interfaces: options.interfaces ?? [],
137 fields,
138 possibleTypes: options.possibleTypes ?? []
139 })
140
141 export const unionType = (
142 name: string,
143 possibleTypes: ReadonlyArray<string>,
144 options: { readonly description?: string } = {}
145 ): Model.UnionType => ({ _tag: "UnionType", name, description: options.description, possibleTypes })
146
147 export const enumType = (
148 name: string,
149 values: ReadonlyArray<Model.EnumValueDefinition>,
150 options: { readonly description?: string } = {}
151 ): Model.EnumType => ({ _tag: "EnumType", name, description: options.description, values })
152
153 export const enumValue = (
154 name: string,
155 options: { readonly description?: string; readonly deprecationReason?: string } = {}
156 ): Model.EnumValueDefinition => ({
157 name,
158 description: options.description,
159 deprecationReason: options.deprecationReason
160 })
161
162 export const inputObjectType = (
163 name: string,
164 fields: ReadonlyArray<Model.InputValue>,
165 options: { readonly description?: string; readonly oneOf?: boolean } = {}
166 ): Model.InputObjectType => ({
167 _tag: "InputObjectType",
168 name,
169 description: options.description,
170 oneOf: options.oneOf ?? false,
171 fields
172 })
173
174 export const field = (
175 name: string,
176 type: Model.TypeRef,
177 options: {
178 readonly description?: string
179 readonly arguments?: ReadonlyArray<Model.InputValue>
180 readonly deprecationReason?: string
181 } = {}
182 ): Model.Field => ({
183 name,
184 description: options.description,
185 arguments: options.arguments ?? [],
186 type,
187 deprecationReason: options.deprecationReason
188 })
189
190 export const inputValue = (
191 name: string,
192 type: Model.TypeRef,
193 options: {
194 readonly description?: string
195 readonly defaultValue?: Model.ConstValue
196 readonly deprecationReason?: string
197 } = {}
198 ): Model.InputValue => ({
199 name,
200 description: options.description,
201 type,
202 defaultValue: options.defaultValue,
203 deprecationReason: options.deprecationReason
204 })
205
206 /** A complete expected schema; the built-in scalars are added for you. */
207 export const schema = (options: {
208 readonly queryType: string
209 readonly mutationType?: string
210 readonly subscriptionType?: string
211 readonly types: ReadonlyArray<Model.NamedType>
212 }): Model.Schema => ({
213 queryType: options.queryType,
214 mutationType: options.mutationType,
215 subscriptionType: options.subscriptionType,
216 types: new Map([...builtInScalars, ...options.types].map((type) => [type.name, type]))
217 })