<commit_before>_G.remote = {
    interfaces={}
}

_G.script = {
    next_event_id=5000,  --Try to avoid clobbering factorio event ids
    generate_event_name = function()
        _G.script.next_event_id = _G.script.next_event_id + 1
        return _G.script.next_event_id
    end,
    on_event = function(id, callback) return end,
    raise_event = function(event_id, event_tbl)
        event_tbl.name = event_id
        Event.dispatch(event_tbl)
    end,
    on_init = function() _G.global._surface_time = {} end,
    on_configuration_changed = function() _G.global._surface_time = _G.global._surface_time or {} end
}

_G.global = { }

require 'spec/defines'
require 'stdlib/event/time'

describe('Event.Time', function()
    before_each(function()
        _G.game = {
            players = {{valid=false}},
            surfaces = {},
            tick = 0,
        }

        _G.game.surfaces.nauvis = {
            daytime = 0,
            index = 1,
            name = "nauvis"
        }

        _G.game.surfaces.testsur = {
            daytime = 0.5,
            index = 2,
            name = "testsur"
        }

        _G.global._surface_time = {}
        _G.nauvis_event_table = {surface = _G.game.surfaces.nauvis}
        _G.testsur_event_table = {surface = _G.game.surfaces.testsur}

        _G.simulate_time = function(ticks)
            local daytime_per_tick = 1 / defines.time.day
            local original_daytime = {}
            for _, surface in pairs(game.surfaces) do
                original_daytime[surface] = surface.daytime
            end
            for i = 1, ticks do
                -- advance daytime for each day
                for _, surface in pairs(game.surfaces) do
                    -- Set daytime based on new number of ticks, in the interval [0,1)
                    surface.daytime = (original_daytime[surface] + i * daytime_per_tick) % 1
                end
                game.tick = game.tick + 1
                Event.dispatch({name = defines.events.on_tick, tick = game.tick})
            end
        end
end)


    it('should dispatch 3600 minutely events and 24 hourly events per day', function()
        --initialize the first tick of the day
        Event.dispatch({name = defines.events.on_tick, tick = 0})

        local counter = { minutely = function() end, hourly = function() end }

        Event.register(Event.Time.minutely, function() counter.minutely() end )
        local minute_spy = spy.on(counter, "minutely")

        Event.register(Event.Time.hourly, function() counter.hourly() end )
        local hour_spy = spy.on(counter, "hourly")

        simulate_time(defines.time.day)

        -- 2 surfaces!
        assert.spy(minute_spy).was_called(1440 * 2)
        assert.spy(hour_spy).was_called(24 * 2)
    end)

    it('should dispatch sunset, sunrise, midnight and midday events once per day', function()
        --initialize the first tick of the day
        Event.dispatch({name = defines.events.on_tick, tick = 0})

        local counter = { sunrise = function() end, sunset = function() end, midnight = function() end, midday = function() end }

        Event.register(Event.Time.sunset, function() counter.sunset() end )
        local sunset_spy = spy.on(counter, "sunset")

        Event.register(Event.Time.sunrise, function() counter.sunrise() end )<commit_msg>Lower the time spec test times<commit_after>_G.remote = {
    interfaces={}
}

_G.script = {
    next_event_id=5000,  --Try to avoid clobbering factorio event ids
    generate_event_name = function()
        _G.script.next_event_id = _G.script.next_event_id + 1
        return _G.script.next_event_id
    end,
    on_event = function(id, callback) return end,
    raise_event = function(event_id, event_tbl)
        event_tbl.name = event_id
        Event.dispatch(event_tbl)
    end,
    on_init = function() _G.global._surface_time = {} end,
    on_configuration_changed = function() _G.global._surface_time = _G.global._surface_time or {} end
}

_G.global = { }

require 'spec/defines'
require 'stdlib/event/time'

describe('Event.Time', function()
    before_each(function()
        _G.game = {
            players = {{valid=false}},
            surfaces = {},
            tick = 0,
        }

        _G.game.surfaces.nauvis = {
            daytime = 0,
            index = 1,
            name = "nauvis"
        }

        _G.game.surfaces.testsur = {
            daytime = 0.5,
            index = 2,
            name = "testsur"
        }

        _G.global._surface_time = {}
        _G.nauvis_event_table = {surface = _G.game.surfaces.nauvis}
        _G.testsur_event_table = {surface = _G.game.surfaces.testsur}

        _G.simulate_time = function(ticks)
            local daytime_per_tick = 1 / defines.time.day
            local original_daytime = {}
            for _, surface in pairs(game.surfaces) do
                original_daytime[surface] = surface.daytime
            end
            for i = 1, ticks do
                -- advance daytime for each day
                for _, surface in pairs(game.surfaces) do
                    -- Set daytime based on new number of ticks, in the interval [0,1)
                    surface.daytime = (original_daytime[surface] + i * daytime_per_tick) % 1
                end
                game.tick = game.tick + 1
                Event.dispatch({name = defines.events.on_tick, tick = game.tick})
            end
        end
    end)


    it('should dispatch 60 minutely events and 1 hourly events per hour', function()
        --initialize the first tick of the day
        Event.dispatch({name = defines.events.on_tick, tick = 0})

        local counter = { minutely = function() end, hourly = function() end }

        Event.register(Event.Time.minutely, function() counter.minutely() end )
        local minute_spy = spy.on(counter, "minutely")

        Event.register(Event.Time.hourly, function() counter.hourly() end )
        local hour_spy = spy.on(counter, "hourly")

        simulate_time(defines.time.hour * 1 + 1)

        -- 2 surfaces!
        assert.spy(minute_spy).was_called(60 * 2)
        assert.spy(hour_spy).was_called(1 * 2)
    end)

    it('should dispatch sunset, sunrise, midnight and midday events once per day', function()
        --initialize the first tick of the day
        Event.dispatch({name = defines.events.on_tick, tick = 0})

        local counter = { sunrise = function() end, sunset = function() end, midnight = function() end, midday = function() end }

        Event.register(Event.Time.sunset, function() counter.sunset() end )
        local sunset_spy = spy.on(counter, "sunset")

        Event.register(Event.Time.sunrise, function() counter.sunrise() end )
{'fraction_non_alphanumeric': 0.10797912005965697, 'fraction_numerical': 0.009545115585384041, 'mean_word_length': 2.116171003717472, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6240786', 'n_tokens_mistral': 2113, 'n_tokens_neox': 1988, 'n_words': 485}