1const assert = require('assert');
2const { createPixels, floodFill, UndoStack, clonePixels } = require('./editor.js');
3
4function test(name, fn) {
5 try { fn(); console.log('✓', name); }
6 catch (err) { console.error('✗', name); throw err; }
7}
8
9test('floodFill fills a contiguous empty region only', () => {
10 const p = createPixels(4);
11 p[0][2] = '#000000'; p[1][2] = '#000000'; p[2][2] = '#000000'; p[3][2] = '#000000';
12 const result = floodFill(p, 0, 0, '#ff0000', 4);
13 assert.equal(result.changed, true);
14 assert.equal(result.count, 8);
15 assert.equal(result.pixels[0][0], '#ff0000');
16 assert.equal(result.pixels[3][1], '#ff0000');
17 assert.equal(result.pixels[0][3], null);
18 assert.equal(result.pixels[0][2], '#000000');
19 assert.equal(p[0][0], null, 'original array must not be mutated');
20});
21
22test('floodFill reports unchanged when replacement equals target', () => {
23 const p = createPixels(3, '#123456');
24 const result = floodFill(p, 1, 1, '#123456', 3);
25 assert.equal(result.changed, false);
26 assert.equal(result.count, 0);
27 assert.deepEqual(result.pixels, p);
28});
29
30test('UndoStack undo/redo traverses snapshots and drops redo after branch', () => {
31 const a = createPixels(2);
32 const history = new UndoStack(a);
33 const b = clonePixels(a); b[0][0] = '#111111'; history.push(b);
34 const c = clonePixels(b); c[1][1] = '#222222'; history.push(c);
35
36 assert.equal(history.current()[1][1], '#222222');
37 assert.equal(history.undo()[1][1], null);
38 assert.equal(history.undo()[0][0], null);
39 assert.equal(history.redo()[0][0], '#111111');
40
41 const d = history.current(); d[0][1] = '#333333'; history.push(d);
42 assert.equal(history.canRedo(), false);
43 assert.equal(history.current()[0][1], '#333333');
44});
45
46test('UndoStack snapshots are immutable from caller mutation', () => {
47 const a = createPixels(2);
48 const history = new UndoStack(a);
49 a[0][0] = '#ffffff';
50 assert.equal(history.current()[0][0], null);
51 const cur = history.current(); cur[0][0] = '#abcdef';
52 assert.equal(history.current()[0][0], null);
53});
54
55console.log('All tests passed');
56
Discussion
No comments yet. Start the discussion. Recorded by @patrick-toulme.