|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * #5056 — the controls the door measurement itself owes. |
| 5 | + * |
| 6 | + * `door-reachability.testkit.ts` decides whether a #4001 batch tightens a shape |
| 7 | + * or reclassifies it as `no door`. Its consumers (`chart.test.ts`, |
| 8 | + * `widget.test.ts`, `i18n.test.ts`) each carry the three controls for THEIR |
| 9 | + * shapes; nothing carried the controls for the instrument. This file does, so |
| 10 | + * that the walker's own limbs — the `.describe()` premise the bridge rests on, |
| 11 | + * the derived-clone bridge, and the overlap threshold that bounds it — cannot |
| 12 | + * rot behind green consumer tests. |
| 13 | + * |
| 14 | + * Every number asserted here was measured on this build, not reasoned about. |
| 15 | + */ |
| 16 | + |
| 17 | +import { describe, it, expect } from 'vitest'; |
| 18 | +import { z } from 'zod'; |
| 19 | +import { measureDoors } from './door-reachability.testkit'; |
| 20 | +import { PageSchema } from './page.zod'; |
| 21 | +import { ObjectListViewSchema } from './view.zod'; |
| 22 | +import { WidgetManifestSchema } from './widget.zod'; |
| 23 | +import { I18nLabelSchema } from './i18n.zod'; |
| 24 | +import { SnakeCaseIdentifierSchema } from '../shared/identifiers.zod'; |
| 25 | + |
| 26 | +/** The walker's identity key, mirrored here so the premise tests can assert on it. */ |
| 27 | +const defOf = (s: unknown): unknown => (s as { _zod?: { def?: unknown } })?._zod?.def; |
| 28 | + |
| 29 | +// ============================================================================ |
| 30 | +// 1. The PREMISE the derived-clone bridge rests on. |
| 31 | +// |
| 32 | +// The bridge exists because zod builders return a clone that shares the base's |
| 33 | +// per-property schema INSTANCES. Which builders share the `_zod.def` OBJECT |
| 34 | +// itself, and which do not, is what decides whether the bridge sees a real |
| 35 | +// derivation or a coincidence. A zod upgrade that changes `clone()` semantics |
| 36 | +// changes the bridge's meaning, and that must be LOUD rather than silent. |
| 37 | +// ============================================================================ |
| 38 | +describe('#5056 premise — which zod builders share the `_zod.def` object', () => { |
| 39 | + it('`.describe()` shares the def of the instance it was called on', () => { |
| 40 | + // THE mechanism behind #5056. `clone(inst)` without an explicit def reuses |
| 41 | + // `inst._zod.def`, so a described clone is def-IDENTICAL to its receiver. |
| 42 | + const base = z.string(); |
| 43 | + expect(defOf(base.describe('x')), '.describe() clone vs its receiver').toBe(defOf(base)); |
| 44 | + expect(defOf(base.describe('x')), 'two different descriptions of ONE instance').toBe(defOf(base.describe('y'))); |
| 45 | + }); |
| 46 | + |
| 47 | + it('but two independently constructed schemas do NOT share a def', () => { |
| 48 | + // #5056's issue body spells the fact as |
| 49 | + // `defOf(z.string().describe('x')) === defOf(z.string())` → true |
| 50 | + // and that spelling is FALSE on this build: two separate `z.string()` calls |
| 51 | + // build two separate defs, so there is nothing to share. Measured, not |
| 52 | + // assumed — the corrected statement is the one above: sharing follows the |
| 53 | + // RECEIVER INSTANCE, not the shape. That distinction is the whole reason |
| 54 | + // the bug bites this repo: the spec funnels dozens of shapes through a |
| 55 | + // handful of SHARED leaf instances, and every `.describe()` of one of them |
| 56 | + // is def-identical to every other. |
| 57 | + expect(defOf(z.string().describe('x'))).not.toBe(defOf(z.string())); |
| 58 | + expect(defOf(z.string())).not.toBe(defOf(z.string())); |
| 59 | + }); |
| 60 | + |
| 61 | + it('the repo\'s shared leaves are therefore def-identical across every site that describes them', () => { |
| 62 | + // `SnakeCaseIdentifierSchema` and `I18nLabelSchema` are the two the campaign |
| 63 | + // actually tripped over: `name:` and `label:` are on nearly every authorable |
| 64 | + // shape in this repo, described in place at each site. |
| 65 | + expect(defOf(SnakeCaseIdentifierSchema.describe('a'))).toBe(defOf(SnakeCaseIdentifierSchema.describe('b'))); |
| 66 | + expect(defOf(I18nLabelSchema.describe('a'))).toBe(defOf(I18nLabelSchema.describe('b'))); |
| 67 | + }); |
| 68 | + |
| 69 | + it('`.optional()` / `.extend()` / `.strip()` each build a NEW def', () => { |
| 70 | + // The other half of the premise: these are the builders whose clones the |
| 71 | + // bridge must still recognise, and they can only be recognised through |
| 72 | + // shared PROPERTY instances, because the def itself is new. |
| 73 | + const base = z.string(); |
| 74 | + expect(defOf(base.optional())).not.toBe(defOf(base)); |
| 75 | + const obj = z.object({ a: z.string() }); |
| 76 | + expect(defOf(obj.extend({ b: z.number() }))).not.toBe(defOf(obj)); |
| 77 | + expect(defOf(obj.strip())).not.toBe(defOf(obj)); |
| 78 | + }); |
| 79 | +}); |
| 80 | + |
| 81 | +// ============================================================================ |
| 82 | +// 2. The three controls, on the instrument itself. |
| 83 | +// ============================================================================ |
| 84 | +describe('#5056 controls — the walker finds doors, and only real ones', () => { |
| 85 | + it('positive: live authoring roots resolve, and the graph is really walked', () => { |
| 86 | + const { verdict, nodeCount, rootCount } = measureDoors(); |
| 87 | + expect(rootCount, 'every metadata type plus ObjectStackSchema').toBeGreaterThan(20); |
| 88 | + expect(nodeCount, 'the graph must actually have been walked').toBeGreaterThan(1000); |
| 89 | + expect(verdict(PageSchema), 'positive control').toBe('direct'); |
| 90 | + expect(verdict(ObjectListViewSchema), 'positive control').toBe('direct'); |
| 91 | + }); |
| 92 | + |
| 93 | + it('positive: a GENUINE derived clone is recognised — this is the bridge limb', () => { |
| 94 | + // The limb #5056 narrowed. Nothing else in the repo exercises it: every |
| 95 | + // shape the consumers assert as reachable measures `direct`, so a bridge |
| 96 | + // that had been narrowed all the way to "never fires" would leave all |
| 97 | + // three consumer files green. `.extend()` and `.strip()` are exactly the |
| 98 | + // builders the bridge was written for. |
| 99 | + const { verdict, cloneOverlap } = measureDoors(); |
| 100 | + |
| 101 | + const extended = PageSchema.extend({ osDoorProbeExtra: z.string() }); |
| 102 | + expect(verdict(extended), 'PageSchema.extend(...) is a real derivation').toBe('derived-clone'); |
| 103 | + expect(cloneOverlap(extended), 'one added key out of 25 kept').toBeGreaterThan(0.9); |
| 104 | + |
| 105 | + const stripped = ObjectListViewSchema.strip(); |
| 106 | + expect(verdict(stripped), 'ObjectListViewSchema.strip() is a real derivation').toBe('derived-clone'); |
| 107 | + expect(cloneOverlap(stripped), 'strip() keeps the whole shape').toBe(1); |
| 108 | + }); |
| 109 | + |
| 110 | + it('negative: a look-alike that shares no property instance stays out', () => { |
| 111 | + // #5056's own negative control, verbatim: deliberately spelled to look like |
| 112 | + // every authorable shape in the repo, but built from fresh `z.string()` |
| 113 | + // instances, so it shares nothing with the graph. |
| 114 | + const { verdict, cloneOverlap } = measureDoors(); |
| 115 | + const lookAlike = z.object({ name: z.string(), label: z.string() }); |
| 116 | + expect(verdict(lookAlike), 'negative control').toBe('unreachable'); |
| 117 | + expect(cloneOverlap(lookAlike)).toBe(0); |
| 118 | + }); |
| 119 | + |
| 120 | + it('flip: injecting a synthetic carrier turns an unreachable shape reachable', () => { |
| 121 | + // Without this, "unreachable" and "the walker is broken" are the same |
| 122 | + // output. `extraRoots` exists for exactly this control. |
| 123 | + const orphan = z.object({ osDoorProbeOrphanKey: z.string() }); |
| 124 | + expect(measureDoors().verdict(orphan), 'before').toBe('unreachable'); |
| 125 | + const carrier = z.object({ orphan }); |
| 126 | + expect(measureDoors([carrier]).verdict(orphan), 'after — a carrier makes the door').toBe('direct'); |
| 127 | + }); |
| 128 | +}); |
| 129 | + |
| 130 | +// ============================================================================ |
| 131 | +// 3. The #5056 regression boundary itself. |
| 132 | +// ============================================================================ |
| 133 | +describe('#5056 regression — the any-one-shared-property bridge stays dead', () => { |
| 134 | + it('WidgetManifestSchema shares leaves with the live graph but is NOT derived from it', () => { |
| 135 | + // The reverse verification, standing rather than one-shot. |
| 136 | + // |
| 137 | + // The OLD bridge fired when ANY one property of the candidate was def-equal |
| 138 | + // to a same-named property of ANY visited object node. `cloneOverlap` is |
| 139 | + // the max shared-property count over visited shapes, normalised — so |
| 140 | + // `cloneOverlap(x) > 0` is EXACTLY the condition under which the old bridge |
| 141 | + // fired. Asserting both halves here pins the fix without keeping a second, |
| 142 | + // defective copy of the walker alive to demonstrate it: |
| 143 | + // |
| 144 | + // > 0 ⇒ the old bridge WOULD have called this file reachable, and did |
| 145 | + // < 0.5 ⇒ the fixed bridge correctly does not. |
| 146 | + // |
| 147 | + // 2 shared keys (`name`, `label`, both described shared LEAVES) out of 19. |
| 148 | + // A coincidence, not a derivation — and a false door here would have spent |
| 149 | + // a v17 breaking to tighten a file nothing imports (#4583's "precisely |
| 150 | + // validated dead slot"). |
| 151 | + const { verdict, cloneOverlap } = measureDoors(); |
| 152 | + const overlap = cloneOverlap(WidgetManifestSchema); |
| 153 | + expect(overlap, 'it DOES share leaves — the old bridge fired on exactly this').toBeGreaterThan(0); |
| 154 | + expect(overlap, 'but nothing structural: measured 2/19').toBeLessThan(0.2); |
| 155 | + expect(verdict(WidgetManifestSchema)).toBe('unreachable'); |
| 156 | + }); |
| 157 | + |
| 158 | + it('the threshold discriminates by SHARE OF SHAPE, not by count of shared keys', () => { |
| 159 | + // Why 0.5 and not "at least 2 shared keys": the same two shared leaves |
| 160 | + // decide opposite verdicts depending on how much of the shape they are. |
| 161 | + const { cloneOverlap } = measureDoors(); |
| 162 | + const shared = { |
| 163 | + name: SnakeCaseIdentifierSchema.describe('Machine name'), |
| 164 | + label: I18nLabelSchema.describe('Display label'), |
| 165 | + }; |
| 166 | + const withFiller = z.object({ ...shared, a: z.string(), b: z.string(), c: z.string() }); |
| 167 | + expect(cloneOverlap(withFiller), '2 shared of 5 keys').toBeCloseTo(0.4, 5); |
| 168 | + }); |
| 169 | + |
| 170 | + it('KNOWN BOUND — a shape made ENTIRELY of shared leaves still bridges, whatever the threshold', () => { |
| 171 | + // Measured limitation, pinned deliberately rather than left latent for a |
| 172 | + // later batch to rediscover as a second false door. |
| 173 | + // |
| 174 | + // The ratio is taken over the CANDIDATE's own keys, so a 2-key shape whose |
| 175 | + // both keys are shared leaves scores 1.0 and is judged `derived-clone` — |
| 176 | + // no threshold in (0, 1] excludes it, because a genuine `.strip()` of a |
| 177 | + // live 2-key schema scores 1.0 too and must stay reachable. The instrument |
| 178 | + // cannot separate those two by overlap alone. |
| 179 | + // |
| 180 | + // It costs nothing today: this is a synthetic shape, and the smallest real |
| 181 | + // `no door` shapes the campaign has measured sit far below the threshold |
| 182 | + // (WidgetManifestSchema at 2/19). It becomes a real hazard the moment a |
| 183 | + // batch measures a SMALL shape (roughly 4 keys or fewer) whose keys are all |
| 184 | + // shared leaves — measure `cloneOverlap` and read this pin before trusting |
| 185 | + // a `derived-clone` verdict on one. Filed for the campaign as #5828. |
| 186 | + const { verdict, cloneOverlap } = measureDoors(); |
| 187 | + const allSharedLeaves = z.object({ |
| 188 | + name: SnakeCaseIdentifierSchema.describe('Machine name'), |
| 189 | + label: I18nLabelSchema.describe('Display label'), |
| 190 | + }); |
| 191 | + expect(cloneOverlap(allSharedLeaves), '2 shared of 2 keys').toBe(1); |
| 192 | + expect(verdict(allSharedLeaves), 'the residual false-reachable case — see #5828').toBe('derived-clone'); |
| 193 | + }); |
| 194 | +}); |
0 commit comments