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
+126 -1
View File
@@ -4,7 +4,7 @@
## tools/tokens.test.mjs and the leak harness.
import std/[json, options, os, sequtils, sets, strutils, unittest]
import db_connector/db_sqlite
import auth, db, tokens, world
import auth, db, props, tokens, world
var tmpCount = 0
proc tmpDir(): string =
@@ -257,3 +257,128 @@ suite "validation (DESIGN 6.1)":
check tokenById(@[t], t.id).isSome
check tokenById(@[t], "").isNone
check tokenById(@[t], t.id & "x").isNone
suite "size, props and statuses (slice 7: #4, #22)":
test "a per-token radius round-trips and overrides the scene default (5.6)":
let (conn, d) = freshDb()
defer:
close(conn)
removeDir(d)
let sc = loadScene(conn).get
var t = plain()
t.radius = some(35.0)
let (outcome, stored) = addToken(conn, sc, t, @[])
check outcome == toOk
check effectiveRadius(sc, stored) == 35.0
let reloaded = loadTokens(conn)[0]
check reloaded.radius.get == 35.0
check effectiveRadius(sc, reloaded) == 35.0
# null again = the scene default
var cleared = reloaded
cleared.radius = none(float)
check storeToken(conn, sc, cleared) == toOk
check loadTokens(conn)[0].radius.isNone
check effectiveRadius(sc, loadTokens(conn)[0]) == 20.0
test "a non-positive radius is refused, and nothing is written":
let (conn, d) = freshDb()
defer:
close(conn)
removeDir(d)
let sc = loadScene(conn).get
var t = plain()
t.radius = some(0.0)
check addToken(conn, sc, t, @[]).outcome == toInvalid
t.radius = some(-5.0)
check addToken(conn, sc, t, @[]).outcome == toInvalid
check loadTokens(conn).len == 0
test "the three property buckets round-trip unchanged (6.1, 8)":
let (conn, d) = freshDb()
defer:
close(conn)
removeDir(d)
let sc = loadScene(conn).get
var t = plain()
t.props[pbPublic] = %*{"aura": "cold", "hp": 3}
t.props[pbOwner] = %*{"notes": ["a", "b"]}
t.props[pbGm] = %*{"secret": true}
let (outcome, stored) = addToken(conn, sc, t, @[])
check outcome == toOk
let r = loadTokens(conn)[0]
check r.props[pbPublic] == stored.props[pbPublic]
check r.props[pbOwner] == stored.props[pbOwner]
check r.props[pbGm] == stored.props[pbGm]
check r.props[pbPublic]["hp"].getInt == 3
check r.props[pbGm]["secret"].getBool
# an update rewrites the buckets with the row
var changed = r
changed.props[pbPublic] = %*{"aura": "warm"}
check storeToken(conn, sc, changed) == toOk
let after = loadTokens(conn)[0]
check after.props[pbPublic]["aura"].getStr == "warm"
check after.props[pbGm]["secret"].getBool
test "a new token starts with three empty buckets, never null":
let (conn, d) = freshDb()
defer:
close(conn)
removeDir(d)
let sc = loadScene(conn).get
discard addToken(conn, sc, plain(), @[])
let r = loadTokens(conn)[0]
for b in PropBucket:
check r.props[b].kind == JObject
check r.props[b].len == 0
test "statuses round-trip sorted, and a store replaces them (5.5)":
let (conn, d) = freshDb()
defer:
close(conn)
removeDir(d)
let sc = loadScene(conn).get
var t = plain()
# added out of order on purpose
t.statuses = @[(stSqueezed, ssGM), (stStuck, ssWalls), (stStuck, ssGM)]
let (outcome, stored) = addToken(conn, sc, t, @[])
check outcome == toOk
check stored.statuses == @[(stStuck, ssWalls), (stStuck, ssGM), (stSqueezed, ssGM)]
let r = loadTokens(conn)[0]
check r.statuses == stored.statuses
check r.isLocked
check r.hasStatus(stStuck, ssWalls)
check not r.hasStatus(stSqueezed, ssWalls)
# a full store replaces the set instead of appending to it
var cleared = r
cleared.statuses = @[(stStuck, ssGM)]
check storeToken(conn, sc, cleared) == toOk
check loadTokens(conn)[0].statuses == @[(stStuck, ssGM)]
var none_left = cleared
none_left.statuses = @[]
check storeToken(conn, sc, none_left) == toOk
check loadTokens(conn)[0].statuses.len == 0
check not loadTokens(conn)[0].isLocked
test "removing a token removes its status rows (FK cascade)":
let (conn, d) = freshDb()
defer:
close(conn)
removeDir(d)
let sc = loadScene(conn).get
var t = plain()
t.statuses = @[(stStuck, ssWalls)]
let (_, stored) = addToken(conn, sc, t, @[])
check parseInt(conn.getValue(sql"SELECT count(*) FROM entity_status")) == 1
check removeToken(conn, stored.id)
check parseInt(conn.getValue(sql"SELECT count(*) FROM entity_status")) == 0
test "status helpers keep the entry set unique and ordered":
var t = plain()
t.addStatus(stSqueezed, ssGM)
t.addStatus(stStuck, ssWalls)
t.addStatus(stStuck, ssWalls) # a duplicate changes nothing
check t.statuses == @[(stStuck, ssWalls), (stSqueezed, ssGM)]
check t.removeStatus(stStuck, ssWalls)
check not t.removeStatus(stStuck, ssWalls) # already gone
check t.statuses == @[(stSqueezed, ssGM)]
check t.isLocked