import assert from "node:assert/strict"; import test from "node:test"; import { parseValue, formatValue } from "../src/values"; import { REF } from "./fixtures"; test("pretty natural JSON uses two-space indentation without losing values or markers", () => { const source = `{"reserved":[null],"queued":[9223372036854775807,{"$actor":"${REF}"},{"$record":{"$actor":"literal"}}]}`; const value = parseValue(source); const pretty = formatValue(value, 2); assert.equal( pretty, `{ "reserved": [ null ], "queued": [ 9223372036854775807, { "$actor": "${REF}" }, { "$record": { "$actor": "literal" } } ] }`, ); assert.deepEqual(parseValue(pretty), value); assert.equal(formatValue(value), source); }); test("editing JSON round-trips exact integers, references, null messages and escaped records", () => { for (const source of [ "null", "-9223372036854775808", "9223372036854775807", `{"reserved":[null],"queued":[{"$actor":"${REF}"},{"$record":{"$actor":"literal"}}]}`, '{"$record":{"$record":{"value":true}}}', '{"__proto__":null}', ]) { const value = parseValue(source); assert.deepEqual(parseValue(formatValue(value)), value); } }); test("natural JSON preserves i64s, string whitespace, markers, and all value variants", () => { assert.deepEqual( parseValue( `{"items":[null,true,-9223372036854775808,9223372036854775807,"two spaces",{"$actor":"@${REF}"}]}`, ), { type: "record", data: { items: { type: "list", data: [ { type: "null" }, { type: "bool", data: true }, { type: "integer", data: -9223372036854775808n }, { type: "integer", data: 9223372036854775807n }, { type: "string", data: "two spaces" }, { type: "actor_ref", data: REF }, ], }, }, }, ); assert.deepEqual(parseValue(`"${REF}"`), { type: "string", data: REF }); assert.deepEqual(parseValue('{"$record":{"$actor":"literal"}}'), { type: "record", data: { $actor: { type: "string", data: "literal" } }, }); assert.deepEqual(parseValue('{"__proto__":null}'), { type: "record", data: Object.fromEntries([["__proto__", { type: "null" }]]), }); }); test("invalid inputs and depth/node/byte overflow fail explicitly", () => { for (const source of [ "", "undefined", "1.5", "1e2", "9223372036854775808", "-9223372036854775809", '{"$actor":"binding"}', '{"$actor":1}', '{"$record":[]}', '{"$record":null}', "[".repeat(33) + "null" + "]".repeat(33), JSON.stringify(Array(4096).fill(null)), JSON.stringify("x".repeat(1024 * 1024)), ]) assert.throws(() => parseValue(source)); assert.doesNotThrow(() => parseValue("[".repeat(32) + "null" + "]".repeat(32))); assert.doesNotThrow(() => parseValue(JSON.stringify(Array(4095).fill(null)))); });