use crate::common;
use common::copy_fixture_to_temp;
use pasta_lua::loader::PastaLoader;
#[test]
fn test_store_actors_is_config_actor_reference() {
let temp = copy_fixture_to_temp("with_actor_config");
let runtime = PastaLoader::load(temp.path()).unwrap();
let result = runtime
.exec(
r#"
local CONFIG = require "@pasta_config"
local STORE = require "pasta.store"
return CONFIG.actor == STORE.actors
"#,
)
.unwrap();
assert!(
result.as_boolean() == Some(true),
"CONFIG.actor と STORE.actors は同一参照であるべき"
);
}
#[test]
fn test_store_actors_has_config_values() {
let temp = copy_fixture_to_temp("with_actor_config");
let runtime = PastaLoader::load(temp.path()).unwrap();
let result = runtime
.exec(
r#"
local STORE = require "pasta.store"
return STORE.actors["さくら"].spot
"#,
)
.unwrap();
assert_eq!(result.as_i64(), Some(0), "さくらの spot は 0 であるべき");
let result = runtime
.exec(
r#"
local STORE = require "pasta.store"
return STORE.actors["うにゅう"].spot
"#,
)
.unwrap();
assert_eq!(result.as_i64(), Some(1), "うにゅうの spot は 1 であるべき");
}
#[test]
fn test_store_actors_empty_when_no_config_actor() {
let temp = copy_fixture_to_temp("minimal");
let runtime = PastaLoader::load(temp.path()).unwrap();
let result = runtime
.exec(
r#"
local STORE = require "pasta.store"
local count = 0
for _ in pairs(STORE.actors) do count = count + 1 end
return count
"#,
)
.unwrap();
assert_eq!(
result.as_i64(),
Some(0),
"CONFIG.actor が無い場合、STORE.actors は空テーブル"
);
}
#[test]
fn test_config_actors_have_metatable() {
let temp = copy_fixture_to_temp("with_actor_config");
let runtime = PastaLoader::load(temp.path()).unwrap();
let result = runtime
.exec(
r#"
local STORE = require "pasta.store"
local actor = STORE.actors["さくら"]
return getmetatable(actor) ~= nil
"#,
)
.unwrap();
assert!(
result.as_boolean() == Some(true),
"CONFIG 由来アクターにはメタテーブルが設定されるべき"
);
}
#[test]
fn test_config_actors_can_use_metatable_methods() {
let temp = copy_fixture_to_temp("with_actor_config");
let runtime = PastaLoader::load(temp.path()).unwrap();
let result = runtime
.exec(
r#"
local ACTOR = require "pasta.actor"
local actor = ACTOR.get_or_create("さくら")
local builder = actor:create_word("表情")
return type(builder) == "table"
"#,
)
.unwrap();
assert!(
result.as_boolean() == Some(true),
":create_word メソッドが利用可能であるべき"
);
}
#[test]
fn test_get_or_create_preserves_config_properties() {
let temp = copy_fixture_to_temp("with_actor_config");
let runtime = PastaLoader::load(temp.path()).unwrap();
let result = runtime
.exec(
r#"
local ACTOR = require "pasta.actor"
local actor = ACTOR.get_or_create("さくら")
return actor.spot
"#,
)
.unwrap();
assert_eq!(
result.as_i64(),
Some(0),
"get_or_create は CONFIG 由来の spot を保持すべき"
);
}
#[test]
fn test_dynamic_and_config_actors_coexist() {
let temp = copy_fixture_to_temp("with_actor_config");
let runtime = PastaLoader::load(temp.path()).unwrap();
let result = runtime
.exec(
r#"
local STORE = require "pasta.store"
local ACTOR = require "pasta.actor"
-- 動的にアクターを追加
local new_actor = ACTOR.get_or_create("マル")
new_actor.spot = 2
-- CONFIG 由来アクターが残っていることを確認
local sakura = STORE.actors["さくら"]
local maru = STORE.actors["マル"]
return sakura ~= nil and maru ~= nil and sakura.spot == 0 and maru.spot == 2
"#,
)
.unwrap();
assert!(
result.as_boolean() == Some(true),
"動的追加と CONFIG 由来アクターが共存すべき"
);
}
#[test]
fn test_config_actors_have_name_field() {
let temp = copy_fixture_to_temp("with_actor_config");
let runtime = PastaLoader::load(temp.path()).unwrap();
let result = runtime
.exec(
r#"
local STORE = require "pasta.store"
return STORE.actors["さくら"].name == "さくら"
"#,
)
.unwrap();
assert!(
result.as_boolean() == Some(true),
"CONFIG由来アクターに name = 'さくら' が注入されるべき"
);
let result = runtime
.exec(
r#"
local STORE = require "pasta.store"
return STORE.actors["うにゅう"].name == "うにゅう"
"#,
)
.unwrap();
assert!(
result.as_boolean() == Some(true),
"CONFIG由来アクターに name = 'うにゅう' が注入されるべき"
);
}
#[test]
fn test_config_actor_name_via_config_module() {
let temp = copy_fixture_to_temp("with_actor_config");
let runtime = PastaLoader::load(temp.path()).unwrap();
let result = runtime
.exec(
r#"
local CONFIG = require "@pasta_config"
return CONFIG.actor["さくら"].name == "さくら"
"#,
)
.unwrap();
assert!(
result.as_boolean() == Some(true),
"CONFIG.actor 経由でも name が参照できるべき(参照共有)"
);
}
#[test]
fn test_dynamic_actor_reflects_in_config() {
let temp = copy_fixture_to_temp("with_actor_config");
let runtime = PastaLoader::load(temp.path()).unwrap();
let result = runtime
.exec(
r#"
local CONFIG = require "@pasta_config"
local STORE = require "pasta.store"
local ACTOR = require "pasta.actor"
-- 動的にアクターを追加
local new_actor = ACTOR.get_or_create("マル")
new_actor.spot = 2
-- CONFIG.actor にも反映されていることを確認(参照共有)
return CONFIG.actor["マル"] ~= nil and CONFIG.actor["マル"].spot == 2
"#,
)
.unwrap();
assert!(
result.as_boolean() == Some(true),
"STORE.actors への変更は CONFIG.actor にも反映されるべき(参照共有)"
);
}