## The intent -> world boundary copies (ANALYSIS 4.1): a regression guard for the *shape* of the ## fix, not for the race itself. A unit test can prove that the helper returns memory that aliases ## nothing the caller passed in (identity and string-payload checks); it cannot prove that every ## handler calls it, and it cannot reproduce a non-atomic refcount race. The handler audit and the ## "frames are built under the lock" rule are verified by reading, not by this file. import std/[json, options, unittest] import jsonbound, props, world proc ptrOf(n: JsonNode): pointer = cast[pointer](n) proc payload(s: string): pointer = ## The address of a string's first character: two strings that share a payload answer the same. if s.len == 0: nil else: cast[pointer](unsafeAddr s[0]) suite "detachedJson": test "rebuilds nodes and string payloads": let src = parseJson("""{"vision":true,"name":"scroll","nested":{"k":[1,2,3]}}""") let dst = detachedJson(src) check ptrOf(dst) != ptrOf(src) check $dst == $src check payload(dst["name"].getStr) != payload(src["name"].getStr) check ptrOf(dst["nested"]) != ptrOf(src["nested"]) check ptrOf(dst["nested"]["k"]) != ptrOf(src["nested"]["k"]) test "mutating the source leaves the copy alone": let src = parseJson("""{"vision":true}""") let dst = detachedJson(src) src["vision"] = %false check dst["vision"].getBool check not src["vision"].getBool test "absent (nil) stays nil and null stays null": check detachedJson(nil).isNil check detachedJson(newJNull()).kind == JNull suite "detachedString": test "does not share the payload": let src = "vision" let dst = detachedString(src) check dst == src check payload(dst) != payload(src) test "empty strings stay empty": check detachedString("") == "" check payload(detachedString("")) == nil suite "detachedProps": test "three independent buckets, keys and values included": let src = parseJson("""{"public":{"a":1},"owner":{"b":{"c":2}},"gm":{}}""") var parsed: Props for name, bucket in src: parsed[parseBucket(name).get] = bucket let dst = detachedProps(parsed) for b in PropBucket: check ptrOf(dst[b]) != ptrOf(parsed[b]) check $dst[b] == $parsed[b] var srcKey, dstKey: string for k in parsed[pbOwner].keys: srcKey = k for k in dst[pbOwner].keys: dstKey = k check payload(dstKey) != payload(srcKey) suite "the premise this guards (world.parseFlags)": test "parseFlags returns the caller's node by reference, not a copy": # If this ever becomes a copy, the boundary still works; the test records why it exists. let env = parseJson("""{"flags":{"vision":true}}""") let flags = parseFlags(env{"flags"}) check flags.isSome check ptrOf(flags.get) == ptrOf(env{"flags"})