// App Store "Header and Search Results" creative assets: local size / alpha / video checks, the check / validate note, // and `apple creative-assets` against the fake App Store Connect (Asset Library upload, select once processed (no // approval wait: pitfall 76), replace on a changed file, nothing on the same file, rejected / failed / archived files, // --add-for-review into the draft submission without submitting, --verify read-only). import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, test } from 'bun:test'; import { md5 } from '../cli/apple/asc'; import { classify, MAX_FAILED, pickAsset, processingTiming, REF, run, selectable } from '../cli/apple/creative-assets'; import { checkCreativeImage, checkCreativeVideo, CREATIVE_NOTE, creativeFiles, creativeNote } from '../cli/shots/creative'; import { imageInfo } from '../cli/shots/spec'; import { upsertTiming } from '../cli/lib/upsert'; import { ascEnv, BASE, capture, project, restore } from './helpers/store-mock'; import { installStores, type Stores, uninstallStores, writes } from './helpers/fake-stores'; /** A PNG header (IHDR only: imageInfo reads no more), RGB or RGBA, plus `salt` bytes so files differ. */ function png(w: number, h: number, alpha = false, salt = 0): Uint8Array { const b = new Uint8Array(33 + 1); b.set([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0, 0, 0, 13, 0x49, 0x48, 0x44, 0x52]); const dv = new DataView(b.buffer); dv.setUint32(16, w); dv.setUint32(20, h); b[24] = 8; b[25] = alpha ? 6 : 2; b[33] = salt; return b; } describe('creative asset checks', () => { test('header: exactly 3840x1646, no alpha', () => { expect(checkCreativeImage(imageInfo(png(3840, 1646)), 'header')).toEqual([]); expect(checkCreativeImage(imageInfo(png(3840, 1645)), 'header')[0]!.code).toBe('size'); expect(checkCreativeImage(imageInfo(png(3840, 1646, true)), 'header').map(i => i.code)).toEqual(['alpha']); }); test('search: 3:2 from 1920x1280 to 3840x2560', () => { for (const [w, h] of [[1920, 1280], [3000, 2000], [3840, 2560]]) expect(checkCreativeImage(imageInfo(png(w!, h!)), 'search')).toEqual([]); for (const [w, h] of [[1800, 1200], [3900, 2600], [2000, 1280]]) expect(checkCreativeImage(imageInfo(png(w!, h!)), 'search')[0]!.code).toBe('size'); }); test('universal: one 5244x2950 PNG for either slot', () => { expect(checkCreativeImage(imageInfo(png(5244, 2950)), 'header', true)).toEqual([]); expect(checkCreativeImage(imageInfo(png(5244, 2950)), 'search', true)).toEqual([]); expect(checkCreativeImage(imageInfo(png(3840, 2160)), 'search', true)[0]!.code).toBe('size'); }); test('video: length, fps, container, aspect; ffprobe missing is a skipped-check warning', () => { const v = (w: number, h: number, fps: number, duration: number, audio = false) => ({ bytes: 1, duration, video: { codec: 'h264', w, h, fps }, ...(audio ? { audio: { codec: 'aac', channels: 2, sampleRate: 48000 } } : {}) }); expect(checkCreativeVideo('header.mov', v(3840, 1646, 30, 10), 'header')).toEqual([]); expect(checkCreativeVideo('search.mp4', v(1920, 1280, 60, 30), 'search')).toEqual([]); expect(checkCreativeVideo('search.mp4', v(1920, 1280, 25, 4), 'search').map(i => i.msg).join()).toMatch(/5-30 s.*30 or 60 fps/); expect(checkCreativeVideo('header.webm', v(3840, 1646, 30, 10), 'header')[0]!.msg).toContain('.mov'); expect(checkCreativeVideo('header.mov', v(1920, 1080, 30, 10), 'header')[0]!.msg).toContain('aspect'); expect(checkCreativeVideo('search.mp4', v(1920, 1280, 30, 10, true), 'search')[0]!.level).toBe('warn'); const skipped = checkCreativeVideo('header.mov', null, 'header'); expect(skipped).toHaveLength(1); expect(skipped[0]!.msg).toContain('not checked (ffprobe missing'); }); test('files: own header / search first, the universal image fills the rest', () => { const list = (d: string) => (d.endsWith('en/creative') ? ['header.png', 'notes.txt'] : d.endsWith('creative') ? ['universal.png'] : []); const fs = creativeFiles('/r', 'en', list); expect(fs.map(f => [f.placement, f.file, f.universal])).toEqual([['header', '/r/en/creative/header.png', false], ['search', '/r/creative/universal.png', true]]); }); test('info note: an App Store app without any asset; none for Play-only or with files', () => { const langs = [{ id: 'en', apple: 'en-US' }]; expect(creativeNote({ apple: {}, languages: langs }, '/r', () => [])).toBe(CREATIVE_NOTE); expect(CREATIVE_NOTE).toContain('optional; without it the App Store uses in-app events, app previews and screenshots'); expect(CREATIVE_NOTE).toContain('feature graphic'); expect(creativeNote({ languages: langs }, '/r', () => [])).toBeNull(); expect(creativeNote({ apple: {}, languages: langs }, '/r', d => (d.endsWith('creative') ? ['search.png'] : []))).toBeNull(); }); }); describe('asset states (pitfall 76: Prepare for Submission is selectable)', () => { test('classify and selectable', () => { expect(['AWAITING_UPLOAD', 'UPLOAD_COMPLETE', undefined].map(classify)).toEqual(['processing', 'processing', 'processing']); expect(selectable(classify('PREPARE_FOR_SUBMISSION'))).toBe(true); expect(selectable(classify('APPROVED'))).toBe(true); expect(selectable(classify('IN_REVIEW'))).toBe(true); for (const s of ['FAILED', 'REJECTED', 'ARCHIVED', 'UPLOAD_COMPLETE']) expect(selectable(classify(s))).toBe(false); }); const a = (id: string, sum: string, state: string, stateDetails?: unknown[]) => ({ id, type: 'appAssetLibraryImages', attributes: { referenceName: REF(sum), state, stateDetails } }); const S = 'a'.repeat(32); test('pickAsset: reviewed beats unreviewed, archived is unarchived, rejected and repeated failures refuse', () => { expect(pickAsset([a('1', S, 'PREPARE_FOR_SUBMISSION'), a('2', S, 'APPROVED')], S).asset!.id).toBe('2'); expect(pickAsset([a('1', S, 'ARCHIVED')], S)).toMatchObject({ unarchive: true }); expect(pickAsset([a('1', S, 'REJECTED', [{ code: 'GUIDELINE_2_3' }])], S).refuse).toContain('GUIDELINE_2_3'); expect(pickAsset([a('1', S, 'FAILED')], S)).toEqual({}); expect(pickAsset(Array.from({ length: MAX_FAILED }, (_, i) => a(String(i), S, 'FAILED')), S).refuse).toContain('failed processing'); expect(pickAsset([a('1', 'b'.repeat(32), 'APPROVED')], S)).toEqual({}); }); }); describe('apple creative-assets against the fake App Store Connect', () => { let s: Stores; beforeAll(ascEnv); afterAll(restore); beforeEach(() => { upsertTiming.waitMs = 0; processingTiming.timeoutMs = 0; processingTiming.pollMs = 0; s = installStores(); }); afterEach(uninstallStores); const APP = '6400000101'; const CFG = { ...BASE, languages: [{ id: 'en', apple: 'en-US', play: 'en-US' }] }; const setup = (files: Record) => { s.asc.put({ type: 'appAssetLibraries', id: 'lib-1', attributes: {}, rels: { app: APP } }); s.asc.put({ type: 'appStoreVersions', id: 'ver-1', attributes: { versionString: '1.0.0', appVersionState: 'PREPARE_FOR_SUBMISSION', platform: 'IOS' }, rels: { app: APP } }); s.asc.put({ type: 'appStoreVersionLocalizations', id: 'loc-en', attributes: { locale: 'en-US' }, rels: { appStoreVersion: 'ver-1' } }); return project(CFG, files); }; const go = async (p: ReturnType, argv: string[] = [], exit = 0) => { const a = s.calls.length; const r = await capture(() => run(p.ctx(argv))); if ((r.exit ?? 0) !== exit) throw new Error(`exit ${r.exit}: ${r.out}\n${r.err}`); return { out: r.out + r.err, w: writes(s.calls.slice(a)).map(c => `${c.method} ${decodeURIComponent(c.path).replace(/\?.*/, '').replace(/\/v1\/(\w+)\/[^/]+$/, '/v1/$1/{id}')}`).filter(x => x.includes(' /v1/')) }; }; const lib = () => [...s.asc.all('appAssetLibraryImages'), ...s.asc.all('appAssetLibraryVideos')]; const files = (h = 1, q = 2) => ({ 'store/renders/en/creative/header.png': png(3840, 1646, false, h), 'store/renders/en/creative/search.png': png(1920, 1280, false, q) }); test('dry run, upload + select in one run (no approval wait), same file = nothing, changed file = replace', async () => { const p = setup(files()); const dry = await go(p, ['--dry']); expect(dry.w).toEqual([]); expect(dry.out).toContain('would upload header.png'); const first = await go(p); expect(first.w).toEqual(['POST /v1/appAssetLibraryImages', 'PATCH /v1/appAssetLibraryImages/{id}', 'POST /v1/appAssetLibraryPlacements', 'POST /v1/appAssetLibraryImages', 'PATCH /v1/appAssetLibraryImages/{id}', 'POST /v1/appAssetLibraryPlacements']); expect(lib().map(x => x.attributes.referenceName)).toContain(REF(md5(files()['store/renders/en/creative/header.png']))); expect(s.asc.all('appAssetLibraryPlacements').map(x => x.attributes.placementType).sort()).toEqual(['APP_STORE_SEARCH_RESULTS_ASSET', 'PRODUCT_PAGE_HEADER_ASSET']); expect(first.out).toContain('--add-for-review'); expect(first.out).not.toContain("waiting for Apple's approval"); expect((await go(p)).w).toEqual([]); const q = project(CFG, files(9)); const rep = await go(q); expect(rep.w).toEqual(['POST /v1/appAssetLibraryImages', 'PATCH /v1/appAssetLibraryImages/{id}', 'DELETE /v1/appAssetLibraryPlacements/{id}', 'POST /v1/appAssetLibraryPlacements']); expect(s.asc.all('appAssetLibraryPlacements')).toHaveLength(2); }); test('processing that outlasts the bounded wait: not selected, re-run selects without a second upload', async () => { const f = files(); const p = setup(f); for (const [k, id] of [['header', 'img-h'], ['search', 'img-s']] as const) s.asc.put({ type: 'appAssetLibraryImages', id, attributes: { category: 'CREATIVE_ASSETS', referenceName: REF(md5(f[`store/renders/en/creative/${k}.png`])), state: 'UPLOAD_COMPLETE', fileName: `${k}.png` }, rels: { assetLibrary: 'lib-1' } }); const r1 = await go(p); expect(r1.w).toEqual([]); expect(r1.out).toContain('still UPLOAD_COMPLETE'); expect(r1.out).toContain('2 still processing'); for (const x of lib()) x.attributes.state = 'PREPARE_FOR_SUBMISSION'; expect((await go(p)).w).toEqual(['POST /v1/appAssetLibraryPlacements', 'POST /v1/appAssetLibraryPlacements']); }); test('a rejected file is not uploaded again; an archived one is unarchived, not re-uploaded', async () => { const f = files(); const p = setup(f); s.asc.put({ type: 'appAssetLibraryImages', id: 'img-rej', attributes: { category: 'CREATIVE_ASSETS', referenceName: REF(md5(f['store/renders/en/creative/header.png'])), state: 'REJECTED', fileName: 'header.png' }, rels: { assetLibrary: 'lib-1' } }); s.asc.put({ type: 'appAssetLibraryImages', id: 'img-arc', attributes: { category: 'CREATIVE_ASSETS', referenceName: REF(md5(f['store/renders/en/creative/search.png'])), state: 'ARCHIVED', fileName: 'search.png' }, rels: { assetLibrary: 'lib-1' } }); const r = await go(p, [], 1); expect(r.out).toContain('rejected by App Review'); expect(r.w).toEqual(['PATCH /v1/appAssetLibraryImages/{id}', 'POST /v1/appAssetLibraryPlacements']); expect(s.asc.all('appAssetLibraryPlacements').map(x => x.attributes.placementType)).toEqual(['APP_STORE_SEARCH_RESULTS_ASSET']); }); test('--add-for-review: unreviewed assets go into the draft once, nothing is submitted', async () => { const p = setup(files()); await go(p); const dry = await go(p, ['--add-for-review', '--dry']); expect(dry.w).toEqual([]); const add = await go(p, ['--add-for-review']); expect(add.w).toEqual(['POST /v1/reviewSubmissions', 'POST /v1/reviewSubmissionItems', 'POST /v1/reviewSubmissionItems']); expect(add.out).toContain('nothing was submitted'); expect(add.w.some(x => x.startsWith('PATCH /v1/reviewSubmissions'))).toBe(false); expect((await go(p, ['--add-for-review'])).w).toEqual([]); }); test('--verify is read-only and fails on a slot that differs', async () => { const p = setup(files()); const before = await go(p, ['--verify'], 1); expect(before.w).toEqual([]); expect(before.out).toContain('shows nothing, wanted header.png'); await go(p); const ok = await go(p, ['--verify']); expect(ok.w).toEqual([]); expect(ok.out).toContain('every slot shows the local file'); }); test('live version only (no editable one): upload, add for review alone, select once approved', async () => { const p = setup(files()); s.asc.all('appStoreVersions')[0]!.attributes.appVersionState = 'READY_FOR_DISTRIBUTION'; const up = await go(p, ['--add-for-review']); expect(up.out).toContain('using the live version 1.0.0'); expect(up.w).toEqual(['POST /v1/appAssetLibraryImages', 'PATCH /v1/appAssetLibraryImages/{id}', 'POST /v1/appAssetLibraryImages', 'PATCH /v1/appAssetLibraryImages/{id}', 'POST /v1/reviewSubmissions', 'POST /v1/reviewSubmissionItems', 'POST /v1/reviewSubmissionItems']); expect(up.out).toContain('2 waiting for App Review (live version)'); expect(s.asc.all('appAssetLibraryPlacements')).toHaveLength(0); for (const x of lib()) x.attributes.state = 'APPROVED'; expect((await go(p)).w).toEqual(['POST /v1/appAssetLibraryPlacements', 'POST /v1/appAssetLibraryPlacements']); expect((await go(p, ['--verify'])).out).toContain('every slot shows the local file'); }); test('a bad file stops the run before any call', async () => { const p = setup({ 'store/renders/en/creative/header.png': png(3840, 1600, true) }); const a = s.calls.length; await expect(capture(() => run(p.ctx([])))).rejects.toThrow('creative asset problems'); expect(s.calls.length).toBe(a); }); });