Fix tests and scoping

This commit is contained in:
Chris Miles
2025-06-16 15:55:51 -05:00
parent 0d93a1ad38
commit d9bf8d5c88
2 changed files with 62 additions and 31 deletions
+55 -27
View File
@@ -5,9 +5,9 @@
#include "../../client.h" #include "../../client.h"
#include "../../common/net/eqstream.h" #include "../../common/net/eqstream.h"
extern Zone *zone; extern Zone* zone;
void ZoneCLI::TestDataBuckets(int argc, char **argv, argh::parser &cmd, std::string &description) void ZoneCLI::TestDataBuckets(int argc, char** argv, argh::parser& cmd, std::string& description)
{ {
if (cmd[{"-h", "--help"}]) { if (cmd[{"-h", "--help"}]) {
return; return;
@@ -25,14 +25,36 @@ void ZoneCLI::TestDataBuckets(int argc, char **argv, argh::parser &cmd, std::str
entity_list.Process(); entity_list.Process();
entity_list.MobProcess(); entity_list.MobProcess();
Client* client = new Client();
client->SetCharacterId(1); // Set a dummy character ID for testing
LogSys.EnableConsoleLogging(); LogSys.EnableConsoleLogging();
LogSys.log_settings[Logs::MySQLQuery].is_category_enabled = std::getenv("DEBUG") ? 1 : 0;
LogSys.log_settings[Logs::MySQLQuery].log_to_console = std::getenv("DEBUG") ? 3 : 0;
// 🧹 Delete all test keys before running tests
std::vector<std::string> test_keys_to_clear = {
"basic_key", "expiring_key", "cache_key", "json_key", "non_existent_key", "simple_key",
"nested", "nested.test1", "nested.test2", "nested.test1.a", "nested.test2.a",
"exp_test", "cache_test", "full_json", "full_json.key2", "complex", "complex.nested.obj1",
"complex.nested.obj2", "plain_string", "json_array", "nested_partial",
"nested_override", "empty_json", "json_string", "deep_nested",
"nested_expire.test.test", "scoped_miss_test", "scoped_nested_miss.key",
"cache_miss_overwrite", "missed_nested_set.test", "account_client_test", "ac_nested.test",
"scoped_db_only_key"
};
DataBucketsRepository::DeleteWhere(
database,
fmt::format("`key` IN ('{}')", Strings::Join(test_keys_to_clear, "','"))
);
DataBucket::ClearCache();
std::cout << "===========================================\n"; std::cout << "===========================================\n";
std::cout << "\uFE0F> Running DataBuckets Tests...\n"; std::cout << "\uFE0F> Running DataBuckets Tests...\n";
std::cout << "===========================================\n\n"; std::cout << "===========================================\n\n";
Client *client = new Client();
// Basic Key-Value Set/Get // Basic Key-Value Set/Get
client->DeleteBucket("basic_key"); client->DeleteBucket("basic_key");
client->SetBucket("basic_key", "simple_value"); client->SetBucket("basic_key", "simple_value");
@@ -162,7 +184,8 @@ void ZoneCLI::TestDataBuckets(int argc, char **argv, argh::parser &cmd, std::str
client->SetBucket("complex.nested.obj2", "data2"); client->SetBucket("complex.nested.obj2", "data2");
client->DeleteBucket("does_not_exist"); // Should do nothing client->DeleteBucket("does_not_exist"); // Should do nothing
value = client->GetBucket("complex"); value = client->GetBucket("complex");
RunTest("Deleting Non-Existent Key Doesn't Break Existing Data", R"({"nested":{"obj1":"data1","obj2":"data2"}})", value); RunTest("Deleting Non-Existent Key Doesn't Break Existing Data", R"({"nested":{"obj1":"data1","obj2":"data2"}})",
value);
// Get nested key value one level up ** // Get nested key value one level up **
client->DeleteBucket("complex"); client->DeleteBucket("complex");
@@ -190,12 +213,12 @@ void ZoneCLI::TestDataBuckets(int argc, char **argv, argh::parser &cmd, std::str
value = client->GetBucket("json_array"); value = client->GetBucket("json_array");
RunTest("Store and Retrieve JSON Array", R"(["item1", "item2"])", value); RunTest("Store and Retrieve JSON Array", R"(["item1", "item2"])", value);
// // Prevent Overwriting Array with Object** // // Prevent Overwriting Array with Object**
// client->DeleteBucket("json_array"); // client->DeleteBucket("json_array");
// client->SetBucket("json_array", R"(["item1", "item2"])"); // client->SetBucket("json_array", R"(["item1", "item2"])");
// client->SetBucket("json_array.item", "new_value"); // Should be rejected // client->SetBucket("json_array.item", "new_value"); // Should be rejected
// value = client->GetBucket("json_array"); // value = client->GetBucket("json_array");
// RunTest("Prevent Overwriting Array with Object", R"(["item1", "item2"])", value); // RunTest("Prevent Overwriting Array with Object", R"(["item1", "item2"])", value);
// Retrieve Non-Existent Nested Key** // Retrieve Non-Existent Nested Key**
client->DeleteBucket("nested_partial"); client->DeleteBucket("nested_partial");
@@ -235,11 +258,11 @@ void ZoneCLI::TestDataBuckets(int argc, char **argv, argh::parser &cmd, std::str
RunTest("Setting a nested key with an expiration protection test", R"({"test":{"test":"shouldnt_expire"}})", value); RunTest("Setting a nested key with an expiration protection test", R"({"test":{"test":"shouldnt_expire"}})", value);
// Delete Deep Nested Key Keeps Parent** // Delete Deep Nested Key Keeps Parent**
// client->DeleteBucket("deep_nested"); // client->DeleteBucket("deep_nested");
// client->SetBucket("deep_nested.level1.level2.level3", R"({"key": "value"})"); // client->SetBucket("deep_nested.level1.level2.level3", R"({"key": "value"})");
// client->DeleteBucket("deep_nested.level1.level2.level3.key"); // client->DeleteBucket("deep_nested.level1.level2.level3.key");
// value = client->GetBucket("deep_nested.level1.level2.level3"); // value = client->GetBucket("deep_nested.level1.level2.level3");
// RunTest("Delete Deep Nested Key Keeps Parent", "{}", value); // RunTest("Delete Deep Nested Key Keeps Parent", "{}", value);
// ================================ // ================================
// 🧪 Scoped Cache-Miss Behavior Tests // 🧪 Scoped Cache-Miss Behavior Tests
@@ -280,24 +303,29 @@ void ZoneCLI::TestDataBuckets(int argc, char **argv, argh::parser &cmd, std::str
// ================================ // ================================
// Clear everything for a clean test // Clear everything for a clean test
client->DeleteBucket("account_client_test"); // Insert directly into the DB without touching cache
const std::string scoped_key = "scoped_db_only_key";
client->DeleteBucket(scoped_key);
DataBucket::ClearCache(); DataBucket::ClearCache();
// Set a value to DB only, bypassing cache // ✅ Scoped insert
client->SetBucket("account_client_test", "cached_value"); DataBucketsRepository::InsertOne(
database, {
.key_ = scoped_key,
.value = "cached_value",
.character_id = client->CharacterID()
}
);
// Clear cache to simulate cold start // Cold cache test — should return ""
DataBucket::ClearCache(); std::string cold_value = client->GetBucket(scoped_key);
// Ensure that value is NOT in cache yet
std::string cold_value = client->GetBucket("account_client_test");
RunTest("Cold Cache Scoped Key Returns Empty (Due to Skip DB)", "", cold_value); RunTest("Cold Cache Scoped Key Returns Empty (Due to Skip DB)", "", cold_value);
// Load account/client buckets into cache via GetDataBuckets() // ✅ Reload cache
DataBucket::GetDataBuckets(client); DataBucket::GetDataBuckets(client);
// Value should now be present from cache after bulk load // Cache should now return the value
std::string hot_value = client->GetBucket("account_client_test"); std::string hot_value = client->GetBucket(scoped_key);
RunTest("Post-BulkLoad Scoped Key Returns Value", "cached_value", hot_value); RunTest("Post-BulkLoad Scoped Key Returns Value", "cached_value", hot_value);
// Also test nested key after preload // Also test nested key after preload
+3
View File
@@ -1959,6 +1959,9 @@ public:
ExternalHandinMoneyReturned GetExternalHandinMoneyReturned() { return m_external_handin_money_returned; } ExternalHandinMoneyReturned GetExternalHandinMoneyReturned() { return m_external_handin_money_returned; }
std::vector<uint32_t> GetExternalHandinItemsReturned() { return m_external_handin_items_returned; } std::vector<uint32_t> GetExternalHandinItemsReturned() { return m_external_handin_items_returned; }
// used only for testing
inline void SetCharacterId(uint32_t id) { character_id = id; }
protected: protected:
friend class Mob; friend class Mob;
void CalcEdibleBonuses(StatBonuses* newbon); void CalcEdibleBonuses(StatBonuses* newbon);