diff --git a/src/commands/rpoplpush.js b/src/commands/rpoplpush.js index 33e98a42..5e59239f 100644 --- a/src/commands/rpoplpush.js +++ b/src/commands/rpoplpush.js @@ -22,11 +22,18 @@ export function rpoplpush(source, destination) { const newSource = this.data.get(source) const item = newSource.pop() - const newDest = this.data.get(destination) + let newDest = newSource // Operate on the same list + if (source !== destination) { + // Operate on two different lists + newDest = this.data.get(destination) + } + newDest.unshift(item) this.data.set(source, newSource) - this.data.set(destination, newDest) + if (newSource !== newDest) { + this.data.set(destination, newDest) + } return item } diff --git a/test/integration/commands/rpoplpush.js b/test/integration/commands/rpoplpush.js index 4f4aecb4..569f5f56 100644 --- a/test/integration/commands/rpoplpush.js +++ b/test/integration/commands/rpoplpush.js @@ -128,5 +128,23 @@ runTwinSuite('rpoplpush', (command, equals) => { ) } ) + + // @TODO Rewrite test so it runs on a real Redis instance + ;(process.env.IS_E2E ? it.skip : it)( + 'should rotate the list if the source and destination are the same', + async () => { + const redis = new Redis({ + data: { + foo: ['1', '2'], + }, + }) + + const result = await redis[command]('foo', 'foo', 'LEFT', 'RIGHT'); + expect(result).toBe('2'); + + const list = await redis.lrange('foo', 0, -1) + expect(list).toEqual(['2', '1']) + } + ) }) })