// Functional test: drives the app through its JS API + synthetic input and checks results. const { launch, sleep } = require('./cdp.js'); const shotDir = process.argv[2] || '/tmp'; (async () => { const b = await launch(); let failures = 0; const page = await b.newPage('http://localhost:8080/'); const check = async (name, expr) => { try { const v = await page.eval(expr); if (v === true || (v && v.ok)) console.log(`ok ${name}${v && v.info ? ' ' + JSON.stringify(v.info) : ''}`); else { failures++; console.log(`FAIL ${name} -> ${JSON.stringify(v)}`); } } catch (e) { failures++; console.log(`FAIL ${name} threw ${e.message.split('\n')[0]}`); } }; try { await sleep(600); await page.eval(` window.T = { px(canvas, x, y) { return Array.from(canvas.getContext('2d').getImageData(x, y, 1, 1).data); }, comp(x, y) { return T.px(atelier.renderer.composite, x, y); }, async frame() { await new Promise(r => requestAnimationFrame(() => requestAnimationFrame(r))); }, }; 1`); await check('new document', `(() => { atelier.newDocument({width: 400, height: 300, background: '#ffffff'}); return atelier.doc.width === 400; })()`); // selection + fill await check('rect selection + fill fg', `(async () => { const { Selection, shapeMask } = await import('/js/core/selection.js'); const d = atelier.doc; d.setSelection(Selection.orNull(shapeMask(400, 300, {type:'rect', x: 10, y: 10, w: 50, h: 40}))); atelier.setColor('fg', {r: 255, g: 0, b: 0, a: 1}); atelier.run('edit.fillFG'); await T.frame(); const inside = T.comp(20, 20), outside = T.comp(100, 100); return inside[0] === 255 && inside[1] === 0 && outside[1] === 255 || {inside, outside}; })()`); await check('undo fill', `(async () => { atelier.undo(); await T.frame(); const p = T.comp(20, 20); return p[1] === 255 || p; })()`); await check('redo fill', `(async () => { atelier.redo(); await T.frame(); const p = T.comp(20, 20); return p[1] === 0 || p; })()`); await check('deselect', `(() => { atelier.run('select.none'); return atelier.doc.selection === null; })()`); // layers + blend modes await check('new layer + multiply', `(async () => { const ops = await import('/js/core/ops.js'); const d = atelier.doc; const l = ops.addLayer(d); l.ctx.fillStyle = 'rgb(128,128,128)'; l.ctx.fillRect(0, 0, 400, 300); l.touch(); d.markDirty(); ops.setLayerProps(d, l, { blendMode: 'multiply' }); await T.frame(); const p = T.comp(20, 20), q = T.comp(200, 200); return (p[0] === 128 && p[1] === 0 && q[0] === 128) || {p, q}; })()`); await check('layer opacity', `(async () => { const ops = await import('/js/core/ops.js'); const d = atelier.doc; ops.setLayerProps(d, d.activeLayer, { opacity: 0.5, blendMode: 'normal' }); await T.frame(); const q = T.comp(200, 200); return Math.abs(q[0] - 191) <= 2 || q; })()`); await check('layer mask from selection', `(async () => { const ops = await import('/js/core/ops.js'); const { Selection } = await import('/js/core/selection.js'); const d = atelier.doc; ops.setLayerProps(d, d.activeLayer, { opacity: 1 }); d.setSelection(Selection.all(400, 300).inverted() || null); const { shapeMask } = await import('/js/core/selection.js'); d.setSelection(Selection.orNull(shapeMask(400, 300, {type:'rect', x: 0, y: 0, w: 200, h: 300}))); ops.addMask(d); d.setSelection(null); await T.frame(); const a = T.comp(100, 150), b = T.comp(300, 150); return (a[0] === 128 && b[0] === 255) || {a, b}; })()`); await check('paint on mask hides', `(async () => { const d = atelier.doc; if (!d.editMask) return 'not editing mask'; atelier.setColor('fg', {r:0,g:0,b:0,a:1}); atelier.run('edit.fillFG'); await T.frame(); const a = T.comp(100, 150); return a[0] === 255 || a; })()`); await check('undo mask paint', `(async () => { atelier.undo(); await T.frame(); const a = T.comp(100,150); return a[0] === 128 || a; })()`); await check('apply mask', `(async () => { atelier.run('layer.applyMask'); await T.frame(); const l = atelier.doc.activeLayer; return !l.mask && T.px(l.canvas, 300, 150)[3] === 0 && T.comp(100,150)[0] === 128; })()`); await check('clipping mask', `(async () => { const ops = await import('/js/core/ops.js'); const d = atelier.doc; const l = ops.addLayer(d); l.ctx.fillStyle = '#00ff00'; l.ctx.fillRect(0, 0, 400, 300); l.touch(); ops.setLayerProps(d, l, { clip: true }); await T.frame(); const a = T.comp(100, 150), b = T.comp(300, 150); return (a[1] === 255 && a[0] === 0 && b[0] === 255 && b[2] === 255) || {a, b}; })()`); await check('merge down keeps look', `(async () => { const before = [T.comp(100,150), T.comp(300,150)].join(); atelier.run('layer.mergeDown'); await T.frame(); const after = [T.comp(100,150), T.comp(300,150)].join(); return before === after && atelier.doc.layers.length === 2 || {before, after, n: atelier.doc.layers.length}; })()`); // text layer await check('text layer', `(async () => { const t = atelier.tools.get('text'); atelier.setTool('text'); t.createAt({x: 20, y: 200}); const ta = document.querySelector('.text-editor'); ta.value = 'Hello'; ta.dispatchEvent(new Event('input')); t.commit(); const l = atelier.doc.activeLayer; return (l.isText && l.text.content === 'Hello' && atelier.doc.history.entries.at(-1).label === 'Add text') || {type: l.type, h: atelier.doc.history.entries.at(-1).label}; })()`); await check('text undo removes layer', `(() => { const n = atelier.doc.layers.length; atelier.undo(); const m = atelier.doc.layers.length; atelier.redo(); return m === n - 1 && atelier.doc.layers.length === n; })()`); // brush stroke via real pointer events on canvas await page.eval(`atelier.setTool('brush'); atelier.run('layer.new'); atelier.renderer.setZoom(1); atelier.renderer.fit(atelier.doc); 1`); const r = await page.eval(`(() => { const v = atelier.doc.view; const c = atelier.renderer.canvas.getBoundingClientRect(); return {x: c.left + v.panX, y: c.top + v.panY, z: v.zoom}; })()`); await page.drag([[r.x + 50 * r.z, r.y + 50 * r.z], [r.x + 150 * r.z, r.y + 50 * r.z], [r.x + 250 * r.z, r.y + 60 * r.z]]); await sleep(100); await check('brush stroke painted', `(() => { const p = T.px(atelier.doc.activeLayer.canvas, 150, 50); return p[3] > 200 || p; })()`); // all filters await check('all filters run', `(async () => { const { FILTERS, defaultParams } = await import('/js/imaging/filters.js'); const c = document.createElement('canvas'); c.width = 64; c.height = 48; const ctx = c.getContext('2d'); const g = ctx.createLinearGradient(0,0,64,48); g.addColorStop(0,'#f00'); g.addColorStop(1,'#00f'); ctx.fillStyle = g; ctx.fillRect(0,0,64,48); const bad = []; for (const [id, f] of Object.entries(FILTERS)) { try { const out = f.apply(ctx.getImageData(0,0,64,48), defaultParams(f), {x:0,y:0,docWidth:64,docHeight:48,fg:{r:0,g:0,b:0},bg:{r:255,g:255,b:255}}); if (!(out instanceof ImageData) || out.width !== 64) bad.push(id + ':shape'); for (let i = 0; i < out.data.length; i++) if (Number.isNaN(out.data[i])) { bad.push(id + ':nan'); break; } } catch (e) { bad.push(id + ':' + e.message); } } return bad.length ? bad : true; })()`); // exports await check('exports', `(async () => { const { exportImage, availableFormats } = await import('/js/io/export.js'); const { decodeTIFF } = await import('/js/io/encoders/tiff.js'); const res = {}; for (const f of availableFormats()) { const blob = await exportImage(atelier.doc, { format: f.id, quality: 0.8 }); let decoded = '-'; try { if (f.id === 'tiff') { const id = decodeTIFF(await blob.arrayBuffer()); decoded = id.width + 'x' + id.height; } else { const bm = await createImageBitmap(blob); decoded = bm.width + 'x' + bm.height; } } catch (e) { decoded = 'ERR ' + e.message; } res[f.id] = blob.size + ' ' + decoded; } const ok = Object.entries(res).every(([k, v]) => k === 'ico' ? !v.includes('ERR') : v.endsWith('400x300')); return ok ? { ok, info: res } : res; })()`); await check('gif/tiff/bmp pixel fidelity', `(async () => { const { exportImage } = await import('/js/io/export.js'); const { decodeTIFF } = await import('/js/io/encoders/tiff.js'); const ref = T.comp(100, 150); const out = {}; for (const fmt of ['gif', 'bmp', 'tiff', 'png']) { const blob = await exportImage(atelier.doc, { format: fmt, dither: false }); let data; if (fmt === 'tiff') data = decodeTIFF(await blob.arrayBuffer()).data.slice((150*400+100)*4, (150*400+100)*4+4); else { const bm = await createImageBitmap(blob); const c = document.createElement('canvas'); c.width = bm.width; c.height = bm.height; c.getContext('2d').drawImage(bm,0,0); data = c.getContext('2d').getImageData(100,150,1,1).data; } out[fmt] = Array.from(data); } const close = (a) => a.every((v, i) => Math.abs(v - ref[i]) <= 8); return Object.values(out).every(close) || { ref, out }; })()`); // project round trip await check('project save/load round trip', `(async () => { const { saveProject, loadProject } = await import('/js/io/project.js'); const { flattenToCanvas } = await import('/js/render/compositor.js'); const d = atelier.doc; const a = flattenToCanvas(d).getContext('2d').getImageData(0,0,d.width,d.height).data; const blob = await saveProject(d); const { doc } = await loadProject(blob); const bdat = flattenToCanvas(doc).getContext('2d').getImageData(0,0,doc.width,doc.height).data; let diff = 0; for (let i = 0; i < a.length; i++) diff = Math.max(diff, Math.abs(a[i]-bdat[i])); const types = doc.layers.map(l => l.type).join(','); return (diff <= 2 && doc.layers.length === d.layers.length && types.includes('text')) || {diff, n: doc.layers.length, types, size: blob.size}; })()`); // document ops await check('image resize/canvas size/rotate/crop + undo', `(async () => { const ops = await import('/js/core/ops.js'); const d = atelier.doc; ops.resizeImage(d, 200, 150); if (d.width !== 200) return 'resize'; ops.resizeCanvas(d, 300, 150, {x: 0, y: 0.5}); if (d.width !== 300) return 'canvas'; ops.rotateImage(d, 90); if (d.width !== 150 || d.height !== 300) return 'rotate'; ops.cropTo(d, {x: 10, y: 10, w: 50, h: 60}); if (d.width !== 50) return 'crop'; for (let i = 0; i < 4; i++) atelier.undo(); return d.width === 400 && d.height === 300 && d.layers.every(l => l.canvas.width === 400) || [d.width, d.height]; })()`); // transform tool: scale layer 2x await check('free transform', `(async () => { const ops = await import('/js/core/ops.js'); const d = atelier.doc; const l = ops.addLayer(d); l.ctx.fillStyle = '#0000ff'; l.ctx.fillRect(100, 100, 20, 20); l.touch(); atelier.setTool('transform'); const t = atelier.tool; if (!t.state) return 'no state'; t.state.w = 40; t.state.h = 40; t.update(); t.apply(); const p = T.px(l.canvas, 125, 125), q = T.px(l.canvas, 85, 85); atelier.setTool('move'); return (p[2] === 255 && p[3] === 255 && q[3] === 0) || {p, q}; })()`); // move tool with selection await check('move selection pixels', `(async () => { const { Selection, shapeMask } = await import('/js/core/selection.js'); const d = atelier.doc; const l = d.activeLayer; d.setSelection(Selection.orNull(shapeMask(400, 300, {type: 'rect', x: 90, y: 90, w: 60, h: 60}))); const m = atelier.tools.get('move'); atelier.setTool('move'); const drag = m.begin(l, false); drag.start = {x: 0, y: 0}; m.applyOffset(drag, 100, 0); m.finish(drag); const moved = T.px(l.canvas, 225, 110), old = T.px(l.canvas, 110, 110); const sb = d.selection.bounds; return (moved[2] === 255 && old[3] === 0 && sb.x === 190) || {moved, old, sb}; })()`); // clipboard internal await check('copy/paste internal', `(async () => { const d = atelier.doc; const n = d.layers.length; atelier.clipboard.copy(false); atelier.clipboard.pasteCanvas(atelier.clipboard.data); const l = d.activeLayer; return (d.layers.length === n + 1 && T.px(l.canvas, 225, 110)[2] === 255) || {n: d.layers.length}; })()`); // wand await check('magic wand', `(async () => { const d = atelier.doc; d.setSelection(null); const w = atelier.tools.get('wand'); w.options.sampleMerged = false; w.options.mode = 'replace'; w.options.feather = 0; atelier.setTool('wand'); w.onPointerDown({shiftKey:false, altKey:false}, {x: 225, y: 110}); const b = d.selection?.bounds; return (b && b.w >= 38 && b.w <= 42) || b; })()`); // bucket fill await check('bucket fill', `(async () => { const ops = await import('/js/core/ops.js'); const d = atelier.doc; d.setSelection(null); d.setActiveLayer(d.layers[0].id); atelier.setColor('fg', {r: 0, g: 200, b: 0, a: 1}); const bt = atelier.tools.get('bucket'); bt.options.sampleMerged = false; bt.options.contiguous = true; atelier.setTool('bucket'); bt.onPointerDown({altKey:false}, {x: 390, y: 290}); const p = T.px(d.layers[0].canvas, 390, 290); return p[1] === 200 || p; })()`); // gradient & shape via tool API await check('gradient + shape tools', `(async () => { const d = atelier.doc; atelier.run('layer.new'); const g = atelier.tools.get('gradient'); atelier.setTool('gradient'); g.onPointerDown({}, {x: 0, y: 0}); g.onPointerMove({}, {x: 400, y: 0}); g.onPointerUp({}, {x: 400, y: 0}); const l = d.activeLayer; const a = T.px(l.canvas, 2, 5), b = T.px(l.canvas, 397, 5); const s = atelier.tools.get('shape'); atelier.setTool('shape'); atelier.run('layer.new'); s.options.shape = 'ellipse'; s.options.fill = 'fg'; s.onPointerDown({}, {x: 10, y: 10}); s.onPointerMove({}, {x: 60, y: 60}); s.onPointerUp({}, {x: 60, y: 60}); const c = T.px(d.activeLayer.canvas, 35, 35), e = T.px(d.activeLayer.canvas, 12, 12); return (a[1] > 150 && b[0] > 200 && c[3] === 255 && e[3] === 0) || {a, b, c, e}; })()`); // menus render await check('menus open', `(() => { const titles = [...document.querySelectorAll('.menu-title')]; let items = 0; for (const t of titles) { t.dispatchEvent(new PointerEvent('pointerdown', {bubbles: true})); items += document.querySelectorAll('.menu-popup .menu-item').length; atelier.ui.menu.close(); } return items > 60 || items; })()`); await check('history panel entries', `document.querySelectorAll('.history-row').length > 10`); await page.eval(`atelier.renderer.fit(atelier.doc); 1`); await sleep(300); await page.screenshot(`${shotDir}/functional.png`); console.log('--- console ---\n' + (page.logs.join('\n') || '(none)')); } finally { await b.close(); } console.log(failures ? `${failures} FAILURES` : 'ALL PASSED'); process.exit(failures ? 1 : 0); })().catch((e) => { console.error(e); process.exit(1); });