From 320e225158a658f65e5e205d88606d02a239bfda Mon Sep 17 00:00:00 2001 From: cixzhang Date: Tue, 17 Nov 2026 01:14:58 -0800 Subject: [PATCH 1/3] test(core): an app's StyleX build of the published layer viewport inset defines every variable it reads (red) --- ...layerViewportInset.consumer-build.test.mjs | 136 ++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100644 packages/core/src/Layer/layerViewportInset.consumer-build.test.mjs diff --git a/packages/core/src/Layer/layerViewportInset.consumer-build.test.mjs b/packages/core/src/Layer/layerViewportInset.consumer-build.test.mjs new file mode 100644 index 0000000000000..c09e46384d168 --- /dev/null +++ b/packages/core/src/Layer/layerViewportInset.consumer-build.test.mjs @@ -0,0 +1,136 @@ +// Copyright (c) Meta Platforms, Inc. and affiliates. + +/** + * @file layerViewportInset.consumer-build.test.mjs + * @input Uses Babel, the StyleX compiler, Core's build config, and the + * published `./Layer/layerViewportInset.stylex` entry as Core builds it + * @output Verifies an app's own StyleX build that reads the layer viewport + * inset references only CSS variables Core's stylesheet defines + * @position Regression test for the published layer viewport inset entry + * (spec:AST-059 FR1, FR7) + */ + +import fs from 'node:fs/promises'; +import os from 'node:os'; +import path from 'node:path'; +import {fileURLToPath} from 'node:url'; +import {transformAsync} from '@babel/core'; +import stylexBabelPlugin from '@stylexjs/babel-plugin'; +import {afterAll, beforeAll, describe, expect, it} from 'vitest'; + +const __dirname = path.dirname(fileURLToPath(import.meta.url)); +const SRC = path.resolve(__dirname, '..'); +const CORE_ROOT = path.resolve(SRC, '..'); + +/** Core's sources whose compiled CSS defines what the entry may reference. */ +const CORE_SOURCES = [ + 'Layer/layerViewportInset.stylex.ts', + 'theme/tokens.stylex.ts', +]; + +/** @type {string} */ +let app; + +async function compileCore(relative) { + const filename = path.join(SRC, relative); + return transformAsync(await fs.readFile(filename, 'utf8'), { + babelrc: false, + configFile: path.join(CORE_ROOT, 'babel.config.json'), + filename, + }); +} + +beforeAll(async () => { + // An app with Core installed as it is published: the entry is the built + // file at the path the package's exports name. + app = await fs.realpath( + await fs.mkdtemp(path.join(os.tmpdir(), 'astryx-inset-consumer-')), + ); + const core = path.join(app, 'node_modules/@astryxdesign/core'); + await fs.mkdir(path.join(core, 'dist/Layer'), {recursive: true}); + await fs.writeFile( + path.join(core, 'package.json'), + JSON.stringify({name: '@astryxdesign/core'}), + ); + const built = await compileCore('Layer/layerViewportInset.stylex.ts'); + await fs.writeFile( + path.join(core, 'dist/Layer/layerViewportInset.stylex.js'), + built?.code ?? '', + ); + await fs.writeFile( + path.join(app, 'package.json'), + JSON.stringify({name: 'inset-consumer-app'}), + ); +}, 60_000); + +afterAll(async () => { + if (app) await fs.rm(app, {recursive: true, force: true}); +}); + +/** The CSS an app's own StyleX build emits for `source`. */ +async function compileApp(source) { + const filename = path.join(app, 'src/Sheet.js'); + await fs.mkdir(path.dirname(filename), {recursive: true}); + await fs.writeFile(filename, source); + const result = await transformAsync(source, { + babelrc: false, + configFile: false, + filename, + plugins: [ + [ + stylexBabelPlugin, + { + dev: false, + runtimeInjection: false, + unstable_moduleResolution: {type: 'commonJS', rootDir: app}, + // The published entry resolves through node_modules. + aliases: { + '@astryxdesign/core/Layer/layerViewportInset.stylex': [ + path.join( + app, + 'node_modules/@astryxdesign/core/dist/Layer/layerViewportInset.stylex.js', + ), + ], + }, + }, + ], + ], + }); + return stylexBabelPlugin.processStylexRules( + result?.metadata?.stylex ?? [], + false, + ); +} + +/** Every custom property Core's compiled stylesheet defines. */ +async function coreDefinitions() { + const rules = []; + for (const relative of CORE_SOURCES) { + rules.push(...((await compileCore(relative))?.metadata?.stylex ?? [])); + } + const css = stylexBabelPlugin.processStylexRules(rules, false); + return new Set([...css.matchAll(/(--[\w-]+)\s*:/g)].map(match => match[1])); +} + +describe('the published layer viewport inset in an app StyleX build (spec:AST-059 FR7)', () => { + it('references no CSS variable that Core leaves undefined', async () => { + const css = await compileApp(` + import * as stylex from '@stylexjs/stylex'; + import {layerViewportInset} from '@astryxdesign/core/Layer/layerViewportInset.stylex'; + export const styles = stylex.create({ + sheet: { + maxBlockSize: layerViewportInset.maxBlockSize, + maxInlineSize: layerViewportInset.maxInlineSize, + marginBlockStart: layerViewportInset.gutterBlockStart, + }, + }); + `); + const defined = await coreDefinitions(); + // A variable with no fallback that nothing defines makes the whole + // declaration invalid, so the app's sheet loses its cap. + const undefinedReferences = [...css.matchAll(/var\((--[\w-]+)\)/g)] + .map(match => match[1]) + .filter(name => !defined.has(name)); + expect(undefinedReferences).toEqual([]); + }); +}); From 17ef1e889f2d041b4498c3c59e2d61a4a6d0635c Mon Sep 17 00:00:00 2001 From: cixzhang Date: Tue, 17 Nov 2026 01:23:13 -0800 Subject: [PATCH 2/3] docs(layer-runtime): record that a StyleX build outside Core cannot read the layer viewport inset The INV11 gap: a StyleX build outside Core that imports the published layer viewport inset module, as a swizzled layer component does, gets an undefined variable, because the module's constants resolve only inside Core's own build. The consumer build test shows it as an expected failure. --- docs/architecture/layer-runtime.md | 9 ++++++ ...layerViewportInset.consumer-build.test.mjs | 30 +++++++++++-------- 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/docs/architecture/layer-runtime.md b/docs/architecture/layer-runtime.md index dca81c5fa7644..8f56b64693570 100644 --- a/docs/architecture/layer-runtime.md +++ b/docs/architecture/layer-runtime.md @@ -28,6 +28,7 @@ verified_by: [ packages/core/src/Layer/useLayer.test.tsx, packages/core/src/Layer/layerViewportInset.test.ts, + packages/core/src/Layer/layerViewportInset.consumer-build.test.mjs, packages/core/src/Layer/useLayerKeyboardClearance.test.tsx, packages/core/src/Layer/LayerProvider.test.tsx, packages/core/src/Layer/toastHost.test.tsx, @@ -755,6 +756,13 @@ be updated only as that work ships. `spec:AST-003` does not cover INV21 or INV22; `spec:AST-059` DEC-9 and DEC-10 govern them. It does not cover the INV7 gaps either. +INV11 gap: a StyleX build outside Core that imports +`Layer/layerViewportInset.stylex`, as a swizzled layer component does, gets an +undefined variable, because the module's constants resolve only inside Core's +own build. No record decides the fix, and `spec:AST-003` does not cover it. +`layerViewportInset.consumer-build.test.mjs` shows the gap as an expected +failure. + INV14 and INV15 are not met yet, and `spec:AST-003` does not cover them. The runtime owns the shared exit copy (INV13, `placeLayerExitCopy`) and the shared presentation hold (`Layer/presentationHold.ts`), which Popover uses for its @@ -948,6 +956,7 @@ this current architecture record. | INV19 | `layerPortalBoundary.test.ts`, `RichTextEditor.portalHost.test.tsx`, and `LayerBoundary.a11y.browser.spec.ts`, `TooltipDecorator.a11y.browser.spec.ts` and `DecoratorPortal.a11y.browser.spec.ts` in Chromium and WebKit | A corrected layer whose host is inside an editor-managed root, and that has no nearer layer root, rendered inside that root; a nested layer moved out of its parent layer's root; a layer that is safe inline moved; a moved wrapper whose `dir` differs from its host's | | INV3–INV5 | `useLayer.test.tsx` and `anchorName.test.ts` | A mode leaks geometry, RTL resolves from the wrong context, fallback clips, or a sibling anchor is lost | | INV11 | `layerViewportInset.test.ts`, `useLayer.test.tsx`, and the `Core/Layer` viewport-inset stories under the story play guard at a desktop and a phone viewport; `BaseTypeahead.incremental.test.tsx` and the Typeahead incremental-results stories for the `spec:AST-092` FR6 exception | A second gutter definition, a `100%`-of-span cap, a layer narrower than its explicit size, a layer box wider than the viewport, a layer inside the gutter, or a layer that slides toward an off-screen anchor; for an open Typeahead popup, a later list for the same query that flips it to the other side or grows it past the room on its side, or a first list for a query that is capped to the span beside the trigger | +| INV11 gap | `layerViewportInset.consumer-build.test.mjs`, an expected failure while the gap stands | A StyleX build outside Core that reads `Layer/layerViewportInset.stylex` defines every variable it references: the test passes, so the expected failure fails | | INV12 | The `Core/Layer` viewport-inset stories' first-frame comparison, and `useLayer.test.tsx`'s synchronous anchor read; for the check after a scroll stops, `useLayer.test.tsx`'s `scrollend` and 150 ms fallback cases (one check, in the first painted frame after the stop, with no read in the scroll handler) and the Tooltip suites' scroll-edge cases; a Chromium trace at 4× CPU of a wheel scroll that carries a Tooltip's trigger to the scroll edge counts the forced layouts per scroll before and after DEC-8: page scroll 27–42 → 5, a scrolling panel 7 → 5, the Slider value bubble 12–22 → 5 | A layer whose first painted rectangle differs from its settled one; a box check, a re-pick or a forced layout for a layer in a scroll frame or a scroll handler; a re-pick before the first painted frame after a scroll stops, or more than one per stop; a painted frame that shows a layer that keeps its anchor clear over its anchor | | INV11, INV12 | The software keyboard's cap: `useLayerKeyboardClearance.test.tsx` for its rule, its timing, its read before first paint, and the inset's hold; the `PopoverKeyboard`, `keyboardCappedLists` and `SelectorKeyboardInset` browser specs for its rendered geometry | A layer that runs under the keyboard, or is capped though the keyboard does not reach it; a cap that changes more than once per keyboard movement or under a press; a layer opened with the keyboard up whose first frame is uncapped or paints its content past its box; a layer that reads the inset and moves down when the keyboard report ends | | INV13 | `layerExitCopy.test.ts` and `layerExitCopy.interaction.browser.spec.ts` | A copy with an id, a form name, a form owner, or pointer reach; a copy off the layer's drawn box; a copy outside the top layer; a second copy for one layer | diff --git a/packages/core/src/Layer/layerViewportInset.consumer-build.test.mjs b/packages/core/src/Layer/layerViewportInset.consumer-build.test.mjs index c09e46384d168..643cf88b75f02 100644 --- a/packages/core/src/Layer/layerViewportInset.consumer-build.test.mjs +++ b/packages/core/src/Layer/layerViewportInset.consumer-build.test.mjs @@ -4,8 +4,9 @@ * @file layerViewportInset.consumer-build.test.mjs * @input Uses Babel, the StyleX compiler, Core's build config, and the * published `./Layer/layerViewportInset.stylex` entry as Core builds it - * @output Verifies an app's own StyleX build that reads the layer viewport - * inset references only CSS variables Core's stylesheet defines + * @output Shows that a StyleX build outside Core that reads the layer viewport + * inset references CSS variables Core's stylesheet does not define (an + * expected failure until the gap closes) * @position Regression test for the published layer viewport inset entry * (spec:AST-059 FR1, FR7) */ @@ -113,8 +114,12 @@ async function coreDefinitions() { } describe('the published layer viewport inset in an app StyleX build (spec:AST-059 FR7)', () => { - it('references no CSS variable that Core leaves undefined', async () => { - const css = await compileApp(` + // The INV11 gap in architecture:layer-runtime: the module's constants + // resolve only inside Core's own build. This fails until the gap closes. + it.fails( + 'references no CSS variable that Core leaves undefined', + async () => { + const css = await compileApp(` import * as stylex from '@stylexjs/stylex'; import {layerViewportInset} from '@astryxdesign/core/Layer/layerViewportInset.stylex'; export const styles = stylex.create({ @@ -125,12 +130,13 @@ describe('the published layer viewport inset in an app StyleX build (spec:AST-05 }, }); `); - const defined = await coreDefinitions(); - // A variable with no fallback that nothing defines makes the whole - // declaration invalid, so the app's sheet loses its cap. - const undefinedReferences = [...css.matchAll(/var\((--[\w-]+)\)/g)] - .map(match => match[1]) - .filter(name => !defined.has(name)); - expect(undefinedReferences).toEqual([]); - }); + const defined = await coreDefinitions(); + // A variable with no fallback that nothing defines makes the whole + // declaration invalid, so the app's sheet loses its cap. + const undefinedReferences = [...css.matchAll(/var\((--[\w-]+)\)/g)] + .map(match => match[1]) + .filter(name => !defined.has(name)); + expect(undefinedReferences).toEqual([]); + }, + ); }); From 02d521b583e122010c88fcb084f97dc82ed32f78 Mon Sep 17 00:00:00 2001 From: cixzhang Date: Tue, 17 Nov 2026 04:20:49 -0800 Subject: [PATCH 3/3] docs(layer-runtime): the gap is for a build that reads the built file; the test asserts it A build that compiles the published source entry defines the constants, so the INV11 gap names a StyleX build that resolves the module to its built dist file. The consumer build test now asserts the three undefined references, so it fails both when the gap closes and when its harness breaks. --- docs/architecture/layer-runtime.md | 15 +++++---- ...layerViewportInset.consumer-build.test.mjs | 33 +++++++++---------- 2 files changed, 23 insertions(+), 25 deletions(-) diff --git a/docs/architecture/layer-runtime.md b/docs/architecture/layer-runtime.md index 8f56b64693570..722d864d8ad76 100644 --- a/docs/architecture/layer-runtime.md +++ b/docs/architecture/layer-runtime.md @@ -756,12 +756,13 @@ be updated only as that work ships. `spec:AST-003` does not cover INV21 or INV22; `spec:AST-059` DEC-9 and DEC-10 govern them. It does not cover the INV7 gaps either. -INV11 gap: a StyleX build outside Core that imports -`Layer/layerViewportInset.stylex`, as a swizzled layer component does, gets an -undefined variable, because the module's constants resolve only inside Core's -own build. No record decides the fix, and `spec:AST-003` does not cover it. -`layerViewportInset.consumer-build.test.mjs` shows the gap as an expected -failure. +INV11 gap: a StyleX build outside Core that resolves +`Layer/layerViewportInset.stylex`, as a swizzled layer component imports it, to +its built `dist` file gets an undefined variable: that file holds the +constants' values, and the build reads each constant as a variable that +nothing defines. A build that compiles the published source entry defines +them. No record decides the fix, and `spec:AST-003` does not cover it. +`layerViewportInset.consumer-build.test.mjs` shows the gap. INV14 and INV15 are not met yet, and `spec:AST-003` does not cover them. The runtime owns the shared exit copy (INV13, `placeLayerExitCopy`) and the shared @@ -956,7 +957,7 @@ this current architecture record. | INV19 | `layerPortalBoundary.test.ts`, `RichTextEditor.portalHost.test.tsx`, and `LayerBoundary.a11y.browser.spec.ts`, `TooltipDecorator.a11y.browser.spec.ts` and `DecoratorPortal.a11y.browser.spec.ts` in Chromium and WebKit | A corrected layer whose host is inside an editor-managed root, and that has no nearer layer root, rendered inside that root; a nested layer moved out of its parent layer's root; a layer that is safe inline moved; a moved wrapper whose `dir` differs from its host's | | INV3–INV5 | `useLayer.test.tsx` and `anchorName.test.ts` | A mode leaks geometry, RTL resolves from the wrong context, fallback clips, or a sibling anchor is lost | | INV11 | `layerViewportInset.test.ts`, `useLayer.test.tsx`, and the `Core/Layer` viewport-inset stories under the story play guard at a desktop and a phone viewport; `BaseTypeahead.incremental.test.tsx` and the Typeahead incremental-results stories for the `spec:AST-092` FR6 exception | A second gutter definition, a `100%`-of-span cap, a layer narrower than its explicit size, a layer box wider than the viewport, a layer inside the gutter, or a layer that slides toward an off-screen anchor; for an open Typeahead popup, a later list for the same query that flips it to the other side or grows it past the room on its side, or a first list for a query that is capped to the span beside the trigger | -| INV11 gap | `layerViewportInset.consumer-build.test.mjs`, an expected failure while the gap stands | A StyleX build outside Core that reads `Layer/layerViewportInset.stylex` defines every variable it references: the test passes, so the expected failure fails | +| INV11 gap | `layerViewportInset.consumer-build.test.mjs`, which asserts the gap while it stands | A StyleX build outside Core that resolves `Layer/layerViewportInset.stylex` to its built file defines every variable it references, or the test cannot build the app: the test fails | | INV12 | The `Core/Layer` viewport-inset stories' first-frame comparison, and `useLayer.test.tsx`'s synchronous anchor read; for the check after a scroll stops, `useLayer.test.tsx`'s `scrollend` and 150 ms fallback cases (one check, in the first painted frame after the stop, with no read in the scroll handler) and the Tooltip suites' scroll-edge cases; a Chromium trace at 4× CPU of a wheel scroll that carries a Tooltip's trigger to the scroll edge counts the forced layouts per scroll before and after DEC-8: page scroll 27–42 → 5, a scrolling panel 7 → 5, the Slider value bubble 12–22 → 5 | A layer whose first painted rectangle differs from its settled one; a box check, a re-pick or a forced layout for a layer in a scroll frame or a scroll handler; a re-pick before the first painted frame after a scroll stops, or more than one per stop; a painted frame that shows a layer that keeps its anchor clear over its anchor | | INV11, INV12 | The software keyboard's cap: `useLayerKeyboardClearance.test.tsx` for its rule, its timing, its read before first paint, and the inset's hold; the `PopoverKeyboard`, `keyboardCappedLists` and `SelectorKeyboardInset` browser specs for its rendered geometry | A layer that runs under the keyboard, or is capped though the keyboard does not reach it; a cap that changes more than once per keyboard movement or under a press; a layer opened with the keyboard up whose first frame is uncapped or paints its content past its box; a layer that reads the inset and moves down when the keyboard report ends | | INV13 | `layerExitCopy.test.ts` and `layerExitCopy.interaction.browser.spec.ts` | A copy with an id, a form name, a form owner, or pointer reach; a copy off the layer's drawn box; a copy outside the top layer; a second copy for one layer | diff --git a/packages/core/src/Layer/layerViewportInset.consumer-build.test.mjs b/packages/core/src/Layer/layerViewportInset.consumer-build.test.mjs index 643cf88b75f02..d630bad4abaea 100644 --- a/packages/core/src/Layer/layerViewportInset.consumer-build.test.mjs +++ b/packages/core/src/Layer/layerViewportInset.consumer-build.test.mjs @@ -4,9 +4,9 @@ * @file layerViewportInset.consumer-build.test.mjs * @input Uses Babel, the StyleX compiler, Core's build config, and the * published `./Layer/layerViewportInset.stylex` entry as Core builds it - * @output Shows that a StyleX build outside Core that reads the layer viewport - * inset references CSS variables Core's stylesheet does not define (an - * expected failure until the gap closes) + * @output Shows that a StyleX build outside Core that reads the built layer + * viewport inset references CSS variables Core's stylesheet does not + * define, while the gap stands * @position Regression test for the published layer viewport inset entry * (spec:AST-059 FR1, FR7) */ @@ -114,12 +114,10 @@ async function coreDefinitions() { } describe('the published layer viewport inset in an app StyleX build (spec:AST-059 FR7)', () => { - // The INV11 gap in architecture:layer-runtime: the module's constants - // resolve only inside Core's own build. This fails until the gap closes. - it.fails( - 'references no CSS variable that Core leaves undefined', - async () => { - const css = await compileApp(` + // The INV11 gap in architecture:layer-runtime. When the gap closes this + // test fails: then it asserts that no reference is undefined. + it('references the three constants it reads as variables Core leaves undefined', async () => { + const css = await compileApp(` import * as stylex from '@stylexjs/stylex'; import {layerViewportInset} from '@astryxdesign/core/Layer/layerViewportInset.stylex'; export const styles = stylex.create({ @@ -130,13 +128,12 @@ describe('the published layer viewport inset in an app StyleX build (spec:AST-05 }, }); `); - const defined = await coreDefinitions(); - // A variable with no fallback that nothing defines makes the whole - // declaration invalid, so the app's sheet loses its cap. - const undefinedReferences = [...css.matchAll(/var\((--[\w-]+)\)/g)] - .map(match => match[1]) - .filter(name => !defined.has(name)); - expect(undefinedReferences).toEqual([]); - }, - ); + const defined = await coreDefinitions(); + // A variable with no fallback that nothing defines makes the whole + // declaration invalid, so the app's sheet loses its cap. + const undefinedReferences = [...css.matchAll(/var\((--[\w-]+)\)/g)] + .map(match => match[1]) + .filter(name => !defined.has(name)); + expect(undefinedReferences).toHaveLength(3); + }); });