Skip to content

Commit 13d3ca3

Browse files
cixzhangfreddymeta
andauthored
test(rtl): report verified-N/A receipt drift without changing coverage status (#13909)
* test(rtl): report verified-N/A receipt drift without changing coverage status Co-authored-by: freddymeta <170298698+freddymeta@users.noreply.github.com> * test(rtl): verify-na states its exit codes and prints current receipts Co-authored-by: freddymeta <170298698+freddymeta@users.noreply.github.com> * test(rtl): verify-na exits 1 when the registry is not a JSON array Co-authored-by: freddymeta <170298698+freddymeta@users.noreply.github.com>
1 parent 093cfc4 commit 13d3ca3

7 files changed

Lines changed: 431 additions & 0 deletions

File tree

‎.github/scripts/rtl-scorecard-summary.mjs‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,24 @@ if (!fs.existsSync(reportPath)) {
189189
out.push('');
190190
}
191191
}
192+
// Receipt drift is informational: it lists receipts whose recorded bytes no
193+
// longer match the checkout, and changes no coverage status.
194+
const drift = r.receiptDrift;
195+
if (drift && drift.drifted.length > 0) {
196+
out.push('### Receipt drift (informational)');
197+
out.push('');
198+
out.push(
199+
`${drift.drifted.length} of ${drift.checked} receipted verified-N/A declarations no longer match their recorded files. Coverage status is unchanged. Re-review each component's source and stories, then refresh its receipts.`,
200+
);
201+
out.push('');
202+
out.push('| Component | Changed | Missing | Malformed |');
203+
out.push('|-----------|---------|---------|-----------|');
204+
for (const entry of drift.drifted)
205+
out.push(
206+
`| ${markdownCell(entry.component)} | ${markdownCell(entry.changed.join(', '))} | ${markdownCell(entry.missing.join(', '))} | ${markdownCell(entry.malformed.join(', '))} |`,
207+
);
208+
out.push('');
209+
}
192210
// Stories whose scan threw and ran once more on a fresh page. The last
193211
// line is the one a log search counts across runs.
194212
const retried = r.retries?.stories ?? [];
Lines changed: 235 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,235 @@
1+
// Copyright (c) Meta Platforms, Inc. and affiliates.
2+
3+
/**
4+
* @file Unit tests for RTL verified-N/A receipt drift.
5+
*/
6+
7+
import crypto from 'node:crypto';
8+
import fs from 'node:fs';
9+
import os from 'node:os';
10+
import path from 'node:path';
11+
import {spawnSync} from 'node:child_process';
12+
import {afterEach, describe, expect, it} from 'vitest';
13+
import {buildComponentCoverage} from '../../apps/storybook/rtl-audit/rtl-audit-coverage.mjs';
14+
import {
15+
fileReceipt,
16+
receiptDrift,
17+
} from '../../apps/storybook/rtl-audit/verified-na-receipts.mjs';
18+
19+
const dirs = [];
20+
21+
afterEach(() => {
22+
for (const dir of dirs.splice(0)) {
23+
fs.rmSync(dir, {recursive: true, force: true});
24+
}
25+
});
26+
27+
function checkout(files) {
28+
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'rtl-receipts-'));
29+
dirs.push(root);
30+
for (const [file, content] of Object.entries(files)) {
31+
fs.mkdirSync(path.dirname(path.join(root, file)), {recursive: true});
32+
fs.writeFileSync(path.join(root, file), content);
33+
}
34+
return root;
35+
}
36+
37+
const receiptOf = content =>
38+
`sha256:${crypto.createHash('sha256').update(content).digest('hex')}`;
39+
40+
describe('fileReceipt', () => {
41+
it('hashes a file, and answers null for a missing one', () => {
42+
const root = checkout({'packages/core/src/Dot/Dot.tsx': 'dot'});
43+
expect(fileReceipt(root, 'packages/core/src/Dot/Dot.tsx')).toBe(
44+
receiptOf('dot'),
45+
);
46+
expect(fileReceipt(root, 'packages/core/src/Dot/Gone.tsx')).toBeNull();
47+
});
48+
49+
it('refuses a path that leaves the repository', () => {
50+
const root = checkout({'a.txt': 'a'});
51+
expect(() => fileReceipt(root, '../outside.txt')).toThrow(
52+
/leaves the repository/,
53+
);
54+
});
55+
});
56+
57+
describe('receiptDrift', () => {
58+
const files = {
59+
'packages/core/src/Dot/Dot.tsx': 'dot source',
60+
'apps/storybook/stories/Dot.stories.tsx': 'dot stories',
61+
};
62+
63+
it('reports nothing when every receipt matches', () => {
64+
const root = checkout(files);
65+
const drift = receiptDrift({
66+
projectRoot: root,
67+
verifiedNa: [
68+
{
69+
component: 'core/Dot',
70+
reason: 'r',
71+
evidence: Object.fromEntries(
72+
Object.entries(files).map(([file, content]) => [
73+
file,
74+
receiptOf(content),
75+
]),
76+
),
77+
},
78+
],
79+
});
80+
expect(drift).toEqual({checked: 1, withoutEvidence: 0, drifted: []});
81+
});
82+
83+
it('separates changed, missing and malformed receipts', () => {
84+
const root = checkout(files);
85+
const drift = receiptDrift({
86+
projectRoot: root,
87+
verifiedNa: [
88+
{
89+
component: 'core/Dot',
90+
reason: 'r',
91+
evidence: {
92+
'packages/core/src/Dot/Dot.tsx': receiptOf('older source'),
93+
'packages/core/src/Dot/index.ts': receiptOf('index'),
94+
'apps/storybook/stories/Dot.stories.tsx': 'sha256:short',
95+
'../outside.tsx': receiptOf('x'),
96+
},
97+
},
98+
{component: 'core/Plain', reason: 'r'},
99+
{component: 'core/Odd', reason: 'r', evidence: ['not', 'a', 'map']},
100+
],
101+
});
102+
expect(drift.checked).toBe(2);
103+
expect(drift.withoutEvidence).toBe(1);
104+
expect(drift.drifted).toEqual([
105+
{
106+
component: 'core/Dot',
107+
changed: ['packages/core/src/Dot/Dot.tsx'],
108+
missing: ['packages/core/src/Dot/index.ts'],
109+
malformed: ['apps/storybook/stories/Dot.stories.tsx', '../outside.tsx'],
110+
},
111+
{
112+
component: 'core/Odd',
113+
changed: [],
114+
missing: [],
115+
malformed: ['evidence is not a path-to-receipt map'],
116+
},
117+
]);
118+
});
119+
120+
it('checks only the components in scope, case-insensitively', () => {
121+
const root = checkout(files);
122+
const verifiedNa = [
123+
{component: 'core/Dot', reason: 'r', evidence: {'a.tsx': receiptOf('a')}},
124+
{
125+
component: 'core/Other',
126+
reason: 'r',
127+
evidence: {'b.tsx': receiptOf('b')},
128+
},
129+
];
130+
const drift = receiptDrift({
131+
projectRoot: root,
132+
verifiedNa,
133+
components: ['core/DOT'],
134+
});
135+
expect(drift.checked).toBe(1);
136+
expect(drift.drifted.map(entry => entry.component)).toEqual(['core/Dot']);
137+
});
138+
139+
it('reports a malformed receipt without changing coverage status', () => {
140+
const root = checkout(files);
141+
const verifiedNa = [
142+
{
143+
component: 'core/Dot',
144+
reason: 'One clipped element with no direction-sensitive behavior.',
145+
evidence: {'packages/core/src/Dot/Dot.tsx': 'not-a-receipt'},
146+
},
147+
];
148+
const coverage = buildComponentCoverage({
149+
components: ['core/Dot'],
150+
verifiedNa,
151+
});
152+
const drift = receiptDrift({
153+
projectRoot: root,
154+
verifiedNa,
155+
components: ['core/Dot'],
156+
});
157+
expect(coverage.results[0].status).toBe('verified-na');
158+
expect(drift.drifted).toEqual([
159+
{
160+
component: 'core/Dot',
161+
changed: [],
162+
missing: [],
163+
malformed: ['packages/core/src/Dot/Dot.tsx'],
164+
},
165+
]);
166+
});
167+
});
168+
169+
describe('receiptDrift on an unreadable registry shape', () => {
170+
it('skips entries it cannot read instead of throwing', () => {
171+
const root = checkout({});
172+
for (const verifiedNa of [null, {}, [null, 5, {component: 7}]]) {
173+
expect(receiptDrift({projectRoot: root, verifiedNa})).toEqual({
174+
checked: 0,
175+
withoutEvidence: 0,
176+
drifted: [],
177+
});
178+
}
179+
});
180+
});
181+
182+
describe('RTL scorecard receipt drift section', () => {
183+
const SCRIPT = path.resolve('.github/scripts/rtl-scorecard-summary.mjs');
184+
185+
function summarize(receiptDriftSection) {
186+
const dir = checkout({});
187+
const reportPath = path.join(dir, 'report.json');
188+
fs.writeFileSync(
189+
reportPath,
190+
JSON.stringify({
191+
autoDiscovery: {
192+
total: 0,
193+
pass: 0,
194+
fail: 0,
195+
na: 0,
196+
surprises: [],
197+
results: [],
198+
},
199+
curated: {results: []},
200+
receiptDrift: receiptDriftSection,
201+
}),
202+
);
203+
const result = spawnSync(
204+
process.execPath,
205+
[SCRIPT, '--report', reportPath, '--scope', 'core/Dot'],
206+
{encoding: 'utf8'},
207+
);
208+
expect(result.status).toBe(0);
209+
return result.stdout;
210+
}
211+
212+
it('lists drifted declarations without changing coverage', () => {
213+
const out = summarize({
214+
checked: 2,
215+
withoutEvidence: 0,
216+
drifted: [
217+
{
218+
component: 'core/Dot',
219+
changed: ['packages/core/src/Dot/Dot.tsx'],
220+
missing: [],
221+
malformed: [],
222+
},
223+
],
224+
});
225+
expect(out).toContain('### Receipt drift (informational)');
226+
expect(out).toContain('1 of 2 receipted verified-N/A declarations');
227+
expect(out).toContain('| core/Dot | packages/core/src/Dot/Dot.tsx | | |');
228+
});
229+
230+
it('omits the section when nothing drifted', () => {
231+
expect(
232+
summarize({checked: 1, withoutEvidence: 0, drifted: []}),
233+
).not.toContain('Receipt drift');
234+
});
235+
});

‎apps/storybook/rtl-audit/README.md‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,19 @@ Add a declaration only after reading the component's source and every story:
321321
An all-N/A scorecard without such a checked-in reason means **unmeasured**, not
322322
RTL-ready.
323323

324+
### Receipts and receipt drift
325+
326+
A declaration may also carry `evidence`: each repository file the reason was
327+
reviewed against, mapped to the `sha256:` of its bytes at that review. The
328+
audit report's `receiptDrift` section, and the scorecard's **Receipt drift
329+
(informational)** table, list every in-scope declaration whose recorded files
330+
changed, went missing, or carry a malformed receipt. Drift changes no coverage
331+
status and fails nothing: it tells a reviewer to re-read the component's source
332+
and stories and refresh the receipts.
333+
334+
`pnpm rtl:audit:verify-na` prints the same drift for the whole registry without
335+
a Storybook build; `--component <package/Name>` checks one declaration.
336+
324337
## Why relationship-based, not pixel baselines
325338

326339
Directional RTL bugs are _semantic_ ("the chevron didn't mirror", "prev/next

‎apps/storybook/rtl-audit/rtl-audit.mjs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ import {
8282
validateStoryRtlAuditParameters,
8383
withD1CompletionIdentity,
8484
} from './rtl-audit-coverage.mjs';
85+
import {receiptDrift} from './verified-na-receipts.mjs';
8586
import {
8687
createCuratedPages,
8788
createPagePool,
@@ -1438,20 +1439,23 @@ async function cleanupRuntime() {
14381439
const {verifiedNa, verifiedNaError} = readVerifiedNa();
14391440
const coverage = buildComponentCoverage({components: auditedComponents, verifiedNa});
14401441
if (verifiedNaError) coverage.registryError = verifiedNaError;
1442+
const drift = receiptDrift({verifiedNa, projectRoot: PROJECT_ROOT, components: auditedComponents});
14411443
fs.writeFileSync(
14421444
OUT,
14431445
`${JSON.stringify(
14441446
{
14451447
generatedAt: new Date().toISOString(),
14461448
scope: {packages: ACTIVE_PACKAGE_NAMES, filters: FILTER},
14471449
coverage,
1450+
receiptDrift: drift,
14481451
},
14491452
null,
14501453
2,
14511454
)}\n`,
14521455
);
14531456
console.error(`NOTE no stories for ${FILTER.join(',')}; WROTE ${OUT} with coverage only`);
14541457
console.error(`COV : ${coverage.measured} measured / ${coverage.verifiedNa} verified N-A / ${coverage.gaps} gap / ${coverage.staleVerifiedNa} stale`);
1458+
console.error(`RCPT: ${drift.drifted.length} of ${drift.checked} receipted declarations drifted (informational)`);
14551459
process.exitCode =
14561460
coverage.gaps > 0 || coverage.staleVerifiedNa > 0 || coverage.registryError != null ? 1 : 0;
14571461
return;
@@ -1675,6 +1679,8 @@ async function cleanupRuntime() {
16751679
enforced: !AUTO_ONLY && !CURATED_ONLY,
16761680
});
16771681
if (verifiedNaError) coverage.registryError = verifiedNaError;
1682+
// Receipt drift is reported beside coverage and never changes its statuses.
1683+
const drift = receiptDrift({verifiedNa, projectRoot: PROJECT_ROOT, components: auditedComponents});
16781684
// Story identities with no public component are not roster entries (INV5);
16791685
// name them so the report shows what the roster left out.
16801686
coverage.ignoredStoryIdentities = ignoredStoryIdentities({
@@ -1771,6 +1777,7 @@ async function cleanupRuntime() {
17711777
},
17721778
curated: {results: curatedResults},
17731779
coverage,
1780+
receiptDrift: drift,
17741781
};
17751782
fs.writeFileSync(OUT, JSON.stringify(report, null, 2));
17761783
console.error(`\nWROTE ${OUT}`);
@@ -1780,6 +1787,7 @@ async function cleanupRuntime() {
17801787
console.error(`POOL: ${pageRecoveries.length} page replacement(s) after a failed story`);
17811788
console.error(retriesLine(storyRetries));
17821789
console.error(`COV : ${coverage.measured} measured / ${coverage.verifiedNa} verified N-A / ${coverage.gaps} gap / ${coverage.staleVerifiedNa} stale`);
1790+
console.error(`RCPT: ${drift.drifted.length} of ${drift.checked} receipted declarations drifted (informational)`);
17831791
if (coverage.ignoredStoryIdentities.length > 0) console.error(`IGN : ${coverage.ignoredStoryIdentities.length} story identities name no public component (not roster entries): ${coverage.ignoredStoryIdentities.map(entry => entry.component).join(', ')}`);
17841792
if (declaredD5PhysicalRegions.length > 0) console.error(`D5P : ${declaredD5PhysicalRegions.length} declared physical region(s): ${declaredD5PhysicalRegions.map(r => `${r.storyId} (${r.physical} within ${r.within})`).join('; ')}`);
17851793
// Non-zero exit only signals CI (which is soft/continue-on-error). Surface a

0 commit comments

Comments
 (0)