Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
50 changes: 41 additions & 9 deletions .github/scripts/chrome-macos-stills.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

/**
* @file chrome-macos-stills.mjs
* @input The dispatch's cell list (CELLS, JSON), a built Storybook, and the
* Playwright install of the checkout under test
* @input The dispatch's cell list (CELLS, JSON), a built Storybook and its
* story index, and the Playwright install of the checkout under test
* @output `cells`: the checked cells with their keys; `capture`: one PNG per
* cell from branded Chrome (Playwright's `chrome` channel) and the Chrome
* version; `report`: a Markdown and JSON record of the head, the base, macOS,
Expand Down Expand Up @@ -114,6 +114,26 @@ export function parseCells(text) {
});
}

/** Resolves the dispatch ID against the stories in this Storybook build. */
export function resolveStory(id, storyIds) {
if (storyIds.has(id)) return id;
// Storybook IDs can omit the word breaks in a component name.
const compact = value => {
const separator = value.indexOf('--');
return (
value.slice(0, separator).replaceAll('-', '') + value.slice(separator)
);
};
const requested = compact(id);
const matches = [...storyIds].filter(story => compact(story) === requested);
if (matches.length === 1) return matches[0];
throw new Error(
matches.length > 1
? `Ambiguous Storybook story "${id}"; use an exact ID: ${matches.join(', ')}`
: `Storybook story "${id}" is not in this build's index.json`,
);
}

/** The story URL (path and query) for one cell. */
export function storyPath(cell) {
const globals = [
Expand Down Expand Up @@ -166,6 +186,12 @@ export function launchOptions() {

/** Screenshots every cell from one built Storybook; returns per-cell results and the Chrome version. */
async function capture({dist, cells, out}) {
const {entries} = JSON.parse(
fs.readFileSync(path.join(dist, 'index.json'), 'utf8'),
);
const storyIds = new Set(
Object.keys(entries).filter(id => entries[id].type === 'story'),
);
const {chromium} = createRequire(path.join(process.cwd(), 'package.json'))(
'playwright',
);
Expand All @@ -176,28 +202,34 @@ async function capture({dist, cells, out}) {
const results = [];
try {
for (const cell of cells) {
const page = await browser.newPage({
viewport: {width: cell.width, height: cell.height},
reducedMotion: 'reduce',
});
let page;
try {
await page.goto(origin + storyPath(cell), {
const story = resolveStory(cell.story, storyIds);
page = await browser.newPage({
viewport: {width: cell.width, height: cell.height},
reducedMotion: 'reduce',
});
await page.goto(origin + storyPath({...cell, story}), {
waitUntil: 'networkidle',
timeout: 60_000,
});
await page.waitForSelector('#storybook-root > *', {timeout: 30_000});
await page.evaluate(() => document.fonts.ready);
await page.waitForTimeout(300);
await page.screenshot({path: path.join(out, `${cell.key}.png`)});
results.push({key: cell.key, result: 'captured'});
results.push({
key: cell.key,
result: 'captured',
...(story !== cell.story && {story}),
});
} catch (error) {
results.push({
key: cell.key,
result: 'failed',
reason: String(error.message ?? error).split('\n')[0],
});
} finally {
await page.close();
await page?.close();
}
}
return {chrome: browser.version(), results};
Expand Down
32 changes: 32 additions & 0 deletions .github/scripts/chrome-macos-stills.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
TITLE,
parseCells,
renderReport,
resolveStory,
storyPath,
} from './chrome-macos-stills.mjs';
import {publishStills, stillsToPublish} from './publish-stills.mjs';
Expand Down Expand Up @@ -220,6 +221,37 @@ describe('parseCells', () => {
});
});

describe('resolveStory', () => {
const ids = new Set([
'core-button--touch-targets',
'core-segmentedcontrol--default',
'core-textinput--default',
]);

it.each([
['core-segmented-control--default', 'core-segmentedcontrol--default'],
['core-text-input--default', 'core-textinput--default'],
['core-button--touch-targets', 'core-button--touch-targets'],
])('resolves %s to %s', (requested, indexed) => {
expect(resolveStory(requested, ids)).toBe(indexed);
});

it('prefers an exact ID and rejects a fallback with multiple matches', () => {
const exact = 'core-text-input--default';
const variants = new Set([...ids, exact]);
expect(resolveStory(exact, variants)).toBe(exact);
expect(() => resolveStory('core-text-in-put--default', variants)).toThrow(
'Ambiguous Storybook story',
);
});

it('does not substitute a different story variant', () => {
expect(() => resolveStory('core-button--touchtargets', ids)).toThrow(
'is not in this build',
);
});
});

describe('publish-stills', () => {
const fixture = () => {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'stills-'));
Expand Down
Loading