From 4264c38689a544f44f0c8dff824dd83ae9327281 Mon Sep 17 00:00:00 2001 From: CRT Date: Sat, 10 Oct 2026 14:16:38 +0000 Subject: [PATCH] Tests: gallery data and HTML, variant paths, contact sheet layout --- tests/fixtures.ts | 25 +++++++++ tests/gallery.test.ts | 120 ++++++++++++++++++++++++++++++++++++++++++ tests/specs.test.ts | 24 +-------- 3 files changed, 146 insertions(+), 23 deletions(-) create mode 100644 tests/fixtures.ts create mode 100644 tests/gallery.test.ts diff --git a/tests/fixtures.ts b/tests/fixtures.ts new file mode 100644 index 00000000..95f703aa --- /dev/null +++ b/tests/fixtures.ts @@ -0,0 +1,25 @@ +// Tiny PNG / JPEG headers for the image checks (imageInfo reads only the header). Shared by the tests. + +/** PNG header imageInfo reads: signature + IHDR (+ optional tRNS) + IEND, padded to `pad` bytes. */ +export function png(w: number, h: number, o: { colorType?: number; depth?: number; trns?: boolean; pad?: number } = {}): Uint8Array { + const chunks = 8 + 25 + (o.trns ? 14 : 0) + 12; + const b = new Uint8Array(Math.max(chunks, o.pad ?? 0)); + b.set([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]); + const dv = new DataView(b.buffer); + dv.setUint32(8, 13); b.set([0x49, 0x48, 0x44, 0x52], 12); dv.setUint32(16, w); dv.setUint32(20, h); + b[24] = o.depth ?? 8; b[25] = o.colorType ?? 2; + let p = 33; + if (o.trns) { dv.setUint32(p, 2); b.set([0x74, 0x52, 0x4e, 0x53], p + 4); p += 14; } + dv.setUint32(p, 0); b.set([0x49, 0x45, 0x4e, 0x44], p + 4); + return b; +} + +/** JPEG with an APP0 segment and a baseline SOF0 frame header. */ +export function jpeg(w: number, h: number, pad = 0): Uint8Array { + const head = [0xff, 0xd8, 0xff, 0xe0, 0, 16, 0x4a, 0x46, 0x49, 0x46, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, + 0xff, 0xc0, 0, 17, 8, h >> 8, h & 255, w >> 8, w & 255, 3, 1, 0x22, 0, 2, 0x11, 1, 3, 0x11, 1, 0xff, 0xd9]; + const b = new Uint8Array(Math.max(head.length, pad)); + b.set(head); + return b; +} + diff --git a/tests/gallery.test.ts b/tests/gallery.test.ts new file mode 100644 index 00000000..cac1f2e1 --- /dev/null +++ b/tests/gallery.test.ts @@ -0,0 +1,120 @@ +// Review gallery (data + HTML), A/B variant paths, contact sheet layout. Files are tiny generated headers; no browser. +import { describe, expect, test } from 'bun:test'; +import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import { join } from 'node:path'; +import { galleryData, galleryHtml, scriptJson } from '../cli/shots/gallery'; +import { renderDir, sheetLayout, variantRoot } from '../cli/shots/lib'; +import { scanRenders } from '../cli/shots/report'; +import { jpeg, png } from './fixtures'; + +function fixture(): string { + const dir = mkdtempSync(join(tmpdir(), 'sb-gallery-')); + const put = (rel: string, b: Uint8Array) => { mkdirSync(join(dir, rel, '..'), { recursive: true }); writeFileSync(join(dir, rel), b); }; + put('en/ios69/01.png', png(1320, 2868)); + put('en/ios69/02.png', png(1320, 2868, { colorType: 6 })); + put('en/play/01.png', png(1080, 1920)); + put('en/play/01.jpg', jpeg(1080, 1920, 8 * 1024 * 1024 + 1)); + put('en/play/02.png', png(1080, 1920)); + put('en/feature/1024x500.png', png(1024, 500)); + put('tr/ios69/01.png', png(1320, 2868)); + put('b/en/ios69/01.png', png(1320, 2868)); + put('en/preview/iphone/01.mp4', new Uint8Array(10)); + put('en/preview/iphone/01-poster.png', png(886, 1920)); + writeFileSync(join(dir, 'report.json'), JSON.stringify({ 'tr/ios69/01.png': { overflow: ['title line 1 shrunk to 60 %: consider shorter words'] } })); + return dir; +} + +describe('gallery', () => { + const probe = () => ({ bytes: 1000, duration: 10, video: { codec: 'h264', w: 886, h: 1920, fps: 30 }, audio: { codec: 'aac', channels: 2, sampleRate: 48000 } }); + + test('data: variants, rows in size order, png/jpeg merged, issues on tiles and sets', () => { + const dir = fixture(); + try { + const scan = scanRenders(dir, { langs: ['en', 'tr'], variants: ['b', 'missing'], required: { ios69: true, play: true, feature: true }, probe }); + const d = galleryData(scan, { at: dir, now: new Date('2026-10-10T12:00:00Z'), exists: f => f.endsWith('-poster.png') }); + expect(d.variants.map(v => v.label)).toEqual(['base', 'variant b']); + const en = d.variants[0]!.langs.find(l => l.lang === 'en')!; + expect(en.rows.map(r => r.size)).toEqual(['ios69', 'play', 'feature', 'preview/iphone']); + const play = en.rows.find(r => r.size === 'play')!; + expect(play.tiles.map(t => t.src)).toEqual(['en/play/01.png', 'en/play/02.png']); + expect(play.tiles[0]!.issues.map(i => i.msg)).toEqual([expect.stringMatching(/^JPEG copy: 8\.0 MB > 8 MB/)]); + expect(en.rows.find(r => r.size === 'ios69')!.tiles[1]!.issues[0]!.code).toBe('alpha'); + const vid = en.rows.find(r => r.size === 'preview/iphone')!.tiles[0]!; + expect(vid).toMatchObject({ kind: 'video', poster: 'en/preview/iphone/01-poster.png', w: 886, h: 1920 }); + expect(vid.issues.some(i => i.msg.includes('15-30 s'))).toBe(true); + const tr = d.variants[0]!.langs.find(l => l.lang === 'tr')!; + expect(tr.rows.find(r => r.size === 'ios69')!.tiles[0]!.issues[0]).toMatchObject({ level: 'warn', code: 'overflow' }); + expect(tr.rows.find(r => r.size === 'play')!.issues[0]!.code).toBe('count'); + expect(d.variants[1]!.langs.map(l => l.lang)).toEqual(['en', 'tr']); + expect(d.totals.images).toBe(8); + expect(d.totals.errors).toBeGreaterThanOrEqual(4); + expect(d.generated).toBe('2026-10-10 12:00'); + } finally { rmSync(dir, { recursive: true, force: true }); } + }); + + test('html: self-contained, escaped, data embedded', () => { + const dir = fixture(); + try { + const scan = scanRenders(dir, { langs: ['en'], probe }); + const html = galleryHtml(galleryData(scan, { at: dir, title: 'Game & renders' })); + expect(html.startsWith('')).toBe(true); + expect(html).toContain('Game <b>&</b> renders'); + expect(html).not.toMatch(/(src|href)=["']?(https?:)?\/\//); + expect(html).not.toMatch(/@import|url\(\s*["']?https?:/); + expect(html).toContain('prefers-color-scheme: dark'); + expect(html).toContain('id="only"'); + const json = /\u2028' })).toBe('{"a":"\\u003c/script>\\u003cb>\\u2028"}'); + }); + + test('relative paths from a gallery placed elsewhere', () => { + const dir = fixture(); + try { + const scan = scanRenders(dir, { langs: ['en'], probe }); + const d = galleryData(scan, { at: join(dir, 'review') }); + expect(d.variants[0]!.langs[0]!.rows[0]!.tiles[0]!.src).toBe('../en/ios69/01.png'); + } finally { rmSync(dir, { recursive: true, force: true }); } + }); +}); + +describe('variants', () => { + const variants = { b: { scenes: ['a'] }, short: {} }; + test('variantRoot', () => { + expect(variantRoot('store/renders', undefined, variants)).toBe('store/renders'); + expect(variantRoot('store/renders', 'base', variants)).toBe('store/renders'); + expect(variantRoot('store/renders/', 'b', variants)).toBe('store/renders/b'); + expect(() => variantRoot('r', 'c', variants)).toThrow('not in shots.variants (b, short)'); + expect(() => variantRoot('r', 'c', undefined)).toThrow('none configured'); + }); + test('renderDir', () => { + expect(renderDir('out', 'en')).toBe('out/en'); + expect(renderDir('out', 'en', 'b')).toBe('out/b/en'); + }); +}); + +describe('contact sheet layout', () => { + test('one scene per size packs into a grid, not a tall strip', () => { + const rows = ['ios69', 'ios65', 'play', 'tab7', 'tab10', 'feature'].map(size => ({ size, files: [`${size}/01.png`] })); + const l = sheetLayout(rows); + expect(l.tiles.length).toBe(6); + expect(l.h).toBeLessThan(2 * 900 + 4 * 24); + expect(l.w).toBeLessThanOrEqual(3600 + 24); + for (const t of l.tiles) expect(t.x + t.w).toBeLessThanOrEqual(l.w); + expect(l.tiles.find(t => t.file.startsWith('feature'))!.h).toBe(450); + }); + test('a wide block keeps its own row and sets the width', () => { + const l = sheetLayout([{ size: 'ios69', files: Array.from({ length: 10 }, (_, i) => `${i}.png`) }, { size: 'play', files: ['1.png'] }]); + const ios = l.tiles.filter(t => t.y === 24); + expect(ios.length).toBe(10); + expect(l.w).toBe(ios.at(-1)!.x + ios.at(-1)!.w + 24); + expect(l.tiles.at(-1)!.y).toBeGreaterThan(24); + expect(sheetLayout([])).toEqual({ w: 0, h: 0, tiles: [] }); + }); +}); diff --git a/tests/specs.test.ts b/tests/specs.test.ts index f256459b..df29bd1e 100644 --- a/tests/specs.test.ts +++ b/tests/specs.test.ts @@ -12,29 +12,7 @@ import { cropTopFor, validateCapture } from '../cli/shots/devices'; import { DEFAULT_IPAD, DEFAULT_PHONE, DEFAULT_TABLET, defaultSizes, frameDims, SIZES } from '../cli/shots/lib'; import { allIssues, requiredSets, scanRenders } from '../cli/shots/report'; import { checkCount, checkImage, type ImageInfo, IMAGE_SPECS, imageInfo } from '../cli/shots/spec'; - -/** PNG header imageInfo reads: signature + IHDR (+ optional tRNS) + IEND, padded to `pad` bytes. */ -function png(w: number, h: number, o: { colorType?: number; depth?: number; trns?: boolean; pad?: number } = {}): Uint8Array { - const chunks = 8 + 25 + (o.trns ? 14 : 0) + 12; - const b = new Uint8Array(Math.max(chunks, o.pad ?? 0)); - b.set([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]); - const dv = new DataView(b.buffer); - dv.setUint32(8, 13); b.set([0x49, 0x48, 0x44, 0x52], 12); dv.setUint32(16, w); dv.setUint32(20, h); - b[24] = o.depth ?? 8; b[25] = o.colorType ?? 2; - let p = 33; - if (o.trns) { dv.setUint32(p, 2); b.set([0x74, 0x52, 0x4e, 0x53], p + 4); p += 14; } - dv.setUint32(p, 0); b.set([0x49, 0x45, 0x4e, 0x44], p + 4); - return b; -} - -/** JPEG with an APP0 segment and a baseline SOF0 frame header. */ -function jpeg(w: number, h: number, pad = 0): Uint8Array { - const head = [0xff, 0xd8, 0xff, 0xe0, 0, 16, 0x4a, 0x46, 0x49, 0x46, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, - 0xff, 0xc0, 0, 17, 8, h >> 8, h & 255, w >> 8, w & 255, 3, 1, 0x22, 0, 2, 0x11, 1, 3, 0x11, 1, 0xff, 0xd9]; - const b = new Uint8Array(Math.max(head.length, pad)); - b.set(head); - return b; -} +import { jpeg, png } from './fixtures'; const codes = (i: { code: string }[]) => i.map(x => x.code);