Skip to content

Commit 41d05c8

Browse files
johnnyhuycodex
andauthored
test: verify history footer painting at native window sizes (#63)
Co-authored-by: OpenAI Codex <[email protected]>
1 parent a25471a commit 41d05c8

1 file changed

Lines changed: 81 additions & 19 deletions

File tree

‎tests/e2e/tool-preview.test.ts‎

Lines changed: 81 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,76 @@ let page: Page;
1313
let fixture: string;
1414
const errors: string[] = [];
1515

16+
async function resizeWindow(width: number, height: number) {
17+
const window = await app.browserWindow(page);
18+
await window.evaluate(
19+
(window, { width, height }) => window.setContentSize(width, height),
20+
{ width, height },
21+
);
22+
// macOS can clamp a requested window to the display's available work area.
23+
const [actualWidth, actualHeight] = await window.evaluate((window) => window.getContentSize());
24+
// Device-metrics emulation can make DOM bounds disagree with captured layers.
25+
// Resize the real window and verify the renderer follows its content bounds.
26+
await page.waitForFunction(
27+
({ width, height }) => innerWidth === width && innerHeight === height,
28+
{ width: actualWidth, height: actualHeight },
29+
);
30+
expect(actualWidth).toBeGreaterThanOrEqual(900);
31+
expect(actualHeight).toBeGreaterThanOrEqual(600);
32+
}
33+
34+
// DOM bounds alone cannot detect a stale compositor layer in a screenshot.
35+
// Inspect the captured pixels where the footer label is meant to be painted.
36+
async function footerPaint(screenshot: Buffer) {
37+
const label = await page.getByRole('button', { name: 'Fork from here', exact: true }).evaluate((button) => {
38+
const { x, y, width, height } = button.getBoundingClientRect();
39+
return {
40+
x, y, width, height, colour: getComputedStyle(button).color,
41+
hitTarget: button.contains(document.elementFromPoint(x + width / 2, y + height / 2)),
42+
};
43+
});
44+
return app.evaluate(({ nativeImage }, { png, label }) => {
45+
const image = nativeImage.createFromBuffer(Buffer.from(png, 'base64'));
46+
const size = image.getSize();
47+
const bitmap = image.toBitmap();
48+
const colour = label.colour.match(/[\d.]+/g)!.slice(0, 3).map(Number);
49+
let textPixels = 0;
50+
for (let y = Math.ceil(label.y + 5); y < Math.min(size.height, label.y + label.height - 5); y++) {
51+
for (let x = Math.ceil(label.x + 20); x < Math.min(size.width, label.x + label.width - 20); x++) {
52+
const offset = (y * size.width + x) * 4;
53+
// Electron's bitmap is BGRA. Allow antialiasing at glyph edges.
54+
if (colour.every((value, channel) => Math.abs(value - bitmap[offset + 2 - channel]) < 20)) textPixels++;
55+
}
56+
}
57+
return { size, label, textPixels };
58+
}, { png: screenshot.toString('base64'), label });
59+
}
60+
61+
async function captureFooter(name: string) {
62+
const footer = page.getByRole('button', { name: 'Fork from here', exact: true });
63+
// Wait for virtual-row measurement after expansion, collapse or resizing.
64+
await expect.poll(async () => {
65+
const card = await page.locator('.wb-tool').boundingBox();
66+
const button = await footer.boundingBox();
67+
return !!card && !!button && button.y >= card.y + card.height;
68+
}).toBe(true);
69+
await footer.scrollIntoViewIfNeeded();
70+
const screenshot = await page.screenshot({ scale: 'css' });
71+
const paint = await footerPaint(screenshot);
72+
const window = await app.browserWindow(page);
73+
const nativeSize = await window.evaluate((window) => window.getContentSize());
74+
const viewport = await page.evaluate(() => ({ width: innerWidth, height: innerHeight }));
75+
await Promise.all([
76+
fs.writeFile(path.join(root, '.tmp/proof', `${name}.png`), screenshot),
77+
fs.writeFile(path.join(shots, `${name}.png`), screenshot),
78+
fs.writeFile(path.join(shots, `${name}.json`), JSON.stringify({ ...paint, nativeSize, viewport }, null, 2)),
79+
]);
80+
expect([viewport.width, viewport.height]).toEqual(nativeSize);
81+
expect(paint.size).toEqual(viewport);
82+
expect(paint.label.hitTarget).toBe(true);
83+
expect(paint.textPixels, `Footer label is missing from ${name}.png`).toBeGreaterThan(8);
84+
}
85+
1686
describe('bounded tool previews in Electron', () => {
1787
beforeAll(async () => {
1888
fixture = await fs.mkdtemp(path.join(os.tmpdir(), 'pidex-tool-preview-'));
@@ -61,7 +131,7 @@ describe('bounded tool previews in Electron', () => {
61131
page.setDefaultTimeout(10_000);
62132
page.on('pageerror', (error) => errors.push(error.message));
63133
await page.getByRole('heading', { name: 'Start a session', exact: true }).waitFor();
64-
await page.setViewportSize({ width: 1280, height: 1100 });
134+
await resizeWindow(1280, 1100);
65135
await page.evaluate(() => document.fonts.ready);
66136
await page.getByRole('button', { name: /^Sessions/ }).click();
67137
});
@@ -71,7 +141,7 @@ describe('bounded tool previews in Electron', () => {
71141
if (fixture) await fs.rm(fixture, { recursive: true, force: true });
72142
});
73143

74-
it.each(['write', 'edit'])('bounds a long %s line, copies the full input and expands on request', async (tool) => {
144+
it.each(['write', 'edit'])('bounds a long %s line and keeps the footer painted through native window resizing', async (tool) => {
75145
await page.getByRole('tree', { name: 'Session archives', exact: true })
76146
.getByRole('treeitem', { name: new RegExp(`Inspect long ${tool} input`) }).click();
77147
const card = page.locator('.wb-tool');
@@ -94,26 +164,18 @@ describe('bounded tool previews in Electron', () => {
94164
await card.getByRole('button', { name: 'Show less' }).click();
95165
expect((await diff.textContent())?.length).toBe(2000);
96166
await card.locator('summary').scrollIntoViewIfNeeded();
97-
// Expansion changes the virtual row height. Wait for measurement before
98-
// capturing so the history footer has moved below the resized card.
99-
await page.evaluate(() => new Promise<void>((resolve) => {
100-
requestAnimationFrame(() => requestAnimationFrame(() => resolve()));
101-
}));
102-
await expect.poll(async () => {
103-
const outputBounds = await card.getByRole('button', { name: 'Output details' }).boundingBox();
104-
const footerBounds = await page.getByRole('button', { name: 'Fork from here', exact: true }).boundingBox();
105-
return !!outputBounds && !!footerBounds && footerBounds.y >= outputBounds.y + outputBounds.height;
106-
}).toBe(true);
107-
const screenshot = `${tool}-bounded-preview.png`;
108-
await page.screenshot({ path: path.join(root, '.tmp/proof', screenshot) });
109-
await page.screenshot({ path: path.join(shots, screenshot) });
167+
await captureFooter(`${tool}-bounded-preview`);
110168

111-
await page.setViewportSize({ width: 900, height: 600 });
169+
await resizeWindow(900, 600);
112170
await card.getByRole('button', { name: 'Show more' }).scrollIntoViewIfNeeded();
113171
expect(await page.evaluate(() => document.body.scrollWidth <= window.innerWidth)).toBe(true);
114-
await page.screenshot({ path: path.join(root, '.tmp/proof', `${tool}-bounded-narrow.png`) });
115-
await page.screenshot({ path: path.join(shots, `${tool}-bounded-narrow.png`) });
116-
await page.setViewportSize({ width: 1280, height: 1100 });
172+
await page.screenshot({ scale: 'css', path: path.join(root, '.tmp/proof', `${tool}-bounded-narrow.png`) });
173+
await page.screenshot({ scale: 'css', path: path.join(shots, `${tool}-bounded-narrow.png`) });
174+
await captureFooter(`${tool}-footer-narrow`);
175+
await resizeWindow(1280, 1100);
176+
await captureFooter(`${tool}-footer-restored`);
177+
await card.locator('summary').click();
178+
await captureFooter(`${tool}-footer-collapsed`);
117179
expect(errors).toEqual([]);
118180
});
119181
});

0 commit comments

Comments
 (0)