diff --git a/.github/workflows/nightly.yml b/.github/workflows/nightly.yml index d21500e7a80..843fe544931 100644 --- a/.github/workflows/nightly.yml +++ b/.github/workflows/nightly.yml @@ -282,7 +282,7 @@ jobs: # viewer: diff-independent, since a break can arrive through the relay, # js/net, or the wire between them. # `gh test` exercises workflow and release tooling, which no pull request runs. - recipe: ["rs doctest --workspace", "rs loom", "test drill-sensitivity", "rs uring", "test drain", "gh test"] + recipe: ["rs doctest --workspace", "rs loom", "test drill-sensitivity", "rs uring", "test drain", "gh test", "test json-properties"] steps: - name: Free disk space uses: jlumbroso/free-disk-space@ceedf095f4ec1a097402bc6bd80831f2e1a6fde6 # main diff --git a/Cargo.lock b/Cargo.lock index 43014bf265d..c9bd4752f5d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1411,6 +1411,12 @@ dependencies = [ "winapi", ] +[[package]] +name = "core_detect" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f8f80099a98041a3d1622845c271458a2d73e688351bf3cb999266764b81d48" + [[package]] name = "coreaudio-rs" version = "0.14.2" @@ -4543,6 +4549,7 @@ dependencies = [ "kio 0.6.2", "moq-flate", "moq-net", + "proptest", "serde", "serde_json", "serde_path_to_error", @@ -7046,6 +7053,22 @@ version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3d595e54a326bc53c1c197b32d295e14b169e3cfeaa8dc82b529f947fba6bcf5" +[[package]] +name = "proptest" +version = "1.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8530004ccb15eae51c7e40009fbe317f341f804db54dc033eec1c50be28cfa0" +dependencies = [ + "bitflags 2.13.2", + "chacha20", + "core_detect", + "num-traits", + "rand 0.10.3", + "rand_xorshift", + "regex-syntax", + "unarray", +] + [[package]] name = "pulseaudio" version = "0.3.1" @@ -7287,6 +7310,15 @@ dependencies = [ "rand_core 0.10.1", ] +[[package]] +name = "rand_xorshift" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60aa6af80be32871323012e02e6e65f8a7cc7890931ae421d217ad8fe0df2ccf" +dependencies = [ + "rand_core 0.10.1", +] + [[package]] name = "range-alloc" version = "0.1.5" @@ -9515,6 +9547,12 @@ dependencies = [ "windows-sys 0.61.2", ] +[[package]] +name = "unarray" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eaea85b334db583fe3274d12b4cd1880032beab409c0d774be044d4480ab9a94" + [[package]] name = "unicode-ident" version = "1.0.26" diff --git a/bun.lock b/bun.lock index 3bfa5dca3df..6ca5557cfc5 100644 --- a/bun.lock +++ b/bun.lock @@ -141,6 +141,7 @@ }, "devDependencies": { "@types/bun": "^1.4.2", + "fast-check": "^4.10.2", "rimraf": "^6.1.3", "typescript": "7.0.2", }, @@ -1193,6 +1194,8 @@ "extend-shallow": ["extend-shallow@2.0.1", "", { "dependencies": { "is-extendable": "^0.1.0" } }, "sha512-zCnTtlxNoAiDc3gqY2aYAWFx7XWWiasuF2K8Me5WbN8otHKTUKBwjPtNpRs/rbUZm7KxWAaNj7P1a/p52GbVug=="], + "fast-check": ["fast-check@4.10.2", "", { "dependencies": { "pure-rand": "^8.0.0" } }, "sha512-iK2f+YrcmoeGqk6fA0ea2bptcu/itMIm4NfEozq6N25+aG6h7s5HZbB/k1aV7b5w5sFLMCbbtRUsTVR+BgC3xw=="], + "fast-glob": ["fast-glob@3.3.3", "", { "dependencies": { "@nodelib/fs.stat": "^2.0.2", "@nodelib/fs.walk": "^1.2.3", "glob-parent": "^5.1.2", "merge2": "^1.3.0", "micromatch": "^4.0.8" } }, "sha512-7MptL8U0cqcFdzIzwOTHoilX9x5BrNqye7Z/LuC7kCMRio1EMSyqRK3BEAUD7sXRq4iT4AzTVuZdhgQ2TCvYLg=="], "fastq": ["fastq@1.20.1", "", { "dependencies": { "reusify": "^1.0.4" } }, "sha512-GGToxJ/w1x32s/D2EKND7kTil4n8OVk/9mycTc4VDza13lOvpUZTGX3mFSCtV9ksdGBVzvsyAVLM6mHFThxXxw=="], @@ -1551,6 +1554,8 @@ "punycode.js": ["punycode.js@2.3.1", "", {}, "sha512-uxFIHU0YlHYhDQtV4R9J6a52SLx28BCjT+4ieh7IGbgwVJWO+km431c4yRlREUAsAmt/uMjQUyQHNEPf0M39CA=="], + "pure-rand": ["pure-rand@8.4.2", "", {}, "sha512-vvuOGgcuPJAirlHvuQw1TrOiw7ptaIXXmIbNuiNOY6lNGJJH49PQ1Kj4nd783nPdQhQdicgOjVI2yI/9BD6/Ng=="], + "queue-microtask": ["queue-microtask@1.2.3", "", {}, "sha512-NuaNSa6flKT5JaSYQzJok04JzTL1CA6aGhv5rfLW3PgqA+M2ChpZQnAC8h8i4ZFkBS8X5RqkDBHA7r4hej3K9A=="], "react": ["react@19.3.0", "", {}, "sha512-E8LUcbtBWt20bbl2YoHfx4ZDBdxVTfOKtCZn9cDSJ4l6/nuoApcpIBcj47t2wZoVX8g2ZHuMHbiShgCR1T5Sog=="], diff --git a/js/json/bench/codec.ts b/js/json/bench/codec.ts new file mode 100644 index 00000000000..1abc04c5a9a --- /dev/null +++ b/js/json/bench/codec.ts @@ -0,0 +1,34 @@ +/** Sweep snapshot codec cost over document size and changed keys. */ +import { Decoder } from "../src/snapshot/decoder.ts"; +import { Encoder } from "../src/snapshot/encoder.ts"; + +const updates = 256; +console.log("keys\tchanged\tcompression\tms/update"); +for (const keys of [1, 10, 100, 1000]) { + for (const changed of new Set([1, Math.min(10, keys)])) { + for (const compression of ["none", "deflate"] as const) { + const samples: number[] = []; + for (let sample = 0; sample < 3; sample++) { + const config = { deltaRatio: 100, compression }; + const encoder = new Encoder>(config); + const decoder = new Decoder>(config); + let value = Object.fromEntries(Array.from({ length: keys }, (_, n) => [`k${n}`, 0])); + const start = performance.now(); + for (let n = 1; n <= updates; n++) { + value = { ...value }; + for (let key = 0; key < changed; key++) value[`k${key}`] = n; + const frame = encoder.update(value); + if (frame) { + if (frame.keyframe) decoder.snapshot(frame.payload); + else decoder.delta(frame.payload); + frame.commit(); + } + if (decoder.decode()?.k0 !== n) throw new Error("codec benchmark lost an update"); + } + samples.push((performance.now() - start) / updates); + } + samples.sort((a, b) => a - b); + console.log(`${keys}\t${changed}\t${compression}\t${samples[1]?.toFixed(6)}`); + } + } +} diff --git a/js/json/bench/tsconfig.json b/js/json/bench/tsconfig.json new file mode 100644 index 00000000000..9e174aaa998 --- /dev/null +++ b/js/json/bench/tsconfig.json @@ -0,0 +1,5 @@ +{ + "extends": "../tsconfig.json", + "compilerOptions": { "rootDir": "..", "noEmit": true }, + "include": ["./*.ts"] +} diff --git a/js/json/package.json b/js/json/package.json index 28504425ffa..4a38f1b5506 100644 --- a/js/json/package.json +++ b/js/json/package.json @@ -30,6 +30,7 @@ }, "devDependencies": { "@types/bun": "^1.4.2", + "fast-check": "^4.10.2", "rimraf": "^6.1.3", "typescript": "7.0.2" } diff --git a/js/json/src/diff.test.ts b/js/json/src/diff.test.ts index 96789927e53..d9c6cb86714 100644 --- a/js/json/src/diff.test.ts +++ b/js/json/src/diff.test.ts @@ -68,3 +68,13 @@ test("deepEqual", () => { expect(deepEqual({ a: 1 }, { a: 1, b: 2 })).toBe(false); expect(deepEqual([1, 2], [1, 2, 3])).toBe(false); }); + +// JSON.parse creates an own data field; merging must preserve that representation. +test("prototype-named JSON keys stay own data fields", () => { + const patch = JSON.parse('{"__proto__":{"x":1}}'); + const result = merge({}, patch) as Record; + expect(Object.getPrototypeOf(result)).toBe(Object.prototype); + expect(Object.hasOwn(result, "__proto__")).toBe(true); + expect(Object.getOwnPropertyDescriptor(result, "__proto__")?.value).toEqual({ x: 1 }); + expect(deepEqual(JSON.parse('{"__proto__":{}}'), { a: {} })).toBe(false); +}); diff --git a/js/json/src/diff.ts b/js/json/src/diff.ts index c352c05b81d..e399572f392 100644 --- a/js/json/src/diff.ts +++ b/js/json/src/diff.ts @@ -18,6 +18,15 @@ function isObject(value: unknown): value is Obj { return typeof value === "object" && value !== null && !Array.isArray(value); } +// Assignment to __proto__ invokes an inherited setter; JSON requires an own data field. +function set(object: Obj, key: string, value: unknown): void { + if (key === "__proto__") { + Object.defineProperty(object, key, { value, enumerable: true, configurable: true, writable: true }); + } else { + object[key] = value; + } +} + /** * Generate an RFC 7396 merge patch transforming `oldVal` into `newVal`. * @@ -36,13 +45,13 @@ export function diff(oldVal: unknown, newVal: unknown): Diff { function diffObjects(oldObj: Obj, newObj: Obj, patch: Obj, forced: { value: boolean }): void { // Keys present in old but missing from new become explicit null deletions. for (const key of Object.keys(oldObj)) { - if (!(key in newObj)) patch[key] = null; + if (!Object.hasOwn(newObj, key)) set(patch, key, null); } for (const key of Object.keys(newObj)) { const newV = newObj[key]; - const oldV = oldObj[key]; - const inOld = key in oldObj; + const inOld = Object.hasOwn(oldObj, key); + const oldV = inOld ? oldObj[key] : undefined; if (inOld && deepEqual(oldV, newV)) continue; @@ -50,7 +59,7 @@ function diffObjects(oldObj: Obj, newObj: Obj, patch: Obj, forced: { value: bool if (isObject(oldV) && isObject(newV)) { const sub: Obj = {}; diffObjects(oldV, newV, sub, forced); - if (Object.keys(sub).length > 0) patch[key] = sub; + if (Object.keys(sub).length > 0) set(patch, key, sub); continue; } @@ -60,7 +69,7 @@ function diffObjects(oldObj: Obj, newObj: Obj, patch: Obj, forced: { value: bool if (newV === null || hasNullMember(newV)) { forced.value = true; } - patch[key] = newV; + set(patch, key, newV); } } @@ -84,7 +93,7 @@ export function merge(target: unknown, patch: unknown): unknown { if (value === null) { delete base[key]; } else { - base[key] = merge(base[key], value); + set(base, key, merge(Object.hasOwn(base, key) ? base[key] : undefined, value)); } } return base; @@ -101,7 +110,7 @@ export function deepEqual(a: unknown, b: unknown): boolean { if (isObject(a) && isObject(b)) { const keys = Object.keys(a); if (keys.length !== Object.keys(b).length) return false; - return keys.every((key) => key in b && deepEqual(a[key], b[key])); + return keys.every((key) => Object.hasOwn(b, key) && deepEqual(a[key], b[key])); } return false; diff --git a/js/json/src/snapshot/properties.test.ts b/js/json/src/snapshot/properties.test.ts new file mode 100644 index 00000000000..e777b0179e0 --- /dev/null +++ b/js/json/src/snapshot/properties.test.ts @@ -0,0 +1,81 @@ +import { expect, test } from "bun:test"; +import fc from "fast-check"; +import { Decoder } from "./decoder.ts"; +import { Encoder } from "./encoder.ts"; + +type Json = null | boolean | number | string | Json[] | { [key: string]: Json }; +type Step = { value: Json; reset: boolean }; + +// Mirror the Rust generator: bounded JSON trees plus edits to a persistent +// object, so sequences exercise deltas as well as unrelated snapshot roots. +const key = fc.oneof( + fc.constantFrom("a", "b", "c", "", "__proto__", "constructor", "prototype", "\0", "雪"), + fc.string({ maxLength: 8 }), +); +const leaf: fc.Arbitrary = fc.oneof( + fc.constant(null), + fc.boolean(), + fc.integer({ min: -1_000_000, max: 1_000_000 }).map((n) => n / 4), + fc.oneof(fc.string({ maxLength: 16 }), fc.constant("雪\0")), +); +const value: fc.Arbitrary = fc.letrec<{ value: Json }>((tie) => ({ + value: fc.oneof( + { maxDepth: 4, depthSize: "small", withCrossShrink: true }, + leaf, + fc.array(tie("value"), { maxLength: 4 }), + fc.dictionary(key, tie("value"), { maxKeys: 4 }), + ), +})).value; +const reset = fc.integer({ min: 0, max: 15 }).map((n) => n === 0); +const roots = fc.array(fc.record({ value, reset }), { minLength: 1, maxLength: 32 }); +const edits = fc + .array(fc.record({ key, value, remove: fc.boolean(), reset }), { minLength: 1, maxLength: 32 }) + .map((edits): Step[] => { + let current: Record = {}; + return edits.map(({ key, value, remove, reset }) => { + const next = { ...current }; + if (remove) delete next[key]; + else Object.defineProperty(next, key, { value, enumerable: true, configurable: true, writable: true }); + current = next; + return { value: next, reset }; + }); + }); + +function setting(name: string, fallback: number, min: number): number { + const raw = process.env[name]; + if (raw !== undefined && !/^\d+$/.test(raw)) throw new Error(`${name} must be an integer`); + const value = Number(raw ?? fallback); + if (!Number.isInteger(value) || value < min || value > 0x7fffffff) { + throw new Error(`${name} must be an integer between ${min} and 2147483647`); + } + return value; +} + +test("snapshot codec property reconstructs each JSON value", () => { + const seed = setting("MOQ_JSON_SEED", 0x6d6f71, 0); + const numRuns = setting("MOQ_JSON_CASES", 256, 1); + fc.assert( + fc.property( + fc.constantFrom(0, 1, 2, 8, 100), + fc.boolean(), + fc.oneof(roots, edits), + (deltaRatio, deflate, steps) => { + const config = { deltaRatio, compression: deflate ? ("deflate" as const) : ("none" as const) }; + const encoder = new Encoder(config); + const decoder = new Decoder(config); + for (const { value, reset } of steps) { + if (reset) encoder.reset(); + const frame = encoder.update(value); + if (frame) { + if (frame.keyframe) decoder.snapshot(frame.payload); + else decoder.delta(frame.payload); + frame.commit(); + } + // JSON serialization defines the domain, including its normalization. + expect(decoder.decode()).toEqual(JSON.parse(JSON.stringify(value))); + } + }, + ), + { seed, numRuns }, + ); +}); diff --git a/quest/m2/README.md b/quest/m2/README.md index 222b9cf3774..01356504a32 100644 --- a/quest/m2/README.md +++ b/quest/m2/README.md @@ -27,7 +27,6 @@ ranked just below deterministic TS legs, which it requires. - [Binding audio delay](/quest/m2/binding-surface.md) - moq-ffi and every wrapper configure and observe audio playout delay - [Drill mutation gate](/quest/m2/drill-mutation-gate.md) - a PR touching a drill mutation's target runs that mutation's sensitivity check, advisory until the drills run on controlled time -- [Snapshot properties](/quest/m2/json-snapshot-properties.md) - random value sequences through the JS and Rust Snapshot codecs always decode to the published value - [Window properties](/quest/m2/json-window-properties.md) - random push/pop sequences through the JS and Rust Window codecs decode to the producer's window, for readers that keep up or join late - [Pipelined requests](/quest/m2/pipeline-requests/README.md) - SUBSCRIBE and the first FETCH go out with the track-info request at every hop, so first data arrives a round trip sooner per hop - [OBS native codecs](/quest/m2/obs-moq-video/README.md) - replace FFmpeg video and audio decoding with moq-video and moq-audio, deliver GPU frames, and use native audio/video encoders diff --git a/quest/m2/json-snapshot-properties.md b/quest/m2/json-snapshot-properties.md deleted file mode 100644 index 5c71ddb83c2..00000000000 --- a/quest/m2/json-snapshot-properties.md +++ /dev/null @@ -1,48 +0,0 @@ -# [S] Snapshot codec property tests - -## Goal - -`@moq/json` and `moq-json` each have a property test that pushes random -sequences of JSON values through the Snapshot `Encoder` and `Decoder` and -asserts the decoder reconstructs every value exactly. It runs on every PR and -in the nightly. No change to the codec itself unless the test finds a bug. - -## Plan - -Why: #5513 fixes a JS encoder that dropped a `null` nested in an object it -wrote whole, because merge patch reads `null` as a deletion. The decoder -silently disagreed with the producer until the next snapshot group. The -shared `rs/moq-json/tests/vectors.json` pins the cases found so far, but only -the ones someone thought of. - -Decided 2026-10-14: - -- **Codec sequences, not diff pairs.** The property runs whole value - sequences through `Encoder` -> `Decoder`, so it covers `diff`/`merge`, - forced snapshots, `deltaRatio` rolls, and compression together, at the - level a consumer sees. A separate pure `diff`/`merge` pair property was - rejected: the codec property already catches what it would, and shrinking - keeps the failing case small. -- **Per implementation.** Each language checks its own round trip. Asserting - that JS and Rust reach the same snapshot/delta decision on random inputs - was rejected for needing a cross-process harness; the shared vectors keep - covering agreement on known cases. -- **Generators** must reach the edge cases of merge patch: `null` at any - depth inside objects and arrays, empty objects, a key changing type - (scalar, array, object), keys added and removed, and non-object roots. -- **CI:** PR runs use a fixed seed and a modest case count, so the inputs - only change when the generator does, and a PR goes red only when it changes - the outcome of one of those cases. The nightly runs a random seed and more - cases. A nightly failure's shrunk case lands with its fix: as a vector in - the shared fixture when it reduces to one value pair, else as a replayable - codec sequence with its config. -- **Equality** is over the JSON value domain (what serializes), not arbitrary - host-language values. -- **Same PR for both languages** so the two generators stay mirrored. -- **Open: library.** Suggested: fast-check (JS) and proptest (Rust), which - shrink failures and print a replay seed. Neither is in the repo yet; - settle it when implementing. - -## Related - -- [Window codec property tests](/quest/m2/json-window-properties.md) - the same treatment for the sliding-window codec diff --git a/quest/m2/json-window-properties.md b/quest/m2/json-window-properties.md index 811ab8d7735..a07fc490539 100644 --- a/quest/m2/json-window-properties.md +++ b/quest/m2/json-window-properties.md @@ -14,7 +14,7 @@ to the codec itself unless the test finds a bug. ## Plan Decided 2026-10-14, alongside -[Snapshot codec property tests](/quest/m2/json-snapshot-properties.md), +[Snapshot codec property tests](/test/json-properties.md), which carries the reasoning for each choice: - **Codec sequences** through `Encoder` -> `Decoder`, per implementation, at @@ -37,4 +37,4 @@ which carries the reasoning for each choice: ## Related -- [Snapshot codec property tests](/quest/m2/json-snapshot-properties.md) - the same treatment for the Snapshot codec +- [Snapshot codec property tests](/test/json-properties.md) - the same treatment for the Snapshot codec diff --git a/rs/moq-json/Cargo.toml b/rs/moq-json/Cargo.toml index 5a7e90edea0..8c12248aa56 100644 --- a/rs/moq-json/Cargo.toml +++ b/rs/moq-json/Cargo.toml @@ -26,6 +26,7 @@ tracing = { workspace = true } [dev-dependencies] criterion = { workspace = true } json-patch = "4" +proptest = { version = "1.12.0", default-features = false, features = ["std", "handle-panics"] } [[bench]] name = "codec" diff --git a/rs/moq-json/src/snapshot/encoder.rs b/rs/moq-json/src/snapshot/encoder.rs index ac23d567cae..c2acbb4b577 100644 --- a/rs/moq-json/src/snapshot/encoder.rs +++ b/rs/moq-json/src/snapshot/encoder.rs @@ -468,6 +468,144 @@ mod test { use super::*; use serde_json::json; + // Mirror the JS generator: bounded JSON trees and edits to a persistent object. + fn property_key() -> impl proptest::strategy::Strategy { + use proptest::prelude::*; + prop_oneof![ + proptest::sample::select(vec![ + "a", + "b", + "c", + "", + "__proto__", + "constructor", + "prototype", + "\0", + "雪" + ]) + .prop_map(str::to_owned), + "[ -~]{0,8}", + ] + } + + fn property_value() -> proptest::strategy::BoxedStrategy { + use proptest::prelude::*; + let leaf = prop_oneof![ + Just(Value::Null), + any::().prop_map(Value::Bool), + (-1_000_000i32..=1_000_000).prop_map(|n| Value::from(f64::from(n) / 4.0)), + prop_oneof!["[ -~]{0,16}", Just("雪\0".to_owned())].prop_map(Value::String), + ]; + leaf.prop_recursive(4, 64, 4, |inner| { + prop_oneof![ + proptest::collection::vec(inner.clone(), 0..5).prop_map(Value::Array), + proptest::collection::btree_map(property_key(), inner, 0..5) + .prop_map(|map| Value::Object(map.into_iter().collect())), + ] + }) + .boxed() + } + + #[derive(Clone, Debug)] + struct PropertyStep { + value: Value, + reset: bool, + } + + fn property_sequence() -> proptest::strategy::BoxedStrategy> { + use proptest::prelude::*; + let roots = proptest::collection::vec((property_value(), 0u8..16), 1..33).prop_map(|steps| { + steps + .into_iter() + .map(|(value, reset)| PropertyStep { + value, + reset: reset == 0, + }) + .collect() + }); + let edits = proptest::collection::vec((property_key(), property_value(), any::(), 0u8..16), 1..33) + .prop_map(|steps| { + let mut current = serde_json::Map::new(); + steps + .into_iter() + .map(|(key, value, remove, reset)| { + if remove { + current.remove(&key); + } else { + current.insert(key, value); + } + PropertyStep { + value: Value::Object(current.clone()), + reset: reset == 0, + } + }) + .collect() + }); + prop_oneof![roots, edits].boxed() + } + + #[test] + fn snapshot_codec_property_reconstructs_each_json_value() { + use proptest::prelude::*; + use proptest::test_runner::{Config as PropertyConfig, RngSeed, TestRunner}; + let setting = |name: &str, fallback: u32, min: u32| { + let value = std::env::var(name).map_or(fallback, |value| { + value.parse::().expect("integer property setting") + }); + assert!( + (min..=i32::MAX as u32).contains(&value), + "{name} outside supported range" + ); + value + }; + let seed = setting("MOQ_JSON_SEED", 0x6d6f71, 0); + let cases = setting("MOQ_JSON_CASES", 256, 1); + eprintln!("MOQ_JSON_SEED={seed} MOQ_JSON_CASES={cases}"); + let mut runner = TestRunner::new(PropertyConfig { + cases, + rng_seed: RngSeed::Fixed(u64::from(seed)), + failure_persistence: None, + ..Default::default() + }); + let sequence = ( + proptest::sample::select(vec![0, 1, 2, 8, 100]), + any::(), + property_sequence(), + ); + runner + .run(&sequence, |(delta_ratio, deflate, steps)| { + let compression = if deflate { + Compression::Deflate + } else { + Compression::None + }; + let mut encoder = Encoder::::new(Config { + delta_ratio, + compression, + }); + let mut decoder = super::super::Decoder::::new(super::super::consumer::Config { + compression, + ..Default::default() + }); + for (index, step) in steps.iter().enumerate() { + if step.reset { + encoder.reset(); + } + if let Some(frame) = encoder.update(&step.value).unwrap() { + if frame.keyframe { + decoder.snapshot(&frame.payload).unwrap(); + } else { + decoder.delta(&frame.payload).unwrap(); + } + frame.commit(); + } + prop_assert_eq!(decoder.decode().unwrap(), Some(step.value.clone()), "step {}", index); + } + Ok(()) + }) + .unwrap(); + } + #[test] fn duplicate_serialized_keys_are_refused() { use serde::ser::SerializeMap; diff --git a/rs/moq-json/tests/vectors.json b/rs/moq-json/tests/vectors.json index cf34be509d3..771bc186466 100644 --- a/rs/moq-json/tests/vectors.json +++ b/rs/moq-json/tests/vectors.json @@ -97,5 +97,33 @@ "new": { "a": [null, { "x": null }] }, "forced": false, "patch": { "a": [null, { "x": null }] } + }, + { + "name": "removed_prototype_named_key", + "old": { "__proto__": 0 }, + "new": {}, + "forced": false, + "patch": { "__proto__": null } + }, + { + "name": "added_prototype_named_key", + "old": {}, + "new": { "__proto__": { "x": 1 } }, + "forced": false, + "patch": { "__proto__": { "x": 1 } } + }, + { + "name": "nested_prototype_named_key", + "old": { "o": {} }, + "new": { "o": { "__proto__": 1 } }, + "forced": false, + "patch": { "o": { "__proto__": 1 } } + }, + { + "name": "prototype_named_key_is_not_inherited_equality", + "old": { "__proto__": {} }, + "new": { "a": {} }, + "forced": false, + "patch": { "__proto__": null, "a": {} } } ] diff --git a/test/README.md b/test/README.md index fb6a4e1dcd6..1edb365c5cd 100644 --- a/test/README.md +++ b/test/README.md @@ -12,6 +12,7 @@ tests live in each language's own justfile. | [audio-quality](audio-quality/README.md) | `just test audio-quality` | browser audio playout stays within budget over an impaired path | | [drain](drain/README.md) | `just test drain` | a JS viewer migrates off a draining relay without a dropped group | | video | `just test video` | the `@moq/video` renderer paints through WebGPU and falls back to Canvas2D after losing the GPU | +| [json-properties](json-properties.md) | `just test json-properties` | both Snapshot codecs reconstruct random JSON sequences with replayable seeds | The first five stand up a `moq-relay` and clients, so two of them running at once, or the same one running from two worktrees, would otherwise collide. `lib/harness.sh` diff --git a/test/json-properties.md b/test/json-properties.md new file mode 100644 index 00000000000..582d6a412d4 --- /dev/null +++ b/test/json-properties.md @@ -0,0 +1,30 @@ +# JSON snapshot properties + +The Rust `moq-json` and JS `@moq/json` unit suites each run 256 Snapshot codec +sequences with the fixed seed `7171953`. They use maintained shrinking +libraries, proptest and fast-check. Both generate bounded JSON trees and +persistent-object edits, including nested nulls, empty objects, added and +removed keys, type changes, arrays, non-object roots, and prototype-named keys. +They route every emitted frame through the Decoder, commit it, and compare the +reconstructed JSON after every update, including updates that emit nothing. +Each sequence varies the delta ratio, compression, and explicit encoder resets. + +`just test json-properties` runs the two properties with 4096 cases and a fresh +seed, prints that seed, then sweeps JS codec cost over document size and changed +keys, with and without compression. Nightly runs this recipe. Replay a failure +with its printed settings: + +```sh +MOQ_JSON_SEED=7171953 MOQ_JSON_CASES=4096 just test json-properties +``` + +Either setting can also be passed to the ordinary per-language unit suite. +Seed values are integers from 0 through 2147483647; case counts are positive +integers in the same range. The libraries print the shrunk input, including its +configuration, and fast-check also prints a replay path. Matching seeds do not +mean matching generated sequences across the languages. + +A failure's fix keeps the minimized case: use the shared +`rs/moq-json/tests/vectors.json` when it reduces to a value pair; otherwise add a +codec sequence regression with its compression, delta ratio, and reset steps. +Do not replace the failing seed or weaken the property. diff --git a/test/json-properties.sh b/test/json-properties.sh new file mode 100644 index 00000000000..886033c90c6 --- /dev/null +++ b/test/json-properties.sh @@ -0,0 +1,14 @@ +#!/usr/bin/env bash +# Nightly explores fresh sequences; printed settings replay either implementation. +set -euo pipefail +cd "$(git rev-parse --show-toplevel)" + +MOQ_JSON_SEED=${MOQ_JSON_SEED:-$(python3 -c 'import secrets; print(secrets.randbits(31))')} +MOQ_JSON_CASES=${MOQ_JSON_CASES:-4096} +export MOQ_JSON_SEED MOQ_JSON_CASES +echo "JSON snapshot properties: MOQ_JSON_SEED=$MOQ_JSON_SEED MOQ_JSON_CASES=$MOQ_JSON_CASES" +bun install --frozen-lockfile +just rs test -p moq-json --lib -E 'test(snapshot_codec_property)' +bun test js/json/src/snapshot/properties.test.ts +bunx tsc --noEmit --project js/json/bench/tsconfig.json +bun js/json/bench/codec.ts diff --git a/test/justfile b/test/justfile index ca585fb6ae3..2260d37daf6 100644 --- a/test/justfile +++ b/test/justfile @@ -180,3 +180,7 @@ max-age: # Cross-language subscription termination over in-memory transports. bare-fin: just rs test -p moq-net --lib bare_fin_interop --run-ignored all + +# Explore JSON snapshot codec sequences with a fresh replayable seed. +json-properties: + bash json-properties.sh