diff --git a/lib/internal/repl/history.js b/lib/internal/repl/history.js index e95056ed5c466b..b197c7049cc089 100644 --- a/lib/internal/repl/history.js +++ b/lib/internal/repl/history.js @@ -142,7 +142,7 @@ class ReplHistory { if (this[kHistory].length === 0 || this[kHistory][0] !== normalizedLine) { if (this[kRemoveHistoryDuplicates]) { // Remove older history line if identical to new one - const dupIndex = ArrayPrototypeIndexOf(this[kHistory], line); + const dupIndex = ArrayPrototypeIndexOf(this[kHistory], normalizedLine); if (dupIndex !== -1) ArrayPrototypeSplice(this[kHistory], dupIndex, 1); } diff --git a/test/parallel/test-repl-history-dedup-multiline.js b/test/parallel/test-repl-history-dedup-multiline.js new file mode 100644 index 00000000000000..03dc089fa6144d --- /dev/null +++ b/test/parallel/test-repl-history-dedup-multiline.js @@ -0,0 +1,44 @@ +'use strict'; + +const common = require('../common'); + +if (process.env.TERM === 'dumb') { + common.skip('skipping - dumb terminal'); +} + +const assert = require('assert'); +const readline = require('readline'); +const { EventEmitter } = require('events'); + +class FakeInput extends EventEmitter { + resume() {} + pause() {} + write() {} + end() {} +} +FakeInput.prototype.readable = true; + +{ + const fi = new FakeInput(); + const rli = new readline.Interface({ + input: fi, + output: fi, + terminal: true, + removeHistoryDuplicates: true, + }); + + function submitLine(line) { + rli.line = line; + fi.emit('keypress', '', { name: 'enter' }); + } + + submitLine('line1\nline2'); + submitLine('other'); + submitLine('line1\nline2'); + + assert.strictEqual(rli.history.length, 2); + assert.strictEqual(rli.history[0], 'line2\rline1'); + assert.strictEqual(rli.history[1], 'other'); + + rli.close(); +}