Skip to content

Commit eb559f5

Browse files
committed
refactor: add tests
1 parent 64501ec commit eb559f5

File tree

5 files changed

+444
-12
lines changed

5 files changed

+444
-12
lines changed

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

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,11 @@ export const nextId2 = () => ({ local: `0x${++id2Count}` });
3434

3535
/**
3636
* Provides default values for trace event properties.
37-
* @param opt - Optional overrides for pid, tid, and timestamp
38-
* @returns Object with pid, tid, and timestamp
37+
* @param opt - Optional overrides for process ID, thread ID, and timestamp
38+
* @param opt.pid - Process ID override, defaults to current process PID
39+
* @param opt.tid - Thread ID override, defaults to current thread ID
40+
* @param opt.ts - Timestamp override in microseconds, defaults to current epoch time
41+
* @returns Object containing pid, tid, and ts with defaults applied
3942
*/
4043
const defaults = (opt?: { pid?: number; tid?: number; ts?: number }) => ({
4144
pid: opt?.pid ?? process.pid,
@@ -275,6 +278,12 @@ export function entryToTraceEvents(
275278
return [];
276279
}
277280

281+
/**
282+
* Creates trace metadata object with standard DevTools fields and custom metadata.
283+
* @param startDate - Optional start date for the trace, defaults to current date
284+
* @param metadata - Optional additional metadata to merge into the trace metadata
285+
* @returns TraceMetadata object with source, startTime, and merged custom metadata
286+
*/
278287
export function getTraceMetadata(
279288
startDate?: Date,
280289
metadata?: Record<string, unknown>,
@@ -306,6 +315,13 @@ export const getTraceFile = (opt: {
306315
),
307316
});
308317

318+
/**
319+
* Processes the detail property of an object using a custom processor function.
320+
* @template T - Object type that may contain a detail property
321+
* @param target - Object containing the detail property to process
322+
* @param processor - Function to transform the detail value
323+
* @returns New object with processed detail property, or original object if no detail
324+
*/
309325
function processDetail<T extends { detail?: unknown }>(
310326
target: T,
311327
processor: (detail: string | object) => string | object,
@@ -319,6 +335,11 @@ function processDetail<T extends { detail?: unknown }>(
319335
return target;
320336
}
321337

338+
/**
339+
* Decodes a JSON string detail property back to its original object form.
340+
* @param target - Object containing a detail property as a JSON string
341+
* @returns UserTimingDetail with the detail property parsed from JSON
342+
*/
322343
export function decodeDetail(target: { detail: string }): UserTimingDetail {
323344
return processDetail(target, detail =>
324345
typeof detail === 'string'
@@ -327,6 +348,11 @@ export function decodeDetail(target: { detail: string }): UserTimingDetail {
327348
) as UserTimingDetail;
328349
}
329350

351+
/**
352+
* Encodes object detail properties to JSON strings for storage/transmission.
353+
* @param target - UserTimingDetail object with detail property to encode
354+
* @returns UserTimingDetail with object details converted to JSON strings
355+
*/
330356
export function encodeDetail(target: UserTimingDetail): UserTimingDetail {
331357
return processDetail(
332358
target as UserTimingDetail & { detail?: unknown },
@@ -337,6 +363,12 @@ export function encodeDetail(target: UserTimingDetail): UserTimingDetail {
337363
) as UserTimingDetail;
338364
}
339365

366+
/**
367+
* Decodes a raw trace event with JSON string details back to typed UserTimingTraceEvent.
368+
* Parses detail properties from JSON strings to objects.
369+
* @param event - Raw trace event with string-encoded details
370+
* @returns UserTimingTraceEvent with parsed detail objects
371+
*/
340372
export function decodeTraceEvent({
341373
args,
342374
...rest
@@ -360,6 +392,12 @@ export function decodeTraceEvent({
360392
return { ...rest, args: processedArgs } as UserTimingTraceEvent;
361393
}
362394

395+
/**
396+
* Encodes a UserTimingTraceEvent to raw format with JSON string details.
397+
* Converts object details to JSON strings for storage/transmission.
398+
* @param event - UserTimingTraceEvent with object details
399+
* @returns TraceEventRaw with string-encoded details
400+
*/
363401
export function encodeTraceEvent({
364402
args,
365403
...rest

0 commit comments

Comments
 (0)