found-pixel 0.2.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.
Files changed (49) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +225 -0
  3. package/bin/found-pixel.js +7 -0
  4. package/dist/bin.d.ts +10 -0
  5. package/dist/bin.d.ts.map +1 -0
  6. package/dist/bin.js +51 -0
  7. package/dist/bin.js.map +1 -0
  8. package/dist/build.d.ts +3 -0
  9. package/dist/build.d.ts.map +1 -0
  10. package/dist/build.js +30 -0
  11. package/dist/build.js.map +1 -0
  12. package/dist/buildkite.d.ts +16 -0
  13. package/dist/buildkite.d.ts.map +1 -0
  14. package/dist/buildkite.js +36 -0
  15. package/dist/buildkite.js.map +1 -0
  16. package/dist/cli.d.ts +2 -0
  17. package/dist/cli.d.ts.map +1 -0
  18. package/dist/cli.js +32 -0
  19. package/dist/cli.js.map +1 -0
  20. package/dist/comment.d.ts +41 -0
  21. package/dist/comment.d.ts.map +1 -0
  22. package/dist/comment.js +114 -0
  23. package/dist/comment.js.map +1 -0
  24. package/dist/config.d.ts +138 -0
  25. package/dist/config.d.ts.map +1 -0
  26. package/dist/config.js +68 -0
  27. package/dist/config.js.map +1 -0
  28. package/dist/github.d.ts +12 -0
  29. package/dist/github.d.ts.map +1 -0
  30. package/dist/github.js +41 -0
  31. package/dist/github.js.map +1 -0
  32. package/dist/index.d.ts +5 -0
  33. package/dist/index.d.ts.map +1 -0
  34. package/dist/index.js +5 -0
  35. package/dist/index.js.map +1 -0
  36. package/dist/review.d.ts +3 -0
  37. package/dist/review.d.ts.map +1 -0
  38. package/dist/review.js +189 -0
  39. package/dist/review.js.map +1 -0
  40. package/dist/storage.d.ts +11 -0
  41. package/dist/storage.d.ts.map +1 -0
  42. package/dist/storage.js +31 -0
  43. package/dist/storage.js.map +1 -0
  44. package/dist/visualRegression.d.ts +14 -0
  45. package/dist/visualRegression.d.ts.map +1 -0
  46. package/dist/visualRegression.js +120 -0
  47. package/dist/visualRegression.js.map +1 -0
  48. package/package.json +62 -0
  49. package/patches/lostPixelWindowApi.cjs +173 -0
@@ -0,0 +1,120 @@
1
+ import { execa } from "execa";
2
+ import { cpSync, existsSync, mkdirSync, readdirSync, rmSync, writeFileSync } from "node:fs";
3
+ import { dirname, resolve } from "node:path";
4
+ import { fileURLToPath } from "node:url";
5
+ import { lostPixelPlaywrightCoreDir, resolveBin } from "./bin.js";
6
+ import { uploadArtifact } from "./buildkite.js";
7
+ const LOSTPIXEL_CONFIG_PATH = resolve(process.cwd(), "lostpixel.config.js");
8
+ const LOSTPIXEL_WINDOW_API_PATCH = resolve(dirname(fileURLToPath(import.meta.url)), "..", "patches", "lostPixelWindowApi.cjs");
9
+ const CHROMIUM_INSTALL_TIMEOUT_MS = 5 * 60_000;
10
+ const PASS_TIMEOUT_MS = 10 * 60_000;
11
+ /**
12
+ * Runs lost-pixel in two passes:
13
+ * 1. generate baseline shots from `baseDir`
14
+ * 2. compare head shots from `headDir` against baseline
15
+ *
16
+ * Returns story IDs whose PNGs landed in `.lostpixel/difference/`.
17
+ */
18
+ export async function runVisualRegression(args) {
19
+ const { baseDir, headDir, artifactsDir, changedStoryIds } = args;
20
+ if (changedStoryIds.length === 0)
21
+ return [];
22
+ await installChromium();
23
+ writeLostPixelConfig(artifactsDir);
24
+ cpSync(LOSTPIXEL_CONFIG_PATH, resolve(artifactsDir, "lostpixel.config.js"));
25
+ await uploadArtifact(`${artifactsDir}/lostpixel.config.js`);
26
+ const lostPixelBin = resolveBin("lost-pixel");
27
+ try {
28
+ // Pass 1: generate baseline from base storybook.
29
+ console.log("--- :camera_with_flash: Generating baseline shots");
30
+ await runLostPixel(lostPixelBin, {
31
+ LP_STORYBOOK_URL: baseDir,
32
+ LP_GENERATE_ONLY: "true",
33
+ });
34
+ // Promote current shots to baseline.
35
+ console.log("~~~ :framed_picture: Promoting baseline shots");
36
+ mkdirSync(".lostpixel/baseline", { recursive: true });
37
+ if (existsSync(".lostpixel/current")) {
38
+ for (const file of readdirSync(".lostpixel/current")) {
39
+ cpSync(`.lostpixel/current/${file}`, `.lostpixel/baseline/${file}`);
40
+ }
41
+ rmSync(".lostpixel/current", { recursive: true, force: true });
42
+ }
43
+ // Pass 2: compare head against baseline. `failOnDifference: false` in the config
44
+ // means lost-pixel exits 0 even when diffs are found — so any non-zero exit is a
45
+ // real error we want surfaced, not swallowed.
46
+ console.log("--- :mag: Comparing head shots against baseline");
47
+ await runLostPixel(lostPixelBin, {
48
+ LP_STORYBOOK_URL: `./${headDir}`,
49
+ LP_GENERATE_ONLY: "false",
50
+ });
51
+ console.log("~~~ :arrow_up: Uploading lost-pixel artifacts");
52
+ await uploadArtifact(".lostpixel/**/*");
53
+ return collectVisuallyChangedIds(changedStoryIds);
54
+ }
55
+ finally {
56
+ rmSync(LOSTPIXEL_CONFIG_PATH, { force: true });
57
+ }
58
+ }
59
+ async function runLostPixel(bin, env) {
60
+ try {
61
+ await execa("node", ["--require", LOSTPIXEL_WINDOW_API_PATCH, bin], {
62
+ stdio: "inherit",
63
+ env: { ...process.env, ...env },
64
+ timeout: PASS_TIMEOUT_MS,
65
+ });
66
+ }
67
+ catch (err) {
68
+ if (isTimeoutError(err)) {
69
+ throw new Error(`lost-pixel timed out after ${PASS_TIMEOUT_MS / 1000}s. ` +
70
+ `This usually means the storybook version is incompatible with ` +
71
+ `lost-pixel, or preview code intercepts fetch / blocks ` +
72
+ `'api.ready()'. Check the uploaded lostpixel.config.js ` +
73
+ `artifact and the base/head screenshots directories.`);
74
+ }
75
+ throw err;
76
+ }
77
+ }
78
+ function isTimeoutError(err) {
79
+ return (typeof err === "object" &&
80
+ err !== null &&
81
+ "timedOut" in err &&
82
+ err.timedOut === true);
83
+ }
84
+ async function installChromium() {
85
+ console.log("~~~ :chrome: Installing lost-pixel chromium");
86
+ const cli = resolve(lostPixelPlaywrightCoreDir(), "cli.js");
87
+ await execa("node", [cli, "install", "--with-deps", "chromium"], {
88
+ stdio: "inherit",
89
+ timeout: CHROMIUM_INSTALL_TIMEOUT_MS,
90
+ });
91
+ }
92
+ function writeLostPixelConfig(artifactsDir) {
93
+ const config = `const fs = require('fs');
94
+ const path = require('path');
95
+ const idsPath = path.resolve(__dirname, ${JSON.stringify(artifactsDir)}, 'changed-story-ids.json');
96
+ const changedIds = JSON.parse(fs.readFileSync(idsPath, 'utf8'));
97
+ module.exports = {
98
+ storybookShots: {
99
+ storybookUrl: process.env.LP_STORYBOOK_URL || './.storybookout',
100
+ },
101
+ generateOnly: process.env.LP_GENERATE_ONLY === 'true',
102
+ failOnDifference: false,
103
+ filterShot: (shot) => changedIds.includes(shot.id),
104
+ };
105
+ `;
106
+ writeFileSync(LOSTPIXEL_CONFIG_PATH, config);
107
+ }
108
+ function collectVisuallyChangedIds(changedStoryIds) {
109
+ const diffDir = ".lostpixel/difference";
110
+ if (!existsSync(diffDir))
111
+ return [];
112
+ const lpFiles = readdirSync(diffDir)
113
+ .filter((f) => f.endsWith(".png"))
114
+ .map((f) => f.replace(".png", ""));
115
+ // Lost-pixel normalises story IDs (strips dashes, lowercases) in file names; map back.
116
+ const normalize = (s) => s.replace(/-/g, "").toLowerCase();
117
+ const normalizedMap = new Map(changedStoryIds.map((id) => [normalize(id), id]));
118
+ return lpFiles.map((lpId) => normalizedMap.get(normalize(lpId)) ?? lpId);
119
+ }
120
+ //# sourceMappingURL=visualRegression.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"visualRegression.js","sourceRoot":"","sources":["../src/visualRegression.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,OAAO,CAAC;AAC9B,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAC5F,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAC7C,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AACzC,OAAO,EAAE,0B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AAC,CAAC,CAAC;IAErC,uFAAuF;IACvF,MAAM,SAAS,GAAG,CAAC,CAAS,EAAE,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,WAAW,EAAE,CAAC;IACnE,MAAM,aAAa,GAAG,IAAI,GAAG,CAAC,eAAe,CAAC,GAAG,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC,SAAS,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC;IAChF,OAAO,OAAO,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,aAAa,CAAC,GAAG,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,IAAI,IAAI,CAAC,CAAC;AAC3E,CAAC"}
package/package.json ADDED
@@ -0,0 +1,62 @@
1
+ {
2
+ "name": "found-pixel",
3
+ "version": "0.2.0",
4
+ "description": "Storybook visual regression workflow for Buildkite: build, upload, diff, visually regress, upsert PR comment.",
5
+ "license": "MIT",
6
+ "author": "Healthengine",
7
+ "keywords": [
8
+ "storybook",
9
+ "storybook-diff",
10
+ "lost-pixel",
11
+ "visual-regression",
12
+ "buildkite",
13
+ "pull-request",
14
+ "ci"
15
+ ],
16
+ "type": "module",
17
+ "bin": {
18
+ "found-pixel": "./bin/found-pixel.js"
19
+ },
20
+ "types": "./dist/index.d.ts",
21
+ "exports": {
22
+ ".": {
23
+ "types": "./dist/index.d.ts",
24
+ "import": "./dist/index.js"
25
+ },
26
+ "./comment": {
27
+ "types": "./dist/comment.d.ts",
28
+ "import": "./dist/comment.js"
29
+ }
30
+ },
31
+ "files": [
32
+ "bin",
33
+ "dist",
34
+ "patches",
35
+ "README.md",
36
+ "LICENSE"
37
+ ],
38
+ "scripts": {
39
+ "build": "tsc",
40
+ "typecheck": "tsc --noEmit",
41
+ "test": "vitest run",
42
+ "test:update": "vitest run -u",
43
+ "preview-comment": "tsx scripts/preview-comment.ts",
44
+ "prepublishOnly": "npm run build && npm test"
45
+ },
46
+ "dependencies": {
47
+ "commander": "^12.1.0",
48
+ "execa": "^9.5.2",
49
+ "lost-pixel": "^3.22.0",
50
+ "storybook-diff": "^0.1.0",
51
+ "zod": "^3.24.0"
52
+ },
53
+ "devDependencies": {
54
+ "@types/node": "^22.10.0",
55
+ "tsx": "^4.19.0",
56
+ "typescript": "^5.7.0",
57
+ "vitest": "^4.1.4"
58
+ },
59
+ "engines": {
60
+ "node": ">=20"
61
+ }
62
+ }
@@ -0,0 +1,173 @@
1
+ // Preloaded via `node --require` before lost-pixel's CLI starts.
2
+ //
3
+ // Replaces lost-pixel's `collectStoriesViaWindowApi` with a version whose
4
+ // browser-side evaluate prunes each story's `parameters` to an allow-list of
5
+ // keys that lost-pixel actually consumes downstream. The upstream implementation
6
+ // runs a depth-10 recursive `Object.keys(...).reduce()` over every story's full
7
+ // parameters tree; for storybooks with large docgen argTypes metadata (e.g.
8
+ // @storybook/nextjs + complex TS types) this OOM-kills the Chromium renderer
9
+ // on stores with many stories. Playwright has no crash rejection plumbed
10
+ // through `page.evaluate`, so the caller hangs forever.
11
+ //
12
+ // The pruned shape preserves every field `generateStorybookShotItems` reads:
13
+ // parameters.fileName
14
+ // parameters.viewport (width, height)
15
+ // parameters.lostpixel.{disable,args,threshold,waitBeforeScreenshot,mask,
16
+ // elementLocator,breakpoints,extraShots}
17
+ // parameters.storyshots.disable
18
+ "use strict";
19
+
20
+ const storybook = require("lost-pixel/dist/crawler/storybook");
21
+ const configModule = require("lost-pixel/dist/config");
22
+
23
+ const fetchStoriesTimeout = () =>
24
+ (configModule.config &&
25
+ configModule.config.timeouts &&
26
+ configModule.config.timeouts.fetchStories) ||
27
+ 60_000;
28
+
29
+ storybook.collectStoriesViaWindowApi = async function collectStoriesViaWindowApi(
30
+ context,
31
+ url,
32
+ isIframeUrl,
33
+ ) {
34
+ const page = await context.newPage();
35
+ const iframeUrl = isIframeUrl
36
+ ? storybook.getStoryBookUrl(url)
37
+ : storybook.getIframeUrl(storybook.getStoryBookUrl(url));
38
+
39
+ await page.goto(iframeUrl);
40
+ await page.waitForFunction(() => window.__STORYBOOK_PREVIEW__, null, {
41
+ timeout: fetchStoriesTimeout(),
42
+ });
43
+
44
+ const isV8OrAbove = await page.evaluate(
45
+ async () => window.__STORYBOOK_PREVIEW__.ready !== undefined,
46
+ );
47
+
48
+ if (isV8OrAbove) {
49
+ await page.evaluate(async () => {
50
+ const api = window.__STORYBOOK_PREVIEW__;
51
+ if (api.ready) await api.ready();
52
+ });
53
+ } else {
54
+ await page.waitForFunction(() => window.__STORYBOOK_CLIENT_API__, null, {
55
+ timeout: fetchStoriesTimeout(),
56
+ });
57
+ await page.evaluate(async () => {
58
+ const api = window.__STORYBOOK_CLIENT_API__;
59
+ if (api.storyStore && api.storyStore.cacheAllCSFFiles) {
60
+ await api.storyStore.cacheAllCSFFiles();
61
+ }
62
+ });
63
+ }
64
+
65
+ const result = await page.evaluate(async () => {
66
+ const isPrimitive = (v) =>
67
+ v === null ||
68
+ v === undefined ||
69
+ typeof v === "string" ||
70
+ typeof v === "number" ||
71
+ typeof v === "boolean";
72
+
73
+ const pluckArgs = (args) => {
74
+ if (!args || typeof args !== "object") return undefined;
75
+ const out = {};
76
+ for (const k of Object.keys(args)) {
77
+ if (isPrimitive(args[k])) out[k] = args[k];
78
+ }
79
+ return out;
80
+ };
81
+
82
+ const pluckMask = (mask) => {
83
+ if (!Array.isArray(mask)) return undefined;
84
+ return mask
85
+ .map((m) => {
86
+ if (!m || typeof m !== "object") return undefined;
87
+ const out = {};
88
+ if (typeof m.selector === "string") out.selector = m.selector;
89
+ return out.selector ? out : undefined;
90
+ })
91
+ .filter(Boolean);
92
+ };
93
+
94
+ const pluckExtraShots = (shots) => {
95
+ if (!Array.isArray(shots)) return undefined;
96
+ return shots.map((s) => {
97
+ if (!s || typeof s !== "object") return {};
98
+ const out = {};
99
+ if (typeof s.prefix === "string") out.prefix = s.prefix;
100
+ if (typeof s.suffix === "string") out.suffix = s.suffix;
101
+ if (typeof s.name === "string") out.name = s.name;
102
+ const args = pluckArgs(s.args);
103
+ if (args) out.args = args;
104
+ return out;
105
+ });
106
+ };
107
+
108
+ const pluckParameters = (p) => {
109
+ if (!p || typeof p !== "object") return {};
110
+ const out = {};
111
+ if (typeof p.fileName === "string") out.fileName = p.fileName;
112
+ if (p.viewport && typeof p.viewport === "object") {
113
+ const v = {};
114
+ if (typeof p.viewport.width === "number") v.width = p.viewport.width;
115
+ if (typeof p.viewport.height === "number") v.height = p.viewport.height;
116
+ out.viewport = v;
117
+ }
118
+ if (p.lostpixel && typeof p.lostpixel === "object") {
119
+ const lp = p.lostpixel;
120
+ const lpOut = {};
121
+ if (typeof lp.disable === "boolean") lpOut.disable = lp.disable;
122
+ const args = pluckArgs(lp.args);
123
+ if (args) lpOut.args = args;
124
+ if (typeof lp.threshold === "number") lpOut.threshold = lp.threshold;
125
+ if (typeof lp.waitBeforeScreenshot === "number")
126
+ lpOut.waitBeforeScreenshot = lp.waitBeforeScreenshot;
127
+ const mask = pluckMask(lp.mask);
128
+ if (mask) lpOut.mask = mask;
129
+ if (typeof lp.elementLocator === "string")
130
+ lpOut.elementLocator = lp.elementLocator;
131
+ if (Array.isArray(lp.breakpoints))
132
+ lpOut.breakpoints = lp.breakpoints.filter(
133
+ (b) => typeof b === "number",
134
+ );
135
+ const extraShots = pluckExtraShots(lp.extraShots);
136
+ if (extraShots) lpOut.extraShots = extraShots;
137
+ out.lostpixel = lpOut;
138
+ }
139
+ if (p.storyshots && typeof p.storyshots === "object") {
140
+ const ss = {};
141
+ if (typeof p.storyshots.disable === "boolean")
142
+ ss.disable = p.storyshots.disable;
143
+ out.storyshots = ss;
144
+ }
145
+ return out;
146
+ };
147
+
148
+ const mapStories = (stories) =>
149
+ stories.map((story) => {
150
+ const parameters = pluckParameters(story.parameters);
151
+ return {
152
+ id: story.id,
153
+ kind: story.kind,
154
+ story: story.story,
155
+ importPath: parameters && parameters.fileName,
156
+ parameters,
157
+ };
158
+ });
159
+
160
+ const previewApi = window.__STORYBOOK_PREVIEW__;
161
+ const clientApi = window.__STORYBOOK_CLIENT_API__;
162
+ let stories = [];
163
+ if (previewApi && previewApi.extract) {
164
+ const items = await previewApi.extract();
165
+ stories = mapStories(Object.values(items));
166
+ } else if (clientApi && clientApi.raw) {
167
+ stories = mapStories(clientApi.raw());
168
+ }
169
+ return { stories };
170
+ });
171
+
172
+ return result;
173
+ };