local describe = require("lua_test.test").describe
local test = require("lua_test.test").test
local expect = require("lua_test.test").expect
describe("Integration - GLOBAL fallback via EVENT.fire", function()
local EVENT
local STORE
local GLOBAL
local function setup()
package.loaded["pasta.store"] = nil
package.loaded["pasta.shiori.event"] = nil
package.loaded["pasta.shiori.event.register"] = nil
package.loaded["pasta.shiori.res"] = nil
package.loaded["pasta.shiori.act"] = nil
package.loaded["pasta.act"] = nil
package.loaded["pasta.global"] = nil
package.loaded["pasta.scene"] = nil
STORE = require("pasta.store")
EVENT = require("pasta.shiori.event")
GLOBAL = require("pasta.global")
STORE.actors = { sakura = { name = "さくら", spot = 0 } }
STORE.co_scene = nil
end
test("GLOBAL.OnHourOther が EVENT.fire 経由で呼ばれる(DSL ラベルなし)", function()
setup()
local global_called = false
GLOBAL.OnHourOther = function(act)
global_called = true
act:talk(act.actors.sakura, "正午です")
end
local response = EVENT.fire({ id = "OnHourOther", method = "get", version = 30 })
expect(global_called):toBe(true)
expect(response:find("SHIORI/3.0 200 OK")).not_:toBe(nil)
expect(response:find("正午です")).not_:toBe(nil)
GLOBAL.OnHourOther = nil
end)
test("GLOBAL に未登録のイベントは 204 No Content を返す", function()
setup()
local response = EVENT.fire({ id = "OnUnknownGlobalEvent", method = "get", version = 30 })
expect(response:find("204 No Content")).not_:toBe(nil)
end)
end)
describe("Integration - DSL scene priority over GLOBAL", function()
local EVENT
local STORE
local SCENE
local GLOBAL
local function setup()
package.loaded["pasta.store"] = nil
package.loaded["pasta.shiori.event"] = nil
package.loaded["pasta.shiori.event.register"] = nil
package.loaded["pasta.shiori.res"] = nil
package.loaded["pasta.shiori.act"] = nil
package.loaded["pasta.act"] = nil
package.loaded["pasta.global"] = nil
package.loaded["pasta.scene"] = nil
package.loaded["@pasta_search"] = { search_scene = function() end, search_word = function() return nil end }
STORE = require("pasta.store")
EVENT = require("pasta.shiori.event")
SCENE = require("pasta.scene")
GLOBAL = require("pasta.global")
STORE.actors = { sakura = { name = "さくら", spot = 0 } }
STORE.co_scene = nil
end
test("GLOBAL (L4) はグローバル辞書 (L5) より優先される", function()
setup()
local global_called = false
GLOBAL.OnPriorityTest = function(act)
global_called = true
act:talk(act.actors.sakura, "GLOBAL版")
end
local dsl_called = false
local dsl_scene_fn = function(act)
dsl_called = true
act:talk(act.actors.sakura, "DSL版")
end
local original_search = SCENE.search
SCENE.search = function(name, scope, attrs)
if name == "OnPriorityTest" then
return {
func = dsl_scene_fn,
global_name = "talk",
local_name = "OnPriorityTest",
}
end
return nil
end
local response = EVENT.fire({ id = "OnPriorityTest", method = "get", version = 30 })
expect(global_called):toBe(true)
expect(dsl_called):toBe(false)
expect(response:find("GLOBAL版")).not_:toBe(nil)
SCENE.search = original_search
GLOBAL.OnPriorityTest = nil
end)
test("DSL が nil で GLOBAL がある場合、GLOBAL にフォールバックする", function()
setup()
local original_search = SCENE.search
SCENE.search = function() return nil end
local global_called = false
GLOBAL.OnFallbackTest = function(act)
global_called = true
act:talk(act.actors.sakura, "GLOBALフォールバック")
end
local response = EVENT.fire({ id = "OnFallbackTest", method = "get", version = 30 })
expect(global_called):toBe(true)
expect(response:find("GLOBALフォールバック")).not_:toBe(nil)
SCENE.search = original_search
GLOBAL.OnFallbackTest = nil
end)
end)
describe("Integration - GLOBAL fallback for OnTalk (Req 2.2)", function()
local EVENT
local STORE
local GLOBAL
local function setup()
package.loaded["pasta.store"] = nil
package.loaded["pasta.shiori.event"] = nil
package.loaded["pasta.shiori.event.register"] = nil
package.loaded["pasta.shiori.res"] = nil
package.loaded["pasta.shiori.act"] = nil
package.loaded["pasta.act"] = nil
package.loaded["pasta.global"] = nil
package.loaded["pasta.scene"] = nil
STORE = require("pasta.store")
EVENT = require("pasta.shiori.event")
GLOBAL = require("pasta.global")
STORE.actors = { sakura = { name = "さくら", spot = 0 } }
STORE.co_scene = nil
end
test("GLOBAL.OnTalk が EVENT.fire 経由で呼ばれる(DSL ラベルなし)", function()
setup()
local global_called = false
GLOBAL.OnTalk = function(act)
global_called = true
act:talk(act.actors.sakura, "ランダムトーク")
end
local response = EVENT.fire({ id = "OnTalk", method = "get", version = 30 })
expect(global_called):toBe(true)
expect(response:find("SHIORI/3.0 200 OK")).not_:toBe(nil)
expect(response:find("ランダムトーク")).not_:toBe(nil)
GLOBAL.OnTalk = nil
end)
end)
describe("Integration - act:build() works via both call and co_exec (Req 4.3)", function()
local EVENT
local STORE
local GLOBAL
local SHIORI_ACT
local function setup()
package.loaded["pasta.store"] = nil
package.loaded["pasta.shiori.event"] = nil
package.loaded["pasta.shiori.event.register"] = nil
package.loaded["pasta.shiori.res"] = nil
package.loaded["pasta.shiori.act"] = nil
package.loaded["pasta.act"] = nil
package.loaded["pasta.global"] = nil
package.loaded["pasta.scene"] = nil
STORE = require("pasta.store")
EVENT = require("pasta.shiori.event")
GLOBAL = require("pasta.global")
SHIORI_ACT = require("pasta.shiori.act")
STORE.actors = { sakura = { name = "さくら", spot = 0 } }
STORE.co_scene = nil
end
test("EVENT.fire 経由(co_exec)で act:build() がさくらスクリプトを構築する", function()
setup()
GLOBAL.OnBuildTestCoExec = function(act)
act:talk(act.actors.sakura, "co_exec経由ビルド")
end
local response = EVENT.fire({ id = "OnBuildTestCoExec", method = "get", version = 30 })
expect(response:find("200 OK")).not_:toBe(nil)
expect(response:find("co_exec経由ビルド")).not_:toBe(nil)
GLOBAL.OnBuildTestCoExec = nil
end)
test("act:call() 経由で act:build() がさくらスクリプトを構築する", function()
setup()
GLOBAL.OnBuildTestCall = function(act)
act:talk(act.actors.sakura, "call経由ビルド")
end
local act = SHIORI_ACT.new(STORE.actors)
act:call(nil, "OnBuildTestCall", nil)
local result = act:build()
expect(result).not_:toBe(nil)
expect(result:find("call経由ビルド")).not_:toBe(nil)
GLOBAL.OnBuildTestCall = nil
end)
end)