/** * Shared helpers for the language front-end tests. * * Expectations are written as `Plain` nodes: the AST without `loc`, with * `undefined` children omitted, so a test states only what the grammar says. */ import type * as Ast from "@effect/graphql-generator/internal/Ast" import type { Diagnostic, Source } from "@effect/graphql-generator/internal/Diagnostic" import { parse } from "@effect/graphql-generator/internal/Parser" import { assert } from "@effect/vitest" import * as Result from "effect/Result" type OptionalKeys = { [K in keyof T]-?: undefined extends T[K] ? K : never }[keyof T] export type Plain = T extends ReadonlyArray ? ReadonlyArray> : T extends object ? & { readonly [K in Exclude | "loc">]: Plain } & { readonly [K in Exclude, "loc">]?: Plain> } : T export const source = (body: string, path = "test.graphql"): Source => ({ path, body }) const strip = (value: unknown): unknown => { if (Array.isArray(value)) return value.map(strip) if (value !== null && typeof value === "object") { const out: Record = {} for (const [key, child] of Object.entries(value)) { if (key === "loc" || child === undefined) continue out[key] = strip(child) } return out } return value } export const stripLoc = (node: T): Plain => strip(node) as Plain export const formatDiagnostic = (diagnostic: Diagnostic): string => `${diagnostic.path}:${diagnostic.line}:${diagnostic.column} ${diagnostic.message}\n${diagnostic.codeFrame}` export const parseOrThrow = (body: string, path?: string): Ast.Document => { const result = parse(source(body, path)) if (Result.isFailure(result)) { throw new Error(`unexpected diagnostic: ${formatDiagnostic(result.failure)}`) } return result.success } export const assertDefinitions = (body: string, expected: ReadonlyArray>): void => { assert.deepStrictEqual(stripLoc(parseOrThrow(body)).definitions, expected) } interface ExpectedDiagnostic { readonly line: number readonly column: number readonly message: string } export const assertDiagnostic = ( body: string, expected: ExpectedDiagnostic, parser: (source: Source) => Result.Result = parse ): void => { const result = parser(source(body)) if (Result.isSuccess(result)) { assert.fail(`expected a diagnostic for ${JSON.stringify(body)} but it parsed`) } const { column, line, message } = result.failure assert.deepStrictEqual({ line, column, message }, expected) } /** Parses `{ f(s: ) }` and returns the argument's value node. */ export const argumentValue = (literal: string): Plain => { const document = parseOrThrow(`{ f(s: ${literal}) }`) const operation = document.definitions[0] assert(operation !== undefined && operation._tag === "OperationDefinition") const field = operation.selectionSet.selections[0] assert(field !== undefined && field._tag === "Field") const argument = field.arguments[0] assert(argument !== undefined) return stripLoc(argument.value) } /** The decoded value of a string literal placed in argument position. */ export const stringValue = (literal: string): string => { const value = argumentValue(literal) assert.strictEqual(value._tag, "StringValue") return (value as Plain).value } // Plain node builders for expectations. export const name = (value: string): Plain => ({ _tag: "Name", value }) export const namedType = (value: string): Plain => ({ _tag: "NamedType", name: name(value) }) export const nonNull = (type: Plain): Plain => ({ _tag: "NonNullType", type }) export const list = (type: Plain): Plain => ({ _tag: "ListType", type }) export const str = (value: string): Plain => ({ _tag: "StringValue", value }) export const int = (value: string): Plain => ({ _tag: "IntValue", value }) export const argument = (argumentName: string, value: Plain): Plain => ({ _tag: "Argument", name: name(argumentName), value }) export const directive = ( directiveName: string, args: ReadonlyArray> = [] ): Plain => ({ _tag: "Directive", name: name(directiveName), arguments: args }) export const inputValue = ( valueName: string, type: Plain, options: { readonly description?: string readonly defaultValue?: Plain readonly directives?: ReadonlyArray> } = {} ): Plain => ({ _tag: "InputValueDefinition", ...(options.description === undefined ? {} : { description: str(options.description) }), name: name(valueName), type, ...(options.defaultValue === undefined ? {} : { defaultValue: options.defaultValue }), directives: options.directives ?? [] }) export const fieldDefinition = ( fieldName: string, type: Plain, options: { readonly description?: string readonly arguments?: ReadonlyArray> readonly directives?: ReadonlyArray> } = {} ): Plain => ({ _tag: "FieldDefinition", ...(options.description === undefined ? {} : { description: str(options.description) }), name: name(fieldName), arguments: options.arguments ?? [], type, directives: options.directives ?? [] })