Files
FFF_vtt_frozen/tests/tworld.nim
T
SirStone bf9ab3edfd server: scene and wall state, slice-4 migration (#19)
Migration step 3 ("world") creates scene and wall and seeds the single v1 scene
(id 1, 4096x4096, tile = cell 32, vision 400, token radius 20, art_version 1,
next_wall_id 1). server/world.nim holds the in-memory scene with SQLite as
write-through persistence: per-scene wall ids from next_wall_id (monotonic,
never reused, #3), 6.1 validation (a != b, in bounds, a wall is never open),
flags as an opaque JSON object, and one transaction with synchronous=FULL
around every GM authoring write (Q12); memory follows COMMIT, so a failed
write leaves both sides unchanged.

Tests: tests/tworld.nim (ids, reload, doors, validation, pragma, failed write),
tests/tdb.nim (version 3, history, a slice-2 DB migrating to slice 3).
2026-11-15 08:49:16 +01:00

303 lines
12 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, 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, kind = wkWall, open = false): WallRec =
WallRec(a: (ax, ay), b: (bx, by), kind: kind, open: open, flags: newJObject())
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 "kind, open and flags accept only their own shape":
check parseKind("wall").get == wkWall
check parseKind("door").get == wkDoor
check parseKind("window").isNone
check parseKind("").isNone
check parseKind("WALL").isNone
check parseOpen(nil).get == false # omitted = closed
check parseOpen(%true).get == true
check parseOpen(%1).isNone
check parseFlags(nil).get.kind == JObject # omitted = {}
check parseFlags(%*{"a": 1}).get["a"].getInt == 1
check parseFlags(%*[1]).isNone
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
let id1 = addWall(conn, sc, WallRec(a: (0, 0), b: (4096, 0), kind: wkWall,
flags: %*{"note": "north"})).get
let id2 = addWall(conn, sc, wall(100, 25, 300, 25, wkDoor)).get
check setDoor(conn, sc, id2, true) == waOk
let changed = WallRec(id: id1, a: (0, 0), b: (2000, 0), kind: wkDoor,
open: false, 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].kind == wkDoor # kind 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].kind == wkDoor
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 "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 "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, wkWall, 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 kind changes":
let (conn, d) = freshScene()
defer:
close(conn)
removeDir(d)
var sc = loadScene(conn).get
let id = addWall(conn, sc, wall(10, 10, 100, 10, wkDoor)).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
# door -> wall closes it (a wall is never open)
var back = wallById(sc, id).get
back.kind = wkWall
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))))
check after.a == before.a
check after.b == (300.0, 400.0)
check after.kind == before.kind
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