Skip to content

Commit fefaed6

Browse files
committed
refactor: wip
1 parent fd9910c commit fefaed6

File tree

8 files changed

+382
-248
lines changed

8 files changed

+382
-248
lines changed

packages/utils/src/lib/profiler/trace-file-utils.ts

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -335,32 +335,42 @@ function processDetail<T extends { detail?: unknown }>(
335335
return target;
336336
}
337337

338+
function encodeDetailToString<T extends { detail?: unknown }>(
339+
target: T,
340+
): T & { detail?: string } {
341+
return processDetail(target, (detail: string | object) =>
342+
typeof detail === 'object' ? JSON.stringify(detail) : detail,
343+
) as T & { detail?: string };
344+
}
345+
338346
/**
339347
* Decodes a JSON string detail property back to its original object form.
340348
* @param target - Object containing a detail property as a JSON string
341349
* @returns UserTimingDetail with the detail property parsed from JSON
342350
*/
343-
export function decodeDetail(target: { detail: string }): UserTimingDetail {
351+
export function decodeDetail<T extends { detail?: string | object }>(
352+
target: T,
353+
): T {
344354
return processDetail(target, detail =>
345355
typeof detail === 'string'
346356
? (JSON.parse(detail) as string | object)
347357
: detail,
348-
) as UserTimingDetail;
358+
);
349359
}
350360

351361
/**
352362
* Encodes object detail properties to JSON strings for storage/transmission.
353363
* @param target - UserTimingDetail object with detail property to encode
354364
* @returns UserTimingDetail with object details converted to JSON strings
355365
*/
356-
export function encodeDetail(target: UserTimingDetail): UserTimingDetail {
366+
export function encodeDetail<T extends { detail?: string | object }>(
367+
target: T,
368+
): T {
357369
return processDetail(
358-
target as UserTimingDetail & { detail?: unknown },
370+
target as T & { detail?: unknown },
359371
(detail: string | object) =>
360-
typeof detail === 'object'
361-
? JSON.stringify(detail as UserTimingDetail)
362-
: detail,
363-
) as UserTimingDetail;
372+
typeof detail === 'object' ? JSON.stringify(detail) : detail,
373+
);
364374
}
365375

366376
/**
@@ -406,13 +416,13 @@ export function encodeTraceEvent({
406416
return rest as TraceEventRaw;
407417
}
408418

409-
const processedArgs = encodeDetail(args as UserTimingDetail);
419+
const processedArgs = encodeDetailToString(args as { detail?: unknown });
410420
if ('data' in args && args.data && typeof args.data === 'object') {
411421
const result: TraceEventRaw = {
412422
...rest,
413423
args: {
414424
...processedArgs,
415-
data: encodeDetail(args.data as UserTimingDetail),
425+
data: encodeDetailToString(args.data as { detail?: unknown }),
416426
},
417427
};
418428
return result;

packages/utils/src/lib/profiler/trace-file-utils.unit.test.ts

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -623,16 +623,18 @@ describe('getTraceMetadata', () => {
623623

624624
describe('decodeDetail', () => {
625625
it('should decode string detail back to object', () => {
626-
const input = { detail: '{"key": "value"}' };
626+
const input = {
627+
detail: '{"devtools":{"dataType":"marker","color":"primary"}}',
628+
};
627629
const result = decodeDetail(input);
628630

629631
expect(result).toStrictEqual({
630-
detail: { key: 'value' },
632+
detail: { devtools: { dataType: 'marker', color: 'primary' } },
631633
});
632634
});
633635

634636
it('should return object detail unchanged', () => {
635-
const input = { detail: { key: 'value' } };
637+
const input = { detail: { devtools: { dataType: 'marker' as const } } };
636638
const result = decodeDetail(input);
637639

638640
expect(result).toStrictEqual(input);
@@ -655,11 +657,11 @@ describe('decodeDetail', () => {
655657

656658
describe('encodeDetail', () => {
657659
it('should encode object detail to JSON string', () => {
658-
const input = { detail: { key: 'value' } };
660+
const input = { detail: { devtools: { dataType: 'marker' as const } } };
659661
const result = encodeDetail(input);
660662

661663
expect(result).toStrictEqual({
662-
detail: '{"key":"value"}',
664+
detail: '{"devtools":{"dataType":"marker"}}',
663665
});
664666
});
665667

@@ -695,8 +697,11 @@ describe('decodeTraceEvent', () => {
695697
tid: 456,
696698
ts: 1000,
697699
args: {
698-
detail: '{"custom": "data"}',
699-
data: { detail: '{"nested": "value"}' },
700+
detail: '{"devtools":{"dataType":"marker","color":"primary"}}',
701+
data: {
702+
detail:
703+
'{"devtools":{"dataType":"track-entry","track":"test-track"}}',
704+
},
700705
},
701706
};
702707

@@ -710,8 +715,14 @@ describe('decodeTraceEvent', () => {
710715
tid: 456,
711716
ts: 1000,
712717
args: {
713-
detail: { custom: 'data' },
714-
data: { detail: { nested: 'value' } },
718+
detail: {
719+
devtools: { dataType: 'marker' as const, color: 'primary' as const },
720+
},
721+
data: {
722+
detail: {
723+
devtools: { dataType: 'track-entry' as const, track: 'test-track' },
724+
},
725+
},
715726
},
716727
});
717728
});
@@ -724,6 +735,7 @@ describe('decodeTraceEvent', () => {
724735
pid: 123,
725736
tid: 456,
726737
ts: 1000,
738+
args: {},
727739
};
728740

729741
const result = decodeTraceEvent(rawEvent);
@@ -735,6 +747,7 @@ describe('decodeTraceEvent', () => {
735747
pid: 123,
736748
tid: 456,
737749
ts: 1000,
750+
args: {},
738751
});
739752
});
740753

@@ -747,7 +760,7 @@ describe('decodeTraceEvent', () => {
747760
tid: 456,
748761
ts: 1000,
749762
args: {
750-
detail: '{"custom": "data"}',
763+
detail: '{"devtools":{"dataType":"marker","color":"primary"}}',
751764
},
752765
};
753766

@@ -761,7 +774,9 @@ describe('decodeTraceEvent', () => {
761774
tid: 456,
762775
ts: 1000,
763776
args: {
764-
detail: { custom: 'data' },
777+
detail: {
778+
devtools: { dataType: 'marker' as const, color: 'primary' as const },
779+
},
765780
},
766781
});
767782
});
@@ -777,8 +792,14 @@ describe('encodeTraceEvent', () => {
777792
tid: 456,
778793
ts: 1000,
779794
args: {
780-
detail: { custom: 'data' },
781-
data: { detail: { nested: 'value' } },
795+
detail: {
796+
devtools: { dataType: 'marker' as const, color: 'primary' as const },
797+
},
798+
data: {
799+
detail: {
800+
devtools: { dataType: 'track-entry' as const, track: 'test-track' },
801+
},
802+
},
782803
},
783804
};
784805

@@ -792,8 +813,11 @@ describe('encodeTraceEvent', () => {
792813
tid: 456,
793814
ts: 1000,
794815
args: {
795-
detail: '{"custom":"data"}',
796-
data: { detail: '{"nested":"value"}' },
816+
detail: '{"devtools":{"dataType":"marker","color":"primary"}}',
817+
data: {
818+
detail:
819+
'{"devtools":{"dataType":"track-entry","track":"test-track"}}',
820+
},
797821
},
798822
});
799823
});
@@ -806,6 +830,7 @@ describe('encodeTraceEvent', () => {
806830
pid: 123,
807831
tid: 456,
808832
ts: 1000,
833+
args: {},
809834
};
810835

811836
const result = encodeTraceEvent(event);
@@ -817,6 +842,7 @@ describe('encodeTraceEvent', () => {
817842
pid: 123,
818843
tid: 456,
819844
ts: 1000,
845+
args: {},
820846
});
821847
});
822848

@@ -829,7 +855,9 @@ describe('encodeTraceEvent', () => {
829855
tid: 456,
830856
ts: 1000,
831857
args: {
832-
detail: { custom: 'data' },
858+
detail: {
859+
devtools: { dataType: 'marker' as const, color: 'primary' as const },
860+
},
833861
},
834862
};
835863

@@ -843,7 +871,7 @@ describe('encodeTraceEvent', () => {
843871
tid: 456,
844872
ts: 1000,
845873
args: {
846-
detail: '{"custom":"data"}',
874+
detail: '{"devtools":{"dataType":"marker","color":"primary"}}',
847875
},
848876
});
849877
});

packages/utils/src/lib/reports/load-report.unit.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ describe('loadReport', () => {
1919
outputDir: MEMFS_VOLUME,
2020
filename: 'report',
2121
format: 'json',
22+
skipReports: false,
2223
}),
2324
).resolves.toEqual(reportMock());
2425
});
@@ -38,6 +39,7 @@ describe('loadReport', () => {
3839
outputDir: MEMFS_VOLUME,
3940
format: 'md',
4041
filename: 'report',
42+
skipReports: false,
4143
}),
4244
).resolves.toBe('test-42');
4345
});
@@ -58,6 +60,7 @@ describe('loadReport', () => {
5860
outputDir: MEMFS_VOLUME,
5961
filename: 'report',
6062
format: 'json',
63+
skipReports: false,
6164
}),
6265
).rejects.toThrow('slug has to follow the pattern');
6366
});

packages/utils/src/lib/user-timing-extensibility-api.type.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ export type ActionTrackEntryPayload = TrackEntryPayload & ActionColorPayload;
132132
* Utility type that adds an optional devtools payload property.
133133
*/
134134
export type WithDevToolsPayload<T extends TrackEntryPayload | MarkerPayload> = {
135-
devtools?: T;
135+
devtools: T;
136136
};
137137

138138
/**

0 commit comments

Comments
 (0)