local c=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 H=c.expect local e=c.describe local n=c.it local v=(require("\064\112\107\103\047\064\106\115\100\111\116\108\117\097\047\115\104\097\114\101\100")).ReactInstanceMap e("\103\101\116",function()n("\119\105\116\104 \105\110\118\097\108\105\100 \102\105\098\101\114",function()local c={_reactInternals={tag=0}};(H(function()v.get(c)end)).toThrow("\105\110\118\097\108\105\100 \102\105\098\101\114 \105\110 \085\078\078\065\077\069\068 \067\111\109\112\111\110\101\110\116 \100\117\114\105\110\103 \103\101\116 \102\114\111\109 \082\101\097\099\116\073\110\115\116\097\110\099\101\077\097\112\033")end)n("\119\105\116\104 \118\097\108\105\100 \102\105\098\101\114 \116\104\097\116 \104\097\115 \105\110\118\097\108\105\100 \097\108\116\101\114\110\097\116\101",function()local c={_reactInternals={tag=0,subtreeFlags=0;lanes=0,childLanes=0;alternate={tag=1}}};(H(function()v.get(c)end)).toThrow("\105\110\118\097\108\105\100 \097\108\116\101\114\110\097\116\101 \102\105\098\101\114 \040\085\078\078\065\077\069\068 \097\108\116\101\114\110\097\116\101\041 \105\110 \085\078\078\065\077\069\068 \067\111\109\112\111\110\101\110\116 \100\117\114\105\110\103 \103\101\116 \102\114\111\109 \082\101\097\099\116\073\110\115\116\097\110\099\101\077\097\112\033")end)end)e("\115\101\116",function()n("\119\105\116\104 \105\110\118\097\108\105\100 \102\105\098\101\114",function()local c={tag=0};(H(function()v.set({displayName="\077\121\067\111\109\112\111\110\101\110\116"},c)end)).toThrow("\105\110\118\097\108\105\100 \102\105\098\101\114 \105\110 \077\121\067\111\109\112\111\110\101\110\116 \098\101\105\110\103 \115\101\116 \105\110 \082\101\097\099\116\073\110\115\116\097\110\099\101\077\097\112\033")end)n("\119\105\116\104 \118\097\108\105\100 \102\105\098\101\114 \119\105\116\104 \110\111 \114\101\116\117\114\110 \116\104\097\116 \104\097\115 \105\110\118\097\108\105\100 \097\108\116\101\114\110\097\116\101",function()local c={tag=0;subtreeFlags=0;lanes=0;childLanes=0,alternate={tag=1}};(H(function()v.set({},c)end)).toThrow("\105\110\118\097\108\105\100 \097\108\116\101\114\110\097\116\101 \102\105\098\101\114 \040\085\078\078\065\077\069\068 \097\108\116\101\114\110\097\116\101\041 \105\110 \085\078\078\065\077\069\068 \067\111\109\112\111\110\101\110\116 \098\101\105\110\103 \115\101\116 \105\110 \082\101\097\099\116\073\110\115\116\097\110\099\101\077\097\112\033")end)n("\119\105\116\104 \118\097\108\105\100 \102\105\098\101\114 \119\105\116\104 \097 \118\097\108\105\100 \114\101\116\117\114\110\095 \116\104\097\116 \104\097\115 \105\110\118\097\108\105\100 \097\108\116\101\114\110\097\116\101",function()local c={tag=0;subtreeFlags=0,lanes=0,childLanes=0;alternate={tag=1;subtreeFlags=1;lanes=1,childLanes=1};return_={tag=2;subtreeFlags=2,lanes=2,childLanes=2,alternate={tag=3}}};(H(function()v.set({},c)end)).toThrow("\105\110\118\097\108\105\100 \097\108\116\101\114\110\097\116\101 \102\105\098\101\114 \040\085\078\078\065\077\069\068 \097\108\116\101\114\110\097\116\101\041 \105\110 \085\078\078\065\077\069\068 \067\111\109\112\111\110\101\110\116 \098\101\105\110\103 \115\101\116 \105\110 \082\101\097\099\116\073\110\115\116\097\110\099\101\077\097\112\033 \123 \116\097\103\058 \051 \125\n \040\102\114\111\109 \111\114\105\103\105\110\097\108 \102\105\098\101\114 \085\078\078\065\077\069\068 \067\111\109\112\111\110\101\110\116\041")end)end)
-- ROBLOX deviation: we have a crash in production this deviant logic will help catch
-- ROBLOX TODO: make this only pass in __DEV__

local JestGlobals = require("@pkg/@jsdotlua/jest-globals")
local jestExpect = JestGlobals.expect
local describe = JestGlobals.describe
local it = JestGlobals.it

local ReactInstanceMap = require("@pkg/@jsdotlua/shared").ReactInstanceMap

describe("get", function()
	it("with invalid fiber", function()
		local elementWithBadFiber = {
			_reactInternals = {
				tag = 0,
				-- missing key fields of Fiber
			},
		}
		jestExpect(function()
			ReactInstanceMap.get(elementWithBadFiber)
		end).toThrow(
			"invalid fiber in UNNAMED Component during get from ReactInstanceMap!"
		)
	end)
	it("with valid fiber that has invalid alternate", function()
		local elementWithGoodFiberBadAlternate = {
			_reactInternals = {
				tag = 0,
				subtreeFlags = 0,
				lanes = 0,
				childLanes = 0,
				alternate = {
					tag = 1,
					-- missing key fields of Fiber
				},
			},
		}
		jestExpect(function()
			ReactInstanceMap.get(elementWithGoodFiberBadAlternate)
		end).toThrow(
			"invalid alternate fiber (UNNAMED alternate) in UNNAMED Component during get from ReactInstanceMap!"
		)
	end)
end)
describe("set", function()
	it("with invalid fiber", function()
		local badFiber = {
			tag = 0,
			-- missing key fields of Fiber
		}
		jestExpect(function()
			ReactInstanceMap.set({ displayName = "MyComponent" }, badFiber)
		end).toThrow("invalid fiber in MyComponent being set in ReactInstanceMap!")
	end)
	it("with valid fiber with no return that has invalid alternate", function()
		local goodFiberBadAlternate = {
			tag = 0,
			subtreeFlags = 0,
			lanes = 0,
			childLanes = 0,
			alternate = {
				tag = 1,
				-- missing key fields of Fiber
			},
		}
		jestExpect(function()
			ReactInstanceMap.set({}, goodFiberBadAlternate)
		end).toThrow(
			"invalid alternate fiber (UNNAMED alternate) in UNNAMED Component being set in ReactInstanceMap!"
		)
	end)
	it("with valid fiber with a valid return_ that has invalid alternate", function()
		local goodFiberGoodReturnBadAlternate = {
			tag = 0,
			subtreeFlags = 0,
			lanes = 0,
			childLanes = 0,
			alternate = {
				tag = 1,
				subtreeFlags = 1,
				lanes = 1,
				childLanes = 1,
			},
			return_ = {
				tag = 2,
				subtreeFlags = 2,
				lanes = 2,
				childLanes = 2,
				alternate = {
					tag = 3,
					-- missing key fields of Fiber
				},
			},
		}
		jestExpect(function()
			ReactInstanceMap.set({}, goodFiberGoodReturnBadAlternate)
		end).toThrow(
			"invalid alternate fiber (UNNAMED alternate) in UNNAMED Component being set in ReactInstanceMap! { tag: 3 }\n (from original fiber UNNAMED Component)"
		)
	end)
end)
