From 35fc5b16b2ad569ad86005087253ba90fda51d8e Mon Sep 17 00:00:00 2001 From: Aaron Kimbrell Date: Fri, 27 Nov 2026 18:24:09 -0600 Subject: [PATCH] test(scripts): which server script an object runs Custom script over a ported row script, over the empty script and with no row script; empty custom script keeps the row's; an unported custom script still wins (no script); once-per-LOT reporting; IsPorted. Co-Authored-By: Claude Opus 5.5 --- .../dComponentsTests/CMakeLists.txt | 1 + .../dComponentsTests/ScriptComponentTests.cpp | 101 ++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 tests/dGameTests/dComponentsTests/ScriptComponentTests.cpp diff --git a/tests/dGameTests/dComponentsTests/CMakeLists.txt b/tests/dGameTests/dComponentsTests/CMakeLists.txt index d5c8fe3fa..8a657b8ed 100644 --- a/tests/dGameTests/dComponentsTests/CMakeLists.txt +++ b/tests/dGameTests/dComponentsTests/CMakeLists.txt @@ -1,6 +1,7 @@ set(DCOMPONENTS_TESTS "DeletionRestrictionTests.cpp" "MovingPlatformComponentTests.cpp" + "ScriptComponentTests.cpp" "InventorySaveTests.cpp" "EquipMessagesTests.cpp" "DestroyableLevelConfigTests.cpp" diff --git a/tests/dGameTests/dComponentsTests/ScriptComponentTests.cpp b/tests/dGameTests/dComponentsTests/ScriptComponentTests.cpp new file mode 100644 index 000000000..aa268307f --- /dev/null +++ b/tests/dGameTests/dComponentsTests/ScriptComponentTests.cpp @@ -0,0 +1,101 @@ +// Which server script an object runs: as the client picks its script, a non-empty custom script in the level config +// always wins over the ScriptComponent row's; an empty or missing one leaves the row's. +#include "GameDependencies.h" + +#include "GameMessages.h" +#include "CDClientManager.h" +#include "CDScriptComponentTable.h" +#include "Entity.h" +#include "ScriptComponent.h" + +#include + +namespace { + const std::string cannonScript = "scripts\\ai\\MINIGAME\\SG_GF\\SERVER\\SG_CANNON.lua"; + const std::string targetScript = "scripts/ai/ACT/SG_TARGET.lua"; + const std::string emptyScript = "scripts\\empty.lua"; +} + +class ScriptComponentTests : public GameDependenciesTest { +protected: + std::unique_ptr entity; + + void SetUp() override { + SetUpDependencies(); + auto& scripts = CDClientManager::GetEntriesMutable(); + scripts[90001] = { 90001, cannonScript, "" }; + scripts[90002] = { 90002, emptyScript, "" }; + scripts[90003] = { 90003, "", "" }; + entity = std::make_unique(1, info); + } + + void TearDown() override { + entity.reset(); + auto& scripts = CDClientManager::GetEntriesMutable(); + scripts.erase(90001); + scripts.erase(90002); + scripts.erase(90003); + TearDownDependencies(); + } + + CppScripts::Script* Load(const int32_t componentID) { + ScriptComponent component(entity.get(), componentID); + component.LoadDataFromTemplate(); + component.LoadConfigData(); + return component.GetScript(); + } +}; + +TEST(ScriptComponentChoice, CustomScriptWinsOverTheRowScript) { + EXPECT_EQ(ScriptComponent::ChooseServerScript(cannonScript, targetScript), targetScript); + EXPECT_EQ(ScriptComponent::ChooseServerScript(emptyScript, targetScript), targetScript); + EXPECT_EQ(ScriptComponent::ChooseServerScript("", targetScript), targetScript); +} + +TEST(ScriptComponentChoice, EmptyCustomScriptKeepsTheRowScript) { + EXPECT_EQ(ScriptComponent::ChooseServerScript(cannonScript, ""), cannonScript); + EXPECT_EQ(ScriptComponent::ChooseServerScript("", ""), ""); +} + +TEST(ScriptComponentChoice, UnportedReplacementIsReportedOncePerLot) { + EXPECT_TRUE(ScriptComponent::FirstUnportedReplacement(999901)); + EXPECT_FALSE(ScriptComponent::FirstUnportedReplacement(999901)); + EXPECT_TRUE(ScriptComponent::FirstUnportedReplacement(999902)); +} + +TEST(ScriptComponentChoice, PortedScripts) { + EXPECT_TRUE(CppScripts::IsPorted(cannonScript)); + EXPECT_TRUE(CppScripts::IsPorted(targetScript)); + EXPECT_TRUE(CppScripts::IsPorted(emptyScript)); // left out on purpose + EXPECT_FALSE(CppScripts::IsPorted("scripts\\no\\such\\script.lua")); +} + +TEST_F(ScriptComponentTests, RowScriptWithoutCustomScript) { + EXPECT_EQ(Load(90001), CppScripts::GetScript(entity.get(), cannonScript)); +} + +TEST_F(ScriptComponentTests, EmptyCustomScriptKeepsTheRowScript) { + entity->SetVar(u"custom_script_server", ""); + EXPECT_EQ(Load(90001), CppScripts::GetScript(entity.get(), cannonScript)); +} + +TEST_F(ScriptComponentTests, CustomScriptWinsOverAPortedRowScript) { + entity->SetVar(u"custom_script_server", targetScript); + EXPECT_EQ(Load(90001), CppScripts::GetScript(entity.get(), targetScript)); +} + +TEST_F(ScriptComponentTests, CustomScriptWinsOverTheEmptyScript) { + entity->SetVar(u"custom_script_server", targetScript); + EXPECT_EQ(Load(90002), CppScripts::GetScript(entity.get(), targetScript)); +} + +TEST_F(ScriptComponentTests, CustomScriptWithoutARowScript) { + entity->SetVar(u"custom_script_server", targetScript); + EXPECT_EQ(Load(90003), CppScripts::GetScript(entity.get(), targetScript)); +} + +// Not ported: the object runs no script (the row's is lost until the custom one is ported) +TEST_F(ScriptComponentTests, UnportedCustomScriptStillWins) { + entity->SetVar(u"custom_script_server", std::string("scripts\\no\\such\\script.lua")); + EXPECT_EQ(Load(90001), CppScripts::GetInvalidScript()); +}