local U=require("\064\112\107\103\047\064\106\115\100\111\116\108\117\097\047\106\101\115\116-\103\108\111\098\097\108\115")local S=U.beforeEach local Z=U.expect local q=U.describe local i=U.it local r=U.jest local x=require("\064\112\107\103\047\064\106\115\100\111\116\108\117\097\047\108\117\097\117-\112\111\108\121\102\105\108\108")local N=x.Set q("\084\114\097\099\105\110\103\083\117\098\115\099\114\105\112\116\105\111\110\115",function()local U local x local w local b local D local l local m local A local H local O local o local f local y local J local G local Y local p local a={id=0,name="\102\105\114\115\116";timestamp=0}local C={id=1,name="\115\101\099\111\110\100";timestamp=0}local t=123 local function Q(S)local Z=S.enableSchedulerTracing local q=(function()if S.autoSubscribe==nil then return true end return S.autoSubscribe end)()r.resetModules()r.useFakeTimers()w=0 x=(require("\064\112\107\103\047\064\106\115\100\111\116\108\117\097\047\115\104\097\114\101\100")).ReactFeatureFlags x.enableSchedulerTracing=Z U=(require("\064\112\107\103\047\064\106\115\100\111\116\108\117\097\047\115\099\104\101\100\117\108\101\114")).tracing O=false o=false f=false y=false J=false G=false b=r.fn(function()if O then error("\069\120\112\101\099\116\101\100 \101\114\114\111\114 \111\110\073\110\116\101\114\097\099\116\105\111\110\083\099\104\101\100\117\108\101\100\087\111\114\107\067\111\109\112\108\101\116\101\100")end end)D=r.fn(function()if o then error("\069\120\112\101\099\116\101\100 \101\114\114\111\114 \111\110\073\110\116\101\114\097\099\116\105\111\110\084\114\097\099\101\100")end end)l=r.fn(function()if f then error("\069\120\112\101\099\116\101\100 \101\114\114\111\114 \111\110\087\111\114\107\067\097\110\099\101\108\101\100")end end)m=r.fn(function()if y then error("\069\120\112\101\099\116\101\100 \101\114\114\111\114 \111\110\087\111\114\107\083\099\104\101\100\117\108\101\100")end end)A=r.fn(function()if J then error("\069\120\112\101\099\116\101\100 \101\114\114\111\114 \111\110\087\111\114\107\083\116\097\114\116\101\100")end end)H=r.fn(function()if G then error("\069\120\112\101\099\116\101\100 \101\114\114\111\114 \111\110\087\111\114\107\083\116\111\112\112\101\100")end end)Y={onInteractionScheduledWorkCompleted=b,onInteractionTraced=D;onWorkCanceled=l;onWorkScheduled=m,onWorkStarted=A;onWorkStopped=H}p={onInteractionScheduledWorkCompleted=r.fn();onInteractionTraced=r.fn();onWorkCanceled=r.fn(),onWorkScheduled=r.fn(),onWorkStarted=r.fn(),onWorkStopped=r.fn()}if q then U.unstable_subscribe(Y)U.unstable_subscribe(p)end end q("\101\110\097\098\108\101\100",function()S(function()return Q({enableSchedulerTracing=true})end)i("\115\104\111\117\108\100 \108\097\122\105\108\121 \115\117\098\115\099\114\105\098\101 \116\111 \116\114\097\099\105\110\103 \097\110\100 \117\110\115\117\098\115\099\114\105\098\101 \097\103\097\105\110 \105\102 \116\104\101\114\101 \097\114\101 \110\111 \101\120\116\101\114\110\097\108 \115\117\098\115\099\114\105\098\101\114\115",function()Q({enableSchedulerTracing=true,autoSubscribe=false});(Z(U.__subscriberRef.current)).toBe(nil)U.unstable_subscribe(Y);(Z(U.__subscriberRef.current)).toBeDefined()U.unstable_subscribe(p);(Z(U.__subscriberRef.current)).toBeDefined()U.unstable_unsubscribe(p);(Z(U.__subscriberRef.current)).toBeDefined()U.unstable_unsubscribe(Y);(Z(U.__subscriberRef.current)).toBe(nil)end)q("\101\114\114\111\114 \104\097\110\100\108\105\110\103",function()i("\115\104\111\117\108\100 \099\111\118\101\114 \111\110\073\110\116\101\114\097\099\116\105\111\110\084\114\097\099\101\100\047\111\110\087\111\114\107\083\116\097\114\116\101\100 \119\105\116\104\105\110",function()U.unstable_trace(a.name,w,function()local S=r.fn()o=true;(Z(function()return U.unstable_trace(C.name,w,S,t)end)).toThrow("\069\120\112\101\099\116\101\100 \101\114\114\111\114 \111\110\073\110\116\101\114\097\099\116\105\111\110\084\114\097\099\101\100")o=false;(Z(S)).toHaveBeenCalledTimes(1)J=true;(Z(function()return U.unstable_trace(C.name,w,S,t)end)).toThrow("\069\120\112\101\099\116\101\100 \101\114\114\111\114 \111\110\087\111\114\107\083\116\097\114\116\101\100");(Z(S)).toHaveBeenCalledTimes(2);(Z(U.unstable_getCurrent())).toMatchInteractions({a});(Z(p.onInteractionTraced)).toHaveBeenCalledTimes(3);(Z(p.onWorkStarted)).toHaveBeenCalledTimes(3)end)end)i("\115\104\111\117\108\100 \099\111\118\101\114 \111\110\087\111\114\107\083\116\111\112\112\101\100 \119\105\116\104\105\110 \116\114\097\099\101",function()U.unstable_trace(a.name,w,function()local S local q=r.fn(function()S=(U.unstable_getCurrent())._array[2]end)G=true;(Z(function()return U.unstable_trace(C.name,w,q)end)).toThrow("\069\120\112\101\099\116\101\100 \101\114\114\111\114 \111\110\087\111\114\107\083\116\111\112\112\101\100")G=false;(Z(U.unstable_getCurrent())).toMatchInteractions({a});(Z(S.__count)).toBe(0);(Z(p.onWorkStopped)).toHaveBeenCalledTimes(1)end)end)i("\115\104\111\117\108\100 \099\111\118\101\114 \111\110\073\110\116\101\114\097\099\116\105\111\110\083\099\104\101\100\117\108\101\100\087\111\114\107\067\111\109\112\108\101\116\101\100 \119\105\116\104\105\110 \116\114\097\099\101",function()U.unstable_trace(a.name,w,function()local S=r.fn()O=true;(Z(function()return U.unstable_trace(C.name,w,S)end)).toThrow("\069\120\112\101\099\116\101\100 \101\114\114\111\114 \111\110\073\110\116\101\114\097\099\116\105\111\110\083\099\104\101\100\117\108\101\100\087\111\114\107\067\111\109\112\108\101\116\101\100")O=false;(Z(U.unstable_getCurrent())).toMatchInteractions({a});(Z(p.onInteractionScheduledWorkCompleted)).toHaveBeenCalledTimes(1)end)end)i("\115\104\111\117\108\100 \099\111\118\101\114 \116\104\101 \099\097\108\108\098\097\099\107 \119\105\116\104\105\110 \116\114\097\099\101",function()(Z(A)).never.toHaveBeenCalled();(Z(H)).never.toHaveBeenCalled();(Z(function()U.unstable_trace(a.name,w,function()error("\069\120\112\101\099\116\101\100 \101\114\114\111\114 \099\097\108\108\098\097\099\107")end)end)).toThrow("\069\120\112\101\099\116\101\100 \101\114\114\111\114 \099\097\108\108\098\097\099\107");(Z(A)).toHaveBeenCalledTimes(1);(Z(H)).toHaveBeenCalledTimes(1)end)i("\115\104\111\117\108\100 \099\111\118\101\114 \111\110\087\111\114\107\083\099\104\101\100\117\108\101\100 \119\105\116\104\105\110 \119\114\097\112",function()U.unstable_trace(a.name,w,function()local S=(U.unstable_getCurrent())._array[1]local q=S.__count y=true;(Z(function()return U.unstable_wrap(function() end)end)).toThrow("\069\120\112\101\099\116\101\100 \101\114\114\111\114 \111\110\087\111\114\107\083\099\104\101\100\117\108\101\100");(Z(S.__count)).toBe(q);(Z(p.onWorkScheduled)).toHaveBeenCalledTimes(1)end)end)i("\115\104\111\117\108\100 \099\111\118\101\114 \111\110\087\111\114\107\083\116\097\114\116\101\100 \119\105\116\104\105\110 \119\114\097\112",function()local S=r.fn()local q,i U.unstable_trace(a.name,w,function()q=(U.unstable_getCurrent())._array[1]i=U.unstable_wrap(S)end);(Z(q.__count)).toBe(1)J=true;(Z(function()i()end)).toThrow("\069\120\112\101\099\116\101\100 \101\114\114\111\114 \111\110\087\111\114\107\083\116\097\114\116\101\100");(Z(S)).toHaveBeenCalledTimes(1);(Z(q.__count)).toBe(0);(Z(p.onWorkStarted)).toHaveBeenCalledTimes(2)end)i("\115\104\111\117\108\100 \099\111\118\101\114 \111\110\087\111\114\107\083\116\111\112\112\101\100 \119\105\116\104\105\110 \119\114\097\112",function()U.unstable_trace(a.name,w,function()local S=(U.unstable_getCurrent())._array[1];(Z(S.__count)).toBe(1)local q local i U.unstable_trace(C.name,w,function()i=(U.unstable_getCurrent())._array[2];(Z(S.__count)).toBe(1);(Z(i.__count)).toBe(1)q=U.unstable_wrap(r.fn());(Z(S.__count)).toBe(2);(Z(i.__count)).toBe(2)end);(Z(S.__count)).toBe(2);(Z(i.__count)).toBe(1)G=true;(Z(function()q()end)).toThrow("\069\120\112\101\099\116\101\100 \101\114\114\111\114 \111\110\087\111\114\107\083\116\111\112\112\101\100")G=false;(Z(U.unstable_getCurrent())).toMatchInteractions({S});(Z(S.__count)).toBe(1);(Z(i.__count)).toBe(0);(Z(p.onWorkStopped)).toHaveBeenCalledTimes(2)end)end)i("\115\104\111\117\108\100 \099\111\118\101\114 \116\104\101 \099\097\108\108\098\097\099\107 \119\105\116\104\105\110 \119\114\097\112",function()(Z(A)).never.toHaveBeenCalled();(Z(H)).never.toHaveBeenCalled()local S local q U.unstable_trace(a.name,w,function()q=(U.unstable_getCurrent())._array[1]S=U.unstable_wrap(function()error("\069\120\112\101\099\116\101\100 \101\114\114\111\114 \119\114\097\112")end)end);(Z(A)).toHaveBeenCalledTimes(1);(Z(H)).toHaveBeenCalledTimes(1);(Z(function()S()end)).toThrow("\069\120\112\101\099\116\101\100 \101\114\114\111\114 \119\114\097\112");(Z(A)).toHaveBeenCalledTimes(2);(Z(H)).toHaveBeenCalledTimes(2);(Z(H)).toHaveBeenLastNotifiedOfWork({q})end)i("\115\104\111\117\108\100 \099\111\118\101\114 \111\110\087\111\114\107\067\097\110\099\101\108\101\100 \119\105\116\104\105\110 \119\114\097\112",function()local S,q U.unstable_trace(a.name,w,function()S=(U.unstable_getCurrent())._array[1]q=U.unstable_wrap(r.fn())end);(Z(S.__count)).toBe(1)f=true;(Z(function()q.cancel()end)).toThrow("\069\120\112\101\099\116\101\100 \101\114\114\111\114 \111\110\087\111\114\107\067\097\110\099\101\108\101\100");(Z(l)).toHaveBeenCalledTimes(1);(Z(S.__count)).toBe(0);(Z(b)).toHaveBeenLastNotifiedOfInteraction(a);(Z(p.onWorkCanceled)).toHaveBeenCalledTimes(1)end)end)i("\099\097\108\108\115 \108\105\102\101\099\121\099\108\101 \109\101\116\104\111\100\115 \102\111\114 \116\114\097\099\101",function()(Z(D)).never.toHaveBeenCalled();(Z(b)).never.toHaveBeenCalled()U.unstable_trace(a.name,w,function()(Z(D)).toHaveBeenCalledTimes(1);(Z(D)).toHaveBeenLastNotifiedOfInteraction(a);(Z(b)).never.toHaveBeenCalled();(Z(A)).toHaveBeenCalledTimes(1);(Z(A)).toHaveBeenLastNotifiedOfWork(N.new({a}),t);(Z(H)).never.toHaveBeenCalled()U.unstable_trace(C.name,w,function()(Z(D)).toHaveBeenCalledTimes(2);(Z(D)).toHaveBeenLastNotifiedOfInteraction(C);(Z(b)).never.toHaveBeenCalled();(Z(A)).toHaveBeenCalledTimes(2);(Z(A)).toHaveBeenLastNotifiedOfWork(N.new({a;C}),t);(Z(H)).never.toHaveBeenCalled()end,t);(Z(b)).toHaveBeenCalledTimes(1);(Z(b)).toHaveBeenLastNotifiedOfInteraction(C);(Z(H)).toHaveBeenCalledTimes(1);(Z(H)).toHaveBeenLastNotifiedOfWork(N.new({a;C}),t)end,t);(Z(b)).toHaveBeenCalledTimes(2);(Z(b)).toHaveBeenLastNotifiedOfInteraction(a);(Z(m)).never.toHaveBeenCalled();(Z(l)).never.toHaveBeenCalled();(Z(A)).toHaveBeenCalledTimes(2);(Z(H)).toHaveBeenCalledTimes(2);(Z(H)).toHaveBeenLastNotifiedOfWork(N.new({a}),t)end)i("\099\097\108\108\115 \108\105\102\101\099\121\099\108\101 \109\101\116\104\111\100\115 \102\111\114 \119\114\097\112",function()local S=r.fn()local q U.unstable_trace(a.name,w,function()(Z(D)).toHaveBeenCalledTimes(1);(Z(D)).toHaveBeenLastNotifiedOfInteraction(a)U.unstable_trace(C.name,w,function()(Z(D)).toHaveBeenCalledTimes(2);(Z(D)).toHaveBeenLastNotifiedOfInteraction(C)q=U.unstable_wrap(S,t);(Z(m)).toHaveBeenCalledTimes(1);(Z(m)).toHaveBeenLastNotifiedOfWork(N.new({a;C}),t)end)end);(Z(D)).toHaveBeenCalledTimes(2);(Z(b)).never.toHaveBeenCalled()q();(Z(S)).toHaveBeenCalled();(Z(m)).toHaveBeenCalledTimes(1);(Z(l)).never.toHaveBeenCalled();(Z(A)).toHaveBeenCalledTimes(3);(Z(A)).toHaveBeenLastNotifiedOfWork(N.new({a,C}),t);(Z(H)).toHaveBeenCalledTimes(3);(Z(H)).toHaveBeenLastNotifiedOfWork(N.new({a,C}),t);(Z(b.mock.calls[1][1])).toMatchInteraction(a);(Z(b.mock.calls[2][1])).toMatchInteraction(C)end)i("\115\104\111\117\108\100 \099\097\108\108 \116\104\101 \099\111\114\114\101\099\116 \105\110\116\101\114\097\099\116\105\111\110 \115\117\098\115\099\114\105\098\101\114 \109\101\116\104\111\100\115 \119\104\101\110 \097 \119\114\097\112\112\101\100 \099\097\108\108\098\097\099\107 \105\115 \099\097\110\099\101\108\101\100",function()local S=r.fn()local q=r.fn()local i,x U.unstable_trace(a.name,w,function()i=U.unstable_wrap(S,t)U.unstable_trace(C.name,w,function()x=U.unstable_wrap(q,t)end)end);(Z(D)).toHaveBeenCalledTimes(2);(Z(b)).never.toHaveBeenCalled();(Z(l)).never.toHaveBeenCalled();(Z(A)).toHaveBeenCalledTimes(2);(Z(H)).toHaveBeenCalledTimes(2)x:cancel();(Z(b)).toHaveBeenCalledTimes(1);(Z(b)).toHaveBeenLastNotifiedOfInteraction(C);(Z(l)).toHaveBeenCalledTimes(1);(Z(l)).toHaveBeenLastNotifiedOfWork(N.new({a;C}),t)i:cancel();(Z(b)).toHaveBeenCalledTimes(2);(Z(b)).toHaveBeenLastNotifiedOfInteraction(a);(Z(l)).toHaveBeenCalledTimes(2);(Z(l)).toHaveBeenLastNotifiedOfWork(N.new({a}),t);(Z(S)).never.toHaveBeenCalled();(Z(q)).never.toHaveBeenCalled()end)i("\115\104\111\117\108\100 \110\111\116 \101\110\100 \097\110 \105\110\116\101\114\097\099\116\105\111\110 \116\119\105\099\101 \105\102 \119\114\097\112 \105\115 \117\115\101\100 \116\111 \115\099\104\101\100\117\108\101 \102\111\108\108\111\119 \117\112 \119\111\114\107 \119\105\116\104\105\110 \097\110\111\116\104\101\114 \119\114\097\112",function()local S,q local i=r.fn()local x=r.fn(function()q=U.unstable_wrap(i,t)end)U.unstable_trace(a.name,w,function()S=U.unstable_wrap(x,t)end);(Z(D)).toHaveBeenCalledTimes(1);(Z(b)).never.toHaveBeenCalled()S();(Z(D)).toHaveBeenCalledTimes(1);(Z(b)).never.toHaveBeenCalled()q();(Z(D)).toHaveBeenCalledTimes(1);(Z(b)).toHaveBeenCalledTimes(1);(Z(b)).toHaveBeenLastNotifiedOfInteraction(a)end)i("\115\104\111\117\108\100 \110\111\116 \100\101\099\114\101\109\101\110\116 \116\104\101 \105\110\116\101\114\097\099\116\105\111\110 \099\111\117\110\116 \116\119\105\099\101 \105\102 \097 \119\114\097\112\112\101\100 \102\117\110\099\116\105\111\110 \105\115 \114\117\110 \116\119\105\099\101",function()local S=r.fn()local q=r.fn()local i,x U.unstable_trace(a.name,w,function()i=U.unstable_wrap(S,t)x=U.unstable_wrap(q,t)end);(Z(D)).toHaveBeenCalledTimes(1);(Z(b)).never.toHaveBeenCalled()i();(Z(S)).toHaveBeenCalledTimes(1);(Z(D)).toHaveBeenCalledTimes(1);(Z(b)).never.toHaveBeenCalled()i();(Z(S)).toHaveBeenCalledTimes(2);(Z(D)).toHaveBeenCalledTimes(1);(Z(b)).never.toHaveBeenCalled()x();(Z(D)).toHaveBeenCalledTimes(1);(Z(b)).toHaveBeenCalledTimes(1);(Z(b)).toHaveBeenLastNotifiedOfInteraction(a)end)i("\115\104\111\117\108\100 \117\110\115\117\098\115\099\114\105\098\101",function()U.unstable_unsubscribe(Y)U.unstable_trace(a.name,w,function() end);(Z(D)).never.toHaveBeenCalled()end)end)q("\100\105\115\097\098\108\101\100",function()S(function()return Q({enableSchedulerTracing=false})end)i("\084\079\068\079 - \119\101 \110\101\101\100 \097\116 \108\101\097\115\116 \111\110\101 \116\101\115\116 \102\111\114 \074\101\115\116\082\111\098\108\111\120 \110\111\116 \116\111 \116\104\114\111\119",function() end)end)end)
-- ROBLOX upstream: https://github.com/facebook/react/blob/9abc2785cb070148d64fae81e523246b90b92016/packages/scheduler/src/__tests__/TracingSubscriptions-test.internal.js
-- /**
--  * Copyright (c) Facebook, Inc. and its affiliates.
--  *
--  * This source code is licensed under the MIT license found in the
--  * LICENSE file in the root directory of this source tree.
--  *
--  * @flow
--  */

local JestGlobals = require("@pkg/@jsdotlua/jest-globals")
local beforeEach = JestGlobals.beforeEach
local jestExpect = JestGlobals.expect
local describe = JestGlobals.describe
local it = JestGlobals.it
local jest = JestGlobals.jest
local LuauPolyfill = require("@pkg/@jsdotlua/luau-polyfill")
local Set = LuauPolyfill.Set

describe("TracingSubscriptions", function()
	local SchedulerTracing
	local ReactFeatureFlags
	local currentTime
	local onInteractionScheduledWorkCompleted
	local onInteractionTraced
	local onWorkCanceled
	local onWorkScheduled
	local onWorkStarted
	local onWorkStopped
	local throwInOnInteractionScheduledWorkCompleted
	local throwInOnInteractionTraced
	local throwInOnWorkCanceled
	local throwInOnWorkScheduled
	local throwInOnWorkStarted
	local throwInOnWorkStopped
	local firstSubscriber
	local secondSubscriber
	local firstEvent = { id = 0, name = "first", timestamp = 0 }
	local secondEvent = { id = 1, name = "second", timestamp = 0 }
	local threadID = 123
	local function loadModules(config)
		local enableSchedulerTracing = config.enableSchedulerTracing
		local autoSubscribe = (function()
			if config.autoSubscribe == nil then
				return true
			end
			return config.autoSubscribe
		end)()

		jest.resetModules()
		jest.useFakeTimers()

		currentTime = 0

		ReactFeatureFlags = require("@pkg/@jsdotlua/shared").ReactFeatureFlags
		ReactFeatureFlags.enableSchedulerTracing = enableSchedulerTracing

		SchedulerTracing = require("@pkg/@jsdotlua/scheduler").tracing

		throwInOnInteractionScheduledWorkCompleted = false
		throwInOnInteractionTraced = false
		throwInOnWorkCanceled = false
		throwInOnWorkScheduled = false
		throwInOnWorkStarted = false
		throwInOnWorkStopped = false

		onInteractionScheduledWorkCompleted = jest.fn(function()
			if throwInOnInteractionScheduledWorkCompleted then
				error("Expected error onInteractionScheduledWorkCompleted")
			end
		end)
		onInteractionTraced = jest.fn(function()
			if throwInOnInteractionTraced then
				error("Expected error onInteractionTraced")
			end
		end)
		onWorkCanceled = jest.fn(function()
			if throwInOnWorkCanceled then
				error("Expected error onWorkCanceled")
			end
		end)
		onWorkScheduled = jest.fn(function()
			if throwInOnWorkScheduled then
				error("Expected error onWorkScheduled")
			end
		end)
		onWorkStarted = jest.fn(function()
			if throwInOnWorkStarted then
				error("Expected error onWorkStarted")
			end
		end)
		onWorkStopped = jest.fn(function()
			if throwInOnWorkStopped then
				error("Expected error onWorkStopped")
			end
		end)

		firstSubscriber = {
			onInteractionScheduledWorkCompleted = onInteractionScheduledWorkCompleted,
			onInteractionTraced = onInteractionTraced,
			onWorkCanceled = onWorkCanceled,
			onWorkScheduled = onWorkScheduled,
			onWorkStarted = onWorkStarted,
			onWorkStopped = onWorkStopped,
		}

		secondSubscriber = {
			onInteractionScheduledWorkCompleted = jest.fn(),
			onInteractionTraced = jest.fn(),
			onWorkCanceled = jest.fn(),
			onWorkScheduled = jest.fn(),
			onWorkStarted = jest.fn(),
			onWorkStopped = jest.fn(),
		}
		if autoSubscribe then
			SchedulerTracing.unstable_subscribe(firstSubscriber)
			SchedulerTracing.unstable_subscribe(secondSubscriber)
		end
	end
	describe("enabled", function()
		beforeEach(function()
			return loadModules({ enableSchedulerTracing = true })
		end)
		it(
			"should lazily subscribe to tracing and unsubscribe again if there are no external subscribers",
			function()
				loadModules({ enableSchedulerTracing = true, autoSubscribe = false })
				jestExpect(SchedulerTracing.__subscriberRef.current).toBe(nil)
				SchedulerTracing.unstable_subscribe(firstSubscriber)
				jestExpect(SchedulerTracing.__subscriberRef.current).toBeDefined()
				SchedulerTracing.unstable_subscribe(secondSubscriber)
				jestExpect(SchedulerTracing.__subscriberRef.current).toBeDefined()
				SchedulerTracing.unstable_unsubscribe(secondSubscriber)
				jestExpect(SchedulerTracing.__subscriberRef.current).toBeDefined()
				SchedulerTracing.unstable_unsubscribe(firstSubscriber)
				jestExpect(SchedulerTracing.__subscriberRef.current).toBe(nil)
			end
		)
		describe("error handling", function()
			it("should cover onInteractionTraced/onWorkStarted within", function()
				SchedulerTracing.unstable_trace(firstEvent.name, currentTime, function()
					local mock = jest.fn()

					-- It should call the callback before re-throwing
					throwInOnInteractionTraced = true
					jestExpect(function()
						return SchedulerTracing.unstable_trace(
							secondEvent.name,
							currentTime,
							mock,
							threadID
						)
					end).toThrow("Expected error onInteractionTraced")
					throwInOnInteractionTraced = false
					jestExpect(mock).toHaveBeenCalledTimes(1)
					throwInOnWorkStarted = true
					jestExpect(function()
						return SchedulerTracing.unstable_trace(
							secondEvent.name,
							currentTime,
							mock,
							threadID
						)
					end).toThrow("Expected error onWorkStarted")
					jestExpect(mock).toHaveBeenCalledTimes(2)

					-- It should restore the previous/outer interactions
					jestExpect(SchedulerTracing.unstable_getCurrent()).toMatchInteractions({
						firstEvent,
					})

					-- It should call other subscribers despite the earlier error
					jestExpect(secondSubscriber.onInteractionTraced).toHaveBeenCalledTimes(
						3
					)
					jestExpect(secondSubscriber.onWorkStarted).toHaveBeenCalledTimes(3)
				end)
			end)
			it("should cover onWorkStopped within trace", function()
				SchedulerTracing.unstable_trace(firstEvent.name, currentTime, function()
					local innerInteraction
					local mock = jest.fn(function()
						innerInteraction =
							SchedulerTracing.unstable_getCurrent()._array[2] --[[ ROBLOX adaptation: added 1 to array index ]]
					end)

					throwInOnWorkStopped = true

					jestExpect(function()
						return SchedulerTracing.unstable_trace(
							secondEvent.name,
							currentTime,
							mock
						)
					end).toThrow("Expected error onWorkStopped")
					throwInOnWorkStopped = false

					-- It should restore the previous/outer interactions
					jestExpect(SchedulerTracing.unstable_getCurrent()).toMatchInteractions({
						firstEvent,
					})

					-- It should update the interaction count so as not to interfere with subsequent calls
					jestExpect(innerInteraction.__count).toBe(0)

					-- It should call other subscribers despite the earlier error
					jestExpect(secondSubscriber.onWorkStopped).toHaveBeenCalledTimes(1)
				end)
			end)
			it("should cover onInteractionScheduledWorkCompleted within trace", function()
				SchedulerTracing.unstable_trace(firstEvent.name, currentTime, function()
					local mock = jest.fn()
					throwInOnInteractionScheduledWorkCompleted = true
					jestExpect(function()
						return SchedulerTracing.unstable_trace(
							secondEvent.name,
							currentTime,
							mock
						)
					end).toThrow(
						"Expected error onInteractionScheduledWorkCompleted"
					)
					throwInOnInteractionScheduledWorkCompleted = false
					jestExpect(SchedulerTracing.unstable_getCurrent()).toMatchInteractions({
						firstEvent,
					})
					jestExpect(secondSubscriber.onInteractionScheduledWorkCompleted).toHaveBeenCalledTimes(
						1
					)
				end)
			end)
			it("should cover the callback within trace", function()
				jestExpect(onWorkStarted).never.toHaveBeenCalled()
				jestExpect(onWorkStopped).never.toHaveBeenCalled()
				jestExpect(function()
					SchedulerTracing.unstable_trace(
						firstEvent.name,
						currentTime,
						function()
							error("Expected error callback")
						end
					)
				end).toThrow("Expected error callback")
				jestExpect(onWorkStarted).toHaveBeenCalledTimes(1)
				jestExpect(onWorkStopped).toHaveBeenCalledTimes(1)
			end)
			it("should cover onWorkScheduled within wrap", function()
				SchedulerTracing.unstable_trace(firstEvent.name, currentTime, function()
					-- ROBLOX FIXME: Array.from() polyfill doesn't recognize Set correctly
					local interaction = SchedulerTracing.unstable_getCurrent()._array[1] --[[ ROBLOX adaptation: added 1 to array index ]]
					local beforeCount = interaction.__count
					throwInOnWorkScheduled = true
					jestExpect(function()
						return SchedulerTracing.unstable_wrap(function() end)
					end).toThrow("Expected error onWorkScheduled")
					jestExpect(interaction.__count).toBe(beforeCount)
					jestExpect(secondSubscriber.onWorkScheduled).toHaveBeenCalledTimes(1)
				end)
			end)
			it("should cover onWorkStarted within wrap", function()
				local mock = jest.fn()
				local interaction, wrapped
				SchedulerTracing.unstable_trace(firstEvent.name, currentTime, function()
					-- ROBLOX FIXME: Array.from() polyfill doesn't recognize Set correctly
					interaction = SchedulerTracing.unstable_getCurrent()._array[1] --[[ ROBLOX adaptation: added 1 to array index ]]
					wrapped = SchedulerTracing.unstable_wrap(mock)
				end)
				jestExpect(interaction.__count).toBe(1)
				throwInOnWorkStarted = true
				jestExpect(function()
					wrapped()
				end).toThrow("Expected error onWorkStarted")
				jestExpect(mock).toHaveBeenCalledTimes(1)
				jestExpect(interaction.__count).toBe(0)
				jestExpect(secondSubscriber.onWorkStarted).toHaveBeenCalledTimes(2)
			end)
			it("should cover onWorkStopped within wrap", function()
				SchedulerTracing.unstable_trace(firstEvent.name, currentTime, function()
					-- ROBLOX FIXME: Array.from() polyfill doesn't recognize Set correctly
					local outerInteraction =
						SchedulerTracing.unstable_getCurrent()._array[1] --[[ ROBLOX adaptation: added 1 to array index ]]
					jestExpect(outerInteraction.__count).toBe(1)
					local wrapped
					local innerInteraction
					SchedulerTracing.unstable_trace(
						secondEvent.name,
						currentTime,
						function()
							-- ROBLOX FIXME: Array.from() polyfill doesn't recognize Set correctly
							innerInteraction =
								SchedulerTracing.unstable_getCurrent()._array[2] --[[ ROBLOX adaptation: added 1 to array index ]]
							jestExpect(outerInteraction.__count).toBe(1)
							jestExpect(innerInteraction.__count).toBe(1)
							wrapped = SchedulerTracing.unstable_wrap(jest.fn())
							jestExpect(outerInteraction.__count).toBe(2)
							jestExpect(innerInteraction.__count).toBe(2)
						end
					)
					jestExpect(outerInteraction.__count).toBe(2)
					jestExpect(innerInteraction.__count).toBe(1)
					throwInOnWorkStopped = true
					jestExpect(function()
						wrapped()
					end).toThrow("Expected error onWorkStopped")
					throwInOnWorkStopped = false
					jestExpect(SchedulerTracing.unstable_getCurrent()).toMatchInteractions({
						outerInteraction,
					})
					jestExpect(outerInteraction.__count).toBe(1)
					jestExpect(innerInteraction.__count).toBe(0)
					jestExpect(secondSubscriber.onWorkStopped).toHaveBeenCalledTimes(2)
				end)
			end)

			it("should cover the callback within wrap", function()
				jestExpect(onWorkStarted).never.toHaveBeenCalled()
				jestExpect(onWorkStopped).never.toHaveBeenCalled()

				local wrapped
				local interaction
				SchedulerTracing.unstable_trace(firstEvent.name, currentTime, function()
					-- ROBLOX FIXME: Array.from() polyfill doesn't recognize Set correctly
					interaction = SchedulerTracing.unstable_getCurrent()._array[1] --[[ ROBLOX adaptation: added 1 to array index ]]
					wrapped = SchedulerTracing.unstable_wrap(function()
						error("Expected error wrap")
					end)
				end)

				jestExpect(onWorkStarted).toHaveBeenCalledTimes(1)
				jestExpect(onWorkStopped).toHaveBeenCalledTimes(1)

				jestExpect(function()
					wrapped()
				end).toThrow("Expected error wrap")

				jestExpect(onWorkStarted).toHaveBeenCalledTimes(2)
				jestExpect(onWorkStopped).toHaveBeenCalledTimes(2)
				jestExpect(onWorkStopped).toHaveBeenLastNotifiedOfWork({ interaction })
			end)

			it("should cover onWorkCanceled within wrap", function()
				local interaction, wrapped
				SchedulerTracing.unstable_trace(firstEvent.name, currentTime, function()
					-- ROBLOX FIXME: Array.from() polyfill doesn't recognize Set correctly
					interaction = SchedulerTracing.unstable_getCurrent()._array[1] --[[ ROBLOX adaptation: added 1 to array index ]]
					wrapped = SchedulerTracing.unstable_wrap(jest.fn())
				end)
				jestExpect(interaction.__count).toBe(1)
				throwInOnWorkCanceled = true
				jestExpect(function()
					wrapped.cancel()
				end).toThrow("Expected error onWorkCanceled")
				jestExpect(onWorkCanceled).toHaveBeenCalledTimes(1)
				jestExpect(interaction.__count).toBe(0)
				jestExpect(onInteractionScheduledWorkCompleted).toHaveBeenLastNotifiedOfInteraction(
					firstEvent
				)
				jestExpect(secondSubscriber.onWorkCanceled).toHaveBeenCalledTimes(1)
			end)
		end)
		it("calls lifecycle methods for trace", function()
			jestExpect(onInteractionTraced).never.toHaveBeenCalled()
			jestExpect(onInteractionScheduledWorkCompleted).never.toHaveBeenCalled()
			SchedulerTracing.unstable_trace(firstEvent.name, currentTime, function()
				jestExpect(onInteractionTraced).toHaveBeenCalledTimes(1)
				jestExpect(onInteractionTraced).toHaveBeenLastNotifiedOfInteraction(
					firstEvent
				)
				jestExpect(onInteractionScheduledWorkCompleted).never.toHaveBeenCalled()
				jestExpect(onWorkStarted).toHaveBeenCalledTimes(1)
				jestExpect(onWorkStarted).toHaveBeenLastNotifiedOfWork(
					Set.new({ firstEvent }),
					threadID
				)
				jestExpect(onWorkStopped).never.toHaveBeenCalled()

				SchedulerTracing.unstable_trace(secondEvent.name, currentTime, function()
					jestExpect(onInteractionTraced).toHaveBeenCalledTimes(2)
					jestExpect(onInteractionTraced).toHaveBeenLastNotifiedOfInteraction(
						secondEvent
					)
					jestExpect(onInteractionScheduledWorkCompleted).never.toHaveBeenCalled()
					jestExpect(onWorkStarted).toHaveBeenCalledTimes(2)
					jestExpect(onWorkStarted).toHaveBeenLastNotifiedOfWork(
						Set.new({ firstEvent, secondEvent }),
						threadID
					)
					jestExpect(onWorkStopped).never.toHaveBeenCalled()
				end, threadID)
				jestExpect(onInteractionScheduledWorkCompleted).toHaveBeenCalledTimes(1)
				jestExpect(onInteractionScheduledWorkCompleted).toHaveBeenLastNotifiedOfInteraction(
					secondEvent
				)
				jestExpect(onWorkStopped).toHaveBeenCalledTimes(1)
				jestExpect(onWorkStopped).toHaveBeenLastNotifiedOfWork(
					Set.new({ firstEvent, secondEvent }),
					threadID
				)
			end, threadID)
			jestExpect(onInteractionScheduledWorkCompleted).toHaveBeenCalledTimes(2)
			jestExpect(onInteractionScheduledWorkCompleted).toHaveBeenLastNotifiedOfInteraction(
				firstEvent
			)
			jestExpect(onWorkScheduled).never.toHaveBeenCalled()
			jestExpect(onWorkCanceled).never.toHaveBeenCalled()
			jestExpect(onWorkStarted).toHaveBeenCalledTimes(2)
			jestExpect(onWorkStopped).toHaveBeenCalledTimes(2)
			jestExpect(onWorkStopped).toHaveBeenLastNotifiedOfWork(
				Set.new({ firstEvent }),
				threadID
			)
		end)
		it("calls lifecycle methods for wrap", function()
			local unwrapped = jest.fn()
			local wrapped
			SchedulerTracing.unstable_trace(firstEvent.name, currentTime, function()
				jestExpect(onInteractionTraced).toHaveBeenCalledTimes(1)
				jestExpect(onInteractionTraced).toHaveBeenLastNotifiedOfInteraction(
					firstEvent
				)
				SchedulerTracing.unstable_trace(secondEvent.name, currentTime, function()
					jestExpect(onInteractionTraced).toHaveBeenCalledTimes(2)
					jestExpect(onInteractionTraced).toHaveBeenLastNotifiedOfInteraction(
						secondEvent
					)
					wrapped = SchedulerTracing.unstable_wrap(unwrapped, threadID)
					jestExpect(onWorkScheduled).toHaveBeenCalledTimes(1)
					jestExpect(onWorkScheduled).toHaveBeenLastNotifiedOfWork(
						Set.new({ firstEvent, secondEvent }),
						threadID
					)
				end)
			end)
			jestExpect(onInteractionTraced).toHaveBeenCalledTimes(2)
			jestExpect(onInteractionScheduledWorkCompleted).never.toHaveBeenCalled()
			wrapped()
			jestExpect(unwrapped).toHaveBeenCalled()
			jestExpect(onWorkScheduled).toHaveBeenCalledTimes(1)
			jestExpect(onWorkCanceled).never.toHaveBeenCalled()
			jestExpect(onWorkStarted).toHaveBeenCalledTimes(3)
			jestExpect(onWorkStarted).toHaveBeenLastNotifiedOfWork(
				Set.new({ firstEvent, secondEvent }),
				threadID
			)
			jestExpect(onWorkStopped).toHaveBeenCalledTimes(3)
			jestExpect(onWorkStopped).toHaveBeenLastNotifiedOfWork(
				Set.new({ firstEvent, secondEvent }),
				threadID
			)
			jestExpect(onInteractionScheduledWorkCompleted
				.mock
				.calls
				[1] --[[ ROBLOX adaptation: added 1 to array index ]]
				[1] --[[ ROBLOX adaptation: added 1 to array index ]]).toMatchInteraction(
				firstEvent
			)
			jestExpect(onInteractionScheduledWorkCompleted
				.mock
				.calls
				[2] --[[ ROBLOX adaptation: added 1 to array index ]]
				[1] --[[ ROBLOX adaptation: added 1 to array index ]]).toMatchInteraction(
				secondEvent
			)
		end)
		it(
			"should call the correct interaction subscriber methods when a wrapped callback is canceled",
			function()
				local fnOne = jest.fn()
				local fnTwo = jest.fn()
				local wrappedOne, wrappedTwo
				SchedulerTracing.unstable_trace(firstEvent.name, currentTime, function()
					wrappedOne = SchedulerTracing.unstable_wrap(fnOne, threadID)
					SchedulerTracing.unstable_trace(
						secondEvent.name,
						currentTime,
						function()
							wrappedTwo = SchedulerTracing.unstable_wrap(fnTwo, threadID)
						end
					)
				end)
				jestExpect(onInteractionTraced).toHaveBeenCalledTimes(2)
				jestExpect(onInteractionScheduledWorkCompleted).never.toHaveBeenCalled()
				jestExpect(onWorkCanceled).never.toHaveBeenCalled()
				jestExpect(onWorkStarted).toHaveBeenCalledTimes(2)
				jestExpect(onWorkStopped).toHaveBeenCalledTimes(2)
				wrappedTwo:cancel()
				jestExpect(onInteractionScheduledWorkCompleted).toHaveBeenCalledTimes(1)
				jestExpect(onInteractionScheduledWorkCompleted).toHaveBeenLastNotifiedOfInteraction(
					secondEvent
				)
				jestExpect(onWorkCanceled).toHaveBeenCalledTimes(1)
				jestExpect(onWorkCanceled).toHaveBeenLastNotifiedOfWork(
					Set.new({ firstEvent, secondEvent }),
					threadID
				)
				wrappedOne:cancel()
				jestExpect(onInteractionScheduledWorkCompleted).toHaveBeenCalledTimes(2)
				jestExpect(onInteractionScheduledWorkCompleted).toHaveBeenLastNotifiedOfInteraction(
					firstEvent
				)
				jestExpect(onWorkCanceled).toHaveBeenCalledTimes(2)
				jestExpect(onWorkCanceled).toHaveBeenLastNotifiedOfWork(
					Set.new({ firstEvent }),
					threadID
				)
				jestExpect(fnOne).never.toHaveBeenCalled()
				jestExpect(fnTwo).never.toHaveBeenCalled()
			end
		)
		it(
			"should not end an interaction twice if wrap is used to schedule follow up work within another wrap",
			function()
				local wrappedOne, wrappedTwo
				local fnTwo = jest.fn()
				local fnOne = jest.fn(function()
					wrappedTwo = SchedulerTracing.unstable_wrap(fnTwo, threadID)
				end)
				SchedulerTracing.unstable_trace(firstEvent.name, currentTime, function()
					wrappedOne = SchedulerTracing.unstable_wrap(fnOne, threadID)
				end)
				jestExpect(onInteractionTraced).toHaveBeenCalledTimes(1)
				jestExpect(onInteractionScheduledWorkCompleted).never.toHaveBeenCalled()
				wrappedOne()
				jestExpect(onInteractionTraced).toHaveBeenCalledTimes(1)
				jestExpect(onInteractionScheduledWorkCompleted).never.toHaveBeenCalled()
				wrappedTwo()
				jestExpect(onInteractionTraced).toHaveBeenCalledTimes(1)
				jestExpect(onInteractionScheduledWorkCompleted).toHaveBeenCalledTimes(1)
				jestExpect(onInteractionScheduledWorkCompleted).toHaveBeenLastNotifiedOfInteraction(
					firstEvent
				)
			end
		)
		it(
			"should not decrement the interaction count twice if a wrapped function is run twice",
			function()
				local unwrappedOne = jest.fn()
				local unwrappedTwo = jest.fn()
				local wrappedOne, wrappedTwo
				SchedulerTracing.unstable_trace(firstEvent.name, currentTime, function()
					wrappedOne = SchedulerTracing.unstable_wrap(unwrappedOne, threadID)
					wrappedTwo = SchedulerTracing.unstable_wrap(unwrappedTwo, threadID)
				end)
				jestExpect(onInteractionTraced).toHaveBeenCalledTimes(1)
				jestExpect(onInteractionScheduledWorkCompleted).never.toHaveBeenCalled()
				wrappedOne()
				jestExpect(unwrappedOne).toHaveBeenCalledTimes(1)
				jestExpect(onInteractionTraced).toHaveBeenCalledTimes(1)
				jestExpect(onInteractionScheduledWorkCompleted).never.toHaveBeenCalled()
				wrappedOne()
				jestExpect(unwrappedOne).toHaveBeenCalledTimes(2)
				jestExpect(onInteractionTraced).toHaveBeenCalledTimes(1)
				jestExpect(onInteractionScheduledWorkCompleted).never.toHaveBeenCalled()
				wrappedTwo()
				jestExpect(onInteractionTraced).toHaveBeenCalledTimes(1)
				jestExpect(onInteractionScheduledWorkCompleted).toHaveBeenCalledTimes(1)
				jestExpect(onInteractionScheduledWorkCompleted).toHaveBeenLastNotifiedOfInteraction(
					firstEvent
				)
			end
		)
		it("should unsubscribe", function()
			SchedulerTracing.unstable_unsubscribe(firstSubscriber)
			SchedulerTracing.unstable_trace(firstEvent.name, currentTime, function() end)
			jestExpect(onInteractionTraced).never.toHaveBeenCalled()
		end)
	end)
	describe("disabled", function()
		beforeEach(function()
			return loadModules({ enableSchedulerTracing = false })
		end)

		it("TODO - we need at least one test for JestRoblox not to throw", function() end)

		-- TODO
	end)
end)
