File size: 8,085 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
169
const path = require('path');
const { pathToFileURL } = require('url');
const { chromium } = require('playwright');

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

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

async function runCase(browser, name, validate) {
  const context = await browser.newContext({ viewport: { width: 1200, height: 800 } });
  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);
    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 = reduced || visibleWidth < initialVisibleWidth - 1 || style.display === 'none' ||
        style.visibility === 'hidden' || element.getAttribute('aria-hidden') === 'true';
      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 sidebar and content shell have no intervening gap.
    await runCase(browser, 'ENHANCED_CASE_1', async (page) => {
      const gap = await page.evaluate(() => {
        const sidebar = document.querySelector('[data-testid="sidebar"]')?.getBoundingClientRect();
        const shell = document.querySelector('[data-testid="content-shell"]')?.getBoundingClientRect();
        return sidebar && shell ? shell.left - sidebar.right : null;
      });
      assert(gap !== null, 'fixture sidebar and content shell must exist');
      assert(Math.abs(gap) <= 1, `sidebar/content gap must be removed; got ${gap}`);
    });

    // ENHANCED_CASE_2: the former 40px content-left padding is fully removed.
    await runCase(browser, 'ENHANCED_CASE_2', async (page) => {
      const paddingLeft = await page.locator('[data-testid="main-content"]').evaluate((element) =>
        parseFloat(getComputedStyle(element).paddingLeft) || 0);
      assert(paddingLeft <= 1, `content left padding must be removed; got ${paddingLeft}px`);
      const visualInset = await page.evaluate(() => {
        const shell = document.querySelector('[data-testid="content-shell"]').getBoundingClientRect();
        const button = document.querySelector('[data-testid="collapse-sidebar"]').getBoundingClientRect();
        return button.left - shell.left;
      });
      assert(visualInset <= 8, `content children must not recreate the removed 40px inset; got ${visualInset}px`);
    });

    // ENHANCED_CASE_3: collapsing never shifts content right and prompt display preserves collapse.
    await runCase(browser, 'ENHANCED_CASE_3', async (page) => {
      const shell = page.locator('[data-testid="content-shell"]');
      const initialLeft = await shell.evaluate((element) => element.getBoundingClientRect().left);
      const initialSidebarWidth = 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(initialSidebarWidth > 1, 'sidebar must initially be visible');
      await page.evaluate(() => {
        window.__enhancedCollapseSamples = [];
        window.__enhancedSampling = true;
        const sample = () => {
          const element = document.querySelector('[data-testid="content-shell"]');
          if (element) window.__enhancedCollapseSamples.push(element.getBoundingClientRect().left);
          if (window.__enhancedSampling) requestAnimationFrame(sample);
        };
        sample();
      });
      await page.locator('[data-testid="collapse-sidebar"]').click();
      await waitForCollapsedSidebar(page, '[data-testid="sidebar"]', initialSidebarWidth);
      const samples = await page.evaluate(() => {
        window.__enhancedSampling = false;
        return window.__enhancedCollapseSamples.slice();
      });
      assert(samples.length > 0, 'collapse trajectory must produce at least one layout sample');
      assert(samples.every((left) => left <= initialLeft + 1), 'content must not shift right at any sampled point during sidebar collapse');

      const collapsedSidebarWidth = 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));
      });
      await page.locator('[data-testid="show-prompt"]').click();
      await page.waitForFunction(() => {
        const prompt = document.querySelector('[data-testid="prompt-input"]');
        if (!prompt) return false;
        const rect = prompt.getBoundingClientRect();
        const style = getComputedStyle(prompt);
        return !prompt.hidden && rect.width > 0 && rect.height > 0 &&
          style.display !== 'none' && style.visibility !== 'hidden';
      });
      const state = await page.evaluate(() => {
        const sidebar = document.querySelector('[data-testid="sidebar"]')?.getBoundingClientRect();
        const prompt = document.querySelector('[data-testid="prompt-input"]');
        if (!sidebar || !prompt) return null;
        const promptRect = prompt.getBoundingClientRect();
        const promptStyle = getComputedStyle(prompt);
        return {
          sidebarVisibleWidth: Math.max(0, Math.min(innerWidth, sidebar.right) - Math.max(0, sidebar.left)),
          promptVisible: !prompt.hidden && promptRect.width > 0 && promptRect.height > 0 &&
            promptStyle.display !== 'none' && promptStyle.visibility !== 'hidden',
        };
      });
      assert(state, 'fixture sidebar and prompt input must exist');
      assert(state.promptVisible, 'prompt input must become visible');
      assert(state.sidebarVisibleWidth <= collapsedSidebarWidth + 1,
        'showing prompt must preserve collapsed-sidebar state');
    });
  } finally {
    await browser.close();
  }
})().catch((error) => {
  console.error(error && error.stack || error);
  process.exit(1);
});