File size: 4,340 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
const assert = require('node:assert/strict');
const path = require('node:path');
const { pathToFileURL } = require('node:url');
const { chromium } = require('playwright');

async function findScrollHost(page) {
  return page.evaluateHandle(() => {
    const blocks = document.querySelectorAll('[data-testid="prompt-response-block"]');
    for (let parent = blocks[blocks.length - 1].parentElement; parent; parent = parent.parentElement) {
      if (parent.scrollHeight - parent.clientHeight <= 4) continue;
      const overflow = getComputedStyle(parent).overflowY;
      if (parent === document.scrollingElement || overflow !== 'visible') return parent;
    }
    return null;
  });
}

async function checkOverflowScrolling(page) {
  await page.setViewportSize({width: 1200, height: 420});
  await page.evaluate(() => window.__setMockGenerate(prompt => Promise.resolve('result: ' + prompt)));
  const placeholder = page.getByTestId('new-block-placeholder');
  for (let index = 0; index < 20; index++) {
    await page.getByTestId('prompt-input').nth(index).fill('scroll request ' + index);
    await page.getByTestId('send-button').nth(index).click();
    await placeholder.click();
    await page.getByTestId('prompt-response-block').nth(index + 1).waitFor({state: 'attached'});
    const candidate = await findScrollHost(page);
    try {
      const host = candidate.asElement();
      if (!host) continue;
      const start = await host.evaluate(element => {
        const root = element === document.scrollingElement;
        const rect = (root ? document.querySelector('[data-testid="content"]') : element).getBoundingClientRect();
        return {
          x: (Math.max(0, rect.left) + Math.min(innerWidth, rect.right)) / 2,
          y: (Math.max(0, rect.top) + Math.min(innerHeight, rect.bottom)) / 2,
          scrollTop: element.scrollTop,
        };
      });
      await page.mouse.move(start.x, start.y);
      await page.mouse.wheel(0, start.scrollTop > 2 ? -1000 : 1000);
      await page.waitForFunction(({element, initial}) => Math.abs(element.scrollTop - initial) > 1,
        {element: host, initial: start.scrollTop}, {timeout: 5000});
      return true;
    } finally {
      await candidate.dispose();
    }
  }
  return false;
}

(async () => {
  const browser = await chromium.launch({ headless: true, args: ['--no-sandbox'] });
  try {
    const page = await browser.newPage({ viewport: { width: 1200, height: 800 } });
    await page.route(/^https?:/, route => route.abort());
    await page.goto(pathToFileURL(path.resolve('index.html')).href);
    const dimensions = await page.evaluate(() => {
      const sidebar = document.querySelector('[data-testid="sidebar"]');
      const content = document.querySelector('[data-testid="content"]');
      const styles = getComputedStyle(content);
      const sidebarBox = sidebar.getBoundingClientRect();
      const contentBox = content.getBoundingClientRect();
      return {
        sidebar: sidebarBox.width,
        content: contentBox.width,
        gap: contentBox.left - sidebarBox.right,
        extraRight: document.documentElement.clientWidth - contentBox.right - sidebarBox.left,
        padding: [parseFloat(styles.paddingLeft), parseFloat(styles.paddingRight)],
      };
    });
    assert(Math.abs(dimensions.sidebar - 240) <= 5, 'desktop sidebar must be about 240px');
    assert(dimensions.content > 0 && dimensions.content <= 905, 'content must render within max 900px');
    assert(dimensions.padding.every(value => value >= 15), 'content needs horizontal padding');
    // When a max-width panel is centered in the remaining space, the extra
    // margin on its left is balanced by the equivalent right margin.
    const nearGutter = gap => Math.abs(gap - 32) <= 2;
    assert(nearGutter(dimensions.gap) ||
      dimensions.extraRight > 0 && nearGutter(dimensions.gap - dimensions.extraRight),
      'desktop layout needs a 32px sidebar gutter outside any centered surplus margin');

    const overflowTested = await checkOverflowScrolling(page);
    console.log('desktop layout and sidebar gutter passed');
    console.log(overflowTested ? 'wheel scrolled the overflowing workspace' : 'generated blocks fit without vertical overflow');
  } finally {
    await browser.close();
  }
})().catch(error => { console.error(error); process.exit(1); });