| 1 | import { describe, expect, it } from "vitest"; |
| 2 | import { parse } from "graphql"; |
| 3 | import { measure } from "../../src/http/graphql.js"; |
| 4 | import { randomAlnum } from "../../src/lib/util.js"; |
| 5 | |
| 6 | describe("GraphQL limits", () => { |
| 7 | it("counts fields and depth with fragments expanded where they're spread", () => { |
| 8 | const doc = parse(`fragment pr on PullRequest { number title commits(last: 1) { nodes { commit { oid } } } } |
| 9 | query { a: repository(owner: "o", name: "r") { p1: pullRequest(number: 1) { ...pr } p2: pullRequest(number: 2) { ...pr } } }`); |
| 10 | // repository + 2 × (pullRequest + number + title + commits + nodes + commit + oid) |
| 11 | expect(measure(doc)).toEqual({ fields: 15, depth: 6 }); |
| 12 | }); |
| 13 | |
| 14 | it("doesn't loop on a fragment that spreads itself", () => { |
| 15 | const doc = parse(`fragment f on Repository { id parent { ...f } } query { repository(owner: "o", name: "r") { ...f } }`); |
| 16 | expect(measure(doc).fields).toBe(3); |
| 17 | }); |
| 18 | }); |
| 19 | |
| 20 | describe("randomAlnum", () => { |
| 21 | it("draws letters and digits only, at the asked length", () => { |
| 22 | for (const n of [1, 40, 200]) expect(randomAlnum(n)).toMatch(new RegExp(`^[A-Za-z0-9]{${n}}$`)); |
| 23 | expect(new Set(Array.from({ length: 20 }, () => randomAlnum(40))).size).toBe(20); |
| 24 | }); |
| 25 | }); |