local s=require(script.Parent.Parent.mock.BindableEvent)local Q=require(script.Parent.Parent.topoRuntime)local a=require(script.Parent.useEvent)return function()describe("\117\115\101\069\118\101\110\116",function()it("\115\104\111\117\108\100 \113\117\101\117\101 \117\112 \101\118\101\110\116\115 \117\110\116\105\108 \117\115\101\069\118\101\110\116 \105\115 \099\097\108\108\101\100 \097\103\097\105\110",function()local i={system={}}local q=s.new()local U,z,u local N=true local b=0 local Y=function()if N then local s=0 for Q,a,i,q in a(q,q.Event)do(expect(Q)).to.equal(s+1)s+=1 U=a z=i u=q end;(expect(s)).to.equal(b)end end Q.start(i,Y)q:Fire(3,4,5)b=1 Q.start(i,Y);(expect(U)).to.equal(3);(expect(z)).to.equal(4);(expect(u)).to.equal(5)b=3 q:Fire()q:Fire()q:Fire()Q.start(i,Y)b=0 Q.start(i,Y)q:Fire()q:Fire()N=false Q.start(i,Y)N=true Q.start(i,Y)end)it("\115\104\111\117\108\100 \099\108\101\097\110\117\112 \105\102 \116\104\101 \101\118\101\110\116 \099\104\097\110\103\101\115",function()local i={system={}}local q=s.new()local U=s.new()local z=q local u=0 local N=function()local s=0 for Q in a(z,"\069\118\101\110\116")do s+=1 end;(expect(s)).to.equal(u)end Q.start(i,N)q:Fire()q:Fire()u=2 Q.start(i,N)q:Fire()q:Fire()z=U u=0 Q.start(i,N)U:Fire()u=1 Q.start(i,N)end)it("\115\104\111\117\108\100 \115\117\112\112\111\114\116 \099\117\115\116\111\109 \101\118\101\110\116\115",function()local s=false local i local q={system={}}local U={connect=function(Q,a)i=a return{destroy=function()s=true end}end}local z={event=U}local u=true local N=0 local function b()if u then for s in a(z,"\101\118\101\110\116")do N+=1 end end end Q.start(q,b);(expect(N)).to.equal(0);(expect(i)).to.be.ok();(expect(s)).to.equal(false)i()i()Q.start(q,b);(expect(N)).to.equal(2);(expect(i)).to.be.ok();(expect(s)).to.equal(false)u=false Q.start(q,b);(expect(N)).to.equal(2);(expect(s)).to.equal(true)end)end)end
local BindableEvent = require(script.Parent.Parent.mock.BindableEvent)
local topoRuntime = require(script.Parent.Parent.topoRuntime)
local useEvent = require(script.Parent.useEvent)

return function()
	describe("useEvent", function()
		it("should queue up events until useEvent is called again", function()
			local node = {
				system = {},
			}

			local event = BindableEvent.new()

			local a, b, c
			local shouldCall = true
			local shouldCount = 0
			local fn = function()
				if shouldCall then
					local count = 0
					for index, fa, fb, fc in useEvent(event, event.Event) do
						expect(index).to.equal(count + 1)
						count += 1
						a = fa
						b = fb
						c = fc
					end
					expect(count).to.equal(shouldCount)
				end
			end

			topoRuntime.start(node, fn)

			event:Fire(3, 4, 5)

			shouldCount = 1
			topoRuntime.start(node, fn)

			expect(a).to.equal(3)
			expect(b).to.equal(4)
			expect(c).to.equal(5)

			shouldCount = 3

			event:Fire()
			event:Fire()
			event:Fire()

			topoRuntime.start(node, fn)

			shouldCount = 0

			topoRuntime.start(node, fn)

			event:Fire()
			event:Fire()

			shouldCall = false

			topoRuntime.start(node, fn)

			shouldCall = true

			-- Count should still be 0 as last frame didn't call useEvent
			topoRuntime.start(node, fn)
		end)

		it("should cleanup if the event changes", function()
			local node = {
				system = {},
			}

			local event1 = BindableEvent.new()
			local event2 = BindableEvent.new()

			local event = event1
			local shouldCount = 0
			local fn = function()
				local count = 0
				for _ in useEvent(event, "Event") do
					count += 1
				end
				expect(count).to.equal(shouldCount)
			end

			topoRuntime.start(node, fn)

			event1:Fire()
			event1:Fire()

			shouldCount = 2
			topoRuntime.start(node, fn)

			event1:Fire()
			event1:Fire()
			event = event2

			shouldCount = 0
			topoRuntime.start(node, fn)

			event2:Fire()

			shouldCount = 1
			topoRuntime.start(node, fn)
		end)

		it("should support custom events", function()
			local disconnected = false
			local eventHandler

			local node = {
				system = {},
			}

			local event = {
				connect = function(_, handler)
					eventHandler = handler

					return {
						destroy = function()
							disconnected = true
						end,
					}
				end,
			}

			local object = {
				event = event,
			}

			local shouldRun = true
			local count = 0
			local function run()
				if shouldRun then
					for _ in useEvent(object, "event") do
						count += 1
					end
				end
			end

			topoRuntime.start(node, run)

			expect(count).to.equal(0)
			expect(eventHandler).to.be.ok()
			expect(disconnected).to.equal(false)

			eventHandler()
			eventHandler()

			topoRuntime.start(node, run)

			expect(count).to.equal(2)
			expect(eventHandler).to.be.ok()
			expect(disconnected).to.equal(false)

			shouldRun = false
			topoRuntime.start(node, run)

			expect(count).to.equal(2)
			expect(disconnected).to.equal(true)
		end)
	end)
end
