tests: characters, names, the encountered list and migration step 7 (#23)

This commit is contained in:
2026-11-15 12:01:30 +01:00
parent 20dcb87afb
commit f134534c6c
5 changed files with 1064 additions and 18 deletions
+178 -3
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, props, tokens, views, vision, world
import characters, geometry, props, tokens, views, vision, world
const Pid = 2 # the subject player
const Other = 3 # another player
@@ -128,8 +128,10 @@ suite "diff (6.4: only what changed)":
var ctx = ctxOf(sceneWith(@[]), @[tok("mine", 400, 400, Pid)], @[Pid])
var known: Known
let first = pass(ctx, known, resync = true)
check typesOf(first) == @["vision", "explored", "token"]
check first[1]["full"].getBool
check typesOf(first) == @["encountered", "vision", "explored", "token"]
check first[0]["full"].getBool # 6.3: the whole list on connect/resync
check first[0]["set"].len == 0
check first[2]["full"].getBool
check typesOf(pass(ctx, known)) == newSeq[string]()
test "a token that moved sends one `token` frame with the new position":
@@ -295,3 +297,176 @@ suite "props and statuses (#22, 6.1: the bucket and the source are per-recipient
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
# ---------------------------------------------------------------------------------------------------
# Slice 8 (#23): characters, names and the encountered list in the typed view.
proc charOf(id: string, owner: int, dname = "a grumpy man", actual = "Lord Vex"): CharacterRec =
CharacterRec(id: id, owner: owner, descriptiveName: dname, actualName: actual, props: newProps())
proc withChar(ctx: var Context, c: CharacterRec): int =
ctx.characters.add c
ctx.characters.len - 1
proc linked(id: string, x, y: float, owner: int, characterId: string): TokenRec =
TokenRec(id: id, scene: 1, kind: "pc", characterId: some(characterId), pos: (x, y),
owner: owner, flags: %*{"vision": true}, props: newProps())
suite "characters in the egress (3.1, 6.1, 8)":
test "the GM gets every character with owner, reveal_all, revealed and all three buckets":
var ctx = ctxOf(sceneWith(@[]), @[], @[Pid, Other])
var c = charOf("c1", 2)
c.revealAll = true
c.props[pbGm] = %*{"trap": true}
discard ctx.withChar(c)
ctx.reveals.withReveal(Other, "c1")
var known: Known
let frames = egressFrames(Recipient(isGm: true, playerId: 1), ctx, known, resync = true)
check frames.len == 1
let f = frames[0]
check f["t"].getStr == "character"
check f["owner"].getInt == 2
check f["descriptive_name"].getStr == "a grumpy man"
check f["actual_name"].getStr == "Lord Vex"
check f["reveal_all"].getBool
check f["revealed"] == %*[Other]
check f["props"].keys.toSeq.sorted == @["gm", "owner", "public"]
test "a player gets only its own characters, without owner, reveal_all or revealed":
var ctx = ctxOf(sceneWith(@[]), @[], @[Pid, Other])
discard ctx.withChar(charOf("mine", Pid))
discard ctx.withChar(charOf("theirs", Other))
var known: Known
let frames = egressFrames(Recipient(isGm: false, playerId: Pid), ctx, known, resync = true)
let chars = frames.filterIt(it["t"].getStr == "character")
check chars.len == 1
check chars[0]["id"].getStr == "mine"
check chars[0]["actual_name"].getStr == "Lord Vex" # the owner always knows it (8)
check chars[0]["props"].keys.toSeq.sorted == @["owner", "public"]
for key in ["owner", "reveal_all", "revealed", "nhidden"]:
check not chars[0].hasKey(key)
test "a reassignment withdraws the record from the old owner and sends it to the new one":
var ctx = ctxOf(sceneWith(@[]), @[], @[Pid, Other])
let idx = ctx.withChar(charOf("c1", Other))
var known: Known
check typesOf(egressFrames(Recipient(isGm: false, playerId: Other), ctx, known, resync = true))
.contains("character")
var otherKnown: Known
check not typesOf(egressFrames(Recipient(isGm: false, playerId: Pid), ctx, otherKnown))
.contains("character")
ctx.characters[idx].owner = Pid
let otherFrames = egressFrames(Recipient(isGm: false, playerId: Other), ctx, known)
check typesOf(otherFrames) == @["character_gone"]
check otherFrames[0]["id"].getStr == "c1"
check typesOf(egressFrames(Recipient(isGm: false, playerId: Pid), ctx, otherKnown))
.contains("character")
test "a linked token's name follows the character, and the owner sees the actual one":
var ctx = ctxOf(sceneWith(@[]), @[], @[Pid, Other])
discard ctx.withChar(charOf("c1", Other))
ctx.tokens = @[linked("t1", 400, 400, Pid, "c1")]
# the subject controls the token but does not own the character
var known: Known
var frame = playerView(ctx, Pid).tokens[0]
check frame.name == "a grumpy man"
check frame.characterId.isNone # 6.1 leak note 2
check frame.cpropsPublic.len == 0
# the character's owner sees the actual name and the link
let owner = playerView(ctx, Other)
check owner.tokens.len == 0 # no LOS of its own: the token is not in its view
discard known
test "cprops carries the character's public bucket to a non-owner, and never the owner bucket":
var ctx = ctxOf(sceneWith(@[]), @[], @[Pid, Other])
let idx = ctx.withChar(charOf("c1", Other))
ctx.characters[idx].props[pbPublic] = %*{"rank": "noble"}
ctx.characters[idx].props[pbOwner] = %*{"hp": 3}
ctx.characters[idx].props[pbGm] = %*{"trap": true}
ctx.tokens = @[linked("t1", 400, 400, Pid, "c1")]
let frame = playerTokenFrame(playerView(ctx, Pid).tokens[0])
check frame["cprops"].keys.toSeq == @["public"]
check frame["cprops"]["public"]["rank"].getStr == "noble"
check "owner" notin keysDeep(frame["cprops"])
check "gm" notin keysDeep(frame)
check frame["props"].keys.toSeq.sorted == @["owner", "public"] # its own buckets
test "a player sees the actual name only when the GM revealed it to them (8)":
var ctx = ctxOf(sceneWith(@[]), @[], @[Pid, Other])
discard ctx.withChar(charOf("c1", Other))
ctx.tokens = @[linked("t1", 400, 400, Pid, "c1")]
var known: Known
check playerView(ctx, Pid).tokens[0].name == "a grumpy man"
ctx.reveals.withReveal(Pid, "c1")
check playerView(ctx, Pid).tokens[0].name == "Lord Vex"
discard known
suite "the encountered list (8)":
proc entryFor(id: string, name: string, grey = false): EncounteredView =
EncounteredView(id: id, name: name, grey: grey)
test "a resync sends the whole list, then only what changed, then what was removed":
var ctx = ctxOf(sceneWith(@[]), @[tok("mine", 400, 400, Pid)], @[Pid])
ctx.encountered[Pid] = initTable[string, characters.EncounteredEntry]()
ctx.encountered[Pid]["mine"] = EncounteredEntry(entityId: "mine",
descriptiveName: "a mine")
var known: Known
let full = pass(ctx, known, resync = true)
let enc = full.filterIt(it["t"].getStr == "encountered")
check enc.len == 1
check enc[0]["full"].getBool
check enc[0]["set"] == %*[{"id": "mine", "name": "a mine", "grey": false}]
# nothing changed: no frame
check pass(ctx, known).len == 0
# greyed: the entry is re-sent, nothing is deleted
ctx.encountered[Pid]["mine"].actualName = some("Mine")
let changed = pass(ctx, known).filterIt(it["t"].getStr == "encountered")
check changed.len == 1
check changed[0]["full"].getBool == false
check changed[0]["set"] == %*[{"id": "mine", "name": "Mine", "grey": false}]
check changed[0]["del"] == newJArray()
# removed: only a `del`
ctx.encountered[Pid].del "mine"
let removed = pass(ctx, known).filterIt(it["t"].getStr == "encountered")
check removed.len == 1
check removed[0]["set"] == newJArray()
check removed[0]["del"] == %*["mine"]
test "an entry for a token that is gone (or out of view) is grey":
var ctx = ctxOf(sceneWith(@[]), @[tok("mine", 400, 400, Pid), tok("near", 500, 400, 0)], @[Pid])
check not ctx.greyFor(Pid, "near") # in LOS
check ctx.greyFor(Pid, "unknown") # removed from the world
ctx.tokens[1].pos = (3000.0, 3000.0)
check ctx.greyFor(Pid, "near") # moved away
ctx.tokens[1].gmOnly = true
ctx.tokens[1].pos = (500.0, 400.0)
check ctx.greyFor(Pid, "near") # a gm_only flip greys it, it is not deleted
test "the GM gets one list per player, tagged with the player id":
var ctx = ctxOf(sceneWith(@[]), @[tok("mine", 400, 400, Pid), tok("theirs", 400, 400, Other)],
@[Pid, Other])
for pid in [Pid, Other]:
ctx.encountered[pid] = initTable[string, characters.EncounteredEntry]()
ctx.encountered[Pid]["mine"] = EncounteredEntry(entityId: "mine", descriptiveName: "a mine")
ctx.encountered[Other]["theirs"] = EncounteredEntry(entityId: "theirs",
descriptiveName: "a theirs")
var known: Known
let frames = egressFrames(Recipient(isGm: true, playerId: 1), ctx, known, resync = true)
let lists = frames.filterIt(it["t"].getStr == "encountered")
check lists.len == 2
check lists[0]["player"].getInt == Pid
check lists[0]["set"][0]["id"].getStr == "mine"
check lists[1]["player"].getInt == Other
check lists[1]["set"][0]["id"].getStr == "theirs"
test "a player never receives another player's list":
var ctx = ctxOf(sceneWith(@[]), @[tok("theirs", 400, 400, Other)], @[Pid, Other])
for pid in [Pid, Other]:
ctx.encountered[pid] = initTable[string, characters.EncounteredEntry]()
ctx.encountered[Other]["theirs"] = EncounteredEntry(entityId: "theirs",
descriptiveName: "a theirs")
var known: Known
let frames = egressFrames(Recipient(isGm: false, playerId: Pid), ctx, known, resync = true)
for f in frames.filterIt(it["t"].getStr == "encountered"):
check f["set"].len == 0
check not f.hasKey("player")