|
| 1 | +import { mkdtempSync, readFileSync, rmSync } from 'node:fs'; |
| 2 | +import { tmpdir } from 'node:os'; |
| 3 | +import { join } from 'node:path'; |
| 4 | +import puppeteer from 'puppeteer-core'; |
| 5 | +import { afterAll, beforeAll, describe, expect, it } from 'vitest'; |
| 6 | +import { findSystemChrome } from './chrome-discovery'; |
| 7 | +import { EXPAND_DECK_SCRIPT, exportPng } from './png'; |
| 8 | + |
| 9 | +let tempDir = ''; |
| 10 | + |
| 11 | +beforeAll(() => { |
| 12 | + tempDir = mkdtempSync(join(tmpdir(), 'codesign-png-chrome-test-')); |
| 13 | +}); |
| 14 | + |
| 15 | +afterAll(() => { |
| 16 | + rmSync(tempDir, { recursive: true, force: true }); |
| 17 | +}); |
| 18 | + |
| 19 | +async function layoutInChrome(html: string) { |
| 20 | + const browser = await puppeteer.launch({ |
| 21 | + executablePath: await findSystemChrome(), |
| 22 | + headless: true, |
| 23 | + }); |
| 24 | + try { |
| 25 | + const page = await browser.newPage(); |
| 26 | + await page.setViewport({ width: 1280, height: 800 }); |
| 27 | + await page.setContent(html, { waitUntil: 'load' }); |
| 28 | + const laidOut = await page.evaluate(EXPAND_DECK_SCRIPT); |
| 29 | + const shown = await page.$$eval('section', (sections) => |
| 30 | + sections |
| 31 | + .filter((section) => section.getBoundingClientRect().height > 0) |
| 32 | + .map((section) => ({ |
| 33 | + top: section.getBoundingClientRect().top + window.scrollY, |
| 34 | + className: section.className, |
| 35 | + })), |
| 36 | + ); |
| 37 | + return { |
| 38 | + laidOut, |
| 39 | + tops: shown.map((section) => section.top), |
| 40 | + classes: shown.map((section) => section.className), |
| 41 | + }; |
| 42 | + } finally { |
| 43 | + await browser.close(); |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +describe.runIf(process.env['CODESIGN_EXPORT_BROWSER_TESTS'] === '1')( |
| 48 | + 'PNG deck layout in system Chrome', |
| 49 | + () => { |
| 50 | + it('stacks every slide of the built-in 16:9 deck scaffold', async () => { |
| 51 | + const scaffold = readFileSync( |
| 52 | + new URL( |
| 53 | + '../../../apps/desktop/resources/templates/scaffolds/decks/slide-16-9.html', |
| 54 | + import.meta.url, |
| 55 | + ), |
| 56 | + 'utf8', |
| 57 | + ); |
| 58 | + |
| 59 | + const { laidOut, tops, classes } = await layoutInChrome(scaffold); |
| 60 | + |
| 61 | + expect(laidOut).toBe(2); |
| 62 | + expect(tops).toHaveLength(2); |
| 63 | + expect(tops[0]).toBeGreaterThanOrEqual(0); |
| 64 | + expect(tops[1]).toBeGreaterThan(tops[0] ?? 0); |
| 65 | + expect(classes).toEqual(['slide title-slide', 'slide content-slide']); |
| 66 | + }, 60_000); |
| 67 | + |
| 68 | + it('leaves pages whose sections are all visible unchanged', async () => { |
| 69 | + const page = `<main>${'<section style="height:160px">Task</section>'.repeat(3)}</main>`; |
| 70 | + |
| 71 | + const { laidOut, tops } = await layoutInChrome(page); |
| 72 | + |
| 73 | + expect(laidOut).toBe(0); |
| 74 | + expect(tops).toHaveLength(3); |
| 75 | + }, 60_000); |
| 76 | + |
| 77 | + async function stackedSlides(html: string) { |
| 78 | + const browser = await puppeteer.launch({ |
| 79 | + executablePath: await findSystemChrome(), |
| 80 | + headless: true, |
| 81 | + }); |
| 82 | + try { |
| 83 | + const page = await browser.newPage(); |
| 84 | + await page.setViewport({ width: 1280, height: 800 }); |
| 85 | + await page.setContent(html, { waitUntil: 'load' }); |
| 86 | + const laidOut = await page.evaluate(EXPAND_DECK_SCRIPT); |
| 87 | + const shown = await page.$$eval('section', (sections) => |
| 88 | + sections |
| 89 | + .filter((section) => getComputedStyle(section).display !== 'none') |
| 90 | + .map((section) => { |
| 91 | + const style = getComputedStyle(section); |
| 92 | + return [ |
| 93 | + section.id, |
| 94 | + section.className, |
| 95 | + section.dataset['title'] ?? '', |
| 96 | + style.opacity, |
| 97 | + style.visibility, |
| 98 | + style.color, |
| 99 | + ].join(' | '); |
| 100 | + }), |
| 101 | + ); |
| 102 | + return { laidOut, shown }; |
| 103 | + } finally { |
| 104 | + await browser.close(); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + it('applies the active-slide styles to every stacked slide', async () => { |
| 109 | + const slides = ['one', 'two', 'three'] |
| 110 | + .map( |
| 111 | + (id, i) => |
| 112 | + `<section id="${id}" class="slide${i === 0 ? ' active' : ' hidden'}"${i === 0 ? '' : ' aria-hidden="true"'}>${id}</section>`, |
| 113 | + ) |
| 114 | + .join(''); |
| 115 | + const html = `<!doctype html><html><head><style> |
| 116 | + body { margin: 0; } |
| 117 | + .deck { width: 1280px; height: 720px; position: relative; } |
| 118 | + .slide { position: absolute; inset: 0; display: none; opacity: 0; transition: opacity 0.3s; } |
| 119 | + .slide.active { display: grid; opacity: 1; } |
| 120 | + .slide.hidden, .slide[aria-hidden="true"] { visibility: hidden; } |
| 121 | + </style></head><body><div class="deck">${slides}</div></body></html>`; |
| 122 | + |
| 123 | + const { laidOut, shown } = await stackedSlides(html); |
| 124 | + |
| 125 | + expect(laidOut).toBe(3); |
| 126 | + expect(shown).toEqual([ |
| 127 | + 'one | slide active | | 1 | visible | rgb(0, 0, 0)', |
| 128 | + 'two | slide active | | 1 | visible | rgb(0, 0, 0)', |
| 129 | + 'three | slide active | | 1 | visible | rgb(0, 0, 0)', |
| 130 | + ]); |
| 131 | + }, 60_000); |
| 132 | + |
| 133 | + it('keeps the id, own classes and content attributes of each slide', async () => { |
| 134 | + const html = `<!doctype html><html><head><style> |
| 135 | + body { margin: 0; } |
| 136 | + .deck { width: 1280px; height: 720px; position: relative; } |
| 137 | + .slide { position: absolute; inset: 0; display: none; opacity: 0; } |
| 138 | + .slide[data-active="true"] { display: grid; opacity: 1; } |
| 139 | + .intro { color: rgb(200, 0, 0); } |
| 140 | + .market { color: rgb(0, 0, 200); } |
| 141 | + </style></head><body><div class="deck"> |
| 142 | + <section id="intro" class="slide intro" data-title="Intro" data-active="true">Intro</section> |
| 143 | + <section id="market" class="slide market" data-title="Market" data-active="false">Market</section> |
| 144 | + </div></body></html>`; |
| 145 | + |
| 146 | + const { laidOut, shown } = await stackedSlides(html); |
| 147 | + |
| 148 | + expect(laidOut).toBe(2); |
| 149 | + expect(shown).toEqual([ |
| 150 | + 'intro | slide intro | Intro | 1 | visible | rgb(200, 0, 0)', |
| 151 | + 'market | slide market | Market | 1 | visible | rgb(0, 0, 200)', |
| 152 | + ]); |
| 153 | + }, 60_000); |
| 154 | + |
| 155 | + it('captures the full height of a long stacked deck', async () => { |
| 156 | + const slides = Array.from( |
| 157 | + { length: 20 }, |
| 158 | + (_, i) => |
| 159 | + `<section class="slide"${i === 0 ? ' data-active="true"' : ''}>${i + 1}</section>`, |
| 160 | + ).join(''); |
| 161 | + const html = `<!doctype html><html><head><style> |
| 162 | + body { margin: 0; display: grid; place-items: center; height: 100%; } |
| 163 | + .deck { width: 1200px; aspect-ratio: 16 / 9; position: relative; } |
| 164 | + .slide { position: absolute; inset: 0; display: none; } |
| 165 | + .slide[data-active="true"] { display: block; } |
| 166 | + </style></head><body><main class="deck">${slides}</main></body></html>`; |
| 167 | + const dest = join(tempDir, 'long-deck.png'); |
| 168 | + |
| 169 | + await exportPng(html, dest, { sourcePath: 'deck.html' }); |
| 170 | + |
| 171 | + // 24px top padding, then 20 slides of 675px each followed by a 24px gap, at 2x. |
| 172 | + expect(readFileSync(dest).readUInt32BE(20)).toBe((24 + 20 * (675 + 24)) * 2); |
| 173 | + }, 120_000); |
| 174 | + }, |
| 175 | +); |
0 commit comments