local describe = require("lua_test.test").describe
local test = require("lua_test.test").test
local expect = require("lua_test.test").expect
local function with_fresh_modules(config_stub, load_actor, body)
local saved = {
store = package.loaded["pasta.store"],
actor = package.loaded["pasta.actor"],
config = package.loaded["@pasta_config"],
}
package.loaded["pasta.store"] = nil
package.loaded["@pasta_config"] = config_stub
if load_actor then
package.loaded["pasta.actor"] = nil
end
local ok, err = pcall(function()
local fresh_store = require("pasta.store")
if load_actor then
require("pasta.actor")
end
body(fresh_store)
end)
package.loaded["pasta.store"] = saved.store
package.loaded["pasta.actor"] = saved.actor
package.loaded["@pasta_config"] = saved.config
if not ok then error(err, 0) end
end
describe("pasta.store - CONFIG.actor 初期化", function()
test("CONFIG.actor が STORE.actors として採用される", function()
local config = { actor = { さくら = { spot = 0 } } }
with_fresh_modules(config, false, function(store)
expect(store.actors):toBe(config.actor)
expect(store.actors["さくら"]):toBe(config.actor["さくら"])
end)
end)
test("数値 spot のみ actor_spots へ転送される(欠落・非数値・非テーブルは転送なし)", function()
local config = {
actor = {
さくら = { spot = 0 },
うにゅう = { spot = 2 },
無指定 = {},
文字spot = { spot = "left" },
非テーブル = "ただの文字列",
},
}
with_fresh_modules(config, false, function(store)
expect(store.actor_spots["さくら"]):toBe(0)
expect(store.actor_spots["うにゅう"]):toBe(2)
expect(store.actor_spots["無指定"]):toBeNil()
expect(store.actor_spots["文字spot"]):toBeNil()
expect(store.actor_spots["非テーブル"]):toBeNil()
end)
end)
end)
describe("pasta.actor - CONFIG 由来アクターのメタテーブル設定", function()
test("name 欠落アクターは辞書キーで補完され、ACTOR_IMPL メソッドが利用可能になる", function()
local config = { actor = { さくら = { spot = 0 } } }
with_fresh_modules(config, true, function(store)
local actor = store.actors["さくら"]
expect(actor.name):toBe("さくら")
expect(type(actor.create_word)):toBe("function")
end)
end)
test("name 既設アクターは上書きされない・非テーブル要素はスキップされる", function()
local config = {
actor = {
別名持ち = { name = "カスタム名" },
非テーブル = "文字列要素",
},
}
with_fresh_modules(config, true, function(store)
expect(store.actors["別名持ち"].name):toBe("カスタム名")
expect(store.actors["非テーブル"]):toBe("文字列要素")
end)
end)
end)