Files
FFF_vtt_frozen/tests/tworld.nim
T
SirStone 0f3aa2300f feat(walls): door refusals and secret/locked equivalence gate (#56)
- door.set: opening a secret or locked door is invalid; closing either is
  allowed; a fixed window/non-door still has no open state.
- new tools/doors.test.mjs: two fresh servers, one identical script, three
  equivalence pairs (secret/wall, secret+locked/wall, locked/closed door),
  canonicalised frame + full per-player tile sweeps, an open-door positive
  control and a pinned zero-thickness close-push.
- secretgap fault hook (sight-only) makes the pair fail as exactly SECRETGAP;
  the clean pair passes and the shipped binary carries no fault hook.
- docs/TESTING.md T2/T17 and the door-equivalence/10.3 sections: built.

Ref #56
2026-11-19 22:49:39 +01:00

499 lines
21 KiB
Nim

## Scene and wall state (#19, DESIGN 3.1, 4.1, 4.4, 6.1, 8): wall validation, the per-scene
## never-reused wall ids, the write-through persistence (synchronous=FULL for authoring) and the
## reload that a restart performs. The wire behaviour over a real socket lives in tools/walls.test.mjs.
import std/[json, options, os, sequtils, strutils, unittest]
import db_connector/db_sqlite
import db, world
var tmpCount = 0
proc tmpDir(): string =
inc tmpCount
result = getTempDir() / "ffftworld-" & $getCurrentProcessId() & "-" & $tmpCount
removeDir(result)
createDir(result)
proc wall(ax, ay, bx, by: float, door = false, open = false): WallRec =
result = defaultWall((ax, ay), (bx, by))
result.door = door
result.open = open
proc freshScene(): tuple[db: DbConn, dir: string] =
## A migrated DB (the scene row comes from migration step 3) plus the scene loaded into memory.
let d = tmpDir()
let conn = openDatabase(d / "state.db")
discard migrate(conn)
(conn, d)
suite "scene (migration step 3, Q3: exactly one scene)":
test "the migration seeds scene 1 with the v1 defaults, no walls, next_wall_id 1":
let (conn, d) = freshScene()
defer:
close(conn)
removeDir(d)
check sceneCount(conn) == 1
let sc = loadScene(conn)
check sc.isSome
let s = sc.get
check s.id == DefaultSceneId
check s.name == "Scene"
check (s.w, s.h) == (4096.0, 4096.0)
check s.tileSize == 32 and s.cell == 32 # Q4 FIXED: tile == explored cell
check s.gridSize == 0 # no grid by default
check s.visionRadiusDefault == 400 # prototype default (5.5 step cap)
check s.tokenRadiusDefault == 20 # #4: scene default token size
check s.artVersion == 1 # M3 always 1 (3.3)
check s.nextWallId == 1
check s.walls.len == 0
check s.extra.kind == JObject
test "an unknown scene id is not there (the wire answers not-found)":
let (conn, d) = freshScene()
defer:
close(conn)
removeDir(d)
check loadScene(conn, DefaultSceneId + 1).isNone
check loadScene(conn).isSome
suite "field parsing (6.1 field notes)":
test "a point is exactly two finite numbers, ints included":
check parseVec(%*[10, 20]).get == (10.0, 20.0)
check parseVec(%*[10.5, 20.25]).get == (10.5, 20.25)
check parseVec(%*[10]).isNone
check parseVec(%*[10, 20, 30]).isNone
check parseVec(%*["10", 20]).isNone
check parseVec(%*{"x": 1}).isNone
check parseVec(newJNull()).isNone
check parseVec(nil).isNone
test "position, sight_dir and flags accept only their own shape":
check parsePosition("center").get == wpCenter
check parsePosition("left").get == wpLeft
check parsePosition("right").get == wpRight
check parsePosition("middle").isNone
check parsePosition("").isNone
check parseSightDir("both").get == sdBoth
check parseSightDir("left").get == sdLeft
check parseSightDir("right").get == sdRight
check parseSightDir("any").isNone
check parseFlags(nil).get.kind == JObject # omitted = {}
check parseFlags(%*{"a": 1}).get["a"].getInt == 1
check parseFlags(%*[1]).isNone
test "kind is derived from the switches, never stored (5.8)":
check derivedKind(defaultWall((0.0, 0.0), (1.0, 0.0))) == "wall"
var door = defaultWall((0.0, 0.0), (1.0, 0.0))
door.door = true
check derivedKind(door) == "door"
var window = defaultWall((0.0, 0.0), (1.0, 0.0))
window.blocksSight = false
check derivedKind(window) == "window"
window.door = true
check derivedKind(window) == "window" # a fixed or opening window is still a window
var oneWay = defaultWall((0.0, 0.0), (1.0, 0.0))
oneWay.sightDir = sdLeft
check derivedKind(oneWay) == "one_way"
var custom = defaultWall((0.0, 0.0), (1.0, 0.0))
custom.blocksMovement = false
check derivedKind(custom) == "custom"
test "undefined and null are the same as an absent field":
check fieldOrNull(%*{"open": true}, "open").getBool
var withNull = newJObject()
withNull["open"] = newJNull()
check fieldOrNull(withNull, "open").isNil
check fieldOrNull(%*{}, "open").isNil
check fieldOrNull(nil, "open").isNil
suite "wall ids (FIXED #3: per scene, monotonic, never reused)":
test "adds count from 1 and the counter advances":
let (conn, d) = freshScene()
defer:
close(conn)
removeDir(d)
var sc = loadScene(conn).get
check addWall(conn, sc, wall(10, 10, 100, 10)).get == 1
check addWall(conn, sc, wall(10, 20, 100, 20)).get == 2
check addWall(conn, sc, wall(10, 30, 100, 30)).get == 3
check sc.nextWallId == 4
check sc.walls.len == 3
check sc.walls[0].id == 1 and sc.walls[2].id == 3
test "a removal never renumbers and the next add never reuses the id":
let (conn, d) = freshScene()
defer:
close(conn)
removeDir(d)
var sc = loadScene(conn).get
discard addWall(conn, sc, wall(10, 10, 100, 10))
discard addWall(conn, sc, wall(10, 20, 100, 20))
check removeWall(conn, sc, 1) == waOk
check sc.walls.len == 1
check sc.walls[0].id == 2 # no renumbering
check sc.nextWallId == 3 # no decrement
check addWall(conn, sc, wall(10, 30, 100, 30)).get == 3 # not 1
check removeWall(conn, sc, 3) == waOk
check addWall(conn, sc, wall(10, 40, 100, 40)).get == 4
test "the id counter survives a reload, even when the newest wall was removed":
let (conn, d) = freshScene()
defer:
close(conn)
removeDir(d)
var sc = loadScene(conn).get
discard addWall(conn, sc, wall(10, 10, 100, 10))
discard addWall(conn, sc, wall(10, 20, 100, 20))
discard removeWall(conn, sc, 2)
let reloaded = loadScene(conn).get
check reloaded.nextWallId == 3
check reloaded.walls.len == 1
var again = reloaded
check addWall(conn, again, wall(10, 30, 100, 30)).get == 3
suite "persistence (write-through, restart-identical)":
test "add, update, door set and remove round-trip through a reload":
let (conn, d) = freshScene()
defer:
close(conn)
removeDir(d)
var sc = loadScene(conn).get
var north = defaultWall((0.0, 0.0), (4096.0, 0.0))
north.flags = %*{"note": "north"}
let id1 = addWall(conn, sc, north).get
let id2 = addWall(conn, sc, wall(100, 25, 300, 25, door = true)).get
check setDoor(conn, sc, id2, true) == waOk
var changed = defaultWall((0.0, 0.0), (2000.0, 0.0))
changed.id = id1
changed.door = true
changed.flags = %*{"note": "north wall"}
check updateWall(conn, sc, changed) == waOk
let reloaded = loadScene(conn).get
check reloaded.walls.len == 2
check reloaded.nextWallId == sc.nextWallId
check reloaded.walls[0].id == id1
check reloaded.walls[0].a == (0.0, 0.0)
check reloaded.walls[0].b == (2000.0, 0.0)
check reloaded.walls[0].door # the door switch is updatable
check reloaded.walls[0].open == false
check reloaded.walls[0].flags["note"].getStr == "north wall"
check reloaded.walls[1].id == id2
check reloaded.walls[1].door
check reloaded.walls[1].open == true # an open door stays open
check reloaded.walls[1].flags.kind == JObject # flags default to {}
check removeWall(conn, sc, id1) == waOk
check loadScene(conn).get.walls.len == 1
check loadScene(conn).get.nextWallId == 3
test "door.set refuses to open a secret or locked door, closing either is allowed (#56)":
let (conn, d) = freshScene()
defer:
close(conn)
removeDir(d)
var sc = loadScene(conn).get
var secret = wall(10, 10, 100, 10, door = true)
secret.secret = true
let secretId = addWall(conn, sc, secret).get
var locked = wall(10, 20, 100, 20, door = true)
locked.locked = true
let lockedId = addWall(conn, sc, locked).get
check setDoor(conn, sc, secretId, true) == waInvalid
check setDoor(conn, sc, lockedId, true) == waInvalid
check wallById(sc, secretId).get.open == false
check wallById(sc, lockedId).get.open == false
check loadScene(conn).get.walls[0].open == false
# Closing either is a no-op, never a refusal: the setting only refuses opening.
check setDoor(conn, sc, secretId, false) == waOk
check setDoor(conn, sc, lockedId, false) == waOk
# Clearing the setting makes the same door openable, and the lock/secret never leaks.
var clear = wallById(sc, lockedId).get
clear.locked = false
check updateWall(conn, sc, clear) == waOk
check setDoor(conn, sc, lockedId, true) == waOk
test "nullable height and label use SQL literals and round-trip as none, not 0 or ''":
let (conn, d) = freshScene()
defer:
close(conn)
removeDir(d)
var sc = loadScene(conn).get
var withBoth = defaultWall((10.0, 10.0), (100.0, 10.0))
withBoth.height = some(2.5)
withBoth.label = some("the king's window")
let id = addWall(conn, sc, withBoth).get
let plain = addWall(conn, sc, defaultWall((10.0, 20.0), (100.0, 20.0))).get
let reloaded = loadScene(conn).get
check reloaded.walls[0].height == some(2.5)
check reloaded.walls[0].label == some("the king's window")
check reloaded.walls[1].height.isNone # NULL, never 0 (a height is > 0)
check reloaded.walls[1].label.isNone # NULL, never '' (a label is 1..64)
check conn.getValue(sql("SELECT label FROM wall WHERE id = " & $id)) == "the king's window"
check sqlLitText("the king's window") == "'the king''s window'"
check sqlLitTextOrNull(none(string)) == "NULL"
check sqlLitRealOrNull(none(float)) == "NULL"
check sqlLitRealOrNull(some(1.8)) == "1.8"
check conn.getValue(sql("SELECT count(*) FROM wall WHERE id = " & $plain & " AND height IS NULL")) == "1"
test "authoring runs with synchronous=FULL and leaves NORMAL behind (Q12)":
let (conn, d) = freshScene()
defer:
close(conn)
removeDir(d)
var sc = loadScene(conn).get
check conn.getValue(sql"PRAGMA synchronous") == "1" # NORMAL from openDatabase
discard addWall(conn, sc, wall(10, 10, 100, 10))
check conn.getValue(sql"PRAGMA synchronous") == "1"
discard setDoor(conn, sc, 1, true) # not a door: no write at all
check conn.getValue(sql"PRAGMA synchronous") == "1"
test "a failing write leaves memory and the pragma alone":
let (conn, d) = freshScene()
defer:
close(conn)
removeDir(d)
var sc = loadScene(conn).get
conn.exec(sql"DROP TABLE wall") # make the INSERT fail inside the transaction
var raised = false
try:
discard addWall(conn, sc, wall(10, 10, 100, 10))
except CatchableError:
raised = true
check raised
check sc.walls.len == 0
check sc.nextWallId == 1
check conn.getValue(sql"PRAGMA synchronous") == "1"
test "walls load in id order whatever the insertion order was":
let (conn, d) = freshScene()
defer:
close(conn)
removeDir(d)
var sc = loadScene(conn).get
for i in 0 ..< 5:
discard addWall(conn, sc, wall(float(i), 0, float(i) + 1, 0))
discard removeWall(conn, sc, 2)
discard addWall(conn, sc, wall(9, 0, 10, 0))
let ids = loadScene(conn).get.walls.mapIt(it.id)
check ids == @[1, 3, 4, 5, 6]
suite "validation (DESIGN 6.1 `wall.*`)":
test "a zero-length wall is invalid and writes nothing":
let (conn, d) = freshScene()
defer:
close(conn)
removeDir(d)
var sc = loadScene(conn).get
check addWall(conn, sc, wall(50, 50, 50, 50)).isNone
check addWall(conn, sc, wall(0.0, 0.0, 0.0, 0.0)).isNone
check sc.walls.len == 0
check sc.nextWallId == 1
test "both endpoints must be inside [0,w] x [0,h]; the border itself is allowed":
let (conn, d) = freshScene()
defer:
close(conn)
removeDir(d)
var sc = loadScene(conn).get
check addWall(conn, sc, wall(-1, 10, 100, 10)).isNone
check addWall(conn, sc, wall(10, 10, 4097, 10)).isNone
check addWall(conn, sc, wall(10, 10.5, 100, 4097)).isNone
check addWall(conn, sc, wall(10, 4100, 100, 4100)).isNone
check addWall(conn, sc, wall(0, 0, 4096, 4096)).isSome # corners, in bounds
check addWall(conn, sc, wall(4096, 4096, 0, 0)).isSome
check sc.walls.len == 2
test "presets expand to switches only through the one resolve path (#55)":
let base = defaultWall((0.0, 0.0), (10.0, 0.0))
var sized = base
sized.thickness = 12
sized.position = wpLeft
sized.height = some(3.0)
sized.label = some("railing")
check parsePreset("wall").get == pwWall
check parsePreset("one_way").get == pwOneWay
check parsePreset("terrain").isNone
let window = resolveWall(sized, some(pwWindow), WallSwitches()).get
check not window.blocksSight and window.blocksMovement
check window.door and not window.open and window.playerMarker
check window.thickness == 12 and window.position == wpLeft and window.height == some(3.0)
check window.label == some("railing") # a preset never touches non-switch fields
let oneWay = resolveWall(sized, some(pwOneWay), WallSwitches()).get
check oneWay.sightDir == sdLeft and oneWay.blocksSight and not oneWay.door
# preset xor raw switches: even a switch equal to the expansion is a mix
check resolveWall(sized, some(pwDoor), WallSwitches(blocksSight: some(true))).isNone
check resolveWall(sized, some(pwDoor), WallSwitches(open: some(true))).isNone
test "the switch-set invariants reject door-only settings on a non-door (#55)":
let plain = defaultWall((0.0, 0.0), (10.0, 0.0))
check resolveWall(plain, none(Preset), WallSwitches(open: some(true))).isNone
check resolveWall(plain, none(Preset), WallSwitches(secret: some(true))).isNone
check resolveWall(plain, none(Preset), WallSwitches(locked: some(true))).isNone
check resolveWall(plain, none(Preset), WallSwitches(secret: some(false))).isSome
# clearing door closes and de-secrets, but naming a true door-only switch with it is invalid
var secretDoor = plain
secretDoor.door = true
secretDoor.secret = true
let closed = resolveWall(secretDoor, none(Preset), WallSwitches(door: some(false))).get
check not closed.door and not closed.open and not closed.secret and not closed.locked
check resolveWall(secretDoor, none(Preset),
WallSwitches(door: some(false), secret: some(true))).isNone
# open excludes secret and locked; a direction needs sight to block
var lockedDoor = plain
lockedDoor.door = true
lockedDoor.locked = true
check resolveWall(lockedDoor, none(Preset), WallSwitches(open: some(true))).isNone
check resolveWall(plain, none(Preset),
WallSwitches(blocksSight: some(false), sightDir: some(sdLeft))).isNone
test "a label is 1..64 code points, trimmed and free of control characters (#55)":
let (conn, d) = freshScene()
defer:
close(conn)
removeDir(d)
var sc = loadScene(conn).get
var w = defaultWall((10.0, 10.0), (100.0, 10.0))
w.label = some("x")
check addWall(conn, sc, w).isSome
w.label = some("é".repeat(64)) # code points, not bytes
check addWall(conn, sc, w).isSome
for bad in ["", " x", "x ", "x\ty", "x\u007fy", " ".repeat(3), "x".repeat(65)]:
w.label = some(bad)
check addWall(conn, sc, w).isNone
check sc.walls.len == 2
test "a wall is never open: the stored row normalizes it":
let (conn, d) = freshScene()
defer:
close(conn)
removeDir(d)
var sc = loadScene(conn).get
let id = addWall(conn, sc, wall(10, 10, 100, 10, open = true)).get
check wallById(sc, id).get.open == false
check loadScene(conn).get.walls[0].open == false
test "update keeps the row's id and open flag unless the door switch is cleared":
let (conn, d) = freshScene()
defer:
close(conn)
removeDir(d)
var sc = loadScene(conn).get
let id = addWall(conn, sc, wall(10, 10, 100, 10, door = true)).get
check setDoor(conn, sc, id, true) == waOk
var w = wallById(sc, id).get
w.a = (20.0, 20.0)
check updateWall(conn, sc, w) == waOk
check wallById(sc, id).get.open == true # untouched by update
# clearing the door switch closes it (a non-door stores no door settings)
var back = wallById(sc, id).get
back.door = false
check updateWall(conn, sc, back) == waOk
check wallById(sc, id).get.open == false
check loadScene(conn).get.walls[0].open == false
test "dot patching: only the given fields change":
let (conn, d) = freshScene()
defer:
close(conn)
removeDir(d)
var sc = loadScene(conn).get
let id = addWall(conn, sc, wall(10, 10, 100, 10)).get
let before = wallById(sc, id).get
let after = patched(before, WallPatch(b: some((300.0, 400.0)))).get
check after.a == before.a
check after.b == (300.0, 400.0)
check after.door == before.door
check after.thickness == before.thickness
check after.flags.kind == JObject
suite "lookup outcomes (main.nim's 6.5 mapping)":
test "unknown ids are not-found, wrong kinds are their own outcome":
let (conn, d) = freshScene()
defer:
close(conn)
removeDir(d)
var sc = loadScene(conn).get
let id = addWall(conn, sc, wall(10, 10, 100, 10)).get
check removeWall(conn, sc, 99) == waNotFound
check updateWall(conn, sc, WallRec(id: 99, a: (0, 0), b: (1, 1))) == waNotFound
check setDoor(conn, sc, 99, true) == waNotFound
check setDoor(conn, sc, id, true) == waNotDoor # a plain wall has no open state
check setDoor(conn, sc, id, false) == waNotDoor
check sc.walls.len == 1
check wallById(sc, 99).isNone
check wallIndex(sc, id).get == 0
test "the first wall of a fresh scene is id 1, not 0":
let (conn, d) = freshScene()
defer:
close(conn)
removeDir(d)
var sc = loadScene(conn).get
check sc.nextWallId == 1
let id = addWall(conn, sc, wall(1, 1, 2, 2)).get
check id == 1
check wallById(sc, 0).isNone
suite "scene.set (6.1, slice 10 / #25)":
test "a patch changes only the fields it names and the row round-trips":
let (conn, d) = freshScene()
defer:
close(conn)
removeDir(d)
let before = loadScene(conn).get
let p = parseScenePatch(%*{"name": "Cave", "grid_size": 64,
"vision_radius_default": 500, "token_radius_default": 30,
"extra": {"mood": "dark"}})
check p.isSome
let after = patched(before, p.get)
check after.name == "Cave"
check after.gridSize == 64
check after.visionRadiusDefault == 500
check after.tokenRadiusDefault == 30
check after.extra["mood"].getStr == "dark"
# the non-settable columns and the wall counter are untouched (import is the M6 CLI)
check (after.w, after.h) == (before.w, before.h)
check after.tileSize == before.tileSize and after.cell == before.cell
check after.artVersion == before.artVersion
check after.nextWallId == before.nextWallId
storeScene(conn, after)
let back = loadScene(conn).get
check back.name == "Cave"
check back.gridSize == 64
check back.visionRadiusDefault == 500
check back.tokenRadiusDefault == 30
check back.extra["mood"].getStr == "dark"
test "one field is enough; a patch with none is invalid":
check parseScenePatch(%*{"name": "Cave"}).isSome
check parseScenePatch(%*{"grid_size": 0}).isSome
check parseScenePatch(%*{}).isNone
check parseScenePatch(newJNull()).isNone
check parseScenePatch(nil).isNone
check parseScenePatch(%*[1]).isNone
test "the non-settable columns are unknown fields: alone they leave no patch field":
for key in ["w", "h", "tile_size", "cell", "art_version"]:
check parseScenePatch(%*{key: 1}).isNone
# and they are ignored next to a real field (6.2)
let p = parseScenePatch(%*{key: 1, "name": "Cave"})
check p.isSome
check p.get.name.get == "Cave"
test "field shapes: text by the 6.5 rule, numbers finite, extra an object":
check parseScenePatch(%*{"name": ""}).isNone
check parseScenePatch(%*{"name": " padded"}).isNone # trimmed rule, like other names
check parseScenePatch(%*{"name": repeat("x", SceneNameMaxLen + 1)}).isNone
check parseScenePatch(%*{"name": repeat("x", SceneNameMaxLen)}).isSome
check parseScenePatch(%*{"name": 5}).isNone
check parseScenePatch(%*{"name": newJNull()}).isNone # declared, wrong type
check parseScenePatch(%*{"grid_size": -1}).isNone
check parseScenePatch(%*{"grid_size": "60"}).isNone
check parseScenePatch(%*{"grid_size": 0}).isSome
check parseScenePatch(%*{"vision_radius_default": 0}).isNone
check parseScenePatch(%*{"vision_radius_default": -5}).isNone
check parseScenePatch(%*{"token_radius_default": 0}).isNone
check parseScenePatch(%*{"token_radius_default": 20.5}).isSome
check parseScenePatch(%*{"extra": []}).isNone
check parseScenePatch(%*{"extra": newJNull()}).isNone
check parseScenePatch(%*{"extra": {}}).isSome