|
| 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 | +}); |
0 commit comments