Skip to content

Commit a7ce230

Browse files
committed
fix: update types in tests, update timing of tests
1 parent 8e659f9 commit a7ce230

7 files changed

+675
-304
lines changed

src/v3/recordingComputeUtils.test.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type { SpanAndAnnotation, SpanAnnotation } from './spanAnnotationTypes'
88
import type { Span } from './spanTypes'
99
import type { CompleteTraceDefinition, Timestamp } from './types'
1010

11-
type ScopeBase = Record<string, unknown>
11+
type AnyScope = Record<string, unknown>
1212

1313
describe('recordingComputeUtils', () => {
1414
const EPOCH_START = 1_000
@@ -18,7 +18,7 @@ describe('recordingComputeUtils', () => {
1818
})
1919

2020
const createAnnotation = (
21-
span: Span<ScopeBase>,
21+
span: Span<AnyScope>,
2222
traceStartTime: Timestamp,
2323
partial: Partial<SpanAnnotation> = {},
2424
): SpanAnnotation => ({
@@ -31,7 +31,7 @@ describe('recordingComputeUtils', () => {
3131
...partial,
3232
})
3333

34-
const createMockSpan = <TSpan extends Span<ScopeBase>>(
34+
const createMockSpan = <TSpan extends Span<AnyScope>>(
3535
startTimeNow: number,
3636
partial: Partial<TSpan>,
3737
): TSpan =>
@@ -57,11 +57,11 @@ describe('recordingComputeUtils', () => {
5757
...partial,
5858
}) as TSpan
5959

60-
const createMockSpanAndAnnotation = <TSpan extends Span<ScopeBase>>(
60+
const createMockSpanAndAnnotation = <TSpan extends Span<AnyScope>>(
6161
startTimeNow: number,
6262
spanPartial: Partial<TSpan> = {},
6363
annotationPartial: Partial<SpanAnnotation> = {},
64-
): SpanAndAnnotation<ScopeBase> => {
64+
): SpanAndAnnotation<AnyScope> => {
6565
const span = createMockSpan<TSpan>(startTimeNow, spanPartial)
6666
return {
6767
span,
@@ -72,7 +72,7 @@ describe('recordingComputeUtils', () => {
7272
const onEnd = jest.fn()
7373

7474
describe('error status propagation', () => {
75-
const baseDefinition: CompleteTraceDefinition<ScopeBase, ScopeBase> = {
75+
const baseDefinition: CompleteTraceDefinition<AnyScope, AnyScope> = {
7676
name: 'test-trace',
7777
scopes: [],
7878
requiredToEnd: [() => true],
@@ -194,7 +194,7 @@ describe('recordingComputeUtils', () => {
194194
})
195195

196196
describe('getComputedSpans', () => {
197-
const baseDefinition: CompleteTraceDefinition<ScopeBase, ScopeBase> = {
197+
const baseDefinition: CompleteTraceDefinition<AnyScope, AnyScope> = {
198198
name: 'test-trace',
199199
scopes: [],
200200
requiredToEnd: [() => true],
@@ -231,7 +231,7 @@ describe('recordingComputeUtils', () => {
231231
})
232232

233233
it('should handle operation-start and operation-end special matchers', () => {
234-
const definition: CompleteTraceDefinition<ScopeBase, ScopeBase> = {
234+
const definition: CompleteTraceDefinition<AnyScope, AnyScope> = {
235235
...baseDefinition,
236236
computedSpanDefinitions: [
237237
{
@@ -261,7 +261,7 @@ describe('recordingComputeUtils', () => {
261261
})
262262

263263
describe('getComputedValues', () => {
264-
const baseDefinition: CompleteTraceDefinition<ScopeBase, ScopeBase> = {
264+
const baseDefinition: CompleteTraceDefinition<AnyScope, AnyScope> = {
265265
name: 'test-trace',
266266
scopes: [],
267267
requiredToEnd: [() => true],

src/v3/test/fixtures.ts

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { ScopeBase, TraceDefinition } from '../types'
1+
import type { TraceDefinition } from '../types'
22

33
export interface TicketIdScope {
44
ticketId: string
@@ -7,10 +7,15 @@ export interface UserIdScope {
77
userId: string
88
}
99

10+
export type FixtureAllPossibleScopes = TicketIdScope | UserIdScope
11+
1012
const TICKET_DISPOSE_EVENT_NAME = `ticket.dispose`
1113
const TICKET_NAVIGATED_AWAY_EVENT_NAME = `ticket.navigated-away`
1214

13-
export const ticketActivationDefinition: TraceDefinition<TicketIdScope> = {
15+
export const ticketActivationDefinition: TraceDefinition<
16+
TicketIdScope,
17+
FixtureAllPossibleScopes
18+
> = {
1419
name: 'ticket.activation',
1520
type: 'operation',
1621
requiredScopeKeys: ['ticketId'],
@@ -20,30 +25,36 @@ export const ticketActivationDefinition: TraceDefinition<TicketIdScope> = {
2025
{
2126
type: 'component-render',
2227
name: 'OmniLog',
23-
scopes: ['ticketId'],
28+
matchScopes: ['ticketId'],
2429
isIdle: true,
2530
},
2631
],
2732
interruptOn: [
28-
{ type: 'mark', name: TICKET_DISPOSE_EVENT_NAME, scopes: ['ticketId'] },
33+
{
34+
type: 'mark',
35+
name: TICKET_DISPOSE_EVENT_NAME,
36+
matchScopes: ['ticketId'],
37+
},
2938
{
3039
type: 'mark',
3140
name: TICKET_NAVIGATED_AWAY_EVENT_NAME,
32-
scopes: ['ticketId'],
41+
matchScopes: ['ticketId'],
3342
},
3443
],
3544
debounceOn: [
3645
// debounce on anything that has matching ticketId scope:
37-
{ scopes: ['ticketId'] },
46+
{ matchScopes: ['ticketId'] },
3847
// TODO: { type: 'measure', name: (name, scope) => `ticket/${scope.ticketId}/open` },
3948
// metric from ember: ticket_workspace.module.js
40-
({ span }, scope) =>
41-
span.type === 'measure' && span.name === `ticket/${scope.ticketId}/open`,
49+
({ span }) =>
50+
span.type === 'measure' &&
51+
span.name === `ticket/${span.scope?.ticketId}/open`,
4252
// debounce on element timing sentinels:
43-
({ span }, scope) =>
53+
({ span }) =>
54+
span.type === 'element' &&
55+
span.name === `ticket_workspace/${span.scope?.ticketId}`,
56+
({ span }) =>
4457
span.type === 'element' &&
45-
span.name === `ticket_workspace/${scope.ticketId}`,
46-
({ span }, scope) =>
47-
span.type === 'element' && span.name === `omnilog/${scope.ticketId}`,
58+
span.name === `omnilog/${span.scope?.ticketId}`,
4859
],
4960
}

src/v3/test/makeTimeline.ts

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
1-
export interface ComponentRenderStub {
1+
/* eslint-disable @typescript-eslint/no-explicit-any */
2+
import { ensureTimestamp } from '../ensureTimestamp'
3+
import type {
4+
ComponentRenderSpan,
5+
PerformanceEntrySpan,
6+
Span,
7+
SpanType,
8+
} from '../spanTypes'
9+
10+
export interface ComponentRenderStub
11+
extends Partial<Omit<ComponentRenderSpan<any>, 'startTime' | 'duration'>> {
212
// TODO: double check what this is
313
entryType: 'component-render'
414
duration: number
@@ -13,7 +23,8 @@ export interface LongTaskStub {
1323
name?: string
1424
}
1525

16-
export interface MarkStub {
26+
export interface MarkStub
27+
extends Partial<Omit<PerformanceEntrySpan<any>, 'startTime' | 'duration'>> {
1728
entryType: 'mark'
1829
name: string
1930
startTime?: number
@@ -29,17 +40,25 @@ export interface IdleStub {
2940
duration: number
3041
}
3142

32-
export type Stub = ComponentRenderStub | LongTaskStub | MarkStub | FmpStub | IdleStub
43+
export type Stub =
44+
| ComponentRenderStub
45+
| LongTaskStub
46+
| MarkStub
47+
| FmpStub
48+
| IdleStub
3349

3450
export const Render = (
3551
name: string,
3652
duration: number,
37-
options: { start?: number, isIdle?: boolean } = {},
53+
options: { startTime?: number } & Partial<
54+
Omit<ComponentRenderSpan<any>, 'startTime' | 'duration'>
55+
> = {},
3856
): ComponentRenderStub => ({
3957
entryType: 'component-render',
40-
duration,
41-
startTime: options.start,
4258
name,
59+
duration,
60+
startTime: options.startTime,
61+
...options,
4362
})
4463

4564
export const LongTask = (
@@ -108,11 +127,11 @@ export function makeEntries(events: Stub[]): {
108127
return { entries, fmpTime }
109128
}
110129

111-
export function getEventsFromTimeline(
130+
export function getSpansFromTimeline<AllPossibleScopesT>(
112131
_: TemplateStringsArray,
113132
...exprs: (Stub | number)[]
114-
): { entries: PerformanceEntry[]; fmpTime: number | null } {
115-
const entries: PerformanceEntry[] = []
133+
): { spans: Span<AllPossibleScopesT>[]; fmpTime: number | null } {
134+
const spans: Span<AllPossibleScopesT>[] = []
116135
let fmpTime: number | null = null
117136

118137
const stubs = exprs.filter((expr) => typeof expr !== 'number')
@@ -143,15 +162,35 @@ export function getEventsFromTimeline(
143162
if (stub.entryType === 'fmp') {
144163
fmpTime = currentTime
145164
}
146-
entries.push({
165+
spans.push({
166+
type: stub.entryType as SpanType,
147167
duration: 0,
148168
name: `${stub.entryType}`,
149169
...stub,
150-
startTime: currentTime,
151-
} as PerformanceEntry)
170+
startTime: {
171+
now: 'startTime' in stub ? stub.startTime ?? currentTime : currentTime,
172+
epoch:
173+
'startTime' in stub ? stub.startTime ?? currentTime : currentTime,
174+
},
175+
isIdle: 'name' in stub ? stub.name?.includes('idle') : undefined,
176+
renderedOutput:
177+
'name' in stub
178+
? stub.name?.includes('idle')
179+
? 'content'
180+
: 'loading'
181+
: undefined,
182+
performanceEntry: {
183+
duration: 0,
184+
name: `${stub.entryType}`,
185+
...stub,
186+
startTime:
187+
'startTime' in stub ? stub.startTime ?? currentTime : currentTime,
188+
toJSON: () => {},
189+
},
190+
} as Span<AllPossibleScopesT>)
152191
}
153192

154-
return { entries, fmpTime }
193+
return { spans, fmpTime }
155194
}
156195

157196
// example usage

src/v3/test/processEntries.ts

Lines changed: 0 additions & 29 deletions
This file was deleted.

src/v3/test/processSpans.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Span } from '../spanTypes'
2+
import { TraceManager } from '../traceManager'
3+
4+
function processSpans<ScopeT>(
5+
spans: Span<ScopeT>[],
6+
traceManager: TraceManager<ScopeT>,
7+
) {
8+
spans.forEach((span, i) => {
9+
traceManager.processSpan(span)
10+
})
11+
}
12+
13+
export default processSpans

0 commit comments

Comments
 (0)