refactor(core): Avoid passing around static state like default timezone (no-changelog) (#7221)

This commit is contained in:
कारतोफ्फेलस्क्रिप्ट™
2023-10-27 14:17:52 +02:00
committed by GitHub
parent 62c096710f
commit 35bb42c1b9
31 changed files with 76 additions and 224 deletions

View File

@@ -3,7 +3,6 @@
*/
import { DateTime, Duration, Interval } from 'luxon';
import { Expression } from '@/Expression';
import { Workflow } from '@/Workflow';
import * as Helpers from './Helpers';
import type { ExpressionTestEvaluation, ExpressionTestTransform } from './ExpressionFixtures/base';
@@ -21,6 +20,7 @@ for (const evaluator of ['tmpl', 'tournament'] as const) {
describe('getParameterValue()', () => {
const nodeTypes = Helpers.NodeTypes();
const workflow = new Workflow({
id: '1',
nodes: [
{
name: 'node',
@@ -35,10 +35,10 @@ for (const evaluator of ['tmpl', 'tournament'] as const) {
active: false,
nodeTypes,
});
const expression = new Expression(workflow);
const expression = workflow.expression;
const evaluate = (value: string) =>
expression.getParameterValue(value, null, 0, 0, 'node', [], 'manual', '', {});
expression.getParameterValue(value, null, 0, 0, 'node', [], 'manual', {});
it('should not be able to use global built-ins from denylist', () => {
expect(evaluate('={{document}}')).toEqual({});
@@ -84,9 +84,13 @@ for (const evaluator of ['tmpl', 'tournament'] as const) {
expect(evaluate('={{DateTime.now().toLocaleString()}}')).toEqual(
DateTime.now().toLocaleString(),
);
jest.useFakeTimers({ now: new Date() });
expect(evaluate('={{Interval.after(new Date(), 100)}}')).toEqual(
Interval.after(new Date(), 100),
);
jest.useRealTimers();
expect(evaluate('={{Duration.fromMillis(100)}}')).toEqual(Duration.fromMillis(100));
expect(evaluate('={{new Object()}}')).toEqual(new Object());
@@ -170,6 +174,7 @@ for (const evaluator of ['tmpl', 'tournament'] as const) {
describe('Test all expression value fixtures', () => {
const nodeTypes = Helpers.NodeTypes();
const workflow = new Workflow({
id: '1',
nodes: [
{
name: 'node',
@@ -185,21 +190,11 @@ for (const evaluator of ['tmpl', 'tournament'] as const) {
nodeTypes,
});
const expression = new Expression(workflow);
const expression = workflow.expression;
const evaluate = (value: string, data: INodeExecutionData[]) => {
const itemIndex = data.length === 0 ? -1 : 0;
return expression.getParameterValue(
value,
null,
0,
itemIndex,
'node',
data,
'manual',
'',
{},
);
return expression.getParameterValue(value, null, 0, itemIndex, 'node', data, 'manual', {});
};
for (const t of baseFixtures) {

View File

@@ -3,7 +3,10 @@
*/
import { DateTime } from 'luxon';
import { evaluate, getLocalISOString, TEST_TIMEZONE } from './Helpers';
import { getGlobalState } from '@/GlobalState';
import { evaluate, getLocalISOString } from './Helpers';
const { defaultTimezone } = getGlobalState();
describe('Data Transformation Functions', () => {
describe('Date Data Transformation Functions', () => {
@@ -16,17 +19,17 @@ describe('Data Transformation Functions', () => {
test('.beginningOf("week") should work correctly on a date', () => {
expect(evaluate('={{ DateTime.local(2023, 1, 20).beginningOf("week") }}')).toEqual(
DateTime.local(2023, 1, 16, { zone: TEST_TIMEZONE }),
DateTime.local(2023, 1, 16, { zone: defaultTimezone }),
);
expect(evaluate('={{ new Date(2023, 0, 20).beginningOf("week") }}')).toEqual(
DateTime.local(2023, 1, 16, { zone: TEST_TIMEZONE }).toJSDate(),
DateTime.local(2023, 1, 16, { zone: defaultTimezone }).toJSDate(),
);
});
test('.beginningOf("week") should work correctly on a string', () => {
const evaluatedDate = evaluate('={{ "2023-01-30".toDate().beginningOf("week") }}');
const expectedDate = DateTime.local(2023, 1, 23, { zone: TEST_TIMEZONE }).toJSDate();
const expectedDate = DateTime.local(2023, 1, 23, { zone: defaultTimezone }).toJSDate();
if (evaluatedDate && evaluatedDate instanceof Date) {
expect(evaluatedDate.toDateString()).toEqual(expectedDate.toDateString());
@@ -35,7 +38,7 @@ describe('Data Transformation Functions', () => {
test('.beginningOf("month") should work correctly on a string', () => {
const evaluatedDate = evaluate('={{ "2023-06-16".toDate().beginningOf("month") }}');
const expectedDate = DateTime.local(2023, 6, 1, { zone: TEST_TIMEZONE }).toJSDate();
const expectedDate = DateTime.local(2023, 6, 1, { zone: defaultTimezone }).toJSDate();
if (evaluatedDate && evaluatedDate instanceof Date) {
expect(evaluatedDate.toDateString()).toEqual(expectedDate.toDateString());
@@ -44,7 +47,7 @@ describe('Data Transformation Functions', () => {
test('.beginningOf("year") should work correctly on a string', () => {
const evaluatedDate = evaluate('={{ "2023-01-30".toDate().beginningOf("year") }}');
const expectedDate = DateTime.local(2023, 1, 1, { zone: TEST_TIMEZONE }).toJSDate();
const expectedDate = DateTime.local(2023, 1, 1, { zone: defaultTimezone }).toJSDate();
if (evaluatedDate && evaluatedDate instanceof Date) {
expect(evaluatedDate.toDateString()).toEqual(expectedDate.toDateString());
@@ -53,10 +56,10 @@ describe('Data Transformation Functions', () => {
test('.endOfMonth() should work correctly on a date', () => {
expect(evaluate('={{ DateTime.local(2023, 1, 16).endOfMonth() }}')).toEqual(
DateTime.local(2023, 1, 31, 23, 59, 59, 999, { zone: TEST_TIMEZONE }),
DateTime.local(2023, 1, 31, 23, 59, 59, 999, { zone: defaultTimezone }),
);
expect(evaluate('={{ new Date(2023, 0, 16).endOfMonth() }}')).toEqual(
DateTime.local(2023, 1, 31, 23, 59, 59, 999, { zone: TEST_TIMEZONE }).toJSDate(),
DateTime.local(2023, 1, 31, 23, 59, 59, 999, { zone: defaultTimezone }).toJSDate(),
);
});

View File

@@ -1,10 +1,7 @@
import type { IDataObject } from '@/Interfaces';
import { Expression } from '@/Expression';
import { Workflow } from '@/Workflow';
import * as Helpers from '../Helpers';
export const TEST_TIMEZONE = 'America/New_York';
export const nodeTypes = Helpers.NodeTypes();
export const workflow = new Workflow({
nodes: [
@@ -20,11 +17,8 @@ export const workflow = new Workflow({
connections: {},
active: false,
nodeTypes,
settings: {
timezone: TEST_TIMEZONE,
},
});
export const expression = new Expression(workflow);
export const expression = workflow.expression;
export const evaluate = (value: string, values?: IDataObject[]) =>
expression.getParameterValue(
@@ -35,7 +29,6 @@ export const evaluate = (value: string, values?: IDataObject[]) =>
'node',
values?.map((v) => ({ json: v })) ?? [],
'manual',
TEST_TIMEZONE,
{},
);

View File

@@ -37,6 +37,8 @@ import type { Workflow } from '@/Workflow';
import { WorkflowDataProxy } from '@/WorkflowDataProxy';
import { WorkflowHooks } from '@/WorkflowHooks';
import * as NodeHelpers from '@/NodeHelpers';
import { deepCopy } from '@/utils';
import { getGlobalState } from '@/GlobalState';
export interface INodeTypesObject {
[key: string]: INodeType;
@@ -127,7 +129,6 @@ export function getNodeParameter(
parameterName: string,
itemIndex: number,
mode: WorkflowExecuteMode,
timezone: string,
additionalKeys: IWorkflowDataProxyAdditionalKeys,
executeData: IExecuteData,
fallbackValue?: any,
@@ -153,7 +154,6 @@ export function getNodeParameter(
node.name,
connectionInputData,
mode,
timezone,
additionalKeys,
);
} catch (e) {
@@ -240,7 +240,6 @@ export function getExecuteFunctions(
parameterName,
itemIndex,
mode,
additionalData.timezone,
{},
fallbackValue,
);
@@ -255,7 +254,7 @@ export function getExecuteFunctions(
return additionalData.restApiUrl;
},
getTimezone: (): string => {
return additionalData.timezone;
return workflow.settings.timezone ?? getGlobalState().defaultTimezone;
},
getExecuteData: (): IExecuteData => {
return executeData;
@@ -277,7 +276,6 @@ export function getExecuteFunctions(
connectionInputData,
{},
mode,
additionalData.timezone,
{},
executeData,
);
@@ -421,7 +419,7 @@ export function getExecuteSingleFunctions(
return additionalData.restApiUrl;
},
getTimezone: (): string => {
return additionalData.timezone;
return workflow.settings.timezone ?? getGlobalState().defaultTimezone;
},
getExecuteData: (): IExecuteData => {
return executeData;
@@ -444,7 +442,6 @@ export function getExecuteSingleFunctions(
parameterName,
itemIndex,
mode,
additionalData.timezone,
{},
fallbackValue,
);
@@ -466,7 +463,6 @@ export function getExecuteSingleFunctions(
connectionInputData,
{},
mode,
additionalData.timezone,
{},
executeData,
);
@@ -679,7 +675,6 @@ export function WorkflowExecuteAdditionalData(): IWorkflowExecuteAdditionalData
executeWorkflow: async (workflowInfo: IExecuteWorkflowInfo): Promise<any> => {},
sendDataToUI: (message: string) => {},
restApiUrl: '',
timezone: 'America/New_York',
webhookBaseUrl: 'webhook',
webhookWaitingBaseUrl: 'webhook-waiting',
webhookTestBaseUrl: 'webhook-test',

View File

@@ -1182,7 +1182,6 @@ describe('Workflow', () => {
];
const nodeTypes = Helpers.NodeTypes();
const timezone = 'America/New_York';
for (const testData of tests) {
test(testData.description, () => {
@@ -1310,7 +1309,6 @@ describe('Workflow', () => {
activeNodeName,
connectionInputData,
'manual',
timezone,
{},
);
expect(result).toEqual(testData.output[parameterName]);
@@ -1465,7 +1463,6 @@ describe('Workflow', () => {
activeNodeName,
connectionInputData,
'manual',
timezone,
{},
);

View File

@@ -253,10 +253,9 @@ describe('WorkflowDataProxy', () => {
0,
0,
nameLastNode,
lastNodeConnectionInputData || [],
lastNodeConnectionInputData ?? [],
{},
'manual',
'America/New_York',
{},
executeData,
);