@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
ts 69 lines 2.89 KB
Raw
1 import type * as Ast from "@effect/graphql-generator/internal/Ast"
2 import { print } from "@effect/graphql-generator/internal/Printer"
3 import { assert, describe, it } from "@effect/vitest"
4 import { readdirSync, readFileSync } from "node:fs"
5 import { parseOrThrow, stripLoc } from "./utils/ast.ts"
6
7 const operationsDirectory = new URL("./fixtures/github/operations/", import.meta.url)
8
9 const printed = (body: string) => print(parseOrThrow(body))
10
11 describe("Printer", () => {
12 it("an anonymous query collapses to the shorthand", () => {
13 assert.strictEqual(printed("query { a }"), "{a}")
14 })
15
16 it("an anonymous query with variables keeps the keyword", () => {
17 assert.strictEqual(printed("query ($a: Int) { f }"), "query($a:Int){f}")
18 })
19
20 it("is token-minimal: a space only where two non-punctuators would merge", () => {
21 assert.strictEqual(
22 printed("query Q($a: Int = 1, $b: [Int!]) @d { f(x: [1, -2], y: {}) ...F }"),
23 "query Q($a:Int=1$b:[Int!])@d{f(x:[1 -2]y:{})...F}"
24 )
25 })
26
27 it("prints fragment spreads, inline fragments and fragment definitions", () => {
28 assert.strictEqual(
29 printed("{ ...F @d ... on T { a } ... { b } } fragment F on T @e { alias: c }"),
30 "{...F@d...on T{a}...{b}}fragment F on T@e{alias:c}"
31 )
32 })
33
34 it("numbers keep their source text", () => {
35 assert.strictEqual(printed("{ f(a: -0, b: 2E+2) }"), "{f(a:-0 b:2E+2)}")
36 })
37
38 it("strings print as regular strings with escapes and upper-case control escapes", () => {
39 assert.strictEqual(
40 printed("{ f(s: \"a\\\"b\\\\c\\nd\\u0001\\u007fé\") }"),
41 "{f(s:\"a\\\"b\\\\c\\nd\\u0001\\u007Fé\")}"
42 )
43 })
44
45 it("block strings print dedented as regular strings", () => {
46 assert.strictEqual(printed("{ f(s: \"\"\"\n multi\n line\n \"\"\") }"), "{f(s:\"multi\\n line\")}")
47 })
48
49 it("descriptions on operations, variables and fragments are not printed", () => {
50 assert.strictEqual(
51 printed("\"docs\" query Q(\"x\" $x: Int) { a } \"f\" fragment F on T { a }"),
52 "query Q($x:Int){a}fragment F on T{a}"
53 )
54 })
55
56 it("prints a synthesised document of selected definitions in the order given", () => {
57 const parsed = parseOrThrow("query Q { ...B ...A } fragment A on T { a } fragment B on T { b }")
58 const [query, a, b] = parsed.definitions as ReadonlyArray<Ast.ExecutableDefinition>
59 const document: Ast.Document = { _tag: "Document", definitions: [query!, b!, a!], loc: { start: 0, end: 0 } }
60 assert.strictEqual(print(document), "query Q{...B...A}fragment B on T{b}fragment A on T{a}")
61 })
62
63 for (const file of readdirSync(operationsDirectory).filter((file) => file.endsWith(".graphql")).sort()) {
64 it(`GitHub operations ${file} reparse to the same AST`, () => {
65 const document = parseOrThrow(readFileSync(new URL(file, operationsDirectory), "utf8"), file)
66 assert.deepStrictEqual(stripLoc(parseOrThrow(print(document))), stripLoc(document))
67 })
68 }
69 })