|
1 | 1 | import { type PerformanceEntry, performance } from 'node:perf_hooks'; |
2 | 2 | import type { MockedFunction } from 'vitest'; |
3 | 3 | import { MockPerformanceObserver } from '@code-pushup/test-utils'; |
4 | | -import { MockSink } from '../../mocks/sink.mock'; |
| 4 | +import { MockFileSink } from '../../mocks/sink.mock'; |
5 | 5 | import { |
6 | 6 | DEFAULT_FLUSH_THRESHOLD, |
7 | 7 | DEFAULT_MAX_QUEUE_SIZE, |
8 | 8 | type PerformanceObserverOptions, |
9 | 9 | PerformanceObserverSink, |
10 | 10 | validateFlushThreshold, |
11 | 11 | } from './performance-observer.js'; |
| 12 | +import type { Codec } from './wal.js'; |
12 | 13 |
|
13 | 14 | describe('validateFlushThreshold', () => { |
14 | 15 | it.each([ |
@@ -55,12 +56,12 @@ describe('validateFlushThreshold', () => { |
55 | 56 |
|
56 | 57 | describe('PerformanceObserverSink', () => { |
57 | 58 | let encodePerfEntry: MockedFunction<(entry: PerformanceEntry) => string[]>; |
58 | | - let sink: MockSink; |
| 59 | + let sink: MockFileSink; |
59 | 60 | let options: PerformanceObserverOptions<string>; |
60 | 61 |
|
61 | 62 | beforeEach(() => { |
62 | 63 | vi.clearAllMocks(); |
63 | | - sink = new MockSink(); |
| 64 | + sink = new MockFileSink(); |
64 | 65 | sink.open(); |
65 | 66 | encodePerfEntry = vi.fn((entry: PerformanceEntry) => [ |
66 | 67 | `${entry.name}:${entry.entryType}`, |
@@ -663,6 +664,28 @@ describe('PerformanceObserverSink', () => { |
663 | 664 | ); |
664 | 665 | }); |
665 | 666 |
|
| 667 | + it('accepts custom sinks with append method', () => { |
| 668 | + const collectedItems: string[] = []; |
| 669 | + const customSink = { |
| 670 | + // eslint-disable-next-line functional/immutable-data |
| 671 | + append: (item: string) => collectedItems.push(item), |
| 672 | + }; |
| 673 | + |
| 674 | + const observer = new PerformanceObserverSink({ |
| 675 | + sink: customSink, |
| 676 | + encode: (entry: PerformanceEntry) => [`${entry.name}:${entry.duration}`], |
| 677 | + }); |
| 678 | + |
| 679 | + observer.subscribe(); |
| 680 | + |
| 681 | + const mockObserver = MockPerformanceObserver.lastInstance(); |
| 682 | + mockObserver?.emitMark('test-mark'); |
| 683 | + |
| 684 | + observer.flush(); |
| 685 | + |
| 686 | + expect(collectedItems).toContain('test-mark:0'); |
| 687 | + }); |
| 688 | + |
666 | 689 | it('tracks addedSinceLastFlush counter correctly', () => { |
667 | 690 | const observer = new PerformanceObserverSink({ |
668 | 691 | sink, |
|
0 commit comments