local e=script.Parent local Q=e.Parent local U=require(Q.Flags)local P=require(e.deserializeRepository)return function()afterEach(function()U.reset()end)it("\115\104\111\117\108\100 \109\097\107\101 \110\111 \099\104\097\110\103\101\115 \105\102 \116\104\101 \115\101\116 \111\102 \102\108\097\103\115 \105\115 \101\109\112\116\121",function()local e={TestFlag={config={active=true,retired=false,ruleSets={}},version=1}}local Q={};(expect(U.exists("\084\101\115\116\070\108\097\103"))).to.equal(false)P(e,Q);(expect(U.exists("\084\101\115\116\070\108\097\103"))).to.equal(false)end)it("\115\104\111\117\108\100 \099\114\101\097\116\101 \097 \102\108\097\103 \105\102 \105\116 \100\111\101\115 \110\111\116 \101\120\105\115\116",function()local e={TestFlag={config={active=true;retired=false;ruleSets={}};version=1}}local Q={TestFlag=1};(expect(U.exists("\084\101\115\116\070\108\097\103"))).to.equal(false)P(e,Q);(expect(U.exists("\084\101\115\116\070\108\097\103"))).to.equal(true)local m=U.read("\084\101\115\116\070\108\097\103");(expect(m.active)).to.equal(true);(expect(m.retired)).to.equal(false);(expect(m.ruleSets)).to.be.ok()end)it("\115\104\111\117\108\100 \117\112\100\097\116\101 \097 \102\108\097\103 \105\102 \105\116 \101\120\105\115\116\115",function()local e={config={active=true;retired=false;ruleSets={}};version=1}local Q={config={active=false,retired=true;ruleSets={}},version=2}local m={TestFlag=e}local g={TestFlag=1};(expect(U.exists("\084\101\115\116\070\108\097\103"))).to.equal(false)P(m,g);(expect(U.exists("\084\101\115\116\070\108\097\103"))).to.equal(true)local X=U.read("\084\101\115\116\070\108\097\103");(expect(X.active)).to.equal(true);(expect(X.retired)).to.equal(false);(expect(X.ruleSets)).to.be.ok()local f={TestFlag=Q}local x={TestFlag=2}P(f,x);(expect(U.exists("\084\101\115\116\070\108\097\103"))).to.equal(true)X=U.read("\084\101\115\116\070\108\097\103");(expect(X.active)).to.equal(false);(expect(X.retired)).to.equal(true);(expect(X.ruleSets)).to.be.ok()end)it("\115\104\111\117\108\100 \101\114\114\111\114 \105\102 \116\104\101 \102\108\097\103 \100\111\101\115 \110\111\116 \101\120\105\115\116 \105\110 \116\104\101 \114\101\112\111\115\105\116\111\114\121",function()local e={}local Q={TestFlag=1};(expect(function()P(e,Q)end)).to.throw("\070\108\097\103 \'\084\101\115\116\070\108\097\103\' \110\111\116 \102\111\117\110\100 \105\110 \114\101\112\111\115\105\116\111\114\121\046")end)it("\115\104\111\117\108\100 \099\114\101\097\116\101 \109\097\110\121 \110\101\119 \102\108\097\103\115 \105\102 \116\104\101\121 \100\111 \110\111\116 \101\120\105\115\116",function()local e={TestFlag1={config={active=true,retired=false,ruleSets={}};version=1};TestFlag2={config={active=true,retired=false;ruleSets={}},version=1},TestFlag3={config={active=true,retired=false,ruleSets={}},version=1}}local Q={TestFlag1=1,TestFlag2=1,TestFlag3=1};(expect(U.exists("\084\101\115\116\070\108\097\103\049"))).to.equal(false);(expect(U.exists("\084\101\115\116\070\108\097\103\050"))).to.equal(false);(expect(U.exists("\084\101\115\116\070\108\097\103\051"))).to.equal(false)P(e,Q);(expect(U.exists("\084\101\115\116\070\108\097\103\049"))).to.equal(true);(expect(U.exists("\084\101\115\116\070\108\097\103\050"))).to.equal(true);(expect(U.exists("\084\101\115\116\070\108\097\103\051"))).to.equal(true)local m=U.read("\084\101\115\116\070\108\097\103\049");(expect(m.active)).to.equal(true);(expect(m.retired)).to.equal(false);(expect(m.ruleSets)).to.be.ok()m=U.read("\084\101\115\116\070\108\097\103\050");(expect(m.active)).to.equal(true);(expect(m.retired)).to.equal(false);(expect(m.ruleSets)).to.be.ok()m=U.read("\084\101\115\116\070\108\097\103\051");(expect(m.active)).to.equal(true);(expect(m.retired)).to.equal(false);(expect(m.ruleSets)).to.be.ok()end)it("\115\104\111\117\108\100 \117\112\100\097\116\101 \109\097\110\121 \102\108\097\103\115 \105\102 \116\104\101\121 \101\120\105\115\116",function()local e={config={active=true,retired=false;ruleSets={}};version=1}local Q={config={active=false,retired=true,ruleSets={}};version=2}local m={config={active=true;retired=false,ruleSets={}},version=1}local g={config={active=false;retired=true,ruleSets={}},version=2}local X={config={active=true;retired=false;ruleSets={}},version=1}local f={config={active=false,retired=true,ruleSets={}},version=2}local x={TestFlag1=e,TestFlag2=m;TestFlag3=X}local q={TestFlag1=1;TestFlag2=1;TestFlag3=1};(expect(U.exists("\084\101\115\116\070\108\097\103\049"))).to.equal(false);(expect(U.exists("\084\101\115\116\070\108\097\103\050"))).to.equal(false);(expect(U.exists("\084\101\115\116\070\108\097\103\051"))).to.equal(false)P(x,q);(expect(U.exists("\084\101\115\116\070\108\097\103\049"))).to.equal(true);(expect(U.exists("\084\101\115\116\070\108\097\103\050"))).to.equal(true);(expect(U.exists("\084\101\115\116\070\108\097\103\051"))).to.equal(true)local Z=U.read("\084\101\115\116\070\108\097\103\049");(expect(Z.active)).to.equal(true);(expect(Z.retired)).to.equal(false);(expect(Z.ruleSets)).to.be.ok()Z=U.read("\084\101\115\116\070\108\097\103\050");(expect(Z.active)).to.equal(true);(expect(Z.retired)).to.equal(false);(expect(Z.ruleSets)).to.be.ok()Z=U.read("\084\101\115\116\070\108\097\103\051");(expect(Z.active)).to.equal(true);(expect(Z.retired)).to.equal(false);(expect(Z.ruleSets)).to.be.ok()local A={TestFlag1=Q,TestFlag2=g;TestFlag3=f}local Y={TestFlag1=2,TestFlag2=2,TestFlag3=2}P(A,Y);(expect(U.exists("\084\101\115\116\070\108\097\103\049"))).to.equal(true);(expect(U.exists("\084\101\115\116\070\108\097\103\050"))).to.equal(true);(expect(U.exists("\084\101\115\116\070\108\097\103\051"))).to.equal(true)Z=U.read("\084\101\115\116\070\108\097\103\049");(expect(Z.active)).to.equal(false);(expect(Z.retired)).to.equal(true);(expect(Z.ruleSets)).to.be.ok()Z=U.read("\084\101\115\116\070\108\097\103\050");(expect(Z.active)).to.equal(false);(expect(Z.retired)).to.equal(true);(expect(Z.ruleSets)).to.be.ok()Z=U.read("\084\101\115\116\070\108\097\103\051");(expect(Z.active)).to.equal(false);(expect(Z.retired)).to.equal(true);(expect(Z.ruleSets)).to.be.ok()end)it("\115\104\111\117\108\100 \117\112\100\097\116\101 \102\108\097\103\115 \097\110\100 \099\114\101\097\116\101 \110\101\119 \102\108\097\103\115 \097\116 \116\104\101 \115\097\109\101 \116\105\109\101",function()local e={config={active=true;retired=false,ruleSets={}},version=1}local Q={config={active=false;retired=true;ruleSets={}};version=2}local m={config={active=true;retired=false;ruleSets={}},version=1}local g={config={active=false,retired=true,ruleSets={}},version=2}local X={config={active=true,retired=false;ruleSets={}},version=1}local f={TestFlag1=e;TestFlag2=m}local x={TestFlag1=1;TestFlag2=1};(expect(U.exists("\084\101\115\116\070\108\097\103\049"))).to.equal(false);(expect(U.exists("\084\101\115\116\070\108\097\103\050"))).to.equal(false);(expect(U.exists("\084\101\115\116\070\108\097\103\051"))).to.equal(false)P(f,x);(expect(U.exists("\084\101\115\116\070\108\097\103\049"))).to.equal(true);(expect(U.exists("\084\101\115\116\070\108\097\103\050"))).to.equal(true);(expect(U.exists("\084\101\115\116\070\108\097\103\051"))).to.equal(false)local q=U.read("\084\101\115\116\070\108\097\103\049");(expect(q.active)).to.equal(true);(expect(q.retired)).to.equal(false);(expect(q.ruleSets)).to.be.ok()q=U.read("\084\101\115\116\070\108\097\103\050");(expect(q.active)).to.equal(true);(expect(q.retired)).to.equal(false);(expect(q.ruleSets)).to.be.ok()local Z={TestFlag1=Q,TestFlag2=g,TestFlag3=X}local A={TestFlag1=2,TestFlag2=2,TestFlag3=1}P(Z,A);(expect(U.exists("\084\101\115\116\070\108\097\103\049"))).to.equal(true);(expect(U.exists("\084\101\115\116\070\108\097\103\050"))).to.equal(true);(expect(U.exists("\084\101\115\116\070\108\097\103\051"))).to.equal(true)q=U.read("\084\101\115\116\070\108\097\103\049");(expect(q.active)).to.equal(false);(expect(q.retired)).to.equal(true);(expect(q.ruleSets)).to.be.ok()q=U.read("\084\101\115\116\070\108\097\103\050");(expect(q.active)).to.equal(false);(expect(q.retired)).to.equal(true);(expect(q.ruleSets)).to.be.ok()q=U.read("\084\101\115\116\070\108\097\103\051");(expect(q.active)).to.equal(true);(expect(q.retired)).to.equal(false);(expect(q.ruleSets)).to.be.ok()end)end
local Serialization = script.Parent
local Package = Serialization.Parent
local Flags = require(Package.Flags)
local deserializeRepository = require(Serialization.deserializeRepository)

return function()
	afterEach(function()
		Flags.reset()
	end)

	it("should make no changes if the set of flags is empty", function()
		local repository = {
			["TestFlag"] = {
				config = {
					active = true,
					retired = false,
					ruleSets = {},
				},
				version = 1,
			},
		}
		local flags = {}

		expect(Flags.exists("TestFlag")).to.equal(false)

		deserializeRepository(repository, flags)

		expect(Flags.exists("TestFlag")).to.equal(false)
	end)

	it("should create a flag if it does not exist", function()
		local repository = {
			["TestFlag"] = {
				config = {
					active = true,
					retired = false,
					ruleSets = {},
				},
				version = 1,
			},
		}
		local flags = {
			["TestFlag"] = 1,
		}

		expect(Flags.exists("TestFlag")).to.equal(false)

		deserializeRepository(repository, flags)

		expect(Flags.exists("TestFlag")).to.equal(true)

		local flagConfig = Flags.read("TestFlag")
		expect(flagConfig.active).to.equal(true)
		expect(flagConfig.retired).to.equal(false)
		expect(flagConfig.ruleSets).to.be.ok()
	end)

	it("should update a flag if it exists", function()
		local flag1 = {
			config = {
				active = true,
				retired = false,
				ruleSets = {},
			},
			version = 1,
		}

		local flag2 = {
			config = {
				active = false,
				retired = true,
				ruleSets = {},
			},
			version = 2,
		}

		local repository1 = {
			["TestFlag"] = flag1,
		}
		local flags1 = {
			["TestFlag"] = 1,
		}

		expect(Flags.exists("TestFlag")).to.equal(false)

		deserializeRepository(repository1, flags1)

		expect(Flags.exists("TestFlag")).to.equal(true)

		local flagConfig = Flags.read("TestFlag")
		expect(flagConfig.active).to.equal(true)
		expect(flagConfig.retired).to.equal(false)
		expect(flagConfig.ruleSets).to.be.ok()

		local repository2 = {
			["TestFlag"] = flag2,
		}

		local flags2 = {
			["TestFlag"] = 2,
		}

		deserializeRepository(repository2, flags2)

		expect(Flags.exists("TestFlag")).to.equal(true)

		flagConfig = Flags.read("TestFlag")
		expect(flagConfig.active).to.equal(false)
		expect(flagConfig.retired).to.equal(true)
		expect(flagConfig.ruleSets).to.be.ok()
	end)

	it("should error if the flag does not exist in the repository", function()
		local repository = {}
		local flags = {
			["TestFlag"] = 1,
		}

		expect(function()
			deserializeRepository(repository, flags)
		end).to.throw("Flag 'TestFlag' not found in repository.")
	end)

	it("should create many new flags if they do not exist", function()
		local repository = {
			["TestFlag1"] = {
				config = {
					active = true,
					retired = false,
					ruleSets = {},
				},
				version = 1,
			},
			["TestFlag2"] = {
				config = {
					active = true,
					retired = false,
					ruleSets = {},
				},
				version = 1,
			},
			["TestFlag3"] = {
				config = {
					active = true,
					retired = false,
					ruleSets = {},
				},
				version = 1,
			},
		}
		local flags = {
			["TestFlag1"] = 1,
			["TestFlag2"] = 1,
			["TestFlag3"] = 1,
		}

		expect(Flags.exists("TestFlag1")).to.equal(false)
		expect(Flags.exists("TestFlag2")).to.equal(false)
		expect(Flags.exists("TestFlag3")).to.equal(false)

		deserializeRepository(repository, flags)

		expect(Flags.exists("TestFlag1")).to.equal(true)
		expect(Flags.exists("TestFlag2")).to.equal(true)
		expect(Flags.exists("TestFlag3")).to.equal(true)

		local flagConfig = Flags.read("TestFlag1")
		expect(flagConfig.active).to.equal(true)
		expect(flagConfig.retired).to.equal(false)
		expect(flagConfig.ruleSets).to.be.ok()

		flagConfig = Flags.read("TestFlag2")
		expect(flagConfig.active).to.equal(true)
		expect(flagConfig.retired).to.equal(false)
		expect(flagConfig.ruleSets).to.be.ok()

		flagConfig = Flags.read("TestFlag3")
		expect(flagConfig.active).to.equal(true)
		expect(flagConfig.retired).to.equal(false)
		expect(flagConfig.ruleSets).to.be.ok()
	end)

	it("should update many flags if they exist", function()
		local flag1v1 = {
			config = {
				active = true,
				retired = false,
				ruleSets = {},
			},
			version = 1,
		}

		local flag1v2 = {
			config = {
				active = false,
				retired = true,
				ruleSets = {},
			},
			version = 2,
		}

		local flag2v1 = {
			config = {
				active = true,
				retired = false,
				ruleSets = {},
			},
			version = 1,
		}

		local flag2v2 = {
			config = {
				active = false,
				retired = true,
				ruleSets = {},
			},
			version = 2,
		}

		local flag3v1 = {
			config = {
				active = true,
				retired = false,
				ruleSets = {},
			},
			version = 1,
		}

		local flag3v2 = {
			config = {
				active = false,
				retired = true,
				ruleSets = {},
			},
			version = 2,
		}

		local repository1 = {
			["TestFlag1"] = flag1v1,
			["TestFlag2"] = flag2v1,
			["TestFlag3"] = flag3v1,
		}
		local flags1 = {
			["TestFlag1"] = 1,
			["TestFlag2"] = 1,
			["TestFlag3"] = 1,
		}

		expect(Flags.exists("TestFlag1")).to.equal(false)
		expect(Flags.exists("TestFlag2")).to.equal(false)
		expect(Flags.exists("TestFlag3")).to.equal(false)

		deserializeRepository(repository1, flags1)

		expect(Flags.exists("TestFlag1")).to.equal(true)
		expect(Flags.exists("TestFlag2")).to.equal(true)
		expect(Flags.exists("TestFlag3")).to.equal(true)

		local flagConfig = Flags.read("TestFlag1")
		expect(flagConfig.active).to.equal(true)
		expect(flagConfig.retired).to.equal(false)
		expect(flagConfig.ruleSets).to.be.ok()

		flagConfig = Flags.read("TestFlag2")
		expect(flagConfig.active).to.equal(true)
		expect(flagConfig.retired).to.equal(false)
		expect(flagConfig.ruleSets).to.be.ok()

		flagConfig = Flags.read("TestFlag3")
		expect(flagConfig.active).to.equal(true)
		expect(flagConfig.retired).to.equal(false)
		expect(flagConfig.ruleSets).to.be.ok()

		local repository2 = {
			["TestFlag1"] = flag1v2,
			["TestFlag2"] = flag2v2,
			["TestFlag3"] = flag3v2,
		}

		local flags2 = {
			["TestFlag1"] = 2,
			["TestFlag2"] = 2,
			["TestFlag3"] = 2,
		}

		deserializeRepository(repository2, flags2)

		expect(Flags.exists("TestFlag1")).to.equal(true)
		expect(Flags.exists("TestFlag2")).to.equal(true)
		expect(Flags.exists("TestFlag3")).to.equal(true)

		flagConfig = Flags.read("TestFlag1")
		expect(flagConfig.active).to.equal(false)
		expect(flagConfig.retired).to.equal(true)
		expect(flagConfig.ruleSets).to.be.ok()

		flagConfig = Flags.read("TestFlag2")
		expect(flagConfig.active).to.equal(false)
		expect(flagConfig.retired).to.equal(true)
		expect(flagConfig.ruleSets).to.be.ok()

		flagConfig = Flags.read("TestFlag3")
		expect(flagConfig.active).to.equal(false)
		expect(flagConfig.retired).to.equal(true)
		expect(flagConfig.ruleSets).to.be.ok()
	end)

	it("should update flags and create new flags at the same time", function()
		local flag1v1 = {
			config = {
				active = true,
				retired = false,
				ruleSets = {},
			},
			version = 1,
		}

		local flag1v2 = {
			config = {
				active = false,
				retired = true,
				ruleSets = {},
			},
			version = 2,
		}

		local flag2v1 = {
			config = {
				active = true,
				retired = false,
				ruleSets = {},
			},
			version = 1,
		}

		local flag2v2 = {
			config = {
				active = false,
				retired = true,
				ruleSets = {},
			},
			version = 2,
		}

		local flag3v1 = {
			config = {
				active = true,
				retired = false,
				ruleSets = {},
			},
			version = 1,
		}

		local repository1 = {
			["TestFlag1"] = flag1v1,
			["TestFlag2"] = flag2v1,
		}
		local flags1 = {
			["TestFlag1"] = 1,
			["TestFlag2"] = 1,
		}

		expect(Flags.exists("TestFlag1")).to.equal(false)
		expect(Flags.exists("TestFlag2")).to.equal(false)
		expect(Flags.exists("TestFlag3")).to.equal(false)

		deserializeRepository(repository1, flags1)

		expect(Flags.exists("TestFlag1")).to.equal(true)
		expect(Flags.exists("TestFlag2")).to.equal(true)
		expect(Flags.exists("TestFlag3")).to.equal(false)

		local flagConfig = Flags.read("TestFlag1")
		expect(flagConfig.active).to.equal(true)
		expect(flagConfig.retired).to.equal(false)
		expect(flagConfig.ruleSets).to.be.ok()

		flagConfig = Flags.read("TestFlag2")
		expect(flagConfig.active).to.equal(true)
		expect(flagConfig.retired).to.equal(false)
		expect(flagConfig.ruleSets).to.be.ok()

		local repository2 = {
			["TestFlag1"] = flag1v2,
			["TestFlag2"] = flag2v2,
			["TestFlag3"] = flag3v1,
		}

		local flags2 = {
			["TestFlag1"] = 2,
			["TestFlag2"] = 2,
			["TestFlag3"] = 1,
		}

		deserializeRepository(repository2, flags2)

		expect(Flags.exists("TestFlag1")).to.equal(true)
		expect(Flags.exists("TestFlag2")).to.equal(true)
		expect(Flags.exists("TestFlag3")).to.equal(true)

		flagConfig = Flags.read("TestFlag1")
		expect(flagConfig.active).to.equal(false)
		expect(flagConfig.retired).to.equal(true)
		expect(flagConfig.ruleSets).to.be.ok()

		flagConfig = Flags.read("TestFlag2")
		expect(flagConfig.active).to.equal(false)
		expect(flagConfig.retired).to.equal(true)
		expect(flagConfig.ruleSets).to.be.ok()

		flagConfig = Flags.read("TestFlag3")
		expect(flagConfig.active).to.equal(true)
		expect(flagConfig.retired).to.equal(false)
		expect(flagConfig.ruleSets).to.be.ok()
	end)
end
