@storysnap/cli 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +297 -0
- package/dist/api/client.js +156 -0
- package/dist/cli/commands/init.js +55 -0
- package/dist/cli/commands/login.js +114 -0
- package/dist/cli/commands/logout.js +36 -0
- package/dist/cli/commands/test.js +366 -0
- package/dist/cli/commands/update.js +229 -0
- package/dist/cli/index.js +93 -0
- package/dist/cli/reporter.js +140 -0
- package/dist/config/credentials.js +69 -0
- package/dist/config/loader.js +102 -0
- package/dist/config/schema.js +59 -0
- package/dist/core/compare.js +139 -0
- package/dist/core/screenshot.js +123 -0
- package/dist/core/storybook.js +124 -0
- package/dist/core/types.js +2 -0
- package/dist/storage/index.js +15 -0
- package/dist/storage/local.js +64 -0
- package/dist/storage/s3.js +109 -0
- package/package.json +79 -0
|
@@ -0,0 +1,366 @@
|
|
|
1
|
+
import { loadConfig } from '../../config/loader.js';
|
|
2
|
+
import { loadCredentials } from '../../config/credentials.js';
|
|
3
|
+
import { discoverStories } from '../../core/storybook.js';
|
|
4
|
+
import { captureScreenshots, getScreenshotKey, parseScreenshotKey } from '../../core/screenshot.js';
|
|
5
|
+
import { compareAllScreenshots, DEFAULT_MAX_DIFF_PERCENT } from '../../core/compare.js';
|
|
6
|
+
import { createStorage } from '../../storage/index.js';
|
|
7
|
+
import { createReporter } from '../reporter.js';
|
|
8
|
+
import { ApiClient } from '../../api/client.js';
|
|
9
|
+
function parseBrowsers(browserArg) {
|
|
10
|
+
if (!browserArg) {
|
|
11
|
+
return undefined;
|
|
12
|
+
}
|
|
13
|
+
const validBrowsers = ['chromium', 'firefox', 'webkit'];
|
|
14
|
+
const browsers = browserArg.split(',').map(b => b.trim().toLowerCase());
|
|
15
|
+
const result = [];
|
|
16
|
+
for (const b of browsers) {
|
|
17
|
+
if (!validBrowsers.includes(b)) {
|
|
18
|
+
throw new Error(`Invalid browser: ${b}. Valid options: ${validBrowsers.join(', ')}`);
|
|
19
|
+
}
|
|
20
|
+
result.push(b);
|
|
21
|
+
}
|
|
22
|
+
return result;
|
|
23
|
+
}
|
|
24
|
+
export async function testCommand(options = {}) {
|
|
25
|
+
const reporter = createReporter({ verbose: options.verbose });
|
|
26
|
+
try {
|
|
27
|
+
reporter.header('StorySnap Visual Regression Test');
|
|
28
|
+
// Load config
|
|
29
|
+
reporter.startSpinner('Loading configuration...');
|
|
30
|
+
const config = await loadConfig(options.config);
|
|
31
|
+
// Override browsers if specified via CLI
|
|
32
|
+
const cliOverrideBrowsers = parseBrowsers(options.browser);
|
|
33
|
+
if (cliOverrideBrowsers) {
|
|
34
|
+
config.browsers = cliOverrideBrowsers;
|
|
35
|
+
}
|
|
36
|
+
// CI pipelines pick a port at run time, so the URL has to be overridable
|
|
37
|
+
// without rewriting the config file.
|
|
38
|
+
if (options.storybookUrl) {
|
|
39
|
+
config.storybookUrl = options.storybookUrl;
|
|
40
|
+
config.storybookStaticDir = undefined;
|
|
41
|
+
}
|
|
42
|
+
reporter.succeedSpinner('Configuration loaded');
|
|
43
|
+
// Determine if we should use API mode
|
|
44
|
+
const credentials = await loadCredentials();
|
|
45
|
+
const apiUrl = config.apiUrl || credentials?.apiUrl;
|
|
46
|
+
const apiToken = config.apiToken || credentials?.apiToken;
|
|
47
|
+
const useApi = !options.local && apiUrl && apiToken;
|
|
48
|
+
if (useApi) {
|
|
49
|
+
await runApiMode(reporter, config, apiUrl, apiToken, options);
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
await runLocalMode(reporter, config, options);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
catch (error) {
|
|
56
|
+
reporter.stopSpinner();
|
|
57
|
+
reporter.error(error instanceof Error ? error.message : 'Unknown error');
|
|
58
|
+
process.exit(1);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
async function runApiMode(reporter, config, apiUrl, apiToken, options) {
|
|
62
|
+
reporter.info(`Running in API mode (${apiUrl})`);
|
|
63
|
+
reporter.info(`Browsers: ${config.browsers.join(', ')}`);
|
|
64
|
+
reporter.newLine();
|
|
65
|
+
const client = new ApiClient({ apiUrl, apiToken });
|
|
66
|
+
// Apply story filter if provided
|
|
67
|
+
if (options.story) {
|
|
68
|
+
config.include = [options.story];
|
|
69
|
+
}
|
|
70
|
+
// Screenshots are captured here, not by the server: the worker has no route
|
|
71
|
+
// to a Storybook running on this machine or on a CI runner.
|
|
72
|
+
reporter.startSpinner('Discovering stories...');
|
|
73
|
+
const stories = await discoverStories(config);
|
|
74
|
+
if (stories.length === 0) {
|
|
75
|
+
reporter.failSpinner('No stories found');
|
|
76
|
+
reporter.warning('Make sure Storybook is running or the static build exists.');
|
|
77
|
+
process.exit(1);
|
|
78
|
+
}
|
|
79
|
+
const totalTests = stories.length * config.browsers.length;
|
|
80
|
+
reporter.succeedSpinner(`Found ${reporter.formatStoryCount(stories.length)} (${totalTests} tests across ${config.browsers.length} browser(s))`);
|
|
81
|
+
reporter.startSpinner('Capturing screenshots...');
|
|
82
|
+
const screenshots = await captureScreenshots(config, stories, (completed, total, story, browser) => {
|
|
83
|
+
reporter.progress(completed, total, `${story.title}/${story.name} (${browser})`);
|
|
84
|
+
});
|
|
85
|
+
reporter.succeedSpinner('Screenshots captured');
|
|
86
|
+
reporter.startSpinner('Creating test run...');
|
|
87
|
+
// Only sent when the config actually declares them. Left undefined the server
|
|
88
|
+
// applies the project's own defaults, which is what makes `PATCH /projects/me`
|
|
89
|
+
// worth having; a config that does declare them overrides for this run.
|
|
90
|
+
const testRun = await client.createTestRun({
|
|
91
|
+
storybookUrl: config.storybookUrl,
|
|
92
|
+
branch: options.branch,
|
|
93
|
+
commit: options.commit,
|
|
94
|
+
threshold: config.threshold,
|
|
95
|
+
maxDiffPercent: config.maxDiffPercent,
|
|
96
|
+
});
|
|
97
|
+
reporter.succeedSpinner(`Test run created: ${testRun.id}`);
|
|
98
|
+
reporter.startSpinner('Uploading screenshots...');
|
|
99
|
+
const uploaded = await uploadScreenshots(client, testRun.id, config, stories, screenshots, reporter);
|
|
100
|
+
if (uploaded === 0) {
|
|
101
|
+
reporter.failSpinner('No screenshots could be uploaded');
|
|
102
|
+
process.exit(1);
|
|
103
|
+
}
|
|
104
|
+
reporter.succeedSpinner(uploaded === totalTests
|
|
105
|
+
? `Uploaded ${uploaded} screenshots`
|
|
106
|
+
: `Uploaded ${uploaded} of ${totalTests} screenshots`);
|
|
107
|
+
reporter.startSpinner('Queueing comparisons...');
|
|
108
|
+
await client.finalizeTestRun(testRun.id);
|
|
109
|
+
reporter.succeedSpinner('Comparisons queued');
|
|
110
|
+
// Wait for test run to complete
|
|
111
|
+
reporter.startSpinner('Running tests...');
|
|
112
|
+
let lastStatus = '';
|
|
113
|
+
const completedTestRun = await client.waitForTestRun(testRun.id, (run) => {
|
|
114
|
+
const status = `${run.passedStories + run.failedStories + run.newStories + run.errorStories}/${run.totalStories || '?'} stories`;
|
|
115
|
+
if (status !== lastStatus) {
|
|
116
|
+
lastStatus = status;
|
|
117
|
+
reporter.updateSpinner(`Running tests... ${status}`);
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
reporter.succeedSpinner('Tests complete');
|
|
121
|
+
// Report results
|
|
122
|
+
reportApiResults(reporter, completedTestRun, options);
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Registers each captured screenshot with the API and PUTs the PNG to the
|
|
126
|
+
* presigned URL it hands back. Returns how many made it.
|
|
127
|
+
*/
|
|
128
|
+
async function uploadScreenshots(client, testRunId, config, stories, screenshots, reporter) {
|
|
129
|
+
const total = stories.length * config.browsers.length;
|
|
130
|
+
let uploaded = 0;
|
|
131
|
+
let attempted = 0;
|
|
132
|
+
for (const story of stories) {
|
|
133
|
+
for (const browser of config.browsers) {
|
|
134
|
+
attempted++;
|
|
135
|
+
const screenshot = screenshots.get(getScreenshotKey(story.id, browser));
|
|
136
|
+
if (!screenshot || screenshot.buffer.length === 0) {
|
|
137
|
+
reporter.progress(attempted, total, `${story.title}/${story.name} (${browser}) - capture failed`);
|
|
138
|
+
continue;
|
|
139
|
+
}
|
|
140
|
+
reporter.progress(attempted, total, `${story.title}/${story.name} (${browser})`);
|
|
141
|
+
try {
|
|
142
|
+
const target = await client.createResult(testRunId, {
|
|
143
|
+
storyId: story.id,
|
|
144
|
+
storyTitle: story.title,
|
|
145
|
+
storyName: story.name,
|
|
146
|
+
browser,
|
|
147
|
+
viewportWidth: config.viewport.width,
|
|
148
|
+
viewportHeight: config.viewport.height,
|
|
149
|
+
});
|
|
150
|
+
await client.uploadImage(target.uploadUrl, screenshot.buffer);
|
|
151
|
+
uploaded++;
|
|
152
|
+
}
|
|
153
|
+
catch (error) {
|
|
154
|
+
reporter.warning(`Failed to upload ${story.id} (${browser}): ${error instanceof Error ? error.message : 'Unknown error'}`);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return uploaded;
|
|
159
|
+
}
|
|
160
|
+
async function runLocalMode(reporter, config, options) {
|
|
161
|
+
if (options.local) {
|
|
162
|
+
reporter.info('Running in local mode (--local flag)');
|
|
163
|
+
}
|
|
164
|
+
reporter.info(`Browsers: ${config.browsers.join(', ')}`);
|
|
165
|
+
reporter.newLine();
|
|
166
|
+
// Apply story filter if provided
|
|
167
|
+
if (options.story) {
|
|
168
|
+
config.include = [options.story];
|
|
169
|
+
}
|
|
170
|
+
// Create storage
|
|
171
|
+
const storage = createStorage(config.storage);
|
|
172
|
+
// Discover stories
|
|
173
|
+
reporter.startSpinner('Discovering stories...');
|
|
174
|
+
const stories = await discoverStories(config);
|
|
175
|
+
if (stories.length === 0) {
|
|
176
|
+
reporter.failSpinner('No stories found');
|
|
177
|
+
reporter.warning('Make sure Storybook is running or the static build exists.');
|
|
178
|
+
process.exit(1);
|
|
179
|
+
}
|
|
180
|
+
const totalTests = stories.length * config.browsers.length;
|
|
181
|
+
reporter.succeedSpinner(`Found ${reporter.formatStoryCount(stories.length)} (${totalTests} tests across ${config.browsers.length} browser(s))`);
|
|
182
|
+
// Capture screenshots
|
|
183
|
+
reporter.startSpinner('Capturing screenshots...');
|
|
184
|
+
const screenshots = await captureScreenshots(config, stories, (completed, total, story, browser) => {
|
|
185
|
+
reporter.progress(completed, total, `${story.title}/${story.name} (${browser})`);
|
|
186
|
+
});
|
|
187
|
+
reporter.succeedSpinner('Screenshots captured');
|
|
188
|
+
// Compare against baselines
|
|
189
|
+
reporter.startSpinner('Comparing against baselines...');
|
|
190
|
+
const results = await compareAllScreenshots(screenshots, {
|
|
191
|
+
threshold: config.threshold,
|
|
192
|
+
maxDiffPercent: config.maxDiffPercent,
|
|
193
|
+
storage,
|
|
194
|
+
localSnapshotsDir: config.localSnapshotsDir,
|
|
195
|
+
}, (completed, total, key) => {
|
|
196
|
+
const { storyId, browser } = parseScreenshotKey(key);
|
|
197
|
+
reporter.progress(completed, total, `${storyId} (${browser})`);
|
|
198
|
+
});
|
|
199
|
+
reporter.succeedSpinner('Comparison complete');
|
|
200
|
+
// Build summary
|
|
201
|
+
const summary = buildSummary(stories, config.browsers, results);
|
|
202
|
+
// Report results
|
|
203
|
+
reporter.header('Results');
|
|
204
|
+
for (const result of summary.results) {
|
|
205
|
+
reportLocalResult(result.story, result.comparison);
|
|
206
|
+
}
|
|
207
|
+
reporter.reportSummary(summary, config.maxDiffPercent ?? DEFAULT_MAX_DIFF_PERCENT);
|
|
208
|
+
// Exit with appropriate code
|
|
209
|
+
if (options.ci) {
|
|
210
|
+
// In CI mode, fail on any diffs or errors
|
|
211
|
+
if (summary.failed > 0 || summary.errors > 0 || summary.newStories > 0) {
|
|
212
|
+
process.exit(1);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
else {
|
|
216
|
+
// In normal mode, only fail on errors
|
|
217
|
+
if (summary.errors > 0) {
|
|
218
|
+
process.exit(1);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
function reportLocalResult(story, comparison) {
|
|
223
|
+
const storyName = `${story.title}/${story.name}`;
|
|
224
|
+
const browserSuffix = comparison.browser !== 'chromium' ? ` (${comparison.browser})` : '';
|
|
225
|
+
const displayName = `${storyName}${browserSuffix}`;
|
|
226
|
+
switch (comparison.status) {
|
|
227
|
+
case 'pass':
|
|
228
|
+
console.log('\x1b[32m ✓\x1b[0m', `\x1b[2m${displayName}\x1b[0m`);
|
|
229
|
+
break;
|
|
230
|
+
case 'fail':
|
|
231
|
+
console.log('\x1b[31m ✗\x1b[0m', displayName);
|
|
232
|
+
if (comparison.diffPercentage !== undefined) {
|
|
233
|
+
console.log(`\x1b[2m → ${comparison.diffPixels} pixels different (${comparison.diffPercentage.toFixed(2)}%)\x1b[0m`);
|
|
234
|
+
}
|
|
235
|
+
break;
|
|
236
|
+
case 'new':
|
|
237
|
+
console.log('\x1b[33m ○\x1b[0m', `\x1b[33m${displayName}\x1b[0m`, '\x1b[2m(new)\x1b[0m');
|
|
238
|
+
break;
|
|
239
|
+
case 'error':
|
|
240
|
+
console.log('\x1b[31m ✗\x1b[0m', displayName, '\x1b[31m(error)\x1b[0m');
|
|
241
|
+
if (comparison.errorMessage) {
|
|
242
|
+
console.log(`\x1b[2m → ${comparison.errorMessage}\x1b[0m`);
|
|
243
|
+
}
|
|
244
|
+
break;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
function reportApiResults(reporter, testRun, options) {
|
|
248
|
+
reporter.header('Results');
|
|
249
|
+
for (const result of testRun.results) {
|
|
250
|
+
const storyName = result.storyName || result.storyId;
|
|
251
|
+
const browserSuffix = result.browser && result.browser !== 'chromium' ? ` (${result.browser})` : '';
|
|
252
|
+
const displayName = `${storyName}${browserSuffix}`;
|
|
253
|
+
switch (result.status) {
|
|
254
|
+
case 'pass':
|
|
255
|
+
console.log('\x1b[32m ✓\x1b[0m', `\x1b[2m${displayName}\x1b[0m`);
|
|
256
|
+
// A pass that used up most of the allowance is worth seeing - otherwise
|
|
257
|
+
// a story can drift right up to the limit without anyone noticing.
|
|
258
|
+
if (result.diffPercentage) {
|
|
259
|
+
console.log(`\x1b[2m → ${result.diffPercentage.toFixed(3)}% differed, within the ${testRun.maxDiffPercent}% allowed\x1b[0m`);
|
|
260
|
+
}
|
|
261
|
+
break;
|
|
262
|
+
case 'fail':
|
|
263
|
+
console.log('\x1b[31m ✗\x1b[0m', displayName);
|
|
264
|
+
if (result.diffPercentage !== undefined) {
|
|
265
|
+
console.log(`\x1b[2m → ${result.diffPixels} pixels different (${result.diffPercentage.toFixed(3)}%), ` +
|
|
266
|
+
`over the ${testRun.maxDiffPercent}% allowed\x1b[0m`);
|
|
267
|
+
}
|
|
268
|
+
break;
|
|
269
|
+
case 'new':
|
|
270
|
+
console.log('\x1b[33m ○\x1b[0m', `\x1b[33m${displayName}\x1b[0m`, '\x1b[2m(new)\x1b[0m');
|
|
271
|
+
break;
|
|
272
|
+
case 'error':
|
|
273
|
+
console.log('\x1b[31m ✗\x1b[0m', displayName, '\x1b[31m(error)\x1b[0m');
|
|
274
|
+
if (result.errorMessage) {
|
|
275
|
+
console.log(`\x1b[2m → ${result.errorMessage}\x1b[0m`);
|
|
276
|
+
}
|
|
277
|
+
break;
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
reporter.newLine();
|
|
281
|
+
console.log('\x1b[1mSummary:\x1b[0m');
|
|
282
|
+
reporter.newLine();
|
|
283
|
+
// The run's own value, not the local config's - the server falls back to the
|
|
284
|
+
// project default when the config omits one, and that is what judged these
|
|
285
|
+
// results.
|
|
286
|
+
console.log(`\x1b[2m Tolerance: up to ${testRun.maxDiffPercent}% of pixels may differ\x1b[0m`);
|
|
287
|
+
reporter.newLine();
|
|
288
|
+
const parts = [];
|
|
289
|
+
if (testRun.passedStories > 0) {
|
|
290
|
+
parts.push(`\x1b[32m${testRun.passedStories} passed\x1b[0m`);
|
|
291
|
+
}
|
|
292
|
+
if (testRun.failedStories > 0) {
|
|
293
|
+
parts.push(`\x1b[31m${testRun.failedStories} failed\x1b[0m`);
|
|
294
|
+
}
|
|
295
|
+
if (testRun.newStories > 0) {
|
|
296
|
+
parts.push(`\x1b[33m${testRun.newStories} new\x1b[0m`);
|
|
297
|
+
}
|
|
298
|
+
if (testRun.errorStories > 0) {
|
|
299
|
+
parts.push(`\x1b[31m${testRun.errorStories} errors\x1b[0m`);
|
|
300
|
+
}
|
|
301
|
+
console.log(` ${parts.join(', ')} of ${testRun.totalStories} stories`);
|
|
302
|
+
reporter.newLine();
|
|
303
|
+
if (testRun.status === 'failed') {
|
|
304
|
+
console.log('\x1b[31mVisual regression tests failed.\x1b[0m', '\x1b[2mReview changes in the dashboard or run "storysnap update" to update baselines.\x1b[0m');
|
|
305
|
+
}
|
|
306
|
+
else {
|
|
307
|
+
console.log('\x1b[32mAll visual regression tests passed!\x1b[0m');
|
|
308
|
+
}
|
|
309
|
+
// Exit with appropriate code
|
|
310
|
+
if (options.ci) {
|
|
311
|
+
if (testRun.failedStories > 0 || testRun.errorStories > 0 || testRun.newStories > 0) {
|
|
312
|
+
process.exit(1);
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
else {
|
|
316
|
+
if (testRun.errorStories > 0) {
|
|
317
|
+
process.exit(1);
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
function buildSummary(stories, browsers, results) {
|
|
322
|
+
const summary = {
|
|
323
|
+
total: stories.length * browsers.length,
|
|
324
|
+
passed: 0,
|
|
325
|
+
failed: 0,
|
|
326
|
+
newStories: 0,
|
|
327
|
+
errors: 0,
|
|
328
|
+
results: [],
|
|
329
|
+
};
|
|
330
|
+
for (const story of stories) {
|
|
331
|
+
for (const browser of browsers) {
|
|
332
|
+
const key = getScreenshotKey(story.id, browser);
|
|
333
|
+
const comparison = results.get(key);
|
|
334
|
+
if (!comparison) {
|
|
335
|
+
summary.errors++;
|
|
336
|
+
summary.results.push({
|
|
337
|
+
story,
|
|
338
|
+
comparison: {
|
|
339
|
+
storyId: story.id,
|
|
340
|
+
browser,
|
|
341
|
+
status: 'error',
|
|
342
|
+
errorMessage: 'No comparison result',
|
|
343
|
+
},
|
|
344
|
+
});
|
|
345
|
+
continue;
|
|
346
|
+
}
|
|
347
|
+
switch (comparison.status) {
|
|
348
|
+
case 'pass':
|
|
349
|
+
summary.passed++;
|
|
350
|
+
break;
|
|
351
|
+
case 'fail':
|
|
352
|
+
summary.failed++;
|
|
353
|
+
break;
|
|
354
|
+
case 'new':
|
|
355
|
+
summary.newStories++;
|
|
356
|
+
break;
|
|
357
|
+
case 'error':
|
|
358
|
+
summary.errors++;
|
|
359
|
+
break;
|
|
360
|
+
}
|
|
361
|
+
summary.results.push({ story, comparison });
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
return summary;
|
|
365
|
+
}
|
|
366
|
+
//# sourceMappingURL=test.js.map
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
import { loadConfig } from '../../config/loader.js';
|
|
2
|
+
import { loadCredentials } from '../../config/credentials.js';
|
|
3
|
+
import { discoverStories, getStoryKey } from '../../core/storybook.js';
|
|
4
|
+
import { captureScreenshots, getScreenshotKey } from '../../core/screenshot.js';
|
|
5
|
+
import { createStorage } from '../../storage/index.js';
|
|
6
|
+
import { createReporter } from '../reporter.js';
|
|
7
|
+
import { ApiClient } from '../../api/client.js';
|
|
8
|
+
function parseBrowsers(browserArg) {
|
|
9
|
+
if (!browserArg) {
|
|
10
|
+
return undefined;
|
|
11
|
+
}
|
|
12
|
+
const validBrowsers = ['chromium', 'firefox', 'webkit'];
|
|
13
|
+
const browsers = browserArg.split(',').map(b => b.trim().toLowerCase());
|
|
14
|
+
const result = [];
|
|
15
|
+
for (const b of browsers) {
|
|
16
|
+
if (!validBrowsers.includes(b)) {
|
|
17
|
+
throw new Error(`Invalid browser: ${b}. Valid options: ${validBrowsers.join(', ')}`);
|
|
18
|
+
}
|
|
19
|
+
result.push(b);
|
|
20
|
+
}
|
|
21
|
+
return result;
|
|
22
|
+
}
|
|
23
|
+
export async function updateCommand(options = {}) {
|
|
24
|
+
const reporter = createReporter({ verbose: options.verbose });
|
|
25
|
+
try {
|
|
26
|
+
reporter.header('StorySnap Baseline Update');
|
|
27
|
+
if (options.dryRun) {
|
|
28
|
+
reporter.warning('Dry run mode - no changes will be made');
|
|
29
|
+
reporter.newLine();
|
|
30
|
+
}
|
|
31
|
+
// Load config
|
|
32
|
+
reporter.startSpinner('Loading configuration...');
|
|
33
|
+
const config = await loadConfig(options.config);
|
|
34
|
+
// Override browsers if specified via CLI
|
|
35
|
+
const cliOverrideBrowsers = parseBrowsers(options.browser);
|
|
36
|
+
if (cliOverrideBrowsers) {
|
|
37
|
+
config.browsers = cliOverrideBrowsers;
|
|
38
|
+
}
|
|
39
|
+
// CI pipelines pick a port at run time, so the URL has to be overridable
|
|
40
|
+
// without rewriting the config file.
|
|
41
|
+
if (options.storybookUrl) {
|
|
42
|
+
config.storybookUrl = options.storybookUrl;
|
|
43
|
+
config.storybookStaticDir = undefined;
|
|
44
|
+
}
|
|
45
|
+
reporter.succeedSpinner('Configuration loaded');
|
|
46
|
+
// Determine if we should use API mode
|
|
47
|
+
const credentials = await loadCredentials();
|
|
48
|
+
const apiUrl = config.apiUrl || credentials?.apiUrl;
|
|
49
|
+
const apiToken = config.apiToken || credentials?.apiToken;
|
|
50
|
+
const useApi = !options.local && apiUrl && apiToken;
|
|
51
|
+
if (useApi) {
|
|
52
|
+
await runApiMode(reporter, config, apiUrl, apiToken, options);
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
await runLocalMode(reporter, config, options);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
catch (error) {
|
|
59
|
+
reporter.stopSpinner();
|
|
60
|
+
reporter.error(error instanceof Error ? error.message : 'Unknown error');
|
|
61
|
+
process.exit(1);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
async function runApiMode(reporter, config, apiUrl, apiToken, options) {
|
|
65
|
+
reporter.info(`Running in API mode (${apiUrl})`);
|
|
66
|
+
reporter.info(`Browsers: ${config.browsers.join(', ')}`);
|
|
67
|
+
reporter.newLine();
|
|
68
|
+
const client = new ApiClient({ apiUrl, apiToken });
|
|
69
|
+
// Apply story filter if provided
|
|
70
|
+
if (options.story) {
|
|
71
|
+
config.include = [options.story];
|
|
72
|
+
}
|
|
73
|
+
// Discover stories
|
|
74
|
+
reporter.startSpinner('Discovering stories...');
|
|
75
|
+
const stories = await discoverStories(config);
|
|
76
|
+
if (stories.length === 0) {
|
|
77
|
+
reporter.failSpinner('No stories found');
|
|
78
|
+
reporter.warning('Make sure Storybook is running or the static build exists.');
|
|
79
|
+
process.exit(1);
|
|
80
|
+
}
|
|
81
|
+
const totalTests = stories.length * config.browsers.length;
|
|
82
|
+
reporter.succeedSpinner(`Found ${reporter.formatStoryCount(stories.length)} (${totalTests} baselines across ${config.browsers.length} browser(s))`);
|
|
83
|
+
// Capture screenshots
|
|
84
|
+
reporter.startSpinner('Capturing screenshots...');
|
|
85
|
+
const screenshots = await captureScreenshots(config, stories, (completed, total, story, browser) => {
|
|
86
|
+
reporter.progress(completed, total, `${story.title}/${story.name} (${browser})`);
|
|
87
|
+
});
|
|
88
|
+
reporter.succeedSpinner('Screenshots captured');
|
|
89
|
+
// Upload baselines via API
|
|
90
|
+
reporter.startSpinner('Uploading baselines via API...');
|
|
91
|
+
let uploaded = 0;
|
|
92
|
+
let failed = 0;
|
|
93
|
+
for (const story of stories) {
|
|
94
|
+
for (const browser of config.browsers) {
|
|
95
|
+
const screenshotKey = getScreenshotKey(story.id, browser);
|
|
96
|
+
const screenshot = screenshots.get(screenshotKey);
|
|
97
|
+
if (!screenshot || screenshot.buffer.length === 0) {
|
|
98
|
+
reporter.updateSpinner(`Skipping ${story.title}/${story.name} (${browser}) (capture failed)`);
|
|
99
|
+
failed++;
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
reporter.progress(uploaded + failed + 1, totalTests, `${story.title}/${story.name} (${browser})`);
|
|
103
|
+
if (!options.dryRun) {
|
|
104
|
+
try {
|
|
105
|
+
// Get presigned upload URL from API
|
|
106
|
+
const uploadInfo = await client.getBaselineUploadUrl(story.id, browser, config.viewport.width, config.viewport.height);
|
|
107
|
+
// Upload directly to S3 using presigned URL
|
|
108
|
+
await client.uploadBaseline(uploadInfo.url, screenshot.buffer);
|
|
109
|
+
// Record the baseline. The upload alone leaves an orphaned object -
|
|
110
|
+
// comparisons look the baseline up in the database, not in S3.
|
|
111
|
+
await client.registerBaseline({
|
|
112
|
+
storyId: story.id,
|
|
113
|
+
browser,
|
|
114
|
+
viewportWidth: config.viewport.width,
|
|
115
|
+
viewportHeight: config.viewport.height,
|
|
116
|
+
storageKey: uploadInfo.storageKey,
|
|
117
|
+
});
|
|
118
|
+
uploaded++;
|
|
119
|
+
}
|
|
120
|
+
catch (error) {
|
|
121
|
+
failed++;
|
|
122
|
+
if (options.verbose) {
|
|
123
|
+
reporter.error(`Failed to upload ${story.id} (${browser}): ${error instanceof Error ? error.message : 'Unknown error'}`);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
uploaded++;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
if (uploaded === totalTests) {
|
|
133
|
+
reporter.succeedSpinner('Baselines uploaded');
|
|
134
|
+
}
|
|
135
|
+
else {
|
|
136
|
+
reporter.succeedSpinner(`Baselines uploaded (${failed} failed)`);
|
|
137
|
+
}
|
|
138
|
+
// Report summary
|
|
139
|
+
reporter.reportUpdateSummary(uploaded, totalTests);
|
|
140
|
+
if (failed > 0) {
|
|
141
|
+
reporter.warning(`${failed} baselines could not be updated`);
|
|
142
|
+
}
|
|
143
|
+
if (options.dryRun) {
|
|
144
|
+
reporter.info('Dry run complete. No changes were made.');
|
|
145
|
+
}
|
|
146
|
+
else {
|
|
147
|
+
reporter.success('Baselines have been updated successfully!');
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
async function runLocalMode(reporter, config, options) {
|
|
151
|
+
if (options.local) {
|
|
152
|
+
reporter.info('Running in local mode (--local flag)');
|
|
153
|
+
}
|
|
154
|
+
reporter.info(`Browsers: ${config.browsers.join(', ')}`);
|
|
155
|
+
reporter.newLine();
|
|
156
|
+
// Apply story filter if provided
|
|
157
|
+
if (options.story) {
|
|
158
|
+
config.include = [options.story];
|
|
159
|
+
}
|
|
160
|
+
// Create storage
|
|
161
|
+
const storage = createStorage(config.storage);
|
|
162
|
+
// Discover stories
|
|
163
|
+
reporter.startSpinner('Discovering stories...');
|
|
164
|
+
const stories = await discoverStories(config);
|
|
165
|
+
if (stories.length === 0) {
|
|
166
|
+
reporter.failSpinner('No stories found');
|
|
167
|
+
reporter.warning('Make sure Storybook is running or the static build exists.');
|
|
168
|
+
process.exit(1);
|
|
169
|
+
}
|
|
170
|
+
const totalTests = stories.length * config.browsers.length;
|
|
171
|
+
reporter.succeedSpinner(`Found ${reporter.formatStoryCount(stories.length)} (${totalTests} baselines across ${config.browsers.length} browser(s))`);
|
|
172
|
+
// Capture screenshots
|
|
173
|
+
reporter.startSpinner('Capturing screenshots...');
|
|
174
|
+
const screenshots = await captureScreenshots(config, stories, (completed, total, story, browser) => {
|
|
175
|
+
reporter.progress(completed, total, `${story.title}/${story.name} (${browser})`);
|
|
176
|
+
});
|
|
177
|
+
reporter.succeedSpinner('Screenshots captured');
|
|
178
|
+
// Upload baselines
|
|
179
|
+
reporter.startSpinner('Uploading baselines...');
|
|
180
|
+
let uploaded = 0;
|
|
181
|
+
let failed = 0;
|
|
182
|
+
for (const story of stories) {
|
|
183
|
+
for (const browser of config.browsers) {
|
|
184
|
+
const screenshotKey = getScreenshotKey(story.id, browser);
|
|
185
|
+
const screenshot = screenshots.get(screenshotKey);
|
|
186
|
+
if (!screenshot || screenshot.buffer.length === 0) {
|
|
187
|
+
reporter.updateSpinner(`Skipping ${story.title}/${story.name} (${browser}) (capture failed)`);
|
|
188
|
+
failed++;
|
|
189
|
+
continue;
|
|
190
|
+
}
|
|
191
|
+
// Use browser-aware storage key
|
|
192
|
+
const key = getStoryKey(story.id, browser);
|
|
193
|
+
reporter.progress(uploaded + failed + 1, totalTests, `${story.title}/${story.name} (${browser})`);
|
|
194
|
+
if (!options.dryRun) {
|
|
195
|
+
try {
|
|
196
|
+
await storage.upload(key, screenshot.buffer);
|
|
197
|
+
uploaded++;
|
|
198
|
+
}
|
|
199
|
+
catch (error) {
|
|
200
|
+
failed++;
|
|
201
|
+
if (options.verbose) {
|
|
202
|
+
reporter.error(`Failed to upload ${key}: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
else {
|
|
207
|
+
uploaded++;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
if (uploaded === totalTests) {
|
|
212
|
+
reporter.succeedSpinner('Baselines uploaded');
|
|
213
|
+
}
|
|
214
|
+
else {
|
|
215
|
+
reporter.succeedSpinner(`Baselines uploaded (${failed} failed)`);
|
|
216
|
+
}
|
|
217
|
+
// Report summary
|
|
218
|
+
reporter.reportUpdateSummary(uploaded, totalTests);
|
|
219
|
+
if (failed > 0) {
|
|
220
|
+
reporter.warning(`${failed} baselines could not be updated`);
|
|
221
|
+
}
|
|
222
|
+
if (options.dryRun) {
|
|
223
|
+
reporter.info('Dry run complete. No changes were made.');
|
|
224
|
+
}
|
|
225
|
+
else {
|
|
226
|
+
reporter.success('Baselines have been updated successfully!');
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
//# sourceMappingURL=update.js.map
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { Command } from 'commander';
|
|
3
|
+
import { testCommand } from './commands/test.js';
|
|
4
|
+
import { updateCommand } from './commands/update.js';
|
|
5
|
+
import { initCommand } from './commands/init.js';
|
|
6
|
+
import { loginCommand } from './commands/login.js';
|
|
7
|
+
import { logoutCommand } from './commands/logout.js';
|
|
8
|
+
const program = new Command();
|
|
9
|
+
program
|
|
10
|
+
.name('storysnap')
|
|
11
|
+
.description('Visual regression testing tool for Storybook')
|
|
12
|
+
.version('1.0.0');
|
|
13
|
+
program
|
|
14
|
+
.command('init')
|
|
15
|
+
.description('Initialize StorySnap configuration')
|
|
16
|
+
.option('-f, --format <format>', 'Config file format (ts, js, json)', 'ts')
|
|
17
|
+
.option('--force', 'Overwrite existing config file')
|
|
18
|
+
.action(async (options) => {
|
|
19
|
+
await initCommand({
|
|
20
|
+
format: options.format,
|
|
21
|
+
force: options.force,
|
|
22
|
+
});
|
|
23
|
+
});
|
|
24
|
+
program
|
|
25
|
+
.command('login')
|
|
26
|
+
.description('Authenticate with StorySnap API and store credentials')
|
|
27
|
+
.option('-u, --api-url <url>', 'API URL (default: https://api.storysnap.dev)')
|
|
28
|
+
.option('-t, --token <token>', 'API token (will prompt if not provided)')
|
|
29
|
+
.option('-v, --verbose', 'Verbose output')
|
|
30
|
+
.action(async (options) => {
|
|
31
|
+
await loginCommand({
|
|
32
|
+
apiUrl: options.apiUrl,
|
|
33
|
+
token: options.token,
|
|
34
|
+
verbose: options.verbose,
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
program
|
|
38
|
+
.command('logout')
|
|
39
|
+
.description('Remove stored API credentials')
|
|
40
|
+
.option('-v, --verbose', 'Verbose output')
|
|
41
|
+
.action(async (options) => {
|
|
42
|
+
await logoutCommand({
|
|
43
|
+
verbose: options.verbose,
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
program
|
|
47
|
+
.command('test')
|
|
48
|
+
.description('Run visual regression tests')
|
|
49
|
+
.option('-c, --config <path>', 'Path to config file')
|
|
50
|
+
.option('-s, --story <pattern>', 'Filter stories by pattern (supports * wildcards)')
|
|
51
|
+
.option('-b, --browser <browsers>', 'Browsers to test (chromium,firefox,webkit)')
|
|
52
|
+
.option('--storybook-url <url>', 'Storybook URL, overriding the config file')
|
|
53
|
+
.option('--branch <branch>', 'Branch name to record on the test run (API mode)')
|
|
54
|
+
.option('--commit <sha>', 'Commit SHA to record on the test run (API mode)')
|
|
55
|
+
.option('--ci', 'CI mode - fail on any visual diff or new story')
|
|
56
|
+
.option('-v, --verbose', 'Verbose output')
|
|
57
|
+
.option('--local', 'Force local-only mode without using the API')
|
|
58
|
+
.action(async (options) => {
|
|
59
|
+
await testCommand({
|
|
60
|
+
config: options.config,
|
|
61
|
+
story: options.story,
|
|
62
|
+
browser: options.browser,
|
|
63
|
+
storybookUrl: options.storybookUrl,
|
|
64
|
+
branch: options.branch,
|
|
65
|
+
commit: options.commit,
|
|
66
|
+
ci: options.ci,
|
|
67
|
+
verbose: options.verbose,
|
|
68
|
+
local: options.local,
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
program
|
|
72
|
+
.command('update')
|
|
73
|
+
.description('Update baseline screenshots')
|
|
74
|
+
.option('-c, --config <path>', 'Path to config file')
|
|
75
|
+
.option('-s, --story <pattern>', 'Filter stories by pattern (supports * wildcards)')
|
|
76
|
+
.option('-b, --browser <browsers>', 'Browsers to test (chromium,firefox,webkit)')
|
|
77
|
+
.option('--storybook-url <url>', 'Storybook URL, overriding the config file')
|
|
78
|
+
.option('-v, --verbose', 'Verbose output')
|
|
79
|
+
.option('--dry-run', 'Show what would be updated without making changes')
|
|
80
|
+
.option('--local', 'Force local-only mode without using the API')
|
|
81
|
+
.action(async (options) => {
|
|
82
|
+
await updateCommand({
|
|
83
|
+
config: options.config,
|
|
84
|
+
story: options.story,
|
|
85
|
+
browser: options.browser,
|
|
86
|
+
storybookUrl: options.storybookUrl,
|
|
87
|
+
verbose: options.verbose,
|
|
88
|
+
dryRun: options.dryRun,
|
|
89
|
+
local: options.local,
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
program.parse();
|
|
93
|
+
//# sourceMappingURL=index.js.map
|