|
| 1 | +import {test} from 'node:test'; |
| 2 | +import assert from 'node:assert/strict'; |
| 3 | +import PriorityQueue from '../source/priority-queue.js'; |
| 4 | + |
| 5 | +const createRun = (value: string) => async () => value; |
| 6 | + |
| 7 | +test('PriorityQueue ignores dequeued items in queue operations', () => { |
| 8 | + const queue = new PriorityQueue(); |
| 9 | + const first = createRun('first'); |
| 10 | + const second = createRun('second'); |
| 11 | + const third = createRun('third'); |
| 12 | + |
| 13 | + queue.enqueue(first, {id: 'same', priority: 0}); |
| 14 | + queue.enqueue(second, {id: 'same', priority: 0}); |
| 15 | + queue.enqueue(third, {id: 'third', priority: 0}); |
| 16 | + |
| 17 | + assert.equal(queue.dequeue(), first); |
| 18 | + assert.deepEqual(queue.filter({priority: 0}), [second, third]); |
| 19 | + |
| 20 | + queue.setPriority('same', 2); |
| 21 | + queue.remove(third); |
| 22 | + |
| 23 | + assert.equal(queue.size, 1); |
| 24 | + assert.equal(queue.dequeue(), second); |
| 25 | + assert.equal(queue.dequeue(), undefined); |
| 26 | +}); |
| 27 | + |
| 28 | +test('PriorityQueue inserts high-priority items correctly after partial dequeue', () => { |
| 29 | + const queue = new PriorityQueue(); |
| 30 | + const first = createRun('first'); |
| 31 | + const second = createRun('second'); |
| 32 | + const third = createRun('third'); |
| 33 | + const urgent = createRun('urgent'); |
| 34 | + |
| 35 | + queue.enqueue(first, {priority: 0}); |
| 36 | + queue.enqueue(second, {priority: 0}); |
| 37 | + queue.enqueue(third, {priority: -1}); |
| 38 | + |
| 39 | + assert.equal(queue.dequeue(), first); |
| 40 | + |
| 41 | + queue.enqueue(urgent, {priority: 1}); |
| 42 | + |
| 43 | + assert.equal(queue.size, 3); |
| 44 | + assert.equal(queue.dequeue(), urgent); |
| 45 | + assert.equal(queue.dequeue(), second); |
| 46 | + assert.equal(queue.dequeue(), third); |
| 47 | + assert.equal(queue.dequeue(), undefined); |
| 48 | +}); |
| 49 | + |
| 50 | +test('PriorityQueue removes the live duplicate id after partial dequeue', () => { |
| 51 | + const queue = new PriorityQueue(); |
| 52 | + const first = createRun('first'); |
| 53 | + const second = createRun('second'); |
| 54 | + const third = createRun('third'); |
| 55 | + |
| 56 | + queue.enqueue(first, {id: 'same'}); |
| 57 | + queue.enqueue(second, {id: 'same'}); |
| 58 | + queue.enqueue(third, {id: 'third'}); |
| 59 | + |
| 60 | + assert.equal(queue.dequeue(), first); |
| 61 | + |
| 62 | + queue.remove('same'); |
| 63 | + |
| 64 | + assert.equal(queue.size, 1); |
| 65 | + assert.equal(queue.dequeue(), third); |
| 66 | + assert.equal(queue.dequeue(), undefined); |
| 67 | +}); |
| 68 | + |
| 69 | +test('PriorityQueue removes by run after partial dequeue', () => { |
| 70 | + const queue = new PriorityQueue(); |
| 71 | + const first = createRun('first'); |
| 72 | + const second = createRun('second'); |
| 73 | + const third = createRun('third'); |
| 74 | + |
| 75 | + queue.enqueue(first); |
| 76 | + queue.enqueue(second); |
| 77 | + queue.enqueue(third); |
| 78 | + |
| 79 | + assert.equal(queue.dequeue(), first); |
| 80 | + |
| 81 | + queue.remove(second); |
| 82 | + |
| 83 | + assert.equal(queue.size, 1); |
| 84 | + assert.equal(queue.dequeue(), third); |
| 85 | + assert.equal(queue.dequeue(), undefined); |
| 86 | +}); |
| 87 | + |
| 88 | +test('PriorityQueue stays ordered after cursor compaction', () => { |
| 89 | + const queue = new PriorityQueue(); |
| 90 | + const urgent = createRun('urgent'); |
| 91 | + |
| 92 | + for (let index = 0; index < 150; index++) { |
| 93 | + queue.enqueue(createRun(`task-${index}`)); |
| 94 | + } |
| 95 | + |
| 96 | + // Cross the compaction threshold while leaving queued items behind. |
| 97 | + for (let index = 0; index < 120; index++) { |
| 98 | + assert.notEqual(queue.dequeue(), undefined); |
| 99 | + } |
| 100 | + |
| 101 | + queue.enqueue(urgent, {priority: 1}); |
| 102 | + |
| 103 | + assert.equal(queue.size, 31); |
| 104 | + assert.equal(queue.dequeue(), urgent); |
| 105 | + |
| 106 | + let remaining = 0; |
| 107 | + while (queue.dequeue() !== undefined) { |
| 108 | + remaining++; |
| 109 | + } |
| 110 | + |
| 111 | + assert.equal(remaining, 30); |
| 112 | + assert.equal(queue.size, 0); |
| 113 | +}); |
| 114 | + |
| 115 | +test('PriorityQueue setPriority works when only one live item remains', () => { |
| 116 | + const queue = new PriorityQueue(); |
| 117 | + const first = createRun('first'); |
| 118 | + const second = createRun('second'); |
| 119 | + |
| 120 | + queue.enqueue(first, {id: 'first'}); |
| 121 | + queue.enqueue(second, {id: 'second'}); |
| 122 | + |
| 123 | + assert.equal(queue.dequeue(), first); |
| 124 | + |
| 125 | + queue.setPriority('second', 1); |
| 126 | + |
| 127 | + assert.equal(queue.size, 1); |
| 128 | + assert.equal(queue.dequeue(), second); |
| 129 | + assert.equal(queue.dequeue(), undefined); |
| 130 | +}); |
0 commit comments