local Z=script.Parent.Parent.Parent.Parent local N=Z.Parent local l=require(N.Dev.JestGlobals)local d=l.describe local t=l.expect local B=l.it local w=require(N.LuauPolyfill)local b=w.Array local U=(require(N.GraphQLTag)).default local h=(require(script.Parent.Parent.Parent.common.cloneDeep)).cloneDeep local f=require(script.Parent.Parent.directives)local F=f.shouldInclude local D=f.hasDirectives local T=(require(script.Parent.Parent.getFromAST)).getQueryDefinition d("\104\097\115\068\105\114\101\099\116\105\118\101",function()B("\115\104\111\117\108\100 \097\108\108\111\119 \115\101\097\114\099\104\105\110\103 \116\104\101 \097\115\116 \102\111\114 \097 \100\105\114\101\099\116\105\118\101",function()local Z=U("\n      \113\117\101\114\121 \083\105\109\112\108\101 \123\n        \102\105\101\108\100 \064\108\105\118\101\n      \125\n    ");(t(D({"\108\105\118\101"},Z))).toBe(true);(t(D({"\100\101\102\101\114"},Z))).toBe(false)end)B("\119\111\114\107\115 \102\111\114 \097\108\108 \111\112\101\114\097\116\105\111\110 \116\121\112\101\115",function()local Z=U("\n      \123\n        \102\105\101\108\100 \064\108\105\118\101 \123\n          \115\117\098\070\105\101\108\100 \123\n            \104\101\108\108\111 \064\108\105\118\101\n          \125\n        \125\n      \125\n    ")local N=U("\n      \109\117\116\097\116\105\111\110 \068\105\114\101\099\116\105\118\101 \123\n        \109\117\116\097\116\101 \123\n          \102\105\101\108\100 \123\n            \115\117\098\070\105\101\108\100 \123\n              \104\101\108\108\111 \064\108\105\118\101\n            \125\n          \125\n        \125\n      \125\n    ")local l=U("\n      \115\117\098\115\099\114\105\112\116\105\111\110 \076\105\118\101\068\105\114\101\099\116\105\118\101 \123\n        \115\117\098 \123\n          \102\105\101\108\100 \123\n            \115\117\098\070\105\101\108\100 \123\n              \104\101\108\108\111 \064\108\105\118\101\n            \125\n          \125\n        \125\n      \125\n    ")b.forEach({Z;N;l},function(Z)(t(D({"\108\105\118\101"},Z))).toBe(true);(t(D({"\100\101\102\101\114"},Z))).toBe(false)end)end)B("\119\111\114\107\115 \102\111\114 \115\105\109\112\108\101 \102\114\097\103\109\101\110\116\115",function()local Z=U("\n      \113\117\101\114\121 \083\105\109\112\108\101 \123\n        \046\046\046\102\105\101\108\100\070\114\097\103\109\101\110\116\n      \125\n\n      \102\114\097\103\109\101\110\116 \102\105\101\108\100\070\114\097\103\109\101\110\116 \111\110 \070\105\101\108\100 \123\n        \102\111\111 \064\108\105\118\101\n      \125\n    ");(t(D({"\108\105\118\101"},Z))).toBe(true);(t(D({"\100\101\102\101\114"},Z))).toBe(false)end)B("\119\111\114\107\115 \102\111\114 \110\101\115\116\101\100 \102\114\097\103\109\101\110\116\115",function()local Z=U("\n      \113\117\101\114\121 \083\105\109\112\108\101 \123\n        \046\046\046\102\105\101\108\100\070\114\097\103\109\101\110\116\049\n      \125\n\n      \102\114\097\103\109\101\110\116 \102\105\101\108\100\070\114\097\103\109\101\110\116\049 \111\110 \070\105\101\108\100 \123\n        \098\097\114 \123\n          \098\097\122 \123\n            \046\046\046\110\101\115\116\101\100\070\114\097\103\109\101\110\116\n          \125\n        \125\n      \125\n\n      \102\114\097\103\109\101\110\116 \110\101\115\116\101\100\070\114\097\103\109\101\110\116 \111\110 \070\105\101\108\100 \123\n        \102\111\111 \064\108\105\118\101\n      \125\n    ");(t(D({"\108\105\118\101"},Z))).toBe(true);(t(D({"\100\101\102\101\114"},Z))).toBe(false)end)end)d("\115\104\111\117\108\100\073\110\099\108\117\100\101",function()B("\115\104\111\117\108\100 \115\104\111\117\108\100 \110\111\116 \105\110\099\108\117\100\101 \097 \115\107\105\112\112\101\100 \102\105\101\108\100",function()local Z=U("\n      \113\117\101\114\121 \123\n        \102\111\114\116\117\110\101\067\111\111\107\105\101 \064\115\107\105\112\040\105\102\058 \116\114\117\101\041\n      \125\n    ")local N=(T(Z)).selectionSet.selections[1];(t(not F(N,{}))).toBe(true)end)B("\115\104\111\117\108\100 \105\110\099\108\117\100\101 \097\110 \105\110\099\108\117\100\101\100 \102\105\101\108\100",function()local Z=U("\n      \113\117\101\114\121 \123\n        \102\111\114\116\117\110\101\067\111\111\107\105\101 \064\105\110\099\108\117\100\101\040\105\102\058 \116\114\117\101\041\n      \125\n    ")local N=(T(Z)).selectionSet.selections[1];(t(F(N,{}))).toBe(true)end)B("\115\104\111\117\108\100 \110\111\116 \105\110\099\108\117\100\101 \097 \110\111\116 \105\110\099\108\117\100\101\058 \102\097\108\115\101 \102\105\101\108\100",function()local Z=U("\n      \113\117\101\114\121 \123\n        \102\111\114\116\117\110\101\067\111\111\107\105\101 \064\105\110\099\108\117\100\101\040\105\102\058 \102\097\108\115\101\041\n      \125\n    ")local N=(T(Z)).selectionSet.selections[1];(t(not F(N,{}))).toBe(true)end)B("\115\104\111\117\108\100 \105\110\099\108\117\100\101 \097 \115\107\105\112\058 \102\097\108\115\101 \102\105\101\108\100",function()local Z=U("\n      \113\117\101\114\121 \123\n        \102\111\114\116\117\110\101\067\111\111\107\105\101 \064\115\107\105\112\040\105\102\058 \102\097\108\115\101\041\n      \125\n    ")local N=(T(Z)).selectionSet.selections[1];(t(F(N,{}))).toBe(true)end)B("\115\104\111\117\108\100 \110\111\116 \105\110\099\108\117\100\101 \097 \102\105\101\108\100 \105\102 \115\107\105\112\058 \116\114\117\101 \097\110\100 \105\110\099\108\117\100\101\058 \116\114\117\101",function()local Z=U("\n      \113\117\101\114\121 \123\n        \102\111\114\116\117\110\101\067\111\111\107\105\101 \064\115\107\105\112\040\105\102\058 \116\114\117\101\041 \064\105\110\099\108\117\100\101\040\105\102\058 \116\114\117\101\041\n      \125\n    ")local N=(T(Z)).selectionSet.selections[1];(t(not F(N,{}))).toBe(true)end)B("\115\104\111\117\108\100 \110\111\116 \105\110\099\108\117\100\101 \097 \102\105\101\108\100 \105\102 \115\107\105\112\058 \116\114\117\101 \097\110\100 \105\110\099\108\117\100\101\058 \102\097\108\115\101",function()local Z=U("\n      \113\117\101\114\121 \123\n        \102\111\114\116\117\110\101\067\111\111\107\105\101 \064\115\107\105\112\040\105\102\058 \116\114\117\101\041 \064\105\110\099\108\117\100\101\040\105\102\058 \102\097\108\115\101\041\n      \125\n    ")local N=(T(Z)).selectionSet.selections[1];(t(not F(N,{}))).toBe(true)end)B("\115\104\111\117\108\100 \105\110\099\108\117\100\101 \097 \102\105\101\108\100 \105\102 \115\107\105\112\058 \102\097\108\115\101 \097\110\100 \105\110\099\108\117\100\101\058 \116\114\117\101",function()local Z=U("\n      \113\117\101\114\121 \123\n        \102\111\114\116\117\110\101\067\111\111\107\105\101 \064\115\107\105\112\040\105\102\058 \102\097\108\115\101\041 \064\105\110\099\108\117\100\101\040\105\102\058 \116\114\117\101\041\n      \125\n    ")local N=(T(Z)).selectionSet.selections[1];(t(F(N,{}))).toBe(true)end)B("\115\104\111\117\108\100 \110\111\116 \105\110\099\108\117\100\101 \097 \102\105\101\108\100 \105\102 \115\107\105\112\058 \102\097\108\115\101 \097\110\100 \105\110\099\108\117\100\101\058 \102\097\108\115\101",function()local Z=U("\n      \113\117\101\114\121 \123\n        \102\111\114\116\117\110\101\067\111\111\107\105\101 \064\115\107\105\112\040\105\102\058 \102\097\108\115\101\041 \064\105\110\099\108\117\100\101\040\105\102\058 \102\097\108\115\101\041\n      \125\n    ")local N=(T(Z)).selectionSet.selections[1];(t(not F(N,{}))).toBe(true)end)B("\115\104\111\117\108\100 \108\101\097\118\101 \116\104\101 \111\114\105\103\105\110\097\108 \113\117\101\114\121 \117\110\109\111\100\105\102\105\101\100",function()local Z=U("\n      \113\117\101\114\121 \123\n        \102\111\114\116\117\110\101\067\111\111\107\105\101 \064\115\107\105\112\040\105\102\058 \102\097\108\115\101\041 \064\105\110\099\108\117\100\101\040\105\102\058 \102\097\108\115\101\041\n      \125\n    ")local N=h(Z)local l=(T(Z)).selectionSet.selections[1]F(l,{});(t(Z)).toEqual(N)end)B("\100\111\101\115 \110\111\116 \116\104\114\111\119 \097\110 \101\114\114\111\114 \111\110 \097\110 \117\110\115\117\112\112\111\114\116\101\100 \100\105\114\101\099\116\105\118\101",function()local Z=U("\n      \113\117\101\114\121 \123\n        \102\111\114\116\117\110\101\067\111\111\107\105\101 \064\100\111\115\111\109\101\116\104\105\110\103\040\105\102\058 \116\114\117\101\041\n      \125\n    ")local N=(T(Z)).selectionSet.selections[1];(t(function()F(N,{})end)).never.toThrow()end)B("\116\104\114\111\119\115 \097\110 \101\114\114\111\114 \111\110 \097\110 \105\110\118\097\108\105\100 \097\114\103\117\109\101\110\116 \102\111\114 \116\104\101 \115\107\105\112 \100\105\114\101\099\116\105\118\101",function()local Z=U("\n      \113\117\101\114\121 \123\n        \102\111\114\116\117\110\101\067\111\111\107\105\101 \064\115\107\105\112\040\110\111\116\104\105\110\103\058 \116\114\117\101\041\n      \125\n    ")local N=(T(Z)).selectionSet.selections[1];(t(function()F(N,{})end)).toThrow()end)B("\116\104\114\111\119\115 \097\110 \101\114\114\111\114 \111\110 \097\110 \105\110\118\097\108\105\100 \097\114\103\117\109\101\110\116 \102\111\114 \116\104\101 \105\110\099\108\117\100\101 \100\105\114\101\099\116\105\118\101",function()local Z=U("\n      \113\117\101\114\121 \123\n        \102\111\114\116\117\110\101\067\111\111\107\105\101 \064\105\110\099\108\117\100\101\040\110\111\116\104\105\110\103\058 \116\114\117\101\041\n      \125\n    ")local N=(T(Z)).selectionSet.selections[1];(t(function()F(N,{})end)).toThrow()end)B("\116\104\114\111\119\115 \097\110 \101\114\114\111\114 \111\110 \097\110 \105\110\118\097\108\105\100 \118\097\114\105\097\098\108\101 \110\097\109\101 \119\105\116\104\105\110 \097 \100\105\114\101\099\116\105\118\101 \097\114\103\117\109\101\110\116",function()local Z=U("\n      \113\117\101\114\121 \123\n        \102\111\114\116\117\110\101\067\111\111\107\105\101 \064\105\110\099\108\117\100\101\040\105\102\058 \036\110\101\118\101\114\068\101\102\105\110\101\100\041\n      \125\n    ")local N=(T(Z)).selectionSet.selections[1];(t(function()F(N,{})end)).toThrow()end)B("\101\118\097\108\117\097\116\101\115 \118\097\114\105\097\098\108\101\115 \111\110 \115\107\105\112 \102\105\101\108\100\115",function()local Z=U("\n      \113\117\101\114\121\040\036\115\104\111\117\108\100\083\107\105\112\058 \066\111\111\108\101\097\110\041 \123\n        \102\111\114\116\117\110\101\067\111\111\107\105\101 \064\115\107\105\112\040\105\102\058 \036\115\104\111\117\108\100\083\107\105\112\041\n      \125\n    ")local N={shouldSkip=true}local l=(T(Z)).selectionSet.selections[1];(t(not F(l,N))).toBe(true)end)B("\101\118\097\108\117\097\116\101\115 \118\097\114\105\097\098\108\101\115 \111\110 \105\110\099\108\117\100\101 \102\105\101\108\100\115",function()local Z=U("\n      \113\117\101\114\121\040\036\115\104\111\117\108\100\083\107\105\112\058 \066\111\111\108\101\097\110\041 \123\n        \102\111\114\116\117\110\101\067\111\111\107\105\101 \064\105\110\099\108\117\100\101\040\105\102\058 \036\115\104\111\117\108\100\073\110\099\108\117\100\101\041\n      \125\n    ")local N={shouldInclude=false}local l=(T(Z)).selectionSet.selections[1];(t(not F(l,N))).toBe(true)end)B("\116\104\114\111\119\115 \097\110 \101\114\114\111\114 \105\102 \116\104\101 \118\097\108\117\101 \111\102 \116\104\101 \097\114\103\117\109\101\110\116 \105\115 \110\111\116 \097 \118\097\114\105\097\098\108\101 \111\114 \098\111\111\108\101\097\110",function()local Z=U("\n      \113\117\101\114\121 \123\n        \102\111\114\116\117\110\101\067\111\111\107\105\101 \064\105\110\099\108\117\100\101\040\105\102\058 \"\115\116\114\105\110\103\"\041\n      \125\n    ")local N=(T(Z)).selectionSet.selections[1];(t(function()F(N,{})end)).toThrow()end)end)return{}
--[[
 * Copyright (c) 2021 Apollo Graph, Inc. (Formerly Meteor Development Group, Inc.)
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
]]
-- ROBLOX upstream: https://github.com/apollographql/apollo-client/blob/v3.4.2/src/utilities/graphql/__tests__/directives.ts

local srcWorkspace = script.Parent.Parent.Parent.Parent
local rootWorkspace = srcWorkspace.Parent

local JestGlobals = require(rootWorkspace.Dev.JestGlobals)
local describe = JestGlobals.describe
local expect = JestGlobals.expect
local it = JestGlobals.it

local LuauPolyfill = require(rootWorkspace.LuauPolyfill)
local Array = LuauPolyfill.Array

local gql = require(rootWorkspace.GraphQLTag).default

local cloneDeep = require(script.Parent.Parent.Parent.common.cloneDeep).cloneDeep

local directivesModule = require(script.Parent.Parent.directives)
local shouldInclude = directivesModule.shouldInclude
local hasDirectives = directivesModule.hasDirectives
local getQueryDefinition = require(script.Parent.Parent.getFromAST).getQueryDefinition

describe("hasDirective", function()
	it("should allow searching the ast for a directive", function()
		local query = gql([[

      query Simple {
        field @live
      }
    ]])

		expect(hasDirectives({ "live" }, query)).toBe(true)
		expect(hasDirectives({ "defer" }, query)).toBe(false)
	end)

	it("works for all operation types", function()
		local query = gql([[

      {
        field @live {
          subField {
            hello @live
          }
        }
      }
    ]])

		local mutation = gql([[

      mutation Directive {
        mutate {
          field {
            subField {
              hello @live
            }
          }
        }
      }
    ]])

		local subscription = gql([[

      subscription LiveDirective {
        sub {
          field {
            subField {
              hello @live
            }
          }
        }
      }
    ]])

		Array.forEach({ query, mutation, subscription }, function(x)
			expect(hasDirectives({ "live" }, x)).toBe(true)
			expect(hasDirectives({ "defer" }, x)).toBe(false)
		end)
	end)

	it("works for simple fragments", function()
		local query = gql([[

      query Simple {
        ...fieldFragment
      }

      fragment fieldFragment on Field {
        foo @live
      }
    ]])
		expect(hasDirectives({ "live" }, query)).toBe(true)
		expect(hasDirectives({ "defer" }, query)).toBe(false)
	end)

	it("works for nested fragments", function()
		local query = gql([[

      query Simple {
        ...fieldFragment1
      }

      fragment fieldFragment1 on Field {
        bar {
          baz {
            ...nestedFragment
          }
        }
      }

      fragment nestedFragment on Field {
        foo @live
      }
    ]])
		expect(hasDirectives({ "live" }, query)).toBe(true)
		expect(hasDirectives({ "defer" }, query)).toBe(false)
	end)
end)

describe("shouldInclude", function()
	it("should should not include a skipped field", function()
		local query = gql([[

      query {
        fortuneCookie @skip(if: true)
      }
    ]])
		local field = getQueryDefinition(query).selectionSet.selections[1]
		expect(not shouldInclude(field, {})).toBe(true)
	end)

	it("should include an included field", function()
		local query = gql([[

      query {
        fortuneCookie @include(if: true)
      }
    ]])

		local field = getQueryDefinition(query).selectionSet.selections[1]
		expect(shouldInclude(field, {})).toBe(true)
	end)

	it("should not include a not include: false field", function()
		local query = gql([[

      query {
        fortuneCookie @include(if: false)
      }
    ]])

		local field = getQueryDefinition(query).selectionSet.selections[1]
		expect(not shouldInclude(field, {})).toBe(true)
	end)

	it("should include a skip: false field", function()
		local query = gql([[

      query {
        fortuneCookie @skip(if: false)
      }
    ]])

		local field = getQueryDefinition(query).selectionSet.selections[1]
		expect(shouldInclude(field, {})).toBe(true)
	end)

	it("should not include a field if skip: true and include: true", function()
		local query = gql([[

      query {
        fortuneCookie @skip(if: true) @include(if: true)
      }
    ]])

		local field = getQueryDefinition(query).selectionSet.selections[1]
		expect(not shouldInclude(field, {})).toBe(true)
	end)

	it("should not include a field if skip: true and include: false", function()
		local query = gql([[

      query {
        fortuneCookie @skip(if: true) @include(if: false)
      }
    ]])
		local field = getQueryDefinition(query).selectionSet.selections[1]
		expect(not shouldInclude(field, {})).toBe(true)
	end)

	it("should include a field if skip: false and include: true", function()
		local query = gql([[

      query {
        fortuneCookie @skip(if: false) @include(if: true)
      }
    ]])

		local field = getQueryDefinition(query).selectionSet.selections[1]

		expect(shouldInclude(field, {})).toBe(true)
	end)

	it("should not include a field if skip: false and include: false", function()
		local query = gql([[

      query {
        fortuneCookie @skip(if: false) @include(if: false)
      }
    ]])
		local field = getQueryDefinition(query).selectionSet.selections[1]
		expect(not shouldInclude(field, {})).toBe(true)
	end)

	it("should leave the original query unmodified", function()
		local query = gql([[

      query {
        fortuneCookie @skip(if: false) @include(if: false)
      }
    ]])
		local queryClone = cloneDeep(query)
		local field = getQueryDefinition(query).selectionSet.selections[1]
		shouldInclude(field, {})
		expect(query).toEqual(queryClone)
	end)

	it("does not throw an error on an unsupported directive", function()
		local query = gql([[

      query {
        fortuneCookie @dosomething(if: true)
      }
    ]])
		local field = getQueryDefinition(query).selectionSet.selections[1]
		expect(function()
			shouldInclude(field, {})
		end).never.toThrow()
	end)

	it("throws an error on an invalid argument for the skip directive", function()
		local query = gql([[

      query {
        fortuneCookie @skip(nothing: true)
      }
    ]])
		local field = getQueryDefinition(query).selectionSet.selections[1]
		expect(function()
			shouldInclude(field, {})
		end).toThrow()
	end)

	it("throws an error on an invalid argument for the include directive", function()
		local query = gql([[

      query {
        fortuneCookie @include(nothing: true)
      }
    ]])
		local field = getQueryDefinition(query).selectionSet.selections[1]
		expect(function()
			shouldInclude(field, {})
		end).toThrow()
	end)

	it("throws an error on an invalid variable name within a directive argument", function()
		local query = gql([[

      query {
        fortuneCookie @include(if: $neverDefined)
      }
    ]])
		local field = getQueryDefinition(query).selectionSet.selections[1]
		expect(function()
			shouldInclude(field, {})
		end).toThrow()
	end)

	it("evaluates variables on skip fields", function()
		local query = gql([[

      query($shouldSkip: Boolean) {
        fortuneCookie @skip(if: $shouldSkip)
      }
    ]])
		local variables = { shouldSkip = true }
		local field = getQueryDefinition(query).selectionSet.selections[1]
		expect(not shouldInclude(field, variables)).toBe(true)
	end)

	it("evaluates variables on include fields", function()
		local query = gql([[

      query($shouldSkip: Boolean) {
        fortuneCookie @include(if: $shouldInclude)
      }
    ]])
		local variables = { shouldInclude = false }
		local field = getQueryDefinition(query).selectionSet.selections[1]
		expect(not shouldInclude(field, variables)).toBe(true)
	end)

	it("throws an error if the value of the argument is not a variable or boolean", function()
		local query = gql([[

      query {
        fortuneCookie @include(if: "string")
      }
    ]])
		local field = getQueryDefinition(query).selectionSet.selections[1]
		expect(function()
			shouldInclude(field, {})
		end).toThrow()
	end)
end)

return {}
