<commit_before>const puppeteer = require("puppeteer");
const commander = require("commander");
const path = require("path");
const host = "localhost:9991";
const options = {};

commander
    .arguments('<integration> <message_id>')
    .action((integration, messageId) => {
        options.integration = integration;
        options.messageId = messageId;
        console.log(`Capturing screenshot for ${integration} using message ${messageId}`);
    })
    .parse(process.argv);

if (options.integration === undefined) {
    console.error('no integration specified!');
    process.exit(1);
}

// TODO: Refactor to share code with frontend_tests/puppeteer_tests/00-realm-creation.js
async function run() {
    const browser = await puppeteer.launch({
        args: [
            '--window-size=1400,1024',
            '--no-sandbox', '--disable-setuid-sandbox',
            // Helps render fonts correctly on Ubuntu: https://github.com/puppeteer/puppeteer/issues/661
            '--font-render-hinting=none',
        ],
        defaultViewport: null,
        headless: true,
    });
    try {
        const page = await browser.newPage();
        // deviceScaleFactor:2 gives better quality screenshots (higher pixel density)
        await page.setViewport({ width: 1280, height: 1024, deviceScaleFactor: 2 });
        await page.goto('http://' + host);
        // wait for devlogin admin button and click on it
        await page.waitForSelector('.btn-admin');
        await page.click('.btn-admin');

        // Navigate to message and capture screenshot
        await page.goto(`http://${host}/#narrow/near/${options.messageId}`);
        const messageSelector = `#zfilt${options.messageId}`;
        await page.waitForSelector(messageSelector);
        // remove unread marker and don't select message
        const marker = `#zfilt${options.messageId} .unread_marker`;
        await page.evaluate((sel) => $(sel).remove(), marker);  // eslint-disable-line no-undef
        await page.evaluate(() => navigate.up());  // eslint-disable-line no-undef
        const messageBox = await page.$(messageSelector);
        const messageGroup = (await messageBox.$x('..'))[0];
        // Compute screenshot area, with some padding around the message group
        const clip = Object.assign({}, await messageGroup.boundingBox());
        clip.y -= 5;
        clip.x -= 5;
        clip.width += 10;
        clip.height += 10;
        const imagePath = path.join(__dirname, '..', 'static', 'images', 'integrations', options.integration, '001.png');
        await page.screenshot({ path: imagePath, clip: clip });
        console.log(`Screenshot captured to: \x1B[1;31m${imagePath}\n`);
    } catch (e) {
        console.log(e);
        process.exit(1);
    } finally {
        await browser.close();
    }
}

run();<commit_msg>message-screenshot: Create image directory if it doesn't exist.<commit_after>const puppeteer = require("puppeteer");
const commander = require("commander");
const path = require("path");
const mkdirp = require("mkdirp");
const host = "localhost:9991";
const options = {};

commander
    .arguments('<integration> <message_id>')
    .action((integration, messageId) => {
        options.integration = integration;
        options.messageId = messageId;
        console.log(`Capturing screenshot for ${integration} using message ${messageId}`);
    })
    .parse(process.argv);

if (options.integration === undefined) {
    console.error('no integration specified!');
    process.exit(1);
}

// TODO: Refactor to share code with frontend_tests/puppeteer_tests/00-realm-creation.js
async function run() {
    const browser = await puppeteer.launch({
        args: [
            '--window-size=1400,1024',
            '--no-sandbox', '--disable-setuid-sandbox',
            // Helps render fonts correctly on Ubuntu: https://github.com/puppeteer/puppeteer/issues/661
            '--font-render-hinting=none',
        ],
        defaultViewport: null,
        headless: true,
    });
    try {
        const page = await browser.newPage();
        // deviceScaleFactor:2 gives better quality screenshots (higher pixel density)
        await page.setViewport({ width: 1280, height: 1024, deviceScaleFactor: 2 });
        await page.goto('http://' + host);
        // wait for devlogin admin button and click on it
        await page.waitForSelector('.btn-admin');
        await page.click('.btn-admin');

        // Navigate to message and capture screenshot
        await page.goto(`http://${host}/#narrow/near/${options.messageId}`);
        const messageSelector = `#zfilt${options.messageId}`;
        await page.waitForSelector(messageSelector);
        // remove unread marker and don't select message
        const marker = `#zfilt${options.messageId} .unread_marker`;
        await page.evaluate((sel) => $(sel).remove(), marker);  // eslint-disable-line no-undef
        await page.evaluate(() => navigate.up());  // eslint-disable-line no-undef
        const messageBox = await page.$(messageSelector);
        const messageGroup = (await messageBox.$x('..'))[0];
        // Compute screenshot area, with some padding around the message group
        const clip = Object.assign({}, await messageGroup.boundingBox());
        clip.y -= 5;
        clip.x -= 5;
        clip.width += 10;
        clip.height += 10;
        const imageDir = path.join(__dirname, '..', 'static', 'images', 'integrations', options.integration);
        mkdirp.sync(imageDir);
        const imagePath = path.join(imageDir, '001.png');
        await page.screenshot({ path: imagePath, clip: clip });
        console.log(`Screenshot captured to: \x1B[1;31m${imagePath}\n`);
    } catch (e) {
        console.log(e);
        process.exit(1);
    } finally {
        await browser.close();
    }
}

run();
{'fraction_non_alphanumeric': 0.14531977245302535, 'fraction_numerical': 0.014825030167212549, 'mean_word_length': 2.774886141834743, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4864059', 'n_tokens_mistral': 1737, 'n_tokens_neox': 1657, 'n_words': 407}