refactor(editor): Use typed-mocks to speed up tests and type-checking (no-changelog) (#9796)

This commit is contained in:
कारतोफ्फेलस्क्रिप्ट™
2024-06-19 07:41:27 +02:00
committed by GitHub
parent 41e06be6fd
commit e3e20b48eb
28 changed files with 444 additions and 8684 deletions

View File

@@ -1,59 +1,4 @@
import type { INodeTypeData, INodeTypeDescription, IN8nUISettings } from 'n8n-workflow';
import {
AGENT_NODE_TYPE,
SET_NODE_TYPE,
CHAT_TRIGGER_NODE_TYPE,
MANUAL_TRIGGER_NODE_TYPE,
} from '@/constants';
import nodeTypesJson from '../../../nodes-base/dist/types/nodes.json';
import aiNodeTypesJson from '../../../@n8n/nodes-langchain/dist/types/nodes.json';
const allNodeTypes = [...nodeTypesJson, ...aiNodeTypesJson];
export function findNodeTypeDescriptionByName(name: string): INodeTypeDescription {
return allNodeTypes.find((node) => node.name === name) as INodeTypeDescription;
}
export const testingNodeTypes: INodeTypeData = {
[MANUAL_TRIGGER_NODE_TYPE]: {
sourcePath: '',
type: {
description: findNodeTypeDescriptionByName(MANUAL_TRIGGER_NODE_TYPE),
},
},
[SET_NODE_TYPE]: {
sourcePath: '',
type: {
description: findNodeTypeDescriptionByName(SET_NODE_TYPE),
},
},
[CHAT_TRIGGER_NODE_TYPE]: {
sourcePath: '',
type: {
description: findNodeTypeDescriptionByName(CHAT_TRIGGER_NODE_TYPE),
},
},
[AGENT_NODE_TYPE]: {
sourcePath: '',
type: {
description: findNodeTypeDescriptionByName(AGENT_NODE_TYPE),
},
},
};
export const defaultMockNodeTypes: INodeTypeData = {
[MANUAL_TRIGGER_NODE_TYPE]: testingNodeTypes[MANUAL_TRIGGER_NODE_TYPE],
[SET_NODE_TYPE]: testingNodeTypes[SET_NODE_TYPE],
};
export function mockNodeTypesToArray(nodeTypes: INodeTypeData): INodeTypeDescription[] {
return Object.values(nodeTypes).map(
(nodeType) => nodeType.type.description as INodeTypeDescription,
);
}
export const defaultMockNodeTypesArray: INodeTypeDescription[] =
mockNodeTypesToArray(defaultMockNodeTypes);
import type { IN8nUISettings } from 'n8n-workflow';
export const defaultSettings: IN8nUISettings = {
allowedModules: {},

View File

@@ -2,48 +2,76 @@ import type {
INodeType,
INodeTypeData,
INodeTypes,
IVersionedNodeType,
IConnections,
IDataObject,
INode,
IPinData,
IWorkflowSettings,
LoadedClass,
INodeTypeDescription,
} from 'n8n-workflow';
import { NodeHelpers, Workflow } from 'n8n-workflow';
import { uuid } from '@jsplumb/util';
import { defaultMockNodeTypes } from '@/__tests__/defaults';
import type { INodeUi, ITag, IUsedCredential, IWorkflowDb, WorkflowMetadata } from '@/Interface';
import type { ProjectSharingData } from '@/types/projects.types';
import type { RouteLocationNormalized } from 'vue-router';
import { mock } from 'vitest-mock-extended';
export function createTestNodeTypes(data: INodeTypeData = {}): INodeTypes {
const nodeTypes = {
...defaultMockNodeTypes,
...Object.keys(data).reduce<INodeTypeData>((acc, key) => {
acc[key] = data[key];
import {
AGENT_NODE_TYPE,
CHAT_TRIGGER_NODE_TYPE,
CODE_NODE_TYPE,
EXECUTABLE_TRIGGER_NODE_TYPES,
MANUAL_TRIGGER_NODE_TYPE,
NO_OP_NODE_TYPE,
SET_NODE_TYPE,
} from '@/constants';
return acc;
}, {}),
};
const mockNode = (name: string, type: string, props: Partial<INode> = {}) =>
mock<INode>({ name, type, ...props });
function getKnownTypes(): IDataObject {
return {};
}
const mockLoadedClass = (name: string) =>
mock<LoadedClass<INodeType>>({
type: mock<INodeType>({
// @ts-expect-error
description: mock<INodeTypeDescription>({
name,
displayName: name,
version: 1,
properties: [],
group: EXECUTABLE_TRIGGER_NODE_TYPES.includes(name) ? ['trigger'] : [],
inputs: ['main'],
outputs: ['main'],
documentationUrl: 'https://docs',
webhooks: undefined,
}),
}),
});
function getByName(nodeType: string): INodeType | IVersionedNodeType {
return nodeTypes[nodeType].type;
}
export const mockNodes = [
mockNode('Manual Trigger', MANUAL_TRIGGER_NODE_TYPE),
mockNode('Set', SET_NODE_TYPE),
mockNode('Code', CODE_NODE_TYPE),
mockNode('Rename', SET_NODE_TYPE),
mockNode('Chat Trigger', CHAT_TRIGGER_NODE_TYPE),
mockNode('Agent', AGENT_NODE_TYPE),
mockNode('End', NO_OP_NODE_TYPE),
];
function getByNameAndVersion(nodeType: string, version?: number): INodeType {
return NodeHelpers.getVersionedNodeType(getByName(nodeType), version);
}
export const defaultNodeTypes = mockNodes.reduce<INodeTypeData>((acc, { type }) => {
acc[type] = mockLoadedClass(type);
return acc;
}, {});
return {
getKnownTypes,
getByName,
getByNameAndVersion,
};
}
export const defaultNodeDescriptions = Object.values(defaultNodeTypes).map(
({ type }) => type.description,
) as INodeTypeDescription[];
const nodeTypes = mock<INodeTypes>({
getByName(nodeType) {
return defaultNodeTypes[nodeType].type;
},
getByNameAndVersion(nodeType: string, version?: number): INodeType {
return NodeHelpers.getVersionedNodeType(defaultNodeTypes[nodeType].type, version);
},
});
export function createTestWorkflowObject({
id = uuid(),
@@ -51,7 +79,6 @@ export function createTestWorkflowObject({
nodes = [],
connections = {},
active = false,
nodeTypes = {},
staticData = {},
settings = {},
pinData = {},
@@ -61,7 +88,6 @@ export function createTestWorkflowObject({
nodes?: INode[];
connections?: IConnections;
active?: boolean;
nodeTypes?: INodeTypeData;
staticData?: IDataObject;
settings?: IWorkflowSettings;
pinData?: IPinData;
@@ -75,38 +101,10 @@ export function createTestWorkflowObject({
staticData,
settings,
pinData,
nodeTypes: createTestNodeTypes(nodeTypes),
nodeTypes,
});
}
export function createTestWorkflow(options: {
id?: string;
name: string;
active?: boolean;
createdAt?: number | string;
updatedAt?: number | string;
nodes?: INodeUi[];
connections?: IConnections;
settings?: IWorkflowSettings;
tags?: ITag[] | string[];
pinData?: IPinData;
sharedWithProjects?: ProjectSharingData[];
homeProject?: ProjectSharingData;
versionId?: string;
usedCredentials?: IUsedCredential[];
meta?: WorkflowMetadata;
}): IWorkflowDb {
return {
...options,
createdAt: options.createdAt ?? '',
updatedAt: options.updatedAt ?? '',
versionId: options.versionId ?? '',
id: options.id ?? uuid(),
active: options.active ?? false,
connections: options.connections ?? {},
} as IWorkflowDb;
}
export function createTestNode(node: Partial<INode> = {}): INode {
return {
id: uuid(),
@@ -118,27 +116,3 @@ export function createTestNode(node: Partial<INode> = {}): INode {
...node,
};
}
export function createTestRouteLocation({
path = '',
params = {},
fullPath = path,
hash = '',
matched = [],
redirectedFrom = undefined,
name = path,
meta = {},
query = {},
}: Partial<RouteLocationNormalized> = {}): RouteLocationNormalized {
return {
path,
params,
fullPath,
hash,
matched,
redirectedFrom,
name,
meta,
query,
};
}