Skip to content

Commit

Permalink
fix: update types in tests, update timing of tests
Browse files Browse the repository at this point in the history
  • Loading branch information
xnanodax committed Dec 10, 2024
1 parent 8e659f9 commit a7ce230
Show file tree
Hide file tree
Showing 7 changed files with 675 additions and 304 deletions.
18 changes: 9 additions & 9 deletions src/v3/recordingComputeUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type { SpanAndAnnotation, SpanAnnotation } from './spanAnnotationTypes'
import type { Span } from './spanTypes'
import type { CompleteTraceDefinition, Timestamp } from './types'

type ScopeBase = Record<string, unknown>
type AnyScope = Record<string, unknown>

describe('recordingComputeUtils', () => {
const EPOCH_START = 1_000
Expand All @@ -18,7 +18,7 @@ describe('recordingComputeUtils', () => {
})

const createAnnotation = (
span: Span<ScopeBase>,
span: Span<AnyScope>,
traceStartTime: Timestamp,
partial: Partial<SpanAnnotation> = {},
): SpanAnnotation => ({
Expand All @@ -31,7 +31,7 @@ describe('recordingComputeUtils', () => {
...partial,
})

const createMockSpan = <TSpan extends Span<ScopeBase>>(
const createMockSpan = <TSpan extends Span<AnyScope>>(
startTimeNow: number,
partial: Partial<TSpan>,
): TSpan =>
Expand All @@ -57,11 +57,11 @@ describe('recordingComputeUtils', () => {
...partial,
}) as TSpan

const createMockSpanAndAnnotation = <TSpan extends Span<ScopeBase>>(
const createMockSpanAndAnnotation = <TSpan extends Span<AnyScope>>(
startTimeNow: number,
spanPartial: Partial<TSpan> = {},
annotationPartial: Partial<SpanAnnotation> = {},
): SpanAndAnnotation<ScopeBase> => {
): SpanAndAnnotation<AnyScope> => {
const span = createMockSpan<TSpan>(startTimeNow, spanPartial)
return {
span,
Expand All @@ -72,7 +72,7 @@ describe('recordingComputeUtils', () => {
const onEnd = jest.fn()

describe('error status propagation', () => {
const baseDefinition: CompleteTraceDefinition<ScopeBase, ScopeBase> = {
const baseDefinition: CompleteTraceDefinition<AnyScope, AnyScope> = {
name: 'test-trace',
scopes: [],
requiredToEnd: [() => true],
Expand Down Expand Up @@ -194,7 +194,7 @@ describe('recordingComputeUtils', () => {
})

describe('getComputedSpans', () => {
const baseDefinition: CompleteTraceDefinition<ScopeBase, ScopeBase> = {
const baseDefinition: CompleteTraceDefinition<AnyScope, AnyScope> = {
name: 'test-trace',
scopes: [],
requiredToEnd: [() => true],
Expand Down Expand Up @@ -231,7 +231,7 @@ describe('recordingComputeUtils', () => {
})

it('should handle operation-start and operation-end special matchers', () => {
const definition: CompleteTraceDefinition<ScopeBase, ScopeBase> = {
const definition: CompleteTraceDefinition<AnyScope, AnyScope> = {
...baseDefinition,
computedSpanDefinitions: [
{
Expand Down Expand Up @@ -261,7 +261,7 @@ describe('recordingComputeUtils', () => {
})

describe('getComputedValues', () => {
const baseDefinition: CompleteTraceDefinition<ScopeBase, ScopeBase> = {
const baseDefinition: CompleteTraceDefinition<AnyScope, AnyScope> = {
name: 'test-trace',
scopes: [],
requiredToEnd: [() => true],
Expand Down
35 changes: 23 additions & 12 deletions src/v3/test/fixtures.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ScopeBase, TraceDefinition } from '../types'
import type { TraceDefinition } from '../types'

export interface TicketIdScope {
ticketId: string
Expand All @@ -7,10 +7,15 @@ export interface UserIdScope {
userId: string
}

export type FixtureAllPossibleScopes = TicketIdScope | UserIdScope

const TICKET_DISPOSE_EVENT_NAME = `ticket.dispose`
const TICKET_NAVIGATED_AWAY_EVENT_NAME = `ticket.navigated-away`

export const ticketActivationDefinition: TraceDefinition<TicketIdScope> = {
export const ticketActivationDefinition: TraceDefinition<
TicketIdScope,
FixtureAllPossibleScopes
> = {
name: 'ticket.activation',
type: 'operation',
requiredScopeKeys: ['ticketId'],

Check failure on line 21 in src/v3/test/fixtures.ts

View workflow job for this annotation

GitHub Actions / Test on node 20

Object literal may only specify known properties, and 'requiredScopeKeys' does not exist in type 'TraceDefinition<TicketIdScope, FixtureAllPossibleScopes>'.

Check failure on line 21 in src/v3/test/fixtures.ts

View workflow job for this annotation

GitHub Actions / Test on node 20

Object literal may only specify known properties, and 'requiredScopeKeys' does not exist in type 'TraceDefinition<TicketIdScope, FixtureAllPossibleScopes>'.
Expand All @@ -20,30 +25,36 @@ export const ticketActivationDefinition: TraceDefinition<TicketIdScope> = {
{
type: 'component-render',
name: 'OmniLog',
scopes: ['ticketId'],
matchScopes: ['ticketId'],
isIdle: true,
},
],
interruptOn: [
{ type: 'mark', name: TICKET_DISPOSE_EVENT_NAME, scopes: ['ticketId'] },
{
type: 'mark',
name: TICKET_DISPOSE_EVENT_NAME,
matchScopes: ['ticketId'],
},
{
type: 'mark',
name: TICKET_NAVIGATED_AWAY_EVENT_NAME,
scopes: ['ticketId'],
matchScopes: ['ticketId'],
},
],
debounceOn: [
// debounce on anything that has matching ticketId scope:
{ scopes: ['ticketId'] },
{ matchScopes: ['ticketId'] },
// TODO: { type: 'measure', name: (name, scope) => `ticket/${scope.ticketId}/open` },
// metric from ember: ticket_workspace.module.js
({ span }, scope) =>
span.type === 'measure' && span.name === `ticket/${scope.ticketId}/open`,
({ span }) =>
span.type === 'measure' &&
span.name === `ticket/${span.scope?.ticketId}/open`,
// debounce on element timing sentinels:
({ span }, scope) =>
({ span }) =>
span.type === 'element' &&
span.name === `ticket_workspace/${span.scope?.ticketId}`,
({ span }) =>
span.type === 'element' &&
span.name === `ticket_workspace/${scope.ticketId}`,
({ span }, scope) =>
span.type === 'element' && span.name === `omnilog/${scope.ticketId}`,
span.name === `omnilog/${span.scope?.ticketId}`,
],
}
65 changes: 52 additions & 13 deletions src/v3/test/makeTimeline.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
export interface ComponentRenderStub {
/* eslint-disable @typescript-eslint/no-explicit-any */
import { ensureTimestamp } from '../ensureTimestamp'
import type {
ComponentRenderSpan,
PerformanceEntrySpan,
Span,
SpanType,
} from '../spanTypes'

export interface ComponentRenderStub
extends Partial<Omit<ComponentRenderSpan<any>, 'startTime' | 'duration'>> {
// TODO: double check what this is
entryType: 'component-render'
duration: number
Expand All @@ -13,7 +23,8 @@ export interface LongTaskStub {
name?: string
}

export interface MarkStub {
export interface MarkStub
extends Partial<Omit<PerformanceEntrySpan<any>, 'startTime' | 'duration'>> {
entryType: 'mark'
name: string
startTime?: number
Expand All @@ -29,17 +40,25 @@ export interface IdleStub {
duration: number
}

export type Stub = ComponentRenderStub | LongTaskStub | MarkStub | FmpStub | IdleStub
export type Stub =
| ComponentRenderStub
| LongTaskStub
| MarkStub
| FmpStub
| IdleStub

export const Render = (
name: string,
duration: number,
options: { start?: number, isIdle?: boolean } = {},
options: { startTime?: number } & Partial<
Omit<ComponentRenderSpan<any>, 'startTime' | 'duration'>
> = {},
): ComponentRenderStub => ({
entryType: 'component-render',
duration,
startTime: options.start,
name,
duration,
startTime: options.startTime,
...options,
})

export const LongTask = (
Expand Down Expand Up @@ -108,11 +127,11 @@ export function makeEntries(events: Stub[]): {
return { entries, fmpTime }
}

export function getEventsFromTimeline(
export function getSpansFromTimeline<AllPossibleScopesT>(
_: TemplateStringsArray,
...exprs: (Stub | number)[]
): { entries: PerformanceEntry[]; fmpTime: number | null } {
const entries: PerformanceEntry[] = []
): { spans: Span<AllPossibleScopesT>[]; fmpTime: number | null } {
const spans: Span<AllPossibleScopesT>[] = []
let fmpTime: number | null = null

const stubs = exprs.filter((expr) => typeof expr !== 'number')
Expand Down Expand Up @@ -143,15 +162,35 @@ export function getEventsFromTimeline(
if (stub.entryType === 'fmp') {
fmpTime = currentTime
}
entries.push({
spans.push({
type: stub.entryType as SpanType,
duration: 0,
name: `${stub.entryType}`,
...stub,
startTime: currentTime,
} as PerformanceEntry)
startTime: {
now: 'startTime' in stub ? stub.startTime ?? currentTime : currentTime,
epoch:
'startTime' in stub ? stub.startTime ?? currentTime : currentTime,
},
isIdle: 'name' in stub ? stub.name?.includes('idle') : undefined,
renderedOutput:
'name' in stub
? stub.name?.includes('idle')
? 'content'
: 'loading'
: undefined,
performanceEntry: {
duration: 0,
name: `${stub.entryType}`,
...stub,
startTime:
'startTime' in stub ? stub.startTime ?? currentTime : currentTime,
toJSON: () => {},
},
} as Span<AllPossibleScopesT>)
}

return { entries, fmpTime }
return { spans, fmpTime }
}

// example usage
Expand Down
29 changes: 0 additions & 29 deletions src/v3/test/processEntries.ts

This file was deleted.

13 changes: 13 additions & 0 deletions src/v3/test/processSpans.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Span } from '../spanTypes'
import { TraceManager } from '../traceManager'

function processSpans<ScopeT>(
spans: Span<ScopeT>[],
traceManager: TraceManager<ScopeT>,

Check failure on line 6 in src/v3/test/processSpans.ts

View workflow job for this annotation

GitHub Actions / Test on node 20

Type 'ScopeT' does not satisfy the constraint '{ [K in keyof ScopeT]: ScopeValue; }'.

Check failure on line 6 in src/v3/test/processSpans.ts

View workflow job for this annotation

GitHub Actions / Test on node 20

Type 'ScopeT' does not satisfy the constraint '{ [K in keyof ScopeT]: ScopeValue; }'.
) {
spans.forEach((span, i) => {
traceManager.processSpan(span)
})
}

export default processSpans
Loading

0 comments on commit a7ce230

Please sign in to comment.