local describe = require("lua_test.test").describe
local test = require("lua_test.test").test
local expect = require("lua_test.test").expect
describe("OnSecondChange handler - thread passthrough", function()
local REG
local dispatcher
local SHIORI_ACT
local function setup()
package.loaded["pasta.shiori.event.second_change"] = nil
package.loaded["pasta.shiori.event.register"] = nil
package.loaded["pasta.shiori.event.virtual_dispatcher"] = nil
package.loaded["pasta.shiori.act"] = nil
dispatcher = require("pasta.shiori.event.virtual_dispatcher")
SHIORI_ACT = require("pasta.shiori.act")
dispatcher._reset()
require("pasta.shiori.event.second_change")
REG = require("pasta.shiori.event.register")
end
test("returns thread when dispatcher returns thread", function()
setup()
dispatcher._set_scene_executor(function(event_name, act)
return coroutine.create(function() return "scene result" end)
end)
local actors = { sakura = { name = "さくら", spot = "sakura" } }
local act = SHIORI_ACT.new(actors, {
id = "OnSecondChange",
status = "idle",
date = { unix = 0, year = 2026, month = 2, day = 1, hour = 14, min = 0, sec = 0, wday = 0 },
})
dispatcher.check_hour(act)
act.req.date.unix = 3600
act.req.date.hour = 15
local result = REG.OnSecondChange(act)
expect(type(result)):toBe("thread")
end)
test("returns nil when dispatcher returns nil", function()
setup()
dispatcher._set_scene_executor(function(event_name, act)
return nil
end)
local STORE = require("pasta.store")
STORE.co_scene = nil
local actors = { sakura = { name = "さくら", spot = "sakura" } }
local act = SHIORI_ACT.new(actors, {
id = "OnSecondChange",
status = "idle",
date = { unix = 0, year = 2026, month = 2, day = 1, hour = 14, min = 0, sec = 0, wday = 0 },
})
dispatcher.check_hour(act)
dispatcher.check_talk(act)
act.req.date.unix = 1
local result = REG.OnSecondChange(act)
expect(result):toBe(nil)
end)
test("passthrough preserves thread from dispatcher", function()
setup()
local expected_thread = coroutine.create(function() return "test value" end)
local original_dispatch = dispatcher.dispatch
dispatcher.dispatch = function(act)
return expected_thread
end
local actors = { sakura = { name = "さくら", spot = "sakura" } }
local act = SHIORI_ACT.new(actors, {
id = "OnSecondChange",
status = "idle",
date = { unix = 0 },
})
local result = REG.OnSecondChange(act)
expect(result):toBe(expected_thread)
dispatcher.dispatch = original_dispatch
end)
end)