local V=require("\046\046\047\110\111\089\105\101\108\100")local h=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 G=h.expect local Q=h.jest local R=h.it local A=h.describe R("\099\097\108\108\115 \102\117\110\099\116\105\111\110\115 \110\111\114\109\097\108\108\121",function()local h,R=Q.fn(function()return"\115\117\099\099\101\115\115"end)local A=V(R,5,false,6);(G(h)).toHaveBeenCalledTimes(1);(G(h)).toHaveBeenCalledWith(5,false,6);(G(A)).toBe("\115\117\099\099\101\115\115")end)if _G.DEV then A("\119\105\116\104 \068\069\086\061\116\114\117\101",function()R("\116\104\114\111\119\115 \111\110 \121\105\101\108\100",function()local h,R=Q.fn(function(V)task.wait(V)end);(G(function()V(R,.1)end)).toThrow("\102\117\110\099\116\105\111\110 \105\115 \110\111\116 \097\108\108\111\119\101\100 \116\111 \121\105\101\108\100")end)R("\112\114\111\112\097\103\097\116\101\115 \101\114\114\111\114 \109\101\115\115\097\103\101\115",function()local h,R=Q.fn(function()error("\111\111\102")end);(G(function()V(R,.1)end)).toThrow("\111\111\102")end)end)else A("\119\105\116\104 \068\069\086\061\102\097\108\115\101",function()R("\100\111\101\115 \110\111\116 \116\104\114\111\119 \111\110 \121\105\101\108\100",function()local h,R=Q.fn(function(V)task.wait(V)end);(G(function()V(R,.1)end)).never.toThrow("\102\117\110\099\116\105\111\110 \105\115 \110\111\116 \097\108\108\111\119\101\100 \116\111 \121\105\101\108\100")end)end)end
local noYield = require('../noYield')

local jestGlobals = require('@pkg/@jsdotlua/jest-globals')

local expect = jestGlobals.expect
local jest = jestGlobals.jest
local it = jestGlobals.it
local describe = jestGlobals.describe

it('calls functions normally', function()
    local testFnMock, testFn = jest.fn(function()
        return 'success'
    end)

    local result = noYield(testFn, 5, false, 6)

    expect(testFnMock).toHaveBeenCalledTimes(1)
    expect(testFnMock).toHaveBeenCalledWith(5, false, 6)
    expect(result).toBe('success')
end)

if _G.DEV then
    describe('with DEV=true', function()
        it('throws on yield', function()
            local _testFnMock, testFn = jest.fn(function(d)
                task.wait(d)
            end)

            expect(function()
                noYield(testFn, 0.1)
            end).toThrow('function is not allowed to yield')
        end)

        it('propagates error messages', function()
            local _testFnMock, testFn = jest.fn(function()
                error('oof')
            end)

            expect(function()
                noYield(testFn, 0.1)
            end).toThrow('oof')
        end)
    end)
else
    describe('with DEV=false', function()
        it('does not throw on yield', function()
            local _testFnMock, testFn = jest.fn(function(d)
                task.wait(d)
            end)

            expect(function()
                noYield(testFn, 0.1)
            end).never.toThrow('function is not allowed to yield')
        end)
    end)
end
