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('