Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .github/scripts/rtl-scorecard-summary.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,24 @@ if (!fs.existsSync(reportPath)) {
out.push('');
}
}
// Receipt drift is informational: it lists receipts whose recorded bytes no
// longer match the checkout, and changes no coverage status.
const drift = r.receiptDrift;
if (drift && drift.drifted.length > 0) {
out.push('### Receipt drift (informational)');
out.push('');
out.push(
`${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.`,
);
out.push('');
out.push('| Component | Changed | Missing | Malformed |');
out.push('|-----------|---------|---------|-----------|');
for (const entry of drift.drifted)
out.push(
`| ${markdownCell(entry.component)} | ${markdownCell(entry.changed.join(', '))} | ${markdownCell(entry.missing.join(', '))} | ${markdownCell(entry.malformed.join(', '))} |`,
);
out.push('');
}
// Stories whose scan threw and ran once more on a fresh page. The last
// line is the one a log search counts across runs.
const retried = r.retries?.stories ?? [];
Expand Down
235 changes: 235 additions & 0 deletions .github/scripts/rtl-verified-na-receipts.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
// Copyright (c) Meta Platforms, Inc. and affiliates.

/**
* @file Unit tests for RTL verified-N/A receipt drift.
*/

import crypto from 'node:crypto';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import {spawnSync} from 'node:child_process';
import {afterEach, describe, expect, it} from 'vitest';
import {buildComponentCoverage} from '../../apps/storybook/rtl-audit/rtl-audit-coverage.mjs';
import {
fileReceipt,
receiptDrift,
} from '../../apps/storybook/rtl-audit/verified-na-receipts.mjs';

const dirs = [];

afterEach(() => {
for (const dir of dirs.splice(0)) {
fs.rmSync(dir, {recursive: true, force: true});
}
});

function checkout(files) {
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'rtl-receipts-'));
dirs.push(root);
for (const [file, content] of Object.entries(files)) {
fs.mkdirSync(path.dirname(path.join(root, file)), {recursive: true});
fs.writeFileSync(path.join(root, file), content);
}
return root;
}

const receiptOf = content =>
`sha256:${crypto.createHash('sha256').update(content).digest('hex')}`;

describe('fileReceipt', () => {
it('hashes a file, and answers null for a missing one', () => {
const root = checkout({'packages/core/src/Dot/Dot.tsx': 'dot'});
expect(fileReceipt(root, 'packages/core/src/Dot/Dot.tsx')).toBe(
receiptOf('dot'),
);
expect(fileReceipt(root, 'packages/core/src/Dot/Gone.tsx')).toBeNull();
});

it('refuses a path that leaves the repository', () => {
const root = checkout({'a.txt': 'a'});
expect(() => fileReceipt(root, '../outside.txt')).toThrow(
/leaves the repository/,
);
});
});

describe('receiptDrift', () => {
const files = {
'packages/core/src/Dot/Dot.tsx': 'dot source',
'apps/storybook/stories/Dot.stories.tsx': 'dot stories',
};

it('reports nothing when every receipt matches', () => {
const root = checkout(files);
const drift = receiptDrift({
projectRoot: root,
verifiedNa: [
{
component: 'core/Dot',
reason: 'r',
evidence: Object.fromEntries(
Object.entries(files).map(([file, content]) => [
file,
receiptOf(content),
]),
),
},
],
});
expect(drift).toEqual({checked: 1, withoutEvidence: 0, drifted: []});
});

it('separates changed, missing and malformed receipts', () => {
const root = checkout(files);
const drift = receiptDrift({
projectRoot: root,
verifiedNa: [
{
component: 'core/Dot',
reason: 'r',
evidence: {
'packages/core/src/Dot/Dot.tsx': receiptOf('older source'),
'packages/core/src/Dot/index.ts': receiptOf('index'),
'apps/storybook/stories/Dot.stories.tsx': 'sha256:short',
'../outside.tsx': receiptOf('x'),
},
},
{component: 'core/Plain', reason: 'r'},
{component: 'core/Odd', reason: 'r', evidence: ['not', 'a', 'map']},
],
});
expect(drift.checked).toBe(2);
expect(drift.withoutEvidence).toBe(1);
expect(drift.drifted).toEqual([
{
component: 'core/Dot',
changed: ['packages/core/src/Dot/Dot.tsx'],
missing: ['packages/core/src/Dot/index.ts'],
malformed: ['apps/storybook/stories/Dot.stories.tsx', '../outside.tsx'],
},
{
component: 'core/Odd',
changed: [],
missing: [],
malformed: ['evidence is not a path-to-receipt map'],
},
]);
});

it('checks only the components in scope, case-insensitively', () => {
const root = checkout(files);
const verifiedNa = [
{component: 'core/Dot', reason: 'r', evidence: {'a.tsx': receiptOf('a')}},
{
component: 'core/Other',
reason: 'r',
evidence: {'b.tsx': receiptOf('b')},
},
];
const drift = receiptDrift({
projectRoot: root,
verifiedNa,
components: ['core/DOT'],
});
expect(drift.checked).toBe(1);
expect(drift.drifted.map(entry => entry.component)).toEqual(['core/Dot']);
});

it('reports a malformed receipt without changing coverage status', () => {
const root = checkout(files);
const verifiedNa = [
{
component: 'core/Dot',
reason: 'One clipped element with no direction-sensitive behavior.',
evidence: {'packages/core/src/Dot/Dot.tsx': 'not-a-receipt'},
},
];
const coverage = buildComponentCoverage({
components: ['core/Dot'],
verifiedNa,
});
const drift = receiptDrift({
projectRoot: root,
verifiedNa,
components: ['core/Dot'],
});
expect(coverage.results[0].status).toBe('verified-na');
expect(drift.drifted).toEqual([
{
component: 'core/Dot',
changed: [],
missing: [],
malformed: ['packages/core/src/Dot/Dot.tsx'],
},
]);
});
});

describe('receiptDrift on an unreadable registry shape', () => {
it('skips entries it cannot read instead of throwing', () => {
const root = checkout({});
for (const verifiedNa of [null, {}, [null, 5, {component: 7}]]) {
expect(receiptDrift({projectRoot: root, verifiedNa})).toEqual({
checked: 0,
withoutEvidence: 0,
drifted: [],
});
}
});
});

describe('RTL scorecard receipt drift section', () => {
const SCRIPT = path.resolve('.github/scripts/rtl-scorecard-summary.mjs');

function summarize(receiptDriftSection) {
const dir = checkout({});
const reportPath = path.join(dir, 'report.json');
fs.writeFileSync(
reportPath,
JSON.stringify({
autoDiscovery: {
total: 0,
pass: 0,
fail: 0,
na: 0,
surprises: [],
results: [],
},
curated: {results: []},
receiptDrift: receiptDriftSection,
}),
);
const result = spawnSync(
process.execPath,
[SCRIPT, '--report', reportPath, '--scope', 'core/Dot'],
{encoding: 'utf8'},
);
expect(result.status).toBe(0);
return result.stdout;
}

it('lists drifted declarations without changing coverage', () => {
const out = summarize({
checked: 2,
withoutEvidence: 0,
drifted: [
{
component: 'core/Dot',
changed: ['packages/core/src/Dot/Dot.tsx'],
missing: [],
malformed: [],
},
],
});
expect(out).toContain('### Receipt drift (informational)');
expect(out).toContain('1 of 2 receipted verified-N/A declarations');
expect(out).toContain('| core/Dot | packages/core/src/Dot/Dot.tsx | | |');
});

it('omits the section when nothing drifted', () => {
expect(
summarize({checked: 1, withoutEvidence: 0, drifted: []}),
).not.toContain('Receipt drift');
});
});
13 changes: 13 additions & 0 deletions apps/storybook/rtl-audit/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,19 @@ Add a declaration only after reading the component's source and every story:
An all-N/A scorecard without such a checked-in reason means **unmeasured**, not
RTL-ready.

### Receipts and receipt drift

A declaration may also carry `evidence`: each repository file the reason was
reviewed against, mapped to the `sha256:` of its bytes at that review. The
audit report's `receiptDrift` section, and the scorecard's **Receipt drift
(informational)** table, list every in-scope declaration whose recorded files
changed, went missing, or carry a malformed receipt. Drift changes no coverage
status and fails nothing: it tells a reviewer to re-read the component's source
and stories and refresh the receipts.

`pnpm rtl:audit:verify-na` prints the same drift for the whole registry without
a Storybook build; `--component <package/Name>` checks one declaration.

## Why relationship-based, not pixel baselines

Directional RTL bugs are _semantic_ ("the chevron didn't mirror", "prev/next
Expand Down
8 changes: 8 additions & 0 deletions apps/storybook/rtl-audit/rtl-audit.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ import {
validateStoryRtlAuditParameters,
withD1CompletionIdentity,
} from './rtl-audit-coverage.mjs';
import {receiptDrift} from './verified-na-receipts.mjs';
import {
createCuratedPages,
createPagePool,
Expand Down Expand Up @@ -1438,20 +1439,23 @@ async function cleanupRuntime() {
const {verifiedNa, verifiedNaError} = readVerifiedNa();
const coverage = buildComponentCoverage({components: auditedComponents, verifiedNa});
if (verifiedNaError) coverage.registryError = verifiedNaError;
const drift = receiptDrift({verifiedNa, projectRoot: PROJECT_ROOT, components: auditedComponents});
fs.writeFileSync(
OUT,
`${JSON.stringify(
{
generatedAt: new Date().toISOString(),
scope: {packages: ACTIVE_PACKAGE_NAMES, filters: FILTER},
coverage,
receiptDrift: drift,
},
null,
2,
)}\n`,
);
console.error(`NOTE no stories for ${FILTER.join(',')}; WROTE ${OUT} with coverage only`);
console.error(`COV : ${coverage.measured} measured / ${coverage.verifiedNa} verified N-A / ${coverage.gaps} gap / ${coverage.staleVerifiedNa} stale`);
console.error(`RCPT: ${drift.drifted.length} of ${drift.checked} receipted declarations drifted (informational)`);
process.exitCode =
coverage.gaps > 0 || coverage.staleVerifiedNa > 0 || coverage.registryError != null ? 1 : 0;
return;
Expand Down Expand Up @@ -1675,6 +1679,8 @@ async function cleanupRuntime() {
enforced: !AUTO_ONLY && !CURATED_ONLY,
});
if (verifiedNaError) coverage.registryError = verifiedNaError;
// Receipt drift is reported beside coverage and never changes its statuses.
const drift = receiptDrift({verifiedNa, projectRoot: PROJECT_ROOT, components: auditedComponents});
// Story identities with no public component are not roster entries (INV5);
// name them so the report shows what the roster left out.
coverage.ignoredStoryIdentities = ignoredStoryIdentities({
Expand Down Expand Up @@ -1771,6 +1777,7 @@ async function cleanupRuntime() {
},
curated: {results: curatedResults},
coverage,
receiptDrift: drift,
};
fs.writeFileSync(OUT, JSON.stringify(report, null, 2));
console.error(`\nWROTE ${OUT}`);
Expand All @@ -1780,6 +1787,7 @@ async function cleanupRuntime() {
console.error(`POOL: ${pageRecoveries.length} page replacement(s) after a failed story`);
console.error(retriesLine(storyRetries));
console.error(`COV : ${coverage.measured} measured / ${coverage.verifiedNa} verified N-A / ${coverage.gaps} gap / ${coverage.staleVerifiedNa} stale`);
console.error(`RCPT: ${drift.drifted.length} of ${drift.checked} receipted declarations drifted (informational)`);
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(', ')}`);
if (declaredD5PhysicalRegions.length > 0) console.error(`D5P : ${declaredD5PhysicalRegions.length} declared physical region(s): ${declaredD5PhysicalRegions.map(r => `${r.storyId} (${r.physical} within ${r.within})`).join('; ')}`);
// Non-zero exit only signals CI (which is soft/continue-on-error). Surface a
Expand Down
Loading
Loading