/*** IMPORT ------------------------------------------- ***/ import { expect, test } from "vitest"; /*** UTILITY ------------------------------------------ ***/ import { deriveTitle, parseOperations } from "../source/library/graphql/operations.ts"; /*** TESTS -------------------------------------------- ***/ test("parseOperations returns empty for blank query", () => { expect(parseOperations("")).toEqual([]); expect(parseOperations(" ")).toEqual([]); }); test("parseOperations returns empty on syntax error", () => { expect(parseOperations("query { ...")).toEqual([]); }); test("parseOperations captures a single named query", () => { const ops = parseOperations("query Foo { viewer { id } }"); expect(ops).toEqual([{ name: "Foo", type: "query" }]); }); test("parseOperations returns null name for anonymous ops", () => { const ops = parseOperations("{ viewer { id } }"); expect(ops).toEqual([{ name: null, type: "query" }]); }); test("parseOperations captures multiple operations", () => { const ops = parseOperations(` query Foo { a } mutation Bar { b } subscription Baz { c } `); expect(ops).toEqual([ { name: "Foo", type: "query" }, { name: "Bar", type: "mutation" }, { name: "Baz", type: "subscription" } ]); }); test("deriveTitle prefers the first operation name", () => { const ops = parseOperations("query Foo { a }"); expect(deriveTitle("query Foo { a }", ops)).toEqual("Foo"); }); test("deriveTitle falls back to operation type", () => { const ops = parseOperations("mutation { a }"); expect(deriveTitle("mutation { a }", ops)).toEqual("mutation"); }); test("deriveTitle falls back to the first 20 chars when unparsable", () => { const query = "this is not valid graphql at all"; expect(deriveTitle(query, parseOperations(query))).toEqual("this is not valid gr"); }); test("deriveTitle returns 'untitled' for empty input", () => { expect(deriveTitle("", [])).toEqual("untitled"); expect(deriveTitle(" ", [])).toEqual("untitled"); });