10000 Add types and tests for isLocalActivityMarkerEvent by stevekinney · Pull Request #994 · temporalio/ui · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add types and tests for isLocalActivityMarkerEvent #994

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 36 additions & 9 deletions src/events.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,39 @@
type HistoryEvent = import('$types').HistoryEvent;
type PendingActivityInfo = import('$types').PendingActivityInfo;
type EventHistory = Replace<
import('$types').History,
{ events: HistoryEvent[] }
>;

type HistoryEvent = Replace<
import('$types').HistoryEvent,
{ eventType: EventType }
>;

type GetWorkflowExecutionHistoryResponse = Replace<
import('$types').GetWorkflowExecutionHistoryResponse,
{ history: EventHistory }
>;

type PendingActivityInfo = Replace<
import('$types').PendingActivityInfo,
{ activityId: string }
>;

type PendingActivity = Replace<
PendingActivityInfo,
'activityId',
{
id: string;
state: PendingActivityState;
activityType?: { name: string };
}
>;

type PendingActivityState =
| 'Unspecified'
| 'Scheduled'
| 'Started'
| 'CancelRequested';

type PendingChildren = import('$types').PendingChildrenInfo;

type EventRequestMetadata = {
Expand Down Expand Up @@ -32,7 +66,6 @@ type EventClassification =

interface WorkflowEvent extends HistoryEvent {
id: string;
eventType: EventType;
attributes: EventAttribute;
timestamp: string;
classification: EventClassification;
Expand All @@ -42,12 +75,6 @@ interface WorkflowEvent extends HistoryEvent {

type WorkflowEvents = WorkflowEvent[];

interface PendingActivity extends PendingActivityInfo {
id: typeof PendingActivityInfo.activityId;
state: 'Unspecified' | 'Scheduled' | 'Started' | 'CancelRequested';
activityType: { name: string };
}

type PendingActivityWithMetadata = {
activity: PendingActivity;
} & EventRequestMetadata;
Expand Down
2 changes: 2 additions & 0 deletions src/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ type NamespaceItem = {
type Optional<T, K extends keyof T = keyof T> = Omit<T, K> &
Partial<Pick<T, K>>;

type Replace<T, U extends { [key: string]: unknown }> = Omit<T, keyof U> & U;

interface Window {
Prism: {
highlightAll: () => void;
Expand Down
10 changes: 5 additions & 5 deletions src/lib/models/event-history/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export async function getEventAttributes(
};
}

const toEvent = async ({
export const toEvent = async ({
historyEvent,
namespace,
settings,
Expand All @@ -80,13 +80,13 @@ const toEvent = async ({

return {
...historyEvent,
attributes,
name: eventType,
id,
eventType,
timestamp,
classification,
category,
id,
name: eventType,
timestamp,
attributes,
};
};

Expand Down
4 changes: 2 additions & 2 deletions src/lib/models/workflow-execution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import { simplifyAttributes } from './event-history/simplify-attributes';
const toPendingActivities = (
pendingActivity: PendingActivityInfo[] = [],
): PendingActivity[] => {
return pendingActivity.map((activity) => {
return pendingActivity.map((activity): PendingActivity => {
const attributes = simplifyAttributes(activity, true);
const id = activity.activityId;

return { ...attributes, id } as unknown as PendingActivity;
return { ...attributes, id };
});
};

Expand Down
5 changes: 2 additions & 3 deletions src/lib/services/events-service.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import type { GetWorkflowExecutionHistoryResponse } from '$types';

import { paginated } from '$lib/utilities/paginated';
import { requestFromAPI } from '$lib/utilities/request-from-api';
import { routeForApi } from '$lib/utilities/route-for-api';
import { toEventHistory } from '$lib/models/event-history';
import type { EventSortOrder } from '$lib/stores/event-view';
import { isSortOrder } from '$lib/utilities/is';

import type { EventSortOrder } from '$lib/stores/event-view';

export type FetchEventsParameters = NamespaceScopedRequest &
PaginationCallbacks<GetWorkflowExecutionHistoryResponse> & {
workflowId: string;
Expand Down
75 changes: 75 additions & 0 deletions src/lib/utilities/is-event-type.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// eslint-disable-next-line @typescript-eslint/triple-slash-reference
/// <reference path="../../events.d.ts" />

import { expect } from 'vitest';
import { describe, it } from 'vitest';
import { isLocalActivityMarkerEvent } from './is-event-type';
import { toEvent } from '../models/event-history';

import type { EventType } from './is-event-type';

const baseEvent = {
id: '1',
eventTime: '2022-12-12T00:17:18.840595463Z',
version: '0',
taskId: '28312355',
} as const;

const workflowTaskStarted: EventType = 'WorkflowTaskStarted';

describe('isLocalActivityMarkerEvent', () => {
it('should return false if it is not a MarkerRecordedEvent', () => {
const event = toEvent({
historyEvent: {
...baseEvent,
eventType: workflowTaskStarted,
workflowTaskStartedEventAttributes: {
scheduledEventId: '10',
identity: '50509@MacBook-Pro-2.lan1@',
requestId: 'ba23ccc5-86f1-46cb-9a6b-a578b2d66ed8',
},
},
namespace: 'default',
settings: {},
accessToken: '',
});

expect(isLocalActivityMarkerEvent(event)).toBe(false);
});

it('should return false if the event does not have "markerRecordedEventAttributes"', () => {
const event = toEvent({
historyEvent: {
...baseEvent,
eventType: 'MarkerRecorded',
workflowTaskStartedEventAttributes: {
scheduledEventId: '10',
identity: '50509@MacBook-Pro-2.lan1@',
requestId: 'ba23ccc5-86f1-46cb-9a6b-a578b2d66ed8',
},
},
namespace: 'default',
settings: {},
accessToken: '',
});

expect(isLocalActivityMarkerEvent(event)).toBe(false);
});

it('should return false the markerName is not "LocalActivity"', () => {
const event = toEvent({
historyEvent: {
...baseEvent,
eventType: 'MarkerRecorded',
markerRecordedEventAttributes: {
markerName: 'Version',
},
},
namespace: 'default',
settings: {},
accessToken: '',
});

expect(isLocalActivityMarkerEvent(event)).toBe(false);
});
});
22 changes: 13 additions & 9 deletions src/lib/utilities/is-event-type.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { has } from './has';

export type ActivityType = typeof activityEvents[number];
export const activityEvents = [
'ActivityTaskCanceled',
Expand Down Expand Up @@ -131,7 +133,7 @@ export const findAttributesAndKey = (

const hasAttributes =
<T extends EventWithAttributes<EventAttributeKey>>(key: EventAttributeKey) =>
(event: CommonHistoryEvent): event is T => {
(event: IterableEvent | CommonHistoryEvent): event is T => {
return Boolean(event[key]);
};

Expand Down Expand Up @@ -325,12 +327,14 @@ export const isUpsertWorkflowSearchAttributesEvent =
'upsertWorkflowSearchAttributesEventAttributes',
);

export const isLocalActivityMarkerEvent = (event) => {
const payload: any =
event?.markerRecordedEventAttributes?.details?.data?.payloads?.[0];
return (
isMarkerRecordedEvent(event) &&
event?.markerRecordedEventAttributes?.markerName === 'LocalActivity' &&
Boolean(payload?.ActivityType ?? payload?.activity_type)
);
export const isLocalActivityMarkerEvent = (
event: IterableEvent | CommonHistoryEvent,
) => {
if (!isMarkerRecordedEvent(event)) return false;

if (event.markerRecordedEventAttributes.markerName !== 'LocalActivity') {
return false;
}

return true;
};
0