-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathaudit.test.mjs
More file actions
395 lines (362 loc) · 14.6 KB
/
Copy pathaudit.test.mjs
File metadata and controls
395 lines (362 loc) · 14.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
// Copyright (c) Meta Platforms, Inc. and affiliates.
/**
* @file audit.test.mjs
* Pins the two properties that make the readiness report trustworthy:
* the source tree overrides the manifest (a stale claim cannot inflate a
* score), and a component invisible to the CI accessibility or RTL gates
* cannot pass the checks that depend on those gates. The second property is
* the regression guard for the gap that let every lab component sit outside
* both audits while still being scored against them.
*/
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import {describe, it, expect, beforeAll, afterAll} from 'vitest';
import {CHECK_KEYS, HUMAN_REVIEW_KEYS, getCheck} from './catalog.mjs';
import {deriveChecks, _internal} from './automated.mjs';
import {auditCandidate, buildRegistry, buildReport} from './audit.mjs';
/** A candidate whose files we can write into a scratch repo. */
const candidate = {
id: 'widget',
displayName: 'Widget',
sourceDir: 'Widget',
targetPackage: '@astryxdesign/core',
trackingIssue: 1,
voteIssue: 1,
storyTitle: 'Lab/Widget',
storybookStoryId: 'lab-widget--default',
publicExports: ['Widget'],
stateProps: ['isDisabled'],
summary: 'A widget.',
declared: {},
};
let root;
/** Write a fully passing scratch repo for `candidate`. */
function scaffold(overrides = {}) {
const files = {
'.github/workflows/ci.yml':
'CHANGED=$(git diff --name-only origin/main...HEAD -- packages/core/src/ packages/lab/src/ | grep -v x)\n',
'apps/storybook/rtl-audit/rtl-audit.mjs':
"const AUDITED_STORY_PREFIXES = ['core-', 'lab-'];\n",
'packages/lab/package.json': JSON.stringify({
name: '@astryxdesign/lab',
dependencies: {'d3-scale': '^4.0.2'},
peerDependencies: {react: '>=19.0.0'},
}),
'packages/lab/src/index.ts': "export {Widget} from './Widget';\n",
'packages/lab/src/Widget/index.ts':
"export {Widget} from './Widget';\nexport type {WidgetProps} from './Widget';\n",
'packages/lab/src/Widget/Widget.tsx':
"import {Text} from '@astryxdesign/core/Text';\nexport function Widget() { return <Text />; }\n",
'packages/lab/src/Widget/Widget.doc.mjs':
'export const docs = {props: [], usage: {}, examples: [], theming: {targets: []}};\n',
'packages/lab/src/Widget/Widget.test.tsx':
"it('renders', () => {});\nit('supports isDisabled', () => {});\nit('handles Enter', () => {});\n",
'apps/storybook/stories/Widget.stories.tsx':
"const meta = {title: 'Lab/Widget'};\n" +
'export const Default: Story = {};\n' +
'export const Disabled: Story = {isDisabled: true};\n' +
'export const Loading: Story = {};\n' +
'export const Empty: Story = {};\n' +
'export const ThemeMatrix: Story = {};\n',
...overrides,
};
for (const [rel, contents] of Object.entries(files)) {
if (contents === null) continue;
const abs = path.join(root, rel);
fs.mkdirSync(path.dirname(abs), {recursive: true});
fs.writeFileSync(abs, contents);
}
}
beforeAll(() => {
root = fs.mkdtempSync(path.join(os.tmpdir(), 'lab-readiness-'));
});
afterAll(() => {
fs.rmSync(root, {recursive: true, force: true});
});
describe('automated derivation', () => {
it('passes the derivable checks for a fully wired component', () => {
scaffold();
const derived = deriveChecks(root, candidate);
const failing = Object.entries(derived)
.filter(([, v]) => v.state !== 'passed')
.map(([k, v]) => `${k}: ${v.note}`);
expect(failing).toEqual([]);
});
it('fails the accessibility check when ci.yml stops watching lab', () => {
scaffold({
'.github/workflows/ci.yml':
'CHANGED=$(git diff --name-only origin/main...HEAD -- packages/core/src/ | grep -v x)\n',
});
const derived = deriveChecks(root, candidate);
expect(derived.accessibilityContracts.state).not.toBe('passed');
expect(derived.accessibilityContracts.note).toMatch(
/excludes packages\/lab/,
);
});
it('fails the keyboard check when the RTL sweep stops covering lab', () => {
scaffold({
'apps/storybook/rtl-audit/rtl-audit.mjs':
"const AUDITED_STORY_PREFIXES = ['core-'];\n",
});
const derived = deriveChecks(root, candidate);
expect(derived.keyboardAccessibility.state).not.toBe('passed');
expect(derived.keyboardAccessibility.note).toMatch(/RTL/);
});
it('fails the accessibility check when no story title resolves to the component name', () => {
scaffold({
'apps/storybook/stories/Widget.stories.tsx':
"const meta = {title: 'Lab/WidgetSelector'};\nexport const Default: Story = {};\n",
});
const derived = deriveChecks(root, candidate);
expect(derived.accessibilityContracts.state).not.toBe('passed');
expect(derived.accessibilityContracts.note).toMatch(
/resolves to the analyzer/,
);
});
it('requires an advertised state to appear in both a story and a test', () => {
scaffold({
'packages/lab/src/Widget/Widget.test.tsx':
"it('renders', () => {});\nit('handles Enter', () => {});\n",
});
const derived = deriveChecks(root, candidate);
expect(derived.stateCoverage.state).not.toBe('passed');
expect(derived.stateCoverage.note).toMatch(/isDisabled.*no test/);
});
it('asks for an edge-case story only for states the props expose', () => {
scaffold({
'packages/lab/src/Widget/Widget.tsx':
"import {Text} from '@astryxdesign/core/Text';\n" +
'export interface WidgetProps { isDisabled?: boolean; isLoading?: boolean }\n' +
'export function Widget() { return <Text />; }\n',
'apps/storybook/stories/Widget.stories.tsx':
"const meta = {title: 'Lab/Widget'};\n" +
'export const Default: Story = {};\n' +
'export const ThemeMatrix: Story = {};\n',
});
const derived = deriveChecks(root, candidate);
expect(derived.edgeCases.state).not.toBe('passed');
expect(derived.edgeCases.note).toMatch(/disabled/);
expect(derived.edgeCases.note).toMatch(/loading/);
// Nothing in the props implies an empty state, so it must not be demanded.
expect(derived.edgeCases.note).not.toMatch(/empty/);
});
it('owes no edge-case story when the component exposes no such state', () => {
// The rubric describes the API; it must never pressure someone into adding
// an isLoading prop that nothing needs just to turn a check green.
scaffold({
'apps/storybook/stories/Widget.stories.tsx':
"const meta = {title: 'Lab/Widget'};\n" +
'export const Default: Story = {};\n' +
'export const Disabled: Story = {};\n' +
'export const ThemeMatrix: Story = {};\n',
});
const derived = deriveChecks(root, candidate);
expect(derived.edgeCases.state).toBe('passed');
expect(derived.edgeCases.note).toMatch(/none is owed a story/);
});
it('asks for an empty story when the component renders an EmptyState', () => {
scaffold({
'packages/lab/src/Widget/Widget.tsx':
"import {EmptyState} from '@astryxdesign/core/EmptyState';\n" +
'export function Widget() { return <EmptyState />; }\n',
'apps/storybook/stories/Widget.stories.tsx':
"const meta = {title: 'Lab/Widget'};\n" +
'export const Default: Story = {};\n' +
'export const ThemeMatrix: Story = {};\n',
});
const derived = deriveChecks(root, candidate);
expect(derived.edgeCases.state).not.toBe('passed');
expect(derived.edgeCases.note).toMatch(/empty/);
});
it('accepts a locally defined SVG glyph', () => {
// Core defines glyphs this way in Avatar, Thumbnail, and Indicator, so a
// raw <svg> is idiomatic rather than a finding.
scaffold({
'packages/lab/src/Widget/Widget.tsx':
"import {Text} from '@astryxdesign/core/Text';\n" +
'function Glyph() { return <svg><circle /></svg>; }\n' +
'export function Widget() { return <Text><Glyph /></Text>; }\n',
});
const derived = deriveChecks(root, candidate);
expect(derived.systemIntegration.state).toBe('passed');
expect(derived.reuseNaming.state).toBe('passed');
});
it('flags an icon import from an undeclared package', () => {
scaffold({
'packages/lab/src/Widget/Widget.tsx':
"import {Text} from '@astryxdesign/core/Text';\n" +
"import {GripVertical} from 'lucide-react';\n" +
'export function Widget() { return <Text><GripVertical /></Text>; }\n',
});
const derived = deriveChecks(root, candidate);
expect(derived.systemIntegration.state).not.toBe('passed');
expect(derived.systemIntegration.note).toMatch(/lucide-react/);
expect(derived.reuseNaming.note).toMatch(/lucide-react/);
});
it('does not flag a package the lab manifest declares', () => {
scaffold({
'packages/lab/src/Widget/Widget.tsx':
"import {Text} from '@astryxdesign/core/Text';\n" +
"import {scaleLinear} from 'd3-scale';\n" +
'export function Widget() { return <Text>{String(scaleLinear)}</Text>; }\n',
});
expect(deriveChecks(root, candidate).systemIntegration.state).toBe(
'passed',
);
});
it('accepts inline type modifiers in the barrel', () => {
scaffold({
'packages/lab/src/Widget/index.ts':
"export {Widget, type WidgetProps} from './Widget';\n",
});
expect(deriveChecks(root, candidate).structureTypes.state).toBe('passed');
});
});
describe('manifest reconciliation', () => {
it('lets the source tree override a passing manifest claim', () => {
scaffold({
'packages/lab/src/Widget/Widget.doc.mjs':
'export const docs = {props: [], usage: {}, examples: []};\n',
});
const result = auditCandidate(root, {
...candidate,
declared: {
tokensTheming: {
state: 'passed',
note: 'claimed',
evidence: [{label: 'PR #1', url: 'https://example.com'}],
},
},
});
const check = result.checks.find(c => c.key === 'tokensTheming');
expect(check.state).toBe('in_progress');
expect(check.contradictsManifest).toBe(true);
expect(result.contradictions.map(c => c.key)).toContain('tokensTheming');
});
it('demotes a passing claim that links no evidence', () => {
scaffold();
const result = auditCandidate(root, {
...candidate,
declared: {visualQuality: {state: 'passed', note: 'looks fine'}},
});
const check = result.checks.find(c => c.key === 'visualQuality');
expect(check.state).toBe('in_progress');
expect(check.note).toMatch(/requires linked evidence/);
});
it('keeps a human-review claim that does link evidence', () => {
scaffold();
const result = auditCandidate(root, {
...candidate,
declared: {
visualQuality: {
state: 'passed',
note: 'reviewed',
evidence: [{label: 'PR #2', url: 'https://example.com'}],
},
},
});
expect(result.checks.find(c => c.key === 'visualQuality').state).toBe(
'passed',
);
});
it('never derives a human-review check from the source tree', () => {
scaffold();
const derived = deriveChecks(root, candidate);
for (const key of HUMAN_REVIEW_KEYS) {
expect(derived[key]).toBeUndefined();
}
});
it('treats responsive and interaction readiness as an evidenced human-review check', () => {
expect(HUMAN_REVIEW_KEYS).toContain('responsiveInteractionReadiness');
expect(getCheck('responsiveInteractionReadiness')).toMatchObject({
stageKey: 'hardenReview',
sectionKey: 'humanReview',
protocolUrl:
'https://github.com/facebook/astryx/wiki/Component-Hardening-Protocol',
humanReview: true,
});
scaffold();
const result = auditCandidate(root, {
...candidate,
declared: {
responsiveInteractionReadiness: {
state: 'passed',
note: 'Checklist completed with Pass and N/A rows.',
},
},
});
const check = result.checks.find(
c => c.key === 'responsiveInteractionReadiness',
);
expect(check.state).toBe('in_progress');
expect(check.note).toMatch(/requires linked evidence/);
});
});
describe('report shape', () => {
it('scores every catalog check exactly once', () => {
scaffold();
const result = auditCandidate(root, candidate);
expect(result.checks.map(c => c.key)).toEqual(CHECK_KEYS);
expect(result.totalChecks).toBe(CHECK_KEYS.length);
});
it('reports graduation readiness only when every check passes', () => {
scaffold();
const result = auditCandidate(root, candidate);
expect(result.isGraduationReady).toBe(false);
expect(result.passedChecks).toBeLessThan(CHECK_KEYS.length);
// A perfect source tree is not enough: the checks that only a manifest can
// assert (research, spec, review, merge, and human sign-off) must each
// carry evidence too.
const derivable = new Set(Object.keys(deriveChecks(root, candidate)));
const declaredOnly = CHECK_KEYS.filter(key => !derivable.has(key));
expect(declaredOnly).toEqual(expect.arrayContaining(HUMAN_REVIEW_KEYS));
const complete = auditCandidate(root, {
...candidate,
declared: Object.fromEntries(
declaredOnly.map(key => [
key,
{
state: 'passed',
note: 'evidenced',
evidence: [{label: 'PR #3', url: 'https://example.com'}],
},
]),
),
});
expect(complete.isGraduationReady).toBe(true);
expect(complete.passedChecks).toBe(CHECK_KEYS.length);
});
it('emits a registry the Storybook panel can read', () => {
scaffold();
const report = buildReport(root, [candidate]);
const registry = buildRegistry(report);
expect(registry.components).toHaveLength(1);
const [component] = registry.components;
expect(component.id).toBe('widget');
expect(Object.keys(component.stageResults)).toEqual([
'research',
'spec',
'build',
'hardenChecks',
'hardenReview',
]);
expect(component.checks).toHaveLength(CHECK_KEYS.length);
});
});
describe('CI wiring parsers', () => {
it('reads the component roots out of the real ci.yml', () => {
const repoRoot = path.resolve(import.meta.dirname, '..', '..');
const roots = _internal.ciComponentRoots(repoRoot);
expect(roots).toContain('packages/core/src/');
expect(roots).toContain('packages/lab/src/');
});
it('reads the audited story prefixes out of the real rtl-audit', () => {
const repoRoot = path.resolve(import.meta.dirname, '..', '..');
expect(_internal.rtlAuditedPrefixes(repoRoot)).toEqual(['core-', 'lab-']);
});
it('derives the component name pr-a11y matches against', () => {
expect(_internal.a11yComponentFromTitle('Lab/ListInput')).toBe('listinput');
expect(_internal.a11yComponentFromTitle('Core/XDSButton')).toBe('button');
});
});