← back to The Ai Factory

scripts/clickthrough.js

58 lines

// Live click-through of The AI Factory viewer.
// Opens :9891, screenshots the run table, clicks a row to expand events, screenshots that.

const { chromium } = require('playwright');
const path = require('node:path');
const fs = require('node:fs/promises');

const VIEWER = 'http://127.0.0.1:9891';
const ORCH   = 'http://127.0.0.1:9890';
const OUT    = path.join(__dirname, '..', 'output', 'clickthrough');

(async () => {
  await fs.mkdir(OUT, { recursive: true });
  const browser = await chromium.launch({ headless: true });
  const ctx = await browser.newContext({ viewport: { width: 1440, height: 900 }, deviceScaleFactor: 2 });
  const page = await ctx.newPage();
  const consoleMsgs = [];
  page.on('console', m => consoleMsgs.push(`[${m.type()}] ${m.text()}`));
  page.on('pageerror', e => consoleMsgs.push(`[PAGEERROR] ${e.message}`));

  console.log('STEP 1: table view');
  await page.goto(VIEWER, { waitUntil: 'networkidle', timeout: 15000 });
  await page.waitForTimeout(1500);
  await page.screenshot({ path: path.join(OUT, '01-table.png'), fullPage: true });

  const rowCount = await page.locator('tr.run-row').count();
  console.log(`  run rows: ${rowCount}`);
  const activateCount = await page.locator('.activate-btn').count();
  console.log(`  activate buttons: ${activateCount}`);

  // Click first row to expand events
  if (rowCount > 0) {
    console.log('STEP 2: click first row');
    await page.locator('tr.run-row').first().click();
    await page.waitForTimeout(1000);
    await page.screenshot({ path: path.join(OUT, '02-events-expanded.png'), fullPage: true });
    const eventCount = await page.locator('.event').count();
    console.log(`  events visible: ${eventCount}`);
  }

  await fs.writeFile(path.join(OUT, 'console.log'), consoleMsgs.join('\n'), 'utf8');
  const summary = {
    viewer_status: 'reachable',
    row_count: rowCount,
    activate_buttons: activateCount,
    console_messages: consoleMsgs.length,
    screenshots: ['01-table.png', '02-events-expanded.png']
  };
  await fs.writeFile(path.join(OUT, 'summary.json'), JSON.stringify(summary, null, 2));
  console.log('SUMMARY:', JSON.stringify(summary));

  const health = await (await fetch(`${ORCH}/health`)).json();
  const runs   = await (await fetch(`${ORCH}/runs`)).json();
  console.log(`ORCH /health: ${JSON.stringify(health)}  runs: ${runs.runs?.length || 0}`);

  await browser.close();
})().catch(e => { console.error(e); process.exit(1); });