## Property buckets, caps and the schema registry (#22, DESIGN 6.1 prop payload rules, 8, 9). Pure ## unit tests: no DB, no socket. The wire behaviour (the ack codes, the bucket filtering a player ## sees) lives in tools/details.test.mjs and tests/tviews.nim. import std/[algorithm, json, options, sequtils, strutils, tables, unittest] import props proc bucketed(): Props = result = newProps() result[pbPublic] = %*{"aura": "cold"} result[pbOwner] = %*{"hp": 7} result[pbGm] = %*{"secret": "yes"} suite "caps (6.1, numbers FIXED owner-delegated by #22)": test "64 keys per bucket, the 65th is refused and nothing changes": var p = newProps() for i in 0 ..< MaxPropKeys: check p.setProp(pbPublic, "k" & $i, %i) check p[pbPublic].len == MaxPropKeys check not p.setProp(pbPublic, "one-too-many", %1) check p[pbPublic].len == MaxPropKeys check not p.contains(pbPublic, "one-too-many") # a bucket has its own budget: the owner bucket is still empty check p.setProp(pbOwner, "k0", %1) test "an existing key can still be overwritten at the cap": var p = newProps() for i in 0 ..< MaxPropKeys: check p.setProp(pbPublic, "k" & $i, %i) check p.setProp(pbPublic, "k0", %"replaced") check p[pbPublic]["k0"].getStr == "replaced" test "a key is 1..64 code points, trimmed, with no control characters": check validPropKey("hp") check validPropKey(repeat("x", MaxPropKeyLen)) check validPropKey("café") # code points, not bytes check not validPropKey("") check not validPropKey(" padded") check not validPropKey("padded ") check not validPropKey(repeat("x", MaxPropKeyLen + 1)) check not validPropKey("tab\there") test "a value is at most 8 KB of serialized JSON": var p = newProps() let small = %repeat("x", 8000) # ~8 KB with the quotes check valueBytes(small) <= MaxPropValueBytes check p.setProp(pbPublic, "small", small) let big = %repeat("x", MaxPropValueBytes + 1) check valueBytes(big) > MaxPropValueBytes suite "parseProps (the `props` field of token.create)": test "absent and null mean three empty buckets": for node in [JsonNode(nil), newJNull()]: let parsed = parseProps(defaultRegistry(), node) check parsed.isSome for b in PropBucket: check parsed.get[b].kind == JObject and parsed.get[b].len == 0 test "the three bucket names are the only ones accepted": let reg = defaultRegistry() check parseProps(reg, %*{"public": {"a": 1}}).isSome check parseProps(reg, %*{"public": {}, "owner": {}, "gm": {}}).isSome check parseProps(reg, %*{"secret": {}}).isNone check parseProps(reg, %*{"public": 3}).isNone check parseProps(reg, %*{"public": {"k": newJNull()}}).isNone check parseProps(reg, %*[1, 2]).isNone check parseProps(reg, %*{"public": {"": 1}}).isNone test "the caps are enforced while parsing": let many = newJObject() for i in 0 ..< MaxPropKeys + 1: many["k" & $i] = %i check parseProps(defaultRegistry(), %*{"public": many}).isNone let tooBig = newJObject() tooBig["k"] = %repeat("x", MaxPropValueBytes + 1) check parseProps(defaultRegistry(), %*{"public": tooBig}).isNone suite "the registry (DESIGN 8/9: v1 is empty and permissive)": test "with no entries every key is accepted in every bucket": let reg = defaultRegistry() for b in PropBucket: check reg.checkWrite(b, "anything", %*{"nested": [1, 2, 3]}) check reg.checkWrite(b, "anything", %"text") check reg.checkWrite(b, "anything", %12.5) check reg.checkWrite(b, "anything", %true) test "a registered key is enforced: kind and bucket": var reg = defaultRegistry() reg.schemas["hp"] = PropSchema(kind: pkNumber, bucket: some(pbOwner)) check reg.checkWrite(pbOwner, "hp", %7) check not reg.checkWrite(pbOwner, "hp", %"seven") # wrong kind check not reg.checkWrite(pbPublic, "hp", %7) # wrong bucket check reg.checkWrite(pbPublic, "other", %"anything") # unregistered keys still use the fallback check parseProps(reg, %*{"owner": {"hp": 3}}).isSome check parseProps(reg, %*{"owner": {"hp": "three"}}).isNone check parseProps(reg, %*{"public": {"hp": 3}}).isNone test "a schema with no bucket accepts the key anywhere": var reg = defaultRegistry() reg.schemas["note"] = PropSchema(kind: pkString, bucket: none(PropBucket)) for b in PropBucket: check reg.checkWrite(b, "note", %"hi") for b in PropBucket: check not reg.checkWrite(b, "note", %1) suite "set / unset": test "unsetting an absent key is allowed and reports it did nothing": var p = bucketed() check p.unsetProp(pbPublic, "aura") check not p.contains(pbPublic, "aura") check not p.unsetProp(pbPublic, "aura") test "buckets are independent: the same key may live in all three": var p = newProps() check p.setProp(pbPublic, "hp", %1) check p.setProp(pbOwner, "hp", %2) check p.setProp(pbGm, "hp", %3) check p[pbPublic]["hp"].getInt == 1 check p[pbOwner]["hp"].getInt == 2 check p[pbGm]["hp"].getInt == 3 test "propsJson carries all three buckets, always as objects": let node = propsJson(bucketed()) check node.kind == JObject check node.keys.toSeq.sorted == @["gm", "owner", "public"] check node["public"]["aura"].getStr == "cold" check node["gm"]["secret"].getStr == "yes" test "clone gives fresh buckets: editing the copy cannot reach the original": var original = bucketed() var copy = original.clone() check copy.setProp(pbPublic, "new", %1) check copy.unsetProp(pbPublic, "aura") check not original.contains(pbPublic, "new") check original.contains(pbPublic, "aura") check copy[pbOwner]["hp"].getInt == 7 # untouched values are shared, not lost test "normalize repairs nil buckets": var p: Props let fixed = normalize(p) for b in PropBucket: check fixed[b].kind == JObject