tests: props, statuses, the resize rules and migration step 6 (#22)

tests/tprops.nim is new: the caps (per bucket, keys, values), the key rules,
the registry (empty fallback, a registered key's kind and bucket), set/unset
and the clone that keeps an edit away from the original bucket.

ttokens covers radius, the three buckets, the status set (round trip, sorted,
replaced rather than appended, removed by the FK cascade). tmoverules covers
per-token radius, shrink/grow/squeeze, the no-crossing rule, the corner
relaxation and the independence of the two sources; the old `blocked` tests
became `stuckWalls`. tviews covers the bucket and source filtering, one
`token` frame per change and the effective radius. tdb/tdb's fixtures move to
step 6, and tprobing follows the rename.
This commit is contained in:
2026-11-15 11:02:43 +01:00
parent 81898ab69c
commit 20575f2147
6 changed files with 537 additions and 95 deletions
+73 -1
View File
@@ -3,7 +3,7 @@
## that keep GM-only data out of a player's frames. The black-box proof over a real socket is
## tools/leak.test.mjs with tools/leakcheck.nim.
import std/[algorithm, json, options, sequtils, sets, tables, unittest]
import geometry, tokens, views, vision, world
import geometry, props, tokens, views, vision, world
const Pid = 2 # the subject player
const Other = 3 # another player
@@ -223,3 +223,75 @@ suite "frames (6.1)":
check sceneJson["art_version"].getInt == 1
for key in ["walls", "tokens", "entities", "walls_count", "count"]:
check not sceneJson.hasKey(key)
suite "props and statuses (#22, 6.1: the bucket and the source are per-recipient)":
proc withProps(id: string, x, y: float, owner: int): TokenRec =
result = tok(id, x, y, owner)
result.props[pbPublic] = %*{"aura": "cold", "tag": "wolf"}
result.props[pbOwner] = %*{"hp": 7}
result.props[pbGm] = %*{"secret": "the duke"}
test "the GM frame carries all three buckets and the source of every status":
var t = withProps("a", 100, 100, 0)
t.statuses = @[(stStuck, ssWalls), (stSqueezed, ssGM)]
let frame = gmTokenFrame(gmView(ctxOf(sceneWith(@[]), @[t], @[Pid])).tokens[0])
check frame["props"].keys.toSeq.sorted == @["gm", "owner", "public"]
check frame["props"]["public"]["aura"].getStr == "cold"
check frame["props"]["owner"]["hp"].getInt == 7
check frame["props"]["gm"]["secret"].getStr == "the duke"
check frame["statuses"] == %*[{"status": "stuck", "source": "walls"},
{"status": "squeezed", "source": "GM"}]
test "a player who sees the token gets the public bucket only":
let sc = sceneWith(@[])
let tokens = @[withProps("mine", 400, 400, Pid), withProps("near", 500, 400, 0)]
let view = playerView(ctxOf(sc, tokens, @[Pid]), Pid)
let other = playerTokenFrame(view.tokens[1])
check other["props"].keys.toSeq == @["public"]
check other["props"]["public"]["tag"].getStr == "wolf"
check not other["props"].hasKey("owner")
check not other["props"].hasKey("gm")
test "the controller gets public + owner, never the gm bucket":
let sc = sceneWith(@[])
let view = playerView(ctxOf(sc, @[withProps("mine", 400, 400, Pid)], @[Pid]), Pid)
let frame = playerTokenFrame(view.tokens[0])
check frame["props"].keys.toSeq.sorted == @["owner", "public"]
check frame["props"]["owner"]["hp"].getInt == 7
check not frame["props"].hasKey("gm")
test "the controller is not told what caused a status either (5.5, 6.1 note 6)":
var t = withProps("mine", 400, 400, Pid)
t.statuses = @[(stStuck, ssWalls), (stSqueezed, ssGM)]
let frame = playerTokenFrame(playerView(ctxOf(sceneWith(@[]), @[t], @[Pid]), Pid).tokens[0])
check frame["statuses"] == %*[{"status": "stuck"}, {"status": "squeezed"}]
check "source" notin keysDeep(frame)
check frame["statuses"][0].len == 1
test "a prop change and a status change each reach a viewer as one `token` frame":
var ctx = ctxOf(sceneWith(@[]), @[withProps("mine", 400, 400, Pid)], @[Pid])
var known: Known
discard pass(ctx, known, resync = true)
check typesOf(pass(ctx, known)) == newSeq[string]()
var changed = ctx.tokens[0]
changed.props[pbPublic] = %*{"aura": "warm", "tag": "wolf"}
ctx.tokens[0] = changed
let frames = pass(ctx, known)
check typesOf(frames) == @["token"]
check frames[0]["props"]["public"]["aura"].getStr == "warm"
var stuck = ctx.tokens[0]
stuck.statuses = @[(stStuck, ssWalls)]
ctx.tokens[0] = stuck
let after = pass(ctx, known)
check typesOf(after) == @["token"]
check after[0]["statuses"] == %*[{"status": "stuck"}]
test "a radius change reaches every viewer as the effective radius":
var ctx = ctxOf(sceneWith(@[]), @[tok("mine", 400, 400, Pid), tok("near", 500, 400, 0)], @[Pid])
var known: Known
discard pass(ctx, known, resync = true)
ctx.tokens[1].radius = some(35.0)
let frames = pass(ctx, known)
check typesOf(frames) == @["token"]
check frames[0]["radius"].getFloat == 35.0
check not frames[0].hasKey("radius_own") # the GM-only own-radius field stays out