@cappa/core 0.1.2 → 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.
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","names":["defaultDiffConfig: DiffConfig","diffImagePath: string | undefined"],"sources":["../src/config.ts","../src/compare.ts","../src/screenshot.ts"],"sourcesContent":["import type { PossiblePromise, UserConfig } from \"./types\";\n\n/**\n * Type helper to make it easier to use cappa.config.ts, or a function that returns it. The function receives a ConfigEnv object.\n */\nexport function defineConfig(\n options:\n | PossiblePromise<UserConfig | Array<UserConfig>>\n | (() => PossiblePromise<UserConfig | Array<UserConfig>>),\n): typeof options {\n return options;\n}\n","import fs from \"node:fs\";\nimport blazediff from \"@blazediff/core\";\nimport type { BlazeDiffOptions } from \"@blazediff/types\";\nimport { PNG } from \"pngjs\";\nimport type { DiffConfig } from \"./types\";\n\nexport type CompareOptions = BlazeDiffOptions;\n\nexport interface CompareResult {\n numDiffPixels: number;\n totalPixels: number;\n percentDifference: number;\n diffBuffer?: Buffer; // PNG buffer of the diff image\n passed: boolean; // Whether the comparison passed based on threshold\n}\n\nconst compare = (\n image1: Uint8Array | Uint8ClampedArray,\n image2: Uint8Array | Uint8ClampedArray,\n diff: Uint8Array | Uint8ClampedArray | undefined,\n width: number,\n height: number,\n options?: BlazeDiffOptions,\n) => {\n return blazediff(image1, image2, diff, width, height, options);\n};\n\n/**\n * Compare two PNG images and return the difference\n * @param image1 - First image (file path or Buffer)\n * @param image2 - Second image (file path or Buffer)\n * @param withDiff - Whether to create a diff image\n * @param options - Comparison options\n * @returns Comparison result\n */\nexport async function compareImages(\n image1: string | Buffer,\n image2: string | Buffer,\n withDiff: boolean = false,\n options: DiffConfig = {},\n): Promise<CompareResult> {\n // Load images as PNG objects\n const png1 = await loadPNG(image1);\n const png2 = await loadPNG(image2);\n\n const { width, height } = png1;\n\n const diff = withDiff ? new PNG({ width, height }) : undefined;\n\n const numDiffPixels = compare(\n png1.data,\n png2.data,\n diff?.data,\n width,\n height,\n options,\n );\n\n const totalPixels = width * height;\n const percentDifference = (numDiffPixels / totalPixels) * 100;\n\n // Convert diff image to buffer\n const diffBuffer = diff ? PNG.sync.write(diff) : undefined;\n\n return {\n numDiffPixels,\n totalPixels,\n percentDifference,\n diffBuffer,\n passed: isPassed(percentDifference, numDiffPixels, options),\n };\n}\n\n/**\n * Load a PNG image from file path or Buffer\n */\nasync function loadPNG(source: string | Buffer): Promise<PNG> {\n if (Buffer.isBuffer(source)) {\n return PNG.sync.read(source);\n } else {\n const buffer = fs.readFileSync(source);\n return PNG.sync.read(buffer);\n }\n}\n\n/**\n * Save a comparison result diff image to file\n * @param result - Comparison result containing diffBuffer\n * @param outputPath - Path to save the diff image\n */\nexport function saveDiffImage(result: CompareResult, outputPath: string): void {\n if (!result.diffBuffer) {\n throw new Error(\"No diff buffer available in comparison result\");\n }\n\n fs.writeFileSync(outputPath, result.diffBuffer);\n}\n\n/**\n * Quick comparison function that returns only pass/fail\n * @param image1 - First image (file path or Buffer)\n * @param image2 - Second image (file path or Buffer)\n * @param options - Comparison options\n * @returns Whether the images match\n */\nexport async function imagesMatch(\n image1: string | Buffer,\n image2: string | Buffer,\n options: DiffConfig = {},\n): Promise<boolean> {\n const result = await compareImages(image1, image2, false, options);\n\n return result.passed;\n}\n\nconst isPassed = (\n percentDifference: number,\n numDiffPixels: number,\n options: DiffConfig,\n) => {\n if (options.maxDiffPercentage && options.maxDiffPixels) {\n return (\n percentDifference <= options.maxDiffPercentage &&\n numDiffPixels <= options.maxDiffPixels\n );\n }\n\n if (options.maxDiffPercentage) {\n return percentDifference <= options.maxDiffPercentage;\n }\n\n if (options.maxDiffPixels) {\n return numDiffPixels <= options.maxDiffPixels;\n }\n\n // fallback to 0 pixels\n return numDiffPixels === 0;\n};\n","import fs from \"node:fs\";\nimport path from \"node:path\";\nimport { getLogger, type Logger } from \"@cappa/logger\";\nimport {\n type Browser,\n type BrowserContext,\n chromium,\n firefox,\n type Locator,\n type Page,\n type PageScreenshotOptions,\n webkit,\n} from \"playwright-core\";\nimport {\n type CompareOptions,\n type CompareResult,\n compareImages,\n} from \"./compare\";\nimport type { DiffConfig, ScreenshotOptions } from \"./types\";\n\nconst defaultDiffConfig: DiffConfig = {\n threshold: 0.1,\n includeAA: false,\n fastBufferCheck: true,\n maxDiffPixels: 0,\n maxDiffPercentage: 0,\n};\n\nclass ScreenshotTool {\n browserType: \"chromium\" | \"firefox\" | \"webkit\";\n headless: boolean;\n viewport: { width: number; height: number };\n outputDir: string;\n actualDir: string;\n diffDir: string;\n expectedDir: string;\n browser: Browser | null = null;\n context: BrowserContext | null = null;\n page: Page | null = null;\n diff: DiffConfig;\n logger: Logger;\n\n constructor(options: {\n browserType?: \"chromium\" | \"firefox\" | \"webkit\";\n headless?: boolean;\n viewport?: { width: number; height: number };\n outputDir?: string;\n diff?: DiffConfig;\n }) {\n this.browserType = options.browserType || \"chromium\";\n this.headless = options.headless !== false; // Default to headless\n this.viewport = options.viewport || { width: 1920, height: 1080 };\n this.outputDir = options.outputDir || \"./screenshots\";\n this.diff = { ...defaultDiffConfig, ...options.diff };\n // Set up subdirectories\n this.actualDir = path.join(this.outputDir, \"actual\");\n this.diffDir = path.join(this.outputDir, \"diff\");\n this.expectedDir = path.join(this.outputDir, \"expected\");\n\n this.logger = getLogger();\n }\n\n /**\n * Initialize playwright and page\n */\n async init() {\n // Create output directories if they don't exist\n this.createDirectories();\n\n // Launch browser based on type\n const browserMap = {\n chromium,\n firefox,\n webkit,\n };\n\n const browserClass = browserMap[this.browserType];\n if (!browserClass) {\n throw new Error(`Unsupported browser type: ${this.browserType}`);\n }\n\n this.browser = await browserClass.launch({\n headless: this.headless,\n });\n\n const defaultUserAgent = (await this.browser.newContext())\n .newPage()\n .then((page) => page.evaluate(() => navigator.userAgent));\n\n this.context = await this.browser.newContext({\n reducedMotion: \"reduce\",\n deviceScaleFactor: 2,\n userAgent: `${await defaultUserAgent} CappaStorybook`,\n viewport: this.viewport,\n });\n\n this.page = await this.context?.newPage();\n }\n\n /**\n * Closes browser\n * If not closed, the process will not exit\n */\n async close() {\n if (this.context) {\n await this.context.close();\n }\n if (this.browser) {\n await this.browser.close();\n }\n }\n\n /**\n * Navigates to a URL with \"domcontentloaded\" waitUntil\n */\n async goTo(page: Page, url: string) {\n if (!this.context) {\n throw new Error(\"Browser not initialized\");\n }\n await page.goto(url, { waitUntil: \"domcontentloaded\" });\n }\n\n /**\n * Creates directories for screenshots and other files\n */\n private createDirectories() {\n // Create main output directory and subdirectories\n const directories = [\n this.outputDir,\n this.actualDir,\n this.diffDir,\n this.expectedDir,\n ];\n\n for (const dir of directories) {\n if (!fs.existsSync(dir)) {\n fs.mkdirSync(dir, { recursive: true });\n this.logger.debug(`Initialized directory: ${dir}`);\n }\n }\n }\n\n /**\n * Gets the path to the actual screenshot file\n */\n getActualFilePath(filename: string): string {\n return path.join(this.actualDir, filename);\n }\n\n /**\n * Gets the path to the diff screenshot file\n */\n getDiffFilePath(filename: string): string {\n return path.join(this.diffDir, filename);\n }\n\n /**\n * Gets the path to the expected screenshot file\n */\n getExpectedFilePath(filename: string): string {\n return path.join(this.expectedDir, filename);\n }\n\n /**\n * Takes a screenshot of the page\n */\n async takeScreenshot(\n page: Page,\n filename: string,\n options: ScreenshotOptions,\n ) {\n if (!this.browser) {\n throw new Error(\"Browser not initialized\");\n }\n\n if (options.skip) {\n return;\n }\n\n try {\n // Generate filename - save to actual directory\n const filepath = this.getActualFilePath(filename);\n\n // Take screenshot\n const screenshotOptions = {\n path: filepath,\n fullPage: options.fullPage,\n type: \"png\" as const,\n timeout: 60000,\n mask: options.mask,\n omitBackground: options.omitBackground,\n scale: \"css\" as const,\n } satisfies PageScreenshotOptions;\n\n if (options.delay) {\n await page.waitForTimeout(options.delay);\n }\n\n await page.screenshot(screenshotOptions);\n\n this.logger.success(`Screenshot saved: ${filepath}`);\n return filepath;\n } catch (error) {\n this.logger.error(\n `Error taking screenshot of ${page.url()}:`,\n (error as Error).message,\n );\n throw error;\n }\n }\n\n async takeScreenshotWithComparison(\n page: Page,\n filename: string,\n referenceImage: Buffer,\n options: {\n fullPage?: boolean;\n mask?: Locator[];\n omitBackground?: boolean;\n // Comparison options\n compareOptions?: CompareOptions;\n maxDifferencePercent?: number;\n saveDiffImage?: boolean;\n diffImageFilename?: string;\n } = {},\n ): Promise<{\n screenshotPath: string;\n comparisonResult: CompareResult;\n diffImagePath?: string;\n }> {\n if (!this.browser) {\n throw new Error(\"Browser not initialized\");\n }\n\n try {\n // Generate filename and take screenshot - save to actual directory\n const filepath = this.getActualFilePath(filename);\n fs.mkdirSync(path.dirname(filepath), { recursive: true });\n\n // Take screenshot and get buffer\n const screenshotOptions = {\n fullPage: options.fullPage,\n type: \"png\" as const,\n timeout: 60000,\n mask: options.mask,\n omitBackground: options.omitBackground,\n scale: \"css\" as const,\n } satisfies PageScreenshotOptions;\n\n // Get screenshot as buffer for comparison\n const screenshotBuffer = await page.screenshot(screenshotOptions);\n\n // Save screenshot to actual directory\n fs.writeFileSync(filepath, screenshotBuffer);\n\n this.logger.success(`Screenshot saved: ${filepath}`);\n\n // Perform comparison\n const comparisonResult = await compareImages(\n screenshotBuffer,\n referenceImage,\n true,\n { ...this.diff, ...options.compareOptions },\n );\n\n this.logger.debug(\n `Comparison result: ${comparisonResult.numDiffPixels} pixels different (${comparisonResult.percentDifference.toFixed(2)}%)`,\n );\n\n if (comparisonResult.passed) {\n this.logger.success(`Screenshot passed visual comparison`);\n } else {\n this.logger.error(`Screenshot failed visual comparison`);\n }\n\n let diffImagePath: string | undefined;\n\n // Save diff image if requested and there are differences\n if (\n options.saveDiffImage &&\n comparisonResult.diffBuffer &&\n !comparisonResult.passed\n ) {\n const diffFilename = options.diffImageFilename || filename;\n // Save to diff directory\n diffImagePath = this.getDiffFilePath(diffFilename);\n\n fs.mkdirSync(path.dirname(diffImagePath), { recursive: true });\n fs.writeFileSync(diffImagePath, comparisonResult.diffBuffer);\n\n this.logger.debug(`Diff image saved: ${diffImagePath}`);\n }\n\n return {\n screenshotPath: filepath,\n comparisonResult,\n diffImagePath,\n };\n } catch (error) {\n this.logger.error(\n `Error taking screenshot with comparison of ${page.url()}:`,\n (error as Error).message,\n );\n throw error;\n }\n }\n\n // Utility method to check if expected image exists\n hasExpectedImage(filename: string): boolean {\n const expectedPath = this.getExpectedFilePath(filename);\n return fs.existsSync(expectedPath);\n }\n\n // Utility method to get expected image as buffer (for future approval process)\n getExpectedImageBuffer(filename: string): Buffer {\n const expectedPath = this.getExpectedFilePath(filename);\n if (!fs.existsSync(expectedPath)) {\n throw new Error(`Expected image not found: ${expectedPath}`);\n }\n return fs.readFileSync(expectedPath);\n }\n}\n\nexport default ScreenshotTool;\n"],"mappings":";;;;;;;;;;;AAKA,SAAgB,aACd,SAGgB;AAChB,QAAO;;;;;ACMT,MAAM,WACJ,QACA,QACA,MACA,OACA,QACA,YACG;AACH,QAAO,UAAU,QAAQ,QAAQ,MAAM,OAAO,QAAQ,QAAQ;;;;;;;;;;AAWhE,eAAsB,cACpB,QACA,QACA,WAAoB,OACpB,UAAsB,EAAE,EACA;CAExB,MAAM,OAAO,MAAM,QAAQ,OAAO;CAClC,MAAM,OAAO,MAAM,QAAQ,OAAO;CAElC,MAAM,EAAE,OAAO,WAAW;CAE1B,MAAM,OAAO,WAAW,IAAI,IAAI;EAAE;EAAO;EAAQ,CAAC,GAAG;CAErD,MAAM,gBAAgB,QACpB,KAAK,MACL,KAAK,MACL,MAAM,MACN,OACA,QACA,QACD;CAED,MAAM,cAAc,QAAQ;CAC5B,MAAM,oBAAqB,gBAAgB,cAAe;CAG1D,MAAM,aAAa,OAAO,IAAI,KAAK,MAAM,KAAK,GAAG;AAEjD,QAAO;EACL;EACA;EACA;EACA;EACA,QAAQ,SAAS,mBAAmB,eAAe,QAAQ;EAC5D;;;;;AAMH,eAAe,QAAQ,QAAuC;AAC5D,KAAI,OAAO,SAAS,OAAO,CACzB,QAAO,IAAI,KAAK,KAAK,O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E5D,SAAK,OAAO,MAAM,qBAAqB,gBAAgB;;AAGzD,UAAO;IACL,gBAAgB;IAChB;IACA;IACD;WACM,OAAO;AACd,QAAK,OAAO,MACV,8CAA8C,KAAK,KAAK,CAAC,IACxD,MAAgB,QAClB;AACD,SAAM;;;CAKV,iBAAiB,UAA2B;EAC1C,MAAM,eAAe,KAAK,oBAAoB,SAAS;AACvD,SAAO,GAAG,WAAW,aAAa;;CAIpC,uBAAuB,UAA0B;EAC/C,MAAM,eAAe,KAAK,oBAAoB,SAAS;AACvD,MAAI,CAAC,GAAG,WAAW,aAAa,CAC9B,OAAM,IAAI,MAAM,6BAA6B,eAAe;AAE9D,SAAO,GAAG,aAAa,aAAa;;;AAIxC,yBAAe"}
1
+ {"version":3,"file":"index.mjs","names":["defaultDiffConfig: DiffConfig","screenshotSettings: ScreenshotSettings","diffImagePath: string | undefined","baseResult: ScreenshotCaptureDetails","variantResults: ScreenshotVariantCaptureDetails[]","variantOptions: ScreenshotSettings","variantResult: ScreenshotVariantCaptureDetails"],"sources":["../src/config.ts","../src/filesystem.ts","../src/compare.ts","../src/screenshot.ts"],"sourcesContent":["import type { PossiblePromise, UserConfig } from \"./types\";\n\n/**\n * Type helper to make it easier to use cappa.config.ts, or a function that returns it. The function receives a ConfigEnv object.\n */\nexport function defineConfig(\n options:\n | PossiblePromise<UserConfig | Array<UserConfig>>\n | (() => PossiblePromise<UserConfig | Array<UserConfig>>),\n): typeof options {\n return options;\n}\n","import fs from \"node:fs\";\nimport path from \"node:path\";\n\n/**\n * Class for interacting with the local file system to store the screenshots.\n */\nexport class ScreenshotFileSystem {\n private readonly actualDir: string;\n private readonly expectedDir: string;\n private readonly diffDir: string;\n\n constructor(outputDir: string) {\n this.actualDir = path.resolve(outputDir, \"actual\");\n this.expectedDir = path.resolve(outputDir, \"expected\");\n this.diffDir = path.resolve(outputDir, \"diff\");\n\n this.ensureParentDir(this.actualDir);\n this.ensureParentDir(this.expectedDir);\n this.ensureParentDir(this.diffDir);\n }\n\n clearActual() {\n this.removeDir(this.actualDir);\n }\n\n clearDiff() {\n this.removeDir(this.diffDir);\n }\n\n clearActualAndDiff() {\n this.clearActual();\n this.clearDiff();\n }\n\n /**\n * Approve a screenshot based on its actual file path.\n * @param actualFilePath - The path to the actual screenshot file.\n * @returns The paths to the actual, expected, and diff screenshot files.\n */\n approveFromActualPath(actualFilePath: string) {\n const actualAbsolute = this.resolveActualPath(actualFilePath);\n const relativePath = path.relative(this.actualDir, actualAbsolute);\n\n if (relativePath.startsWith(\"..\")) {\n throw new Error(\n `Cannot approve screenshot outside of actual directory: ${actualFilePath}`,\n );\n }\n\n return this.approveRelative(relativePath);\n }\n\n /**\n * Approve a screenshot based on its name.\n * @param name - The name of the screenshot. Can include '.png' extension.\n * @returns The paths to the actual, expected, and diff screenshot files.\n */\n approveByName(name: string) {\n const relativeWithExtension = name.endsWith(\".png\") ? name : `${name}.png`;\n\n return this.approveRelative(relativeWithExtension);\n }\n\n /**\n * Approve a screenshot based on a relative path.\n * @param relativePath - The relative path to the screenshot.\n * @returns The paths to the actual, expected, and diff screenshot files.\n */\n private approveRelative(relativePath: string) {\n const actualPath = path.resolve(this.actualDir, relativePath);\n const expectedPath = path.resolve(this.expectedDir, relativePath);\n const diffPath = path.resolve(this.diffDir, relativePath);\n\n this.ensureParentDir(expectedPath);\n\n fs.copyFileSync(actualPath, expectedPath);\n\n if (fs.existsSync(diffPath)) {\n fs.unlinkSync(diffPath);\n }\n\n return {\n actualPath,\n expectedPath,\n diffPath,\n };\n }\n\n /**\n * Ensure the parent directory of a file exists. If not, it creates it.\n * @param filePath - The path to the file.\n */\n private ensureParentDir(filePath: string) {\n const dir = path.dirname(filePath);\n\n if (!fs.existsSync(dir)) {\n fs.mkdirSync(dir, { recursive: true });\n }\n }\n\n /**\n * Remove a directory and all its contents.\n * @param dir - The path to the directory.\n */\n private removeDir(dir: string) {\n if (fs.existsSync(dir)) {\n fs.rmSync(dir, { recursive: true, force: true });\n }\n }\n\n /**\n * Get the actual directory path\n */\n getActualDir(): string {\n return this.actualDir;\n }\n\n /**\n * Get the expected directory path\n */\n getExpectedDir(): string {\n return this.expectedDir;\n }\n\n /**\n * Get the diff directory path\n */\n getDiffDir(): string {\n return this.diffDir;\n }\n\n /**\n * Get the path to an actual screenshot file\n */\n getActualFilePath(filename: string): string {\n return path.resolve(this.actualDir, filename);\n }\n\n /**\n * Get the path to an expected screenshot file\n */\n getExpectedFilePath(filename: string): string {\n return path.resolve(this.expectedDir, filename);\n }\n\n /**\n * Get the path to a diff screenshot file\n */\n getDiffFilePath(filename: string): string {\n return path.resolve(this.diffDir, filename);\n }\n\n /**\n * Write a file to the actual directory\n */\n writeActualFile(filename: string, data: Buffer): void {\n const filepath = this.getActualFilePath(filename);\n this.ensureParentDir(filepath);\n fs.writeFileSync(filepath, data);\n }\n\n /**\n * Write a file to the diff directory\n */\n writeDiffFile(filename: string, data: Buffer): void {\n const filepath = this.getDiffFilePath(filename);\n this.ensureParentDir(filepath);\n fs.writeFileSync(filepath, data);\n }\n\n /**\n * Check if an expected file exists\n */\n hasExpectedFile(filename: string): boolean {\n const expectedPath = this.getExpectedFilePath(filename);\n return fs.existsSync(expectedPath);\n }\n\n /**\n * Read an expected file\n */\n readExpectedFile(filename: string): Buffer {\n const expectedPath = this.getExpectedFilePath(filename);\n if (!fs.existsSync(expectedPath)) {\n throw new Error(`Expected image not found: ${expectedPath}`);\n }\n return fs.readFileSync(expectedPath);\n }\n\n /**\n * Resolve the actual path of a screenshot file.\n * @param filePath - The path to the screenshot file.\n * @returns The actual path of the screenshot file.\n */\n private resolveActualPath(filePath: string) {\n if (path.isAbsolute(filePath)) {\n return filePath;\n }\n\n return path.resolve(this.actualDir, filePath);\n }\n}\n","import fs from \"node:fs\";\nimport blazediff from \"@blazediff/core\";\nimport type { BlazeDiffOptions } from \"@blazediff/types\";\nimport { getLogger } from \"@cappa/logger\";\nimport { PNG } from \"pngjs\";\nimport type { DiffConfig } from \"./types\";\n\nexport type CompareOptions = BlazeDiffOptions;\n\nexport interface CompareResult {\n numDiffPixels: number;\n totalPixels: number;\n percentDifference: number;\n diffBuffer?: Buffer; // PNG buffer of the diff image\n passed: boolean; // Whether the comparison passed based on threshold\n error?: string; // Error message if the comparison failed\n differentSizes: boolean; // Whether the images have different sizes\n}\n\nconst compare = (\n image1: Uint8Array | Uint8ClampedArray,\n image2: Uint8Array | Uint8ClampedArray,\n diff: Uint8Array | Uint8ClampedArray | undefined,\n width: number,\n height: number,\n options?: BlazeDiffOptions,\n) => {\n return blazediff(image1, image2, diff, width, height, options);\n};\n\n/**\n * Compare two PNG images and return the difference\n * @param image1 - First image (file path or Buffer)\n * @param image2 - Second image (file path or Buffer)\n * @param withDiff - Whether to create a diff image\n * @param options - Comparison options\n * @returns Comparison result\n */\nexport async function compareImages(\n image1: string | Buffer,\n image2: string | Buffer,\n withDiff: boolean = false,\n options: DiffConfig = {},\n): Promise<CompareResult> {\n const png1 = await loadPNG(image1);\n const png2 = await loadPNG(image2);\n\n const { width, height } = png1;\n\n if (width !== png2.width || height !== png2.height) {\n return {\n numDiffPixels: 0,\n totalPixels: 0,\n percentDifference: 0,\n passed: false,\n differentSizes: true,\n };\n }\n\n const diff = withDiff ? new PNG({ width, height }) : undefined;\n\n try {\n const numDiffPixels = compare(\n png1.data,\n png2.data,\n diff?.data,\n width,\n height,\n options,\n );\n\n const totalPixels = width * height;\n const percentDifference = (numDiffPixels / totalPixels) * 100;\n\n // Convert diff image to buffer\n const diffBuffer = diff ? PNG.sync.write(diff) : undefined;\n\n return {\n numDiffPixels,\n totalPixels,\n percentDifference,\n diffBuffer,\n passed: isPassed(percentDifference, numDiffPixels, options),\n differentSizes: false,\n };\n } catch (error) {\n getLogger().error((error as Error).message || \"Unknown error\");\n\n return {\n numDiffPixels: 0,\n totalPixels: 0,\n percentDifference: 0,\n passed: false,\n differentSizes: false,\n error: (error as Error).message || \"Unknown error\",\n };\n }\n}\n\n/**\n * Load a PNG image from file path or Buffer\n */\nasync function loadPNG(source: string | Buffer): Promise<PNG> {\n if (Buffer.isBuffer(source)) {\n return PNG.sync.read(source);\n } else {\n const buffer = fs.readFileSync(source);\n return PNG.sync.read(buffer);\n }\n}\n\n/**\n * Save a comparison result diff image to file\n * @param result - Comparison result containing diffBuffer\n * @param outputPath - Path to save the diff image\n */\nexport function saveDiffImage(result: CompareResult, outputPath: string): void {\n if (!result.diffBuffer) {\n throw new Error(\"No diff buffer available in comparison result\");\n }\n\n fs.writeFileSync(outputPath, result.diffBuffer);\n}\n\n/**\n * Quick comparison function that returns only pass/fail\n * @param image1 - First image (file path or Buffer)\n * @param image2 - Second image (file path or Buffer)\n * @param options - Comparison options\n * @returns Whether the images match\n */\nexport async function imagesMatch(\n image1: string | Buffer,\n image2: string | Buffer,\n options: DiffConfig = {},\n): Promise<boolean> {\n const result = await compareImages(image1, image2, false, options);\n\n return result.passed;\n}\n\n/**\n * Check if the comparison passed based on the options\n * @param percentDifference - Percentage difference between the images\n * @param numDiffPixels - Number of different pixels\n * @param options - Comparison options\n * @returns Whether the comparison passed\n */\nconst isPassed = (\n percentDifference: number,\n numDiffPixels: number,\n options: DiffConfig,\n) => {\n if (options.maxDiffPercentage && options.maxDiffPixels) {\n return (\n percentDifference <= options.maxDiffPercentage &&\n numDiffPixels <= options.maxDiffPixels\n );\n }\n\n if (options.maxDiffPercentage) {\n return percentDifference <= options.maxDiffPercentage;\n }\n\n if (options.maxDiffPixels) {\n return numDiffPixels <= options.maxDiffPixels;\n }\n\n // fallback to 0 pixels\n return numDiffPixels === 0;\n};\n\n/**\n * Create different size png image with all pixels red\n * @returns\n */\nexport function createDiffSizePngImage(width: number, height: number): Buffer {\n const png = new PNG({ width, height });\n\n for (let y = 0; y < height; y++) {\n for (let x = 0; x < width; x++) {\n const idx = (width * y + x) << 2;\n png.data[idx] = 255; // red\n png.data[idx + 1] = 0; // green\n png.data[idx + 2] = 0; // blue\n png.data[idx + 3] = 255; // alpha\n }\n }\n\n return PNG.sync.write(png);\n}\n","import path from \"node:path\";\nimport { getLogger, type Logger } from \"@cappa/logger\";\nimport {\n type Browser,\n type BrowserContext,\n chromium,\n firefox,\n type Page,\n type PageScreenshotOptions,\n webkit,\n} from \"playwright-core\";\nimport {\n type CompareResult,\n compareImages,\n createDiffSizePngImage,\n} from \"./compare\";\nimport { ScreenshotFileSystem } from \"./filesystem\";\nimport type {\n DiffConfig,\n ScreenshotOptions,\n ScreenshotSettings,\n ScreenshotVariant,\n} from \"./types\";\n\nconst defaultDiffConfig: DiffConfig = {\n threshold: 0.1,\n includeAA: false,\n fastBufferCheck: true,\n maxDiffPixels: 0,\n maxDiffPercentage: 0,\n};\n\nexport interface ScreenshotCaptureDetails {\n filename: string;\n filepath?: string;\n comparisonResult?: CompareResult;\n diffImagePath?: string;\n skipped?: boolean;\n}\n\nexport interface ScreenshotVariantCaptureDetails\n extends ScreenshotCaptureDetails {\n id: string;\n label?: string;\n}\n\nexport interface ScreenshotCaptureResult {\n base: ScreenshotCaptureDetails;\n variants: ScreenshotVariantCaptureDetails[];\n}\n\nexport interface ScreenshotCaptureExtras {\n saveDiffImage?: boolean;\n diffImageFilename?: string;\n variants?: Record<\n string,\n {\n saveDiffImage?: boolean;\n diffImageFilename?: string;\n }\n >;\n}\n\nclass ScreenshotTool {\n browserType: \"chromium\" | \"firefox\" | \"webkit\";\n headless: boolean;\n viewport: { width: number; height: number };\n outputDir: string;\n browser: Browser | null = null;\n context: BrowserContext | null = null;\n page: Page | null = null;\n concurrency: number;\n contexts: BrowserContext[] = [];\n pages: Page[] = [];\n diff: DiffConfig;\n logger: Logger;\n retries: number;\n filesystem: ScreenshotFileSystem;\n\n constructor(options: {\n browserType?: \"chromium\" | \"firefox\" | \"webkit\";\n headless?: boolean;\n viewport?: { width: number; height: number };\n outputDir?: string;\n diff?: DiffConfig;\n retries?: number;\n concurrency?: number;\n }) {\n this.browserType = options.browserType || \"chromium\";\n this.headless = options.headless !== false; // Default to headless\n this.viewport = options.viewport || { width: 1920, height: 1080 };\n this.outputDir = options.outputDir || \"./screenshots\";\n this.diff = { ...defaultDiffConfig, ...options.diff };\n this.concurrency = Math.max(1, options.concurrency || 1);\n\n this.logger = getLogger();\n this.retries = options.retries || 2;\n this.filesystem = new ScreenshotFileSystem(this.outputDir);\n }\n\n /**\n * Initialize playwright and page\n */\n async init() {\n // Launch browser based on type\n const browserMap = {\n chromium,\n firefox,\n webkit,\n };\n\n const browserClass = browserMap[this.browserType];\n if (!browserClass) {\n throw new Error(`Unsupported browser type: ${this.browserType}`);\n }\n\n this.browser = await browserClass.launch({\n headless: this.headless,\n });\n\n const defaultUserAgent = (await this.browser.newContext())\n .newPage()\n .then((page) => page.evaluate(() => navigator.userAgent));\n\n // Create N contexts and pages\n this.contexts = [];\n this.pages = [];\n\n for (let i = 0; i < this.concurrency; i++) {\n const context = await this.browser.newContext({\n reducedMotion: \"reduce\",\n deviceScaleFactor: 2,\n userAgent: `${await defaultUserAgent} CappaStorybook`,\n viewport: this.viewport,\n });\n const page = await context.newPage();\n\n this.contexts.push(context);\n this.pages.push(page);\n }\n\n // Backward compatibility\n this.context = this.contexts[0] ?? null;\n this.page = this.pages[0] ?? null;\n }\n\n /**\n * Closes browser\n * If not closed, the process will not exit\n */\n async close() {\n for (const context of this.contexts) {\n await context.close();\n }\n if (this.browser) {\n await this.browser.close();\n }\n }\n\n /**\n * Navigates to a URL with \"domcontentloaded\" waitUntil\n */\n async goTo(page: Page, url: string) {\n if (!this.context) {\n throw new Error(\"Browser not initialized\");\n }\n await page.goto(url, { waitUntil: \"domcontentloaded\" });\n }\n\n /**\n * Takes a screenshot of the page\n */\n async takeScreenshot(\n page: Page,\n filename: string,\n options: ScreenshotSettings,\n ) {\n if (!this.browser) {\n throw new Error(\"Browser not initialized\");\n }\n\n if (options.skip) {\n return;\n }\n\n try {\n // Generate filename - save to actual directory\n const filepath = this.filesystem.getActualFilePath(filename);\n\n // Take screenshot\n const screenshotOptions = {\n path: filepath,\n fullPage: options.fullPage,\n type: \"png\" as const,\n timeout: 60000,\n mask: options.mask,\n omitBackground: options.omitBackground,\n scale: \"css\" as const,\n animations: \"disabled\",\n caret: \"hide\",\n } satisfies PageScreenshotOptions;\n\n await this.withViewport(page, options.viewport, async () => {\n if (options.delay) {\n await page.waitForTimeout(options.delay);\n }\n\n await page.screenshot(screenshotOptions);\n });\n\n return filepath;\n } catch (error) {\n this.logger.error(\n `Error taking screenshot of ${page.url()}:`,\n (error as Error).message,\n );\n throw error;\n }\n }\n\n /**\n * Sets the viewport size and executes the action, ensuring the original viewport is restored\n * even if the action throws an error.\n */\n private async withViewport<T>(\n page: Page,\n viewport: ScreenshotSettings[\"viewport\"],\n action: () => Promise<T>,\n ): Promise<T> {\n if (!viewport) {\n return action();\n }\n\n const previousViewport = page.viewportSize();\n\n // Check if the new viewport is the same as the current one\n if (\n previousViewport &&\n previousViewport.width === viewport.width &&\n previousViewport.height === viewport.height\n ) {\n return action();\n }\n\n await page.setViewportSize(viewport);\n\n try {\n return await action();\n } finally {\n // Always restore the original viewport, even if the action throws an error\n if (previousViewport) {\n await page.setViewportSize(previousViewport);\n }\n }\n }\n\n /**\n * Takes a screenshot and returns the buffer (for comparison purposes)\n */\n async takeScreenshotBuffer(\n page: Page,\n options: ScreenshotSettings,\n ): Promise<Buffer> {\n if (!this.browser) {\n throw new Error(\"Browser not initialized\");\n }\n\n if (options.skip) {\n throw new Error(\"Screenshot skipped\");\n }\n\n try {\n // Take screenshot without saving to file\n const screenshotOptions = {\n fullPage: options.fullPage,\n type: \"png\" as const,\n timeout: 60000,\n mask: options.mask,\n omitBackground: options.omitBackground,\n scale: \"css\" as const,\n animations: \"disabled\",\n caret: \"hide\",\n } satisfies PageScreenshotOptions;\n\n const buffer = await this.withViewport(\n page,\n options.viewport,\n async () => {\n if (options.delay) {\n await page.waitForTimeout(options.delay);\n }\n\n return page.screenshot(screenshotOptions);\n },\n );\n\n return buffer;\n } catch (error) {\n this.logger.error(\n `Error taking screenshot of ${page.url()}:`,\n (error as Error).message,\n );\n throw error;\n }\n }\n\n async takeScreenshotWithComparison(\n page: Page,\n filename: string,\n referenceImage: Buffer,\n options: ScreenshotSettings & {\n saveDiffImage?: boolean;\n diffImageFilename?: string;\n },\n ): Promise<{\n screenshotPath: string;\n comparisonResult: CompareResult;\n diffImagePath?: string;\n }> {\n if (!this.browser) {\n throw new Error(\"Browser not initialized\");\n }\n\n try {\n // Generate filename and take screenshot - save to actual directory\n const filepath = this.filesystem.getActualFilePath(filename);\n\n const screenshotSettings: ScreenshotSettings = {\n fullPage: options.fullPage,\n mask: options.mask,\n omitBackground: options.omitBackground,\n delay: options.delay,\n viewport: options.viewport,\n };\n\n const retryScreenshot = await this.retryScreenshot(\n page,\n referenceImage,\n screenshotSettings,\n );\n\n if (!retryScreenshot.screenshotPath) {\n throw new Error(\"Screenshot buffer is undefined\");\n }\n\n // Save screenshot to actual directory\n this.filesystem.writeActualFile(filename, retryScreenshot.screenshotPath);\n this.logger.success(`Screenshot saved: ${filepath}`);\n\n if (retryScreenshot.passed) {\n this.logger.success(`Screenshot passed visual comparison`);\n } else {\n this.logger.error(`Screenshot failed visual comparison`);\n }\n\n let diffImagePath: string | undefined;\n\n // Save diff image if requested and there are differences\n if (\n options.saveDiffImage &&\n retryScreenshot.comparisonResult.diffBuffer &&\n !retryScreenshot.passed\n ) {\n const diffFilename = options.diffImageFilename || filename;\n // Save to diff directory\n diffImagePath = this.filesystem.getDiffFilePath(diffFilename);\n\n this.filesystem.writeDiffFile(\n diffFilename,\n retryScreenshot.comparisonResult.diffBuffer,\n );\n\n this.logger.debug(`Diff image saved: ${diffImagePath}`);\n }\n\n // different sizes diff image\n if (retryScreenshot.comparisonResult.differentSizes) {\n this.logger.warn(`Screenshot has different sizes than reference image`);\n\n const diffFilename = options.diffImageFilename || filename;\n diffImagePath = this.filesystem.getDiffFilePath(diffFilename);\n const diffBuffer = createDiffSizePngImage(200, 200);\n\n this.filesystem.writeDiffFile(diffFilename, diffBuffer);\n\n this.logger.debug(`Diff Size image saved: ${diffImagePath}`);\n }\n\n return {\n screenshotPath: filepath,\n comparisonResult: retryScreenshot.comparisonResult,\n diffImagePath,\n };\n } catch (error) {\n this.logger.error(\n `Error taking screenshot with comparison of ${page.url()}:`,\n (error as Error).message,\n );\n throw error;\n }\n }\n\n async retryScreenshot(\n page: Page,\n referenceImage: Buffer,\n options: ScreenshotSettings,\n ) {\n for (let i = 0; i < this.retries; i++) {\n try {\n const screenshotBuffer = await this.takeScreenshotBuffer(page, {\n ...options,\n delay: this.getIncBackoffDelay(i, options.delay || 0),\n });\n\n if (!screenshotBuffer) {\n throw new Error(\"Screenshot buffer is undefined\");\n }\n\n const comparisonResult = await compareImages(\n screenshotBuffer,\n referenceImage,\n true,\n this.diff,\n );\n\n // bail early if the images are different sizes\n if (comparisonResult.differentSizes) {\n return {\n screenshotPath: screenshotBuffer,\n comparisonResult,\n passed: false,\n };\n }\n\n // last retry\n if (i === this.retries - 1 && !comparisonResult.passed) {\n this.logger.error(\n `Failed to match screenshot after ${this.retries} retries`,\n );\n return {\n screenshotPath: screenshotBuffer,\n comparisonResult,\n passed: false,\n };\n }\n\n if (comparisonResult.passed) {\n return {\n screenshotPath: screenshotBuffer,\n comparisonResult,\n passed: true,\n };\n } else {\n this.logger.warn(\n `Comparison did not match. Retrying in ${this.getIncBackoffDelay(i + 1, options.delay || 0)}ms... \\nComparison result: ${comparisonResult.numDiffPixels} pixels different (${comparisonResult.percentDifference.toFixed(2)}%)`,\n );\n }\n } catch (error) {\n this.logger.error(\n `Error taking screenshot after ${i + 1} retries:`,\n (error as Error).message,\n );\n }\n }\n\n return {\n screenshotPath: null,\n comparisonResult: null,\n passed: false,\n };\n }\n\n getVariantFilename(filename: string, variant: ScreenshotVariant): string {\n if (variant.filename) {\n return variant.filename;\n }\n\n const parsed = path.parse(filename);\n const sanitizedId = variant.id\n .trim()\n .replace(/[^a-zA-Z0-9_-]+/g, \"-\")\n .replace(/-+/g, \"-\")\n .replace(/^-|-$/g, \"\");\n const suffix = sanitizedId || \"variant\";\n const ext = parsed.ext || \".png\";\n const variantName = `${parsed.name}--${suffix}${ext}`;\n\n return parsed.dir ? path.join(parsed.dir, variantName) : variantName;\n }\n\n async capture(\n page: Page,\n filename: string,\n options: ScreenshotOptions,\n extras: ScreenshotCaptureExtras = {},\n ): Promise<ScreenshotCaptureResult> {\n if (!this.browser) {\n throw new Error(\"Browser not initialized\");\n }\n\n const { variants = [], ...baseOptions } = options;\n const baseResult: ScreenshotCaptureDetails = { filename };\n const variantResults: ScreenshotVariantCaptureDetails[] = [];\n\n const baseSaveDiff = extras.saveDiffImage ?? false;\n const baseDiffFilename = extras.diffImageFilename ?? filename;\n\n if (baseOptions.skip) {\n return {\n base: { ...baseResult, skipped: true },\n variants: variants.map((variant) => ({\n id: variant.id,\n label: variant.label,\n filename: this.getVariantFilename(filename, variant),\n skipped: true,\n })),\n };\n }\n\n if (this.filesystem.hasExpectedFile(filename)) {\n const { screenshotPath, comparisonResult, diffImagePath } =\n await this.takeScreenshotWithComparison(\n page,\n filename,\n this.filesystem.readExpectedFile(filename),\n {\n ...baseOptions,\n saveDiffImage: baseSaveDiff,\n diffImageFilename: baseDiffFilename,\n },\n );\n\n baseResult.filepath = screenshotPath;\n baseResult.comparisonResult = comparisonResult;\n baseResult.diffImagePath = diffImagePath;\n } else {\n baseResult.filepath = await this.takeScreenshot(\n page,\n filename,\n baseOptions,\n );\n }\n\n for (const variant of variants) {\n const variantFilename = this.getVariantFilename(filename, variant);\n const variantOptions: ScreenshotSettings = {\n ...baseOptions,\n ...variant.options,\n };\n\n const variantResult: ScreenshotVariantCaptureDetails = {\n id: variant.id,\n label: variant.label,\n filename: variantFilename,\n };\n\n if (variantOptions.skip) {\n variantResult.skipped = true;\n variantResults.push(variantResult);\n continue;\n }\n\n const variantExtra = extras.variants?.[variant.id];\n const variantSaveDiff = variantExtra?.saveDiffImage ?? baseSaveDiff;\n const variantDiffFilename =\n variantExtra?.diffImageFilename ?? variantFilename;\n\n if (this.filesystem.hasExpectedFile(variantFilename)) {\n const { screenshotPath, comparisonResult, diffImagePath } =\n await this.takeScreenshotWithComparison(\n page,\n variantFilename,\n this.filesystem.readExpectedFile(variantFilename),\n {\n ...variantOptions,\n saveDiffImage: variantSaveDiff,\n diffImageFilename: variantDiffFilename,\n },\n );\n\n variantResult.filepath = screenshotPath;\n variantResult.comparisonResult = comparisonResult;\n variantResult.diffImagePath = diffImagePath;\n } else {\n variantResult.filepath = await this.takeScreenshot(\n page,\n variantFilename,\n variantOptions,\n );\n }\n\n variantResults.push(variantResult);\n }\n\n return {\n base: baseResult,\n variants: variantResults,\n };\n }\n\n /**\n * Gets the exponential backoff delay\n */\n getIncBackoffDelay(i: number, delay: number): number {\n return i > 0 ? 500 * 2 ** (i - 1) + (delay || 0) : 0;\n }\n\n /**\n * Get a page from the pool by index\n */\n getPageFromPool(index: number): Page {\n if (index < 0) {\n throw new Error(`Page index ${index} must be non-negative`);\n }\n\n if (index >= this.pages.length) {\n throw new Error(\n `Page index ${index} exceeds pool size ${this.pages.length}`,\n );\n }\n\n if (!this.pages[index]) {\n throw new Error(`Page index ${index} is undefined`);\n }\n\n return this.pages[index];\n }\n}\n\nexport default ScreenshotTool;\n"],"mappings":";;;;;;;;;;;AAKA,SAAgB,aACd,SAGgB;AAChB,QAAO;;;;;;;;ACJT,IAAa,uBAAb,MAAkC;CAChC,AAAiB;CACjB,AAAiB;CACjB,AAAiB;CAEjB,YAAY,WAAmB;AAC7B,OAAK,YAAY,KAAK,QAAQ,WAAW,SAAS;AAClD,OAAK,cAAc,KAAK,QAAQ,WAAW,WAAW;AACtD,OAAK,UAAU,KAAK,QAAQ,WAAW,OAAO;AAE9C,OAAK,gBAAgB,KAAK,UAAU;AACpC,OAAK,gBAAgB,KAAK,YAAY;AACtC,OAAK,gBAAgB,KAAK,QAAQ;;CAGpC,cAAc;AACZ,OAAK,UAAU,KAAK,UAAU;;CAGhC,YAAY;AACV,OAAK,UAAU,KAAK,QAAQ;;CAG9B,qBAAq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OACtB,OAAM,IAAI,MACR,cAAc,MAAM,qBAAqB,KAAK,MAAM,SACrD;AAGH,MAAI,CAAC,KAAK,MAAM,OACd,OAAM,IAAI,MAAM,cAAc,MAAM,eAAe;AAGrD,SAAO,KAAK,MAAM;;;AAItB,yBAAe"}
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "@cappa/core",
3
- "version": "0.1.2",
3
+ "version": "0.2.0",
4
4
  "description": "Core Playwright screenshot functionality for Cappa",
5
- "author": "tim-richter",
5
+ "author": "Tim Richter",
6
6
  "license": "MIT",
7
7
  "publishConfig": {
8
8
  "access": "public"
@@ -18,21 +18,19 @@
18
18
  "./package.json": "./package.json"
19
19
  },
20
20
  "dependencies": {
21
- "@blazediff/core": "^1.1.0",
22
- "@blazediff/types": "^1.1.0",
23
- "pixelmatch": "^6.0.0",
24
- "playwright-core": "^1.55.0",
21
+ "@blazediff/core": "^1.4.1",
22
+ "@blazediff/types": "^1.4.1",
23
+ "playwright-core": "^1.56.0",
25
24
  "pngjs": "^7.0.0",
26
- "@cappa/logger": "0.0.2"
25
+ "@cappa/logger": "0.0.3"
27
26
  },
28
27
  "devDependencies": {
29
- "@types/node": "22.13.10",
30
- "@types/pixelmatch": "^5.2.4",
31
- "@types/pngjs": "^6.0.4",
28
+ "@types/node": "24.8.0",
29
+ "@types/pngjs": "^6.0.5",
32
30
  "@vitest/coverage-v8": "3.2.4",
33
31
  "@vitest/ui": "3.2.4",
34
- "tsdown": "0.14.1",
35
- "typescript": "5.7.3",
32
+ "tsdown": "0.15.7",
33
+ "typescript": "5.9.3",
36
34
  "vitest": "3.2.4"
37
35
  },
38
36
  "keywords": [
@@ -185,9 +185,13 @@ describe("compare", () => {
185
185
  const largeImage = createSolidColorPNG(100, 100, [255, 0, 0, 255]);
186
186
  const smallImage = createSolidColorPNG(50, 50, [255, 0, 0, 255]);
187
187
 
188
- await expect(compareImages(largeImage, smallImage)).rejects.toThrow(
189
- "Image sizes do not match. Image 1 size: 40000, image 2 size: 10000",
190
- );
188
+ const result = await compareImages(largeImage, smallImage);
189
+
190
+ expect(result.differentSizes).toBe(true);
191
+ expect(result.numDiffPixels).toBe(0);
192
+ expect(result.totalPixels).toBe(0);
193
+ expect(result.percentDifference).toBe(0);
194
+ expect(result.passed).toBe(false);
191
195
  });
192
196
 
193
197
  it("should handle custom compare options", async () => {
@@ -307,6 +311,7 @@ describe("compare", () => {
307
311
  totalPixels: 100,
308
312
  percentDifference: 0,
309
313
  passed: true,
314
+ differentSizes: false,
310
315
  // diffBuffer is undefined
311
316
  };
312
317
 
@@ -353,7 +358,7 @@ describe("compare", () => {
353
358
  const result = await compareImages(redImage, blueImage, false);
354
359
 
355
360
  expect(result.passed).toBe(false);
356
- expect(result.percentDifference).toBe(56.00000000000001);
361
+ expect(result.percentDifference).toBe(100);
357
362
  });
358
363
 
359
364
  it("should handle zero difference threshold", async () => {
package/src/compare.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import fs from "node:fs";
2
2
  import blazediff from "@blazediff/core";
3
3
  import type { BlazeDiffOptions } from "@blazediff/types";
4
+ import { getLogger } from "@cappa/logger";
4
5
  import { PNG } from "pngjs";
5
6
  import type { DiffConfig } from "./types";
6
7
 
@@ -12,6 +13,8 @@ export interface CompareResult {
12
13
  percentDifference: number;
13
14
  diffBuffer?: Buffer; // PNG buffer of the diff image
14
15
  passed: boolean; // Whether the comparison passed based on threshold
16
+ error?: string; // Error message if the comparison failed
17
+ differentSizes: boolean; // Whether the images have different sizes
15
18
  }
16
19
 
17
20
  const compare = (
@@ -39,36 +42,59 @@ export async function compareImages(
39
42
  withDiff: boolean = false,
40
43
  options: DiffConfig = {},
41
44
  ): Promise<CompareResult> {
42
- // Load images as PNG objects
43
45
  const png1 = await loadPNG(image1);
44
46
  const png2 = await loadPNG(image2);
45
47
 
46
48
  const { width, height } = png1;
47
49
 
50
+ if (width !== png2.width || height !== png2.height) {
51
+ return {
52
+ numDiffPixels: 0,
53
+ totalPixels: 0,
54
+ percentDifference: 0,
55
+ passed: false,
56
+ differentSizes: true,
57
+ };
58
+ }
59
+
48
60
  const diff = withDiff ? new PNG({ width, height }) : undefined;
49
61
 
50
- const numDiffPixels = compare(
51
- png1.data,
52
- png2.data,
53
- diff?.data,
54
- width,
55
- height,
56
- options,
57
- );
58
-
59
- const totalPixels = width * height;
60
- const percentDifference = (numDiffPixels / totalPixels) * 100;
61
-
62
- // Convert diff image to buffer
63
- const diffBuffer = diff ? PNG.sync.write(diff) : undefined;
64
-
65
- return {
66
- numDiffPixels,
67
- totalPixels,
68
- percentDifference,
69
- diffBuffer,
70
- passed: isPassed(percentDifference, numDiffPixels, options),
71
- };
62
+ try {
63
+ const numDiffPixels = compare(
64
+ png1.data,
65
+ png2.data,
66
+ diff?.data,
67
+ width,
68
+ height,
69
+ options,
70
+ );
71
+
72
+ const totalPixels = width * height;
73
+ const percentDifference = (numDiffPixels / totalPixels) * 100;
74
+
75
+ // Convert diff image to buffer
76
+ const diffBuffer = diff ? PNG.sync.write(diff) : undefined;
77
+
78
+ return {
79
+ numDiffPixels,
80
+ totalPixels,
81
+ percentDifference,
82
+ diffBuffer,
83
+ passed: isPassed(percentDifference, numDiffPixels, options),
84
+ differentSizes: false,
85
+ };
86
+ } catch (error) {
87
+ getLogger().error((error as Error).message || "Unknown error");
88
+
89
+ return {
90
+ numDiffPixels: 0,
91
+ totalPixels: 0,
92
+ percentDifference: 0,
93
+ passed: false,
94
+ differentSizes: false,
95
+ error: (error as Error).message || "Unknown error",
96
+ };
97
+ }
72
98
  }
73
99
 
74
100
  /**
@@ -113,6 +139,13 @@ export async function imagesMatch(
113
139
  return result.passed;
114
140
  }
115
141
 
142
+ /**
143
+ * Check if the comparison passed based on the options
144
+ * @param percentDifference - Percentage difference between the images
145
+ * @param numDiffPixels - Number of different pixels
146
+ * @param options - Comparison options
147
+ * @returns Whether the comparison passed
148
+ */
116
149
  const isPassed = (
117
150
  percentDifference: number,
118
151
  numDiffPixels: number,
@@ -136,3 +169,23 @@ const isPassed = (
136
169
  // fallback to 0 pixels
137
170
  return numDiffPixels === 0;
138
171
  };
172
+
173
+ /**
174
+ * Create different size png image with all pixels red
175
+ * @returns
176
+ */
177
+ export function createDiffSizePngImage(width: number, height: number): Buffer {
178
+ const png = new PNG({ width, height });
179
+
180
+ for (let y = 0; y < height; y++) {
181
+ for (let x = 0; x < width; x++) {
182
+ const idx = (width * y + x) << 2;
183
+ png.data[idx] = 255; // red
184
+ png.data[idx + 1] = 0; // green
185
+ png.data[idx + 2] = 0; // blue
186
+ png.data[idx + 3] = 255; // alpha
187
+ }
188
+ }
189
+
190
+ return PNG.sync.write(png);
191
+ }
@@ -0,0 +1,83 @@
1
+ import fs from "node:fs";
2
+ import os from "node:os";
3
+ import path from "node:path";
4
+ import { afterEach, beforeEach, describe, expect, it } from "vitest";
5
+ import { ScreenshotFileSystem } from "./filesystem";
6
+
7
+ let tempDir: string;
8
+
9
+ beforeEach(() => {
10
+ tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "cappa-fs-"));
11
+ fs.mkdirSync(path.join(tempDir, "actual"), { recursive: true });
12
+ fs.mkdirSync(path.join(tempDir, "expected"), { recursive: true });
13
+ fs.mkdirSync(path.join(tempDir, "diff"), { recursive: true });
14
+ });
15
+
16
+ afterEach(() => {
17
+ fs.rmSync(tempDir, { recursive: true, force: true });
18
+ });
19
+
20
+ describe("ScreenshotFileSystem", () => {
21
+ it("clears existing actual and diff directories", () => {
22
+ const fileSystem = new ScreenshotFileSystem(tempDir);
23
+
24
+ const actualFile = path.join(tempDir, "actual", "example.png");
25
+ const diffFile = path.join(tempDir, "diff", "example.png");
26
+
27
+ fs.writeFileSync(actualFile, "actual");
28
+ fs.writeFileSync(diffFile, "diff");
29
+
30
+ fileSystem.clearActual();
31
+ fileSystem.clearDiff();
32
+
33
+ expect(fs.existsSync(path.join(tempDir, "actual"))).toBe(false);
34
+ expect(fs.existsSync(path.join(tempDir, "diff"))).toBe(false);
35
+ });
36
+
37
+ it("approves screenshots from absolute actual paths and removes diffs", () => {
38
+ const fileSystem = new ScreenshotFileSystem(tempDir);
39
+
40
+ const actualPath = path.join(tempDir, "actual", "component", "button.png");
41
+ const expectedPath = path.join(
42
+ tempDir,
43
+ "expected",
44
+ "component",
45
+ "button.png",
46
+ );
47
+ const diffPath = path.join(tempDir, "diff", "component", "button.png");
48
+
49
+ fs.mkdirSync(path.dirname(actualPath), { recursive: true });
50
+ fs.mkdirSync(path.dirname(diffPath), { recursive: true });
51
+
52
+ fs.writeFileSync(actualPath, "button");
53
+ fs.writeFileSync(diffPath, "diff");
54
+
55
+ const result = fileSystem.approveFromActualPath(actualPath);
56
+
57
+ expect(fs.readFileSync(expectedPath, "utf-8")).toBe("button");
58
+ expect(fs.existsSync(diffPath)).toBe(false);
59
+ expect(result).toEqual({
60
+ actualPath,
61
+ expectedPath,
62
+ diffPath,
63
+ });
64
+ });
65
+
66
+ it("approves screenshots by logical name", () => {
67
+ const fileSystem = new ScreenshotFileSystem(tempDir);
68
+
69
+ const actualPath = path.join(
70
+ tempDir,
71
+ "actual",
72
+ "pages",
73
+ "home",
74
+ "hero.png",
75
+ );
76
+ fs.mkdirSync(path.dirname(actualPath), { recursive: true });
77
+ fs.writeFileSync(actualPath, "hero");
78
+
79
+ const { expectedPath } = fileSystem.approveByName("pages/home/hero");
80
+
81
+ expect(fs.readFileSync(expectedPath, "utf-8")).toBe("hero");
82
+ });
83
+ });
@@ -0,0 +1,202 @@
1
+ import fs from "node:fs";
2
+ import path from "node:path";
3
+
4
+ /**
5
+ * Class for interacting with the local file system to store the screenshots.
6
+ */
7
+ export class ScreenshotFileSystem {
8
+ private readonly actualDir: string;
9
+ private readonly expectedDir: string;
10
+ private readonly diffDir: string;
11
+
12
+ constructor(outputDir: string) {
13
+ this.actualDir = path.resolve(outputDir, "actual");
14
+ this.expectedDir = path.resolve(outputDir, "expected");
15
+ this.diffDir = path.resolve(outputDir, "diff");
16
+
17
+ this.ensureParentDir(this.actualDir);
18
+ this.ensureParentDir(this.expectedDir);
19
+ this.ensureParentDir(this.diffDir);
20
+ }
21
+
22
+ clearActual() {
23
+ this.removeDir(this.actualDir);
24
+ }
25
+
26
+ clearDiff() {
27
+ this.removeDir(this.diffDir);
28
+ }
29
+
30
+ clearActualAndDiff() {
31
+ this.clearActual();
32
+ this.clearDiff();
33
+ }
34
+
35
+ /**
36
+ * Approve a screenshot based on its actual file path.
37
+ * @param actualFilePath - The path to the actual screenshot file.
38
+ * @returns The paths to the actual, expected, and diff screenshot files.
39
+ */
40
+ approveFromActualPath(actualFilePath: string) {
41
+ const actualAbsolute = this.resolveActualPath(actualFilePath);
42
+ const relativePath = path.relative(this.actualDir, actualAbsolute);
43
+
44
+ if (relativePath.startsWith("..")) {
45
+ throw new Error(
46
+ `Cannot approve screenshot outside of actual directory: ${actualFilePath}`,
47
+ );
48
+ }
49
+
50
+ return this.approveRelative(relativePath);
51
+ }
52
+
53
+ /**
54
+ * Approve a screenshot based on its name.
55
+ * @param name - The name of the screenshot. Can include '.png' extension.
56
+ * @returns The paths to the actual, expected, and diff screenshot files.
57
+ */
58
+ approveByName(name: string) {
59
+ const relativeWithExtension = name.endsWith(".png") ? name : `${name}.png`;
60
+
61
+ return this.approveRelative(relativeWithExtension);
62
+ }
63
+
64
+ /**
65
+ * Approve a screenshot based on a relative path.
66
+ * @param relativePath - The relative path to the screenshot.
67
+ * @returns The paths to the actual, expected, and diff screenshot files.
68
+ */
69
+ private approveRelative(relativePath: string) {
70
+ const actualPath = path.resolve(this.actualDir, relativePath);
71
+ const expectedPath = path.resolve(this.expectedDir, relativePath);
72
+ const diffPath = path.resolve(this.diffDir, relativePath);
73
+
74
+ this.ensureParentDir(expectedPath);
75
+
76
+ fs.copyFileSync(actualPath, expectedPath);
77
+
78
+ if (fs.existsSync(diffPath)) {
79
+ fs.unlinkSync(diffPath);
80
+ }
81
+
82
+ return {
83
+ actualPath,
84
+ expectedPath,
85
+ diffPath,
86
+ };
87
+ }
88
+
89
+ /**
90
+ * Ensure the parent directory of a file exists. If not, it creates it.
91
+ * @param filePath - The path to the file.
92
+ */
93
+ private ensureParentDir(filePath: string) {
94
+ const dir = path.dirname(filePath);
95
+
96
+ if (!fs.existsSync(dir)) {
97
+ fs.mkdirSync(dir, { recursive: true });
98
+ }
99
+ }
100
+
101
+ /**
102
+ * Remove a directory and all its contents.
103
+ * @param dir - The path to the directory.
104
+ */
105
+ private removeDir(dir: string) {
106
+ if (fs.existsSync(dir)) {
107
+ fs.rmSync(dir, { recursive: true, force: true });
108
+ }
109
+ }
110
+
111
+ /**
112
+ * Get the actual directory path
113
+ */
114
+ getActualDir(): string {
115
+ return this.actualDir;
116
+ }
117
+
118
+ /**
119
+ * Get the expected directory path
120
+ */
121
+ getExpectedDir(): string {
122
+ return this.expectedDir;
123
+ }
124
+
125
+ /**
126
+ * Get the diff directory path
127
+ */
128
+ getDiffDir(): string {
129
+ return this.diffDir;
130
+ }
131
+
132
+ /**
133
+ * Get the path to an actual screenshot file
134
+ */
135
+ getActualFilePath(filename: string): string {
136
+ return path.resolve(this.actualDir, filename);
137
+ }
138
+
139
+ /**
140
+ * Get the path to an expected screenshot file
141
+ */
142
+ getExpectedFilePath(filename: string): string {
143
+ return path.resolve(this.expectedDir, filename);
144
+ }
145
+
146
+ /**
147
+ * Get the path to a diff screenshot file
148
+ */
149
+ getDiffFilePath(filename: string): string {
150
+ return path.resolve(this.diffDir, filename);
151
+ }
152
+
153
+ /**
154
+ * Write a file to the actual directory
155
+ */
156
+ writeActualFile(filename: string, data: Buffer): void {
157
+ const filepath = this.getActualFilePath(filename);
158
+ this.ensureParentDir(filepath);
159
+ fs.writeFileSync(filepath, data);
160
+ }
161
+
162
+ /**
163
+ * Write a file to the diff directory
164
+ */
165
+ writeDiffFile(filename: string, data: Buffer): void {
166
+ const filepath = this.getDiffFilePath(filename);
167
+ this.ensureParentDir(filepath);
168
+ fs.writeFileSync(filepath, data);
169
+ }
170
+
171
+ /**
172
+ * Check if an expected file exists
173
+ */
174
+ hasExpectedFile(filename: string): boolean {
175
+ const expectedPath = this.getExpectedFilePath(filename);
176
+ return fs.existsSync(expectedPath);
177
+ }
178
+
179
+ /**
180
+ * Read an expected file
181
+ */
182
+ readExpectedFile(filename: string): Buffer {
183
+ const expectedPath = this.getExpectedFilePath(filename);
184
+ if (!fs.existsSync(expectedPath)) {
185
+ throw new Error(`Expected image not found: ${expectedPath}`);
186
+ }
187
+ return fs.readFileSync(expectedPath);
188
+ }
189
+
190
+ /**
191
+ * Resolve the actual path of a screenshot file.
192
+ * @param filePath - The path to the screenshot file.
193
+ * @returns The actual path of the screenshot file.
194
+ */
195
+ private resolveActualPath(filePath: string) {
196
+ if (path.isAbsolute(filePath)) {
197
+ return filePath;
198
+ }
199
+
200
+ return path.resolve(this.actualDir, filePath);
201
+ }
202
+ }
package/src/index.ts CHANGED
@@ -1,15 +1,36 @@
1
1
  import { defineConfig } from "./config";
2
+ import { ScreenshotFileSystem } from "./filesystem";
2
3
  import type { Plugin, PluginDef, PluginFunction } from "./plugin";
3
- import ScreenshotTool from "./screenshot";
4
- import type { Screenshot, ScreenshotOptions, UserConfig } from "./types";
4
+ import ScreenshotTool, {
5
+ type ScreenshotCaptureDetails,
6
+ type ScreenshotCaptureExtras,
7
+ type ScreenshotCaptureResult,
8
+ type ScreenshotVariantCaptureDetails,
9
+ } from "./screenshot";
10
+ import type {
11
+ Screenshot,
12
+ ScreenshotOptions,
13
+ ScreenshotSettings,
14
+ ScreenshotVariant,
15
+ UserConfig,
16
+ Viewport,
17
+ } from "./types";
5
18
 
6
19
  export {
7
20
  ScreenshotTool,
21
+ ScreenshotFileSystem,
8
22
  defineConfig,
9
23
  type PluginFunction,
10
24
  type Plugin,
11
25
  type PluginDef,
12
26
  type UserConfig,
13
27
  type ScreenshotOptions,
28
+ type ScreenshotSettings,
29
+ type ScreenshotVariant,
14
30
  type Screenshot,
31
+ type ScreenshotCaptureResult,
32
+ type ScreenshotCaptureDetails,
33
+ type ScreenshotVariantCaptureDetails,
34
+ type ScreenshotCaptureExtras,
35
+ type Viewport,
15
36
  };