|
| 1 | +/** |
| 2 | + * The Console manifest's icons, by branding favicon (objectstack#29332): each |
| 3 | + * icon declares its real type and size, and a branding without a large enough |
| 4 | + * square raster gains the Console's 192 px and 512 px PNGs, so the Console |
| 5 | + * stays installable. |
| 6 | + */ |
| 7 | +import { describe, it, expect, vi } from 'vitest'; |
| 8 | +import { generatePWAManifest } from '@object-ui/mobile'; |
| 9 | +import { |
| 10 | + iconTypeOf, |
| 11 | + measureIconInBrowser, |
| 12 | + resolveManifestIcons, |
| 13 | + type IconSize, |
| 14 | + type MeasureIcon, |
| 15 | +} from './pwaManifestIcons'; |
| 16 | + |
| 17 | +const BASE = 'https://acme.example/_console/'; |
| 18 | + |
| 19 | +const DEFAULT_PNGS = [ |
| 20 | + { src: `${BASE}icon-192.png`, sizes: '192x192', type: 'image/png', purpose: 'any' }, |
| 21 | + { src: `${BASE}icon-512.png`, sizes: '512x512', type: 'image/png', purpose: 'any' }, |
| 22 | +]; |
| 23 | + |
| 24 | +/** A measurement stub that answers `size` for every source. */ |
| 25 | +function measuring(size: IconSize | null) { |
| 26 | + return vi.fn<MeasureIcon>(async () => size); |
| 27 | +} |
| 28 | + |
| 29 | +/** The icons of the manifest the Console generates for a favicon. */ |
| 30 | +async function manifestIcons(faviconUrl: string | undefined, measure: MeasureIcon) { |
| 31 | + const icons = await resolveManifestIcons(faviconUrl, BASE, measure); |
| 32 | + return generatePWAManifest({ enabled: true, name: 'Acme', shortName: 'Acme', icons }).icons; |
| 33 | +} |
| 34 | + |
| 35 | +describe('Console PWA manifest icons', () => { |
| 36 | + it('no branding: the default SVG, scalable, nothing measured', async () => { |
| 37 | + const measure = measuring(null); |
| 38 | + expect(await manifestIcons(undefined, measure)).toEqual([ |
| 39 | + { src: `${BASE}favicon.svg`, sizes: 'any', type: 'image/svg+xml', purpose: 'any' }, |
| 40 | + ]); |
| 41 | + expect(measure).not.toHaveBeenCalled(); |
| 42 | + }); |
| 43 | + |
| 44 | + it('SVG branding: the branded SVG, scalable, nothing measured', async () => { |
| 45 | + const measure = measuring(null); |
| 46 | + expect(await manifestIcons('/runtime/assets/brand.svg?v=3', measure)).toEqual([ |
| 47 | + { |
| 48 | + src: 'https://acme.example/runtime/assets/brand.svg?v=3', |
| 49 | + sizes: 'any', |
| 50 | + type: 'image/svg+xml', |
| 51 | + purpose: 'any', |
| 52 | + }, |
| 53 | + ]); |
| 54 | + expect(measure).not.toHaveBeenCalled(); |
| 55 | + }); |
| 56 | + |
| 57 | + it('small PNG branding: declared as a PNG at its measured size, plus the default PNGs', async () => { |
| 58 | + const measure = measuring({ width: 32, height: 32 }); |
| 59 | + expect(await manifestIcons('https://cdn.acme.example/favicon.png', measure)).toEqual([ |
| 60 | + { src: 'https://cdn.acme.example/favicon.png', sizes: '32x32', type: 'image/png', purpose: 'any' }, |
| 61 | + ...DEFAULT_PNGS, |
| 62 | + ]); |
| 63 | + expect(measure).toHaveBeenCalledWith('https://cdn.acme.example/favicon.png'); |
| 64 | + }); |
| 65 | + |
| 66 | + it('PNG branding of at least 192 px square: the branded PNG alone', async () => { |
| 67 | + expect(await manifestIcons('/brand/icon.PNG', measuring({ width: 256, height: 256 }))).toEqual([ |
| 68 | + { src: 'https://acme.example/brand/icon.PNG', sizes: '256x256', type: 'image/png', purpose: 'any' }, |
| 69 | + ]); |
| 70 | + }); |
| 71 | + |
| 72 | + it('a large but non-square PNG still gets the default PNGs', async () => { |
| 73 | + expect(await manifestIcons('/brand/wide.png', measuring({ width: 600, height: 200 }))).toEqual([ |
| 74 | + { src: 'https://acme.example/brand/wide.png', sizes: '600x200', type: 'image/png', purpose: 'any' }, |
| 75 | + ...DEFAULT_PNGS, |
| 76 | + ]); |
| 77 | + }); |
| 78 | + |
| 79 | + it('ICO branding: declared as an icon file at its measured size, plus the default PNGs', async () => { |
| 80 | + expect(await manifestIcons('/favicon.ico', measuring({ width: 48, height: 48 }))).toEqual([ |
| 81 | + { src: 'https://acme.example/favicon.ico', sizes: '48x48', type: 'image/x-icon', purpose: 'any' }, |
| 82 | + ...DEFAULT_PNGS, |
| 83 | + ]); |
| 84 | + }); |
| 85 | + |
| 86 | + it('a raster favicon that cannot be measured is left out, the default PNGs stand in', async () => { |
| 87 | + expect(await manifestIcons('/favicon.ico', measuring(null))).toEqual(DEFAULT_PNGS); |
| 88 | + }); |
| 89 | + |
| 90 | + it('a favicon of unknown type is left out unmeasured, the default PNGs stand in', async () => { |
| 91 | + const measure = measuring({ width: 512, height: 512 }); |
| 92 | + expect(await manifestIcons('/api/branding/favicon', measure)).toEqual(DEFAULT_PNGS); |
| 93 | + expect(measure).not.toHaveBeenCalled(); |
| 94 | + }); |
| 95 | +}); |
| 96 | + |
| 97 | +describe('iconTypeOf', () => { |
| 98 | + it.each([ |
| 99 | + ['https://a.example/x.svg', 'image/svg+xml'], |
| 100 | + ['https://a.example/x.svg#frag', 'image/svg+xml'], |
| 101 | + ['https://a.example/x.png?v=2', 'image/png'], |
| 102 | + ['https://a.example/x.ico', 'image/x-icon'], |
| 103 | + ['https://a.example/x.webp', 'image/webp'], |
| 104 | + ['https://a.example/x.jpeg', 'image/jpeg'], |
| 105 | + ['data:image/png;base64,AAAA', 'image/png'], |
| 106 | + ['data:image/svg+xml,%3Csvg%2F%3E', 'image/svg+xml'], |
| 107 | + ['https://a.example/x.svg.txt', undefined], |
| 108 | + ['https://a.example/favicon', undefined], |
| 109 | + ['data:text/plain,hi', undefined], |
| 110 | + ['not a url', undefined], |
| 111 | + ])('%s -> %s', (src, type) => { |
| 112 | + expect(iconTypeOf(src)).toBe(type); |
| 113 | + }); |
| 114 | +}); |
| 115 | + |
| 116 | +describe('measureIconInBrowser', () => { |
| 117 | + /** Replaces `Image` with one whose load the test drives. */ |
| 118 | + function stubImage(outcome: { width: number; height: number } | 'error' | 'hang') { |
| 119 | + class FakeImage { |
| 120 | + naturalWidth = 0; |
| 121 | + naturalHeight = 0; |
| 122 | + onload: (() => void) | null = null; |
| 123 | + onerror: (() => void) | null = null; |
| 124 | + set src(_value: string) { |
| 125 | + queueMicrotask(() => { |
| 126 | + if (outcome === 'hang') return; |
| 127 | + if (outcome === 'error') { |
| 128 | + this.onerror?.(); |
| 129 | + return; |
| 130 | + } |
| 131 | + this.naturalWidth = outcome.width; |
| 132 | + this.naturalHeight = outcome.height; |
| 133 | + this.onload?.(); |
| 134 | + }); |
| 135 | + } |
| 136 | + } |
| 137 | + vi.stubGlobal('Image', FakeImage); |
| 138 | + } |
| 139 | + |
| 140 | + it('resolves the natural size of a loaded image', async () => { |
| 141 | + stubImage({ width: 64, height: 64 }); |
| 142 | + await expect(measureIconInBrowser('/x.png')).resolves.toEqual({ width: 64, height: 64 }); |
| 143 | + vi.unstubAllGlobals(); |
| 144 | + }); |
| 145 | + |
| 146 | + it('settles null on a load error or a zero size', async () => { |
| 147 | + stubImage('error'); |
| 148 | + await expect(measureIconInBrowser('/x.png')).resolves.toBeNull(); |
| 149 | + stubImage({ width: 0, height: 0 }); |
| 150 | + await expect(measureIconInBrowser('/x.png')).resolves.toBeNull(); |
| 151 | + vi.unstubAllGlobals(); |
| 152 | + }); |
| 153 | + |
| 154 | + it('settles null when the image never answers, after the timeout', async () => { |
| 155 | + vi.useFakeTimers(); |
| 156 | + stubImage('hang'); |
| 157 | + const pending = measureIconInBrowser('/x.png', 500); |
| 158 | + await vi.advanceTimersByTimeAsync(500); |
| 159 | + await expect(pending).resolves.toBeNull(); |
| 160 | + vi.useRealTimers(); |
| 161 | + vi.unstubAllGlobals(); |
| 162 | + }); |
| 163 | +}); |
0 commit comments