diff --git a/blocksuite/affine/blocks/surface/src/__tests__/priority-queue.unit.spec.ts b/blocksuite/affine/blocks/surface/src/__tests__/priority-queue.unit.spec.ts index 83a3d364c6..ceffc4b423 100644 --- a/blocksuite/affine/blocks/surface/src/__tests__/priority-queue.unit.spec.ts +++ b/blocksuite/affine/blocks/surface/src/__tests__/priority-queue.unit.spec.ts @@ -19,4 +19,32 @@ describe('priority queue', () => { expect(pq.dequeue()).toBe('e'); expect(pq.dequeue()).toBe(null); }); + + it('should not sink a node past children that are all larger than it', () => { + // While sinking `4`, `bubbleDown` reaches a node whose children are + // `[7, 6]`. The right child is smaller than the left one, but both are + // larger than `4`, so the node must stay where it is. + const pq = new PriorityQueue((a, b) => a - b); + [1, 2, 3, 7, 6, 5, 4].forEach(priority => pq.enqueue(priority, priority)); + + const dequeued: number[] = []; + while (!pq.empty()) { + dequeued.push(pq.dequeue() as number); + } + + expect(dequeued).toEqual([1, 2, 3, 4, 5, 6, 7]); + }); + + it('should dequeue in ascending order regardless of insertion order', () => { + const input = [11, 63, 91, 7, 38, 67, 8, 13, 29]; + const pq = new PriorityQueue((a, b) => a - b); + input.forEach(priority => pq.enqueue(priority, priority)); + + const dequeued: number[] = []; + while (!pq.empty()) { + dequeued.push(pq.dequeue() as number); + } + + expect(dequeued).toEqual([...input].sort((a, b) => a - b)); + }); }); diff --git a/blocksuite/affine/blocks/surface/src/utils/priority-queue.ts b/blocksuite/affine/blocks/surface/src/utils/priority-queue.ts index a5df2b5c03..7284734287 100644 --- a/blocksuite/affine/blocks/surface/src/utils/priority-queue.ts +++ b/blocksuite/affine/blocks/surface/src/utils/priority-queue.ts @@ -31,9 +31,9 @@ export class PriorityQueue { leftChild = this.heap[leftChildIndex]; rightChild = this.heap[rightChildIndex]; if ( - (swap === null && + (swap === -1 && this._compare(rightChild.priority, element.priority) < 0) || - (swap !== null && + (swap !== -1 && this._compare(rightChild.priority, leftChild.priority) < 0) ) { swap = rightChildIndex; diff --git a/blocksuite/affine/blocks/surface/vitest.config.ts b/blocksuite/affine/blocks/surface/vitest.config.ts new file mode 100644 index 0000000000..3f96bdf56b --- /dev/null +++ b/blocksuite/affine/blocks/surface/vitest.config.ts @@ -0,0 +1,30 @@ +import { defineConfig } from 'vitest/config'; + +export default defineConfig({ + esbuild: { + target: 'es2018', + }, + test: { + globalSetup: '../../../../scripts/vitest-global.js', + include: ['src/__tests__/**/*.unit.spec.ts'], + testTimeout: 1000, + coverage: { + provider: 'istanbul', + reporter: ['lcov'], + reportsDirectory: '../../../../.coverage/affine-block-surface', + }, + /** + * Custom handler for console.log in tests. + * + * Return `false` to ignore the log. + */ + onConsoleLog(log, type) { + if (log.includes('https://lit.dev/msg/dev-mode')) { + return false; + } + console.warn(`Unexpected ${type} log`, log); + throw new Error(log); + }, + environment: 'happy-dom', + }, +});