local describe = require("lua_test.test").describe
local test = require("lua_test.test").test
local expect = require("lua_test.test").expect
local function create_mock_act(req)
return { req = req }
end
describe("virtual_dispatcher - thread返却", function()
local dispatcher, STORE
local function setup()
package.loaded["pasta.shiori.event.virtual_dispatcher"] = nil
package.loaded["pasta.store"] = nil
dispatcher = require("pasta.shiori.event.virtual_dispatcher")
STORE = require("pasta.store")
dispatcher._reset()
STORE.reset()
end
test("check_hour() が thread を返す", function()
setup()
dispatcher._set_scene_executor(function(event_name, act)
return coroutine.create(function(act)
return "hour_result"
end)
end)
local act = create_mock_act({
id = "OnSecondChange",
status = "idle",
date = { unix = 1702648800, hour = 14 }
})
dispatcher.check_hour(act)
act.req.date.unix = 1702652400
act.req.date.hour = 15
local result = dispatcher.check_hour(act)
expect(type(result)):toBe("thread")
end)
test("check_talk() が thread を返す", function()
setup()
dispatcher._set_scene_executor(function(event_name, act)
return coroutine.create(function(act)
return "talk_result"
end)
end)
local act = create_mock_act({
id = "OnSecondChange",
status = "idle",
date = { unix = 0 }
})
dispatcher.check_hour(act) dispatcher.check_talk(act)
local state = dispatcher._get_internal_state()
act.req.date.unix = state.next_talk_time + 1
local result = dispatcher.check_talk(act)
expect(type(result)):toBe("thread")
end)
test("dispatch() が thread を返す", function()
setup()
dispatcher._set_scene_executor(function(event_name, act)
return coroutine.create(function(act)
return event_name .. "_result"
end)
end)
local act = create_mock_act({
id = "OnSecondChange",
status = "idle",
date = { unix = 0 }
})
dispatcher.dispatch(act)
local state = dispatcher._get_internal_state()
act.req.date.unix = state.next_talk_time + 1
local result = dispatcher.dispatch(act)
expect(type(result)):toBe("thread")
end)
test("シーンが見つからない場合 nil を返す", function()
setup()
dispatcher._set_scene_executor(function(event_name, act)
return nil
end)
local act = create_mock_act({
id = "OnSecondChange",
status = "idle",
date = { unix = 0 }
})
dispatcher.check_hour(act)
dispatcher.check_talk(act)
local state = dispatcher._get_internal_state()
act.req.date.unix = state.next_talk_time + 1
local result = dispatcher.check_talk(act)
expect(result):toBe(nil)
end)
end)
describe("virtual_dispatcher - チェイントーク継続", function()
local dispatcher, STORE
local function setup()
package.loaded["pasta.shiori.event.virtual_dispatcher"] = nil
package.loaded["pasta.store"] = nil
dispatcher = require("pasta.shiori.event.virtual_dispatcher")
STORE = require("pasta.store")
dispatcher._reset()
STORE.reset()
end
test("STORE.co_scene が存在する場合 check_talk() は継続用 thread を返す", function()
setup()
local existing_co = coroutine.create(function(act)
coroutine.yield("first")
return "final"
end)
coroutine.resume(existing_co)
local act = create_mock_act({
id = "OnSecondChange",
status = "idle",
date = { unix = 0 }
})
dispatcher.check_hour(act)
dispatcher.check_talk(act)
local state = dispatcher._get_internal_state()
act.req.date.unix = state.next_talk_time + 1
STORE.co_scene = existing_co
local result = dispatcher.check_talk(act)
expect(result):toBe(existing_co)
end)
test("STORE.co_scene が nil の場合 新規シーンを検索", function()
setup()
dispatcher._set_scene_executor(function(event_name, act)
return coroutine.create(function(act)
return "new_scene"
end)
end)
expect(STORE.co_scene):toBe(nil)
local act = create_mock_act({
id = "OnSecondChange",
status = "idle",
date = { unix = 0 }
})
dispatcher.check_hour(act)
dispatcher.check_talk(act)
local state = dispatcher._get_internal_state()
act.req.date.unix = state.next_talk_time + 1
local result = dispatcher.check_talk(act)
expect(type(result)):toBe("thread")
end)
test("OnHour は STORE.co_scene を無視して常に新規 thread を返す", function()
setup()
local existing_co = coroutine.create(function(act)
coroutine.yield("first")
end)
coroutine.resume(existing_co)
STORE.co_scene = existing_co
dispatcher._set_scene_executor(function(event_name, act)
return coroutine.create(function(act)
return "hour_scene"
end)
end)
local act = create_mock_act({
id = "OnSecondChange",
status = "idle",
date = { unix = 0, hour = 14 }
})
dispatcher.check_hour(act)
act.req.date.unix = 3600
act.req.date.hour = 15
local result = dispatcher.check_hour(act)
expect(type(result)):toBe("thread")
expect(result ~= existing_co):toBe(true)
end)
end)
return true