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

main
ts 151 lines 5.45 KB
Raw
1 /**
2 * Shared helpers for the language front-end tests.
3 *
4 * Expectations are written as `Plain` nodes: the AST without `loc`, with
5 * `undefined` children omitted, so a test states only what the grammar says.
6 */
7 import type * as Ast from "@effect/graphql-generator/internal/Ast"
8 import type { Diagnostic, Source } from "@effect/graphql-generator/internal/Diagnostic"
9 import { parse } from "@effect/graphql-generator/internal/Parser"
10 import { assert } from "@effect/vitest"
11 import * as Result from "effect/Result"
12
13 type OptionalKeys<T> = { [K in keyof T]-?: undefined extends T[K] ? K : never }[keyof T]
14
15 export type Plain<T> = T extends ReadonlyArray<infer U> ? ReadonlyArray<Plain<U>>
16 : T extends object ?
17 & { readonly [K in Exclude<keyof T, OptionalKeys<T> | "loc">]: Plain<T[K]> }
18 & { readonly [K in Exclude<OptionalKeys<T>, "loc">]?: Plain<Exclude<T[K], undefined>> }
19 : T
20
21 export const source = (body: string, path = "test.graphql"): Source => ({ path, body })
22
23 const strip = (value: unknown): unknown => {
24 if (Array.isArray(value)) return value.map(strip)
25 if (value !== null && typeof value === "object") {
26 const out: Record<string, unknown> = {}
27 for (const [key, child] of Object.entries(value)) {
28 if (key === "loc" || child === undefined) continue
29 out[key] = strip(child)
30 }
31 return out
32 }
33 return value
34 }
35
36 export const stripLoc = <T>(node: T): Plain<T> => strip(node) as Plain<T>
37
38 export const formatDiagnostic = (diagnostic: Diagnostic): string =>
39 `${diagnostic.path}:${diagnostic.line}:${diagnostic.column} ${diagnostic.message}\n${diagnostic.codeFrame}`
40
41 export const parseOrThrow = (body: string, path?: string): Ast.Document => {
42 const result = parse(source(body, path))
43 if (Result.isFailure(result)) {
44 throw new Error(`unexpected diagnostic: ${formatDiagnostic(result.failure)}`)
45 }
46 return result.success
47 }
48
49 export const assertDefinitions = (body: string, expected: ReadonlyArray<Plain<Ast.Definition>>): void => {
50 assert.deepStrictEqual(stripLoc(parseOrThrow(body)).definitions, expected)
51 }
52
53 interface ExpectedDiagnostic {
54 readonly line: number
55 readonly column: number
56 readonly message: string
57 }
58
59 export const assertDiagnostic = (
60 body: string,
61 expected: ExpectedDiagnostic,
62 parser: (source: Source) => Result.Result<unknown, Diagnostic> = parse
63 ): void => {
64 const result = parser(source(body))
65 if (Result.isSuccess(result)) {
66 assert.fail(`expected a diagnostic for ${JSON.stringify(body)} but it parsed`)
67 }
68 const { column, line, message } = result.failure
69 assert.deepStrictEqual({ line, column, message }, expected)
70 }
71
72 /** Parses `{ f(s: <literal>) }` and returns the argument's value node. */
73 export const argumentValue = (literal: string): Plain<Ast.Value> => {
74 const document = parseOrThrow(`{ f(s: ${literal}) }`)
75 const operation = document.definitions[0]
76 assert(operation !== undefined && operation._tag === "OperationDefinition")
77 const field = operation.selectionSet.selections[0]
78 assert(field !== undefined && field._tag === "Field")
79 const argument = field.arguments[0]
80 assert(argument !== undefined)
81 return stripLoc(argument.value)
82 }
83
84 /** The decoded value of a string literal placed in argument position. */
85 export const stringValue = (literal: string): string => {
86 const value = argumentValue(literal)
87 assert.strictEqual(value._tag, "StringValue")
88 return (value as Plain<Ast.StringValue>).value
89 }
90
91 // Plain node builders for expectations.
92
93 export const name = (value: string): Plain<Ast.Name> => ({ _tag: "Name", value })
94
95 export const namedType = (value: string): Plain<Ast.NamedType> => ({ _tag: "NamedType", name: name(value) })
96
97 export const nonNull = (type: Plain<Ast.NamedType | Ast.ListType>): Plain<Ast.NonNullType> => ({
98 _tag: "NonNullType",
99 type
100 })
101
102 export const list = (type: Plain<Ast.Type>): Plain<Ast.ListType> => ({ _tag: "ListType", type })
103
104 export const str = (value: string): Plain<Ast.StringValue> => ({ _tag: "StringValue", value })
105
106 export const int = (value: string): Plain<Ast.IntValue> => ({ _tag: "IntValue", value })
107
108 export const argument = (argumentName: string, value: Plain<Ast.ConstValue>): Plain<Ast.ConstArgument> => ({
109 _tag: "Argument",
110 name: name(argumentName),
111 value
112 })
113
114 export const directive = (
115 directiveName: string,
116 args: ReadonlyArray<Plain<Ast.ConstArgument>> = []
117 ): Plain<Ast.ConstDirective> => ({ _tag: "Directive", name: name(directiveName), arguments: args })
118
119 export const inputValue = (
120 valueName: string,
121 type: Plain<Ast.Type>,
122 options: {
123 readonly description?: string
124 readonly defaultValue?: Plain<Ast.ConstValue>
125 readonly directives?: ReadonlyArray<Plain<Ast.ConstDirective>>
126 } = {}
127 ): Plain<Ast.InputValueDefinition> => ({
128 _tag: "InputValueDefinition",
129 ...(options.description === undefined ? {} : { description: str(options.description) }),
130 name: name(valueName),
131 type,
132 ...(options.defaultValue === undefined ? {} : { defaultValue: options.defaultValue }),
133 directives: options.directives ?? []
134 })
135
136 export const fieldDefinition = (
137 fieldName: string,
138 type: Plain<Ast.Type>,
139 options: {
140 readonly description?: string
141 readonly arguments?: ReadonlyArray<Plain<Ast.InputValueDefinition>>
142 readonly directives?: ReadonlyArray<Plain<Ast.ConstDirective>>
143 } = {}
144 ): Plain<Ast.FieldDefinition> => ({
145 _tag: "FieldDefinition",
146 ...(options.description === undefined ? {} : { description: str(options.description) }),
147 name: name(fieldName),
148 arguments: options.arguments ?? [],
149 type,
150 directives: options.directives ?? []
151 })