1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
|
// Checks for layer styles, guides and rulers, the command palette, preferences,
// menu keyboard navigation, symmetry painting, alignment and selection commands.
const { launch, sleep } = require('./cdp.js');
const dir = process.argv[2] || '/tmp';
const CTRL = 2, SHIFT = 8;
(async () => {
const b = await launch();
const page = await b.newPage('http://localhost:8080/');
let fails = 0;
const check = async (name, expr) => {
try {
const v = typeof expr === 'string' ? await page.eval(expr) : await expr();
if (v === true) console.log(`ok ${name}`);
else { fails++; console.log(`FAIL ${name} -> ${JSON.stringify(v)}`); }
} catch (e) { fails++; console.log(`FAIL ${name} threw ${e.message.split('\n')[0]}`); }
};
try {
await sleep(500);
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))); },
screen(x, y) { const r = atelier.renderer.canvas.getBoundingClientRect(); const v = atelier.doc.view; return [r.left + v.panX + x * v.zoom, r.top + v.panY + y * v.zoom]; },
}; 1`);
const screen = (x, y) => page.eval(`T.screen(${x}, ${y})`);
// ------------------------------------------------------------ layer styles
await page.eval(`(() => {
atelier.newDocument({ width: 200, height: 200, background: null });
const l = atelier.doc.activeLayer;
l.ctx.fillStyle = '#ff0000'; l.ctx.fillRect(50, 50, 50, 50); l.touch(); atelier.doc.markDirty();
return 1; })()`);
await check('outside stroke', `(async () => {
const ops = await import('/js/core/ops.js');
const d = atelier.doc, l = d.activeLayer;
ops.setLayerProps(d, l, { effects: { stroke: { enabled: true, size: 4, position: 'outside', color: '#0000ff', opacity: 100 } } }, 'Layer style');
await T.frame();
const ring = T.comp(47, 75), far = T.comp(40, 75), inner = T.comp(52, 75);
return (ring.join() === '0,0,255,255' && far[3] === 0 && inner.join() === '255,0,0,255') || { ring, far, inner };
})()`);
await check('inside stroke + raw pixels untouched', `(async () => {
const ops = await import('/js/core/ops.js');
const d = atelier.doc, l = d.activeLayer;
ops.setLayerProps(d, l, { effects: { stroke: { enabled: true, size: 4, position: 'inside', color: '#0000ff', opacity: 100 } } });
await T.frame();
const edge = T.comp(51, 75), mid = T.comp(60, 75), out = T.comp(48, 75), raw = T.px(l.canvas, 51, 75);
return (edge[2] === 255 && edge[0] === 0 && mid[0] === 255 && out[3] === 0 && raw[0] === 255) || { edge, mid, out, raw };
})()`);
await check('drop shadow offset by angle', `(async () => {
const ops = await import('/js/core/ops.js');
const d = atelier.doc, l = d.activeLayer;
ops.setLayerProps(d, l, { effects: { dropShadow: { enabled: true, color: '#000000', opacity: 100, angle: 135, distance: 10, size: 0 } } });
await T.frame();
const sh = T.comp(105, 105), none = T.comp(45, 45), body = T.comp(75, 75);
return (sh[3] > 240 && sh[0] < 10 && none[3] === 0 && body[0] === 255) || { sh, none, body };
})()`);
await check('outer glow, inner shadow, color overlay', `(async () => {
const ops = await import('/js/core/ops.js');
const d = atelier.doc, l = d.activeLayer;
ops.setLayerProps(d, l, { effects: {
outerGlow: { enabled: true, color: '#00ff00', opacity: 100, spread: 50, size: 10 },
innerShadow: { enabled: true, color: '#000000', opacity: 100, angle: 90, distance: 6, size: 0 },
colorOverlay: { enabled: true, color: '#ffff00', opacity: 100, blendMode: 'normal' },
} });
await T.frame();
const glow = T.comp(46, 75), top = T.comp(75, 52), centre = T.comp(75, 75);
return (glow[1] > 100 && glow[0] === 0 && top[0] < 30 && centre[0] === 255 && centre[1] === 255 && centre[2] === 0) || { glow, top, centre };
})()`);
await check('style undo/redo + history label', `(async () => {
const d = atelier.doc, l = d.activeLayer;
const label = d.history.entries.at(-1).label;
atelier.undo(); await T.frame();
const undone = l.effects?.dropShadow?.enabled && !l.effects.colorOverlay;
atelier.redo(); await T.frame();
return (label === 'Layer properties' && undone && l.effects.colorOverlay.enabled) || { label, fx: l.effects };
})()`);
await check('layer style dialog live preview + OK', async () => {
await page.eval(`atelier.run('layer.clearStyle'); setTimeout(() => atelier.run('layer.style'), 0); 1`);
await sleep(250);
const rows = await page.eval(`[...document.querySelectorAll('.fx-row')].map(r => r.textContent).join('|')`);
// enable Stroke via its checkbox
await page.eval(`[...document.querySelectorAll('.fx-row')].find(r => r.textContent === 'Stroke').querySelector('input').click(); 1`);
await sleep(100);
const live = await page.eval(`(async () => { await T.frame(); return T.comp(48, 75); })()`);
await page.eval(`document.querySelector('.dialog-footer .btn.primary').click(); 1`);
await sleep(100);
const after = await page.eval(`({ label: atelier.doc.history.entries.at(-1).label, on: !!atelier.doc.activeLayer.effects?.stroke?.enabled, badge: !!document.querySelector('.layer-row.active .fx-badge') })`);
return (rows.includes('Drop Shadow') && live[3] === 255 && after.label === 'Layer style' && after.on && after.badge) || { rows, live, after };
});
await check('layer style dialog cancel restores', async () => {
await page.eval(`setTimeout(() => atelier.run('layer.style'), 0); 1`);
await sleep(200);
await page.eval(`[...document.querySelectorAll('.fx-row')].find(r => r.textContent === 'Stroke').querySelector('input').click(); 1`);
await sleep(50);
const off = await page.eval(`(async () => { await T.frame(); return T.comp(48, 75)[3]; })()`);
await page.key('Escape', { code: 'Escape' });
await sleep(100);
const back = await page.eval(`(async () => { await T.frame(); return [T.comp(48, 75)[3], atelier.doc.history.entries.at(-1).label]; })()`);
return (off === 0 && back[0] === 255 && back[1] === 'Layer style') || { off, back };
});
await check('copy/paste layer style', `(async () => {
const ops = await import('/js/core/ops.js');
const d = atelier.doc, src = d.activeLayer;
atelier.run('layer.copyStyle');
const l2 = ops.addLayer(d);
atelier.run('layer.pasteStyle');
return (l2.effects === src.effects && d.history.entries.at(-1).label === 'Paste layer style') || l2.effects;
})()`);
await check('project keeps styles; merge down bakes them', `(async () => {
const { saveProject, loadProject } = await import('/js/io/project.js');
const d = atelier.doc;
const { doc } = await loadProject(await saveProject(d));
const kept = doc.layers[0].effects?.stroke?.size === 3;
const before = T.comp(48, 75).join();
atelier.run('layer.mergeDown'); await T.frame();
const base = d.layers[0];
return (kept && base.effects === null && T.px(base.canvas, 48, 75)[3] === 255 && T.comp(48, 75).join() === before) || { kept, fx: base.effects, before, after: T.comp(48, 75) };
})()`);
await page.screenshot(`${dir}/features-fx.png`);
// ------------------------------------------------------------ rulers, guides, snapping
await page.eval(`atelier.newDocument({ width: 400, height: 300 }); atelier.renderer.setZoom(1); atelier.setTool('move'); 1`);
await sleep(100);
await page.key('r', { code: 'KeyR', modifiers: CTRL });
await check('Ctrl+R shows rulers', `atelier.guides.rulers === true && atelier.actions.get('view.rulers').checked(atelier)`);
await check('drag from top ruler adds a horizontal guide', async () => {
const rect = await page.eval(`(() => { const r = atelier.renderer.canvas.getBoundingClientRect(); return [r.left, r.top]; })()`);
const [, gy] = await screen(0, 120);
const [gx] = await screen(200, 0);
await page.drag([[gx, rect[1] + 8], [gx, gy - 20], [gx, gy]]);
return page.eval(`(() => { const d = atelier.doc; return (d.guideLines.length === 1 && d.guideLines[0].axis === 'y' && d.guideLines[0].pos === 120 && d.history.entries.at(-1).label === 'New guide') || d.guideLines; })()`);
});
await check('drag from left ruler adds a vertical guide (snaps to center)', async () => {
const rect = await page.eval(`(() => { const r = atelier.renderer.canvas.getBoundingClientRect(); return [r.left, r.top]; })()`);
const [gx, gy] = await screen(203, 150);
await page.drag([[rect[0] + 8, gy], [gx - 30, gy], [gx, gy]]);
return page.eval(`JSON.stringify(atelier.doc.guideLines[1]) === '{"axis":"x","pos":200}' || atelier.doc.guideLines`);
});
await check('move tool drags a guide; undo restores it', async () => {
const [x, y] = await screen(100, 120);
await page.drag([[x, y], [x, y + 20], [x, y + 40]]);
const moved = await page.eval(`atelier.doc.guideLines.find(g => g.axis === 'y').pos`);
await page.eval(`atelier.undo(); 1`);
const back = await page.eval(`atelier.doc.guideLines.find(g => g.axis === 'y').pos`);
return (moved === 160 && back === 120) || { moved, back };
});
await check('marquee snaps to a guide', async () => {
await page.eval(`atelier.setTool('rect-select'); 1`);
const a = await screen(50, 40), m = await screen(150, 100), z = await screen(196, 116);
await page.drag([a, m, z]);
return page.eval(`JSON.stringify(atelier.doc.selection.bounds) === '{"x":50,"y":40,"w":150,"h":80}' || atelier.doc.selection.bounds`);
});
await check('moving a layer snaps its edge to a guide', async () => {
await page.eval(`(async () => {
const ops = await import('/js/core/ops.js');
atelier.run('select.none');
const l = ops.addLayer(atelier.doc); l.ctx.fillStyle = '#123456'; l.ctx.fillRect(20, 20, 30, 30); l.touch();
atelier.setTool('move'); })()`);
const a = await screen(30, 30), m = await screen(100, 60), z = await screen(176, 120);
await page.drag([a, m, z]);
// dragged by 146: the left edge (20 + 146 = 166) is far from targets but the right edge (196) snaps to x = 200
return page.eval(`(() => { const p = T.px(atelier.doc.activeLayer.canvas, 171, 125); const q = T.px(atelier.doc.activeLayer.canvas, 169, 125); return (p[3] === 255 && q[3] === 0) || [p, q]; })()`);
});
await check('rotate / flip / crop carry guides along', `(async () => {
const ops = await import('/js/core/ops.js');
const d = atelier.doc;
d.change('t', () => { d.guideLines = [{ axis: 'x', pos: 100 }, { axis: 'y', pos: 50 }]; });
ops.rotateImage(d, 90);
const rot = JSON.stringify(d.guideLines);
ops.flipImage(d, 'h');
const flip = JSON.stringify(d.guideLines);
ops.cropTo(d, { x: 10, y: 20, w: 200, h: 200 });
const crop = JSON.stringify(d.guideLines);
const ok = rot === '[{"axis":"y","pos":100},{"axis":"x","pos":250}]' && flip === '[{"axis":"y","pos":100},{"axis":"x","pos":50}]' && crop === '[{"axis":"y","pos":80},{"axis":"x","pos":40}]';
return ok || { rot, flip, crop };
})()`);
await check('guides survive a project round trip', `(async () => {
const { saveProject, loadProject } = await import('/js/io/project.js');
const { doc } = await loadProject(await saveProject(atelier.doc));
return JSON.stringify(doc.guideLines) === JSON.stringify(atelier.doc.guideLines) || doc.guideLines;
})()`);
await check('clear guides is undoable', `(() => {
atelier.run('view.clearGuides'); const n = atelier.doc.guideLines.length; atelier.undo();
return n === 0 && atelier.doc.guideLines.length === 2;
})()`);
await page.eval(`atelier.renderer.fit(atelier.doc); 1`);
await sleep(150);
await page.screenshot(`${dir}/features-rulers.png`);
await page.key('r', { code: 'KeyR', modifiers: CTRL });
// ------------------------------------------------------------ command palette
await check('palette fuzzy scoring', `(async () => {
const { fuzzyScore, wordPrefixMatch } = await import('/js/ui/palette.js');
return fuzzyScore('gaus', 'Gaussian Blur') > fuzzyScore('gb', 'Gaussian Blur') && fuzzyScore('gb', 'Gaussian Blur') > 0
&& fuzzyScore('gaus', 'Brightness / Contrast') < 0 && wordPrefixMatch('adj lev', 'Image ▸ Adjustments Levels') && !wordPrefixMatch('gaus', 'Image ▸ Adjustments Levels');
})()`);
await check('Ctrl+K palette runs a command with the keyboard', async () => {
const before = await page.eval(`atelier.doc.layers.length`);
await page.key('k', { code: 'KeyK', modifiers: CTRL });
await sleep(150);
await page.type('new layer');
await sleep(50);
const top = await page.eval(`document.querySelector('.palette-row.active .palette-label')?.textContent`);
await page.key('Enter', { code: 'Enter' });
await sleep(150);
const after = await page.eval(`[atelier.doc.layers.length, !!document.querySelector('.palette')]`);
return (top === 'New Layer' && after[0] === before + 1 && !after[1]) || { top, before, after };
});
await check('palette lists tools and remembers recent commands', async () => {
await page.key('k', { code: 'KeyK', modifiers: CTRL });
await sleep(150);
const first = await page.eval(`document.querySelector('.palette-row .palette-label')?.textContent`);
await page.type('ellip');
await sleep(50);
await page.key('Enter', { code: 'Enter' });
await sleep(100);
const tool = await page.eval(`atelier.tool.id`);
return (first === 'New Layer' && tool === 'ellipse-select') || { first, tool };
});
await check('palette opens dialog commands after closing', async () => {
await page.key('k', { code: 'KeyK', modifiers: CTRL });
await sleep(150);
await page.type('canvas size');
await sleep(50);
await page.key('Enter', { code: 'Enter' });
await sleep(200);
const title = await page.eval(`[...document.querySelectorAll('.dialog-header span')].map(s => s.textContent).join('|')`);
await page.key('Escape', { code: 'Escape' });
await sleep(100);
return title === 'Canvas Size' || title;
});
// ------------------------------------------------------------ preferences
await check('preferences dialog applies settings', async () => {
await page.eval(`setTimeout(() => atelier.run('edit.preferences'), 0); 1`);
await sleep(200);
await page.eval(`(() => {
const sel = [...document.querySelectorAll('.dialog select')][0]; sel.value = 'light'; sel.dispatchEvent(new Event('change'));
const cb = [...document.querySelectorAll('.dialog .check-field')].find(l => l.textContent.includes('wheel')).querySelector('input'); cb.click();
const num = [...document.querySelectorAll('.dialog .range-field')].find(l => l.textContent.includes('History')).querySelector('input.num');
num.value = 25; num.dispatchEvent(new Event('change'));
document.querySelector('.dialog-footer .btn.primary').click(); return 1; })()`);
await sleep(100);
const r = await page.eval(`(async () => { const { historyLimits } = await import('/js/core/history.js'); return [document.documentElement.dataset.theme, atelier.prefs.get('wheelZoom'), historyLimits.entries]; })()`);
await page.eval(`atelier.applyTheme('dark'); atelier.prefs.set('wheelZoom', false); 1`);
return (r[0] === 'light' && r[1] === true && r[2] === 25) || r;
});
// ------------------------------------------------------------ menu keyboard navigation
await check('F10 + arrows + Enter run a menu command', async () => {
await page.eval(`atelier.run('select.none'); 1`);
await page.key('F10', { code: 'F10' });
await sleep(50);
const opened = await page.eval(`document.querySelector('.menu-title.open')?.textContent`);
// Tab steps across menus (ArrowRight would open Image ▸ Adjustments on the way).
// File -> Edit -> Image -> Layer -> Select; first item of Select is "All"
for (let i = 0; i < 4; i++) await page.key('Tab', { code: 'Tab' });
const menu = await page.eval(`document.querySelector('.menu-title.open')?.textContent`);
const focused = await page.eval(`document.querySelector('.menu-item.focused .menu-label')?.textContent`);
await page.key('Enter', { code: 'Enter' });
await sleep(50);
const sel = await page.eval(`!!atelier.doc.selection && !document.querySelector('.menu-popup')`);
return (opened === 'File' && menu === 'Select' && focused === 'All' && sel) || { opened, menu, focused, sel };
});
await check('submenu via ArrowRight, Esc closes one level', async () => {
// park the pointer where no menu opens, or hover can steal the keyboard focus
await page.mouse('mouseMoved', 1430, 890, { button: 'none' });
await page.key('F10', { code: 'F10' });
await page.key('ArrowRight', { code: 'ArrowRight' });
await page.key('ArrowRight', { code: 'ArrowRight' }); // Image menu: first item "Adjustments" has a submenu
await page.key('ArrowRight', { code: 'ArrowRight' });
const sub = await page.eval(`[document.querySelectorAll('.menu-popup').length, document.querySelector('.menu-popup.sub .menu-item.focused .menu-label')?.textContent]`);
await page.key('Escape', { code: 'Escape' });
const afterEsc = await page.eval(`document.querySelectorAll('.menu-popup').length`);
await page.key('Escape', { code: 'Escape' });
const closed = await page.eval(`document.querySelectorAll('.menu-popup').length`);
return (sub[0] === 2 && sub[1] === 'Brightness / Contrast…' && afterEsc === 1 && closed === 0) || { sub, afterEsc, closed };
});
// ------------------------------------------------------------ symmetry painting
await page.eval(`atelier.newDocument({ width: 400, height: 300, background: null }); atelier.renderer.setZoom(1); 1`);
await sleep(100);
await check('mirror brush paints both halves in one undo step', async () => {
await page.eval(`atelier.setTool('brush'); Object.assign(atelier.tool.options, { size: 10, hardness: 100, symmetry: 'x', smoothing: 0 }); atelier.setColor('fg', '#ff0000'); 1`);
await page.drag([await screen(50, 100), await screen(80, 100), await screen(100, 100)]);
const r = await page.eval(`(() => { const c = atelier.doc.activeLayer.canvas; return [T.px(c, 80, 100)[3], T.px(c, 320, 100)[3], T.px(c, 200, 100)[3], atelier.doc.history.entries.length]; })()`);
return (r[0] === 255 && r[1] === 255 && r[2] === 0 && r[3] === 1) || r;
});
await check('radial pencil and 4-way mirror points', `(async () => {
const { mirrorPoints } = await import('/js/tools/paint.js');
const four = mirrorPoints({ x: 10, y: 20 }, 'xy', 100, 100).map(p => p.x + ',' + p.y).join(' ');
const six = mirrorPoints({ x: 50, y: 0 }, 'r6', 100, 100);
const ok6 = six.length === 6 && six.every(p => Math.abs(Math.hypot(p.x - 50, p.y - 50) - 50) < 1e-9);
atelier.setTool('pencil'); Object.assign(atelier.tool.options, { size: 1, symmetry: 'r8' });
const t = atelier.tool;
t.onPointerDown({ altKey: false, shiftKey: false, pointerType: 'mouse' }, { x: 200.5, y: 50.5 }); t.onPointerUp({}, { x: 200.5, y: 50.5 });
const c = atelier.doc.activeLayer.canvas;
// rotated copies land on (200, 250) and (300, 150), give or take a pixel of rounding
const near = (x, y) => [-1, 0, 1].some(i => [-1, 0, 1].some(j => T.px(c, x + i, y + j)[3] === 255));
return (four === '10,20 90,20 10,80 90,80' && ok6 && near(200, 250) && near(300, 150) && near(129, 79)) || { four, ok6 };
})()`);
await check('brush "More" popover holds extra options', async () => {
await page.eval(`atelier.setTool('brush'); 1`);
const r = await page.eval(`(() => {
const btn = document.querySelector('.options-bar .more-btn'); btn.click();
const pop = document.querySelector('.options-popover');
const labels = pop ? [...pop.querySelectorAll('.field')].map(f => f.textContent.trim()) : [];
const inBar = [...document.querySelectorAll('.options-bar .field-label')].map(l => l.textContent);
document.body.dispatchEvent(new PointerEvent('pointerdown', { bubbles: true }));
return { labels, inBar, closed: !document.querySelector('.options-popover'), fits: document.querySelector('.options-bar').scrollWidth <= document.querySelector('.options-bar').clientWidth };
})()`);
return (r.labels.some((l) => l.startsWith('Spacing')) && !r.inBar.includes('Spacing') && r.inBar.includes('Mirror') && r.closed && r.fits) || r;
});
await check('recent colors record painted colors', `(() => {
const recent = [...document.querySelectorAll('.recent-swatches .swatch')].map(s => s.style.getPropertyValue('--c'));
return recent[0] === '#ff0000' || recent;
})()`);
// ------------------------------------------------------------ align, border, smooth
await check('align layer to canvas and to selection', `(async () => {
const ops = await import('/js/core/ops.js');
const { Selection, shapeMask } = await import('/js/core/selection.js');
const d = atelier.doc; const l = ops.addLayer(d);
l.ctx.fillStyle = '#00f'; l.ctx.fillRect(100, 100, 20, 10); l.touch();
atelier.run('layer.align-right');
const b1 = JSON.stringify(ops.layerBounds(l));
d.setSelection(Selection.orNull(shapeMask(400, 300, { type: 'rect', x: 50, y: 60, w: 100, h: 100 })));
atelier.run('layer.align-vcenter'); atelier.run('layer.align-left');
const b2 = JSON.stringify(ops.layerBounds(l));
const label = d.history.entries.at(-1).label;
atelier.undo(); atelier.undo();
const b3 = JSON.stringify(ops.layerBounds(l));
return (b1 === '{"x":380,"y":100,"w":20,"h":10}' && b2 === '{"x":50,"y":105,"w":20,"h":10}' && label === 'Align layer' && b3 === b1) || { b1, b2, b3, label };
})()`);
await check('align moves text layers by their box', `(async () => {
const ops = await import('/js/core/ops.js');
const { Layer } = await import('/js/core/layer.js');
const d = atelier.doc; d.setSelection(null);
const t = new Layer(d.width, d.height, { type: 'text' }); t.text.content = 'Hi'; t.text.x = 30; t.text.y = 40; t.renderText();
ops.addLayer(d, t);
atelier.run('layer.align-bottom');
const b = ops.layerBounds(t);
return (t.isText && b.y + b.h === 300 && t.text.x === 30) || b;
})()`);
await check('select border and smooth', `(async () => {
const { Selection, shapeMask } = await import('/js/core/selection.js');
const d = atelier.doc;
const sel = Selection.orNull(shapeMask(400, 300, { type: 'rect', x: 100, y: 100, w: 100, h: 100 }));
const ring = sel.border(10);
const inRing = ring.contains(100, 150), centre = ring.contains(150, 150), outside = ring.contains(90, 150);
const smooth = sel.smoothed(20);
const corner = smooth.contains(101, 101), mid = smooth.contains(150, 101);
return (inRing && !centre && !outside && !corner && mid) || { inRing, centre, outside, corner, mid };
})()`);
// ------------------------------------------------------------ dodge / burn / sponge
await page.eval(`atelier.newDocument({ width: 300, height: 200, background: '#808080' }); atelier.renderer.setZoom(1); 1`);
await sleep(100);
await check('dodge lightens, burn (Alt) darkens, one undo step each', async () => {
await page.eval(`atelier.setTool('dodge'); Object.assign(atelier.tool.options, { tone: 'dodge', range: 'midtones', exposure: 50, size: 30, hardness: 100, flow: 100 }); 1`);
await page.drag([await screen(50, 50), await screen(70, 50), await screen(90, 50)]);
await page.drag([await screen(50, 150), await screen(70, 150), await screen(90, 150)], { modifiers: 1 });
const r = await page.eval(`(() => { const c = atelier.doc.activeLayer.canvas; return { lit: T.px(c, 70, 50), dark: T.px(c, 70, 150), off: T.px(c, 200, 100), labels: atelier.doc.history.entries.map(e => e.label).join() }; })()`);
return (r.lit[0] > 170 && r.dark[0] < 80 && r.off[0] === 128 && r.lit[3] === 255 && r.labels === 'Dodge,Burn') || r;
});
await check('sponge desaturates and keeps transparency', `(async () => {
const { tonedCanvas } = await import('/js/tools/retouch.js');
const c = document.createElement('canvas'); c.width = 2; c.height = 1;
const ctx = c.getContext('2d'); ctx.fillStyle = '#ff0000'; ctx.fillRect(0, 0, 1, 1);
const out = tonedCanvas(c, 'desaturate', 'midtones', 1);
const a = T.px(out, 0, 0), b = T.px(out, 1, 0);
return (a[0] === a[1] && a[1] === a[2] && b[3] === 0) || { a, b };
})()`);
// ------------------------------------------------------------ touch gestures + eyedropper
await check('two-finger pinch zooms around the fingers and cancels the stroke', async () => {
await page.eval(`atelier.setTool('brush'); atelier.renderer.setZoom(1); 1`);
const n0 = await page.eval(`atelier.doc.history.entries.length`);
const [cx, cy] = await screen(150, 100);
const touch = (type, pts) => page.send('Input.dispatchTouchEvent', { type, touchPoints: pts.map(([x, y], id) => ({ x, y, id })) });
await touch('touchStart', [[cx - 40, cy]]);
await touch('touchMove', [[cx - 45, cy]]);
await touch('touchStart', [[cx - 45, cy], [cx + 40, cy]]);
await touch('touchMove', [[cx - 85, cy], [cx + 80, cy]]);
await touch('touchEnd', []);
await sleep(100);
const r = await page.eval(`(() => { const v = atelier.doc.view; const d = atelier.renderer.screenToDoc(atelier.renderer.clientToScreen({ clientX: ${cx}, clientY: ${cy} })); return { z: v.zoom, d: [d.x, d.y], n: atelier.doc.history.entries.length, stroke: !!atelier.doc.stroke }; })()`);
// fingers spread from 85 px to 165 px apart; the point between them stays put
return (Math.abs(r.z - 165 / 85) < 0.05 && Math.abs(r.d[0] - 150) < 3 && Math.abs(r.d[1] - 100) < 3 && r.n === n0 && !r.stroke) || { ...r, n0 };
});
await check('eyedropper shows its ring while sampling and Esc restores the color', async () => {
await page.eval(`atelier.renderer.setZoom(1); atelier.setTool('eyedropper'); atelier.setColor('fg', '#123456'); 1`);
const [x, y] = await screen(150, 100);
await page.mouse('mouseMoved', x, y);
await page.mouse('mousePressed', x, y);
const during = await page.eval(`[atelier.tool.down, atelier.colors.fg.toHex()]`);
await page.key('Escape', { code: 'Escape' });
await page.mouse('mouseReleased', x, y);
const after = await page.eval(`atelier.colors.fg.toHex()`);
return (during[0] === true && during[1] === '#808080' && after === '#123456') || { during, after };
});
// ------------------------------------------------------------ dithering
await page.eval(`window.D = { grad(w = 128, h = 16) {
const id = new ImageData(w, h);
for (let y = 0; y < h; y++) for (let x = 0; x < w; x++) { const i = (y * w + x) * 4; id.data[i] = id.data[i + 1] = id.data[i + 2] = Math.round(x * 255 / (w - 1)); id.data[i + 3] = 255; }
return id; },
colors(id) { const s = new Set(); for (let i = 0; i < id.data.length; i += 4) s.add(id.data.slice(i, i + 4).join()); return s; },
mean(id) { let s = 0; for (let i = 0; i < id.data.length; i += 4) s += id.data[i]; return s / (id.data.length / 4); },
}; 1`);
await check('every built-in dither algorithm gives pure black/white and keeps the mean tone', `(async () => {
const M = await import('/js/imaging/dither.js');
const base = Object.fromEntries(M.ditherParams().map((f) => [f.key, f.default]));
const bad = [];
for (const a of M.ALGORITHMS) {
const out = M.applyDither(D.grad(), { ...base, algorithm: a.id }, {});
const cols = [...D.colors(out)].sort().join('|');
if (cols !== '0,0,0,255|255,255,255,255' || Math.abs(D.mean(out) - 127.5) > 4) bad.push({ id: a.id, cols, mean: D.mean(out) });
}
return bad.length === 0 || bad;
})()`);
await check('dither RGB levels per channel, FG/BG ink, hard alpha', `(async () => {
const M = await import('/js/imaging/dither.js');
const base = Object.fromEntries(M.ditherParams().map((f) => [f.key, f.default]));
const src = new ImageData(64, 8);
for (let i = 0; i < src.data.length; i += 4) { const x = (i / 4) % 64; src.data.set([x * 4, 255 - x * 4, 128, x < 32 ? 100 : 200], i); }
const rgb = M.applyDither(new ImageData(new Uint8ClampedArray(src.data), 64, 8), { ...base, mode: 'rgb', rLevels: 3, gLevels: 2, bLevels: 5, algorithm: 'bayer4', hardAlpha: true }, {});
const ok = (v, n) => [...Array(n).keys()].map((k) => Math.round(k * 255 / (n - 1))).includes(v);
let badRgb = 0, clear = 0;
for (let i = 0; i < rgb.data.length; i += 4) {
if (rgb.data[i + 3] === 0) { clear++; continue; } // hard alpha: x < 32 went transparent
if (!ok(rgb.data[i], 3) || !ok(rgb.data[i + 1], 2) || !ok(rgb.data[i + 2], 5) || rgb.data[i + 3] !== 255) badRgb++;
}
if (clear !== 256) badRgb += 1000;
const duo = M.applyDither(D.grad(), { ...base, ink: 'fgbg', levels: 3 }, { fg: { r: 200, g: 0, b: 0 }, bg: { r: 0, g: 0, b: 255 } });
const duoCols = [...D.colors(duo)].sort().join('|');
return (badRgb === 0 && duoCols === '0,0,255,255|100,0,128,255|200,0,0,255') || { badRgb, duoCols };
})()`);
await check('dither scripts run in a worker; errors and stray values are handled', `(async () => {
const M = await import('/js/imaging/dither.js');
const base = Object.fromEntries(M.ditherParams().map((f) => [f.key, f.default]));
M.setUserScripts([
{ id: 'k', name: 'kernel', source: 'api.diffuse([[1, 0, 1]], { divisor: 1 })' },
{ id: 'raw', name: 'raw', source: 'for (let i = 0; i < img.data.length; i++) img.data[i] = i % 3 ? 0.7 : NaN;' },
{ id: 'oops', name: 'oops', source: 'let a = 1;\\nundefinedThing();' },
{ id: 'loop', name: 'loop', source: 'for (;;) {}' },
]);
const k = await M.applyDither(D.grad(), { ...base, algorithm: 'script:k' }, {});
const raw = await M.applyDither(D.grad(8, 1), { ...base, algorithm: 'script:raw' }, {});
const err = await M.applyDither(D.grad(), { ...base, algorithm: 'script:oops' }, {}).catch((e) => e.message);
const s = M.prepare(D.grad(), base);
const t0 = performance.now();
const loop = await M.runScript('for (;;) {}', s, base, {}, { timeout: 300 }).catch((e) => e.message);
const stopped = performance.now() - t0 < 3000 && /stopped/.test(loop);
// a newer run supersedes an older one
const first = M.runScript('for (;;) {}', M.prepare(D.grad(), base), base, {}).catch((e) => e.superseded);
await M.runScript('', M.prepare(D.grad(), base), base, {});
const superseded = await first;
M.setUserScripts([]);
return (D.colors(k).size === 2 && [...D.colors(raw)].join('|') === '0,0,0,255|255,255,255,255'
&& /ReferenceError.*\\(line 2\\)/.test(err) && stopped && superseded === true) || { k: D.colors(k).size, raw: [...D.colors(raw)], err, loop, superseded };
})()`);
await check('Dither dialog previews, commits inside the selection, and undoes', `(async () => {
atelier.newDocument({ width: 100, height: 60, background: null });
const d = atelier.doc, l = d.activeLayer;
const g = l.ctx.createLinearGradient(0, 0, 100, 0); g.addColorStop(0, '#000'); g.addColorStop(1, '#fff');
l.ctx.fillStyle = g; l.ctx.fillRect(0, 0, 100, 60); l.touch();
const before = T.px(l.canvas, 70, 30).join();
const { Selection, shapeMask } = await import('/js/core/selection.js');
d.setSelection(Selection.orNull(shapeMask(100, 60, { type: 'rect', x: 0, y: 0, w: 50, h: 60 }, false)));
localStorage.removeItem('atelier.filter.dither');
setTimeout(() => atelier.run('filter.dither'), 0);
await new Promise((r) => setTimeout(r, 400));
const previewing = !!l.preview;
[...document.querySelectorAll('.dialog button')].find((b) => b.textContent === 'OK').click();
await new Promise((r) => setTimeout(r, 300));
const left = new Set(), data = l.canvas.getContext('2d').getImageData(0, 0, 50, 60).data;
for (let i = 0; i < data.length; i += 4) left.add(data[i]);
const right = T.px(l.canvas, 70, 30).join();
d.history.undo();
const undone = T.px(l.canvas, 20, 30).join() === T.px(l.canvas, 20, 31).join() && T.px(l.canvas, 20, 30)[0] > 0;
return (previewing && !l.preview && [...left].sort().join() === '0,255' && right === before && undone) || { previewing, left: [...left], right, before, undone };
})()`);
await check('dialogs drag by the header, stop dimming, ignore backdrop clicks, reopen where left', async () => {
await page.eval(`(() => { atelier.newDocument({ width: 200, height: 120, background: '#808080' }); localStorage.removeItem('atelier.filter.dither'); setTimeout(() => atelier.run('filter.posterize'), 0); return 1; })()`);
await sleep(300);
const h = await page.eval(`(() => { const r = document.querySelector('.dialog-header span').getBoundingClientRect(); return [r.left + 10, r.top + 5]; })()`);
await page.drag([h, [h[0] + 50, h[1] + 20], [h[0] + 300, h[1] + 200]]);
const moved = await page.eval(`(() => { const b = document.querySelector('.dialog-backdrop'), r = b.firstChild.getBoundingClientRect(); return { moved: b.classList.contains('moved'), bg: getComputedStyle(b).backgroundColor, left: Math.round(r.left), top: Math.round(r.top) }; })()`);
await page.click(20, 300); // the backdrop, away from the dialog
const stillOpen = await page.eval(`!!document.querySelector('.dialog')`);
await page.key('Escape', { code: 'Escape' });
await sleep(100);
await page.eval(`(setTimeout(() => atelier.run('filter.posterize'), 0), 1)`);
await sleep(300);
const reopened = await page.eval(`(() => { const r = document.querySelector('.dialog').getBoundingClientRect(); return [Math.round(r.left), Math.round(r.top)]; })()`);
await page.key('Escape', { code: 'Escape' });
await sleep(100);
return (moved.moved && moved.bg === 'rgba(0, 0, 0, 0)' && stillOpen && reopened.join() === `${moved.left},${moved.top}`) || { moved, stillOpen, reopened };
});
// ------------------------------------------------------------ regressions
await check('filters on a transparency-locked layer keep alpha exactly', `(async () => {
atelier.newDocument({ width: 60, height: 60, background: null });
const l = atelier.doc.activeLayer;
l.ctx.fillStyle = 'rgba(255,0,0,0.5)'; l.ctx.fillRect(0, 0, 60, 60); l.touch(); l.alphaLock = true;
atelier.run('filter.invert');
await new Promise((r) => setTimeout(r, 100));
const p = T.px(l.canvas, 30, 30);
return (p[0] < 3 && p[1] > 252 && p[2] > 252 && Math.abs(p[3] - 128) <= 1) || p;
})()`);
await check('move tool click inside a selection adds no history step', `(async () => {
atelier.newDocument({ width: 100, height: 100 });
const d = atelier.doc; const { Selection } = await import('/js/core/selection.js');
d.setSelection(Selection.all(100, 100));
const n = d.history.index;
atelier.setTool('move');
atelier.tool.onPointerDown({}, { x: 50, y: 50 }); atelier.tool.onPointerUp({}, { x: 50, y: 50 });
return (d.history.index === n && !!d.selection && !d.floating) || d.history.entries.map((e) => e.label);
})()`);
await check('cancelled smudge on a mask refreshes the mask', `(async () => {
atelier.newDocument({ width: 100, height: 100 });
const d = atelier.doc; const ops = await import('/js/core/ops.js');
ops.addMask(d);
const l = d.activeLayer;
const m = l.mask.getContext('2d'); m.fillStyle = '#000'; m.fillRect(0, 0, 50, 100); l.touchMask();
atelier.setTool('smudge'); const t = atelier.tool;
t.options.size = 40; t.options.strength = 100;
t.onPointerDown({}, { x: 45, y: 50 }); for (let x = 46; x < 80; x += 2) t.onPointerMove({}, { x, y: 50 });
t.cancel();
return T.px(l.maskAlpha(), 70, 50)[3] === 255 || T.px(l.maskAlpha(), 70, 50);
})()`);
await check('rotating a layer mask keeps it opaque', `(async () => {
atelier.newDocument({ width: 200, height: 100 });
const d = atelier.doc; const ops = await import('/js/core/ops.js');
ops.addMask(d);
ops.transformLayer(d, 'rotate-cw');
return T.px(d.activeLayer.mask, 5, 50).join() === '255,255,255,255' || T.px(d.activeLayer.mask, 5, 50);
})()`);
await check('text layers follow crop, image size and canvas size', `(async () => {
atelier.newDocument({ width: 400, height: 300 });
const d = atelier.doc; const ops = await import('/js/core/ops.js'); const { Layer } = await import('/js/core/layer.js');
const t = new Layer(400, 300, { type: 'text' }); t.text.content = 'Hi'; t.renderText();
ops.addLayer(d, t);
const sizes = [];
ops.cropTo(d, { x: 0, y: 0, w: 200, h: 150 }); sizes.push([t.canvas.width, t.canvas.height].join());
ops.resizeImage(d, 600, 450); sizes.push([t.canvas.width, t.canvas.height].join());
ops.resizeCanvas(d, 700, 500); sizes.push([t.canvas.width, t.canvas.height].join());
return sizes.join(' ') === '200,150 600,450 700,500' || sizes;
})()`);
await check('merge visible / flatten match the screen with a hidden clipping base', `(async () => {
atelier.newDocument({ width: 50, height: 50 });
const d = atelier.doc; const ops = await import('/js/core/ops.js'); const { Layer } = await import('/js/core/layer.js');
const { flattenToCanvas } = await import('/js/render/compositor.js');
const base = new Layer(50, 50); base.ctx.fillStyle = '#00f'; base.ctx.fillRect(0, 0, 50, 50); base.visible = false;
const clipped = new Layer(50, 50); clipped.ctx.fillStyle = '#f00'; clipped.ctx.fillRect(0, 0, 50, 50); clipped.clip = true;
d.layers.push(base, clipped); d.markDirty(true);
ops.mergeVisible(d);
const merged = T.px(flattenToCanvas(d), 25, 25).join();
d.history.undo();
ops.flatten(d);
const flat = T.px(flattenToCanvas(d), 25, 25).join();
return (merged === '255,255,255,255' && flat === '255,255,255,255') || { merged, flat };
})()`);
await check('Escape closes only the topmost of two dialogs', `(async () => {
const dl = await import('/js/ui/dialogs.js');
let outer = 'open', inner = 'open';
dl.alertDialog('outer').then(() => { outer = 'closed'; });
dl.alertDialog('inner').then(() => { inner = 'closed'; });
await new Promise((r) => setTimeout(r, 20));
document.dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape', bubbles: true }));
await new Promise((r) => setTimeout(r, 20));
const res = [outer, inner];
document.dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape', bubbles: true }));
await new Promise((r) => setTimeout(r, 20));
return (res.join() === 'open,closed' && outer === 'closed' && !dl.isDialogOpen()) || { res, outer };
})()`);
console.log('--- console ---\n' + (page.logs.filter((l) => !l.includes('willReadFrequently')).join('\n') || '(none)'));
} finally {
await b.close();
}
console.log(fails ? `${fails} FAILURES` : 'ALL PASSED');
process.exit(fails ? 1 : 0);
})().catch((e) => { console.error(e); process.exit(1); });
|