testplane
Version:
Tests framework based on mocha and wdio
127 lines • 6.08 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.saveViewportImageForDebugIfNeeded = exports.saveRenderedPiecesForDebugIfNeeded = exports.saveViewportImageWithDebugRects = exports.COMPOSITE_IMAGE_DEBUG_COLORS = void 0;
const node_fs_1 = __importDefault(require("node:fs"));
const eight_bit_rgba_to_png_1 = require("../../../utils/eight-bit-rgba-to-png");
const preload_utils_1 = require("../../../utils/preload-utils");
const node_path_1 = __importDefault(require("node:path"));
/*
This file is used for debugging purposes only, to produce images with capture areas, safe areas, etc. visible when TESTPLANE_DEBUG_SCREENSHOTS is set
Green frame means safe area, red means area we want to capture.
*/
exports.COMPOSITE_IMAGE_DEBUG_COLORS = {
safeArea: { r: 0, g: 255, b: 0, a: 255 }, // green
captureSpecVisible: { r: 255, g: 0, b: 0, a: 255 }, // red
visibleCoveringRect: { r: 255, g: 105, b: 180, a: 255 }, // pink
};
const initJsquashPromise = new Promise(resolve => {
const wasmLocation = require.resolve("@jsquash/png/codec/pkg/squoosh_png_bg.wasm");
Promise.all([
(0, preload_utils_1.loadEsm)("@jsquash/png/decode.js"),
node_fs_1.default.promises.readFile(wasmLocation),
])
.then(([mod, wasmBytes]) => mod.init(wasmBytes))
.then(resolve);
});
const decodePngToRgba = async (buffer) => {
const [mod] = await Promise.all([
(0, preload_utils_1.loadEsm)("@jsquash/png/decode.js"),
initJsquashPromise,
]);
const arrayBuffer = buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength);
const imageData = await mod.decode(arrayBuffer, { bitDepth: 8 });
return {
data: Buffer.from(imageData.data.buffer, imageData.data.byteOffset, imageData.data.byteLength),
width: imageData.width,
height: imageData.height,
};
};
function setPixel(rgbaData, imageWidth, imageHeight, x, y, color) {
if (x < 0 || y < 0 || x >= imageWidth || y >= imageHeight) {
return;
}
const offset = (y * imageWidth + x) * 4;
rgbaData[offset] = color.r;
rgbaData[offset + 1] = color.g;
rgbaData[offset + 2] = color.b;
rgbaData[offset + 3] = color.a;
}
function drawRectOutline(rgbaData, imageWidth, imageHeight, rect, color) {
const left = Math.max(0, Math.floor(rect.left));
const top = Math.max(0, Math.floor(rect.top));
const right = Math.min(imageWidth - 1, Math.ceil(rect.left + rect.width) - 1);
const bottom = Math.min(imageHeight - 1, Math.ceil(rect.top + rect.height) - 1);
if (right < left || bottom < top) {
return;
}
for (let x = left; x <= right; x++) {
setPixel(rgbaData, imageWidth, imageHeight, x, top, color);
setPixel(rgbaData, imageWidth, imageHeight, x, bottom, color);
}
for (let y = top; y <= bottom; y++) {
setPixel(rgbaData, imageWidth, imageHeight, left, y, color);
setPixel(rgbaData, imageWidth, imageHeight, right, y, color);
}
}
async function saveViewportImageWithDebugRects(image, rects, outputPath) {
const pngBuffer = await image.toPngBuffer({ resolveWithObject: false });
const { data, width, height } = await decodePngToRgba(pngBuffer);
for (const { rect, color } of rects) {
drawRectOutline(data, width, height, rect, color);
}
const annotatedPngBuffer = (0, eight_bit_rgba_to_png_1.convertRgbaToPng)(data, width, height);
await node_fs_1.default.promises.writeFile(outputPath, annotatedPngBuffer);
}
exports.saveViewportImageWithDebugRects = saveViewportImageWithDebugRects;
async function saveRenderedPiecesForDebugIfNeeded(renderedPieces, destinationDirPath) {
if (process.env.TESTPLANE_DEBUG_SCREENSHOTS && destinationDirPath) {
const tmpDir = node_path_1.default.join(destinationDirPath, "rendered-pieces");
node_fs_1.default.mkdirSync(tmpDir, { recursive: true });
for (let index = 0; index < renderedPieces.length; index++) {
const renderedPiece = renderedPieces[index];
const piecePath = node_path_1.default.join(tmpDir, `rendered-piece-${index}.png`);
await renderedPiece.save(piecePath);
}
console.log(`Testplane Composite image pieces saved to ${destinationDirPath}`);
}
}
exports.saveRenderedPiecesForDebugIfNeeded = saveRenderedPiecesForDebugIfNeeded;
async function saveViewportImageForDebugIfNeeded(chunkIndex, viewportImage, imageSize, safeArea, captureSpecs, visibleCoveringRect, destinationDirPath) {
if (!process.env.TESTPLANE_DEBUG_SCREENSHOTS || !destinationDirPath) {
return;
}
try {
const viewportDebugDir = node_path_1.default.join(destinationDirPath, `viewports`);
node_fs_1.default.mkdirSync(viewportDebugDir, { recursive: true });
const debugRects = [
{
rect: {
left: 0,
top: safeArea.top,
width: imageSize.width,
height: safeArea.height,
},
color: exports.COMPOSITE_IMAGE_DEBUG_COLORS.safeArea,
},
...captureSpecs
.filter(spec => spec.visible.width > 0 && spec.visible.height > 0)
.map(spec => ({
rect: spec.visible,
color: exports.COMPOSITE_IMAGE_DEBUG_COLORS.captureSpecVisible,
})),
{
rect: visibleCoveringRect,
color: exports.COMPOSITE_IMAGE_DEBUG_COLORS.visibleCoveringRect,
},
];
await saveViewportImageWithDebugRects(viewportImage, debugRects, node_path_1.default.join(viewportDebugDir, `viewport-${chunkIndex}.png`));
}
catch (error) {
console.warn("Failed to save viewport debug image: %O", error);
}
}
exports.saveViewportImageForDebugIfNeeded = saveViewportImageForDebugIfNeeded;
//# sourceMappingURL=debug-utils.js.map