local h=game:GetService("\082\101\112\108\105\099\097\116\101\100\083\116\111\114\097\103\101")local C=require(script.Parent.WaitForSibling)local A=h.TestTarget:FindFirstChild("\084\104\105\110\103\049",true)return function()it("\115\104\111\117\108\100 \102\105\110\100 \116\104\101 \102\105\114\115\116 \109\097\116\099\104\105\110\103 \115\105\098\108\105\110\103",function()local h=C(A,"\084\104\105\110\103\050");(expect(h)).to.be.ok();(expect(h)).to.be.a("\117\115\101\114\100\097\116\097");(expect(h.Name)).to.equal("\084\104\105\110\103\050")end)it("\115\104\111\117\108\100 \116\105\109\101\111\117\116 \105\102 \116\104\101 \115\105\098\108\105\110\103 \105\115 \110\111\116 \102\111\117\110\100",function()local h=C(A,"\084\104\105\110\103\049\048\048",.5)local t=C(A,"\084\104\105\110\103\049\048\048",{timeout=.5});(expect(h)).never.to.be.ok();(expect(t)).never.to.be.ok()end)it("\115\104\111\117\108\100 \119\097\114\110 \105\102 \110\111 \116\105\109\101\111\117\116 \105\115 \112\114\111\118\105\100\101\100",function()local h=getfenv(C)local t=Instance.new("\066\105\110\100\097\098\108\101\069\118\101\110\116")local B=false function h.warn()B=true t:Fire()end local E=setfenv(C,h)task.spawn(function()E(A,"\084\104\105\110\103\049\048\048",{__warnAfter__=.5})end)t.Event:Wait();(expect(B)).to.equal(true)end)it("\115\104\111\117\108\100 \114\101\115\111\108\118\101 \111\110\099\101 \116\104\101 \115\105\098\108\105\110\103 \105\115 \102\111\117\110\100",function()task.spawn(function()task.wait(.5)local h=Instance.new("\070\111\108\100\101\114")h.Name="\084\104\105\110\103\049\048"h.Parent=A.Parent end)local h=C(A,"\084\104\105\110\103\049\048",1);(expect(h)).to.be.ok();(expect(h)).to.be.a("\117\115\101\114\100\097\116\097");(expect(h.Name)).to.equal("\084\104\105\110\103\049\048")end)end
--!strict
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local WaitForSibling = require(script.Parent.WaitForSibling)

local TestRoot = ReplicatedStorage.TestTarget:FindFirstChild("Thing1", true)

return function()
	it("should find the first matching sibling", function()
		local result = WaitForSibling(TestRoot, "Thing2")

		expect(result).to.be.ok()
		expect(result).to.be.a("userdata")
		expect(result.Name).to.equal("Thing2")
	end)

	it("should timeout if the sibling is not found", function()
		local result = WaitForSibling(TestRoot, "Thing100", 0.5)
		local result2 = WaitForSibling(TestRoot, "Thing100", {
			timeout = 0.5,
		})

		expect(result).never.to.be.ok()
		expect(result2).never.to.be.ok()
	end)

	it("should warn if no timeout is provided", function()
		local newenv = getfenv(WaitForSibling)
		local didWarnSignal = Instance.new("BindableEvent")
		local didWarn = false

		function newenv.warn()
			didWarn = true
			didWarnSignal:Fire()
		end

		local fenv = setfenv(WaitForSibling, newenv)

		task.spawn(function()
			fenv(TestRoot, "Thing100", {
				__warnAfter__ = 0.5,
			})
		end)

		didWarnSignal.Event:Wait()

		expect(didWarn).to.equal(true)
	end)

	it("should resolve once the sibling is found", function()
		task.spawn(function()
			task.wait(0.5)

			local sibling = Instance.new("Folder")
			sibling.Name = "Thing10"
			sibling.Parent = TestRoot.Parent
		end)

		local result = WaitForSibling(TestRoot, "Thing10", 1)

		expect(result).to.be.ok()
		expect(result).to.be.a("userdata")
		expect(result.Name).to.equal("Thing10")
	end)
end
