11import { type PerformanceEntry , performance } from 'node:perf_hooks' ;
22import type { MockedFunction } from 'vitest' ;
33import { MockPerformanceObserver } from '@code-pushup/test-utils' ;
4- import { MockSink } from '../../mocks/sink.mock' ;
4+ import { MockFileSink } from '../../mocks/sink.mock' ;
55import {
66 type PerformanceObserverOptions ,
77 PerformanceObserverSink ,
88} from './performance-observer.js' ;
9+ import type { Codec } from './wal.js' ;
910
1011describe ( 'PerformanceObserverSink' , ( ) => {
1112 let encode : MockedFunction < ( entry : PerformanceEntry ) => string [ ] > ;
12- let sink : MockSink ;
13+ let sink : MockFileSink ;
1314 let options : PerformanceObserverOptions < string > ;
1415
1516 beforeEach ( ( ) => {
1617 vi . clearAllMocks ( ) ;
17- sink = new MockSink ( ) ;
18+ sink = new MockFileSink ( ) ;
1819 encode = vi . fn ( ( entry : PerformanceEntry ) => [
1920 `${ entry . name } :${ entry . entryType } ` ,
2021 ] ) ;
2122 options = {
2223 sink,
2324 encode,
24- // we test buffered behavior separately
25+
2526 flushThreshold : 1 ,
2627 } ;
2728
@@ -43,24 +44,21 @@ describe('PerformanceObserverSink', () => {
4344 } ) ,
4445 ) . not . toThrow ( ) ;
4546 expect ( MockPerformanceObserver . instances ) . toHaveLength ( 0 ) ;
46- // Instance creation covers the default flushThreshold assignment
4747 } ) ;
4848
4949 it ( 'automatically flushes when pendingCount reaches flushThreshold' , ( ) => {
5050 const observer = new PerformanceObserverSink ( {
5151 sink,
5252 encode,
53- flushThreshold : 2 , // Set threshold to 2
53+ flushThreshold : 2 ,
5454 } ) ;
5555 observer . subscribe ( ) ;
5656
5757 const mockObserver = MockPerformanceObserver . lastInstance ( ) ;
5858
59- // Emit 1 entry - should not trigger flush yet (pendingCount = 1 < 2)
6059 mockObserver ?. emitMark ( 'first-mark' ) ;
6160 expect ( sink . getWrittenItems ( ) ) . toStrictEqual ( [ ] ) ;
6261
63- // Emit 1 more entry - should trigger flush (pendingCount = 2 >= 2)
6462 mockObserver ?. emitMark ( 'second-mark' ) ;
6563 expect ( sink . getWrittenItems ( ) ) . toStrictEqual ( [
6664 'first-mark:mark' ,
@@ -135,7 +133,7 @@ describe('PerformanceObserverSink', () => {
135133 it ( 'internal PerformanceObserver should process observed entries' , ( ) => {
136134 const observer = new PerformanceObserverSink ( {
137135 ...options ,
138- flushThreshold : 20 , // Disable automatic flushing for this test
136+ flushThreshold : 20 ,
139137 } ) ;
140138 observer . subscribe ( ) ;
141139
@@ -248,12 +246,22 @@ describe('PerformanceObserverSink', () => {
248246 } ) ;
249247
250248 it ( 'flush wraps sink write errors with descriptive error message' , ( ) => {
251- const failingSink = {
252- write : vi . fn ( ( ) => {
249+ const failingCodec : Codec < string > = {
250+ encode : ( ) => {
253251 throw new Error ( 'Sink write failed' ) ;
254- } ) ,
252+ } ,
253+ decode : ( data : string ) => data ,
255254 } ;
256255
256+ const failingSink = new MockFileSink ( {
257+ file : '/test/path' ,
258+ codec : failingCodec ,
259+ } ) ;
260+
261+ vi . spyOn ( failingSink , 'append' ) . mockImplementation ( ( ) => {
262+ throw new Error ( 'Sink write failed' ) ;
263+ } ) ;
264+
257265 const observer = new PerformanceObserverSink ( {
258266 sink : failingSink as any ,
259267 encode,
@@ -298,4 +306,26 @@ describe('PerformanceObserverSink', () => {
298306 } ) ,
299307 ) ;
300308 } ) ;
309+
310+ it ( 'accepts custom sinks with append method' , ( ) => {
311+ const collectedItems : string [ ] = [ ] ;
312+ const customSink = {
313+ // eslint-disable-next-line functional/immutable-data
314+ append : ( item : string ) => collectedItems . push ( item ) ,
315+ } ;
316+
317+ const observer = new PerformanceObserverSink ( {
318+ sink : customSink ,
319+ encode : ( entry : PerformanceEntry ) => [ `${ entry . name } :${ entry . duration } ` ] ,
320+ } ) ;
321+
322+ observer . subscribe ( ) ;
323+
324+ const mockObserver = MockPerformanceObserver . lastInstance ( ) ;
325+ mockObserver ?. emitMark ( 'test-mark' ) ;
326+
327+ observer . flush ( ) ;
328+
329+ expect ( collectedItems ) . toContain ( 'test-mark:0' ) ;
330+ } ) ;
301331} ) ;
0 commit comments