|
| 1 | +import type { Event } from '@rolldown/debug' |
| 2 | +import { describe, expect, it } from 'vitest' |
| 3 | +import { createPluginBuildMetricsSummary, RolldownEventsManager } from '../events-manager' |
| 4 | +import { stringifyInSlices } from '../log-cache' |
| 5 | + |
| 6 | +const base = { build_id: 'bid_1', session_id: 'sid_1' } |
| 7 | + |
| 8 | +function hookCalls(callId: string, pluginId: number, module: string, time: number): Record<string, unknown>[] { |
| 9 | + const plugin = { plugin_name: `plugin-${pluginId}`, plugin_id: pluginId, trigger: 'automatic', ...base } |
| 10 | + return [ |
| 11 | + { timestamp: time, action: 'HookResolveIdCallStart', importer: null, module_request: module, import_kind: 'import-statement', call_id: `${callId}-r`, ...plugin }, |
| 12 | + { timestamp: time + 1, action: 'HookResolveIdCallEnd', resolved_id: module, is_external: false, call_id: `${callId}-r`, ...plugin }, |
| 13 | + { timestamp: time + 2, action: 'HookLoadCallStart', module_id: module, call_id: `${callId}-l`, ...plugin }, |
| 14 | + { timestamp: time + 5, action: 'HookLoadCallEnd', module_id: module, content: null, call_id: `${callId}-l`, ...plugin }, |
| 15 | + { timestamp: time + 6, action: 'HookTransformCallStart', module_id: module, content: 'a', call_id: `${callId}-t`, ...plugin }, |
| 16 | + { timestamp: time + 9, action: 'HookTransformCallEnd', module_id: module, content: 'bb', call_id: `${callId}-t`, ...plugin }, |
| 17 | + ] |
| 18 | +} |
| 19 | + |
| 20 | +function expectSummariesMatchCalls(manager: RolldownEventsManager) { |
| 21 | + expect(manager.plugin_build_metrics.size).toBeGreaterThan(0) |
| 22 | + const expected = new Map(Array.from(manager.plugin_build_metrics, ([id, metrics]) => [id, createPluginBuildMetricsSummary(metrics)])) |
| 23 | + expect(manager.plugin_build_summaries).toEqual(expected) |
| 24 | +} |
| 25 | + |
| 26 | +describe('plugin summaries accumulated while parsing', () => { |
| 27 | + it('match summaries computed from the recorded calls, also after a restore', () => { |
| 28 | + const manager = new RolldownEventsManager() |
| 29 | + const events = [ |
| 30 | + { timestamp: 1, action: 'BuildStart', ...base }, |
| 31 | + ...hookCalls('a', 1, '/src/a.ts', 10), |
| 32 | + ...hookCalls('b', 2, '/src/b.ts', 20), |
| 33 | + ...hookCalls('c', 1, '/src/c.ts', 40), |
| 34 | + // An end without a matching start isn't recorded, so it must not be counted either. |
| 35 | + { timestamp: 60, action: 'HookTransformCallEnd', module_id: '/src/a.ts', content: 'x', call_id: 'missing', plugin_name: 'plugin-1', plugin_id: 1, trigger: 'automatic', ...base }, |
| 36 | + ] |
| 37 | + for (const event of events) |
| 38 | + manager.handleEvent(event as unknown as Event) |
| 39 | + |
| 40 | + expectSummariesMatchCalls(manager) |
| 41 | + expect(manager.plugin_build_summaries.get(1)!.total.count).toBe(6) |
| 42 | + |
| 43 | + // Restoring a snapshot (package summary cache) rebuilds the summaries. |
| 44 | + const restored = new RolldownEventsManager() |
| 45 | + restored.restore(manager.snapshot()) |
| 46 | + expect(restored.plugin_build_summaries).toEqual(manager.plugin_build_summaries) |
| 47 | + }) |
| 48 | + |
| 49 | + it('include hydrated calls when the log grows after metrics were set without a summary', () => { |
| 50 | + const source = new RolldownEventsManager() |
| 51 | + for (const event of [{ timestamp: 1, action: 'BuildStart', ...base }, ...hookCalls('a', 1, '/src/a.ts', 10)]) |
| 52 | + source.handleEvent(event as unknown as Event) |
| 53 | + |
| 54 | + // Mirrors `hydratePluginBuildMetrics()` on a summary-only read: metrics come from the index, no summary. |
| 55 | + const manager = new RolldownEventsManager() |
| 56 | + manager.handleEvent({ timestamp: 1, action: 'BuildStart', ...base } as unknown as Event) |
| 57 | + manager.plugin_build_metrics.set(1, structuredClone(source.plugin_build_metrics.get(1)!)) |
| 58 | + for (const event of hookCalls('b', 1, '/src/b.ts', 20)) |
| 59 | + manager.handleEvent(event as unknown as Event) |
| 60 | + |
| 61 | + expectSummariesMatchCalls(manager) |
| 62 | + expect(manager.plugin_build_summaries.get(1)!.total.count).toBe(6) |
| 63 | + }) |
| 64 | +}) |
| 65 | + |
| 66 | +describe('sliced cache serialization', () => { |
| 67 | + it('produces the same JSON as JSON.stringify', async () => { |
| 68 | + const large = Array.from({ length: 1100 }, (_, index) => index % 7 === 0 |
| 69 | + ? undefined |
| 70 | + : { |
| 71 | + id: `m${index} "quoted" \u2028 ä`, |
| 72 | + skipped: undefined, |
| 73 | + nested: index % 5 ? [index, null, undefined, { deep: [1, 2] }] : Array.from({ length: 1100 }, (_, i) => i), |
| 74 | + fn: () => index, |
| 75 | + // eslint-disable-next-line no-new-wrappers, unicorn/new-for-builtins |
| 76 | + boxed: new String(`s${index}`), |
| 77 | + date: new Date(index), |
| 78 | + }) |
| 79 | + const value = { version: 3, log: { size: 1, mtime: 2 }, data: { metrics: large, empty: [], text: 'x' }, missing: undefined } |
| 80 | + |
| 81 | + const parts = await stringifyInSlices(value) |
| 82 | + expect(parts.join('')).toBe(JSON.stringify(value)) |
| 83 | + // Fragments are coalesced, so writing doesn't issue one write per item. |
| 84 | + expect(parts.length).toBeLessThan(JSON.stringify(value).length / 1024) |
| 85 | + }) |
| 86 | +}) |
0 commit comments