File size: 5,281 Bytes
ef987bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
const fs = require('fs');
const path = require('path');
const { pathToFileURL } = require('url');
const { chromium } = require('playwright');
const mode = "frontend";

function walk(dir) {
  let out = [];
  for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
    if (entry.name === 'tests' || entry.name === 'logs' || entry.name === 'node_modules') continue;
    const p = path.join(dir, entry.name);
    if (entry.isDirectory()) out = out.concat(walk(p));
    else out.push(p);
  }
  return out;
}

const files = walk(process.cwd());
const html = files.find(f => f.toLowerCase().endsWith('.html'));
const svg = files.find(f => f.toLowerCase().endsWith('.svg'));
const target = mode === 'svg' ? svg : (html || svg);
// This is a Markdown -> PDF batch CLI task; the instruction never requires a web UI.
// The browser smoke check is only applicable when the solution ships an HTML/SVG
// entrypoint. If none exists in cwd, treat the browser smoke as not-applicable and
// pass (the instruction-required PDF structure checks live in validate_ui_behavior.js).
if (!target) {
  console.log('browser smoke skipped for task 9: no browser-openable entrypoint (CLI-only solution)');
  process.exit(0);
}

(async () => {
  const browser = await chromium.launch({ headless: true });
  const page = await browser.newPage({ viewport: { width: 960, height: 720 } });
  const errors = [];
  page.on('pageerror', err => errors.push(err.message));
  page.on('console', msg => {
    if (msg.type() === 'error') errors.push(msg.text());
  });
  await page.goto(pathToFileURL(target).href, { waitUntil: 'domcontentloaded' });
  await page.waitForTimeout(600);
  if (errors.length) throw new Error('browser errors: ' + errors.join(' | '));

  const stats = await page.evaluate((mode) => {
    const visible = Array.from(document.querySelectorAll('body *')).filter(el => {
      const rect = el.getBoundingClientRect();
      const style = window.getComputedStyle(el);
      return rect.width > 0 && rect.height > 0 && style.visibility !== 'hidden' && style.display !== 'none';
    });
    const buttons = document.querySelectorAll('button, [role="button"], input, select, textarea').length;
    const canvasInfo = Array.from(document.querySelectorAll('canvas')).map(c => {
      let nonBlank = false;
      try {
        const ctx = c.getContext('2d');
        if (ctx && c.width && c.height) {
          const data = ctx.getImageData(0, 0, Math.min(c.width, 32), Math.min(c.height, 32)).data;
          nonBlank = Array.from(data).some((v, i) => i % 4 !== 3 && v !== 0);
        }
      } catch (e) {}
      return { width: c.width, height: c.height, nonBlank };
    });
    const svgElements = document.querySelectorAll('svg *').length;
    return {
      bodyTextLength: (document.body && document.body.innerText || '').trim().length,
      visibleCount: visible.length,
      buttons,
      canvasInfo,
      svgElements,
      localStorageLength: window.localStorage ? window.localStorage.length : 0,
      mode,
    };
  }, mode);

  if (stats.visibleCount < (mode === 'svg' ? 3 : 5)) {
    throw new Error('too few visible browser elements: ' + stats.visibleCount);
  }
  if (mode === 'svg' && stats.svgElements < 2) {
    throw new Error('too few rendered svg elements: ' + stats.svgElements);
  }
  if (stats.canvasInfo.length) {
    const validCanvas = stats.canvasInfo.some(c => c.width >= 100 && c.height >= 100);
    if (!validCanvas) throw new Error('canvas exists but has invalid dimensions');
  }

  const before = await page.evaluate(() => {
    const canvases = Array.from(document.querySelectorAll('canvas')).map(c => {
      try { return c.toDataURL(); } catch (e) { return ''; }
    });
    return JSON.stringify({
      text: document.body.innerText,
      storage: window.localStorage ? window.localStorage.length : 0,
      canvas: canvases,
    });
  });
  const clickable = await page.$('button, [role="button"], input[type="button"], input[type="submit"], canvas, svg, body');
  if (clickable) await clickable.click({ position: { x: 20, y: 20 } }).catch(() => {});
  await page.keyboard.press('Space').catch(() => {});
  await page.keyboard.press('ArrowDown').catch(() => {});
  await page.keyboard.press('Enter').catch(() => {});
  await page.waitForTimeout(300);
  const after = await page.evaluate(() => {
    const canvases = Array.from(document.querySelectorAll('canvas')).map(c => {
      try { return c.toDataURL(); } catch (e) { return ''; }
    });
    return JSON.stringify({
      text: document.body.innerText,
      storage: window.localStorage ? window.localStorage.length : 0,
      canvas: canvases,
    });
  });
  if (mode === 'frontend' && stats.buttons === 0 && stats.canvasInfo.length === 0 && stats.bodyTextLength < 20) {
    throw new Error('frontend has no meaningful controls, canvas, or text content');
  }
  if (errors.length) throw new Error('browser errors after interaction: ' + errors.join(' | '));
  console.log('browser smoke passed for task 9; interaction_changed=' + (before !== after));
  await browser.close();
})().catch(async err => {
  console.error(err && err.stack || err);
  process.exit(1);
});