diff --git a/tools/export.sh b/tools/export.sh index a9f3d99..f1cca8c 100755 --- a/tools/export.sh +++ b/tools/export.sh @@ -76,6 +76,23 @@ for preset in "${presets[@]}"; do [ "$diagnostic_status" -eq 0 ] || fail "export of $preset reported errors or warnings ($log)" [ -e "$out" ] || fail "export of $preset produced no $out" if [[ "$preset" == Web* ]]; then + # Favicon caches can retain an old image even after the page revalidates. + # Version the exported links by content before hashing the release manifest. + python3 - "$out" <<'PY' +import hashlib +from pathlib import Path +import sys + +page = Path(sys.argv[1]) +html = page.read_text() +for name in ("index.icon.png", "index.apple-touch-icon.png"): + version = hashlib.sha256((page.parent / name).read_bytes()).hexdigest()[:16] + original = f'href="{name}"' + if html.count(original) != 1: + raise ValueError(f"Expected one exported icon link: {name}") + html = html.replace(original, f'href="{name}?v={version}"') +page.write_text(html) +PY python3 "$ROOT/tools/web_manifest.py" "$(dirname "$out")" --preset "$preset" fi done diff --git a/tools/web_tests/launcher.spec.cjs b/tools/web_tests/launcher.spec.cjs index 0a9f8c4..c9c4898 100644 --- a/tools/web_tests/launcher.spec.cjs +++ b/tools/web_tests/launcher.spec.cjs @@ -1,3 +1,5 @@ +const { createHash } = require('node:crypto'); +const { play } = require('./launch.cjs'); const { test, expect } = require('@playwright/test'); test('launcher explains graphics defaults and keeps launch options secondary', async ({ page }, info) => { @@ -17,3 +19,40 @@ test('launcher explains graphics defaults and keeps launch options secondary', a expect(button.x + button.width).toBeLessThanOrEqual(360); await page.screenshot({ path: info.outputPath('browser-launcher-narrow.png') }); }); + +// Browsers keep a separate favicon cache: changing the image at the old URL is +// insufficient for returning players. The export must give new artwork a new URL. +test('the tab icon is versioned and stays Farlanding artwork after launch', async ({ page }) => { + await page.goto('http://127.0.0.1:8070/'); + const icon = page.locator('link[rel="icon"]'); + await expect(icon).toHaveCount(1); + const href = await icon.getAttribute('href'); + expect(href).toMatch(/^index\.icon\.png\?v=[a-f0-9]{16}$/); + const response = await page.request.get(new URL(href, page.url()).href); + expect(response.ok()).toBe(true); + expect(new URL(href, page.url()).searchParams.get('v')).toBe( + createHash('sha256').update(await response.body()).digest('hex').slice(0, 16)); + const artworkURL = await page.locator('.identity img').evaluate(image => image.src); + async function iconPixels() { + return page.evaluate(async artwork => { + const urls = [document.querySelector('link[rel="icon"]').href, artwork]; + const pixels = await Promise.all(urls.map(async url => { + const bitmap = await createImageBitmap(await (await fetch(url)).blob()); + const canvas = document.createElement('canvas'); + canvas.width = canvas.height = 32; + const context = canvas.getContext('2d'); + context.drawImage(bitmap, 0, 0, 32, 32); + const result = Array.from(context.getImageData(0, 0, 32, 32).data); + bitmap.close(); + return result; + })); + return pixels[0].every((value, index) => value === pixels[1][index]); + }, artworkURL); + } + await expect.poll(iconPixels).toBe(true); + await play(page); + await expect.poll(() => page.evaluate(() => window.farlanding?.ready), { timeout: 90000 }).toBe(true); + await expect(icon).toHaveCount(1); + await expect.poll(iconPixels).toBe(true); + expect(await page.evaluate(() => window.farlanding.errors)).toEqual([]); +});