* This suite intentionally avoids re-testing generic JSON Patch behavior * (pointer parsing, add/replace/remove mechanics, ordering, etc). * * It focuses on Schema.toDifferJsonPatch-specific guarantees: * - reference preservation when patch is a no-op * - reference replacement for root replace
(codec: Schema.Codec<T, E>)
| 17 | */ |
| 18 | |
| 19 | function roundtrip<T, E>(codec: Schema.Codec<T, E>) { |
| 20 | const differ = Schema.toDifferJsonPatch(codec) |
| 21 | const arbitrary = Schema.toArbitrary(codec)(FastCheck) |
| 22 | const arb = arbitrary.filter((v) => { |
| 23 | // avoid prototype-poisoning-ish values that aren't valid JSON-ish containers for patching |
| 24 | if ( |
| 25 | typeof v === "object" && |
| 26 | v !== null && |
| 27 | (Object.getPrototypeOf(v) === null || Object.hasOwn(v as any, "__proto__")) |
| 28 | ) { |
| 29 | return false |
| 30 | } |
| 31 | return true |
| 32 | }) |
| 33 | |
| 34 | FastCheck.assert( |
| 35 | FastCheck.property(arb, arb, (v1, v2) => { |
| 36 | const patch = differ.diff(v1, v2) |
| 37 | const patched = differ.patch(v1, patch) |
| 38 | |
| 39 | // two invalid dates are not considered equal by deepStrictEqual |
| 40 | if (patched instanceof Date && v2 instanceof Date && Object.is(patched.getTime(), v2.getTime())) { |
| 41 | return |
| 42 | } |
| 43 | |
| 44 | deepStrictEqual(patched, v2) |
| 45 | }) |
| 46 | ) |
| 47 | } |
| 48 | |
| 49 | describe("Schema.toDifferJsonPatch", () => { |
| 50 | describe("structural guarantees", () => { |
no test coverage detected