local ____lualib = require("lualib_bundle") local __TS__New = ____lualib.__TS__New local ____exports = __module.env local M__share_base_scoped_object = __MODULE__.Import("share.base.scoped_object", __module) local ScopedObject = M__share_base_scoped_object.ScopedObject local M__framework_core_scheduler = __MODULE__.Import("framework.core.scheduler", __module) local FrameScheduler = M__framework_core_scheduler.FrameScheduler local M__tests_harness_test_runner = __MODULE__.Import("tests.harness.test_runner", __module) local AssertArrayEqual = M__tests_harness_test_runner.AssertArrayEqual local AssertEqual = M__tests_harness_test_runner.AssertEqual function ____exports.CreateSchedulerSuite() return { name = "Step1.Scheduler", cases = { { name = "Frame timers: 0 and 1 fire next frame, N fires on N-th frame", fn = function() local scheduler = __TS__New(FrameScheduler) local fired = {} scheduler:AddFrameTimer( 0, function() fired[#fired + 1] = "f0@" .. tostring(scheduler:GetFrameCount()) end ) scheduler:AddFrameTimer( 1, function() fired[#fired + 1] = "f1@" .. tostring(scheduler:GetFrameCount()) end ) scheduler:AddFrameTimer( 3, function() fired[#fired + 1] = "f3@" .. tostring(scheduler:GetFrameCount()) end ) scheduler:Tick(0.016) AssertArrayEqual(fired, {"f0@1", "f1@1"}) scheduler:Tick(0.016) AssertArrayEqual(fired, {"f0@1", "f1@1"}) scheduler:Tick(0.016) AssertArrayEqual(fired, {"f0@1", "f1@1", "f3@3"}) AssertEqual( scheduler:GetStats().totalTasks, 0, "all frame tasks completed" ) scheduler:Dispose() end }, { name = "World pause freezes world timers while monotonic timers continue", fn = function() local scheduler = __TS__New(FrameScheduler) local log = {} scheduler:AddWorldTimer( 0.1, function() log[#log + 1] = "world" end ) scheduler:AddMonotonicTimer( 0.1, function() log[#log + 1] = "mono" end ) scheduler:Tick(0.06, 0.06, true) scheduler:Tick(0.06, 0.06, true) AssertArrayEqual(log, {"mono"}) scheduler:Tick(0.05, 0.05, false) AssertArrayEqual(log, {"mono"}) scheduler:Tick(0.06, 0.06, false) AssertArrayEqual(log, {"mono", "world"}) AssertEqual( scheduler:GetStats().totalTasks, 0 ) scheduler:Dispose() end }, { name = "World invalidation cancels world & frame tasks but preserves monotonic tasks", fn = function() local scheduler = __TS__New(FrameScheduler) local log = {} scheduler:AddFrameTimer( 2, function() log[#log + 1] = "frame" end ) scheduler:AddWorldTimer( 0.05, function() log[#log + 1] = "world" end ) scheduler:AddMonotonicTimer( 0.05, function() log[#log + 1] = "mono" end ) local nextGen = scheduler:InvalidateWorld() AssertEqual(nextGen, 2, "worldGen incremented") AssertEqual( scheduler:GetStats().worldTasks, 0, "world tasks cleared" ) AssertEqual( scheduler:GetStats().frameTasks, 0, "frame tasks cleared" ) AssertEqual( scheduler:GetStats().monotonicTasks, 1, "monotonic task survives" ) scheduler:Tick(0.06) scheduler:Tick(0.06) AssertArrayEqual(log, {"mono"}) AssertEqual( scheduler:GetStats().totalTasks, 0 ) scheduler:Dispose() end }, { name = "Disposing ScopedObject owner automatically cancels owned timers", fn = function() local scheduler = __TS__New(FrameScheduler) local owner = __TS__New(ScopedObject) local count = 0 scheduler:AddMonotonicRepeatTimer( 0.05, function() count = count + 1 end, owner ) scheduler:Tick(0.06) AssertEqual(count, 1) owner:Dispose() scheduler:Tick(0.06) scheduler:Tick(0.06) AssertEqual(count, 1, "disposed owner timer must not fire") AssertEqual( scheduler:GetStats().totalTasks, 0, "timer cleaned up" ) scheduler:Dispose() end } } } end function __module.UpdateImports() ScopedObject = M__share_base_scoped_object.ScopedObject FrameScheduler = M__framework_core_scheduler.FrameScheduler AssertArrayEqual = M__tests_harness_test_runner.AssertArrayEqual AssertEqual = M__tests_harness_test_runner.AssertEqual end