|
| 1 | +// Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | + |
| 3 | +/** |
| 4 | + * @file Contract: the interaction lanes' scope leaves out the files under a |
| 5 | + * package's src that put nothing on a page the proofs load (unit tests, |
| 6 | + * docs, Markdown records and accessibility specs), in both engines, in the |
| 7 | + * leg-count job and in each lane's own scope step. |
| 8 | + */ |
| 9 | + |
| 10 | +import {execFileSync} from 'node:child_process'; |
| 11 | +import fs from 'node:fs'; |
| 12 | +import os from 'node:os'; |
| 13 | +import path from 'node:path'; |
| 14 | + |
| 15 | +import {describe, expect, it} from 'vitest'; |
| 16 | +import yaml from 'yaml'; |
| 17 | + |
| 18 | +import {isInteractionSpec} from './playwright-legs.mjs'; |
| 19 | +import {proofImporters} from './a11y-pr-contracts.mjs'; |
| 20 | + |
| 21 | +// What a proof's page loads: the interaction specs, the stories they open, |
| 22 | +// and Storybook's preview. |
| 23 | +const isProofPage = file => |
| 24 | + isInteractionSpec(file) || |
| 25 | + /\.stories\.[cm]?[jt]sx?$/.test(file) || |
| 26 | + file === 'apps/storybook/.storybook/preview.tsx'; |
| 27 | + |
| 28 | +const root = path.resolve(import.meta.dirname, '../..'); |
| 29 | +const ci = yaml.parse( |
| 30 | + fs.readFileSync(path.join(root, '.github/workflows/ci.yml'), 'utf8'), |
| 31 | +); |
| 32 | +const scopeOf = (job, id) => |
| 33 | + new RegExp(ci.jobs[job].steps.find(step => step.id === id).with.paths); |
| 34 | + |
| 35 | +const SCOPES = { |
| 36 | + 'Chromium legs': scopeOf('interaction-legs', 'scope-chromium'), |
| 37 | + 'Chromium proofs': scopeOf('pr-interaction', 'scope'), |
| 38 | + 'WebKit legs': scopeOf('interaction-legs', 'scope-webkit'), |
| 39 | + 'WebKit proofs': scopeOf('pr-interaction-webkit', 'scope'), |
| 40 | +}; |
| 41 | + |
| 42 | +// Real files the proofs never load. |
| 43 | +const NON_RUNTIME = [ |
| 44 | + 'packages/core/src/BottomSheet/BottomSheet.test.tsx', |
| 45 | + 'packages/core/src/BottomSheet/BottomSheetCloseFocus.test.tsx', |
| 46 | + 'packages/core/src/BottomSheet/BottomSheet.doc.mjs', |
| 47 | + // Accessibility specs, in and outside a `__tests__/` folder. |
| 48 | + 'packages/core/src/Table/__tests__/TableFocusRing.a11y.chromium.spec.ts', |
| 49 | + 'packages/core/src/Lightbox/LightboxKeyboard.a11y.browser.spec.ts', |
| 50 | +]; |
| 51 | + |
| 52 | +// Real files a proof's page or fixture loads. |
| 53 | +const RUNTIME = [ |
| 54 | + 'packages/core/src/BottomSheet/BottomSheet.tsx', |
| 55 | + 'packages/core/src/BottomSheet/BottomSheetPanel.tsx', |
| 56 | + 'packages/core/src/BottomSheet/useSheetGestures.ts', |
| 57 | + 'apps/storybook/stories/BottomSheet.stories.tsx', |
| 58 | + 'packages/core/src/gestures/__tests__/browser/fixture.ts', |
| 59 | + 'packages/themes/neutral/src/neutralTheme.ts', |
| 60 | + // Theme sources are shared infrastructure: a test there selects too. |
| 61 | + 'packages/themes/neutral/src/neutralTheme.test.ts', |
| 62 | + 'packages/core/src/theme/Theme.test.tsx', |
| 63 | + // So are a component package's shared folders. |
| 64 | + 'packages/lab/src/__tests__/ReorderGripPinchZoom.a11y.chromium.spec.ts', |
| 65 | + 'packages/core/src/gestures/README.md', |
| 66 | + 'packages/core/src/gestures/__tests__/GestureHost.test.ts', |
| 67 | + // So is every package outside the component packages. |
| 68 | + 'packages/native/src/contract/contracts.test.ts', |
| 69 | +]; |
| 70 | + |
| 71 | +describe('the interaction scope', () => { |
| 72 | + for (const [lane, scope] of Object.entries(SCOPES)) { |
| 73 | + it(`${lane}: a test-, doc- or record-only change selects nothing`, () => { |
| 74 | + for (const file of NON_RUNTIME) { |
| 75 | + expect(scope.test(file), file).toBe(false); |
| 76 | + } |
| 77 | + }); |
| 78 | + |
| 79 | + it(`${lane}: a runtime file still selects the proofs`, () => { |
| 80 | + for (const file of RUNTIME) { |
| 81 | + expect(scope.test(file), file).toBe(true); |
| 82 | + } |
| 83 | + }); |
| 84 | + |
| 85 | + if (lane.startsWith('Chromium')) |
| 86 | + it(`${lane}: a test at the root of a package outside the component packages still selects them`, () => { |
| 87 | + // WebKit's scope never covered a package's src root. |
| 88 | + expect(scope.test('packages/build/src/babel.test.mjs')).toBe(true); |
| 89 | + // A component package's src root is shared too. |
| 90 | + expect(scope.test('packages/charts/src/Chart.test.tsx')).toBe(true); |
| 91 | + }); |
| 92 | + |
| 93 | + it(`${lane}: a changed interaction spec still selects them`, () => { |
| 94 | + expect( |
| 95 | + scope.test( |
| 96 | + 'packages/core/src/BottomSheet/__tests__/BottomSheetDetent.interaction.browser.spec.ts', |
| 97 | + ), |
| 98 | + ).toBe(true); |
| 99 | + }); |
| 100 | + } |
| 101 | + |
| 102 | + it('each lane decides the same way in the leg count and in its own scope step', () => { |
| 103 | + expect(SCOPES['Chromium legs'].source).toBe( |
| 104 | + SCOPES['Chromium proofs'].source, |
| 105 | + ); |
| 106 | + expect(SCOPES['WebKit legs'].source).toBe(SCOPES['WebKit proofs'].source); |
| 107 | + }); |
| 108 | +}); |
| 109 | + |
| 110 | +describe('a left-out file that a proof imports', () => { |
| 111 | + it("is found through the proofs' value imports", () => { |
| 112 | + // A spec that imports a `.test` helper beside it. |
| 113 | + const tree = fs.mkdtempSync(path.join(os.tmpdir(), 'proof-imports-')); |
| 114 | + const files = { |
| 115 | + 'packages/core/src/Sheet/__tests__/Sheet.interaction.browser.spec.ts': |
| 116 | + "import {drag} from './drag.test';\nimport type {Kind} from './kinds.test';\n", |
| 117 | + 'packages/core/src/Sheet/__tests__/drag.test.ts': |
| 118 | + "export {ease} from '../Sheet.doc.mjs';\n", |
| 119 | + 'packages/core/src/Sheet/__tests__/kinds.test.ts': '', |
| 120 | + 'packages/core/src/Sheet/Sheet.doc.mjs': '', |
| 121 | + }; |
| 122 | + for (const [file, text] of Object.entries(files)) { |
| 123 | + fs.mkdirSync(path.dirname(path.join(tree, file)), {recursive: true}); |
| 124 | + fs.writeFileSync(path.join(tree, file), text); |
| 125 | + } |
| 126 | + const importers = proofImporters({ |
| 127 | + root: tree, |
| 128 | + listed: Object.keys(files), |
| 129 | + isProof: isInteractionSpec, |
| 130 | + }); |
| 131 | + const spec = |
| 132 | + 'packages/core/src/Sheet/__tests__/Sheet.interaction.browser.spec.ts'; |
| 133 | + expect([...importers.keys()].sort()).toEqual([ |
| 134 | + 'packages/core/src/Sheet/Sheet.doc.mjs', |
| 135 | + 'packages/core/src/Sheet/__tests__/drag.test.ts', |
| 136 | + ]); |
| 137 | + expect([...importers.get('packages/core/src/Sheet/Sheet.doc.mjs')]).toEqual( |
| 138 | + [spec], |
| 139 | + ); |
| 140 | + // So the scopes would leave out files this proof loads. |
| 141 | + for (const file of importers.keys()) |
| 142 | + expect(SCOPES['Chromium proofs'].test(file), file).toBe(false); |
| 143 | + }); |
| 144 | + |
| 145 | + it('is found through a fixture a proof bundles by path', () => { |
| 146 | + const tree = fs.mkdtempSync(path.join(os.tmpdir(), 'bundle-imports-')); |
| 147 | + const files = { |
| 148 | + 'packages/core/src/Sheet/__tests__/Sheet.interaction.browser.spec.ts': |
| 149 | + "const entry = new URL('./sheetBundle.ts', import.meta.url);\nconst other = bundleOf('bare.ts');\n", |
| 150 | + 'packages/core/src/Sheet/__tests__/sheetBundle.ts': |
| 151 | + "import {ease} from '../ease.test';\n", |
| 152 | + 'packages/core/src/Sheet/__tests__/bare.ts': '', |
| 153 | + 'packages/core/src/Sheet/ease.test.ts': '', |
| 154 | + }; |
| 155 | + for (const [file, text] of Object.entries(files)) { |
| 156 | + fs.mkdirSync(path.dirname(path.join(tree, file)), {recursive: true}); |
| 157 | + fs.writeFileSync(path.join(tree, file), text); |
| 158 | + } |
| 159 | + const importers = proofImporters({ |
| 160 | + root: tree, |
| 161 | + listed: Object.keys(files), |
| 162 | + isProof: isProofPage, |
| 163 | + }); |
| 164 | + expect([...importers.keys()].sort()).toEqual([ |
| 165 | + 'packages/core/src/Sheet/__tests__/bare.ts', |
| 166 | + 'packages/core/src/Sheet/__tests__/sheetBundle.ts', |
| 167 | + 'packages/core/src/Sheet/ease.test.ts', |
| 168 | + ]); |
| 169 | + }); |
| 170 | + |
| 171 | + it('is found through a story a proof opens', () => { |
| 172 | + const tree = fs.mkdtempSync(path.join(os.tmpdir(), 'story-imports-')); |
| 173 | + const files = { |
| 174 | + 'apps/storybook/stories/Sheet.stories.tsx': |
| 175 | + "import {Sheet} from '@astryxdesign/core/Sheet/Sheet';\n", |
| 176 | + 'packages/core/src/Sheet/Sheet.tsx': |
| 177 | + "import {notes} from './Sheet.doc.mjs';\n", |
| 178 | + 'packages/core/src/Sheet/Sheet.doc.mjs': '', |
| 179 | + }; |
| 180 | + for (const [file, text] of Object.entries(files)) { |
| 181 | + fs.mkdirSync(path.dirname(path.join(tree, file)), {recursive: true}); |
| 182 | + fs.writeFileSync(path.join(tree, file), text); |
| 183 | + } |
| 184 | + const importers = proofImporters({ |
| 185 | + root: tree, |
| 186 | + listed: Object.keys(files), |
| 187 | + isProof: isProofPage, |
| 188 | + }); |
| 189 | + expect([...importers.get('packages/core/src/Sheet/Sheet.doc.mjs')]).toEqual( |
| 190 | + ['apps/storybook/stories/Sheet.stories.tsx'], |
| 191 | + ); |
| 192 | + }); |
| 193 | + |
| 194 | + it('does not exist: no proof, story or Storybook preview in the repository loads a file the scopes leave out', () => { |
| 195 | + const listed = execFileSync('git', ['ls-files'], { |
| 196 | + cwd: root, |
| 197 | + encoding: 'utf8', |
| 198 | + }) |
| 199 | + .split('\n') |
| 200 | + .filter(Boolean); |
| 201 | + const importers = proofImporters({ |
| 202 | + root, |
| 203 | + listed, |
| 204 | + isProof: isProofPage, |
| 205 | + }); |
| 206 | + expect(importers.size).toBeGreaterThan(0); |
| 207 | + const leftOut = [...importers.keys()].filter( |
| 208 | + file => |
| 209 | + /^packages\/[^/]+\/src\//.test(file) && |
| 210 | + (!SCOPES['Chromium proofs'].test(file) || |
| 211 | + (/^packages\/[^/]+\/src\/[^/]+\//.test(file) && |
| 212 | + !SCOPES['WebKit proofs'].test(file))), |
| 213 | + ); |
| 214 | + // A proof that needs such a file: rename the file so its name says it is |
| 215 | + // runtime code, or narrow the scopes' lookahead. |
| 216 | + expect(leftOut).toEqual([]); |
| 217 | + }); |
| 218 | +}); |
0 commit comments