|
| 1 | +import test from 'ava'; |
| 2 | +import ansiEscapes from 'ansi-escapes'; |
| 3 | +import logUpdate from '../src/log-update.js'; |
| 4 | +import createStdout from './helpers/create-stdout.js'; |
| 5 | + |
| 6 | +test('renders and updates output', t => { |
| 7 | + const stdout = createStdout(); |
| 8 | + const render = logUpdate.create(stdout); |
| 9 | + |
| 10 | + render('Hello'); |
| 11 | + t.is((stdout.write as any).callCount, 1); |
| 12 | + t.is((stdout.write as any).firstCall.args[0], 'Hello\n'); |
| 13 | + |
| 14 | + render('World'); |
| 15 | + t.is((stdout.write as any).callCount, 2); |
| 16 | + t.true( |
| 17 | + ((stdout.write as any).secondCall.args[0] as string).includes('World'), |
| 18 | + ); |
| 19 | +}); |
| 20 | + |
| 21 | +test('skips identical output', t => { |
| 22 | + const stdout = createStdout(); |
| 23 | + const render = logUpdate.create(stdout); |
| 24 | + |
| 25 | + render('Hello'); |
| 26 | + render('Hello'); |
| 27 | + |
| 28 | + t.is((stdout.write as any).callCount, 1); |
| 29 | +}); |
| 30 | + |
| 31 | +test('incremental render (surgical updates)', t => { |
| 32 | + const stdout = createStdout(); |
| 33 | + const render = logUpdate.create(stdout); |
| 34 | + |
| 35 | + render('Line 1\nLine 2\nLine 3'); |
| 36 | + render('Line 1\nUpdated\nLine 3'); |
| 37 | + |
| 38 | + const secondCall = (stdout.write as any).secondCall.args[0] as string; |
| 39 | + t.true(secondCall.includes(ansiEscapes.cursorNextLine)); // Skips unchanged lines |
| 40 | + t.true(secondCall.includes('Updated')); // Only updates changed line |
| 41 | + t.false(secondCall.includes('Line 1')); // Doesn't rewrite unchanged |
| 42 | + t.false(secondCall.includes('Line 3')); // Doesn't rewrite unchanged |
| 43 | +}); |
| 44 | + |
| 45 | +test('clears extra lines when output shrinks', t => { |
| 46 | + const stdout = createStdout(); |
| 47 | + const render = logUpdate.create(stdout); |
| 48 | + |
| 49 | + render('Line 1\nLine 2\nLine 3'); |
| 50 | + render('Line 1'); |
| 51 | + |
| 52 | + const secondCall = (stdout.write as any).secondCall.args[0] as string; |
| 53 | + t.true(secondCall.includes(ansiEscapes.eraseLines(2))); // Erases 2 extra lines |
| 54 | +}); |
| 55 | + |
| 56 | +test('incremental render when output grows', t => { |
| 57 | + const stdout = createStdout(); |
| 58 | + const render = logUpdate.create(stdout); |
| 59 | + |
| 60 | + render('Line 1'); |
| 61 | + render('Line 1\nLine 2\nLine 3'); |
| 62 | + |
| 63 | + const secondCall = (stdout.write as any).secondCall.args[0] as string; |
| 64 | + t.true(secondCall.includes(ansiEscapes.cursorNextLine)); // Skips unchanged first line |
| 65 | + t.true(secondCall.includes('Line 2')); // Adds new line |
| 66 | + t.true(secondCall.includes('Line 3')); // Adds new line |
| 67 | + t.false(secondCall.includes('Line 1')); // Doesn't rewrite unchanged |
| 68 | +}); |
| 69 | + |
| 70 | +test('single write call with multiple surgical updates', t => { |
| 71 | + const stdout = createStdout(); |
| 72 | + const render = logUpdate.create(stdout); |
| 73 | + |
| 74 | + render( |
| 75 | + 'Line 1\nLine 2\nLine 3\nLine 4\nLine 5\nLine 6\nLine 7\nLine 8\nLine 9\nLine 10', |
| 76 | + ); |
| 77 | + render( |
| 78 | + 'Line 1\nUpdated 2\nLine 3\nUpdated 4\nLine 5\nUpdated 6\nLine 7\nUpdated 8\nLine 9\nUpdated 10', |
| 79 | + ); |
| 80 | + |
| 81 | + t.is((stdout.write as any).callCount, 2); // Only 2 writes total (initial + update) |
| 82 | +}); |
0 commit comments