|
| 1 | +// Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | + |
| 3 | +/** |
| 4 | + * @file build-css.layers.test.mjs |
| 5 | + * spec:AST-080 FR3: every package stylesheet registers the Astryx layer order |
| 6 | + * and keeps every rule inside its one layer, `astryx-base`. |
| 7 | + */ |
| 8 | + |
| 9 | +import fs from 'node:fs/promises'; |
| 10 | +import path from 'node:path'; |
| 11 | +import {fileURLToPath} from 'node:url'; |
| 12 | +import {beforeAll, describe, expect, it} from 'vitest'; |
| 13 | +import {ensureCoreBuilt} from '../packages/cli/clients/cli/commands/ensure-core-built.mjs'; |
| 14 | +import {ASTRYX_LAYER_ORDER, composePackageStylesheet} from './build-css.mjs'; |
| 15 | + |
| 16 | +const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..'); |
| 17 | + |
| 18 | +/** |
| 19 | + * Split a stylesheet into its top-level statements, comments removed: each is |
| 20 | + * either an at-rule statement ending in `;` or a block (`prelude { … }`). |
| 21 | + */ |
| 22 | +function topLevelStatements(css) { |
| 23 | + const text = css.replace(/\/\*[\s\S]*?\*\//g, ''); |
| 24 | + const statements = []; |
| 25 | + let depth = 0; |
| 26 | + let start = 0; |
| 27 | + for (let i = 0; i < text.length; i++) { |
| 28 | + const ch = text[i]; |
| 29 | + if (ch === '{') depth++; |
| 30 | + else if (ch === '}') { |
| 31 | + depth--; |
| 32 | + if (depth === 0) { |
| 33 | + statements.push(text.slice(start, i + 1).trim()); |
| 34 | + start = i + 1; |
| 35 | + } |
| 36 | + } else if (ch === ';' && depth === 0) { |
| 37 | + statements.push(text.slice(start, i + 1).trim()); |
| 38 | + start = i + 1; |
| 39 | + } |
| 40 | + } |
| 41 | + expect(depth).toBe(0); |
| 42 | + expect(text.slice(start).trim()).toBe(''); |
| 43 | + return statements.filter(Boolean); |
| 44 | +} |
| 45 | + |
| 46 | +/** Assert FR3's shape: the order line first, then exactly one block in `layer`. */ |
| 47 | +function expectOneAstryxLayer(css, layer) { |
| 48 | + const statements = topLevelStatements(css); |
| 49 | + expect(statements[0]).toBe(ASTRYX_LAYER_ORDER); |
| 50 | + expect(statements).toHaveLength(2); |
| 51 | + expect(statements[1].startsWith(`@layer ${layer} {`)).toBe(true); |
| 52 | +} |
| 53 | + |
| 54 | +describe('package stylesheets register the Astryx layer order (spec:AST-080 FR3)', () => { |
| 55 | + it('names the same order theme output registers', () => { |
| 56 | + expect(ASTRYX_LAYER_ORDER).toBe('@layer reset, astryx-base, astryx-theme;'); |
| 57 | + }); |
| 58 | + |
| 59 | + it('wraps compiled rules after the order line, inside astryx-base only', () => { |
| 60 | + const css = composePackageStylesheet( |
| 61 | + 'Probe', |
| 62 | + '.x1{color:red}\n@media (hover: hover){.x2:hover{color:blue}}', |
| 63 | + ); |
| 64 | + expectOneAstryxLayer(css, 'astryx-base'); |
| 65 | + expect(css).toContain('.x1{color:red}'); |
| 66 | + expect(css).toContain('@media (hover: hover){.x2:hover{color:blue}}'); |
| 67 | + }); |
| 68 | + |
| 69 | + it('fails the shape check for an unlayered rule', () => { |
| 70 | + const css = `${composePackageStylesheet('Probe', '.x1{color:red}')}.x9{color:red}\n`; |
| 71 | + expect(() => expectOneAstryxLayer(css, 'astryx-base')).toThrow(); |
| 72 | + }); |
| 73 | + |
| 74 | + it('fails the shape check without the order line', () => { |
| 75 | + const css = composePackageStylesheet('Probe', '.x1{color:red}').replace( |
| 76 | + `${ASTRYX_LAYER_ORDER}\n`, |
| 77 | + '', |
| 78 | + ); |
| 79 | + expect(() => expectOneAstryxLayer(css, 'astryx-base')).toThrow(); |
| 80 | + }); |
| 81 | + |
| 82 | + describe('the built Core stylesheet', () => { |
| 83 | + let astryxCss; |
| 84 | + beforeAll(async () => { |
| 85 | + ensureCoreBuilt(); |
| 86 | + astryxCss = await fs.readFile( |
| 87 | + path.join(ROOT, 'packages/core/dist/astryx.css'), |
| 88 | + 'utf8', |
| 89 | + ); |
| 90 | + }, 180_000); |
| 91 | + |
| 92 | + it('astryx.css is the order line plus one astryx-base block', () => { |
| 93 | + expectOneAstryxLayer(astryxCss, 'astryx-base'); |
| 94 | + }); |
| 95 | + }); |
| 96 | + |
| 97 | + it('reset.css is the order line plus one reset block', async () => { |
| 98 | + const resetCss = await fs.readFile( |
| 99 | + path.join(ROOT, 'packages/core/src/reset.css'), |
| 100 | + 'utf8', |
| 101 | + ); |
| 102 | + expectOneAstryxLayer(resetCss, 'reset'); |
| 103 | + }); |
| 104 | +}); |
0 commit comments