File size: 8,394 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
const path = require('path');
const { pathToFileURL } = require('url');
const { chromium } = require('playwright');
const fs = require('node:fs');
fs.mkdirSync('/logs/verifier/visual-audit', {recursive: true});

const entryUrl = pathToFileURL(path.join(process.cwd(), 'index.html')).href;
const seed = Number.parseInt(process.env.ENHANCED_SEED || '30031', 10);

function assert(condition, message) {
  if (!condition) throw new Error(message);
}

async function runCase(browser, name, viewport, validate) {
  const context = await browser.newContext({ viewport });
  const networkAttempts = [];
  await context.route('**/*', async (route) => {
    const url = route.request().url();
    if (/^https?:/i.test(url)) {
      networkAttempts.push(url);
      await route.abort('blockedbyclient');
      return;
    }
    await route.continue();
  });
  await context.addInitScript(({ fixedSeed }) => {
    let state = fixedSeed >>> 0;
    Math.random = () => {
      state = (state * 1664525 + 1013904223) >>> 0;
      return state / 0x100000000;
    };
  }, { fixedSeed: seed });
  const page = await context.newPage();
  page.setDefaultTimeout(5000);
  const errors = [];
  page.on('pageerror', (error) => errors.push(error.message));
  try {
    await page.goto(entryUrl, { waitUntil: 'domcontentloaded' });
    await validate(page);
    await page.screenshot({path: `/logs/verifier/visual-audit/${name}.png`, fullPage: true});
    assert(networkAttempts.length === 0, `${name}: external network request attempted`);
    assert(errors.length === 0, `${name}: browser errors: ${errors.join(' | ')}`);
    console.log(`${name} passed`);
  } finally {
    await context.close();
  }
}

async function waitForCollapsedSidebar(page, selector, initialVisibleWidth) {
  await page.evaluate(({ selector, initialVisibleWidth }) => new Promise((resolve, reject) => {
    const deadline = performance.now() + 4500;
    let lastWidth = initialVisibleWidth;
    let stableFrames = 0;
    let reduced = false;
    const sample = () => {
      const element = document.querySelector(selector);
      if (!element) {
        reject(new Error('fixture sidebar must exist'));
        return;
      }
      const rect = element.getBoundingClientRect();
      const style = getComputedStyle(element);
      const visibleWidth = Math.max(0, Math.min(innerWidth, rect.right) - Math.max(0, rect.left));
      reduced = visibleWidth < 40 || style.display === 'none' || style.visibility === 'hidden';
      stableFrames = reduced && Math.abs(visibleWidth - lastWidth) < 0.01 ? stableFrames + 1 : 0;
      lastWidth = visibleWidth;
      if (reduced && stableFrames >= 4) resolve();
      else if (performance.now() >= deadline) reject(new Error('sidebar did not reach a stable collapsed state'));
      else requestAnimationFrame(sample);
    };
    sample();
  }), { selector, initialVisibleWidth });
}

(async () => {
  assert(Number.isInteger(seed), 'ENHANCED_SEED must be a fixed integer');
  const browser = await chromium.launch({
    headless: true,
    args: ['--no-sandbox', '--disable-background-networking', '--disable-component-update', '--no-first-run'],
  });
  try {
    // ENHANCED_CASE_1: the initial small-window placeholder keeps the requested inset.
    await runCase(browser, 'ENHANCED_CASE_1', { width: 500, height: 700 }, async (page) => {
      const inset = await page.evaluate(() => {
        const shell = document.querySelector('[data-testid="content-shell"]')?.getBoundingClientRect();
        const placeholder = document.querySelector('[data-testid="placeholder"]')?.getBoundingClientRect();
        return shell && placeholder ? placeholder.left - shell.left : null;
      });
      assert(inset !== null, 'fixture content shell and placeholder must exist');
      assert(inset >= 63.5, `small-window placeholder must keep at least 64px from the content edge; got ${inset}`);
    });

    // ENHANCED_CASE_2: expanded sidebar spacing remains at least 64px and content is centered.
    await runCase(browser, 'ENHANCED_CASE_2', { width: 1600, height: 800 }, async (page) => {
      const layout = await page.evaluate(() => {
        const sidebar = document.querySelector('[data-testid="sidebar"]')?.getBoundingClientRect();
        const shellElement = document.querySelector('[data-testid="content-shell"]');
        const shell = shellElement?.getBoundingClientRect();
        const content = document.querySelector('[data-testid="main-content"]')?.getBoundingClientRect();
        const paddingLeft = shellElement ? parseFloat(getComputedStyle(shellElement).paddingLeft) || 0 : 0;
        return sidebar && shell && content ? {
          gap: content.left - sidebar.right,
          contentCenter: content.left + content.width / 2,
          availableCenter: (sidebar.right + innerWidth) / 2,
        } : null;
      });
      assert(layout, 'fixture sidebar, shell, and content must exist');
      assert(layout.gap >= 63.5, `expanded sidebar/content gap must be at least 64px; got ${layout.gap}`);
      assert(Math.abs(layout.contentCenter - layout.availableCenter) <= 2, 'large-window content must be centered in the visible area beside the sidebar');
    });

    // ENHANCED_CASE_3: collapsed content still retains at least 64px on its left.
    await runCase(browser, 'ENHANCED_CASE_3', { width: 900, height: 700 }, async (page) => {
      const initialSidebar = await page.locator('[data-testid="sidebar"]').evaluate((element) => {
        const rect = element.getBoundingClientRect();
        return Math.max(0, Math.min(innerWidth, rect.right) - Math.max(0, rect.left));
      });
      assert(initialSidebar > 1, 'sidebar must initially be visible');
      await page.locator('[data-testid="toggle-sidebar"]').click();
      await waitForCollapsedSidebar(page, '[data-testid="sidebar"]', initialSidebar);
      const layout = await page.evaluate(() => {
        const sidebar = document.querySelector('[data-testid="sidebar"]')?.getBoundingClientRect();
        const content = document.querySelector('[data-testid="main-content"]')?.getBoundingClientRect();
        return sidebar && content ? { contentLeft: content.left } : null;
      });
      assert(layout, 'fixture sidebar and content must exist');
      assert(layout.contentLeft >= 63.5, `collapsed content must keep at least 64px from the left edge; got ${layout.contentLeft}`);

      await page.setViewportSize({width: 1600, height: 800});
      const wideSidebarWidth = await page.locator('[data-testid="sidebar"]').evaluate(async element => {
        const started = performance.now();
        let previous = Number.NaN;
        let stableFrames = 0;
        while (performance.now() - started < 4500) {
          await new Promise(requestAnimationFrame);
          const frame = element.getBoundingClientRect();
          const style = getComputedStyle(element);
          const hidden = style.display === 'none' || style.visibility === 'hidden';
          const visible = hidden ? 0 : Math.max(0, Math.min(innerWidth, frame.right) - Math.max(0, frame.left));
          stableFrames = Math.abs(visible - previous) < .05 ? stableFrames + 1 : 0;
          previous = visible;
          if (stableFrames >= 4 && performance.now() - started >= 150) return visible;
        }
        throw Error('sidebar did not settle after the viewport resized');
      });
      if (wideSidebarWidth >= 40) {
        await page.waitForFunction(() => {
          const sidebar = document.querySelector('[data-testid="sidebar"]').getBoundingClientRect();
          const content = document.querySelector('[data-testid="main-content"]').getBoundingClientRect();
          return content.left - sidebar.right >= 63.5 &&
            Math.abs(content.left + content.width / 2 - (sidebar.right + innerWidth) / 2) <= 2;
        }, null, {timeout: 4500});
        await page.locator('[data-testid="toggle-sidebar"]').click();
      }
      await waitForCollapsedSidebar(page, '[data-testid="sidebar"]', wideSidebarWidth);
      await page.waitForFunction(() => {
        const {left, width} = document.querySelector('[data-testid="main-content"]').getBoundingClientRect();
        return left >= 63.5 && Math.abs(left + width / 2 - innerWidth / 2) <= 2;
      }, null, {timeout: 4500});
    });
  } finally {
    await browser.close();
  }
})().catch((error) => {
  console.error(error && error.stack || error);
  process.exit(1);
});