mirror of
https://github.com/PHIDIAS0303/ExpCluster.git
synced 2026-11-21 07:33:12 +09:00
Send every value, register protected entities, fix the pattern offset
- The instance sends every value of a feature, defaults included, so update_config is one loop which sets each field and compares. The stored table is the last set of values, and _apply is gone. - values_equal compares table sizes before the contents. - Protected entities are registered with register_on_object_destroyed and forgotten in on_object_destroyed, which covers every way an entity can go. The four removal handlers and the validity checks are gone. Entities destroyed while the feature is disabled are swept when it is enabled again. Saves from before the registrations table get it through the Storage on_init callback. - Spawn area applied the water offset twice and the pattern offset never. The pattern tiles now move with the entities above them. - pruneFeatureValues reads a validation result instead of catching an error, and FeaturesPage hands its pending state and unsaved bar to helpers. - Shorter comments throughout. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
1 parent
f90b8fcee1
commit
2959d7476b
11 files changed
+300
-350
No files matched your search
@@ -1,11 +1,12 @@
|
||||
local Suite = ... --- @type Suite<ExpScenario.TestEnv>
|
||||
|
||||
--- The values of on_config_updated events, ignoring the event name and tick
|
||||
--- The raised on_config_updated events as { path, old value, new value }, sorted by path
|
||||
local function updates(env)
|
||||
local rtn = {}
|
||||
for _, event in ipairs(env.events) do
|
||||
rtn[#rtn + 1] = { event.feature_name, event.path, event.old_value, event.new_value }
|
||||
rtn[#rtn + 1] = { event.path, event.old_value, event.new_value }
|
||||
end
|
||||
table.sort(rtn, function(a, b) return a[1] < b[1] end)
|
||||
return rtn
|
||||
end
|
||||
|
||||
@@ -16,64 +17,71 @@ Suite.test("register() enables features unless the config says otherwise", funct
|
||||
Suite.throws(function() env.Feature.register("on", {}) end, "Feature already registered: on", "names are unique")
|
||||
end)
|
||||
|
||||
Suite.test("register() returns the config as well", function(env)
|
||||
local feature, config = env.Feature.register("test", { flag = true })
|
||||
Suite.eq(config == feature.config, true, "the same table")
|
||||
end)
|
||||
|
||||
Suite.test("get() finds registered features", function(env)
|
||||
local feature = env.Feature.register("test")
|
||||
Suite.eq(env.Feature.get("test") == feature, true, "by name")
|
||||
Suite.throws(function() env.Feature.get("unknown") end, "Unknown feature: unknown", "unknown names error")
|
||||
end)
|
||||
|
||||
Suite.test("update_config() changes the config in place", function(env)
|
||||
local feature = env.Feature.register("test", { flag = true, count = 5, text = "a" })
|
||||
local config = feature.config
|
||||
feature:update_config(false, { flag = false, count = 7 })
|
||||
Suite.eq(config, { enabled = false, flag = false, count = 7, text = "a" }, "given values replace the defaults")
|
||||
Suite.test("update_config() sets every value, missing ones to their default", function(env)
|
||||
local feature, config = env.Feature.register("test", { flag = true, count = 5, text = "a" })
|
||||
feature:update_config{ enabled = false, flag = false, count = 7 }
|
||||
Suite.eq(config, { enabled = false, flag = false, count = 7, text = "a" }, "given values are set")
|
||||
|
||||
feature:update_config(true, { text = "b" })
|
||||
feature:update_config{ enabled = true, text = "b" }
|
||||
Suite.eq(config, { enabled = true, flag = true, count = 5, text = "b" }, "values left out go back to their default")
|
||||
Suite.eq(env.storage, { test = { enabled = true, text = "b" } }, "the overrides are kept in storage")
|
||||
Suite.eq(env.storage, { test = { enabled = true, text = "b" } }, "the values are kept in storage")
|
||||
end)
|
||||
|
||||
Suite.test("update_config() raises on_config_updated for each changed value", function(env)
|
||||
local feature = env.Feature.register("test", { flag = true, names = env.Feature.set{ "a" }, section = { count = 1 } })
|
||||
feature:update_config(false, { names = { "a" }, ["section.count"] = 2 })
|
||||
Suite.eq(#env.events, 2, "unchanged values raise nothing")
|
||||
feature:update_config{ enabled = false, flag = true, names = { "a" }, ["section.count"] = 2 }
|
||||
Suite.eq(env.Feature.events.on_config_updated, assert(env.events[1]).name, "the event is on_config_updated")
|
||||
|
||||
local seen = updates(env)
|
||||
table.sort(seen, function(a, b) return a[2] < b[2] end)
|
||||
Suite.eq(seen, {
|
||||
{ "test", "enabled", true, false },
|
||||
{ "test", "section.count", 1, 2 },
|
||||
}, "with the path, old value, and new value")
|
||||
Suite.eq(updates(env), {
|
||||
{ "enabled", true, false },
|
||||
{ "section.count", 1, 2 },
|
||||
}, "with the path, old value, and new value, unchanged values raise nothing")
|
||||
end)
|
||||
|
||||
Suite.test("update_config() ignores and logs values which can not be overridden", function(env)
|
||||
Suite.test("update_config() raises when a value goes back to its default", function(env)
|
||||
local feature = env.Feature.register("test", { flag = true })
|
||||
feature:update_config{ enabled = true, flag = false }
|
||||
env.events = {}
|
||||
feature:update_config{ enabled = true }
|
||||
Suite.eq(updates(env), { { "flag", false, true } }, "the value reverts")
|
||||
end)
|
||||
|
||||
Suite.test("update_config() ignores and logs values of the wrong type", function(env)
|
||||
local function check() return true end
|
||||
local feature = env.Feature.register("test", { flag = true, check = check, icon = nil })
|
||||
feature:update_config(true, { flag = "yes", check = false, icon = "x" })
|
||||
Suite.eq(feature.config, { enabled = true, flag = true, check = check }, "wrong types, functions, and unknown keys are skipped")
|
||||
Suite.eq(#env.logged, 3, "each one is logged")
|
||||
Suite.eq(env.storage.test, { enabled = true, flag = "yes", check = false }, "unknown keys are not stored")
|
||||
local feature, config = env.Feature.register("test", { flag = true, check = check })
|
||||
feature:update_config{ enabled = true, flag = "yes", check = false }
|
||||
Suite.eq(config, { enabled = true, flag = true, check = check }, "wrong types and functions keep their default")
|
||||
Suite.eq(#env.logged, 2, "each one is logged")
|
||||
end)
|
||||
|
||||
Suite.test("receive_update() updates known features", function(env)
|
||||
local feature = env.Feature.register("test", { flag = true })
|
||||
env.Feature.receive_update{
|
||||
{ name = "test", enabled = true, values = { flag = false } },
|
||||
{ name = "unknown", enabled = false, values = {} },
|
||||
{ name = "test", values = { enabled = true, flag = false } },
|
||||
{ name = "unknown", values = { enabled = false } },
|
||||
}
|
||||
Suite.eq(feature.config.flag, false, "the known feature changed")
|
||||
Suite.eq(env.storage.unknown, nil, "unknown features are not stored")
|
||||
end)
|
||||
|
||||
Suite.test("on_load applies the overrides from storage without raising events", function(env)
|
||||
local feature = env.Feature.register("test", { flag = true })
|
||||
env.on_load{ test = { enabled = false, flag = false } }
|
||||
Suite.eq(feature.config, { enabled = false, flag = false }, "the saved overrides apply")
|
||||
Suite.test("on_load applies the stored values without raising events", function(env)
|
||||
local feature, config = env.Feature.register("test", { flag = true })
|
||||
env.on_load{ test = { enabled = false, flag = false, gone = 1 } }
|
||||
Suite.eq(config, { enabled = false, flag = false }, "the saved values apply, unknown ones are skipped")
|
||||
Suite.empty(env.events, "nothing is raised")
|
||||
|
||||
feature:update_config(true, {})
|
||||
Suite.eq(env.storage, { test = { enabled = true } }, "later updates go to the loaded storage table")
|
||||
feature:update_config{ enabled = true }
|
||||
Suite.eq(env.storage, { test = { enabled = true } }, "later updates replace the stored values")
|
||||
end)
|
||||
|
||||
Suite.test("guard() skips handlers while the feature is disabled", function(env)
|
||||
@@ -87,7 +95,7 @@ Suite.test("guard() skips handlers while the feature is disabled", function(env)
|
||||
|
||||
lib.events.on_event("a")
|
||||
lib.on_nth_tick[60]("b")
|
||||
feature:update_config(false, {})
|
||||
feature:update_config{ enabled = false }
|
||||
lib.events.on_event("c")
|
||||
lib.on_nth_tick[60]("d")
|
||||
Suite.eq(calls, { "a", "b" }, "only called while enabled")
|
||||
@@ -95,70 +103,48 @@ end)
|
||||
|
||||
Suite.test("register() addresses nested values by path", function(env)
|
||||
local offset = { 1, 2 }
|
||||
local feature = env.Feature.register("test", { section = { flag = true, count = 1 }, offset = offset })
|
||||
feature:update_config(true, { ["section.flag"] = false, offset = { "x" } })
|
||||
Suite.eq(feature.config.section, { flag = false, count = 1 }, "the nested value changes in place")
|
||||
Suite.eq(feature.config.offset, offset, "tables which are not sections can not be overridden")
|
||||
local feature, config = env.Feature.register("test", { section = { flag = true, count = 1 }, offset = offset })
|
||||
feature:update_config{ enabled = true, ["section.flag"] = false, offset = { "x" } }
|
||||
Suite.eq(config.section, { flag = false, count = 1 }, "the nested value changes in place")
|
||||
Suite.eq(config.offset, offset, "tables which are not sections can not be set")
|
||||
|
||||
feature:update_config(true, {})
|
||||
Suite.eq(feature.config.section.flag, true, "and goes back to its default")
|
||||
feature:update_config{ enabled = true }
|
||||
Suite.eq(config.section.flag, true, "and goes back to its default")
|
||||
end)
|
||||
|
||||
Suite.test("list() declares a list of strings which can be overridden", function(env)
|
||||
local feature = env.Feature.register("test", { names = env.Feature.list{ "a", "b" }, empty = env.Feature.list{}, plain = { "x" } })
|
||||
Suite.eq(feature.config.names, { "a", "b" }, "the default is the list")
|
||||
feature:update_config(true, { names = { "c" }, empty = { "d" }, plain = { "y" } })
|
||||
Suite.eq(feature.config.names, { "c" }, "a list is replaced")
|
||||
Suite.eq(feature.config.empty, { "d" }, "an empty list can be overridden")
|
||||
Suite.eq(feature.config.plain, { "x" }, "a plain table can not be overridden")
|
||||
feature:update_config(true, { names = { 1 } })
|
||||
Suite.eq(feature.config.names, { "a", "b" }, "a list with other types is invalid")
|
||||
Suite.test("list() declares a list of strings", function(env)
|
||||
local feature, config = env.Feature.register("test", { names = env.Feature.list{ "a", "b" }, empty = env.Feature.list{}, plain = { "x" } })
|
||||
Suite.eq(config.names, { "a", "b" }, "the default is the list")
|
||||
feature:update_config{ enabled = true, names = { "c" }, empty = { "d" }, plain = { "y" } }
|
||||
Suite.eq(config.names, { "c" }, "a list is replaced")
|
||||
Suite.eq(config.empty, { "d" }, "an empty list can be set")
|
||||
Suite.eq(config.plain, { "x" }, "a plain table can not be set")
|
||||
feature:update_config{ enabled = true, names = { 1 } }
|
||||
Suite.eq(config.names, { "a", "b" }, "a list with other types is invalid")
|
||||
end)
|
||||
|
||||
Suite.test("set() declares a set of strings which is overridden with a list", function(env)
|
||||
local feature = env.Feature.register("test", { names = env.Feature.set{ "a" }, empty = env.Feature.set{} })
|
||||
Suite.eq(feature.config.names, { a = true }, "the default is a set")
|
||||
Suite.eq(feature.config.empty, {}, "and can be empty")
|
||||
feature:update_config(true, { names = { "b", "c" } })
|
||||
Suite.eq(feature.config.names, { b = true, c = true }, "the list is converted to a set")
|
||||
Suite.test("set() declares a set of strings which is sent as a list", function(env)
|
||||
local feature, config = env.Feature.register("test", { names = env.Feature.set{ "a" }, empty = env.Feature.set{} })
|
||||
Suite.eq(config.names, { a = true }, "the default is a set")
|
||||
Suite.eq(config.empty, {}, "and can be empty")
|
||||
feature:update_config{ enabled = true, names = { "b", "c" } }
|
||||
Suite.eq(config.names, { b = true, c = true }, "the list is converted to a set")
|
||||
env.on_load{ test = { enabled = true, names = { "d" } } }
|
||||
Suite.eq(feature.config.names, { d = true }, "also when the overrides are loaded")
|
||||
Suite.eq(config.names, { d = true }, "also when the values are loaded")
|
||||
end)
|
||||
|
||||
Suite.test("optional() declares a value without a default", function(env)
|
||||
local feature = env.Feature.register("test", { role_id = env.Feature.optional("number") })
|
||||
Suite.eq(feature.config.role_id, nil, "nil until overridden")
|
||||
feature:update_config(true, { role_id = 3 })
|
||||
Suite.eq(feature.config.role_id, 3, "overridden with the given type")
|
||||
feature:update_config(true, {})
|
||||
Suite.eq(feature.config.role_id, nil, "and back to nil")
|
||||
end)
|
||||
|
||||
|
||||
Suite.test("register() returns the config as well", function(env)
|
||||
local feature, config = env.Feature.register("test", { flag = true })
|
||||
Suite.eq(config == feature.config, true, "the same table")
|
||||
end)
|
||||
|
||||
Suite.test("update_config() raises when an override is removed", function(env)
|
||||
local feature = env.Feature.register("test", { flag = true, count = 1 })
|
||||
feature:update_config(true, { flag = false })
|
||||
env.events = {}
|
||||
feature:update_config(true, {})
|
||||
Suite.eq(updates(env), { { "test", "flag", false, true } }, "the value goes back to its default")
|
||||
end)
|
||||
|
||||
Suite.test("update_config() skips stored overrides for settings which no longer exist", function(env)
|
||||
local feature = env.Feature.register("test", { flag = true })
|
||||
env.on_load{ test = { enabled = true, gone = 1 } }
|
||||
feature:update_config(true, { flag = false })
|
||||
Suite.eq(updates(env), { { "test", "flag", true, false } }, "only the known value is compared")
|
||||
Suite.eq(env.storage.test, { enabled = true, flag = false }, "and the stale key is not kept")
|
||||
local feature, config = env.Feature.register("test", { role_id = env.Feature.optional("number") })
|
||||
Suite.eq(config.role_id, nil, "nil until set")
|
||||
feature:update_config{ enabled = true, role_id = 3 }
|
||||
Suite.eq(config.role_id, 3, "set with the given type")
|
||||
feature:update_config{ enabled = true }
|
||||
Suite.eq(config.role_id, nil, "and back to nil")
|
||||
end)
|
||||
|
||||
Suite.test("config files register their feature", function(env)
|
||||
local feature = env.load_config("spawn_area")
|
||||
feature:update_config(true, { ["turrets.enabled"] = false })
|
||||
feature:update_config{ enabled = true, ["turrets.enabled"] = false }
|
||||
Suite.eq(feature.config.turrets.enabled, false, "spawn_area can be changed")
|
||||
Suite.eq(feature.config.turrets.ammo_type, "uranium-rounds-magazine", "other values keep their default")
|
||||
end)
|
||||
|
||||
Reference in new issue
Block a user