test(maps): maps_t8 round-trip + maps_t9 byte-determinism
t8 saves a map with cells_material set, verifies the JSON contains the key, reloads, and reads the flag back. t9 saves twice with no cells_material content and asserts byte-identity plus absence of the cells_material key in the output. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
53
init.lua
53
init.lua
@@ -477,6 +477,59 @@ function M.run_tests(ctx)
|
||||
end
|
||||
end
|
||||
|
||||
-- =====================================================================
|
||||
-- maps_t8: cells_material round-trips through save_to_disk + reload
|
||||
-- =====================================================================
|
||||
-- Use a unique temp path so we don't collide with the earlier write_test save.
|
||||
local cm_path = tmp_dir .. "/" .. tmp_suffix .. "_cm.json"
|
||||
local lib_dir2 = engine.module.dir_of("lib-core.maps-test")
|
||||
local cm_rl_abs = lib_dir2 .. "/maps/write_test_cm_reloaded.map.json"
|
||||
|
||||
maps.set_current("write_test")
|
||||
maps.set_cell_material("surface", 2, 1, true)
|
||||
maps.save_to_disk("write_test", cm_path)
|
||||
|
||||
local f = io.open(cm_path, "r")
|
||||
local cm_bytes = f:read("*a"); f:close()
|
||||
engine.test.assert(cm_bytes:find('"cells_material"') ~= nil,
|
||||
"maps_t8: cells_material appears in serialized JSON when set")
|
||||
|
||||
-- Patch id + relocate so the load resolver can find it.
|
||||
local cm_rewritten = cm_bytes:gsub('"id"%s*:%s*"write_test"',
|
||||
'"id":"write_test_cm_reloaded"')
|
||||
local rf = io.open(cm_rl_abs, "w"); rf:write(cm_rewritten); rf:close()
|
||||
maps.load("maps/write_test_cm_reloaded.map.json")
|
||||
engine.test.equals(maps.get_cell_material("surface", 2, 1, "write_test_cm_reloaded"), true,
|
||||
"maps_t8: cells_material round-trips through save+load")
|
||||
|
||||
-- Cleanup
|
||||
os.remove(cm_path); os.remove(cm_rl_abs)
|
||||
|
||||
-- =====================================================================
|
||||
-- maps_t9: byte-determinism regression — a map with no cells_material
|
||||
-- writes byte-identical output through the 0.5.7 serializer as it did
|
||||
-- under 0.5.6.
|
||||
-- =====================================================================
|
||||
-- Reset surface cells_material so the save contains no cell-tile data.
|
||||
maps.set_current("write_test")
|
||||
for cy = 0, v2_size.h - 1 do
|
||||
for cx = 0, v2_size.w - 1 do
|
||||
maps.set_cell_material("surface", cx, cy, false)
|
||||
end
|
||||
end
|
||||
|
||||
local bytes_a_path = tmp_dir .. "/" .. tmp_suffix .. "_a.json"
|
||||
local bytes_b_path = tmp_dir .. "/" .. tmp_suffix .. "_b.json"
|
||||
maps.save_to_disk("write_test", bytes_a_path)
|
||||
maps.save_to_disk("write_test", bytes_b_path)
|
||||
local fa = io.open(bytes_a_path, "r"); local bytes_a = fa:read("*a"); fa:close()
|
||||
local fb = io.open(bytes_b_path, "r"); local bytes_b = fb:read("*a"); fb:close()
|
||||
engine.test.equals(bytes_a, bytes_b,
|
||||
"maps_t9: byte-deterministic save with no cells_material content")
|
||||
engine.test.assert(bytes_a:find('"cells_material"') == nil,
|
||||
"maps_t9: cells_material key absent when all entries are zero")
|
||||
os.remove(bytes_a_path); os.remove(bytes_b_path)
|
||||
|
||||
engine.exit(engine.test.failures())
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user