/** Local-only regression checks for the standalone download website. */ import assert from 'node:assert/strict'; import { createServer } from 'node:http'; import { mkdir, readFile, writeFile } from 'node:fs/promises'; import { dirname, resolve } from 'node:path'; import { fileURLToPath, pathToFileURL } from 'node:url'; import { chromium } from 'playwright'; const root = resolve(dirname(fileURLToPath(import.meta.url)), '..'); const site = resolve(root, 'prototypes/fmode-download-20261120'); const output = resolve(root, 'test-results/download-site'); const html = await readFile(resolve(site, 'index.html'), 'utf8'); const source = await readFile(resolve(site, 'site.js'), 'utf8'); const configPattern = /const FMODE_SITE = (\{[^\n]+\});/; const config = Function('return (' + source.match(configPattern)[1] + ')')(); const errors = []; const checks = []; await mkdir(output, { recursive: true }); const variants = new Map(); function variant(name, apkUrl, extra = {}) { variants.set('/' + name + '/', html.replace(configPattern, () => 'const FMODE_SITE = ' + JSON.stringify({ ...config, apkUrl, ...extra }) + ';')); } variant('missing', ''); variant('unsafe', 'javascript:alert(1)'); variant('insecure', 'http://example.com/fmode.apk'); variant('protocol-relative', '//example.com/fmode.apk'); variant('relative', '/download/future.apk'); variant('new-release', 'https://example.com/fmode-v2.apk?signature=a%2Fb&v=2', { version: '2.0', fileSize: '12 MB', channel: '正式版' }); variants.set('/qr-error/', html.replace('const qr = qrcodegen.QrCode.encodeText', 'const qr = missingQrEncoder.encodeText')); const server = createServer((request, response) => { const pathname = new URL(request.url, 'http://localhost').pathname; if (pathname.endsWith('.apk')) { response.writeHead(200, { 'Content-Type': 'application/vnd.android.package-archive', 'Content-Disposition': 'attachment; filename="Fmode.apk"' }); response.end('download smoke-test fixture'); return; } const content = pathname === '/download/' ? html : variants.get(pathname); response.writeHead(content ? 200 : 404, { 'Content-Type': 'text/html; charset=utf-8' }); response.end(content || 'Not found'); }); await new Promise((done) => server.listen(0, '127.0.0.1', done)); const base = 'http://127.0.0.1:' + server.address().port; const browser = await chromium.launch({ headless: true, executablePath: chromium.executablePath() }); const context = await browser.newContext({ reducedMotion: 'reduce', permissions: ['clipboard-read', 'clipboard-write'] }); await context.route('https://ai.fmode.cn/download/*.apk', (route) => route.fulfill({ status: 200, headers: { 'Content-Type': 'application/vnd.android.package-archive', 'Content-Disposition': 'attachment; filename="Fmode.apk"' }, body: 'download smoke-test fixture', })); const page = await context.newPage(); page.on('pageerror', (error) => errors.push(error.message)); page.on('request', (request) => { if (!request.url().startsWith(base) && !request.url().startsWith('data:') && request.url() !== config.apkUrl) errors.push('Unexpected external request: ' + request.url()); }); async function open(path = '/download/') { await page.goto(base + path); await page.evaluate(() => document.fonts.ready); } async function noOverflow(label) { const sizes = await page.evaluate(() => ({ scroll: document.documentElement.scrollWidth, viewport: window.innerWidth })); assert(sizes.scroll <= sizes.viewport, label + ': horizontal overflow ' + JSON.stringify(sizes)); } async function screenshot(name, locator, fullPage = false) { const options = { path: resolve(output, name + '.png'), animations: 'disabled' }; if (locator) await locator.screenshot({ ...options, style: '.site-header, .mobile-download-bar { visibility: hidden !important; }' }); else await page.screenshot({ ...options, fullPage }); } async function downloadFrom(selector) { const event = page.waitForEvent('download'); await page.locator(selector).click(); const download = await event; assert.equal(download.url(), config.apkUrl); await download.cancel(); } try { for (const width of [320, 360, 375, 390, 430, 650, 760, 768, 1024, 1440, 1920]) { await page.setViewportSize({ width, height: width <= 760 ? 844 : 960 }); await open(); await noOverflow('viewport ' + width); for (const scene of ['workbench', 'deliverables', 'conversation']) { await page.locator('#tab-' + scene).click(); assert.equal(await page.locator('#scene-' + scene).isVisible(), true); await noOverflow(width + ' / ' + scene); } const sizes = await page.locator('.hero-actions .button').evaluateAll((buttons) => buttons.map((button) => ({ height: button.getBoundingClientRect().height, font: parseFloat(getComputedStyle(button).fontSize) }))); assert(sizes.every((size) => size.height >= 44 && size.font >= 12), 'Small hero buttons at ' + width); const isMobile = width <= 760; assert.equal(await page.locator('.mobile-download-bar').isVisible(), isMobile); if (isMobile) { await page.locator('#menuToggle').click(); assert.equal(await page.locator('#menuToggle').getAttribute('aria-expanded'), 'true'); await page.locator('#navLinks a[href="#download"]').click(); assert.equal(await page.locator('#menuToggle').getAttribute('aria-expanded'), 'false'); } await page.evaluate(() => window.scrollTo(0, 0)); if ([320, 390, 1440].includes(width)) { await screenshot('hero-' + width); await screenshot('full-' + width, null, true); await screenshot('download-' + width, page.locator('.download-card')); } await page.locator('.hero-actions [data-show-qr]').click(); assert.equal(await page.locator('#downloadDialog').evaluate((dialog) => dialog.open), true); assert.equal(await page.locator('#dialogQr').isVisible(), true); await noOverflow('QR dialog ' + width); if ([390, 1440].includes(width)) await screenshot('qr-dialog-' + width); await page.keyboard.press('Escape'); assert.equal(await page.locator('#downloadDialog').evaluate((dialog) => dialog.open), false); assert.equal(await page.evaluate(() => document.activeElement.matches('.hero-actions [data-show-qr]')), true); checks.push('Responsive layout, tabs, menu, QR dialog and focus at ' + width + 'px'); } await page.setViewportSize({ width: 1440, height: 960 }); await open(); const targets = await page.locator('[data-download]').evaluateAll((links) => links.map((link) => link.href)); assert(targets.length >= 4 && targets.every((url) => url === config.apkUrl)); await downloadFrom('.hero-actions [data-download]'); await downloadFrom('.nav-actions [data-download]'); await downloadFrom('#apkButton'); await page.locator('.hero-actions [data-show-qr]').click(); await downloadFrom('[data-dialog-download]'); await page.locator('.dialog-actions [data-copy-link]').click(); assert.match(await page.locator('#dialogCopyStatus').textContent(), /已复制/); await page.locator('#dialogClose').click(); await page.locator('.download-qr [data-copy-link]').click(); assert.equal(await page.evaluate(() => navigator.clipboard.readText()), config.apkUrl); const qrImage = await page.locator('.download-qr [data-qr-image]').getAttribute('src'); assert(qrImage.startsWith('data:image/png;base64,')); await writeFile(resolve(output, 'download-qr.png'), Buffer.from(qrImage.split(',')[1], 'base64')); checks.push('Header / hero / section / dialog links share URL; real download events; clipboard; QR exported'); await page.setViewportSize({ width: 390, height: 844 }); await open(); await downloadFrom('.mobile-download-bar [data-download]'); await page.locator('.mobile-download-bar [data-show-qr]').click(); await page.keyboard.press('Tab'); assert.equal(await page.evaluate(() => document.getElementById('downloadDialog').contains(document.activeElement)), true); await page.keyboard.press('Escape'); await page.locator('#menuToggle').click(); await page.keyboard.press('Escape'); assert.equal(await page.evaluate(() => document.activeElement.id), 'menuToggle'); await page.locator('#faq details').first().locator('summary').click(); assert.equal(await page.locator('#faq details').first().getAttribute('open'), ''); checks.push('Mobile sticky download, focus containment, menu Esc, FAQ'); for (const [width, height] of [[320, 568], [390, 600], [760, 420]]) { await page.setViewportSize({ width, height }); await open(); await page.locator('.hero-actions [data-show-qr]').click(); const bounds = await page.locator('#downloadDialog').boundingBox(); assert(bounds.y >= 0 && bounds.y + bounds.height <= height, 'Dialog fits short viewport'); await page.locator('#dialogConfirm').click(); assert.equal(await page.locator('#downloadDialog').evaluate((dialog) => dialog.open), false); } checks.push('QR dialogs scroll and close in 320×568, 390×600 and 760×420 viewports'); for (const name of ['missing', 'unsafe', 'insecure', 'protocol-relative']) { await open('/' + name + '/'); assert.equal(await page.locator('.download-qr [data-qr-image]').isVisible(), false); assert.equal(await page.locator('.download-qr [data-copy-link]').isDisabled(), true); await page.locator('.hero-actions [data-download]').click(); assert.match(await page.locator('#dialogTitle').textContent(), /准备/); assert.equal(await page.locator('[data-dialog-download]').isVisible(), false); await page.keyboard.press('Escape'); } await open('/relative/'); assert.equal(await page.locator('#apkButton').getAttribute('href'), base + '/download/future.apk'); await open('/new-release/'); assert.match(await page.locator('#releaseMeta').textContent(), /2\.0.*12 MB/); assert.equal(await page.locator('#apkButton').getAttribute('href'), 'https://example.com/fmode-v2.apk?signature=a%2Fb&v=2'); const newQrImage = await page.locator('.download-qr [data-qr-image]').getAttribute('src'); assert.notEqual(qrImage, newQrImage); await writeFile(resolve(output, 'download-qr-new-release.png'), Buffer.from(newQrImage.split(',')[1], 'base64')); await open('/qr-error/'); assert.match(await page.locator('[data-qr-placeholder]').textContent(), /不可用/); assert.equal(await page.locator('#apkButton').getAttribute('href'), config.apkUrl); await page.locator('.hero-actions [data-show-qr]').click(); assert.equal(await page.locator('[data-dialog-download]').isVisible(), true); checks.push('Missing / unsafe URLs; relative URL; release changes update QR; QR failure keeps direct download'); for (const [name, userAgent, expected] of [ ['ios', 'Mozilla/5.0 (iPhone; CPU iPhone OS 18_0 like Mac OS X) AppleWebKit/605.1.15 Version/18.0 Mobile/15E148 Safari/604.1', /Android 版/], ['wechat', 'Mozilla/5.0 (Linux; Android 14) AppleWebKit/537.36 Chrome/130.0.0.0 Mobile Safari/537.36 MicroMessenger/8.0', /浏览器/], ]) { const mobileContext = await browser.newContext({ viewport: { width: 390, height: 844 }, userAgent, isMobile: true, hasTouch: true, reducedMotion: 'reduce' }); const mobile = await mobileContext.newPage(); mobile.on('pageerror', (error) => errors.push(error.message)); await mobile.goto(base + '/download/'); await mobile.locator('.mobile-download-bar [data-download]').click(); assert.match(await mobile.locator('#dialogTitle').textContent(), expected); assert.equal(await mobile.locator('[data-dialog-download]').isVisible(), false); await mobile.screenshot({ path: resolve(output, 'device-' + name + '.png'), animations: 'disabled' }); await mobileContext.close(); } checks.push('iOS compatibility message; WeChat browser instructions'); await page.close(); const fallbackContext = await browser.newContext({ reducedMotion: 'reduce' }); const fallback = await fallbackContext.newPage(); await fallback.addInitScript(() => { Object.defineProperty(navigator, 'clipboard', { value: undefined }); }); await fallback.goto(base + '/download/'); await fallback.locator('.download-qr [data-copy-link]').click(); assert.equal(await fallback.locator('#manualCopy').isVisible(), true); assert.equal(await fallback.locator('#downloadLink').inputValue(), config.apkUrl); await fallbackContext.close(); checks.push('Clipboard unavailable: selected, manually copyable URL'); const offline = await context.newPage(); await context.setOffline(true); await offline.goto(pathToFileURL(resolve(site, 'index.html')).href); assert.equal(await offline.locator('.download-qr [data-qr-image]').isVisible(), true); await context.setOffline(false); await offline.close(); checks.push('Standalone file renders QR fully offline'); assert.deepEqual(errors, [], 'Browser errors or unexpected external resources'); await writeFile(resolve(output, 'results.json'), JSON.stringify({ success: true, checks, errors }, null, 2)); console.log(JSON.stringify({ success: true, checks, screenshots: output }, null, 2)); } finally { await browser.close(); await new Promise((done) => server.close(done)); }