visreg-test
Version:
A visual regression testing solution that offers an easy setup with simple yet powerful customisation options, wrapped up in a convenient CLI runner to make assessing and accepting/rejecting diffs a breeze.
80 lines (79 loc) • 3.2 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const path = require("path");
const get_image_1 = require("../../services/get-image");
const fs_1 = require("fs");
const crypto_1 = require("crypto");
const stream_1 = require("stream");
const util_1 = require("util");
const utils_1 = require("../../../utils");
const asyncPipeline = (0, util_1.promisify)(stream_1.pipeline);
const express = require('express');
const router = express.Router();
const MAX_CACHE_SIZE = 20;
const imageCache = new Map();
const cacheKeys = new Array();
const addToCache = (key, value) => {
if (cacheKeys.length >= MAX_CACHE_SIZE) {
const oldestKey = cacheKeys.shift();
if (oldestKey) {
imageCache.delete(oldestKey);
}
}
imageCache.set(key, value);
cacheKeys.push(key);
};
const computeHash = (filePath) => __awaiter(void 0, void 0, void 0, function* () {
const hash = (0, crypto_1.createHash)('md5');
yield asyncPipeline((0, fs_1.createReadStream)(filePath), hash);
return hash.digest('hex');
});
router.get('/image/:suiteSlug/:fileName', (req, res) => __awaiter(void 0, void 0, void 0, function* () {
const { suiteSlug, fileName } = req.params;
const fileInfo = getFileInfo(req.local.suitesDirectory, suiteSlug, fileName);
const filePath = fileInfo.filePath;
const hash = yield computeHash(filePath);
if (imageCache.has(hash)) {
res.send(imageCache.get(hash));
return;
}
const image = yield (0, get_image_1.getImage)(fileInfo);
addToCache(hash, image);
res.send(image);
}));
const getFileInfo = (suitesDirectory, suiteSlug, fileName) => {
const suiteImagesDir = path.join(suitesDirectory, suiteSlug, 'snapshots/snaps');
const cleanName = (0, utils_1.getFileNameWithoutExtension)(fileName);
const baselinePath = path.join(suiteImagesDir, cleanName + '.base.png');
const receivedPath = path.join(suiteImagesDir, '__received_output__', cleanName + '-received.png');
const diffPath = path.join(suiteImagesDir, '__diff_output__', cleanName + '.diff.png');
const type = (0, utils_1.getFileType)(fileName);
let filePath = '';
if (type === 'diff') {
filePath = diffPath;
}
else if (type === 'received') {
filePath = receivedPath;
}
else {
filePath = baselinePath;
}
return {
fileName,
filePath,
cleanName,
suiteImagesDir,
type,
suiteSlug,
};
};
exports.default = router;