Skip to content

Commit b8cd75a

Browse files
authored
Add types and tests for isLocalActivityMarkerEvent (#994)
* Add types and tests for isLocalActivityMarkerEvent Removes explicit `any` type for `isLocalActivityMarkerEvent`. Adds unit tests for `isLocalActivityMarkerEvent`. @Alex-Tideman, question for you: should we iterate over the payloads to see if there are _any_ with `ActivityType` or `activity_type`? Add types and tests for isLocalActivityMarkerEvent Removes explicit `any` type for `isLocalActivityMarkerEvent`. Adds unit tests for `isLocalActivityMarkerEvent`. @Alex-Tideman, question for you: should we iterate over the payloads to see if there are _any_ with `ActivityType` or `activity_type`? Loosen type to just what we care about * Use the new createEvent helper * Simplify to just use the marker name * Refactor types * Refactor types
1 parent 579348a commit b8cd75a

File tree

7 files changed

+135
-28
lines changed

7 files changed

+135
-28
lines changed

Diff for: src/events.d.ts

+36-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,39 @@
1-
type HistoryEvent = import('$types').HistoryEvent;
2-
type PendingActivityInfo = import('$types').PendingActivityInfo;
1+
type EventHistory = Replace<
2+
import('$types').History,
3+
{ events: HistoryEvent[] }
4+
>;
5+
6+
type HistoryEvent = Replace<
7+
import('$types').HistoryEvent,
8+
{ eventType: EventType }
9+
>;
10+
11+
type GetWorkflowExecutionHistoryResponse = Replace<
12+
import('$types').GetWorkflowExecutionHistoryResponse,
13+
{ history: EventHistory }
14+
>;
15+
16+
type PendingActivityInfo = Replace<
17+
import('$types').PendingActivityInfo,
18+
{ activityId: string }
19+
>;
20+
21+
type PendingActivity = Replace<
22+
PendingActivityInfo,
23+
'activityId',
24+
{
25+
id: string;
26+
state: PendingActivityState;
27+
activityType?: { name: string };
28+
}
29+
>;
30+
31+
type PendingActivityState =
32+
| 'Unspecified'
33+
| 'Scheduled'
34+
| 'Started'
35+
| 'CancelRequested';
36+
337
type PendingChildren = import('$types').PendingChildrenInfo;
438

539
type EventRequestMetadata = {
@@ -32,7 +66,6 @@ type EventClassification =
3266

3367
interface WorkflowEvent extends HistoryEvent {
3468
id: string;
35-
eventType: EventType;
3669
attributes: EventAttribute;
3770
timestamp: string;
3871
classification: EventClassification;
@@ -42,12 +75,6 @@ interface WorkflowEvent extends HistoryEvent {
4275

4376
type WorkflowEvents = WorkflowEvent[];
4477

45-
interface PendingActivity extends PendingActivityInfo {
46-
id: typeof PendingActivityInfo.activityId;
47-
state: 'Unspecified' | 'Scheduled' | 'Started' | 'CancelRequested';
48-
activityType: { name: string };
49-
}
50-
5178
type PendingActivityWithMetadata = {
5279
activity: PendingActivity;
5380
} & EventRequestMetadata;

Diff for: src/global.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ type NamespaceItem = {
1212
type Optional<T, K extends keyof T = keyof T> = Omit<T, K> &
1313
Partial<Pick<T, K>>;
1414

15+
type Replace<T, U extends { [key: string]: unknown }> = Omit<T, keyof U> & U;
16+
1517
interface Window {
1618
Prism: {
1719
highlightAll: () => void;

Diff for: src/lib/models/event-history/index.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export async function getEventAttributes(
6060
};
6161
}
6262

63-
const toEvent = async ({
63+
export const toEvent = async ({
6464
historyEvent,
6565
namespace,
6666
settings,
@@ -80,13 +80,13 @@ const toEvent = async ({
8080

8181
return {
8282
...historyEvent,
83-
attributes,
83+
name: eventType,
84+
id,
8485
eventType,
86+
timestamp,
8587
classification,
8688
category,
87-
id,
88-
name: eventType,
89-
timestamp,
89+
attributes,
9090
};
9191
};
9292

Diff for: src/lib/models/workflow-execution.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import { simplifyAttributes } from './event-history/simplify-attributes';
44
const toPendingActivities = (
55
pendingActivity: PendingActivityInfo[] = [],
66
): PendingActivity[] => {
7-
return pendingActivity.map((activity) => {
7+
return pendingActivity.map((activity): PendingActivity => {
88
const attributes = simplifyAttributes(activity, true);
99
const id = activity.activityId;
1010

11-
return { ...attributes, id } as unknown as PendingActivity;
11+
return { ...attributes, id };
1212
});
1313
};
1414

Diff for: src/lib/services/events-service.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
import type { GetWorkflowExecutionHistoryResponse } from '$types';
2-
31
import { paginated } from '$lib/utilities/paginated';
42
import { requestFromAPI } from '$lib/utilities/request-from-api';
53
import { routeForApi } from '$lib/utilities/route-for-api';
64
import { toEventHistory } from '$lib/models/event-history';
7-
import type { EventSortOrder } from '$lib/stores/event-view';
85
import { isSortOrder } from '$lib/utilities/is';
96

7+
import type { EventSortOrder } from '$lib/stores/event-view';
8+
109
export type FetchEventsParameters = NamespaceScopedRequest &
1110
PaginationCallbacks<GetWorkflowExecutionHistoryResponse> & {
1211
workflowId: string;

Diff for: src/lib/utilities/is-event-type.test.ts

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// eslint-disable-next-line @typescript-eslint/triple-slash-reference
2+
/// <reference path="../../events.d.ts" />
3+
4+
import { expect } from 'vitest';
5+
import { describe, it } from 'vitest';
6+
import { isLocalActivityMarkerEvent } from './is-event-type';
7+
import { toEvent } from '../models/event-history';
8+
9+
import type { EventType } from './is-event-type';
10+
11+
const baseEvent = {
12+
id: '1',
13+
eventTime: '2022-12-12T00:17:18.840595463Z',
14+
version: '0',
15+
taskId: '28312355',
16+
} as const;
17+
18+
const workflowTaskStarted: EventType = 'WorkflowTaskStarted';
19+
20+
describe('isLocalActivityMarkerEvent', () => {
21+
it('should return false if it is not a MarkerRecordedEvent', () => {
22+
const event = toEvent({
23+
historyEvent: {
24+
...baseEvent,
25+
eventType: workflowTaskStarted,
26+
workflowTaskStartedEventAttributes: {
27+
scheduledEventId: '10',
28+
identity: '[email protected]@',
29+
requestId: 'ba23ccc5-86f1-46cb-9a6b-a578b2d66ed8',
30+
},
31+
},
32+
namespace: 'default',
33+
settings: {},
34+
accessToken: '',
35+
});
36+
37+
expect(isLocalActivityMarkerEvent(event)).toBe(false);
38+
});
39+
40+
it('should return false if the event does not have "markerRecordedEventAttributes"', () => {
41+
const event = toEvent({
42+
historyEvent: {
43+
...baseEvent,
44+
eventType: 'MarkerRecorded',
45+
workflowTaskStartedEventAttributes: {
46+
scheduledEventId: '10',
47+
identity: '[email protected]@',
48+
requestId: 'ba23ccc5-86f1-46cb-9a6b-a578b2d66ed8',
49+
},
50+
},
51+
namespace: 'default',
52+
settings: {},
53+
accessToken: '',
54+
});
55+
56+
expect(isLocalActivityMarkerEvent(event)).toBe(false);
57+
});
58+
59+
it('should return false the markerName is not "LocalActivity"', () => {
60+
const event = toEvent({
61+
historyEvent: {
62+
...baseEvent,
63+
eventType: 'MarkerRecorded',
64+
markerRecordedEventAttributes: {
65+
markerName: 'Version',
66+
},
67+
},
68+
namespace: 'default',
69+
settings: {},
70+
accessToken: '',
71+
});
72+
73+
expect(isLocalActivityMarkerEvent(event)).toBe(false);
74+
});
75+
});

Diff for: src/lib/utilities/is-event-type.ts

+13-9
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { has } from './has';
2+
13
export type ActivityType = typeof activityEvents[number];
24
export const activityEvents = [
35
'ActivityTaskCanceled',
@@ -131,7 +133,7 @@ export const findAttributesAndKey = (
131133

132134
const hasAttributes =
133135
<T extends EventWithAttributes<EventAttributeKey>>(key: EventAttributeKey) =>
134-
(event: CommonHistoryEvent): event is T => {
136+
(event: IterableEvent | CommonHistoryEvent): event is T => {
135137
return Boolean(event[key]);
136138
};
137139

@@ -325,12 +327,14 @@ export const isUpsertWorkflowSearchAttributesEvent =
325327
'upsertWorkflowSearchAttributesEventAttributes',
326328
);
327329

328-
export const isLocalActivityMarkerEvent = (event) => {
329-
const payload: any =
330-
event?.markerRecordedEventAttributes?.details?.data?.payloads?.[0];
331-
return (
332-
isMarkerRecordedEvent(event) &&
333-
event?.markerRecordedEventAttributes?.markerName === 'LocalActivity' &&
334-
Boolean(payload?.ActivityType ?? payload?.activity_type)
335-
);
330+
export const isLocalActivityMarkerEvent = (
331+
event: IterableEvent | CommonHistoryEvent,
332+
) => {
333+
if (!isMarkerRecordedEvent(event)) return false;
334+
335+
if (event.markerRecordedEventAttributes.markerName !== 'LocalActivity') {
336+
return false;
337+
}
338+
339+
return true;
336340
};

0 commit comments

Comments
 (0)