Skip to content

Commit 4b9846c

Browse files
author
John Doe
committed
refactor: implement review
1 parent 5042806 commit 4b9846c

File tree

4 files changed

+36
-64
lines changed

4 files changed

+36
-64
lines changed

packages/utils/mocks/sink.mock.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import type { Sink } from '../src/lib/sink-source.types';
2+
3+
export class MockSink implements Sink<string, string> {
4+
private writtenItems: string[] = [];
5+
private closed = false;
6+
7+
open(): void {
8+
this.closed = false;
9+
}
10+
11+
write(input: string): void {
12+
this.writtenItems.push(input);
13+
}
14+
15+
close(): void {
16+
this.closed = true;
17+
}
18+
19+
isClosed(): boolean {
20+
return this.closed;
21+
}
22+
23+
encode(input: string): string {
24+
return `${input}-${this.constructor.name}-encoded`;
25+
}
26+
27+
getWrittenItems(): string[] {
28+
return [...this.writtenItems];
29+
}
30+
}

packages/utils/src/lib/performance-observer.int.test.ts

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,12 @@
11
import { type PerformanceEntry, performance } from 'node:perf_hooks';
22
import { beforeEach, describe, expect, it, vi } from 'vitest';
3+
import { MockSink } from '../../mocks/sink.mock';
34
import {
45
type PerformanceObserverOptions,
56
PerformanceObserverSink,
67
} from './performance-observer.js';
78
import type { Sink } from './sink-source.types';
89

9-
// @TODO remove duplicate when file-sink is implemented
10-
class MockSink implements Sink<string, string> {
11-
private writtenItems: string[] = [];
12-
private closed = false;
13-
14-
open(): void {
15-
this.closed = false;
16-
}
17-
18-
write(input: string): void {
19-
this.writtenItems.push(input);
20-
}
21-
22-
close(): void {
23-
this.closed = true;
24-
}
25-
26-
isClosed(): boolean {
27-
return this.closed;
28-
}
29-
30-
encode(input: string): string {
31-
return `${input}-${this.constructor.name}-encoded`;
32-
}
33-
34-
recover(): string[] {
35-
return [...this.writtenItems];
36-
}
37-
}
38-
3910
describe('PerformanceObserverSink', () => {
4011
let sink: MockSink;
4112
let options: PerformanceObserverOptions<string>;
@@ -79,7 +50,7 @@ describe('PerformanceObserverSink', () => {
7950
observer.subscribe();
8051
performance.mark('test-mark');
8152
observer.flush();
82-
expect(sink.recover()).toHaveLength(1);
53+
expect(sink.getWrittenItems()).toHaveLength(1);
8354
});
8455

8556
it('should observe buffered performance entries when buffered is enabled', async () => {
@@ -95,7 +66,7 @@ describe('PerformanceObserverSink', () => {
9566
await new Promise(resolve => setTimeout(resolve, 10));
9667
expect(performance.getEntries()).toHaveLength(2);
9768
observer.flush();
98-
expect(sink.recover()).toHaveLength(2);
69+
expect(sink.getWrittenItems()).toHaveLength(2);
9970
});
10071

10172
it('handles multiple encoded items per performance entry', () => {
@@ -113,6 +84,6 @@ describe('PerformanceObserverSink', () => {
11384
performance.mark('test-mark');
11485
observer.flush();
11586

116-
expect(sink.recover()).toHaveLength(2);
87+
expect(sink.getWrittenItems()).toHaveLength(2);
11788
});
11889
});

packages/utils/src/lib/performance-observer.unit.test.ts

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,42 +8,13 @@ import {
88
vi,
99
} from 'vitest';
1010
import { MockPerformanceObserver } from '@code-pushup/test-utils';
11+
import { MockSink } from '../../mocks/sink.mock';
1112
import {
1213
type PerformanceObserverOptions,
1314
PerformanceObserverSink,
1415
} from './performance-observer.js';
1516
import type { Sink } from './sink-source.types';
1617

17-
// @TODO remove duplicate when file-sink is implemented
18-
class MockSink implements Sink<string, string> {
19-
private writtenItems: string[] = [];
20-
private closed = false;
21-
22-
open(): void {
23-
this.closed = false;
24-
}
25-
26-
write(input: string): void {
27-
this.writtenItems.push(input);
28-
}
29-
30-
close(): void {
31-
this.closed = true;
32-
}
33-
34-
isClosed(): boolean {
35-
return this.closed;
36-
}
37-
38-
encode(input: string): string {
39-
return `${input}-${this.constructor.name}-encoded`;
40-
}
41-
42-
getWrittenItems(): string[] {
43-
return [...this.writtenItems];
44-
}
45-
}
46-
4718
describe('PerformanceObserverSink', () => {
4819
let encode: MockedFunction<(entry: PerformanceEntry) => string[]>;
4920
let sink: MockSink;

packages/utils/src/lib/sink-source.types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export type Sink<I, O> = {
1616
export type Buffered = {
1717
flush: () => void;
1818
};
19-
export type BufferedSink<I, O> = {} & Sink<I, O> & Buffered;
19+
export type BufferedSink<I, O> = Sink<I, O> & Buffered;
2020

2121
export type Source<I, O = unknown> = {
2222
read?: () => O;

0 commit comments

Comments
 (0)