← back to Marketing Command Center

scripts/quickpost-test-lib.js

75 lines

const fs = require('fs');
const path = require('path');

const PROJECT = path.resolve(__dirname, '..');
const URL = process.env.MCC_TEST_URL || 'http://127.0.0.1:9661/#quickpost';

function envFile() {
  const out = {};
  try {
    for (const line of fs.readFileSync(path.join(PROJECT, '.env'), 'utf8').split('\n')) {
      const m = line.match(/^([A-Z0-9_]+)=(.*)$/);
      if (m) out[m[1]] = m[2].replace(/^['"]|['"]$/g, '');
    }
  } catch {}
  return out;
}

function credentials() {
  const env = envFile();
  return { username: env.MCC_USER || 'admin', password: env.MCC_PASS || '' };
}

async function mockRiskyWrites(page) {
  await page.route('https://example.com/wallpaper.jpg', route => route.fulfill({
    status: 200,
    contentType: 'image/svg+xml',
    body: '<svg xmlns="http://www.w3.org/2000/svg" width="600" height="600"><defs><pattern id="p" width="80" height="80" patternUnits="userSpaceOnUse"><rect width="80" height="80" fill="#193b59"/><path d="M0 40h80M40 0v80" stroke="#84b6c9" stroke-width="10"/><circle cx="40" cy="40" r="16" fill="#e5c46a"/></pattern></defs><rect width="600" height="600" fill="url(#p)"/></svg>'
  }));
  await page.route('**/api/copy/generate', route => route.fulfill({
    status: 200,
    contentType: 'application/json',
    body: JSON.stringify({ variants: [
      'A calm blue grasscloth brings texture and quiet color to the room.',
      'Meet a blue grasscloth made for rooms that deserve a softer point of view.',
      'Natural texture, deep blue color, and a wall worth looking at twice.'
    ] })
  }));
  await page.route('**/api/channels/publish', async route => {
    let body = {};
    try { body = route.request().postDataJSON(); } catch {}
    const channels = Array.isArray(body.channels) ? body.channels : [];
    await route.fulfill({
      status: 200,
      contentType: 'application/json',
      body: JSON.stringify({ ok: true, mocked: true, results: channels.map(channel => ({
        channel, ok: true, live: body.dryRun ? false : true
      })) })
    });
  });
}

async function waitForQuickPost(page) {
  await page.goto(URL, { waitUntil: 'domcontentloaded', timeout: 60000 });
  await page.waitForSelector('.kidpost', { timeout: 30000 });
  await page.waitForFunction(() => !document.querySelector('#kp-assets .kp-wait'), null, { timeout: 30000 }).catch(() => {});
}

async function preparePost(page) {
  const firstAsset = page.locator('.kp-asset').first();
  if (await firstAsset.count()) await firstAsset.click();
  else await page.locator('#kp-media').fill('https://example.com/wallpaper.jpg');
  await page.locator('[data-go="2"]').click();
  const music = page.locator('.kp-choice').nth(1);
  if (await music.count()) await music.click();
  await page.locator('[data-go="3"]').click();
  await page.locator('#kp-topic').fill('a new blue grasscloth wallpaper');
  await page.locator('#kp-suggest').click();
  await page.locator('.kp-suggestion').first().waitFor({ timeout: 10000 });
  await page.locator('.kp-suggestion').first().click();
  await page.locator('[data-go="4"]').click();
  await page.locator('#kp-all').click();
}

module.exports = { PROJECT, URL, credentials, mockRiskyWrites, waitForQuickPost, preparePost };