|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#23036] A page component's `visibleWhen` has no `data` root, and neither of |
| 5 | + * its two prose faces may say the shipping renderer mounts one. |
| 6 | + * |
| 7 | + * ## The fact being pinned |
| 8 | + * |
| 9 | + * objectui#9308 ruling B (2026-09-13) removed `data` as a runtime expression |
| 10 | + * root: `SchemaRenderer` no longer publishes its injected data source as |
| 11 | + * `data`, and the one scope channel left (`PredicateScopeProvider`) is fed in |
| 12 | + * the shipping console only by app-shell's `ExpressionProvider`, which |
| 13 | + * publishes `current_user`, `user`, `ctx.user`, `os.user` and `features`. So a |
| 14 | + * `data.*` read in this predicate faults, and this surface answers a faulting |
| 15 | + * predicate by rendering the component: `data.total > 0` and `data.total == 0` |
| 16 | + * both show it. The `.describe()` said the renderer "binds `data` to the |
| 17 | + * data-source ADAPTER here" — a gate written from it never gates, and nothing |
| 18 | + * refuses it. #17203 corrected the same sentence for `app` |
| 19 | + * (`expression-scope-app-root.pin.test.ts`); this file holds the `data` half. |
| 20 | + * |
| 21 | + * ## What is pinned: the claim, not the wording |
| 22 | + * |
| 23 | + * 1. The mounted-roots sentence names exactly `features` and `os.user`, on both |
| 24 | + * faces (the published `.describe()` and the TSDoc an AI author reads). |
| 25 | + * 2. On the published face, every sentence that names `data` says it is NOT |
| 26 | + * mounted here, ties it to the PLANNED API binding, or is about the |
| 27 | + * `page:tabs` item evaluator — the three readings that are true today. |
| 28 | + * 3. The two texts in this file agree. The `visibleWhen` face names the |
| 29 | + * PLANNED API binding as the only `data` carrier, which holds exactly while |
| 30 | + * `ElementApiDataSourceSchema`'s own describe still carries |
| 31 | + * `[PLANNED — not yet rendered]`. When that binding's renderer ships and its |
| 32 | + * describe drops the marker, this pin goes red so the `visibleWhen` face is |
| 33 | + * rewritten in the same change, not left behind. |
| 34 | + * |
| 35 | + * Rewording any of these sentences is free; a sentence that tells an author |
| 36 | + * the renderer binds `data` on this surface is not. |
| 37 | + */ |
| 38 | + |
| 39 | +import fs from 'node:fs'; |
| 40 | +import path from 'node:path'; |
| 41 | +import url from 'node:url'; |
| 42 | + |
| 43 | +import { describe, it, expect } from 'vitest'; |
| 44 | + |
| 45 | +import { ElementApiDataSourceSchema, PageComponentSchema } from './page.zod'; |
| 46 | + |
| 47 | +const HERE = path.dirname(url.fileURLToPath(import.meta.url)); |
| 48 | +const pageSource = fs.readFileSync(path.resolve(HERE, 'page.zod.ts'), 'utf8'); |
| 49 | + |
| 50 | +/** |
| 51 | + * The published description of `PageComponentSchema.visibleWhen`. |
| 52 | + * |
| 53 | + * ⚠️ NOT `.shape` — ADR-0089 D3a made this schema a `.strict().transform(…)` |
| 54 | + * pipe, so the object carrying the property descriptions is its INPUT side. |
| 55 | + */ |
| 56 | +const visibleWhenDescription = (): string => { |
| 57 | + const shape = (PageComponentSchema as unknown as { |
| 58 | + def: { in: { shape: Record<string, { description?: string }> } }; |
| 59 | + }).def.in.shape; |
| 60 | + const d = shape.visibleWhen.description; |
| 61 | + expect(d, 'the describe() must exist — this pin is about its content').toBeTruthy(); |
| 62 | + return d!; |
| 63 | +}; |
| 64 | + |
| 65 | +/** |
| 66 | + * The TSDoc block directly above the `visibleWhen:` property, with the comment |
| 67 | + * gutter stripped and its paragraphs (headings included) kept apart by a blank |
| 68 | + * line. Anchored on its first line and on the property itself, so a block that |
| 69 | + * moves or loses either fails here instead of asserting over nothing. |
| 70 | + */ |
| 71 | +const visibleWhenDocblock = (): string => { |
| 72 | + const start = pageSource.indexOf('Conditional-visibility predicate (CEL) — the component is rendered only when'); |
| 73 | + const end = pageSource.indexOf('visibleWhen: EvaluatedExpressionInputSchema', start); |
| 74 | + expect(start, 'docblock anchor not found — the pin has drifted off its site').toBeGreaterThan(-1); |
| 75 | + expect(end, 'property anchor not found after the docblock').toBeGreaterThan(start); |
| 76 | + return pageSource.slice(start, end) |
| 77 | + .split('\n') |
| 78 | + .map((line) => line.replace(/^\s*\*\/?\s?/, '').trimEnd()) |
| 79 | + .join('\n') |
| 80 | + .split(/\n\s*\n/) |
| 81 | + .map((paragraph) => paragraph.replace(/\s*\n\s*/g, ' ').trim()) |
| 82 | + .filter(Boolean) |
| 83 | + .join('\n\n'); |
| 84 | +}; |
| 85 | + |
| 86 | +/** |
| 87 | + * Sentences of a prose face. A paragraph break ends one; a period, `!` or `?` |
| 88 | + * ends one only when white space and a capital, a backtick or `⚠` follow — so |
| 89 | + * dotted tokens (`os.user`, `page.<var>`, `data.*`) and "e.g." stay inside |
| 90 | + * their sentence. |
| 91 | + */ |
| 92 | +const sentences = (text: string): string[] => |
| 93 | + text.split(/\n\n|(?<=[.!?])\s+(?=[A-Z`⚠])/).map((s) => s.trim()).filter(Boolean); |
| 94 | + |
| 95 | +/** The sentence a mounted-roots claim lives in, located by its anchor. */ |
| 96 | +const sentenceAt = (text: string, anchor: string): string => { |
| 97 | + const hit = sentences(text).find((s) => s.includes(anchor)); |
| 98 | + expect(hit, `anchor not found — the pin has drifted off its site: ${anchor}`).toBeTruthy(); |
| 99 | + return hit!; |
| 100 | +}; |
| 101 | + |
| 102 | +/** Every backticked token in a sentence: the roots it names. */ |
| 103 | +const backticked = (s: string): string[] => [...s.matchAll(/`([^`]+)`/g)].map((m) => m[1]); |
| 104 | + |
| 105 | +/** |
| 106 | + * The published-face rule (2): sentences naming `data` that are none of the |
| 107 | + * three true readings. Returned rather than asserted, so the controls below can |
| 108 | + * run it on a text that MUST fail. |
| 109 | + */ |
| 110 | +const dataClaimsOutsideTheTrueReadings = (text: string): string[] => |
| 111 | + sentences(text) |
| 112 | + .filter((s) => s.includes('`data`')) |
| 113 | + .filter((s) => !/\bNO `data`/.test(s) && !s.includes('PLANNED') && !s.includes('`page:tabs`')); |
| 114 | + |
| 115 | +/** The sentence shapes the retired claim was written in, on either face. */ |
| 116 | +const RETIRED_CLAIM_SHAPES = [/binds `data` to the data-source/i, /`data` is the data-source/i] as const; |
| 117 | + |
| 118 | +const MOUNTED_ROOTS = ['features', 'os.user']; |
| 119 | + |
| 120 | +describe('a page component `visibleWhen` names no `data` root — the shipping renderer mounts none', () => { |
| 121 | + describe('published face — `PageComponentSchema.visibleWhen`.describe()', () => { |
| 122 | + it('the mounted-roots sentence names exactly `features` and `os.user`', () => { |
| 123 | + const s = sentenceAt(visibleWhenDescription(), 'additionally mounts'); |
| 124 | + expect(backticked(s)).toEqual(MOUNTED_ROOTS); |
| 125 | + expect(s).toContain('NOT contract-guaranteed'); |
| 126 | + }); |
| 127 | + |
| 128 | + it('states the absence, and no sentence claims `data` is bound here', () => { |
| 129 | + const d = visibleWhenDescription(); |
| 130 | + expect(d).toContain('mounts NO `data` root'); |
| 131 | + expect(dataClaimsOutsideTheTrueReadings(d)).toEqual([]); |
| 132 | + for (const shape of RETIRED_CLAIM_SHAPES) expect(d).not.toMatch(shape); |
| 133 | + }); |
| 134 | + |
| 135 | + it('keeps the `page:tabs` item reading, which is still true: that evaluator binds `data` to the row', () => { |
| 136 | + const s = sentenceAt(visibleWhenDescription(), '`page:tabs`'); |
| 137 | + expect(s).toContain('`data`'); |
| 138 | + expect(s).toContain('ROW'); |
| 139 | + }); |
| 140 | + |
| 141 | + it('keeps the contract-bound roots, a different clause this card does not touch', () => { |
| 142 | + const s = sentenceAt(visibleWhenDescription(), 'Contract-bound roots'); |
| 143 | + for (const root of ['record', 'current_user', 'user', 'ctx.user']) expect(backticked(s)).toContain(root); |
| 144 | + }); |
| 145 | + }); |
| 146 | + |
| 147 | + describe('TSDoc face — the docblock above `visibleWhen` (no generator reads it; an AI author opening the file does)', () => { |
| 148 | + it('the "Ambient roots" sentence names exactly `features` and `os.user`', () => { |
| 149 | + const s = sentenceAt(visibleWhenDocblock(), 'The shipping renderer additionally mounts'); |
| 150 | + expect(backticked(s)).toEqual([...MOUNTED_ROOTS, 'ExpressionProvider']); |
| 151 | + }); |
| 152 | + |
| 153 | + it('states the absence, and carries neither shape the retired claim was written in', () => { |
| 154 | + const doc = visibleWhenDocblock(); |
| 155 | + expect(doc).toContain('mounts NO `data` root'); |
| 156 | + for (const shape of RETIRED_CLAIM_SHAPES) expect(doc).not.toMatch(shape); |
| 157 | + }); |
| 158 | + }); |
| 159 | + |
| 160 | + describe('the two texts in this file agree — the PLANNED API binding is the only `data` carrier', () => { |
| 161 | + it('`visibleWhen` names the PLANNED API binding as where `data` is declared', () => { |
| 162 | + const s = sentences(visibleWhenDescription()).find((x) => x.includes('PLANNED')); |
| 163 | + expect(s, 'the describe no longer names the planned binding').toBeTruthy(); |
| 164 | + expect(s).toContain('`data`'); |
| 165 | + expect(s).toContain("provider: 'api'"); |
| 166 | + }); |
| 167 | + |
| 168 | + it('…which holds only while the API binding is still PLANNED and still declares a `data` read in `visibleWhen`', () => { |
| 169 | + const api = ElementApiDataSourceSchema.description ?? ''; |
| 170 | + expect( |
| 171 | + api.startsWith('[PLANNED — not yet rendered]'), |
| 172 | + 'The API binding is no longer marked PLANNED, so its renderer has shipped: on an element carrying ' |
| 173 | + + 'that binding `data` is now mounted. Rewrite `PageComponentSchema.visibleWhen`\'s `data` sentence ' |
| 174 | + + '(describe and docblock) in the same change, then this assertion.', |
| 175 | + ).toBe(true); |
| 176 | + expect(api).toContain('`data`'); |
| 177 | + expect(api).toContain('`visibleWhen`'); |
| 178 | + }); |
| 179 | + }); |
| 180 | + |
| 181 | + describe('probe controls — a pass above is only a reading if these hold', () => { |
| 182 | + /** |
| 183 | + * DARK. The describe sentence this card replaced, verbatim. Rule (2) and |
| 184 | + * the retired-shape list must both catch it, or their `toEqual([])` / |
| 185 | + * `not.toMatch` arms above could be passing on a matcher that matches |
| 186 | + * nothing. |
| 187 | + */ |
| 188 | + it('DARK: the replaced describe sentence fails rule (2) and the retired-shape list', () => { |
| 189 | + const replaced = 'Contract-bound roots: `record`, `current_user`, and page state as `page.<var>`. ' |
| 190 | + + 'The shipping renderer additionally mounts `features`, `os.user` and binds `data` to the data-source ' |
| 191 | + + 'ADAPTER here — renderer behaviour, NOT contract-guaranteed (ADR-0068 rules the user object only). ' |
| 192 | + + '⚠️ `data` is surface-dependent: on a `page:tabs` item `visibleWhen` it is the record ROW instead.'; |
| 193 | + expect(dataClaimsOutsideTheTrueReadings(replaced)).toHaveLength(1); |
| 194 | + expect(backticked(sentenceAt(replaced, 'additionally mounts'))).toContain('data'); |
| 195 | + expect(RETIRED_CLAIM_SHAPES.some((shape) => shape.test(replaced))).toBe(true); |
| 196 | + }); |
| 197 | + |
| 198 | + /** DARK. The sentence splitter keeps dotted tokens and "e.g." inside one sentence. */ |
| 199 | + it('DARK: dotted tokens do not split a sentence', () => { |
| 200 | + expect(sentences('Mounts `os.user` and `page.<var>`; e.g. "page.x != \'\'". Next one.')).toHaveLength(2); |
| 201 | + }); |
| 202 | + |
| 203 | + /** DARK. A site renamed out from under this pin fails loudly, never vacuously. */ |
| 204 | + it('DARK: a missing anchor fails the pin rather than passing over nothing', () => { |
| 205 | + expect(() => sentenceAt(visibleWhenDescription(), 'no such anchor exists in this describe')).toThrow(); |
| 206 | + }); |
| 207 | + }); |
| 208 | +}); |
0 commit comments