|
| 1 | +/* |
| 2 | + * Copyright (c) 2026 Certinia Inc. All rights reserved. |
| 3 | + */ |
| 4 | + |
| 5 | +/** |
| 6 | + * Unit tests for MinimapDensityQuery |
| 7 | + * |
| 8 | + * Tests category resolution for minimap coloring, ensuring: |
| 9 | + * - Long-spanning parent frames are not skipped during frame collection |
| 10 | + * - Skyline (on-top time) algorithm correctly identifies dominant category |
| 11 | + * - Both fallback and segment tree paths produce consistent results |
| 12 | + */ |
| 13 | + |
| 14 | +import type { LogEvent } from 'apex-log-parser'; |
| 15 | +import { MinimapDensityQuery } from '../optimised/minimap/MinimapDensityQuery.js'; |
| 16 | +import type { PrecomputedRect } from '../optimised/RectangleManager.js'; |
| 17 | +import { TemporalSegmentTree } from '../optimised/TemporalSegmentTree.js'; |
| 18 | + |
| 19 | +/** |
| 20 | + * Helper to create a mock PrecomputedRect. |
| 21 | + */ |
| 22 | +function createRect( |
| 23 | + category: string, |
| 24 | + timeStart: number, |
| 25 | + timeEnd: number, |
| 26 | + depth: number, |
| 27 | + selfDuration?: number, |
| 28 | +): PrecomputedRect { |
| 29 | + const duration = timeEnd - timeStart; |
| 30 | + return { |
| 31 | + id: `${category}-${timeStart}-${depth}`, |
| 32 | + timeStart, |
| 33 | + timeEnd, |
| 34 | + depth, |
| 35 | + duration, |
| 36 | + selfDuration: selfDuration ?? duration, |
| 37 | + category, |
| 38 | + x: 0, |
| 39 | + y: 0, |
| 40 | + width: 0, |
| 41 | + height: 20, |
| 42 | + eventRef: { timestamp: timeStart } as LogEvent, |
| 43 | + }; |
| 44 | +} |
| 45 | + |
| 46 | +/** |
| 47 | + * Build rectsByCategory from a flat list of rects. |
| 48 | + */ |
| 49 | +function buildRectsByCategory(rects: PrecomputedRect[]): Map<string, PrecomputedRect[]> { |
| 50 | + const map = new Map<string, PrecomputedRect[]>(); |
| 51 | + for (const rect of rects) { |
| 52 | + let arr = map.get(rect.category); |
| 53 | + if (!arr) { |
| 54 | + arr = []; |
| 55 | + map.set(rect.category, arr); |
| 56 | + } |
| 57 | + arr.push(rect); |
| 58 | + } |
| 59 | + return map; |
| 60 | +} |
| 61 | + |
| 62 | +describe('MinimapDensityQuery', () => { |
| 63 | + describe('category resolution with long-spanning parent frames', () => { |
| 64 | + /** |
| 65 | + * Regression test: A long parent Method frame spanning many buckets |
| 66 | + * with a short DML child in the middle. |
| 67 | + * |
| 68 | + * The parent Method frame must be included in all overlapping buckets |
| 69 | + * for correct skyline computation. If frames are collected via binary |
| 70 | + * search on timeEnd (with timeStart-sorted data), long-spanning parent |
| 71 | + * frames can be skipped, causing incorrect coloring. |
| 72 | + * |
| 73 | + * Layout: |
| 74 | + * depth 0: |-------- Method (0-1000) --------| |
| 75 | + * depth 1: |-- DML (300-400) --| |
| 76 | + * |
| 77 | + * Expected: Buckets outside DML range should be Method (green). |
| 78 | + * Bucket covering DML range should be DML (brown) due to weight. |
| 79 | + */ |
| 80 | + const rects = [ |
| 81 | + createRect('Method', 0, 1000, 0, 600), // parent, selfDuration excludes DML child time |
| 82 | + createRect('DML', 300, 400, 1, 100), |
| 83 | + ]; |
| 84 | + |
| 85 | + it('should show Method in buckets outside DML range (fallback path)', () => { |
| 86 | + const rectsByCategory = buildRectsByCategory(rects); |
| 87 | + const query = new MinimapDensityQuery(rectsByCategory, 1000, 1); |
| 88 | + |
| 89 | + // 10 buckets: each covers 100ns |
| 90 | + // Bucket 0 [0-100]: only Method → Method |
| 91 | + // Bucket 3 [300-400]: Method + DML → DML wins (2.5x weight) |
| 92 | + // Bucket 9 [900-1000]: only Method → Method |
| 93 | + const result = query.query(10); |
| 94 | + |
| 95 | + expect(result.buckets[0]!.dominantCategory).toBe('Method'); |
| 96 | + expect(result.buckets[1]!.dominantCategory).toBe('Method'); |
| 97 | + expect(result.buckets[9]!.dominantCategory).toBe('Method'); |
| 98 | + |
| 99 | + // DML bucket: DML at depth 1 is deeper, with 2.5x weight |
| 100 | + expect(result.buckets[3]!.dominantCategory).toBe('DML'); |
| 101 | + }); |
| 102 | + |
| 103 | + it('should show Method in buckets outside DML range (segment tree path)', () => { |
| 104 | + const rectsByCategory = buildRectsByCategory(rects); |
| 105 | + const segmentTree = new TemporalSegmentTree(rectsByCategory); |
| 106 | + const query = new MinimapDensityQuery(rectsByCategory, 1000, 1, segmentTree); |
| 107 | + |
| 108 | + const result = query.query(10); |
| 109 | + |
| 110 | + // These buckets must be Method - the parent frame spans all of them |
| 111 | + expect(result.buckets[0]!.dominantCategory).toBe('Method'); |
| 112 | + expect(result.buckets[1]!.dominantCategory).toBe('Method'); |
| 113 | + expect(result.buckets[5]!.dominantCategory).toBe('Method'); |
| 114 | + expect(result.buckets[9]!.dominantCategory).toBe('Method'); |
| 115 | + |
| 116 | + // DML bucket |
| 117 | + expect(result.buckets[3]!.dominantCategory).toBe('DML'); |
| 118 | + }); |
| 119 | + |
| 120 | + it('should produce consistent results between fallback and segment tree paths', () => { |
| 121 | + const rectsByCategory = buildRectsByCategory(rects); |
| 122 | + const segmentTree = new TemporalSegmentTree(rectsByCategory); |
| 123 | + |
| 124 | + const fallbackQuery = new MinimapDensityQuery(rectsByCategory, 1000, 1); |
| 125 | + const treeQuery = new MinimapDensityQuery(rectsByCategory, 1000, 1, segmentTree); |
| 126 | + |
| 127 | + const fallbackResult = fallbackQuery.query(10); |
| 128 | + const treeResult = treeQuery.query(10); |
| 129 | + |
| 130 | + for (let i = 0; i < 10; i++) { |
| 131 | + expect(treeResult.buckets[i]!.dominantCategory).toBe( |
| 132 | + fallbackResult.buckets[i]!.dominantCategory, |
| 133 | + ); |
| 134 | + } |
| 135 | + }); |
| 136 | + }); |
| 137 | + |
| 138 | + describe('multiple depth levels with overlapping frames', () => { |
| 139 | + /** |
| 140 | + * Layout: |
| 141 | + * depth 0: |-------- Code Unit (0-1000) --------| |
| 142 | + * depth 1: |-------- Method (0-1000) ------------| |
| 143 | + * depth 2: |-- SOQL (200-300) --| |-- DML (600-700) --| |
| 144 | + * |
| 145 | + * This tests that parent frames at multiple depths are all correctly |
| 146 | + * collected even when short children exist between them. |
| 147 | + */ |
| 148 | + it('should resolve Method where no SOQL/DML children exist', () => { |
| 149 | + const rects = [ |
| 150 | + createRect('Code Unit', 0, 1000, 0, 0), // code unit has 0 self duration (all children) |
| 151 | + createRect('Method', 0, 1000, 1, 800), // method covers most of the time |
| 152 | + createRect('SOQL', 200, 300, 2, 100), |
| 153 | + createRect('DML', 600, 700, 2, 100), |
| 154 | + ]; |
| 155 | + |
| 156 | + const rectsByCategory = buildRectsByCategory(rects); |
| 157 | + const segmentTree = new TemporalSegmentTree(rectsByCategory); |
| 158 | + const query = new MinimapDensityQuery(rectsByCategory, 1000, 2, segmentTree); |
| 159 | + |
| 160 | + const result = query.query(10); |
| 161 | + |
| 162 | + // Bucket 0 [0-100]: Code Unit + Method → Method wins (deeper) |
| 163 | + expect(result.buckets[0]!.dominantCategory).toBe('Method'); |
| 164 | + |
| 165 | + // Bucket 4 [400-500]: Code Unit + Method → Method wins (deeper) |
| 166 | + expect(result.buckets[4]!.dominantCategory).toBe('Method'); |
| 167 | + |
| 168 | + // Bucket 2 [200-300]: Code Unit + Method + SOQL → SOQL wins (deepest + 2.5x weight) |
| 169 | + expect(result.buckets[2]!.dominantCategory).toBe('SOQL'); |
| 170 | + |
| 171 | + // Bucket 6 [600-700]: Code Unit + Method + DML → DML wins (deepest + 2.5x weight) |
| 172 | + expect(result.buckets[6]!.dominantCategory).toBe('DML'); |
| 173 | + }); |
| 174 | + }); |
| 175 | + |
| 176 | + describe('edge cases', () => { |
| 177 | + it('should handle single frame spanning all buckets', () => { |
| 178 | + const rects = [createRect('Method', 0, 1000, 0)]; |
| 179 | + const rectsByCategory = buildRectsByCategory(rects); |
| 180 | + const segmentTree = new TemporalSegmentTree(rectsByCategory); |
| 181 | + const query = new MinimapDensityQuery(rectsByCategory, 1000, 0, segmentTree); |
| 182 | + |
| 183 | + const result = query.query(5); |
| 184 | + |
| 185 | + for (const bucket of result.buckets) { |
| 186 | + expect(bucket.dominantCategory).toBe('Method'); |
| 187 | + } |
| 188 | + }); |
| 189 | + |
| 190 | + it('should handle empty timeline', () => { |
| 191 | + const rectsByCategory = new Map<string, PrecomputedRect[]>(); |
| 192 | + const query = new MinimapDensityQuery(rectsByCategory, 0, 0); |
| 193 | + |
| 194 | + const result = query.query(10); |
| 195 | + expect(result.buckets).toHaveLength(0); |
| 196 | + }); |
| 197 | + }); |
| 198 | +}); |
0 commit comments